void HDF5Output::flush() const { hsize_t n = buffer.size(); if (n == 0) return; hid_t file_space = H5Dget_space(dset); hsize_t count = H5Sget_simple_extent_npoints(file_space); // resize dataset hsize_t new_size[RANK] = {count + n}; H5Dset_extent(dset, new_size); // get updated filespace H5Sclose(file_space); file_space = H5Dget_space(dset); hsize_t offset[RANK] = {count}; hsize_t cnt[RANK] = {n}; H5Sselect_hyperslab(file_space, H5S_SELECT_SET, offset, NULL, cnt, NULL); hid_t mspace_id = H5Screate_simple(RANK, cnt, NULL); H5Dwrite(dset, sid, mspace_id, file_space, H5P_DEFAULT, buffer.data()); H5Sclose(mspace_id); H5Sclose(file_space); buffer.clear(); }
// JRC: This fat interface may not scale? What about // scalar attributes? herr_t VsH5Attribute::getDoubleVectorValue(std::vector<double>* dvals) { herr_t err = 0; size_t npoints; hid_t atype = H5Aget_type(getId()); H5T_class_t type = H5Tget_class(atype); hid_t aspace = H5Aget_space(getId()); size_t rank = H5Sget_simple_extent_ndims(aspace); if (type != H5T_FLOAT) { VsLog::warningLog() <<"VsH5Attribute::getDoubleVectorValue() - Requested attribute " <<getShortName() <<" is not a floating point vector." <<std::endl; dvals->resize(0); return -1; } if (rank == 0) { dvals->resize(1); double v; err = H5Aread(getId(), H5T_NATIVE_DOUBLE, &v); (*dvals)[0] = v; return err; } // rank>0 npoints = H5Sget_simple_extent_npoints(aspace); double* v = new double[npoints]; err = H5Aread(getId(), H5T_NATIVE_DOUBLE, v); dvals->resize(npoints); for (size_t i = 0; i<npoints; ++i) { (*dvals)[i] = v[i]; } delete [] v; return err; }
void read_hdf(const std::string &filename, double* &data, int &m_rows, int &m_cols){ hid_t file_id, dataset_id, space_id, property_id; herr_t status; //Create a new file using the default properties. file_id = H5Fopen (filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); dataset_id = H5Dopen(file_id, "x", H5P_DEFAULT); space_id = H5Dget_space(dataset_id); int length = H5Sget_simple_extent_npoints(space_id); hsize_t dims[2]; hsize_t mdims[2]; status = H5Sget_simple_extent_dims(space_id,dims,mdims); m_rows = dims[0]; m_cols = dims[1]; data = new double[length]; status = H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); status = H5Sclose(space_id); status = H5Dclose(dataset_id); status = H5Fclose(file_id); }
/** Append a vector to a specified dataset and return the error status of the write operation. */ herr_t HDF5DataWriter::appendToDataset(hid_t dataset_id, const vector< double >& data) { herr_t status; if (dataset_id < 0){ return -1; } hid_t filespace = H5Dget_space(dataset_id); if (filespace < 0){ return -1; } if (data.size() == 0){ return 0; } hsize_t size = H5Sget_simple_extent_npoints(filespace) + data.size(); status = H5Dset_extent(dataset_id, &size); if (status < 0){ return status; } filespace = H5Dget_space(dataset_id); hsize_t size_increment = data.size(); hid_t memspace = H5Screate_simple(1, &size_increment, NULL); hsize_t start = size - data.size(); H5Sselect_hyperslab(filespace, H5S_SELECT_SET, &start, NULL, &size_increment, NULL); status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, memspace, filespace, H5P_DEFAULT, &data[0]); return status; }
Matrix read_hdf(const std::string &filename){ hid_t file_id, dataset_id, space_id, property_id; herr_t status; //Create a new file using the default properties. file_id = H5Fopen (filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); dataset_id = H5Dopen(file_id, "x", H5P_DEFAULT); space_id = H5Dget_space(dataset_id); int length = H5Sget_simple_extent_npoints(space_id); hsize_t dims[2]; hsize_t mdims[2]; status = H5Sget_simple_extent_dims(space_id,dims,mdims); Matrix tmp(dims[0],dims[1]); status = H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, tmp.ptr()); status = H5Sclose(space_id); status = H5Dclose(dataset_id); status = H5Fclose(file_id); // std::cout << tmp.ptr() << std::endl; return tmp; }
int_f nh5sget_simple_extent_npoints_c ( hid_t_f *space_id , hsize_t_f *npoints ) { int ret_value = 0; hid_t c_space_id; hsize_t c_npoints; c_space_id = *space_id; c_npoints = H5Sget_simple_extent_npoints(c_space_id); if ( c_npoints == 0 ) ret_value = -1; *npoints = (hsize_t_f)c_npoints; return ret_value; }
void ReadStringsT( hid_t iParent, const std::string &iAttrName, size_t iNumStrings, StringT *oStrings ) { ABCA_ASSERT( iParent >= 0, "Invalid parent in ReadStringsT" ); // Open the attribute. hid_t attrId = H5Aopen( iParent, iAttrName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( attrId >= 0, "Couldn't open attribute named: " << iAttrName ); AttrCloser attrCloser( attrId ); // Checking code. { hid_t attrFtype = H5Aget_type( attrId ); DtypeCloser dtypeCloser( attrFtype ); hid_t nativeDtype = GetNativeDtype<CharT>(); ABCA_ASSERT( H5Tget_class( attrFtype ) == H5Tget_class( nativeDtype ) && H5Tget_sign( attrFtype ) == H5Tget_sign( nativeDtype ), "Invalid datatype for stringT" ); } hid_t attrSpace = H5Aget_space( attrId ); ABCA_ASSERT( attrSpace >= 0, "Couldn't get dataspace for attribute: " << iAttrName ); DspaceCloser dspaceCloser( attrSpace ); hssize_t numPoints = H5Sget_simple_extent_npoints( attrSpace ); ABCA_ASSERT( numPoints > 0, "Degenerate string dimensions in ReadStringsT" ); // Create temporary char storage buffer. std::vector<CharT> charStorage( ( size_t )( 1 + numPoints ), ( CharT )0 ); // Read into it. herr_t status = H5Aread( attrId, GetNativeDtype<CharT>(), ( void * )&charStorage.front() ); ABCA_ASSERT( status >= 0, "Couldn't read from attribute: " << iAttrName ); // Extract 'em. ExtractStrings( oStrings, ( const CharT * )&charStorage.front(), 1 + numPoints, iNumStrings ); }
//-***************************************************************************** void ReadTimeSamples( hid_t iParent, std::vector < AbcA::TimeSamplingPtr > & oTimeSamples ) { oTimeSamples.clear(); // add the intrinsic default sampling AbcA::TimeSamplingPtr ts( new AbcA::TimeSampling() ); oTimeSamples.push_back( ts ); uint32_t i = 1; AbcA::TimeSamplingType tst; std::string tstname = "1"; // keep trying to read till we can't find anymore while ( ReadTimeSamplingType( iParent, tstname, tst ) ) { // try to open the time samples attribute std::string timeName = tstname + ".time"; hid_t aid = H5Aopen( iParent, timeName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( aid >= 0, "Couldn't open time samples named: " << timeName ); AttrCloser attrCloser( aid ); // figure out how big it is hid_t sid = H5Aget_space( aid ); ABCA_ASSERT( sid >= 0, "Couldn't get dataspace for time samples: " << timeName ); DspaceCloser dspaceCloser( sid ); hssize_t numPoints = H5Sget_simple_extent_npoints( sid ); ABCA_ASSERT( numPoints > 0, "No time samples data: " << timeName ); std::vector < chrono_t > times(numPoints); // do the read herr_t status = H5Aread( aid, H5T_NATIVE_DOUBLE, &(times.front()) ); ABCA_ASSERT( status >= 0, "Can't read time samples: " << timeName ); // create the TimeSampling and add it to our vector ts.reset( new AbcA::TimeSampling(tst, times) ); oTimeSamples.push_back( ts ); // increment to try and read the next one i++; std::stringstream strm; strm << i; tstname = strm.str(); } }
int main(int argc, char ** argv) { if(argc < 2){ std::cout << "please specify a file" << std::endl; return 0; } std::string filename = argv[1]; herr_t status; //Create a new file using the default properties. hid_t file_id = H5Fopen (filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); hid_t dataset_id = H5Dopen(file_id, "DATASET", H5P_DEFAULT); hid_t space_id = H5Dget_space(dataset_id); int length = H5Sget_simple_extent_npoints(space_id); hsize_t dims[2]; hsize_t mdims[2]; status = H5Sget_simple_extent_dims(space_id,dims,mdims); int m_rows = dims[0]; int m_cols = dims[1]; double * data = new double[m_rows*m_cols]; status = H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); status = H5Sclose(space_id); status = H5Dclose(dataset_id); status = H5Fclose(file_id); // Print to screen for(int r=0; r<m_rows; ++r){ for(int c=0; c<m_cols; ++c){ std::cout << std::setfill (' ') << std::setw (8); std::cout << data[c + r*m_cols]; } std::cout << std::endl; } delete [] data; return 0; }
// Extract information from HDF5 file void get_data(std::string data_path, std::string dataset_name, std::vector<double> &data_out){ // Declare variables const char *fname = data_path.c_str(); hid_t file_id , dataset_id , dataspace_id , file_dataspace_id; hsize_t* dims; hssize_t num_elem; int rank; int ndims; // Open existing HDF5 file file_id = H5Fopen(fname , H5F_ACC_RDONLY , H5P_DEFAULT); // Open existing dataset dataset_id = H5Dopen(file_id , dataset_name.c_str(), H5P_DEFAULT); // Determine dataset parameters file_dataspace_id = H5Dget_space(dataset_id); rank = H5Sget_simple_extent_ndims(file_dataspace_id); dims = (hsize_t*) malloc(rank *sizeof(hsize_t)); ndims = H5Sget_simple_extent_dims(file_dataspace_id, dims, NULL); //std::cout << "rank: " << std::to_string(rank) << std::endl; // Allocate matrix num_elem = H5Sget_simple_extent_npoints(file_dataspace_id); std::vector<double> data_out_tmp(num_elem); // = (double*)malloc(num_elem *sizeof(double)); //std::cout << "num_elem: " << std::to_string(num_elem) << std::endl; // Create dataspace dataspace_id = H5Screate_simple(rank , dims , NULL); // Read matrix data from file H5Dread(dataset_id, H5T_NATIVE_DOUBLE, dataspace_id, file_dataspace_id , H5P_DEFAULT , &data_out_tmp[0]); data_out = data_out_tmp; // Release resources and close file H5Dclose(dataset_id); H5Sclose(dataspace_id); H5Sclose(file_dataspace_id); H5Fclose(file_id); free(dims); }
// JRC: This fat interface may not scale? What about // scalar attributes? herr_t VsH5Attribute::getIntVectorValue(std::vector<int>* ivals) { herr_t err; size_t npoints; hid_t atype = H5Aget_type(getId()); H5T_class_t type = H5Tget_class(atype); hid_t aspace = H5Aget_space(getId()); size_t rank = H5Sget_simple_extent_ndims(aspace); if (type != H5T_INTEGER) { VsLog::warningLog() <<"VsH5Attribute::getIntVectorValue() - Requested attribute " <<getShortName() <<" is not an integer vector." <<std::endl; ivals->resize(0); return -1; } if (rank == 0) { ivals->resize(1); int v; // err = H5Aread(id, atype, &v); err = H5Aread(getId(), H5T_NATIVE_INT, &v); (*ivals)[0] = v; return err; } // rank>0 npoints = H5Sget_simple_extent_npoints(aspace); int* v = new int[npoints]; err = H5Aread(getId(), H5T_NATIVE_INT, v); ivals->resize(npoints); for (size_t i = 0; i<npoints; ++i) { (*ivals)[i] = v[i]; } delete [] v; return err; }
void read(const std::string &filename, std::vector<int> &data) { hid_t file_id, dataset_id, space_id; herr_t status; file_id = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); dataset_id = H5Dopen(file_id, "DATASET", H5P_DEFAULT); space_id = H5Dget_space(dataset_id); int length = H5Sget_simple_extent_npoints(space_id); int * image = new int[length]; status = H5Dread(dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, image); // Copy back to vector data.resize(length); for(int i=0; i<length; ++i){ data[i] = image[i]; } delete [] image; status = H5Sclose(space_id); status = H5Dclose(dataset_id); status = H5Fclose(file_id); }
herr_t H5IMget_palette_info( hid_t loc_id, const char *image_name, int pal_number, hsize_t *pal_dims ) { hid_t did; int has_pal; hid_t atid=-1; hid_t aid; hid_t asid=-1; hssize_t n_refs; hsize_t dim_ref; hobj_ref_t *refbuf; /* buffer to read references */ hid_t pal_id; hid_t pal_space_id; hsize_t pal_maxdims[2]; /* check the arguments */ if (image_name == NULL) return -1; /* Open the dataset. */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "PALETTE" on the >>image<< dataset */ has_pal = H5IM_find_palette(did); if(has_pal == 1) { if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if(H5Tget_class(atid) < 0) goto out; /* Get the reference(s) */ if((asid = H5Aget_space(aid)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(asid); dim_ref = (hsize_t)n_refs; refbuf = (hobj_ref_t*)HDmalloc( sizeof(hobj_ref_t) * (size_t)dim_ref ); if ( H5Aread( aid, atid, refbuf ) < 0) goto out; /* Get the actual palette */ if ( (pal_id = H5Rdereference( did, H5R_OBJECT, &refbuf[pal_number] )) < 0) goto out; if ( (pal_space_id = H5Dget_space( pal_id )) < 0) goto out; if ( H5Sget_simple_extent_ndims( pal_space_id ) < 0) goto out; if ( H5Sget_simple_extent_dims( pal_space_id, pal_dims, pal_maxdims ) < 0) goto out; /* close */ if (H5Dclose(pal_id)<0) goto out; if ( H5Sclose( pal_space_id ) < 0) goto out; if ( H5Sclose( asid ) < 0) goto out; if ( H5Tclose( atid ) < 0) goto out; if ( H5Aclose( aid ) < 0) goto out; HDfree( refbuf ); } /* Close the image dataset. */ if ( H5Dclose( did ) < 0) return -1; return 0; out: H5Dclose( did ); H5Sclose( asid ); H5Tclose( atid ); H5Aclose( aid ); return -1; }
/*------------------------------------------------------------------------- * Function: H5IM_get_palette * * Purpose: private function that reads a palette to memory type TID * * Return: Success: 0, Failure: -1 * * Programmer: Pedro Vicente Nunes, [email protected] * * Date: May 10, 2005 * * Comments: * This function allows reading of an 8bit palette from disk disk * to memory type TID * The memory datatype can be H5T_NATIVE_INT or H5T_NATIVE_UCHAR currently. * the H5T_NATIVE_INT is supposed to be called from * the FORTRAN interface where the image buffer is defined as type "integer" * * Comments: * based on HDF5 Image and Palette Specification * http://hdf.ncsa.uiuc.edu/HDF5/H5Image/ImageSpec.html * * Modifications: * *------------------------------------------------------------------------- */ herr_t H5IM_get_palette(hid_t loc_id, const char *image_name, int pal_number, hid_t tid, void *pal_data) { hid_t image_id; int has_pal; hid_t attr_type; hid_t attr_id; hid_t attr_space_id; hid_t attr_class; hssize_t n_refs; hsize_t dim_ref; hobj_ref_t *refbuf; /* buffer to read references */ hid_t pal_id; /* Open the dataset. */ if((image_id = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "PALETTE" on the >>image<< dataset */ has_pal = H5IM_find_palette(image_id); if(has_pal == 1) { if((attr_id = H5Aopen(image_id, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((attr_type = H5Aget_type(attr_id)) < 0) goto out; if((attr_class = H5Tget_class(attr_type)) < 0) goto out; /* Check if it is really a reference */ if(attr_class == H5T_REFERENCE) { /* Get the reference(s) */ if((attr_space_id = H5Aget_space(attr_id)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(attr_space_id); dim_ref = n_refs; refbuf = malloc(sizeof(hobj_ref_t) * (int)dim_ref); if(H5Aread(attr_id, attr_type, refbuf) < 0) goto out; /* Get the palette id */ if((pal_id = H5Rdereference(image_id, H5R_OBJECT, &refbuf[pal_number])) < 0) goto out; /* Read the palette dataset using the memory type TID */ if(H5Dread(pal_id, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data) < 0) goto out; if(H5Sclose(attr_space_id) < 0) goto out; /* close the dereferenced dataset */ if(H5Dclose(pal_id) < 0) goto out; free(refbuf); } /* H5T_REFERENCE */ if(H5Tclose(attr_type) < 0) goto out; /* Close the attribute. */ if(H5Aclose(attr_id) < 0) goto out; } /* Close the image dataset. */ if(H5Dclose(image_id) < 0) return -1; return 0; out: H5Dclose(image_id); return -1; }
herr_t H5IMget_palette( hid_t loc_id, const char *image_name, int pal_number, unsigned char *pal_data ) { hid_t did; int has_pal; hid_t atid=-1; hid_t aid; hid_t asid=-1; hssize_t n_refs; hsize_t dim_ref; hobj_ref_t *refbuf; /* buffer to read references */ hid_t pal_id; /* Open the dataset. */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "PALETTE" on the >>image<< dataset */ has_pal = H5IM_find_palette(did); if(has_pal == 1 ) { if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if(H5Tget_class(atid) < 0) goto out; /* Get the reference(s) */ if((asid = H5Aget_space(aid)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(asid); dim_ref = n_refs; refbuf = (hobj_ref_t*)malloc( sizeof(hobj_ref_t) * (int)dim_ref ); if ( H5Aread( aid, atid, refbuf ) < 0) goto out; /* Get the palette id */ if ( (pal_id = H5Rdereference( did, H5R_OBJECT, &refbuf[pal_number] )) < 0) goto out; /* Read the palette dataset */ if ( H5Dread( pal_id, H5Dget_type(pal_id), H5S_ALL, H5S_ALL, H5P_DEFAULT, pal_data ) < 0) goto out; /* close */ if (H5Dclose(pal_id)<0) goto out; if ( H5Sclose( asid ) < 0) goto out; if ( H5Tclose( atid ) < 0) goto out; if ( H5Aclose( aid ) < 0) goto out; free( refbuf ); } /* Close the image dataset. */ if ( H5Dclose( did ) < 0) return -1; return 0; out: H5Dclose( did ); H5Sclose( asid ); H5Tclose( atid ); H5Aclose( aid ); return -1; }
/* Read noise correlations from a file into a noise object. Only read noise related to channels which are in the noise object's map, so be sure to call SetChannelIndex before reading correlations into it. */ void ReadNoiseCorrelations(std::string filename, NoiseCorrelations& noise) { hid_t fileID = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); // Verify this is version 1. { unsigned char version; hid_t attID = H5Aopen(fileID, "version", H5P_DEFAULT); H5Aread(attID, H5T_NATIVE_UCHAR, reinterpret_cast<void*>(version)); H5Aclose(attID); assert(version == 1); } // I have one index for the in-memory noise matrices; build another for the file. MapIndexHandler<unsigned char> FileChannelMap = HDF5Helper::ReadMapFromAttribute(fileID, "channel_list"); for(size_t i = 0; i < noise.GetNoiseBlockIndex().MaxIndex(); i++) { // Verify that the file has correlation information for every channel we're requesting. assert(FileChannelMap.HasKey(noise.GetNoiseBlockIndex().KeyForIndex(i).second)); } NoiseCorrelations::NoiseBlockIndexT FileBlockIndex(RangeIndexHandler<unsigned char>(0, 2), FileChannelMap); // Retrieve the actual noise information. std::vector<double> PackedArray; for(size_t f = 0; f < 1024; f++) { NoiseMatrix& mat = noise.GetMatrixForIndex(f); const NoiseCorrelations::NoiseBlockIndexT& NoiseBlockIndex = noise.GetNoiseBlockIndex(); assert(NoiseBlockIndex.MaxIndex() % 2 == 0 and NoiseBlockIndex.MaxIndex() > 0); // Create the name for this dataset. std::ostringstream strstream; strstream << "/noise_corr_" << std::setfill('0') << std::setw(4) << f; std::string dataset_name = strstream.str(); // Get data from file. hid_t datasetID = H5Dopen2(fileID, dataset_name.c_str(), H5P_DEFAULT); hid_t dataspaceID = H5Dget_space(datasetID); size_t PackedSize = ExpectedPackedSize(f, FileBlockIndex.MaxIndex()); assert(PackedSize == H5Sget_simple_extent_npoints(dataspaceID)); PackedArray.resize(PackedSize); H5Dread(datasetID, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, reinterpret_cast<void*>(&PackedArray[0])); // Fill noise matrix from packed array. for(size_t i = 0; i < NoiseBlockIndex.MaxIndex(); i++) { size_t rowInFile = FileBlockIndex.IndexForKey(NoiseBlockIndex.KeyForIndex(i)); for(size_t j = i; j < NoiseBlockIndex.MaxIndex(); j++) { size_t colInFile = FileBlockIndex.IndexForKey(NoiseBlockIndex.KeyForIndex(j)); std::pair<size_t, bool> packed_loc = PackedArrayIndexFor(f, rowInFile, colInFile, FileBlockIndex); if(packed_loc.first == size_t(-1)) mat.GetCorrByIndex(i, j) = 0; else { assert(packed_loc.first < PackedArray.size()); if(packed_loc.second) mat.GetCorrByIndex(i, j) = -PackedArray[packed_loc.first]; else mat.GetCorrByIndex(i, j) = PackedArray[packed_loc.first]; } } } // Release HDF5 file resources. H5Sclose(dataspaceID); H5Dclose(datasetID); } assert(H5Fget_obj_count(fileID, H5F_OBJ_ALL) == 1); // The file should be the only object left. H5Fclose(fileID); }
//-***************************************************************************** ArImpl::ArImpl( const std::string &iFileName, AbcA::ReadArraySampleCachePtr iCache, const bool iCacheHierarchy ) : m_fileName( iFileName ) , m_file( -1 ) , m_readArraySampleCache( iCache ) { // OPEN THE FILE! htri_t exi = H5Fis_hdf5( m_fileName.c_str() ); ABCA_ASSERT( exi == 1, "Nonexistent or not an Alembic file: " << m_fileName ); m_file = H5Fopen( m_fileName.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT ); ABCA_ASSERT( m_file >= 0, "Could not open file: " << m_fileName ); // get the version using HDF5 native calls int version = -INT_MAX; if (H5Aexists(m_file, "abc_version")) { size_t numRead = 0; ReadSmallArray(m_file, "abc_version", H5T_STD_I32LE, H5T_NATIVE_INT32, 1, numRead, &version); } ABCA_ASSERT(version >= -8 && version <= ALEMBIC_HDF5_FILE_VERSION, "Unsupported file version detected: " << version); // if it isn't there, it's pre 1.0 int fileVersion = 9999; if (H5Aexists( m_file, "abc_release_version" )) { size_t numRead = 0; ReadSmallArray( m_file, "abc_release_version", H5T_STD_I32LE, H5T_NATIVE_INT32, 1, numRead, &fileVersion ); } m_archiveVersion = fileVersion; HDF5HierarchyReader reader( m_file, m_H5H, iCacheHierarchy ); H5Node node = m_H5H.createNode( m_file ); H5Node abcRoot = OpenGroup( node, "ABC" ); AbcA::MetaData metaData; ReadMetaData( abcRoot, ".prop.meta", metaData ); m_header.reset( new AbcA::ObjectHeader( "ABC", "/", metaData ) ); m_data.reset( new OrData( m_header, node, m_archiveVersion ) ); CloseObject( abcRoot ); ReadTimeSamples( m_file, m_timeSamples ); if ( H5Aexists( m_file, "abc_max_samples" ) ) { hid_t aid = H5Aopen( m_file, "abc_max_samples", H5P_DEFAULT ); if ( aid < 0 ) { return; } AttrCloser attrCloser( aid ); // figure out how big it is hid_t sid = H5Aget_space( aid ); if ( sid < 0 ) { return; } DspaceCloser dspaceCloser( sid ); hssize_t numPoints = H5Sget_simple_extent_npoints( sid ); if ( numPoints < 1 ) { return; } m_maxSamples.resize( numPoints ); // do the read H5Aread( aid, H5T_NATIVE_LLONG, &( m_maxSamples.front() ) ); } }
//-***************************************************************************** AbcA::ArraySamplePtr ReadArray( AbcA::ReadArraySampleCachePtr iCache, hid_t iParent, const std::string &iName, const AbcA::DataType &iDataType, hid_t iFileType, hid_t iNativeType ) { // Dispatch string stuff. if ( iDataType.getPod() == kStringPOD ) { return ReadStringArray( iCache, iParent, iName, iDataType ); } else if ( iDataType.getPod() == kWstringPOD ) { return ReadWstringArray( iCache, iParent, iName, iDataType ); } assert( iDataType.getPod() != kStringPOD && iDataType.getPod() != kWstringPOD ); // Open the data set. hid_t dsetId = H5Dopen( iParent, iName.c_str(), H5P_DEFAULT ); ABCA_ASSERT( dsetId >= 0, "Cannot open dataset: " << iName ); DsetCloser dsetCloser( dsetId ); // Read the data space. hid_t dspaceId = H5Dget_space( dsetId ); ABCA_ASSERT( dspaceId >= 0, "Could not get dataspace for dataSet: " << iName ); DspaceCloser dspaceCloser( dspaceId ); AbcA::ArraySample::Key key; bool foundDigest = false; // if we are caching, get the key and see if it is being used if ( iCache ) { key.origPOD = iDataType.getPod(); key.readPOD = key.origPOD; key.numBytes = Util::PODNumBytes( key.readPOD ) * H5Sget_simple_extent_npoints( dspaceId ); foundDigest = ReadKey( dsetId, "key", key ); AbcA::ReadArraySampleID found = iCache->find( key ); if ( found ) { AbcA::ArraySamplePtr ret = found.getSample(); assert( ret ); if ( ret->getDataType().getPod() != iDataType.getPod() ) { ABCA_THROW( "ERROR: Read data type for dset: " << iName << ": " << ret->getDataType() << " does not match expected data type: " << iDataType ); } // Got it! return ret; } } // Okay, we haven't found it in a cache. // Read the data type. hid_t dtypeId = H5Dget_type( dsetId ); ABCA_ASSERT( dtypeId >= 0, "Could not get datatype for dataSet: " << iName ); DtypeCloser dtypeCloser( dtypeId ); ABCA_ASSERT( EquivalentDatatypes( iFileType, dtypeId ), "File DataType clash for array dataset: " << iName ); AbcA::ArraySamplePtr ret; H5S_class_t dspaceClass = H5Sget_simple_extent_type( dspaceId ); if ( dspaceClass == H5S_SIMPLE ) { // Get the dimensions int rank = H5Sget_simple_extent_ndims( dspaceId ); ABCA_ASSERT( rank == 1, "H5Sget_simple_extent_ndims() must be 1." ); hsize_t hdim = 0; rank = H5Sget_simple_extent_dims( dspaceId, &hdim, NULL ); Dimensions dims; std::string dimName = iName + ".dims"; if ( H5Aexists( iParent, dimName.c_str() ) ) { ReadDimensions( iParent, dimName, dims ); } else { dims.setRank(1); dims[0] = hdim / iDataType.getExtent(); } ABCA_ASSERT( dims.numPoints() > 0, "Degenerate dims in Dataset read" ); // Create a buffer into which we shall read. ret = AbcA::AllocateArraySample( iDataType, dims ); assert( ret->getData() ); // And... read into it. herr_t status = H5Dread( dsetId, iNativeType, H5S_ALL, H5S_ALL, H5P_DEFAULT, const_cast<void*>( ret->getData() ) ); ABCA_ASSERT( status >= 0, "H5Dread() failed." ); } else if ( dspaceClass == H5S_NULL ) { Dimensions dims; std::string dimName = iName + ".dims"; if ( H5Aexists( iParent, dimName.c_str() ) ) { ReadDimensions( iParent, dimName, dims ); ABCA_ASSERT( dims.rank() > 0, "Degenerate rank in Dataset read" ); // Num points should be zero here. ABCA_ASSERT( dims.numPoints() == 0, "Expecting zero points in dimensions" ); } else { dims.setRank(1); dims[0] = 0; } ret = AbcA::AllocateArraySample( iDataType, dims ); } else { ABCA_THROW( "Unexpected scalar dataspace encountered." ); } // Store if there is a cache. if ( foundDigest && iCache ) { AbcA::ReadArraySampleID stored = iCache->store( key, ret ); if ( stored ) { return stored.getSample(); } } // Otherwise, just leave! ArraySamplePtr returned by AllocateArraySample // already has fancy-dan deleter built in. // I REALLY LOVE SMART PTRS. return ret; }
////////////////////////////////////////////////////////////////////////////////////////// //HaloTrees Append Datasets Group// ////////////////////////////////////////////////////////////////////////////////////////// void HaloTreesGroupDataAppend(hid_t file_out_hdf5, MergerTree **halo, int tree_levels, int nb_nodes) { int i, j, count; int rank, nb_elements; int *nodeIndex, *nodeHost, *nodeDescendent; hid_t dataset, dataspace, cparams, filespace; hsize_t newdims[1], finaldims[1], newdims2[2], finaldims2[2], offset[1], offset2[2]; newdims[0] = nb_nodes; newdims2[0] = nb_nodes; newdims2[1] = 3; double *time, *expansionFactor, *redshift; double *nodeMass; double nodePosition[nb_nodes][3], nodeVelocity[nb_nodes][3]; //for some reason dynamic memory allocation don't work with hdf5 2D datasets nodeIndex = (int *)malloc(nb_nodes*sizeof(int)); nodeDescendent = (int *)malloc(nb_nodes*sizeof(int)); nodeMass = (double *)malloc(nb_nodes*sizeof(double)); expansionFactor = (double *)malloc(nb_nodes*sizeof(double)); redshift = (double *)malloc(nb_nodes*sizeof(double)); time = (double *)malloc(nb_nodes*sizeof(double)); for(i = 0, count = 0; i < tree_levels; i++) for(j = 0; j < halo[i][0].snapshot.nb_halos; j++) { nodeIndex[count] = halo[i][j].idAlias; nodeMass[count] = halo[i][j].mvir; nodeDescendent[count] = halo[i][j].son_idAlias; expansionFactor[count] = halo[i][j].sfactor; time[count] = halo[i][j].age; redshift[count] = 1.0/halo[i][j].sfactor - 1.0; nodePosition[count][0] = halo[i][j].position[0]; nodePosition[count][1] = halo[i][j].position[1]; nodePosition[count][2] = halo[i][j].position[2]; count++; } /* hostIndex Dataset */ dataset = H5Dopen (file_out_hdf5, "/haloTrees/hostIndex", H5P_DEFAULT); filespace = H5Dget_space (dataset); rank = H5Sget_simple_extent_ndims (filespace); nb_elements = H5Sget_simple_extent_npoints( filespace ); finaldims[0] = nb_elements+10;// + nb_nodes-1000; finaldims2[0] = nb_elements + nb_nodes; finaldims2[1] = 3; offset[0] = nb_elements; offset2[0] = nb_elements; offset2[1] = 0; printf("finaldims:%d\n",(int)finaldims[0]); H5Dextend(dataset, finaldims); filespace = H5Dget_space( dataset ); H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, newdims, NULL); dataspace = H5Screate_simple (1, newdims, NULL); //H5Dwrite (dataset, H5T_NATIVE_INT, dataspace, filespace, H5P_DEFAULT, nodeIndex); H5Sclose(dataspace); H5Sclose(filespace); H5Dclose(dataset); /* nodeIndex Dataset */ /* dataset = H5Dopen (file_out_hdf5, "/haloTrees/nodeIndex", H5P_DEFAULT); */ /* H5Dextend(dataset, finaldims); */ /* filespace = H5Dget_space( dataset ); */ /* H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, newdims, NULL); */ /* dataspace = H5Screate_simple (1, newdims, NULL); */ /* H5Dwrite (dataset, H5T_NATIVE_INT, dataspace, filespace, H5P_DEFAULT, nodeIndex); */ /* H5Sclose(dataspace); */ /* H5Dclose(filespace); */ /* H5Dclose(dataset); */ /* descendentIndex Dataset*/ /* dataset = H5Dopen (file_out_hdf5, "/haloTrees/descendentIndex", H5P_DEFAULT); */ /* H5Dextend(dataset, finaldims); */ /* filespace = H5Dget_space( dataset ); */ /* H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, newdims, NULL); */ /* dataspace = H5Screate_simple (1, newdims, NULL); */ /* H5Dwrite (dataset, H5T_NATIVE_INT, dataspace, filespace, H5P_DEFAULT, nodeDescendent); */ /* H5Sclose(dataspace); */ /* H5Dclose(filespace); */ /* H5Dclose(dataset); */ /* nodeMass Dataset*/ /* dataset = H5Dopen (file_out_hdf5, "/haloTrees/nodeMass", H5P_DEFAULT); */ /* H5Dextend(dataset, finaldims); */ /* filespace = H5Dget_space( dataset ); */ /* H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, newdims, NULL); */ /* dataspace = H5Screate_simple (1, newdims, NULL); */ /* H5Dwrite (dataset, H5T_NATIVE_DOUBLE, dataspace, filespace, H5P_DEFAULT, nodeMass); */ /* H5Sclose(dataspace); */ /* H5Dclose(filespace); */ /* H5Dclose(dataset); */ /* /\* exapansionFactor Dataset*\/ */ /* dataset = H5Dopen (file_out_hdf5, "/haloTrees/exapansionFactor", H5P_DEFAULT); */ /* H5Dextend(dataset, finaldims); */ /* filespace = H5Dget_space( dataset ); */ /* H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, newdims, NULL); */ /* dataspace = H5Screate_simple (1, newdims, NULL); */ /* H5Dwrite (dataset, H5T_NATIVE_DOUBLE, dataspace, filespace, H5P_DEFAULT, expansionFactor); */ /* H5Sclose(dataspace); */ /* H5Dclose(filespace); */ /* H5Dclose(dataset); */ /* /\* redshift Dataset *\/ */ /* dataset = H5Dopen (file_out_hdf5, "/haloTrees/redshift", H5P_DEFAULT); */ /* H5Dextend(dataset, finaldims); */ /* filespace = H5Dget_space( dataset ); */ /* H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, newdims, NULL); */ /* dataspace = H5Screate_simple (1, newdims, NULL); */ /* H5Dwrite (dataset, H5T_NATIVE_DOUBLE, dataspace, filespace, H5P_DEFAULT, redshift); */ /* H5Sclose(dataspace); */ /* H5Dclose(filespace); */ /* H5Dclose(dataset); */ /* /\* time Dataset *\/ */ /* dataset = H5Dopen (file_out_hdf5, "/haloTrees/time", H5P_DEFAULT); */ /* H5Dextend(dataset, finaldims); */ /* filespace = H5Dget_space( dataset ); */ /* H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset, NULL, newdims, NULL); */ /* dataspace = H5Screate_simple (1, newdims, NULL); */ /* H5Dwrite (dataset, H5T_NATIVE_DOUBLE, dataspace, filespace, H5P_DEFAULT, time); */ /* H5Sclose(dataspace); */ /* H5Dclose(filespace); */ /* H5Dclose(dataset); */ /* /\* position Dataset *\/ */ /* dataset = H5Dopen (file_out_hdf5, "/haloTrees/position", H5P_DEFAULT); */ /* H5Dextend(dataset, finaldims2); */ /* filespace = H5Dget_space( dataset ); */ /* H5Sselect_hyperslab (filespace, H5S_SELECT_SET, offset2, NULL, newdims2, NULL); */ /* dataspace = H5Screate_simple (2, newdims2, NULL); */ /* H5Dwrite (dataset, H5T_NATIVE_DOUBLE, dataspace, filespace, H5P_DEFAULT, nodePosition); */ /* H5Sclose(dataspace); */ /* H5Dclose(filespace); */ /* H5Dclose(dataset); */ /* velocity Dataset */ //not used by Galacticus yet! /* dataspace = H5Screate_simple(2, dims2, maxdims2); */ /* cparams = H5Pcreate (H5P_DATASET_CREATE); */ /* H5Pset_chunk ( cparams, 2, chunk_dims2); */ /* dataset = H5Dcreate(file_out_hdf5, "/haloTrees/velocity", H5T_IEEE_F64LE, dataspace, H5P_DEFAULT, cparams, H5P_DEFAULT); */ /* H5Dwrite( dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, nodeVelocity); */ /* H5Pclose(cparams); */ /* H5Sclose(dataspace); */ /* H5Dclose(dataset); */ }
void Hdf5DataReader::CommonConstructor() { if (!mDirectory.IsDir() || !mDirectory.Exists()) { EXCEPTION("Directory does not exist: " + mDirectory.GetAbsolutePath()); } std::string file_name = mDirectory.GetAbsolutePath() + mBaseName + ".h5"; FileFinder h5_file(file_name, RelativeTo::Absolute); if (!h5_file.Exists()) { EXCEPTION("Hdf5DataReader could not open " + file_name + " , as it does not exist."); } // Open the file and the main dataset mFileId = H5Fopen(file_name.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); if (mFileId <= 0) { EXCEPTION("Hdf5DataReader could not open " << file_name << " , H5Fopen error code = " << mFileId); } mVariablesDatasetId = H5Dopen(mFileId, mDatasetName.c_str(), H5P_DEFAULT); SetMainDatasetRawChunkCache(); if (mVariablesDatasetId <= 0) { H5Fclose(mFileId); EXCEPTION("Hdf5DataReader opened " << file_name << " but could not find the dataset '" << mDatasetName.c_str() << "', H5Dopen error code = " << mVariablesDatasetId); } hid_t variables_dataspace = H5Dget_space(mVariablesDatasetId); mVariablesDatasetRank = H5Sget_simple_extent_ndims(variables_dataspace); // Get the dataset/dataspace dimensions hsize_t dataset_max_sizes[AbstractHdf5Access::DATASET_DIMS]; H5Sget_simple_extent_dims(variables_dataspace, mDatasetDims, dataset_max_sizes); for (unsigned i=1; i<AbstractHdf5Access::DATASET_DIMS; i++) // Zero is excluded since it may be unlimited { assert(mDatasetDims[i] == dataset_max_sizes[i]); } // Check if an unlimited dimension has been defined if (dataset_max_sizes[0] == H5S_UNLIMITED) { // In terms of an Unlimited dimension dataset: // * Files pre - r16738 (inc. Release 3.1 and earlier) use simply "Time" for "Data"'s unlimited variable. // * Files generated by r16738 - r18257 used "<DatasetName>_Time" for "<DatasetName>"'s unlimited variable, // - These are not to be used and there is no backwards compatibility for them, since they weren't in a release. // * Files post r18257 (inc. Release 3.2 onwards) use "<DatasetName>_Unlimited" for "<DatasetName>"'s // unlimited variable, // - a new attribute "Name" has been added to the Unlimited Dataset to allow it to assign // any name to the unlimited variable. Which can then be easily read by this class. // - if this dataset is missing we look for simply "Time" to remain backwards compatible with Releases <= 3.1 SetUnlimitedDatasetId(); hid_t timestep_dataspace = H5Dget_space(mUnlimitedDatasetId); // Get the dataset/dataspace dimensions H5Sget_simple_extent_dims(timestep_dataspace, &mNumberTimesteps, NULL); } // Get the attribute where the name of the variables are stored hid_t attribute_id = H5Aopen_name(mVariablesDatasetId, "Variable Details"); // Get attribute datatype, dataspace, rank, and dimensions hid_t attribute_type = H5Aget_type(attribute_id); hid_t attribute_space = H5Aget_space(attribute_id); hsize_t attr_dataspace_dim; H5Sget_simple_extent_dims(attribute_space, &attr_dataspace_dim, NULL); unsigned num_columns = H5Sget_simple_extent_npoints(attribute_space); char* string_array = (char *)malloc(sizeof(char)*MAX_STRING_SIZE*(int)num_columns); H5Aread(attribute_id, attribute_type, string_array); // Loop over column names and store them. for (unsigned index=0; index < num_columns; index++) { // Read the string from the raw vector std::string column_name_unit(&string_array[MAX_STRING_SIZE*index]); // Find beginning of unit definition. size_t name_length = column_name_unit.find('('); size_t unit_length = column_name_unit.find(')') - name_length - 1; std::string column_name = column_name_unit.substr(0, name_length); std::string column_unit = column_name_unit.substr(name_length+1, unit_length); mVariableNames.push_back(column_name); mVariableToColumnIndex[column_name] = index; mVariableToUnit[column_name] = column_unit; } // Release all the identifiers H5Tclose(attribute_type); H5Sclose(attribute_space); H5Aclose(attribute_id); // Free allocated memory free(string_array); // Find out if it's incomplete data H5E_BEGIN_TRY //Supress HDF5 error if the IsDataComplete name isn't there { attribute_id = H5Aopen_name(mVariablesDatasetId, "IsDataComplete"); } H5E_END_TRY; if (attribute_id < 0) { // This is in the old format (before we added the IsDataComplete attribute). // Just quit (leaving a nasty hdf5 error). return; } attribute_type = H5Aget_type(attribute_id); attribute_space = H5Aget_space(attribute_id); unsigned is_data_complete; H5Aread(attribute_id, H5T_NATIVE_UINT, &is_data_complete); // Release all the identifiers H5Tclose(attribute_type); H5Sclose(attribute_space); H5Aclose(attribute_id); mIsDataComplete = (is_data_complete == 1) ? true : false; if (is_data_complete) { return; } // Incomplete data // Read the vector thing attribute_id = H5Aopen_name(mVariablesDatasetId, "NodeMap"); attribute_type = H5Aget_type(attribute_id); attribute_space = H5Aget_space(attribute_id); // Get the dataset/dataspace dimensions unsigned num_node_indices = H5Sget_simple_extent_npoints(attribute_space); // Read data from hyperslab in the file into the hyperslab in memory mIncompleteNodeIndices.clear(); mIncompleteNodeIndices.resize(num_node_indices); H5Aread(attribute_id, H5T_NATIVE_UINT, &mIncompleteNodeIndices[0]); H5Tclose(attribute_type); H5Sclose(attribute_space); H5Aclose(attribute_id); }
/* * Operator function. */ static herr_t attr_info(hid_t loc_id, const char *name, const H5A_info_t *ainfo, void *opdata) { hid_t attr, atype, aspace; /* Attribute, datatype and dataspace identifiers */ int rank; hsize_t sdim[64]; herr_t ret; int i; size_t npoints; /* Number of elements in the array attribute. */ float *float_array; /* Pointer to the array attribute. */ /* avoid warnings */ opdata = opdata; /* * Open the attribute using its name. */ attr = H5Aopen(loc_id, name, H5P_DEFAULT); /* * Display attribute name. */ printf("\nName : %s\n", name); /* * Get attribute datatype, dataspace, rank, and dimensions. */ atype = H5Aget_type(attr); aspace = H5Aget_space(attr); rank = H5Sget_simple_extent_ndims(aspace); ret = H5Sget_simple_extent_dims(aspace, sdim, NULL); /* * Display rank and dimension sizes for the array attribute. */ if(rank > 0) { printf("Rank : %d \n", rank); printf("Dimension sizes : "); for (i=0; i< rank; i++) printf("%d ", (int)sdim[i]); printf("\n"); } /* * Read array attribute and display its type and values. */ if (H5T_FLOAT == H5Tget_class(atype)) { printf("Type : FLOAT \n"); npoints = H5Sget_simple_extent_npoints(aspace); float_array = (float *)malloc(sizeof(float)*(int)npoints); ret = H5Aread(attr, atype, float_array); printf("Values : "); for( i = 0; i < (int)npoints; i++) printf("%f ", float_array[i]); printf("\n"); free(float_array); } /* * Release all identifiers. */ H5Tclose(atype); H5Sclose(aspace); H5Aclose(attr); return 0; }
// JRC: This fat interface may not scale? What about // scalar attributes? herr_t getAttributeHelper(const hid_t id, std::string* sval, std::vector<int>* ivals, std::vector<float>* fvals) { herr_t err = 0; size_t npoints; hid_t atype = H5Aget_type(id); H5T_class_t type = H5Tget_class(atype); hid_t aspace = H5Aget_space(id); size_t rank = H5Sget_simple_extent_ndims(aspace); /* hsize_t sdim[rank]; ret = H5Sget_simple_extent_dims(aspace, sdim, NULL); dims->resize(rank); for (size_t i = 0; i < rank; ++i) (*dims)[i] = sdim[i]; */ if (type == H5T_INTEGER) { if (rank == 0) { ivals->resize(1); int v; // err = H5Aread(id, atype, &v); err = H5Aread(id, H5T_NATIVE_INT, &v); (*ivals)[0] = v; return err; } // rank>0 npoints = H5Sget_simple_extent_npoints(aspace); int* v = new int[npoints]; err = H5Aread(id, H5T_NATIVE_INT, v); ivals->resize(npoints); for (size_t i = 0; i<npoints; ++i) { (*ivals)[i] = v[i]; } delete v; return err; } if (type == H5T_FLOAT) { if (rank == 0) { fvals->resize(1); float v; err = H5Aread(id, H5T_NATIVE_FLOAT, &v); (*fvals)[0] = v; return err; } // rank>0 npoints = H5Sget_simple_extent_npoints(aspace); float* v = new float[npoints]; err = H5Aread(id, H5T_NATIVE_FLOAT, v); fvals->resize(npoints); for (size_t i = 0; i<npoints; ++i) { (*fvals)[i] = v[i]; } delete v; return err; } if (type == H5T_STRING) { if (rank != 0) { return -1; } size_t len = H5Aget_storage_size(id); sval->resize(len); char* v = new char[len]; err = H5Aread(id, atype, v); // JRC: is this right? // err = H5Aread(id, H5T_NATIVE_CHAR, v); for (size_t i = 0; i < len; ++i) { (*sval)[i] = v[i]; } delete [] v; return err; } return err; }
herr_t H5IMget_npalettes( hid_t loc_id, const char *image_name, hssize_t *npals ) { hid_t did; hid_t atid; hid_t aid; hid_t asid; H5T_class_t aclass; int has_pal; /* check the arguments */ if(image_name == NULL) return -1; /*assume initially we have no palettes attached*/ *npals = 0; /* Open the dataset. */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "PALETTE" on the >>image<< dataset */ has_pal = H5IM_find_palette(did); if(has_pal == 1 ) { if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if((aclass = H5Tget_class(atid)) < 0) goto out; /* Check if it is really a reference */ if(aclass == H5T_REFERENCE) { if((asid = H5Aget_space(aid)) < 0) goto out; *npals = H5Sget_simple_extent_npoints( asid ); if ( H5Sclose( asid ) < 0) goto out; } /* H5T_REFERENCE */ if ( H5Tclose( atid ) < 0) goto out; /* Close the attribute. */ if ( H5Aclose( aid ) < 0) goto out; } /* Close the image dataset. */ if ( H5Dclose( did ) < 0) return -1; return 0; out: H5Dclose( did ); return -1; }
herr_t H5IMlink_palette( hid_t loc_id, const char *image_name, const char *pal_name ) { hid_t did; hid_t atid=-1; hid_t aid=-1; hid_t asid=-1; hobj_ref_t ref; /* write a new reference */ hobj_ref_t *refbuf; /* buffer to read references */ hssize_t n_refs; hsize_t dim_ref; int ok_pal; /* check the arguments */ if (image_name == NULL) return -1; if (pal_name == NULL) return -1; /* The image dataset may or may not have the attribute "PALETTE" * First we try to open to see if it is already there; if not, it is created. * If it exists, the array of references is extended to hold the reference * to the new palette */ /* First we get the image id */ if((did = H5Dopen2(loc_id, image_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "PALETTE" on the >>image<< dataset */ ok_pal = H5LT_find_attribute( did, "PALETTE" ); /*------------------------------------------------------------------------- * It does not exist. We create the attribute and one reference *------------------------------------------------------------------------- */ if(ok_pal == 0 ) { if((asid = H5Screate(H5S_SCALAR)) < 0) goto out; /* Create the attribute type for the reference */ if((atid = H5Tcopy(H5T_STD_REF_OBJ)) < 0) goto out; /* Create the attribute "PALETTE" to be attached to the image*/ if((aid = H5Acreate2(did, "PALETTE", atid, asid, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto out; /* Create a reference. The reference is created on the local id. */ if(H5Rcreate(&ref, loc_id, pal_name, H5R_OBJECT, (hid_t)-1) < 0) goto out; /* Write the attribute with the reference */ if(H5Awrite(aid, atid, &ref) < 0) goto out; /* close */ if(H5Sclose(asid) < 0) goto out; if(H5Tclose(atid) < 0) goto out; if(H5Aclose(aid) < 0) goto out; } /*------------------------------------------------------------------------- * The attribute already exists, open it *------------------------------------------------------------------------- */ else if(ok_pal == 1) { if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if(H5Tget_class(atid) < 0) goto out; /* Get and save the old reference(s) */ if((asid = H5Aget_space(aid)) < 0) goto out; n_refs = H5Sget_simple_extent_npoints(asid); dim_ref = (hsize_t)n_refs + 1; refbuf = (hobj_ref_t*)HDmalloc( sizeof(hobj_ref_t) * (size_t)dim_ref ); if ( H5Aread( aid, atid, refbuf ) < 0) goto out; /* The attribute must be deleted, in order to the new one can reflect the changes*/ if(H5Adelete(did, "PALETTE") < 0) goto out; /* Create a new reference for this palette. */ if ( H5Rcreate( &ref, loc_id, pal_name, H5R_OBJECT, (hid_t)-1 ) < 0) goto out; refbuf[n_refs] = ref; /* Create the data space for the new references */ if(H5Sclose(asid) < 0) goto out; if((asid = H5Screate_simple(1, &dim_ref, NULL)) < 0) goto out; /* Create the attribute again with the changes of space */ if(H5Aclose(aid) < 0) goto out; if((aid = H5Acreate2(did, "PALETTE", atid, asid, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto out; /* Write the attribute with the new references */ if(H5Awrite(aid, atid, refbuf) < 0) goto out; /* close */ if(H5Sclose(asid) < 0) goto out; if(H5Tclose(atid) < 0) goto out; if(H5Aclose(aid) < 0) goto out; HDfree( refbuf ); } /* ok_pal == 1 */ /* Close the image dataset. */ if ( H5Dclose( did ) < 0) return -1; return 0; out: H5Dclose( did ); H5Sclose( asid ); H5Tclose( atid ); H5Aclose( aid ); return -1; }
herr_t H5IMget_image_info( hid_t loc_id, const char *dset_name, hsize_t *width, hsize_t *height, hsize_t *planes, char *interlace, hssize_t *npals ) { hid_t did; hid_t sid; hsize_t dims[IMAGE24_RANK]; hid_t aid; hid_t asid; hid_t atid; H5T_class_t aclass; int has_pal; int has_attr; /* check the arguments */ if (dset_name == NULL) return -1; if (interlace == NULL) return -1; /*assume initially we have no palettes attached*/ *npals = 0; /* Open the dataset. */ if((did = H5Dopen2(loc_id, dset_name, H5P_DEFAULT)) < 0) return -1; /* Try to find the attribute "INTERLACE_MODE" on the >>image<< dataset */ has_attr = H5LT_find_attribute(did, "INTERLACE_MODE"); /* It exists, get it */ if(has_attr == 1) { if((aid = H5Aopen(did, "INTERLACE_MODE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if(H5Aread(aid, atid, interlace) < 0) goto out; if(H5Tclose(atid) < 0) goto out; if(H5Aclose(aid) < 0) goto out; } /* Get the dataspace handle */ if ( (sid = H5Dget_space( did )) < 0) goto out; /* Get dimensions */ if ( H5Sget_simple_extent_dims( sid, dims, NULL) < 0) goto out; /* Initialize the image dimensions */ if ( has_attr == 1 ) /* This is a 24 bit image */ { if ( HDstrncmp( interlace, "INTERLACE_PIXEL", 15 ) == 0 ) { /* Number of color planes is defined as the third dimension */ *height = dims[0]; *width = dims[1]; *planes = dims[2]; } else if ( HDstrncmp( interlace, "INTERLACE_PLANE", 15 ) == 0 ) { /* Number of color planes is defined as the first dimension */ *planes = dims[0]; *height = dims[1]; *width = dims[2]; } else return -1; } else /* This is a 8 bit image */ { *height = dims[0]; *width = dims[1]; *planes = 1; } /* Close */ if ( H5Sclose( sid ) < 0) goto out; /* Get number of palettes */ /* Try to find the attribute "PALETTE" on the >>image<< dataset */ has_pal = H5IM_find_palette(did); if(has_pal == 1) { if((aid = H5Aopen(did, "PALETTE", H5P_DEFAULT)) < 0) goto out; if((atid = H5Aget_type(aid)) < 0) goto out; if((aclass = H5Tget_class(atid)) < 0) goto out; /* Check if it is really a reference */ if(aclass == H5T_REFERENCE) { /* Get the reference(s) */ if ( (asid = H5Aget_space( aid )) < 0) goto out; *npals = H5Sget_simple_extent_npoints( asid ); if ( H5Sclose( asid ) < 0) goto out; } /* H5T_REFERENCE */ if ( H5Tclose( atid ) < 0) goto out; /* Close the attribute. */ if ( H5Aclose( aid ) < 0) goto out; } /* End access to the dataset and release resources used by it. */ if ( H5Dclose( did ) < 0) goto out; return 0; out: H5Dclose( did ); H5Aclose( aid ); H5Sclose( asid ); H5Tclose( atid ); return -1; }
int main(void) { hid_t fid1; /* HDF5 File IDs */ hid_t dset1, /* Dataset ID */ dset2; /* Dereferenced dataset ID */ hid_t sid1, /* Dataspace ID #1 */ sid2; /* Dataspace ID #2 */ hsize_t * coords; /* Coordinate buffer */ hsize_t low[SPACE2_RANK]; /* Selection bounds */ hsize_t high[SPACE2_RANK]; /* Selection bounds */ hdset_reg_ref_t *rbuf; /* buffer to to read disk */ int *drbuf; /* Buffer for reading numeric data from disk */ int i, j; /* counting variables */ herr_t ret; /* Generic return value */ /* Output message about test being performed */ /* Allocate write & read buffers */ rbuf=malloc(sizeof(hdset_reg_ref_t)*SPACE1_DIM1); drbuf=calloc(sizeof(int),SPACE2_DIM1*SPACE2_DIM2); /* Open the file */ fid1 = H5Fopen(FILE2, H5F_ACC_RDWR, H5P_DEFAULT); /* Open the dataset */ dset1=H5Dopen(fid1,"/Dataset1"); /* Read selection from disk */ ret=H5Dread(dset1,H5T_STD_REF_DSETREG,H5S_ALL,H5S_ALL,H5P_DEFAULT,rbuf); /* Try to open objects */ dset2 = H5Rdereference(dset1,H5R_DATASET_REGION,&rbuf[0]); /* Check information in referenced dataset */ sid1 = H5Dget_space(dset2); ret=H5Sget_simple_extent_npoints(sid1); printf(" Number of elements in the dataset is : %d\n",ret); /* Read from disk */ ret=H5Dread(dset2,H5T_NATIVE_INT,H5S_ALL,H5S_ALL,H5P_DEFAULT,drbuf); for(i=0; i<SPACE2_DIM1; i++) { for (j=0; j<SPACE2_DIM2; j++) printf (" %d ", drbuf[i*SPACE2_DIM2+j]); printf("\n"); } /* Get the hyperslab selection */ sid2=H5Rget_region(dset1,H5R_DATASET_REGION,&rbuf[0]); /* Verify correct hyperslab selected */ ret = H5Sget_select_npoints(sid2); printf(" Number of elements in the hyperslab is : %d \n", ret); ret = H5Sget_select_hyper_nblocks(sid2); coords=malloc(ret*SPACE2_RANK*sizeof(hsize_t)*2); /* allocate space for the hyperslab blocks */ ret = H5Sget_select_hyper_blocklist(sid2,0,ret,coords); printf(" Hyperslab coordinates are : \n"); printf (" ( %lu , %lu ) ( %lu , %lu ) \n", \ (unsigned long)coords[0],(unsigned long)coords[1],(unsigned long)coords[2],(unsigned long)coords[3]); free(coords); ret = H5Sget_select_bounds(sid2,low,high); /* Close region space */ ret = H5Sclose(sid2); /* Get the element selection */ sid2=H5Rget_region(dset1,H5R_DATASET_REGION,&rbuf[1]); /* Verify correct elements selected */ ret = H5Sget_select_elem_npoints(sid2); printf(" Number of selected elements is : %d\n", ret); /* Allocate space for the element points */ coords= malloc(ret*SPACE2_RANK*sizeof(hsize_t)); ret = H5Sget_select_elem_pointlist(sid2,0,ret,coords); printf(" Coordinates of selected elements are : \n"); for (i=0; i<2*NPOINTS; i=i+2) printf(" ( %lu , %lu ) \n", (unsigned long)coords[i],(unsigned long)coords[i+1]); free(coords); ret = H5Sget_select_bounds(sid2,low,high); /* Close region space */ ret = H5Sclose(sid2); /* Close first space */ ret = H5Sclose(sid1); /* Close dereferenced Dataset */ ret = H5Dclose(dset2); /* Close Dataset */ ret = H5Dclose(dset1); /* Close file */ ret = H5Fclose(fid1); /* Free memory buffers */ free(rbuf); free(drbuf); return 0; }
/* ****************************************************************************************************************************** */ int main(int argc, char *argv[]) { hid_t fileID, dataSetID; herr_t hErrVal; int i; hsize_t dims[1024], maxDims[1024]; H5T_class_t class; char classStr[32]; hid_t dataTypeID; size_t dataSize; H5T_order_t order; int rank; /* Note this is an int, not an hssize_t */ int intVal; hid_t dataSpaceID; hid_t rootGroupID; hsize_t numInRootGrp, firstDataSetIdx, foundFirstDataSet; char attrName[1024], firstDataSetName[1024]; ssize_t objectNameSize, attrNameSize; H5G_stat_t objectStatInfo; int numAttrs; int curAttrIdx; hid_t attrID; hsize_t numDataPoints; unsigned majnum, minnum, relnum; /* Load the library -- not required most platforms. */ hErrVal = H5open(); mjrHDF5_chkError(hErrVal); /* Get the library version */ hErrVal = H5get_libversion(&majnum, &minnum, &relnum); mjrHDF5_chkError(hErrVal); printf("Lib Version: v%lu.%lur%lu\n", (unsigned long)majnum, (unsigned long)minnum, (unsigned long)relnum); /* Open an existing file. */ fileID = H5Fopen(TST_FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT); mjrHDF5_chkError(fileID); /* Get the ID for the "root" group -- every HDF5 has one */ rootGroupID = H5Gopen(fileID, "/", H5P_DEFAULT); mjrHDF5_chkError(rootGroupID); /* Get the number of objects in the root group. */ hErrVal = H5Gget_num_objs(rootGroupID, &numInRootGrp); mjrHDF5_chkError(hErrVal); printf("The root group contains %lu object%c\n", (unsigned long)numInRootGrp, (numInRootGrp==1?' ':'s')); if(numInRootGrp < 1) { printf("As the file contains NO objects, I have nothing left to do...\n"); exit(1); } /* end if */ /* Find the first dataset in the root group. */ for(foundFirstDataSet=0,firstDataSetIdx=0; (!foundFirstDataSet)&&(firstDataSetIdx<numInRootGrp); firstDataSetIdx++) { /* Get object name from the index. */ objectNameSize = H5Gget_objname_by_idx(rootGroupID, firstDataSetIdx, firstDataSetName, 1024); mjrHDF5_chkError(objectNameSize); if(objectNameSize == 0) { /* Need to check for zero return too */ printf("ERROR: Object with index %lu doesn't exist in root group!\n", (unsigned long)firstDataSetIdx); exit(1); } /* end if */ /* Now use the object name to get info about the object... */ hErrVal = H5Gget_objinfo(rootGroupID, firstDataSetName, 0, &objectStatInfo); mjrHDF5_chkError(hErrVal); /* If the object is a dataset, then print out some info. */ if(objectStatInfo.type == H5G_DATASET) { printf("Object %luth (%s) is a dataset!\n", (unsigned long)firstDataSetIdx, firstDataSetName); printf("The name of the %luth object of the root group is: %s\n", (unsigned long)firstDataSetIdx, firstDataSetName); foundFirstDataSet = 1; printf("Info for the %s dataset:\n", firstDataSetName); printf(" Modify time: %lu\n", (unsigned long)objectStatInfo.mtime); printf(" Type: %lu\n", (unsigned long)objectStatInfo.type); printf(" Link count: %lu\n", (unsigned long)objectStatInfo.nlink); } /* end if */ } /* end for */ /* Note: At this point index of the dataset will be: firstDataSetIdx-- */ if(!foundFirstDataSet) { printf("ERROR: Could not find a dataset in the root group\n"); exit(1); } /* end if */ /* Open the dataset we found -- we know it exists. */ dataSetID = H5Dopen(rootGroupID, firstDataSetName, H5P_DEFAULT); mjrHDF5_chkError(dataSetID); /* ****************************************************************************************************************************** */ /* Get some info regarding the TYPE of the dataset. */ dataTypeID = H5Dget_type(dataSetID); mjrHDF5_chkError(dataTypeID); /* Get the class of the data */ class = H5Tget_class(dataTypeID); mjrHDF5_Tclass2str(class, classStr); printf(" Object class: %s\n", classStr); /* Get the size of the type */ dataSize = H5Tget_size(dataTypeID); if(dataSize == 0) { printf("ERROR: Failure in H5Tget_size().\n"); exit(1); } /* end if */ printf(" Size of data type: %lu\n", (unsigned long)dataSize); /* Get the byte order */ order = H5Tget_order(dataTypeID); printf(" Byte Order: "); switch(order) { case H5T_ORDER_ERROR : printf("ERROR\n"); break; case H5T_ORDER_LE : printf("Little Endian\n"); break; case H5T_ORDER_BE : printf("Big Endian\n"); break; case H5T_ORDER_VAX : printf("VAX mixed endian\n"); break; case H5T_ORDER_MIXED : printf("Mixed endian\n"); break; case H5T_ORDER_NONE : printf("particular order\n"); break; } /* end switch */ /* We are done with the datatype. */ hErrVal = H5Tclose(dataTypeID); mjrHDF5_chkError(hErrVal); /* ****************************************************************************************************************************** */ /* Figure out the size of the dataset. */ dataSpaceID = H5Dget_space(dataSetID); mjrHDF5_chkError(dataSpaceID); /* Get the number of dims. */ rank = H5Sget_simple_extent_ndims(dataSpaceID); mjrHDF5_chkError(rank); if(rank > 1024) { /* This can't really happen (limit is 32) */ printf("ERROR: rank too large.\n"); exit(1); } /* end if */ /* Get the size of each dim. */ intVal = H5Sget_simple_extent_dims(dataSpaceID, dims, maxDims); mjrHDF5_chkError(intVal); printf(" Dataspace Rank %lu\n", (unsigned long)rank); printf(" Dim Lengths: "); for(i=0; i<rank; i++) if(dims[i] == H5S_UNLIMITED) { printf("%s ", "UNLIMITED"); } else { printf("%ld ", (long)(dims[i])); } /* end if/else */ printf("\n"); printf(" Max Dim Lengths: "); for(i=0; i<rank; i++) if(maxDims[i] == H5S_UNLIMITED) { printf("%s ", "UNLIMITED"); } else { printf("%ld ", (long)(maxDims[i])); } /* end if/else */ printf("\n"); numDataPoints = H5Sget_simple_extent_npoints(dataSpaceID); if(numDataPoints == 0) { printf("ERROR: Call to H5Sget_simple_extent_npoints failed.\n"); exit(1); } /* end if */ printf("Number of data points: %lu\n", (unsigned long)numDataPoints); /* We are done with the dataSpaceID */ hErrVal = H5Sclose(dataSpaceID); mjrHDF5_chkError(hErrVal); /* Get the number of attributes for the dataSet. */ numAttrs = H5Aget_num_attrs(dataSetID); mjrHDF5_chkError(numAttrs); printf(" Number of attrs: %lu\n", (unsigned long)numAttrs); /* If we have any attributes, we get info for them */ if(numAttrs > 0) { printf(" Attribute info:\n"); for(curAttrIdx=0; curAttrIdx<numAttrs; curAttrIdx++) { attrID = H5Aopen_idx(dataSetID, curAttrIdx); mjrHDF5_chkError(attrID); attrNameSize = H5Aget_name(attrID, 1024, attrName); mjrHDF5_chkError(attrNameSize); printf(" Number %3lu: ", (unsigned long)curAttrIdx); dataTypeID = H5Aget_type(attrID); mjrHDF5_chkError(dataTypeID); /* Get the class for the type. */ class = H5Tget_class(dataTypeID); mjrHDF5_Tclass2str(class, classStr); printf(" Class: %-16s", classStr); /* Get the size of the type */ dataSize = H5Tget_size(dataTypeID); if(dataSize == 0) { printf("ERROR: Failure in H5Tget_size().\n"); exit(1); } /* end if */ printf(" Size: %3lu ", (unsigned long)dataSize); hErrVal = H5Tclose(dataTypeID); mjrHDF5_chkError(hErrVal); printf(" Name: %s \n", attrName); hErrVal = H5Aclose(attrID); mjrHDF5_chkError(hErrVal); } /* end for */ } /* end if */