static herr_t visit_link(hid_t g_id, const char *name, const H5L_info_t *info, void *op_data) { /* A positive return value causes the visit iterator to immediately * return that positive value, indicating short-circuit * success. The iterator can be restarted at the next group * member. */ int ret = 1; hid_t id; strncpy(((struct nc_hdf5_link_info *)op_data)->name, name, NC_MAX_NAME); /* Open this critter. */ if ((id = H5Oopen_by_addr(g_id, info->u.address)) < 0) return NC_EHDFERR; /* Is this critter a group, type, data, attribute, or what? */ if ((((struct nc_hdf5_link_info *)op_data)->obj_type = H5Iget_type(id)) < 0) ret = NC_EHDFERR; /* Close the critter to release resouces. */ if (H5Oclose(id) < 0) return NC_EHDFERR; return ret; }
void mhdf_closeData( mhdf_FileHandle file, hid_t handle, mhdf_Status* status ) { FileHandle* file_ptr; herr_t rval = -1; file_ptr = (FileHandle*)(file); if (!mhdf_check_valid_file( file_ptr, status )) return; switch ( H5Iget_type( handle ) ) { case H5I_GROUP : rval = H5Gclose( handle ); break; case H5I_DATATYPE : rval = H5Tclose( handle ); break; case H5I_DATASPACE: rval = H5Sclose( handle ); break; case H5I_DATASET : rval = H5Dclose( handle ); break; default : rval = -1; } if (rval < 0) { mhdf_setFail( status, "H5Xclose failed. Invalid handle?\n"); } else { file_ptr->open_handle_count--; mhdf_setOkay( status ); } }
/* This is a callback function for H5Literate(). The parameters of this callback function have the following values or meanings: g_id Group that serves as root of the iteration; same value as the H5Lvisit group_id parameter name Name of link, relative to g_id, being examined at current step of the iteration info H5L_info_t struct containing information regarding that link op_data User-defined pointer to data required by the application in processing the link; a pass-through of the op_data pointer provided with the H5Lvisit function call */ herr_t op_func (hid_t g_id, const char *name, const H5L_info_t *info, void *op_data) { hid_t id; H5I_type_t obj_type; strcpy((char *)op_data, name); if ((id = H5Oopen_by_addr(g_id, info->u.address)) < 0) ERR; /* Using H5Ovisit is really slow. Use H5Iget_type for a fast * answer. */ /* if (H5Ovisit(id, H5_INDEX_CRT_ORDER, H5_ITER_INC, obj_iter, */ /* (void *)&obj_class) != 1) ERR; */ if ((obj_type = H5Iget_type(id)) < 0) ERR; if (H5Oclose(id) < 0) ERR; /* Turn this on to learn what type of object you've opened. */ /* switch (obj_type) */ /* { */ /* case H5I_GROUP: */ /* printf("group %s\n", name); */ /* break; */ /* case H5I_DATATYPE: */ /* printf("type %s\n", name); */ /* break; */ /* case H5I_DATASET: */ /* printf("data %s\n", name); */ /* break; */ /* default: */ /* printf("unknown class\n"); */ /* } */ return 1; }
//-------------------------------------------------------------------------- // Function: p_valid_id // Purpose: Verifies that the given id is a valid id so it can be passed // into an H5I C function. // Return true if id is valid, false, otherwise // Programmer Binh-Minh Ribler - May, 2005 //-------------------------------------------------------------------------- bool IdComponent::p_valid_id(const hid_t obj_id) { H5I_type_t id_type = H5Iget_type(obj_id); if (id_type <= H5I_BADID || id_type >= H5I_NTYPES) return false; else return true; }
//-------------------------------------------------------------------------- // Function: hdfObjectType ///\brief Given an id, returns the type of the object. ///\return a valid HDF object type, which may be one of the following: /// \li \c H5I_FILE /// \li \c H5I_GROUP /// \li \c H5I_DATATYPE /// \li \c H5I_DATASPACE /// \li \c H5I_DATASET /// \li \c H5I_ATTR /// \li or \c H5I_BADID, if no valid type can be determined or the /// input object id is invalid. // Programmer Binh-Minh Ribler - Jul, 2005 //-------------------------------------------------------------------------- H5I_type_t IdComponent::getHDFObjType(const hid_t obj_id) { H5I_type_t id_type = H5Iget_type(obj_id); if (id_type <= H5I_BADID || id_type >= H5I_NTYPES) return H5I_BADID; // invalid else return id_type; // valid type }
/*------------------------------------------------------------------------- * Function: H5FD_stdio_init * * Purpose: Initialize this driver by registering the driver with the * library. * * Return: Success: The driver ID for the stdio driver. * * Failure: Negative. * * Programmer: Robb Matzke * Thursday, July 29, 1999 * *------------------------------------------------------------------------- */ hid_t H5FD_stdio_init(void) { /* Clear the error stack */ H5Eclear2(H5E_DEFAULT); if (H5I_VFL!=H5Iget_type(H5FD_STDIO_g)) H5FD_STDIO_g = H5FDregister(&H5FD_stdio_g); return H5FD_STDIO_g; } /* end H5FD_stdio_init() */
void close(hid_t id) { if((id == -1) || (is_constant(id))) return; if(!H5Iis_valid(id)) H5CPP_THROW("attempted to close bad hdf5 id: " << id); H5I_type_t t = H5CPP_ERR_ON_NEG(H5Iget_type(id)); _close(id, t); }
/* * Class: hdf_hdf5lib_H5 * Method: H5Iget_type * Signature: (J)I */ JNIEXPORT jint JNICALL Java_hdf_hdf5lib_H5_H5Iget_1type (JNIEnv *env, jclass clss, jlong obj_id) { H5I_type_t retVal = H5I_BADID; retVal = H5Iget_type((hid_t)obj_id); if (retVal == H5I_BADID) h5libraryError(env); return (jint)retVal; } /* end Java_hdf_hdf5lib_H5_H5Iget_1type */
/* Test the H5Iget_type function */ static int test_get_type(void) { hid_t dtype; /* datatype id */ H5I_type_t type_ret; /* return value */ /* Create a datatype id */ dtype = H5Tcopy(H5T_NATIVE_INT); CHECK(dtype, FAIL, "H5Tcopy"); if (dtype < 0) goto out; /* Check that the ID is correct */ type_ret = H5Iget_type(dtype); VERIFY(type_ret, H5I_DATATYPE, "H5Iget_type"); if (type_ret == H5I_BADID) goto out; /* Check that the ID is correct */ type_ret = H5Iget_type((hid_t)H5T_STRING); VERIFY(type_ret, H5I_BADID, "H5Iget_type"); if (type_ret != H5I_BADID) goto out; /* Check that the ID is correct */ type_ret = H5Iget_type((hid_t)-1); VERIFY(type_ret, H5I_BADID, "H5Iget_type"); if (type_ret != H5I_BADID) goto out; H5Tclose(dtype); return 0; out: if(dtype != H5I_INVALID_HID) H5Tclose(dtype); return -1; }
/* * Routine: herr_t danu_slab_select(hid_t dataspace,dslab_t *slab) * Purpose: Select the hyperslab in dataspace * Description: Selects the hyperslab defined in the slab data struct slab. The call * replaces any hyperslab currently defined in dataspace and will not * allow overlapping blocks. * * Parameters: * dataspace IN HDF5 dataspace * slab IN pointer to hyperslab struct * * Returns: The returning value of the HDF5 select hyperslab call * Errors: Checks dataspace is a valid HDF5 id and the dimensions of dataspace and slab * will return immediately if these checks raise an error. */ herr_t danu_slab_select(hid_t id,dslab_t *slab) { int dim,close_space; herr_t status; hid_t dataspace; H5I_type_t type; /* Check input */ if ( H5_ISA_INVALID_ID(id) ) { DANU_ERROR_MESS("Invalid dataspace identifier"); return DANU_FAILURE; } if ( DANU_BAD_PTR(slab) ) { DANU_ERROR_MESS("Invalid slab pointer"); return DANU_FAILURE; } /* Determine if id is a dataspace or dataset */ type = H5Iget_type(id); if ( type == H5I_DATASET ) { dataspace = H5Dget_space(id); close_space = TRUE; } else { dataspace = id; close_space = FALSE; } dim = H5Sget_simple_extent_ndims(dataspace); if ( dim < 0 ) { DANU_ERROR_MESS("Failed to read dimensions from dataspace"); return DANU_FAILURE; } if ( dim != slab->dim ) { DANU_ERROR_MESS("Mismatch between hyperslab dimension and dataspace dimension"); return DANU_FAILURE; } status = H5Sselect_hyperslab(dataspace,H5S_SELECT_SET,slab->offset,slab->stride,slab->count,slab->block); if ( close_space ) { H5Sclose(dataspace); } return status; }
/* * Routine: dslab_t danu_slab_alloc(hid_t dataspace) * Purpose: Create a HDF5 Hyperslab associated with HDF5 dataspace * Description: Given a defined HDF5 dataspace, allocate a hyperslab * struct that contains pointers for offset, count, stride * and block size. The dimensions of these array will be 1xdim * where dim is the dimension of the dataspace. * * * Parameters: * dataspace IN HDF5 dataspace * * Returns: Returns a pointer to a slab structure * Errors: Will return immediately if dataspace is not a valid HDF5 id. If * an error occurs the returning pointer is NULL. */ dslab_t * danu_slab_alloc(hid_t id) { dslab_t *ptr = NULL; H5I_type_t type; hid_t dataspace; int dim, close_space; /* Check the id */ if ( H5_ISA_INVALID_ID(id) ) { DANU_ERROR_MESS("Invalid HDF5 identifier"); return ptr; } /* Id maybe a dataspace or a dataset */ type = H5Iget_type(id); if ( type == H5I_DATASET ) { close_space = TRUE; dataspace = H5Dget_space(id); } else { close_space = FALSE; dataspace = id; } /* Get the dimensions */ dim = H5Sget_simple_extent_ndims(dataspace); if ( dim < 0 ) { DANU_ERROR_MESS("Failed to read the dimensions from HDf5 dataspace"); return ptr; } ptr = DANU_MALLOC(dslab_t,1); ptr->dim = dim; ptr->offset = DANU_MALLOC(hsize_t,dim); ptr->block = DANU_MALLOC(hsize_t,dim); ptr->stride = DANU_MALLOC(hsize_t,dim); ptr->count = DANU_MALLOC(hsize_t,dim); /* Close the dataspace if opened here */ if ( close_space ) { H5Sclose(dataspace); } return ptr; }
/****if* H5If/h5iget_type_c * NAME * h5iget_type_c * PURPOSE * Call H5Iget_type to get the type of an object * INPUTS * obj_id - object identifier * OUTPUTS * type - object type * RETURNS * 0 on success, -1 on failure * AUTHOR * Xiangyang Su * Thursday, March 24, 2000 * HISTORY * * SOURCE */ int_f h5iget_type_c (hid_t_f *obj_id, int_f *type) /******/ { int ret_value = -1; hid_t c_obj_id; H5I_type_t c_type; /* * Call H5Iget_type function. */ c_obj_id = *obj_id; c_type = H5Iget_type(c_obj_id); if (c_type == H5I_BADID) return ret_value; *type = (int_f)c_type; ret_value = 0; return ret_value; }
/*------------------------------------------------------------------------- * Function: H5DOappend() * * Purpose: To append elements to a dataset. * * axis: the dataset dimension (zero-based) for the append * extension: the # of elements to append for the axis-th dimension * memtype: the datatype * buf: buffer with data for the append * * Return: Non-negative on success/Negative on failure * * Programmer: Vailin Choi; Jan 2014 * * Note: * This routine is copied from the fast forward feature branch: features/hdf5_ff * src/H5FF.c:H5DOappend() with the following modifications: * 1) Remove and replace macro calls such as * FUNC_ENTER_API, H5TRACE, HGOTO_ERROR * accordingly because hl does not have these macros * 2) Replace H5I_get_type() by H5Iget_type() * 3) Replace H5P_isa_class() by H5Pisa_class() * 4) Fix a bug in the following: replace extension by size[axis] * if(extension < old_size) { * ret_value = FAIL; * goto done; * } * *------------------------------------------------------------------------- */ herr_t H5DOappend(hid_t dset_id, hid_t dxpl_id, unsigned axis, size_t extension, hid_t memtype, const void *buf) { hbool_t created_dxpl = FALSE; /* Whether we created a DXPL */ hsize_t size[H5S_MAX_RANK]; /* The new size (after extension */ hsize_t old_size = 0; /* The size of the dimension to be extended */ int sndims; /* Number of dimensions in dataspace (signed) */ unsigned ndims; /* Number of dimensions in dataspace */ hid_t space_id = FAIL; /* Old file space */ hid_t new_space_id = FAIL; /* New file space (after extension) */ hid_t mem_space_id = FAIL; /* Memory space for data buffer */ hssize_t snelmts; /* Number of elements in selection (signed) */ hsize_t nelmts; /* Number of elements in selection */ hid_t dapl = FAIL; /* Dataset access property list */ hsize_t start[H5S_MAX_RANK]; /* H5Sselect_Hyperslab: starting offset */ hsize_t count[H5S_MAX_RANK]; /* H5Sselect_hyperslab: # of blocks to select */ hsize_t stride[H5S_MAX_RANK]; /* H5Sselect_hyperslab: # of elements to move when selecting */ hsize_t block[H5S_MAX_RANK]; /* H5Sselect_hyperslab: # of elements in a block */ hsize_t *boundary = NULL; /* Boundary set in append flush property */ H5D_append_cb_t append_cb; /* Callback function set in append flush property */ void *udata; /* User data set in append flush property */ hbool_t hit = FALSE; /* Boundary is hit or not */ hsize_t k; /* Local index variable */ unsigned u; /* Local index variable */ herr_t ret_value = FAIL; /* Return value */ /* check arguments */ if(H5I_DATASET != H5Iget_type(dset_id)) goto done; /* If the user passed in a default DXPL, create one to pass to H5Dwrite() */ if(H5P_DEFAULT == dxpl_id) { if((dxpl_id = H5Pcreate(H5P_DATASET_XFER)) < 0) goto done; created_dxpl = TRUE; } /* end if */ else if(TRUE != H5Pisa_class(dxpl_id, H5P_DATASET_XFER)) goto done; /* Get the dataspace of the dataset */ if(FAIL == (space_id = H5Dget_space(dset_id))) goto done; /* Get the rank of this dataspace */ if((sndims = H5Sget_simple_extent_ndims(space_id)) < 0) goto done; ndims = (unsigned)sndims; /* Verify correct axis */ if(axis >= ndims) goto done; /* Get the dimensions sizes of the dataspace */ if(H5Sget_simple_extent_dims(space_id, size, NULL) < 0) goto done; /* Adjust the dimension size of the requested dimension, * but first record the old dimension size */ old_size = size[axis]; size[axis] += extension; if(size[axis] < old_size) goto done; /* Set the extent of the dataset to the new dimension */ if(H5Dset_extent(dset_id, size) < 0) goto done; /* Get the new dataspace of the dataset */ if(FAIL == (new_space_id = H5Dget_space(dset_id))) goto done; /* Select a hyperslab corresponding to the append operation */ for(u = 0 ; u < ndims ; u++) { start[u] = 0; stride[u] = 1; count[u] = size[u]; block[u] = 1; if(u == axis) { count[u] = extension; start[u] = old_size; } /* end if */ } /* end for */ if(FAIL == H5Sselect_hyperslab(new_space_id, H5S_SELECT_SET, start, stride, count, block)) goto done; /* The # of elemnts in the new extended dataspace */ if((snelmts = H5Sget_select_npoints(new_space_id)) < 0) goto done; nelmts = (hsize_t)snelmts; /* create a memory space */ if(FAIL == (mem_space_id = H5Screate_simple(1, &nelmts, NULL))) goto done; /* Write the data */ if(H5Dwrite(dset_id, memtype, mem_space_id, new_space_id, dxpl_id, buf) < 0) goto done; /* Obtain the dataset's access property list */ if((dapl = H5Dget_access_plist(dset_id)) < 0) goto done; /* Allocate the boundary array */ boundary = (hsize_t *)HDmalloc(ndims * sizeof(hsize_t)); /* Retrieve the append flush property */ if(H5Pget_append_flush(dapl, ndims, boundary, &append_cb, &udata) < 0) goto done; /* No boundary for this axis */ if(boundary[axis] != 0) { /* Determine whether a boundary is hit or not */ for(k = start[axis]; k < size[axis]; k++) if(!((k + 1) % boundary[axis])) { hit = TRUE; break; } if(hit) { /* Hit the boundary */ /* Invoke callback if there is one */ if(append_cb && append_cb(dset_id, size, udata) < 0) goto done; /* Do a dataset flush */ if(H5Dflush(dset_id) < 0) goto done; } /* end if */ } /* end if */ /* Indicate success */ ret_value = SUCCEED; done: /* Close dxpl if we created it vs. one was passed in */ if(created_dxpl) { if(H5Pclose(dxpl_id) < 0) ret_value = FAIL; } /* end if */ /* Close old dataspace */ if(space_id != FAIL && H5Sclose(space_id) < 0) ret_value = FAIL; /* Close new dataspace */ if(new_space_id != FAIL && H5Sclose(new_space_id) < 0) ret_value = FAIL; /* Close memory dataspace */ if(mem_space_id != FAIL && H5Sclose(mem_space_id) < 0) ret_value = FAIL; /* Close the dataset access property list */ if(dapl != FAIL && H5Pclose(dapl) < 0) ret_value = FAIL; if(boundary) HDfree(boundary); return ret_value; } /* H5DOappend() */
SWIGEXPORT hid_t JNICALL Java_permafrost_hdf_libhdf_ObjectLibExtensionJNI_JHOopen_1by_1idx( JNIEnv *jenv, jclass jcls, jint loc_id, jstring group_name, jint idx_type, jint order, jlong n, jint lapl_id, jintArray type, jobjectArray name ) { jint sz = 0; char* pGroupName = NULL; hid_t obj_id = -1; jint jiType; char* pName = NULL; char* tmpName = NULL; const int szName = 64; int lenName = 0; jstring jname; if (!group_name) { SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, "Null group name."); return (-1); } else if (!type) { SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, "Null type array."); return (-1); } else if (!name) { SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, "Null object name array."); return (-1); } sz = (*jenv)->GetArrayLength(jenv, type); if (sz < 1) { SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, "Zero-length type array."); return (-1); } sz = (*jenv)->GetArrayLength(jenv, name); if (sz < 1) { SWIG_JavaThrowException(jenv, SWIG_JavaIllegalArgumentException, "Zero-length object name array."); return (-1); } pGroupName = (char *)(*jenv)->GetStringUTFChars(jenv, group_name, 0); if (!pGroupName) return -1; obj_id = H5Oopen_by_idx( (hid_t)loc_id, pGroupName, (H5_index_t) idx_type, (H5_iter_order_t) order, (hsize_t) n, (hid_t) lapl_id ); if (pGroupName) (*jenv)->ReleaseStringUTFChars(jenv, group_name, (const char *) pGroupName); if (obj_id <= 0) return (obj_id); jiType = (jint) H5Iget_type(obj_id); (*jenv)->SetIntArrayRegion(jenv, type, 0, 1, &jiType); pName = (char*) calloc(szName, sizeof(char)); lenName = H5Iget_name(obj_id, pName, szName-1); if (lenName >= szName) { char* tmpName = (char*) realloc(pName, sizeof(char)*lenName+1); if (!tmpName) { free(pName); pName = NULL; H5Oclose(loc_id); return (-1); } pName = tmpName; lenName = H5Iget_name(obj_id, pName, lenName); } jname = (*jenv)->NewStringUTF(jenv, pName); (*jenv)->SetObjectArrayElement(jenv, name, 0, jname); free(pName); return (obj_id); }
value hdf5_h5i_get_type(value obj_v) { CAMLparam1(obj_v); CAMLreturn(Val_h5i_type(H5Iget_type(Hid_val(obj_v)))); }
/* * 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]); } }
file::file(h5_object obj) : h5_object(std::move(obj)) { if (!H5Iis_valid(this->id)) TRIQS_RUNTIME_ERROR << "Invalid input in h5::file constructor from id"; if (H5Iget_type(this->id) != H5I_FILE) TRIQS_RUNTIME_ERROR << "h5::file constructor must take the id of a file "; }
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; }
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); }