static VALUE rb_H5Fget_obj_ids (VALUE mod, VALUE v_file_id, VALUE v_type, VALUE v_ids) { int count; hid_t *ids; count = H5Fget_obj_count(NUM2INT(v_file_id), NUM2ULONG(v_type)); if ( count < 0 ) rb_hdf5_raise("can't count object"); ids = xmalloc(count*sizeof(hid_t)); count = H5Fget_obj_ids(NUM2INT(v_file_id), NUM2ULONG(v_type), count, ids); if ( count >= 0 ) HID2ARY(count, ids, v_ids); free(ids); if ( count < 0 ) rb_hdf5_raise("can't get object ids"); return INT2NUM(count); }
/****if* H5Ff/h5fget_obj_ids_c * NAME * h5fget_obj_ids_c * PURPOSE * Call H5Fget_obj_count to get number of open objects within a file * INPUTS * file_id - identifier of the file to be closed * obj_type - type of the object * RETURNS * obj_ids - iarray of open objects identifiers * 0 on success, -1 on failure * AUTHOR * Elena Pourmal * Monday, September 30, 2002 * HISTORY * * Changed type of max_obj to size_t_f; added parameter for the * number of open objects * Thursday, September 25, 2008 EIP * * SOURCE */ int_f nh5fget_obj_ids_c ( hid_t_f *file_id , int_f *obj_type, size_t_f *max_objs, hid_t_f *obj_ids, size_t_f *num_objs) /******/ { int ret_value = 0; hid_t c_file_id; unsigned c_obj_type; size_t u; size_t c_max_objs; ssize_t c_num_objs; hid_t *c_obj_ids; c_file_id = (hid_t)*file_id; c_obj_type = (unsigned) *obj_type; c_max_objs = (size_t)*max_objs; c_obj_ids = (hid_t *)HDmalloc(sizeof(hid_t)*c_max_objs); c_num_objs = H5Fget_obj_ids(c_file_id, c_obj_type, c_max_objs, c_obj_ids); if(c_num_objs < 0) ret_value = -1; for(u = 0; u < c_max_objs; u++) obj_ids[u] = (hid_t_f)c_obj_ids[u]; HDfree(c_obj_ids); *num_objs = (size_t_f)c_num_objs; return ret_value; }
void FileHDF5::close() { if (!isOpen()) return; data.close(); metadata.close(); root.close(); unsigned types = H5F_OBJ_GROUP|H5F_OBJ_DATASET|H5F_OBJ_DATATYPE; ssize_t obj_count = H5Fget_obj_count(hid, types); if (obj_count < 0) { throw H5Exception("FileHDF5::close(): Could not get object count"); } std::vector<hid_t> objs(static_cast<size_t>(obj_count)); if (obj_count > 0) { obj_count = H5Fget_obj_ids(hid, types, objs.size(), objs.data()); if (obj_count < 0) { throw H5Exception("FileHDF5::close(): Could not get objs"); } } for (auto obj : objs) { int ref_count = H5Iget_ref(obj); for (int j = 0; j < ref_count; j++) { H5Oclose(obj); } } H5Object::close(); }
//-***************************************************************************** ArImpl::~ArImpl() { m_data.reset(); if ( m_file >= 0 ) { int dsetCount = H5Fget_obj_count( m_file, H5F_OBJ_LOCAL | H5F_OBJ_DATASET); int grpCount = H5Fget_obj_count( m_file, H5F_OBJ_LOCAL | H5F_OBJ_GROUP ); int dtypCount = H5Fget_obj_count( m_file, H5F_OBJ_LOCAL | H5F_OBJ_DATATYPE ); int attrCount = H5Fget_obj_count( m_file, H5F_OBJ_LOCAL | H5F_OBJ_ATTR ); int objCount = dsetCount + grpCount + dtypCount + attrCount; if ( objCount != 0 ) { std::stringstream strm; strm << "Open HDF5 handles detected during reading:" << std::endl << "DataSets: " << dsetCount << ", Groups: " << grpCount << ", DataTypes: " << dtypCount << ", Attributes: " << attrCount; std::vector< hid_t > objList; // when getting the name corresponding to a hid_t, the get_name // functions always append a NULL character, which we strip off // when injecting into out stream. std::string name; if ( dsetCount > 0 ) { strm << std::endl << "DataSets: " << std::endl; objList.resize( dsetCount ); H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_DATASET, dsetCount, &objList.front() ); for ( int i = 0; i < dsetCount; ++i ) { int strLen = H5Iget_name( objList[i], NULL, 0 ) + 1; name.resize( strLen ); H5Iget_name( objList[i], &(name[0]), strLen ); strm << name.substr(0, name.size() - 1) << std::endl; } } if ( grpCount > 0 ) { strm << std::endl << std::endl << "Groups:" << std::endl; objList.resize( grpCount ); H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_GROUP, grpCount, &objList.front() ); for ( int i = 0; i < grpCount; ++i ) { int strLen = H5Iget_name( objList[i], NULL, 0 ) + 1; name.resize( strLen ); H5Iget_name( objList[i], &(name[0]), strLen ); strm << std::endl << name.substr(0, name.size() - 1); } } if ( attrCount > 0 ) { strm << std::endl << std::endl << "Attrs:" << std::endl; objList.resize( attrCount ); H5Fget_obj_ids( m_file, H5F_OBJ_LOCAL | H5F_OBJ_ATTR, attrCount, &objList.front() ); for ( int i = 0; i < attrCount; ++i ) { int strLen = H5Aget_name( objList[i], 0, NULL ) + 1; name.resize( strLen ); H5Aget_name( objList[i], strLen, &(name[0]) ); strm << std::endl << name.substr(0, name.size() - 1); } } // just for formatting purposes if ( dtypCount > 0 ) { strm << std::endl; } m_file = -1; ABCA_THROW( strm.str() ); } H5Fclose( m_file ); m_file = -1; } }
bool ReadHDF5file(char* filename, char* fieldname, dtype** outArray, int* dims) { #ifdef _HDF5_H // Open the file hid_t file_id; file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); //? file_id = H5Fopen(filename,H5F_ACC_RDONLY,faplist_id); if(file_id < 0){ printf("ERROR: Could not open file %s\n",filename); return false; } // Open the dataset hid_t dataset_id; hid_t dataspace_id; dataset_id = H5Dopen1(file_id, fieldname); if(dataset_id < 0){ printf("ERROR: Could not open the data field %s\n",fieldname); return false; } dataspace_id = H5Dget_space(dataset_id); // Test if 2D data int ndims; ndims = H5Sget_simple_extent_ndims(dataspace_id); // Get dimensions of data set (nx, ny, nn) hsize_t* dimsl = new hsize_t[ndims]; H5Sget_simple_extent_dims(dataspace_id,dimsl,NULL); for(int i = 0;(i<ndims&&i<3);i++) dims[i] = dimsl[ndims-1-i]; //!!!!!!!! NOT SURE size_t nn = 1; for(int i = 0;i<ndims;i++) nn *= dimsl[i]; // Create space for the new data dtype* data = *outArray; if (data!=NULL) delete data;//free(data); *outArray = new dtype[nn]; data = *outArray; hid_t datatype_id; H5T_class_t dataclass; size_t size; datatype_id = H5Dget_type(dataset_id); dataclass = H5Tget_class(datatype_id); size = H5Tget_size(datatype_id); int rrr = sizeof(int); if(dataclass == H5T_FLOAT){ if (size == sizeof(float)) { float* buffer = (float *) calloc(nn, sizeof(float)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(double)) { double* buffer = (double *) calloc(nn, sizeof(double)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown floating point type, size=%i\n",(int) size); return false; } } else if(dataclass == H5T_INTEGER){ if (size == sizeof(short)) { short* buffer = (short*) calloc(nn, sizeof(short)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(int)) { int* buffer = (int *) calloc(nn, sizeof(int)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(long)) { long* buffer = (long *) calloc(nn, sizeof(long)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown integer type, size=%i\n",(int) size); return false; } } else { printf("2dData::readHDF5: unknown HDF5 data type\n"); return false; } // Close and cleanup H5Dclose(dataset_id); // Cleanup stale IDs hid_t ids[256]; int n_ids = H5Fget_obj_ids(file_id, H5F_OBJ_ALL, 256, ids); for (long i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); //if ( type == H5I_DATATYPE ) // H5Dclose(id); } H5Fclose(file_id); return true; #endif return false; }
void cData2d::readHDF5(char* filename, char* fieldname){ // Open the file hid_t file_id; file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); if(file_id < 0){ printf("ERROR: Could not open file %s\n",filename); return; } // Open the dataset hid_t dataset_id; hid_t dataspace_id; dataset_id = H5Dopen1(file_id, fieldname); dataspace_id = H5Dget_space(dataset_id); // Test if 2D data int ndims; ndims = H5Sget_simple_extent_ndims(dataspace_id); if(ndims != 2) { printf("2dData::readHDF5: Not 2D data set, ndims=%i\n",ndims); exit(0); } // Get dimensions of data set (nx, ny, nn) hsize_t dims[ndims]; H5Sget_simple_extent_dims(dataspace_id,dims,NULL); ny = dims[0]; nx = dims[1]; nn = 1; for(int i = 0;i<ndims;i++) nn *= dims[i]; // Create space for the new data free(data); data = NULL; data = (tData2d *) calloc(nn, sizeof(tData2d)); // Read in data after setting up a temporary buffer of the appropriate variable type // Somehow this works best when split out accordint to different data types // Fix into general form later hid_t datatype_id; H5T_class_t dataclass; size_t size; datatype_id = H5Dget_type(dataset_id); dataclass = H5Tget_class(datatype_id); size = H5Tget_size(datatype_id); if(dataclass == H5T_FLOAT){ if (size == sizeof(float)) { float* buffer = (float *) calloc(nn, sizeof(float)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(double)) { double* buffer = (double *) calloc(nn, sizeof(double)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown floating point type, size=%i\n",(int) size); return; } } else if(dataclass == H5T_INTEGER){ if (size == sizeof(char)) { char* buffer = (char*) calloc(nn, sizeof(char)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(short)) { short* buffer = (short*) calloc(nn, sizeof(short)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(int)) { int* buffer = (int *) calloc(nn, sizeof(int)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(long)) { long* buffer = (long *) calloc(nn, sizeof(long)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown integer type, size=%lu\n",size); exit(1); } } else { printf("2dData::readHDF5: unknown HDF5 data type\n"); return; } // Close and cleanup H5Dclose(dataset_id); // Cleanup stale IDs hid_t ids[256]; int n_ids = H5Fget_obj_ids(file_id, H5F_OBJ_ALL, 256, ids); for (long i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); //if ( type == H5I_DATATYPE ) // H5Dclose(id); } H5Fclose(file_id); }
/* * Write 2D data to HDF5 file */ void cData2d::writeHDF5(char* filename){ // Figure out the HDF5 data type hid_t out_type_id = 0; if(sizeof(tData2d) == sizeof(float)) out_type_id = H5T_NATIVE_FLOAT; else if(sizeof(tData2d) == sizeof(double)) out_type_id = H5T_NATIVE_DOUBLE; else { printf("2dData::writeHDF5: unsuppoted data type\n"); exit(1); } // Create the file and data group hid_t file_id; file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); H5Gcreate1(file_id,"data",0); // Data space dimensions int ndims = 2; hsize_t dims[ndims]; dims[0] = ny; dims[1] = nx; // Write the data hid_t dataspace_id; hid_t dataset_id; dataspace_id = H5Screate_simple(ndims, dims, NULL); dataset_id = H5Dcreate1(file_id, "/data/data", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id,out_type_id , H5S_ALL, H5S_ALL,H5P_DEFAULT, data)< 0){ printf("2dData::writeHDF5: Error writing data to file\n"); exit(1); } // Close and exit H5Dclose(dataset_id); // Cleanup stale IDs hid_t ids[256]; int n_ids = H5Fget_obj_ids(file_id, H5F_OBJ_ALL, 256, ids); for (long i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); //if ( type == H5I_DATATYPE ) // H5Dclose(id); } H5Fclose(file_id); }
/* * Actually save the powder pattern to file */ void savePowderPattern(cGlobal *global, int detIndex, int powderClass) { // Dereference common variables cPixelDetectorCommon *detector = &(global->detector[detIndex]); long nframes = detector->nPowderFrames[powderClass]; // Define buffer variables double *powderBuffer; double *powderSquaredBuffer; double *powderSigmaBuffer; double *bufferPeaks; char sBuffer[1024]; /* * Filename */ char filename[1024]; char filenamebase[1024]; sprintf(filenamebase,"r%04u-detector%ld-class%d", global->runNumber, global->detector[detIndex].detectorID, powderClass); sprintf(filename,"%s-sum.h5",filenamebase); printf("%s\n",filename); /* * Mess of stuff for writing the compound HDF5 file */ #ifdef H5F_ACC_SWMR_WRITE pthread_mutex_lock(&global->swmr_mutex); #endif hid_t fh, gh, sh, dh; /* File, group, dataspace and data handles */ //herr_t r; hsize_t size[2]; //hsize_t max_size[2]; hid_t h5compression; // Create file fh = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); if ( fh < 0 ) { ERROR("Couldn't create HDF5 file: %s\n", filename); } gh = H5Gcreate(fh, "data", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if ( gh < 0 ) { ERROR("Couldn't create HDF5 group\n"); H5Fclose(fh); } // Setting compression level if (global->h5compress) { h5compression = H5Pcreate(H5P_DATASET_CREATE); } else { h5compression = H5P_DEFAULT; } FOREACH_DATAFORMAT_T(i_f, cDataVersion::DATA_FORMATS) { if (isBitOptionSet(global->detector[detIndex].powderFormat, *i_f)) { cDataVersion dataV(NULL, &global->detector[detIndex], global->detector[detIndex].powderVersion, *i_f); while (dataV.next()) { if (*i_f != cDataVersion::DATA_FORMAT_RADIAL_AVERAGE) { // size for 2D data size[0] = dataV.pix_ny; size[1] = dataV.pix_nx; sh = H5Screate_simple(2, size, NULL); // Compression for 1D if (global->h5compress) { H5Pset_chunk(h5compression, 2, size); H5Pset_deflate(h5compression, global->h5compress); // Compression levels are 0 (none) to 9 (max) } } else { // size for 1D data (radial average) size[0] = dataV.pix_nn; size[1] = 0; sh = H5Screate_simple(1, size, NULL); // Compression for 1D h5compression = H5P_DEFAULT; } double *powder = dataV.getPowder(powderClass); double *powder_squared = dataV.getPowderSquared(powderClass); long *powder_counter = dataV.getPowderCounter(powderClass); pthread_mutex_t *mutex = dataV.getPowderMutex(powderClass); // Copy powder pattern to buffer powderBuffer = (double*) calloc(dataV.pix_nn, sizeof(double)); if (global->threadSafetyLevel > 0) pthread_mutex_lock(mutex); memcpy(powderBuffer, powder, dataV.pix_nn*sizeof(double)); if (global->threadSafetyLevel > 0) pthread_mutex_unlock(mutex); // Masked powders require a per-pixel correction if(global->detector[detIndex].savePowderMasked != 0 && powder_counter != NULL) { for (long i=0; i<dataV.pix_nn; i++) { if(powder_counter[i] != 0) powderBuffer[i] /= powder_counter[i]; else powderBuffer[i] = 0; } } // Write powder to dataset dh = H5Dcreate(gh, dataV.name, H5T_NATIVE_DOUBLE, sh, H5P_DEFAULT, h5compression, H5P_DEFAULT); if (dh < 0) ERROR("Could not create dataset.\n"); H5Dwrite(dh, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, powderBuffer); H5Dclose(dh); // Fluctuations (sigma) powderSquaredBuffer = (double*) calloc(dataV.pix_nn, sizeof(double)); powderSigmaBuffer = (double*) calloc(dataV.pix_nn, sizeof(double)); if (global->threadSafetyLevel > 0) pthread_mutex_lock(mutex); memcpy(powderSquaredBuffer, powder_squared, dataV.pix_nn*sizeof(double)); if (global->threadSafetyLevel > 0) pthread_mutex_unlock(mutex); // Masked powders require a per-pixel correction if(global->detector[detIndex].savePowderMasked != 0 && powder_counter != NULL) { for (long i=0; i<dataV.pix_nn; i++) { if(powder_counter[i] != 0) powderSigmaBuffer[i] = sqrt(powderSquaredBuffer[i]/powder_counter[i] - powderBuffer[i]*powderBuffer[i]); } } else { for (long i=0; i<dataV.pix_nn; i++) { powderSigmaBuffer[i] = sqrt(powderSquaredBuffer[i]/nframes - (powderBuffer[i]/nframes)*(powderBuffer[i]/nframes)); } } // Write to data set sprintf(sBuffer,"%s_sigma",dataV.name); dh = H5Dcreate(gh, sBuffer, H5T_NATIVE_DOUBLE, sh, H5P_DEFAULT, h5compression, H5P_DEFAULT); if (dh < 0) ERROR("Could not create dataset.\n"); H5Dwrite(dh, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, powderSigmaBuffer); H5Dclose(dh); // Soft link if main data set if (dataV.isMainDataset) { // Create symbolic link if this is the main dataset sprintf(sBuffer,"/data/%s",dataV.name); H5Lcreate_soft(sBuffer, fh, "/data/data",0,0); H5Lcreate_soft(sBuffer, fh, "/data/correcteddata",0,0); } free(powderBuffer); free(powderSquaredBuffer); free(powderSigmaBuffer); H5Sclose(sh); } } } // Peak powder size[0] = detector->pix_ny; size[1] = detector->pix_nx; sh = H5Screate_simple(2, size, NULL); bufferPeaks = (double*) calloc(detector->pix_nn, sizeof(double)); pthread_mutex_lock(&detector->powderPeaks_mutex[powderClass]); memcpy(bufferPeaks, detector->powderPeaks[powderClass], detector->pix_nn*sizeof(double)); pthread_mutex_unlock(&detector->powderPeaks_mutex[powderClass]); dh = H5Dcreate(gh, "peakpowder", H5T_NATIVE_DOUBLE, sh, H5P_DEFAULT, h5compression, H5P_DEFAULT); if (dh < 0) ERROR("Could not create dataset.\n"); H5Dwrite(dh, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, bufferPeaks); H5Dclose(dh); H5Sclose(sh); free(bufferPeaks); // Save frame count size[0] = 1; sh = H5Screate_simple(1, size, NULL ); dh = H5Dcreate(gh, "nframes", H5T_NATIVE_LONG, sh, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (dh < 0) ERROR("Could not create dataset.\n"); H5Dwrite(dh, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, &detector->nPowderFrames[powderClass] ); H5Dclose(dh); H5Sclose(sh); // Clean up stale HDF5 links H5Gclose(gh); int n_ids; hid_t ids[256]; n_ids = H5Fget_obj_ids(fh, H5F_OBJ_ALL, 256, ids); for ( int i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATATYPE ) H5Tclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); if ( type == H5I_ATTR ) H5Aclose(id); } H5Fclose(fh); #ifdef H5F_ACC_SWMR_WRITE pthread_mutex_unlock(&global->swmr_mutex); #endif /* * Clean up stuff */ for(long i=0; i<global->nPowderClasses; i++) { fflush(global->powderlogfp[i]); fflush(global->framelist[i]); } }
void readSpectrumEnergyScale(cGlobal *global, char *filename) { char groupname[1024]; char fieldname[1024]; hid_t file_id; hid_t datagroup_id; hid_t dataset_id; hid_t dataspace_id; hid_t datatype_id; H5T_class_t dataclass; size_t size; int ndims; sprintf(groupname, "energySpectrum"); sprintf(fieldname, "runIntegratedEnergyScale"); // Check if an energy scale calibration file has been specified if ( strcmp(filename,"") == 0 ){ printf("spectrum energy scale calibration file path was not specified\n"); printf("spectra will be output with default (0) energy scale\n"); return; } // Check whether file exists! FILE* fp = fopen(filename, "r"); if (fp) // file exists fclose(fp); else { // file doesn't exist printf("specified energy scale calibration file does not exist: %s\n",filename); printf("spectra will be output with default (0) energy scale\n"); return; } printf("Reading energy spectrum scale calibration file:\n"); printf("\t%s\n",filename); // Open the file file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); if(file_id < 0){ printf("ERROR: Could not open file %s\n",filename); printf("spectra will be output with default (0) energy scale\n"); return; } // Open the dataset datagroup_id = H5Gopen1(file_id, groupname); dataset_id = H5Dopen1(datagroup_id, fieldname); dataspace_id = H5Dget_space(dataset_id); // Test if correct dimensions / size ndims = H5Sget_simple_extent_ndims(dataspace_id); if(ndims != 1) { printf("the specified file does not have the correct dimensions for energy scale calibration, ndims=%i\n",ndims); printf("spectra will be output with default (0) energy scale\n"); return; } hsize_t dims[ndims]; H5Sget_simple_extent_dims(dataspace_id,dims,NULL); if (dims[0]!=1 || dims[1]!=(hsize_t)global->espectrumLength) { printf("the specified file does not have the correct dimensions for energy scale calibration\n"); printf("spectra will be output with default (0) energy scale\n"); return; } datatype_id = H5Dget_type(dataset_id); dataclass = H5Tget_class(datatype_id); size = H5Tget_size(datatype_id); double* energyscale = (double *) calloc(global->espectrumLength, sizeof(double)); H5Dread(dataset_id, datatype_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, energyscale); for(int i=0; i<global->espectrumLength; i++) { global->espectrumScale[i] = energyscale[i]; } free(energyscale); // Close and cleanup H5Dclose(dataset_id); H5Gclose(datagroup_id); // Cleanup stale IDs hid_t ids[256]; int n_ids = H5Fget_obj_ids(file_id, H5F_OBJ_ALL, 256, ids); for (long i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); //if ( type == H5I_DATATYPE ) // H5Dclose(id); } H5Fclose(file_id); printf("energy spectrum scale calibration file read successful:\n"); return; }