Diff of /HDF5/Dataset.pm [9fd343] .. [abfdfc] Maximize Restore

  Switch to unified view

a/HDF5/Dataset.pm b/HDF5/Dataset.pm
...
...
1114
            my $ReturnType = 'PDL';         # Default object returned is PDL. If strings are store, then this will
1114
            my $ReturnType = 'PDL';         # Default object returned is PDL. If strings are store, then this will
1115
            # return PDL::Char
1115
            # return PDL::Char
1116
            
1116
            
1117
1117
1118
            # Check for string type:
1118
            # Check for string type:
1119
          my $varLenString = 0; # Flag = 1 if reading variable-length string array
1119
            if( PDL::IO::HDF5::H5Tget_class($HDF5type ) == $H5T_STRING ){  # String type
1120
            if( PDL::IO::HDF5::H5Tget_class($HDF5type ) == $H5T_STRING ){  # String type
1120
            
1121
            
1122
                        # Check for variable length string"
1123
                        if( ! PDL::IO::HDF5::H5Tis_variable_str($HDF5type ) ){
1124
                                # Not a variable length string
1121
          $stringSize = PDL::IO::HDF5::H5Tget_size($HDF5type);
1125
                                $stringSize = PDL::IO::HDF5::H5Tget_size($HDF5type);
1122
          unless( $stringSize >= 0 ){
1126
                                unless( $stringSize >= 0 ){
1123
              carp "Error Calling ".__PACKAGE__."::get: Can't get HDF5 String Datatype Size.\n";
1127
                                    carp "Error Calling ".__PACKAGE__."::get: Can't get HDF5 String Datatype Size.\n";
1124
              carp("Can't close Datatype in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
1128
                                    carp("Can't close Datatype in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
1125
              carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
1129
                                    carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
1126
              carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
1130
                                    carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
1127
              return undef;
1131
                                    return undef;
1128
          }
1132
                                }
1129
          
1133
                                $internalhdf5_type =  $HDF5type; # internal storage the same as the file storage.
1134
                        }
1135
                        else{
1136
                                # Variable-length String, set flag
1137
                                $varLenString = 1;
1138
                                
1139
                                # Create variable-length type for reading from the file
1140
                                $internalhdf5_type = PDL::IO::HDF5::H5Tcopy(PDL::IO::HDF5::H5T_C_S1() );
1141
                                PDL::IO::HDF5::H5Tset_size( $internalhdf5_type, PDL::IO::HDF5::H5T_VARIABLE() );
1142
        
1143
                        }
1144
                        
1130
            $PDLtype = $PDL::Types::PDL_B; 
1145
            $PDLtype = $PDL::Types::PDL_B; 
1131
          $internalhdf5_type =  $HDF5type; # internal storage the same as the file storage.
1132
            $typeID=$HDF5type;
1146
            $typeID=$HDF5type;
1133
            $ReturnType = 'PDL::Char';   # For strings, we return a PDL::Char
1147
            $ReturnType = 'PDL::Char';   # For strings, we return a PDL::Char
1134
            
1148
            
1135
            }
1149
            }
1136
            else{  # Normal Numeric Type
1150
            else{  # Normal Numeric Type
...
...
1174
            return undef;
1188
            return undef;
1175
            }
1189
            }
1176
1190
1177
            @dims = PDL::IO::HDF5::unpackList($dims); # get the dim sizes from the binary structure
1191
            @dims = PDL::IO::HDF5::unpackList($dims); # get the dim sizes from the binary structure
1178
            
1192
            
1193
          # Create initial PDL null array with the proper datatype    
1179
            $attrValue = $ReturnType->null;
1194
            $attrValue = $ReturnType->null;
1180
            $attrValue->set_datatype($PDLtype);
1195
            $attrValue->set_datatype($PDLtype);
1196
          
1181
            my @pdldims;  # dims of the PDL
1197
            my @pdldims;  # dims of the PDL
1198
          my $datatypeSize; # Size of one element of data stored
1182
            if( defined( $stringSize )){  # String types
1199
            if( defined( $stringSize )){ # Fixed-Length String types
1183
        
1200
        
1184
            @pdldims = ($stringSize,reverse(@dims)); # HDF5 stores columns/rows in reverse order than pdl,
1201
            @pdldims = ($stringSize,reverse(@dims)); # HDF5 stores columns/rows in reverse order than pdl,
1185
            #  1st PDL dim is the string length (for PDL::Char)
1202
            #  1st PDL dim is the string length (for PDL::Char)
1203
1204
          $datatypeSize = PDL::howbig($attrValue->get_datatype);
1186
            }
1205
            }
1206
          elsif(  $varLenString ){ # Variable-length String
1207
                              # (Variable length string arrays will be converted to fixed-length strings later)
1208
                            @pdldims = (reverse(@dims));      # HDF5 stores columns/rows in reverse order than pdl
1209
                            
1210
                            # Variable length strings are stored as arrays of string pointers, so get that size
1211
                            #   This will by 4 bytes on 32-bit machines, and 8 bytes on 64-bit machines.
1212
                            $datatypeSize = PDL::IO::HDF5::bufPtrSize();
1213
                    }
1187
            else{ # Normal Numeric types
1214
            else{ # Normal Numeric types
1188
            @pdldims = (reverse(@dims));        # HDF5 stores columns/rows in reverse order than pdl,
1215
            @pdldims = (reverse(@dims));        # HDF5 stores columns/rows in reverse order than pdl,
1216
1217
                        $datatypeSize = PDL::howbig($attrValue->get_datatype);
1218
1189
            }
1219
            }
1190
            
1220
            
1191
          $attrValue->setdims(\@pdldims);
1192
            
1221
            
1193
            my $nelems = 1;
1222
            my $nelems = 1;
1194
            foreach (@pdldims){ $nelems *= $_; }; # calculate the number of elements
1223
            foreach (@pdldims){ $nelems *= $_; }; # calculate the number of elements
1195
1224
1196
          my $datasize = $nelems * PDL::howbig($attrValue->get_datatype);
1225
          my $datasize = $nelems * $datatypeSize;
1197
            
1226
            
1198
            # Create empty space for the data
1227
            # Create empty space for the data
1199
            #   Incrementally, to get around problem on win32
1228
            #   Incrementally, to get around problem on win32
1200
          my $howBig = PDL::howbig($attrValue->get_datatype);
1229
          my $howBig = $datatypeSize;
1201
            my $data = ' ' x $howBig;
1230
            my $data = ' ' x $howBig;
1202
            foreach my $dim(@pdldims){
1231
            foreach my $dim(@pdldims){
1203
            $data = $data x $dim;
1232
            $data = $data x $dim;
1204
            }
1233
            }
1205
1234
...
...
1212
            carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
1241
            carp("Can't close DataSpace in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
1213
            carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
1242
            carp("Can't close Attribute in ".__PACKAGE__.":attrGet\n") if( PDL::IO::HDF5::H5Aclose($attrID) < 0);
1214
            return undef;
1243
            return undef;
1215
            }
1244
            }
1216
1245
1217
          # Update the PDL data with the data read from the file
1246
                    if( $varLenString ){ 
1247
                        # Convert variable-length string to fixed-length string, to be compatible with the PDL::Char type
1248
                        my $maxsize = PDL::IO::HDF5::findMaxVarLenSize($data, $nelems);
1249
        
1250
                        # Create empty space for the fixed-length data
1251
                        #   Incrementally, to get around problem on win32
1252
                        my $howBig = $maxsize + 1; # Adding one to include the null string terminator
1253
                        my $fixeddata = ' ' x $howBig;
1254
                        foreach my $dim(@pdldims){
1255
                            $fixeddata = $fixeddata x $dim;
1256
                        }
1257
                        
1258
                        PDL::IO::HDF5::copyVarLenToFixed($data, $fixeddata, $nelems, $maxsize);
1259
                        
1260
                        # Reclaim data from HDF5 system (HDF5 allocates memory when it reads variable-length strings)
1261
                        $rc = PDL::IO::HDF5::H5Dvlen_reclaim ($internalhdf5_type, $dataspaceID, PDL::IO::HDF5::H5P_DEFAULT(), $data);
1262
                        if( $rc < 0 ){
1263
                            carp("Error reclaiming memeory while reading data from file in ".__PACKAGE__.":get\n");
1264
                            carp("Can't close Datatype in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Tclose($HDF5type) < 0);
1265
                            carp("Can't close DataSpace in ".__PACKAGE__.":get\n") if( PDL::IO::HDF5::H5Sclose($dataspaceID) < 0);
1266
                                return undef;
1267
                        }
1268
        
1269
                        # Adjust for fixed-length PDL creation
1270
                        $data = $fixeddata;
1271
                        unshift @pdldims, ($maxsize+1);
1272
                    }
1273
1274
          # Setup the PDL with the proper dimensions and data
1275
                    $attrValue->setdims(\@pdldims);
1218
            ${$attrValue->get_dataref()} = $data;
1276
            ${$attrValue->get_dataref()} = $data;
1219
            $attrValue->upd_data();
1277
            $attrValue->upd_data();
1220
            
1278
            
1221
1279
1222
#************************************************
1280
#************************************************