int luaC_h5_open_group(lua_State *L) { const char *gname = luaL_checkstring(L, 1); const char *mode = luaL_checkstring(L, 2); hid_t grp = 0; if (PresentFile < 0) { luaL_error(L, "need an open file to open group\n"); } else if (strcmp(mode, "w") == 0) { if (H5Lexists(PresentFile, gname, H5P_DEFAULT)) { H5Ldelete(PresentFile, gname, H5P_DEFAULT); } grp = H5Gcreate(PresentFile, gname, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } else if (strcmp(mode, "r+") == 0) { if (H5Lexists(PresentFile, gname, H5P_DEFAULT)) { grp = H5Gopen(PresentFile, gname, H5P_DEFAULT); } else { grp = H5Gcreate(PresentFile, gname, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } } else { luaL_error(L, "invalid group access mode '%s'\n", mode); } lua_pushnumber(L, grp); return 1; }
CXI_Data * cxi_open_data(CXI_Data_Reference * ref){ cxi_debug("opening data"); char buffer[1024]; if(!ref){ return NULL; } CXI_Data * data = calloc(sizeof(CXI_Data),1); if(!data){ return NULL; } data->handle = H5Gopen(ref->parent_handle,ref->group_name,H5P_DEFAULT); if(data->handle < 0){ free(data); return NULL; } ref->data = data; if(H5Lexists(data->handle,"data",H5P_DEFAULT)){ data->data = calloc(sizeof(CXI_Dataset_Reference),1); data->data->parent_handle = data->handle; data->data->group_name = malloc(sizeof(char)*(strlen(buffer)+1)); strcpy(data->data->group_name,"data"); } if(H5Lexists(data->handle,"errors",H5P_DEFAULT)){ data->errors = calloc(sizeof(CXI_Dataset_Reference),1); data->errors->parent_handle = data->handle; data->errors->group_name = malloc(sizeof(char)*(strlen(buffer)+1)); strcpy(data->errors->group_name,"errors"); } return data; }
/** read solution sizes */ static int read_nvnunrnl (hid_t file_id, int *nv, int *nr, int *nl) { if (H5Lexists (file_id, "/fclib_global", H5P_DEFAULT)) { IO (H5LTread_dataset_int (file_id, "/fclib_global/M/n", nv)); IO (H5LTread_dataset_int (file_id, "/fclib_global/H/n", nr)); if (H5Lexists (file_id, "/fclib_global/G", H5P_DEFAULT)) { IO (H5LTread_dataset_int (file_id, "/fclib_global/G/n", nl)); } else *nl = 0; } else if (H5Lexists (file_id, "/fclib_local", H5P_DEFAULT)) { *nv = 0; IO (H5LTread_dataset_int (file_id, "/fclib_local/W/n", nr)); if (H5Lexists (file_id, "/fclib_local/R", H5P_DEFAULT)) { IO (H5LTread_dataset_int (file_id, "/fclib_local/R/n", nl)); } else *nl = 0; } else { fprintf (stderr, "ERROR: neither global nor local problem has been stored. Global or local have to be stored before solutions or guesses\n"); return 0; } return 1; }
CXI_Image * cxi_open_image(CXI_Image_Reference * ref){ cxi_debug("opening image"); char buffer[1024]; if(!ref){ return NULL; } CXI_Image * image = calloc(sizeof(CXI_Image),1); if(!image){ return NULL; } image->handle = H5Gopen(ref->parent_handle,ref->group_name,H5P_DEFAULT); if(image->handle < 0){ free(image); return NULL; } /* Search for Detector groups */ int n = find_max_suffix(image->handle, "detector"); image->detector_count = n; image->detectors = calloc(sizeof(CXI_Detector_Reference *),n); for(int i = 0;i<n;i++){ image->detectors[i] = calloc(sizeof(CXI_Detector_Reference),1); sprintf(buffer,"detector_%d",i+1); image->detectors[i]->parent_handle = image->handle; image->detectors[i]->group_name = malloc(sizeof(char)*(strlen(buffer)+1)); strcpy(image->detectors[i]->group_name,buffer); } ref->image = image; if(H5Lexists(image->handle,"data",H5P_DEFAULT)){ image->data = calloc(sizeof(CXI_Dataset_Reference),1); image->data->parent_handle = image->handle; image->data->group_name = malloc(sizeof(char)*(strlen(buffer)+1)); strcpy(image->data->group_name,"data"); } if(H5Lexists(image->handle,"data_error",H5P_DEFAULT)){ image->data_error = calloc(sizeof(CXI_Dataset_Reference),1); image->data_error->parent_handle = image->handle; image->data_error->group_name = malloc(sizeof(char)*(strlen(buffer)+1)); strcpy(image->data_error->group_name,"data_error"); } if(H5Lexists(image->handle,"mask",H5P_DEFAULT)){ image->mask = calloc(sizeof(CXI_Dataset_Reference),1); image->mask->parent_handle = image->handle; image->mask->group_name = malloc(sizeof(char)*(strlen(buffer)+1)); strcpy(image->mask->group_name,"mask"); } // try_read_string(image->handle, "data_space",&image->data_space); // try_read_string(image->handle, "data_type",&image->data_type); image->dimensionality_valid = try_read_int(image->handle, "dimensionality",&image->dimensionality); image->image_center_valid = try_read_float_array(image->handle, "image_center",image->image_center,3); return image; }
/** Traverse the path of an object in HDF5 file, checking existence of groups in the path and creating them if required. */ hid_t HDF5DataWriter::getDataset(string path) { if (filehandle_ < 0){ return -1; } herr_t status = H5Eset_auto2(H5E_DEFAULT, NULL, NULL); // Create the groups corresponding to this path string::size_type lastslash = path.find_last_of("/"); vector<string> pathTokens; moose::tokenize(path, "/", pathTokens); hid_t prev_id = filehandle_; hid_t id = -1; for ( unsigned int ii = 0; ii < pathTokens.size()-1; ++ii ){ // check if object exists htri_t exists = H5Lexists(prev_id, pathTokens[ii].c_str(), H5P_DEFAULT); if (exists > 0){ // try to open existing group id = H5Gopen2(prev_id, pathTokens[ii].c_str(), H5P_DEFAULT); } else if (exists == 0) { // If that fails, try to create a group id = H5Gcreate2(prev_id, pathTokens[ii].c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } if ((exists < 0) || (id < 0)){ // Failed to open/create a group, print the // offending path (for debugging; the error is // perhaps at the level of hdf5 or file system). cerr << "Error: failed to open/create group: "; for (unsigned int jj = 0; jj <= ii; ++jj){ cerr << "/" << pathTokens[jj]; } cerr << endl; prev_id = -1; } if (prev_id >= 0 && prev_id != filehandle_){ // Successfully opened/created new group, close the old group status = H5Gclose(prev_id); assert( status >= 0 ); } prev_id = id; } string name = pathTokens[pathTokens.size()-1]; htri_t exists = H5Lexists(prev_id, name.c_str(), H5P_DEFAULT); hid_t dataset_id = -1; if (exists > 0){ dataset_id = H5Dopen2(prev_id, name.c_str(), H5P_DEFAULT); } else if (exists == 0){ dataset_id = createDoubleDataset(prev_id, name); } else { cerr << "Error: H5Lexists returned " << exists << " for path \"" << path << "\"" << endl; } return dataset_id; }
int regn_writes_all() { OPEN_WRITE_TEST_FILE; int **regions = fixture_regions(5); int result = (ch5m_regn_set_all(file, 5, regions[0]) == 0); htri_t exists = H5Lexists(file, CH5_REGN_GROUP_NAME, H5P_DEFAULT); if (exists < 1) { fprintf(stderr, "Could not find regions main group\n"); result = 0; } hid_t main_group_id = H5Gopen(file, CH5_REGN_GROUP_NAME, H5P_DEFAULT); exists = H5Lexists(main_group_id, CH5_REGN_DSET_NAME, H5P_DEFAULT); if (exists < 1) { fprintf(stderr, "Could not find regions dataset\n"); result = 0; } hid_t dset_id = H5Dopen(main_group_id, CH5_REGN_DSET_NAME, H5P_DEFAULT); if (dset_id < 0) { fprintf(stderr, "Error opening regions dataset\n"); result = 0; } ch5_dataset dset_info; result &= (ch5_gnrc_get_dset_info(file, CH5_REGN_DSET_FULL_PATH, &dset_info) == 0); if ((dset_info.count != 5) || (dset_info.width != 2)) { fprintf(stderr, "Regions dataset dimensions incorrect (%d,%d)\n", dset_info.count, dset_info.width); result = 0; } int read_regions[5 * 2]; herr_t status = H5Dread(dset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, read_regions); if (status < 0) { fprintf(stderr, "Regions reading failed\n"); result = 0; } if (int_arrays_same(regions[0], read_regions, 5 * 2) == 0) { fprintf(stderr, "Regions data incorrect\n"); result = 0; } H5Dclose(dset_id); H5Gclose(main_group_id); fixture_free_regions(regions); CLOSE_WRITE_TEST_FILE; return result; }
/** read local problem; * return problem on success; NULL on failure */ struct fclib_local* fclib_read_local (const char *path) { struct fclib_local *problem; hid_t file_id, main_id, id; if ((file_id = H5Fopen (path, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) { fprintf (stderr, "ERROR: opening file failed\n"); return NULL; } if (!H5Lexists (file_id, "/fclib_local", H5P_DEFAULT)) { fprintf (stderr, "ERROR: spurious input file %s :: fclib_local group does not exists", path); return NULL; } MM (problem = calloc (1, sizeof (struct fclib_local))); IO (main_id = H5Gopen (file_id, "/fclib_local", H5P_DEFAULT)); IO (H5LTread_dataset_int (file_id, "/fclib_local/spacedim", &problem->spacedim)); IO (id = H5Gopen (file_id, "/fclib_local/W", H5P_DEFAULT)); problem->W = read_matrix (id); IO (H5Gclose (id)); if (H5Lexists (file_id, "/fclib_local/V", H5P_DEFAULT)) { IO (id = H5Gopen (file_id, "/fclib_local/V", H5P_DEFAULT)); problem->V = read_matrix (id); IO (H5Gclose (id)); IO (id = H5Gopen (file_id, "/fclib_local/R", H5P_DEFAULT)); problem->R = read_matrix (id); IO (H5Gclose (id)); } IO (id = H5Gopen (file_id, "/fclib_local/vectors", H5P_DEFAULT)); read_local_vectors (id, problem); IO (H5Gclose (id)); if (H5Lexists (file_id, "/fclib_local/info", H5P_DEFAULT)) { IO (id = H5Gopen (file_id, "/fclib_local/info", H5P_DEFAULT)); problem->info = read_problem_info (id); IO (H5Gclose (id)); } IO (H5Gclose (main_id)); IO (H5Fclose (file_id)); return problem; }
// Check for path validity char AH5_path_valid(hid_t loc_id, const char *path) { char *temp; int i, slashes = 0; temp = strdup(path); for (i = (int) strlen(path); i > 0; i--) { if (temp[i] == '/') { temp[i] = '\0'; slashes++; /* count number of slashes excluding the first one */ } } if (strcmp(path, ".") == 0) { if (!H5Iis_valid(loc_id)) { free(temp); return AH5_FALSE; } } else { if(H5Lexists(loc_id, temp, H5P_DEFAULT) != AH5_TRUE) { free(temp); return AH5_FALSE; } } i = 1; while (slashes > 0) { while (temp[i] != '\0') i++; temp[i] = '/'; slashes--; if(H5Lexists(loc_id, temp, H5P_DEFAULT) != AH5_TRUE) { free(temp); return AH5_FALSE; } } free(temp); return AH5_TRUE; }
// Author & Date: Ehsan Azar Nov 13, 2012 // Purpose: Create type for BmiSpike16_t and commit // Note: For performance reasons and because spike length is constant during // and experiment we use fixed-length array here instead of varible-length // Inputs: // loc - where to add the type // spikeLength - the spike length to use // Outputs: // Returns the type id hid_t CreateSpike16Type(hid_t loc, UINT16 spikeLength) { herr_t ret; hid_t tid = -1; // e.g. for spike length of 48 type name will be "BmiSpike16_48_t" char szNum[4] = {'\0'}; sprintf(szNum, "%u", spikeLength); std::string strLink = "BmiSpike16_"; strLink += szNum; strLink += "_t"; // If already there return it if(H5Lexists(loc, strLink.c_str(), H5P_DEFAULT)) { tid = H5Topen(loc, strLink.c_str(), H5P_DEFAULT); return tid; } hsize_t dims[1] = {spikeLength}; hid_t tid_arr_wave = H5Tarray_create(H5T_NATIVE_INT16, 1, dims); tid = H5Tcreate(H5T_COMPOUND, offsetof(BmiSpike16_t, wave) + sizeof(INT16) * spikeLength); ret = H5Tinsert(tid, "TimeStamp", offsetof(BmiSpike16_t, dwTimestamp), H5T_NATIVE_UINT32); ret = H5Tinsert(tid, "Unit", offsetof(BmiSpike16_t, unit), H5T_NATIVE_UINT8); ret = H5Tinsert(tid, "Wave", offsetof(BmiSpike16_t, wave), tid_arr_wave); ret = H5Tcommit(loc, strLink.c_str(), tid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Tclose(tid_arr_wave); return tid; }
char HDocument::checkExist(const std::string & path) { if(!H5Lexists(fFileId, path.c_str(), H5P_DEFAULT)) return 0; return 1; }
int H5Lexists_safe(hid_t base, char *path) // ----------------------------------------------------------------------------- // The HDF5 specification only allows H5Lexists to be called on an immediate // child of the current object. However, you may wish to see whether a whole // relative path exists, returning false if any of the intermediate links are // not present. This function does that. // http://www.hdfgroup.org/HDF5/doc/RM/RM_H5L.html#Link-Exists // ----------------------------------------------------------------------------- { hid_t last = base, next; char *pch; char pathc[2048]; strcpy(pathc, path); pch = strtok(pathc, "/"); while (pch != NULL) { int exists = H5Lexists(last, pch, H5P_DEFAULT); if (!exists) { if (last != base) H5Gclose(last); return 0; } else { next = H5Gopen(last, pch, H5P_DEFAULT); if (last != base) H5Gclose(last); last = next; } pch = strtok(NULL, "/"); } if (last != base) H5Gclose(last); return 1; }
// Author & Date: Ehsan Azar Nov 13, 2012 // Purpose: Create type for BmiRootAttr_t and commit // Inputs: // loc - where to add the type // Outputs: // Returns the type id hid_t CreateRootAttrType(hid_t loc) { herr_t ret; hid_t tid = -1; std::string strLink = "BmiRootAttr_t"; // If already there return it if(H5Lexists(loc, strLink.c_str(), H5P_DEFAULT)) { tid = H5Topen(loc, strLink.c_str(), H5P_DEFAULT); return tid; } hid_t tid_attr_str = H5Tcopy(H5T_C_S1); ret = H5Tset_size(tid_attr_str, 64); hid_t tid_attr_comment_str = H5Tcopy(H5T_C_S1); ret = H5Tset_size(tid_attr_comment_str, 1024); tid = H5Tcreate(H5T_COMPOUND, sizeof(BmiRootAttr_t)); ret = H5Tinsert(tid, "MajorVersion", offsetof(BmiRootAttr_t, nMajorVersion), H5T_NATIVE_UINT32); ret = H5Tinsert(tid, "MinorVersion", offsetof(BmiRootAttr_t, nMinorVersion), H5T_NATIVE_UINT32); ret = H5Tinsert(tid, "Flags", offsetof(BmiRootAttr_t, nFlags), H5T_NATIVE_UINT32); ret = H5Tinsert(tid, "GroupCount", offsetof(BmiRootAttr_t, nGroupCount), H5T_NATIVE_UINT32); ret = H5Tinsert(tid, "Date", offsetof(BmiRootAttr_t, szDate), tid_attr_str); // date of the file creation ret = H5Tinsert(tid, "Application", offsetof(BmiRootAttr_t, szApplication), tid_attr_str); // application that created the file ret = H5Tinsert(tid, "Comment", offsetof(BmiRootAttr_t, szComment), tid_attr_comment_str); // file comments ret = H5Tcommit(loc, strLink.c_str(), tid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Tclose(tid_attr_str); H5Tclose(tid_attr_comment_str); return tid; }
// Author & Date: Ehsan Azar Nov 13, 2012 // Purpose: Create type for BmiChanExt2Attr_t and commit // Inputs: // loc - where to add the type // Outputs: // Returns the type id hid_t CreateChanExt2AttrType(hid_t loc) { herr_t ret; hid_t tid = -1; std::string strLink = "BmiChanExt2Attr_t"; // If already there return it if(H5Lexists(loc, strLink.c_str(), H5P_DEFAULT)) { tid = H5Topen(loc, strLink.c_str(), H5P_DEFAULT); return tid; } hid_t tid_attr_unit_str = H5Tcopy(H5T_C_S1); ret = H5Tset_size(tid_attr_unit_str, 16); tid = H5Tcreate(H5T_COMPOUND, sizeof(BmiChanExt2Attr_t)); ret = H5Tinsert(tid, "DigitalMin", offsetof(BmiChanExt2Attr_t, digmin), H5T_NATIVE_INT32); ret = H5Tinsert(tid, "DigitalMax", offsetof(BmiChanExt2Attr_t, digmax), H5T_NATIVE_INT32); ret = H5Tinsert(tid, "AnalogMin", offsetof(BmiChanExt2Attr_t, anamin), H5T_NATIVE_INT32); ret = H5Tinsert(tid, "AnalogMax", offsetof(BmiChanExt2Attr_t, anamax), H5T_NATIVE_INT32); ret = H5Tinsert(tid, "AnalogUnit", offsetof(BmiChanExt2Attr_t, anaunit), tid_attr_unit_str); ret = H5Tcommit(loc, strLink.c_str(), tid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Tclose(tid_attr_unit_str); return tid; }
// Author & Date: Ehsan Azar Nov 13, 2012 // Purpose: Create type for BmiTrackingAttr_t and commit // Inputs: // loc - where to add the type // Outputs: // Returns the type id hid_t CreateTrackingAttrType(hid_t loc) { herr_t ret; hid_t tid = -1; std::string strLink = "BmiTrackingAttr_t"; // If already there return it if(H5Lexists(loc, strLink.c_str(), H5P_DEFAULT)) { tid = H5Topen(loc, strLink.c_str(), H5P_DEFAULT); return tid; } hid_t tid_attr_label_str = H5Tcopy(H5T_C_S1); ret = H5Tset_size(tid_attr_label_str, 128); tid = H5Tcreate(H5T_COMPOUND, sizeof(BmiTrackingAttr_t)); ret = H5Tinsert(tid, "Label", offsetof(BmiTrackingAttr_t, szLabel), tid_attr_label_str); ret = H5Tinsert(tid, "Type", offsetof(BmiTrackingAttr_t, type), H5T_NATIVE_UINT16); ret = H5Tinsert(tid, "TrackID", offsetof(BmiTrackingAttr_t, trackID), H5T_NATIVE_UINT16); ret = H5Tinsert(tid, "MaxPoints", offsetof(BmiTrackingAttr_t, maxPoints), H5T_NATIVE_UINT16); ret = H5Tcommit(loc, strLink.c_str(), tid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Tclose(tid_attr_label_str); return tid; }
//-***************************************************************************** bool ObjectExists( H5Node& iParent, const std::string &iName ) { ABCA_ASSERT( iParent.isValidObject(), "Invalid parent node passed into HDF5Util GroupExists: " << iName << std::endl ); HDF5Hierarchy* H5HPtr = iParent.getH5HPtr(); if ( H5HPtr ) { return H5HPtr->childExists( iParent.getRef(), iName ); } else { // First, check to make sure the link exists. hid_t iParentObject = iParent.getObject(); htri_t exi = H5Lexists( iParentObject, iName.c_str(), H5P_DEFAULT ); if ( exi < 1 ) { return false; } else { return true; } } }
//-***************************************************************************** bool DatasetExists( hid_t iParent, const std::string &iName ) { ABCA_ASSERT( iParent >= 0, "Invalid Parent in DatasetExists" ); // First, check to make sure the link exists. htri_t exi = H5Lexists( iParent, iName.c_str(), H5P_DEFAULT ); if ( exi < 1 ) { return false; } // Now make sure it is a group. H5O_info_t oinfo; herr_t status = H5Oget_info_by_name( iParent, iName.c_str(), &oinfo, H5P_DEFAULT ); if ( status < 0 ) { return false; } if ( oinfo.type != H5O_TYPE_DATASET ) { return false; } return true; }
hdf5_dataset::hdf5_dataset( hdf5_file const& file, std::string const& path ) : path_(path) { // Check if name exists in this file. htri_t status = H5Lexists(file.get_id(), path.c_str(), H5P_DEFAULT); if(status > 0) { // Full path exists. // Attempt to open it as a dataset set_id(H5Dopen2(file.get_id(), path.c_str(), H5P_DEFAULT)); if(get_id() < 0) { boost::serialization::throw_exception( hdf5_archive_exception( hdf5_archive_exception::hdf5_archive_dataset_access_error, path.c_str() ) ); } } else { // path does not exist, or other error boost::serialization::throw_exception( hdf5_archive_exception( hdf5_archive_exception::hdf5_archive_bad_path_error, path.c_str() ) ); } }
H5Link::H5Link(H5Object & _parent, const std::string & _name) : H5Object(_parent, _name) { if (H5Lexists(_parent.getH5Id(), name.c_str(), H5P_DEFAULT) <= 0) { throw H5Exception(__LINE__, __FILE__, _("The link %s does not exist."), name.c_str()); } }
void DCDataSet::createReference(hid_t refGroup, hid_t srcGroup, DCDataSet &srcDataSet) throw (DCException) { if (opened) throw DCException(getExceptionString("createReference: dataset is already open")); if (checkExistence && H5Lexists(refGroup, name.c_str(), H5P_LINK_ACCESS_DEFAULT)) throw DCException(getExceptionString("createReference: this reference already exists")); getLogicalSize().set(srcDataSet.getLogicalSize()); this->ndims = srcDataSet.getNDims(); if (H5Rcreate(®ionRef, srcGroup, srcDataSet.getName().c_str(), H5R_OBJECT, -1) < 0) throw DCException(getExceptionString("createReference: failed to create region reference")); hsize_t ndims = 1; dataspace = H5Screate_simple(1, &ndims, NULL); if (dataspace < 0) throw DCException(getExceptionString("createReference: failed to create dataspace for reference")); dataset = H5Dcreate(refGroup, name.c_str(), H5T_STD_REF_OBJ, dataspace, H5P_DEFAULT, dsetProperties, H5P_DEFAULT); if (dataset < 0) throw DCException(getExceptionString("createReference: failed to create dataset for reference")); if (H5Dwrite(dataset, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, dsetWriteProperties, ®ionRef) < 0) throw DCException(getExceptionString("createReference: failed to write reference")); isReference = true; opened = true; }
PetscErrorCode PetscViewerHDF5OpenGroup(PetscViewer viewer, hid_t *fileId, hid_t *groupId) { hid_t file_id, group; const char *groupName = NULL; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscViewerHDF5GetFileId(viewer, &file_id);CHKERRQ(ierr); ierr = PetscViewerHDF5GetGroup(viewer, &groupName);CHKERRQ(ierr); /* Open group */ if (groupName) { PetscBool root; ierr = PetscStrcmp(groupName, "/", &root);CHKERRQ(ierr); if (!root && !H5Lexists(file_id, groupName, H5P_DEFAULT)) { #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800) group = H5Gcreate2(file_id, groupName, 0, H5P_DEFAULT, H5P_DEFAULT); #else /* deprecated HDF5 1.6 API */ group = H5Gcreate(file_id, groupName, 0); #endif if (group < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not create group %s", groupName); ierr = H5Gclose(group);CHKERRQ(ierr); } #if (H5_VERS_MAJOR * 10000 + H5_VERS_MINOR * 100 + H5_VERS_RELEASE >= 10800) group = H5Gopen2(file_id, groupName, H5P_DEFAULT); #else group = H5Gopen(file_id, groupName); #endif if (group < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_LIB, "Could not open group %s", groupName); } else group = file_id; *fileId = file_id; *groupId = group; PetscFunctionReturn(0); }
/** read global problem; * return problem on success; NULL on failure */ struct fclib_global* fclib_read_global (const char *path) { struct fclib_global *problem; hid_t file_id, main_id, id; if ((file_id = H5Fopen (path, H5F_ACC_RDONLY, H5P_DEFAULT)) < 0) { fprintf (stderr, "ERROR: opening file failed\n"); return NULL; } MM (problem = calloc (1, sizeof (struct fclib_global))); IO (main_id = H5Gopen (file_id, "/fclib_global", H5P_DEFAULT)); IO (H5LTread_dataset_int (file_id, "/fclib_global/spacedim", &problem->spacedim)); IO (id = H5Gopen (file_id, "/fclib_global/M", H5P_DEFAULT)); problem->M = read_matrix (id); IO (H5Gclose (id)); IO (id = H5Gopen (file_id, "/fclib_global/H", H5P_DEFAULT)); problem->H = read_matrix (id); IO (H5Gclose (id)); if (H5Lexists (file_id, "/fclib_global/G", H5P_DEFAULT)) { IO (id = H5Gopen (file_id, "/fclib_global/G", H5P_DEFAULT)); problem->G = read_matrix (id); IO (H5Gclose (id)); } IO (id = H5Gopen (file_id, "/fclib_global/vectors", H5P_DEFAULT)); read_global_vectors (id, problem); IO (H5Gclose (id)); if (H5Lexists (file_id, "/fclib_global/info", H5P_DEFAULT)) { IO (id = H5Gopen (file_id, "/fclib_global/info", H5P_DEFAULT)); problem->info = read_problem_info (id); IO (H5Gclose (id)); } IO (H5Gclose (main_id)); IO (H5Fclose (file_id)); return problem; }
void hdf5file::touch_group(const std::string& name) { //std::cout << "touch: " << name << std::endl; hid_t location_id = handle(); hid_t result = -1; if(! H5Lexists(location_id, name.c_str(), H5P_DEFAULT)) result = H5Gcreate(location_id, name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); else result = H5Gopen(location_id, name.c_str(), H5P_DEFAULT); H5Gclose(result); }
bool utils_hdf5_check_present(hid_t loc_id, const char *name) { htri_t bool_id; if ((bool_id = H5Lexists(loc_id, name, H5P_DEFAULT)) < 0 || !bool_id) return false; if ((bool_id = H5Oexists_by_name(loc_id, name, H5P_DEFAULT)) < 0 || !bool_id) return false; return true; }
hdf5_dataset::hdf5_dataset ( hdf5_file &file, std::string const& path, hdf5_datatype const& datatype, hdf5_dataspace const& dataspace ) : path_(path) { // Check if name exists in this file. htri_t status = H5Lexists(file.get_id(), path.c_str(), H5P_DEFAULT); if(status > 0) { // Full path exists. // Attempt to open it as a dataset set_id(H5Dopen2(file.get_id(), path.c_str(), H5P_DEFAULT)); if(get_id() < 0) { boost::serialization::throw_exception( hdf5_archive_exception( hdf5_archive_exception::hdf5_archive_dataset_access_error, path.c_str() ) ); } } else if(status == 0){ // Final link in path does not exist. // Create the dataset. set_id(H5Dcreate2( file.get_id(), path.c_str(), datatype.get_id(), dataspace.get_id(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ) ); if(get_id() < 0) { boost::serialization::throw_exception( hdf5_archive_exception( hdf5_archive_exception::hdf5_archive_dataset_create_error, path.c_str() ) ); } } else { // intermediate link does not exist, or other error boost::serialization::throw_exception( hdf5_archive_exception( hdf5_archive_exception::hdf5_archive_bad_path_error, path.c_str() ) ); } }
static int find_max_suffix(hid_t loc, char *basename){ int n; for(n = 1;;n++){ char buffer[1024]; sprintf(buffer,"%s_%d",basename,n); if(!H5Lexists(loc,buffer,H5P_DEFAULT)){ break; } } return n-1; }
/** make grourp */ static hid_t H5Gmake (hid_t loc_id, const char *name) { hid_t id; if (H5Lexists (loc_id, name, H5P_DEFAULT)) { id = H5Gopen (loc_id, name, H5P_DEFAULT); } else return H5Gcreate (loc_id, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); return id; }
void DCDataSet::create(const CollectionType& colType, hid_t group, const Dimensions size, uint32_t ndims, bool compression) throw (DCException) { log_msg(2, "DCDataSet::create (%s, size %s)", name.c_str(), size.toString().c_str()); if (opened) throw DCException(getExceptionString("create: dataset is already open")); // if the dataset already exists, remove/unlink it // note that this won't free the memory occupied by this // dataset, however, there currently is no function to delete // a dataset if (!checkExistence || (checkExistence && H5Lexists(group, name.c_str(), H5P_LINK_ACCESS_DEFAULT))) H5Ldelete(group, name.c_str(), H5P_LINK_ACCESS_DEFAULT); this->ndims = ndims; this->compression = compression; this->datatype = colType.getDataType(); getLogicalSize().set(size); setChunking(colType.getSize()); setCompression(); if (getPhysicalSize().getScalarSize() != 0) { hsize_t *max_dims = new hsize_t[ndims]; for (size_t i = 0; i < ndims; ++i) max_dims[i] = H5F_UNLIMITED; dataspace = H5Screate_simple(ndims, getPhysicalSize().getPointer(), max_dims); delete[] max_dims; max_dims = NULL; } else dataspace = H5Screate(H5S_NULL); if (dataspace < 0) throw DCException(getExceptionString("create: Failed to create dataspace")); // create the new dataset dataset = H5Dcreate(group, this->name.c_str(), this->datatype, dataspace, H5P_DEFAULT, dsetProperties, H5P_DEFAULT); if (dataset < 0) throw DCException(getExceptionString("create: Failed to create dataset")); isReference = false; opened = true; }
// Fields }}} // Informational {{{ bool Location::exists(optional<LinkAccessProperties const&> const& optional_link_access_properties) const { string const& name = *(this->name); if(name == "." || name == "/") return true; return assertSuccess( "ascertaining location existence", H5Lexists( getParentId(), name.c_str(), getOptionalPropertiesId(optional_link_access_properties) ) ) == 1; }
fast5_file fast5_open(const std::string& filename) { fast5_file fh; fh.hdf5_file = H5Fopen(filename.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); // Check for attribute that indicates whether it is single or multi-fast5 // see: https://community.nanoporetech.com/posts/multi-fast5-format const std::string indicator_p1 = "/UniqueGlobalKey/"; const std::string indicator_p2 = indicator_p1 + "tracking_id/"; bool has_indicator = H5Lexists(fh.hdf5_file, indicator_p1.c_str(), H5P_DEFAULT) && H5Lexists(fh.hdf5_file, indicator_p2.c_str(), H5P_DEFAULT); fh.is_multi_fast5 = !has_indicator; return fh; }
//------------------------------------------------------------------------------ xdm::RefPtr< DatasetIdentifier > createDatasetIdentifier( const DatasetParameters& parameters ) { // check if the dataset already exists within the given parent htri_t exists = H5Lexists( parameters.parent, parameters.name.c_str(), H5P_DEFAULT ); if ( exists ) { if ( parameters.mode == xdm::Dataset::kCreate ) { // the dataset exists and a create was requested, delete the existing one H5Ldelete( parameters.parent, parameters.name.c_str(), H5P_DEFAULT ); } else { // read or modify access, open and return the existing dataset return openExistingDataset( parameters ); } } // the dataset doesn't exist. Read only access is an error. if ( parameters.mode == xdm::Dataset::kRead ) { XDM_THROW( xdm::DatasetNotFound( parameters.name ) ); } // Determine the dataset access properties based off chunking and compression // parameters. xdm::RefPtr< PListIdentifier > createPList( new PListIdentifier( H5P_DEFAULT ) ); if ( parameters.chunked ) { createPList->reset( H5Pcreate( H5P_DATASET_CREATE ) ); setupChunks( createPList->get(), parameters.chunkSize, parameters.dataspace ); // Chunking is enabled, so check for compression and enable it if possible. if ( parameters.compress ) { setupCompression( createPList->get(), parameters.compressionLevel ); } } // the mode is create or modify. In both cases we want to create it if it // doesn't yet exist. It is safe to create the dataset here because we deleted // the dataset earlier if it existed. hid_t datasetId = H5Dcreate( parameters.parent, parameters.name.c_str(), parameters.type, parameters.dataspace, H5P_DEFAULT, createPList->get(), H5P_DEFAULT ); return xdm::RefPtr< DatasetIdentifier >( new DatasetIdentifier( datasetId ) ); }