int main (void) { hid_t file; /* file handle */ hid_t grp_a, grp_b, grp_c; /* group handlers */ /* * Create a new file using H5F_ACC_TRUNC access, * default file creation properties, and default file * access properties. */ file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create groups in the file. */ grp_a = H5Gcreate2(file, "/a", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); grp_b = H5Gcreate2(grp_a, "b", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); grp_c = H5Gcreate2(grp_b, "c", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(grp_a); H5Gclose(grp_b); H5Gclose(grp_c); H5Fclose(file); return 0; }
int main(){ hid_t fprop; hid_t fid; hid_t vol_id = H5VL_memvol_init(); herr_t status; hid_t g1, g2; hid_t plist; char name[1024]; fprop = H5Pcreate(H5P_FILE_ACCESS); H5Pset_vol(fprop, vol_id, &fprop); fid = H5Fcreate("test", H5F_ACC_TRUNC, H5P_DEFAULT, fprop); H5VLget_plugin_name(fid, name, 1024); printf ("Using VOL %s\n", name); g1 = H5Gcreate2(fid, "g1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(g1); g2 = H5Gcreate2(fid, "g2", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); g1 = H5Gcreate2(g2, "g1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(g1); H5Gclose(g2); // is this allowed? //g3 = H5Gcreate2(fid, "g1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); //H5Gclose(g3); printf("Testing additional functions\n"); g1 = H5Gopen2(fid, "g1", H5P_DEFAULT ); plist = H5Gget_create_plist(g1); H5G_info_t group_info; H5Gget_info(g1, & group_info ); H5Gget_info_by_idx(fid, "g1", H5_INDEX_CRT_ORDER, H5_ITER_NATIVE, 0, & group_info, H5P_DEFAULT ) ; H5Gget_info_by_idx(fid, "g1", H5_INDEX_NAME, H5_ITER_NATIVE, 0, & group_info, H5P_DEFAULT ) ; H5Gget_info_by_name(fid, "g1", & group_info, H5P_DEFAULT); H5Pclose(plist); status = H5Gclose(g1); g1 = H5Gopen2(fid, "g2", H5P_DEFAULT ); H5Gclose(g1); //g1 = H5Gopen2(fid, "INVALID", H5P_DEFAULT ); //H5Gclose(g1); g1 = H5Gcreate_anon( fid, H5P_DEFAULT, H5P_DEFAULT ); H5Gclose(g1); H5Fclose(fid); H5VL_memvol_finalize(); printf("Status: %d\n", status); return 0; }
/*------------------------------------------------------------------------- * Function: test_creating_groups_using_plugins * * Purpose: Tests creating group with dynamically loaded filters * * Return: SUCCEED/FAIL * *------------------------------------------------------------------------- */ static herr_t test_creating_groups_using_plugins(hid_t fid) { hid_t gcpl_id = -1; hid_t gid = -1; hid_t sub_gid = -1; int i; char subgroup_name[256]; TESTING("creating groups with filter plugin 4"); if ((gcpl_id = H5Pcreate(H5P_GROUP_CREATE)) < 0) TEST_ERROR; /* Use a filter plugin for creating groups */ if (H5Pset_filter(gcpl_id, FILTER4_ID, H5Z_FLAG_MANDATORY, (size_t)0, NULL) < 0) TEST_ERROR; /* Create a group using this filter */ if ((gid = H5Gcreate2(fid, TOP_LEVEL_GROUP_NAME, H5P_DEFAULT, gcpl_id, H5P_DEFAULT)) < 0) TEST_ERROR; /* Create multiple groups under the top-level group */ for (i = 0; i < N_SUBGROUPS; i++) { char *sp = subgroup_name; sp += HDsprintf(subgroup_name, SUBGROUP_PREFIX); HDsprintf(sp, "%d", i); if ((sub_gid = H5Gcreate2(gid, subgroup_name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; if (H5Gclose(sub_gid) < 0) TEST_ERROR; } /* Close everything */ if (H5Gclose(gid) < 0) TEST_ERROR; if (H5Pclose(gcpl_id) < 0) TEST_ERROR; PASSED(); return SUCCEED; error: /* Clean up objects used for this test */ H5E_BEGIN_TRY { H5Gclose(sub_gid); H5Gclose(gid); H5Pclose(gcpl_id); } H5E_END_TRY return FAIL; } /* end test_creating_groups_using_plugins() */
int main (void) { hid_t fid1=-1; hid_t fid2=-1; hid_t gid=-1; char filename1[NAME_BUF_SIZE]; char filename2[NAME_BUF_SIZE]; /* Name the files differently depending on the endianness of this platform */ switch(H5Tget_order(H5T_NATIVE_INT)) { case H5T_ORDER_LE: strcpy(filename1, NAME_LE_1); strcpy(filename2, NAME_LE_2); break; case H5T_ORDER_BE: strcpy(filename1, NAME_BE_1); strcpy(filename2, NAME_BE_2); break; default: goto error; } /* Create the two files */ if((fid1 = H5Fcreate(filename1, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if((fid2 = H5Fcreate(filename2, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; /* Create two groups in the second file */ if((gid = H5Gcreate2(fid2, "group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if((H5Gclose(gid)) < 0) goto error; if((gid = H5Gcreate2(fid2, "group/subgroup", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if((H5Gclose(gid)) < 0) goto error; /* Create an external link in the first file pointing to the group in the second file */ if(H5Lcreate_external(filename2, "group", fid1, "ext_link", H5P_DEFAULT, H5P_DEFAULT) < 0) goto error; if((H5Fclose(fid1)) < 0) goto error; if((H5Fclose(fid2)) < 0) goto error; return 0; error: H5E_BEGIN_TRY { H5Fclose(fid1); H5Fclose(fid2); H5Gclose(gid); } H5E_END_TRY return 1; }
/** * Create an HDF5 file. */ static hid_t _hdf_create(const char *path, int cmode) { hid_t grp_id; hid_t fd; hid_t tmp_id; hid_t hdf_gpid; hid_t fpid; fpid = H5Pcreate (H5P_FILE_ACCESS); /*VF use all the features of new HDF5 1.8*/ H5Pset_libver_bounds (fpid, H5F_LIBVER_18, H5F_LIBVER_18); H5E_BEGIN_TRY { fd = H5Fcreate(path, cmode, H5P_DEFAULT, fpid); } H5E_END_TRY; if (fd < 0) { /*TODO: report error properly*/ return MI_LOG_ERROR(MI2_MSG_CREATEFILE,path); } /* Create the default groups. * Should we use a non-zero value for size_hint (parameter 3)??? */ hdf_gpid = H5Pcreate (H5P_GROUP_CREATE); H5Pset_attr_phase_change (hdf_gpid, 0, 0); MI_CHECK_HDF_CALL_RET(grp_id = H5Gcreate2(fd, MI_ROOT_PATH , H5P_DEFAULT, hdf_gpid, H5P_DEFAULT),"H5Gcreate2") MI_CHECK_HDF_CALL_RET(tmp_id = H5Gcreate2(grp_id, "dimensions", H5P_DEFAULT, hdf_gpid, H5P_DEFAULT),"H5Gcreate2") H5Gclose(tmp_id); MI_CHECK_HDF_CALL_RET(tmp_id = H5Gcreate2(grp_id, "info", H5P_DEFAULT, hdf_gpid, H5P_DEFAULT),"H5Gcreate2") H5Gclose(tmp_id); MI_CHECK_HDF_CALL_RET(tmp_id = H5Gcreate2(grp_id, "image", H5P_DEFAULT, hdf_gpid, H5P_DEFAULT),"H5Gcreate2") H5Gclose(tmp_id); MI_CHECK_HDF_CALL_RET(tmp_id = H5Gcreate2(grp_id, "image/0", H5P_DEFAULT, hdf_gpid, H5P_DEFAULT),"H5Gcreate2") H5Pclose ( hdf_gpid ); H5Gclose(tmp_id); H5Gclose(grp_id); return fd; }
/*------------------------------------------------------------------------- * Function: create_file * * Purpose: Creates file used in part 1 of the test * * Return: Success: 0 * * Failure: 1 * * Programmer: Leon Arber * Sept. 26, 2006 * * Modifications: * *------------------------------------------------------------------------- */ static hid_t create_file(char* name, hid_t fapl) { hid_t file, dcpl, space, dset, groups, grp, plist; hsize_t ds_size[2] = {100, 100}; hsize_t ch_size[2] = {5, 5}; hsize_t i, j; if((file=H5Fcreate(name, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) goto error; /* Create a chunked dataset */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error; if(H5Pset_chunk(dcpl, 2, ch_size) < 0) goto error; if((space = H5Screate_simple(2, ds_size, NULL)) < 0) goto error; if((dset = H5Dcreate2(file, "dset", H5T_NATIVE_FLOAT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; plist = H5Pcreate(H5P_DATASET_XFER); H5Pset_dxpl_mpio(plist, H5FD_MPIO_COLLECTIVE); /* Write some data */ for(i = 0; i < ds_size[0]; i++) { /* * The extra cast in the following statement is a bug workaround * for the Win32 version 5.0 compiler. * 1998-11-06 ptl */ for(j = 0; j < ds_size[1]; j++) the_data[i][j] = (double)(hssize_t)i/(hssize_t)(j+1); } if(H5Dwrite(dset, H5T_NATIVE_DOUBLE, space, space, plist, the_data) < 0) goto error; /* Create some groups */ if((groups = H5Gcreate2(file, "some_groups", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; for(i = 0; i < 100; i++) { sprintf(name, "grp%02u", (unsigned)i); if((grp = H5Gcreate2(groups, name, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Gclose(grp) < 0) goto error; } return file; error: HD_exit(1); }
GWriteHDFFile::GWriteHDFFile(std::string filename, std::valarray<uint32_t> npart_in, std::vector<block_info>* BlockNames, bool format_2, bool debug) : GBaseWriteFile(filename, npart_in), debug(debug) { //Create file hid_t hdf_file = H5Fcreate(filename.c_str(),H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT); if(hdf_file < 0){ throw std::ios_base::failure(std::string("Unable to create file: ")+filename); } //Create groups in the file for(int i = 0; i < N_TYPE; i++){ if(npart_in[i] == 0) continue; snprintf(g_name[i], 20,"PartType%d",i); hid_t group = H5Gcreate2(hdf_file,g_name[i],H5P_DEFAULT, H5P_DEFAULT,H5P_DEFAULT); if (group < 0) throw std::ios_base::failure(std::string("Unable to create group: ")+std::string(g_name[i])); } //Create metadata about datablocks: we only actually use partlen. std::vector<block_info>::iterator it; for(it=(*BlockNames).begin(); it<(*BlockNames).end(); ++it){ //Detect an integer type if ((*it).partlen == sizeof(int64_t)) m_ints.insert((*it).name); } return; }
void SGDSolver<Dtype>::SnapshotSolverStateToHDF5( const string& model_filename) { #ifdef USE_HDF5 string snapshot_filename = Solver<Dtype>::SnapshotFilename(".solverstate.h5"); LOG(INFO) << "Snapshotting solver state to HDF5 file " << snapshot_filename; hid_t file_hid = H5Fcreate(snapshot_filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); CHECK_GE(file_hid, 0) << "Couldn't open " << snapshot_filename << " to save solver state."; hdf5_save_int(file_hid, "iter", this->iter_); hdf5_save_string(file_hid, "learned_net", model_filename); hdf5_save_int(file_hid, "current_step", this->current_step_); hid_t history_hid = H5Gcreate2(file_hid, "history", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK_GE(history_hid, 0) << "Error saving solver state to " << snapshot_filename << "."; for (int i = 0; i < history_.size(); ++i) { ostringstream oss; oss << i; hdf5_save_nd_dataset<Dtype>(history_hid, oss.str(), *history_[i]); } H5Gclose(history_hid); H5Fclose(file_hid); #else LOG(FATAL) << "SnapshotSolverStateToHDF5 requires hdf5;" << " compile with USE_HDF5."; #endif // USE_HDF5 }
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); }
//-------------------------------------------------------------------------- // Function: CommonFG::createGroup ///\brief Creates a new group at this location which can be a file /// or another group. ///\param name - IN: Name of the group to create ///\param size_hint - IN: Indicates the number of bytes to reserve for /// the names that will appear in the group ///\return Group instance ///\exception H5::FileIException or H5::GroupIException ///\par Description /// The optional \a size_hint specifies how much file space to /// reserve for storing the names that will appear in this new /// group. If a non-positive value is provided for the \a size_hint /// then a default size is chosen. // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- Group CommonFG::createGroup( const char* name, size_t size_hint ) const { // Group creation property list for size_hint hid_t gcpl_id = 0; // Set the local heap size hint if(!(size_hint == (size_t)-1 || size_hint == 0)) { // If the creation of the property list failed, throw an exception if((gcpl_id = H5Pcreate(H5P_GROUP_CREATE)) < 0) throwException("createGroup", "H5Pcreate failed"); if( H5Pset_local_heap_size_hint(gcpl_id, size_hint) < 0) { H5Pclose(gcpl_id); throwException("createGroup", "H5Pset_local_heap_size failed"); } } // Call C routine H5Gcreate2 to create the named group, giving the // location id which can be a file id or a group id hid_t group_id = H5Gcreate2( getLocId(), name, H5P_DEFAULT, gcpl_id, H5P_DEFAULT ); // Close the group creation property list, if necessary if(gcpl_id > 0) H5Pclose(gcpl_id); // If the creation of the group failed, throw an exception if( group_id < 0 ) throwException("createGroup", "H5Gcreate2 failed"); // No failure, create and return the Group object Group group( group_id ); return( group ); }
static void soft_link_example(void) { hid_t file_id; hid_t group_id; /* Define the link class that we'll use to register "user-defined soft * links" using the callbacks we defined above. * A link class can have NULL for any callback except its traverse * callback. */ const H5L_class_t UD_soft_class[1] = {{ H5L_LINK_CLASS_T_VERS, /* Version number for this struct. * This field is always H5L_LINK_CLASS_T_VERS */ (H5L_type_t)UD_SOFT_CLASS, /* Link class id number. This can be any * value between H5L_TYPE_UD_MIN (64) and * H5L_TYPE_MAX (255). It should be a * value that isn't already being used by * another kind of link. We'll use 65. */ "UD_soft_link", /* Link class name for debugging */ NULL, /* Creation callback */ NULL, /* Move callback */ NULL, /* Copy callback */ UD_soft_traverse, /* The actual traversal function */ NULL, /* Deletion callback */ NULL /* Query callback */ }}; /* First, create a file and an object within the file for the link to * point to. */ file_id = H5Fcreate(SOFT_LINK_FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); group_id = H5Gcreate2(file_id, TARGET_GROUP, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(group_id); /* This is how we create a normal soft link to the group. */ H5Lcreate_soft(TARGET_GROUP, file_id, SOFT_LINK_NAME, H5P_DEFAULT, H5P_DEFAULT); /* To do the same thing using a user-defined link, we first have to * register the link class we defined. */ H5Lregister(UD_soft_class); /* Now create a user-defined link. We give it the path to the group * as its udata.1 */ H5Lcreate_ud(file_id, UD_SOFT_LINK_NAME, (H5L_type_t)UD_SOFT_CLASS, TARGET_GROUP, strlen(TARGET_GROUP) + 1, H5P_DEFAULT, H5P_DEFAULT); /* We can access the group through the UD soft link like we would through * a normal soft link. This link will still dangle if the object's * original name is changed or unlinked. */ group_id = H5Gopen2(file_id, UD_SOFT_LINK_NAME, H5P_DEFAULT); /* The group is now open normally. Don't forget to close it! */ H5Gclose(group_id); H5Fclose(file_id); }
/* Basic external link example * * Creates two files and uses an external link to access an object in the * second file from the first file. */ static void extlink_example(void) { hid_t source_file_id, targ_file_id; hid_t group_id, group2_id; /* Create two files, a source and a target */ source_file_id = H5Fcreate(SOURCE_FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); targ_file_id = H5Fcreate(TARGET_FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create a group in the target file for the external link to point to. */ group_id = H5Gcreate2(targ_file_id, "target_group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* Close the group and the target file */ H5Gclose(group_id); /* Create an external link in the source file pointing to the target group. * We could instead have created the external link first, then created the * group it points to; the order doesn't matter. */ H5Lcreate_external(TARGET_FILE, "target_group", source_file_id, "ext_link", H5P_DEFAULT, H5P_DEFAULT); /* Now we can use the external link to create a new group inside the * target group (even though the target file is closed!). The external * link works just like a soft link. */ group_id = H5Gcreate2(source_file_id, "ext_link/new_group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* The group is inside the target file and we can access it normally. * Here, group_id and group2_id point to the same group inside the * target file. */ group2_id = H5Gopen2(targ_file_id, "target_group/new_group", H5P_DEFAULT); /* Don't forget to close the IDs we opened. */ H5Gclose(group2_id); H5Gclose(group_id); H5Fclose(targ_file_id); H5Fclose(source_file_id); /* The link from the source file to the target file will work as long as * the target file can be found. If the target file is moved, renamed, * or deleted in the filesystem, HDF5 won't be able to find it and the * external link will "dangle." */ }
/*------------------------------------------------------------------------- * Function: main * * Purpose: Runs external dataset tests. * * Return: Success: exit(0) * * Failure: exit(non-zero) * * Programmer: Robb Matzke * Tuesday, March 3, 1998 * * Modifications: * *------------------------------------------------------------------------- */ int main (void) { hid_t fapl=-1; /*file access properties */ hid_t file=-1; /*file for test_1* functions */ char filename[1024]; /*file name for test_1* funcs */ hid_t grp=-1; /*group to emit diagnostics */ int nerrors=0; /*number of errors */ h5_reset(); fapl = h5_fileaccess(); h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) FAIL_STACK_ERROR if((grp = H5Gcreate2(file, "emit-diagnostics", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) FAIL_STACK_ERROR if(H5Gclose(grp) < 0) goto error; nerrors += test_1a(file); nerrors += test_1b(file); nerrors += test_1c(file); nerrors += test_1d(file); nerrors += test_1e(file); nerrors += test_1f(file); nerrors += test_1g(); nerrors += test_1h(); nerrors += test_2(fapl); nerrors += test_3(fapl); nerrors += test_4(fapl); /* Verify symbol table messages are cached */ nerrors += (h5_verify_cached_stabs(FILENAME, fapl) < 0 ? 1 : 0); if (nerrors>0) goto error; if (H5Fclose(file) < 0) goto error; puts("All external storage tests passed."); if (h5_cleanup(FILENAME, fapl)) { remove("extern_1a.raw"); remove("extern_1b.raw"); remove("extern_2a.raw"); remove("extern_2b.raw"); remove("extern_3a.raw"); remove("extern_3b.raw"); remove("extern_4a.raw"); remove("extern_4b.raw"); } return 0; error: H5E_BEGIN_TRY { H5Fclose(file); H5Pclose(fapl); } H5E_END_TRY; nerrors = MAX(1, nerrors); printf ("%d TEST%s FAILED.\n", nerrors, 1==nerrors?"":"s"); return 1; }
/** 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; }
/*------------------------------------------------------------------------- * Function: gent_empty_group * * Purpose: Generate an empty group in a location * *------------------------------------------------------------------------- */ static void gent_empty_group(hid_t loc_id) { hid_t gid; /* Create group in location */ gid = H5Gcreate2(loc_id, GROUP_EMPTY, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* Release resources */ H5Gclose(gid); }
group(h5a::node const & parent, std::string const & path, bool create) { if (!create) { open_group(parent, path); return; } h5p::proplist gcpl(H5P_GROUP_CREATE); h5e::check_error(H5Pset_link_creation_order(gcpl.hid(), H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)); _self = h5e::check_error(H5Gcreate2(parent.hid(), path.c_str(), H5P_DEFAULT, gcpl.hid(), H5P_DEFAULT)); }
/*------------------------------------------------------------------------- * Function: gent_nested_group * * Purpose: Generate a group in a location and populate it with another group * containing the "standard" datasets * *------------------------------------------------------------------------- */ static void gent_nested_group(hid_t loc_id) { hid_t gid; /* Create group in location */ gid = H5Gcreate2(loc_id, GROUP_NESTED, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* Add datasets to group created */ gent_nested_datasets(gid); /* Release resources */ H5Gclose(gid); }
int main() { char* filename = "file-test.h5"; hid_t fprop; hid_t vol_id = H5VLregister_by_name("h5-esdm"); hid_t file_id, group_id, dataset_id, dataspace_id, attribute_id; herr_t status; char name[1024]; // SET VOL PLUGIN ///////////////////////////////////////////////////////// fprop = H5Pcreate(H5P_FILE_ACCESS); H5Pset_vol(fprop, vol_id, &fprop); // TODO // MOCK SETUP ///////////////////////////////////////////////////////////// /* Create a new file using default properties. */ file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fprop); /* Create a group named "/MyGroup" in the file. */ group_id = H5Gcreate2(file_id, "/MyGroup", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* Close the group. */ status = H5Gclose(group_id); /* Terminate access to the file. */ status = H5Fclose(file_id); // MOCK CLEANUP /////////////////////////////////////////////////////////// // CREATE ///////////////////////////////////////////////////////////////// // OPEN /////////////////////////////////////////////////////////////////// // CLOSE ////////////////////////////////////////////////////////////////// // READ /////////////////////////////////////////////////////////////////// // WRITE ////////////////////////////////////////////////////////////////// // GET //////////////////////////////////////////////////////////////////// // SPECIFIC /////////////////////////////////////////////////////////////// // OPTIONAL /////////////////////////////////////////////////////////////// // Clean up /////////////////////////////////////////////////////////////// H5VLunregister(vol_id); return 0; }
int GWriteHDFFile::WriteHeader(gadget_header& header) { herr_t herr; for(int i=0; i<N_TYPE; i++){ header.npart[i]=npart[i]; } hid_t handle = H5Fopen(filename.c_str(), H5F_ACC_RDWR, H5P_DEFAULT); if (handle < 0) return -1*handle; // Lite interface for making simple attributes // H5LTset_attribute_int (file_id, dset_name, attr_name, data, size); // herr_t H5LTset_attribute_int( hid_t loc_id, const char *obj_name, const char *attr_name, int *buffer, size_t size) hid_t hdgrp = H5Gcreate2(handle, "Header", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); herr = H5LTset_attribute_uint(handle, "Header", "NumPart_ThisFile", header.npart, N_TYPE); if (herr < 0) { WARN("Could not write to header"); return -1*herr; } herr = H5LTset_attribute_uint(handle, "Header", "NumPart_Total", header.npartTotal, N_TYPE); herr = H5LTset_attribute_uint(handle, "Header", "NumPart_Total_HighWord", header.NallHW, N_TYPE); herr = H5LTset_attribute_double(handle, "Header", "MassTable", header.mass, N_TYPE); if (herr < 0) { WARN("Could not write particle numbers"); return -1*herr; } herr = H5LTset_attribute_double(handle, "Header", "Time", &header.time, 1); herr = H5LTset_attribute_double(handle, "Header", "Redshift", &header.redshift, 1); herr = H5LTset_attribute_double(handle, "Header", "BoxSize", &header.BoxSize, 1); herr = H5LTset_attribute_int(handle, "Header", "NumFilesPerSnapshot", &header.num_files, 1); herr = H5LTset_attribute_double(handle, "Header", "Omega0", &header.Omega0, 1); herr = H5LTset_attribute_double(handle, "Header", "OmegaLambda", &header.OmegaLambda, 1); herr = H5LTset_attribute_double(handle, "Header", "HubbleParam", &header.HubbleParam, 1); herr = H5LTset_attribute_int(handle, "Header", "Flag_Sfr", &header.flag_sfr, 1); herr = H5LTset_attribute_int(handle, "Header", "Flag_Cooling", &header.flag_cooling, 1); herr = H5LTset_attribute_int(handle, "Header", "Flag_StellarAge", &header.flag_stellarage, 1); herr = H5LTset_attribute_int(handle, "Header", "Flag_Metals", &header.flag_metals, 1); herr = H5LTset_attribute_int(handle, "Header", "Flag_Feedback", &header.flag_feedback, 1); herr = H5LTset_attribute_int(handle, "Header", "Flag_DoublePrecision", &header.flag_doubleprecision, 1); herr = H5LTset_attribute_int(handle, "Header", "Flag_IC_Info", &header.flag_ic_info, 1); herr = H5LTset_attribute_double(handle, "Header", "UnitLength_in_cm", &header.UnitLength_in_cm, 1); herr = H5LTset_attribute_double(handle, "Header", "UnitMass_in_g", &header.UnitMass_in_g, 1); herr = H5LTset_attribute_double(handle, "Header", "UnitVelocity_in_cm_per_s", &header.UnitVelocity_in_cm_per_s, 1); if (herr < 0) { WARN("Could not write final header flags"); return -1*herr; } H5Gclose(hdgrp); H5Fclose(handle); return 0; }
/*------------------------------------------------------------------------- * Function: test_misc * * Purpose: Test miscellaneous group stuff. * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Tuesday, November 24, 1998 * *------------------------------------------------------------------------- */ static int test_misc(hid_t fapl, hbool_t new_format) { hid_t fid = (-1); /* File ID */ hid_t g1 = (-1), g2 = (-1), g3 = (-1); char filename[NAME_BUF_SIZE]; char comment[64]; if(new_format) TESTING("miscellaneous group tests (w/new group format)") else TESTING("miscellaneous group tests") /* Create file */ h5_fixname(FILENAME[0], fapl, filename, sizeof(filename)); if((fid = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR /* Create initial groups for testing, then close */ if((g1 = H5Gcreate2(fid, "test_1a", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR if((g2 = H5Gcreate2(g1, "sub_1", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR if((g3 = H5Gcreate2(fid, "test_1b", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR if(H5Oset_comment(g3, "hello world") < 0) TEST_ERROR if(H5Gclose(g1) < 0) TEST_ERROR if(H5Gclose(g2) < 0) TEST_ERROR if(H5Gclose(g3) < 0) TEST_ERROR /* Open all groups with absolute names to check for exsistence */ if((g1 = H5Gopen2(fid, "/test_1a", H5P_DEFAULT)) < 0) TEST_ERROR if((g2 = H5Gopen2(fid, "/test_1a/sub_1", H5P_DEFAULT)) < 0) TEST_ERROR if((g3 = H5Gopen2(fid, "/test_1b", H5P_DEFAULT)) < 0) TEST_ERROR if(H5Oget_comment_by_name(g3, "././.", comment, sizeof comment, H5P_DEFAULT) < 0) TEST_ERROR if(HDstrcmp(comment, "hello world")) { H5_FAILED(); puts(" Read the wrong comment string from the group."); printf(" got: \"%s\"\n ans: \"hello world\"\n", comment); TEST_ERROR }
/*------------------------------------------------------------------------- * Function: test_filters_for_groups * * Purpose: Tests creating group with dynamically loaded filters * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * 1 April 2013 * *------------------------------------------------------------------------- */ static herr_t test_filters_for_groups(hid_t file) { hid_t gcpl, gid, group; int i; char gname[256]; TESTING("Testing DYNLIB3 filter for group"); if((gcpl = H5Pcreate(H5P_GROUP_CREATE)) < 0) goto error; /* Use DYNLIB3 for creating groups */ if(H5Pset_filter (gcpl, H5Z_FILTER_DYNLIB3, H5Z_FLAG_MANDATORY, (size_t)0, NULL) < 0) goto error; /* Create a group using this filter */ if((gid = H5Gcreate2(file, "group1", H5P_DEFAULT, gcpl, H5P_DEFAULT)) < 0) goto error; /* Create multiple groups under "group1" */ for (i=0; i < GROUP_ITERATION; i++) { sprintf(gname, "group_%d", i); if((group = H5Gcreate2(gid, gname, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Gclose(group) < 0) goto error; } /* Close the group */ if(H5Gclose(gid) < 0) goto error; /* Clean up objects used for this test */ if(H5Pclose (gcpl) < 0) goto error; PASSED(); return 0; error: return -1; }
/* * test_attrname * Test that attributes can deal with UTF-8 strings */ void test_attrname(hid_t fid, const char * string) { hid_t group_id, attr_id; hid_t dtype_id, space_id; hsize_t dims=1; char read_buf[MAX_STRING_LENGTH]; herr_t ret; /* Create a new group and give it an attribute whose * name and value are UTF-8 strings. */ group_id = H5Gcreate2(fid, GROUP4_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(group_id, FAIL, "H5Gcreate2"); space_id = H5Screate_simple(RANK, &dims, NULL); CHECK(space_id, FAIL, "H5Screate_simple"); dtype_id = H5Tcopy(H5T_C_S1); CHECK(dtype_id, FAIL, "H5Tcopy"); ret = H5Tset_size(dtype_id, (size_t)MAX_STRING_LENGTH); CHECK(ret, FAIL, "H5Tset_size"); /* Create the attribute and check that its name is correct */ attr_id = H5Acreate2(group_id, string, dtype_id, space_id, H5P_DEFAULT, H5P_DEFAULT); CHECK(attr_id, FAIL, "H5Acreate2"); ret = H5Aget_name(attr_id, (size_t)MAX_STRING_LENGTH, read_buf); CHECK(ret, FAIL, "H5Aget_name"); ret = strcmp(read_buf, string); VERIFY(ret, 0, "strcmp"); read_buf[0] = '\0'; /* Try writing and reading from the attribute */ ret = H5Awrite(attr_id, dtype_id, string); CHECK(ret, FAIL, "H5Awrite"); ret = H5Aread(attr_id, dtype_id, read_buf); CHECK(ret, FAIL, "H5Aread"); ret = strcmp(read_buf, string); VERIFY(ret, 0, "strcmp"); /* Clean up */ ret = H5Aclose(attr_id); CHECK(ret, FAIL, "H5Aclose"); ret = H5Tclose(dtype_id); CHECK(ret, FAIL, "H5Tclose"); ret = H5Sclose(space_id); CHECK(ret, FAIL, "H5Sclose"); ret = H5Gclose(group_id); CHECK(ret, FAIL, "H5Gclose"); }
/* * Class: hdf_hdf5lib_H5 * Method: _H5Gcreate2 * Signature: (JLjava/lang/String;JJJ)J */ JNIEXPORT jlong JNICALL Java_hdf_hdf5lib_H5__1H5Gcreate2(JNIEnv *env, jclass clss, jlong loc_id, jstring name, jlong link_plist_id, jlong create_plist_id, jlong access_plist_id) { hid_t group_id = -1; const char *gName; PIN_JAVA_STRING(name, gName, -1); group_id = H5Gcreate2((hid_t)loc_id, gName, (hid_t)link_plist_id, (hid_t)create_plist_id, (hid_t)access_plist_id ); UNPIN_JAVA_STRING(name, gName); if (group_id < 0) h5libraryError(env); return (jlong)group_id; } /* end Java_hdf_hdf5lib_H5__1H5Gcreate2 */
void NSDFWriter::writeModelTree() { vector< string > tokens; ObjId mRoot(modelRoot_); string rootPath = MODELTREEPATH + string("/") + mRoot.element()->getName(); hid_t rootGroup = require_group(filehandle_, rootPath); hid_t tmp; htri_t exists; herr_t status; deque<Id> nodeQueue; deque<hid_t> h5nodeQueue; nodeQueue.push_back(mRoot); h5nodeQueue.push_back(rootGroup); // TODO: need to clarify what happens with array elements. We can // have one node per vec and set a count field for the number of // elements while (nodeQueue.size() > 0){ ObjId node = nodeQueue.front(); nodeQueue.pop_front(); hid_t prev = h5nodeQueue.front();; h5nodeQueue.pop_front(); vector < Id > children; Neutral::children(node.eref(), children); for ( unsigned int ii = 0; ii < children.size(); ++ii){ string name = children[ii].element()->getName(); // skip the system elements if (children[ii].path() == "/Msgs" || children[ii].path() == "/clock" || children[ii].path() == "/classes" || children[ii].path() == "/postmaster"){ continue; } exists = H5Lexists(prev, name.c_str(), H5P_DEFAULT); if (exists > 0){ tmp = H5Gopen2(prev, name.c_str(), H5P_DEFAULT); } else { tmp = H5Gcreate2(prev, name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } writeScalarAttr< string >(tmp, "uid", children[ii].path()); nodeQueue.push_back(children[ii]); h5nodeQueue.push_back(tmp); } status = H5Gclose(prev); } }
/*------------------------------------------------------------------------- * Function: gen_extlink_trg * * Purpose: generate target external link objs * * Programmer: Jonathan Kim (March 03, 2010) *------------------------------------------------------------------------*/ static herr_t gen_extlink_trg(hid_t loc_id) { hid_t gid=0, tid=0; int status; herr_t ret = SUCCEED; /*----------------------------------------------------------------------- * Groups *------------------------------------------------------------------------*/ /*-------------- * target file */ gid = H5Gcreate2(loc_id, "group", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (gid < 0) { fprintf(stderr, "Error: %s %d> H5Gcreate2 failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } /*-------------- * add dataset */ gent_simple(loc_id); /*-------------------- * add named datatype */ tid = H5Tcopy(H5T_NATIVE_INT); status = H5Tcommit2(loc_id, "datatype", tid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (status < 0) { fprintf(stderr, "Error: %s %d> H5Tcommit2 failed.\n", FUNC, __LINE__); ret = FAIL; goto out; } out: if(gid > 0) H5Gclose(gid); if(tid > 0) H5Tclose(tid); return ret; }
Group::Group( CreateAt<Location const> location , optional<LinkCreationProperties const&> const& optional_link_creation_properties , optional<GroupCreationProperties const&> const& optional_group_creation_properties ) : Object( location->getFile(), assertSuccess( "opening group", H5Gcreate2( location->getParentId(), location->getNameAsCStr(), getOptionalPropertiesId(optional_link_creation_properties), getOptionalPropertiesId(optional_group_creation_properties), H5P_DEFAULT ) ), H5Gclose ) {}
/*------------------------------------------------------------------------- * Function: h5_reset * * Purpose: Reset the library by closing it. * * Return: void * * Programmer: Robb Matzke * Friday, November 20, 1998 * *------------------------------------------------------------------------- */ void h5_reset(void) { HDfflush(stdout); HDfflush(stderr); H5close(); /* Save current error stack reporting routine and redirect to our local one */ HDassert(err_func == NULL); H5Eget_auto2(H5E_DEFAULT, &err_func, NULL); H5Eset_auto2(H5E_DEFAULT, h5_errors, NULL); /* * I commented this chunk of code out because it's not clear what diagnostics * were being output and under what circumstances, and creating this file * is throwing off debugging some of the tests. I can't see any _direct_ * harm in keeping this section of code, but I can't see any _direct_ * benefit right now either. If we figure out under which circumstances * diagnostics are being output, we should enable this behavior based on * appropriate configure flags/macros. QAK - 2007/12/20 */ #ifdef OLD_WAY { char filename[1024]; /* * Cause the library to emit some diagnostics early so they don't * interfere with other formatted output. */ sprintf(filename, "/tmp/h5emit-%05d.h5", HDgetpid()); H5E_BEGIN_TRY { hid_t file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); hid_t grp = H5Gcreate2(file, "emit", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Gclose(grp); H5Fclose(file); HDunlink(filename); } H5E_END_TRY; } #endif /* OLD_WAY */ }
//-***************************************************************************** OwData::OwData( hid_t iParentGroup, const std::string &iName, const AbcA::MetaData &iMetaData ) : m_group( -1 ) { // Check validity of all inputs. ABCA_ASSERT( iParentGroup >= 0, "Invalid parent group" ); // Create the HDF5 group corresponding to this object. hid_t copl = CreationOrderPlist(); m_group = H5Gcreate2( iParentGroup, iName.c_str(), H5P_DEFAULT, copl, H5P_DEFAULT ); H5Pclose( copl ); ABCA_ASSERT( m_group >= 0, "Could not create group for object: " << iName ); m_data.reset( new CpwData( ".prop", m_group ) ); AbcA::PropertyHeader topHeader( ".prop", iMetaData ); WritePropertyInfo( m_group, topHeader, false, 0, 0, 0, 0 ); }
static int make_hdf_group( const char* path, hid_t file, size_t sz, mhdf_Status* status ) { #if defined(H5Gcreate_vers) && H5Gcreate_vers > 1 hid_t handle = H5Gcreate2( file, path, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ); /* empty statement to avoid compiler warning */ if (sz) {} #else hid_t handle = H5Gcreate( file, path, sz ); #endif if (handle < 0) { mhdf_setFail( status, "Failed to create \"%s\" group.", path ); return 0; } else { H5Gclose( handle ); return 1; } }
hid_t H5CreateOrOpenGroup(hid_t &file_id, string &group_name) { hid_t group_id; if(group_name == "/") { group_id = H5Gopen2(file_id, group_name.c_str(), H5P_DEFAULT); } else { // first make sure the base group exists string delim = "/"; int pos = group_name.rfind(delim); if((pos != (int) std::string::npos) && (pos != 0)) { string subgroup = group_name.substr(0,pos); group_id = H5CreateOrOpenGroup(file_id, subgroup); H5Gclose (group_id); } // then open or create the group we want if(H5Lexists(file_id, group_name.c_str(), H5P_DEFAULT)) { group_id = H5Gopen2(file_id, group_name.c_str(), H5P_DEFAULT); } else { group_id = H5Gcreate2(file_id, group_name.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); } } return(group_id); }