H5StringData::~H5StringData() { if (transformedData) { delete[] transformedData; } else { char ** _data = reinterpret_cast<char **>(getData()); hid_t space = H5Screate_simple(1, &totalSize, 0); hid_t type = H5Tcopy(H5T_C_S1); H5Tset_size(type, H5T_VARIABLE); H5Tset_strpad(type, H5T_STR_NULLTERM); herr_t err = H5Dvlen_reclaim(type, space, H5P_DEFAULT, _data); if (err < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot free the memory associated with String data")); } H5Tclose(type); H5Sclose(space); } }
/* mkstr * Borrwed from dtypes.c. * Creates a new string data type. Used in string padding tests */ static hid_t mkstr(size_t len, H5T_str_t strpad) { hid_t t; if((t = H5Tcopy(H5T_C_S1)) < 0) return -1; if(H5Tset_size(t, len) < 0) return -1; if(H5Tset_strpad(t, strpad) < 0) return -1; return t; }
//-------------------------------------------------------------------------- // Function: StrType::setStrpad ///\brief Defines the storage mechanism for this string datatype. ///\param strpad - IN: String padding type ///\exception H5::DataTypeIException ///\par Description /// For detail, please refer to the C layer Reference Manual at: /// http://www.hdfgroup.org/HDF5/doc/RM/RM_H5T.html#Datatype-SetStrpad // Programmer Binh-Minh Ribler - 2000 //-------------------------------------------------------------------------- void StrType::setStrpad( H5T_str_t strpad ) const { herr_t ret_value = H5Tset_strpad( id, strpad ); if( ret_value < 0 ) { throw DataTypeIException("StrType::setStrpad", "H5Tset_strpad failed"); } }
hid_t make_type( size_t size ) { bool err = false; hid_t type_id = H5Tcopy(H5T_C_S1); err |= H5Tset_size(type_id, size) < 0; err |= H5Tset_strpad(type_id, H5T_STR_NULLTERM) < 0; if (err) { throw error("creating null-terminated string datatype"); } return type_id; }
hid_t make_type( size_t size ) { bool err = false; hid_t type_id = H5Tcopy(H5T_C_S1); err |= H5Tset_size(type_id, size) < 0; err |= H5Tset_strpad(type_id, H5T_STR_SPACEPAD) < 0; if (err) { throw error("creating space-padded string dataspace"); } return type_id; }
void Simulation3D::dumpTimings(unsigned long* timings, hsize_t total_timings, unsigned int steps_per_timing) { std::ostringstream filename(std::ios::out); filename << dumpDir << "/timing_s" << blockSize << "_p" << world.size() << ".h5"; hid_t file_id=H5Fcreate(filename.str().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); hid_t timingspace=H5Screate_simple(1, & total_timings, NULL); hid_t dx_space = H5Screate(H5S_SCALAR); hid_t dt_space = H5Screate(H5S_SCALAR); hid_t bs_space = H5Screate(H5S_SCALAR); hid_t ns_space = H5Screate(H5S_SCALAR); hid_t alg_name_space = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); #ifndef YEE H5Tset_size(atype, xUpdateRHSs->getAlgName().length()); #else H5Tset_size(atype, std::string("Yee").length()); #endif H5Tset_strpad(atype, H5T_STR_NULLTERM); hid_t timing_dset_id = H5Dcreate(file_id, "timings", H5T_NATIVE_LONG, timingspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); hid_t dx_attr_id = H5Acreate(timing_dset_id, "dx", H5T_NATIVE_DOUBLE, dx_space, H5P_DEFAULT, H5P_DEFAULT); hid_t dt_attr_id = H5Acreate(timing_dset_id, "dt", H5T_NATIVE_DOUBLE, dt_space, H5P_DEFAULT, H5P_DEFAULT); hid_t bs_attr_id = H5Acreate(timing_dset_id, "blockSize", H5T_NATIVE_UINT, bs_space, H5P_DEFAULT, H5P_DEFAULT); hid_t ns_attr_id = H5Acreate(timing_dset_id, "stepsPerTiming", H5T_NATIVE_UINT, ns_space, H5P_DEFAULT, H5P_DEFAULT); hid_t alg_attr_id = H5Acreate(timing_dset_id,"communicationStrategy", atype, alg_name_space, H5P_DEFAULT, H5P_DEFAULT); herr_t status = H5Dwrite(timing_dset_id, H5T_NATIVE_LONG, H5S_ALL, H5S_ALL, H5P_DEFAULT, timings); status = H5Awrite(dx_attr_id, H5T_NATIVE_DOUBLE, & dx); status = H5Awrite(dt_attr_id, H5T_NATIVE_DOUBLE, & dt); status = H5Awrite(bs_attr_id, H5T_NATIVE_UINT, & blockSize); status = H5Awrite(ns_attr_id, H5T_NATIVE_UINT, & steps_per_timing); #ifndef YEE status = H5Awrite(alg_attr_id, atype, xUpdateRHSs->getAlgName().c_str()); #else status = H5Awrite(alg_attr_id, atype, "Yee"); #endif H5Sclose(timingspace); H5Sclose(dx_space); H5Sclose(dt_space); H5Sclose(alg_name_space); H5Sclose(bs_space); H5Sclose(ns_space); H5Tclose(atype); H5Aclose(dx_attr_id); H5Aclose(dt_attr_id); H5Aclose(bs_attr_id); H5Aclose(alg_attr_id); H5Aclose(ns_attr_id); H5Dclose(timing_dset_id); H5Fclose(file_id); }
// Constructor FileIO::FileIO(Parallel *_parallel, Setup *setup): parallel(_parallel) { // Set Initial values inputFileName = setup->get("DataOutput.InputFileName", "--- None ---"); outputFileName = setup->get("DataOutput.OutputFileName", "default.h5"); info = setup->get("DataOutput.Info", "No information provided"); resumeFile = setup->get("DataOutput.Resume", 0); overwriteFile = setup->get("DataOutput.Overwrite", 0) || (setup->flags & HELIOS_OVERWRITE); dataFileFlushTiming = Timing(setup->get("DataOutput.Flush.Step", -1) , setup->get("DataOutput.Flush.Time", 100.)); ///////// Define Timeing Datatype timing_tid = H5Tcreate(H5T_COMPOUND, sizeof(Timing)); H5Tinsert(timing_tid, "Timestep", HOFFSET(Timing, step), H5T_NATIVE_INT ); H5Tinsert(timing_tid, "Time" , HOFFSET(Timing, time), H5T_NATIVE_DOUBLE); // don't changes r and i name otherwise it will break compatibiltiy with pyTables complex_tid = H5Tcreate(H5T_COMPOUND, sizeof (Complex)); H5Tinsert(complex_tid, "r", HOFFSET(Complex,r), H5T_NATIVE_DOUBLE); H5Tinsert(complex_tid, "i", HOFFSET(Complex,i), H5T_NATIVE_DOUBLE); vector3D_tid = H5Tcreate(H5T_COMPOUND, sizeof (Vector3D)); H5Tinsert(vector3D_tid, "x", HOFFSET(Vector3D,x), H5T_NATIVE_DOUBLE); H5Tinsert(vector3D_tid, "y", HOFFSET(Vector3D,y), H5T_NATIVE_DOUBLE); H5Tinsert(vector3D_tid, "z", HOFFSET(Vector3D,z), H5T_NATIVE_DOUBLE); hsize_t species_dim[] = { setup->get("Grid.Ns", 1)}; species_tid = H5Tarray_create(H5T_NATIVE_DOUBLE, 1, species_dim); // used for species name // BUG : Somehow HDF-8 stores up to 8 chars fro 64 possible. Rest are truncated ! Why ? s256_tid = H5Tcopy(H5T_C_S1); H5Tset_size(s256_tid, 64); H5Tset_strpad(s256_tid, H5T_STR_NULLTERM); offset0[0] = 0; offset0[1] = 0; offset0[2] = 0; offset0[3] = 0; offset0[4] = 0; offset0[5] = 0; offset0[6] = 0; offset0[7] = 0; // Create/Load HDF5 file if(resumeFile == false || (inputFileName != outputFileName)) create(setup); }
void writeStringAttribute(hid_t element, const char *attr_name, const char *attr_value) { herr_t hdfstatus = -1; hid_t hdfdatatype = -1; hid_t hdfattr = -1; hid_t hdfattrdataspace = -1; hdfattrdataspace = H5Screate(H5S_SCALAR); hdfdatatype = H5Tcopy(H5T_C_S1); hdfstatus = H5Tset_size(hdfdatatype, strlen(attr_value)); hdfstatus = H5Tset_strpad(hdfdatatype, H5T_STR_NULLTERM); hdfattr = H5Acreate2(element, attr_name, hdfdatatype, hdfattrdataspace, H5P_DEFAULT, H5P_DEFAULT); hdfstatus = H5Awrite(hdfattr, hdfdatatype, attr_value); H5Aclose (hdfattr); H5Sclose(hdfattrdataspace); return; }
herr_t ASDF_write_string_attribute(hid_t dataset_id, const char *attr_name, const char *attr_value) { hid_t space_id, type_id, attr_id; CHK_H5(space_id = H5Screate(H5S_SCALAR)); CHK_H5(type_id = H5Tcopy(H5T_C_S1)); CHK_H5(H5Tset_size(type_id, strlen(attr_value))); CHK_H5(H5Tset_strpad(type_id,H5T_STR_NULLPAD)); CHK_H5(attr_id = H5Acreate(dataset_id, attr_name, type_id, space_id, H5P_DEFAULT, H5P_DEFAULT)); CHK_H5(H5Awrite(attr_id, type_id, attr_value)); CHK_H5(H5Aclose(attr_id)); CHK_H5(H5Tclose(type_id)); CHK_H5(H5Sclose(space_id)); return 0; // Success }
extern void put_string_attribute(hid_t parent, char *name, char *value) { hid_t attr, space_attr, typ_attr; hsize_t dim_attr[1] = {1}; // Single dimension array of values if (!value) value = ""; typ_attr = H5Tcopy(H5T_C_S1); if (typ_attr < 0) { debug3("PROFILE: failed to copy type for attribute %s", name); return; } H5Tset_size(typ_attr, strlen(value)); H5Tset_strpad(typ_attr, H5T_STR_NULLTERM); space_attr = H5Screate_simple(1, dim_attr, NULL); if (space_attr < 0) { H5Tclose(typ_attr); debug3("PROFILE: failed to create space for attribute %s", name); return; } attr = H5Acreate(parent, name, typ_attr, space_attr, H5P_DEFAULT, H5P_DEFAULT); if (attr < 0) { H5Tclose(typ_attr); H5Sclose(space_attr); debug3("PROFILE: failed to create attribute %s", name); return; } if (H5Awrite(attr, typ_attr, value) < 0) { debug3("PROFILE: failed to write attribute %s", name); // Fall through to release resources } H5Sclose(space_attr); H5Tclose(typ_attr); H5Aclose(attr); return; }
/*------------------------------------------------------------------------- * Function: test_multi * * Purpose: Tests the file handle interface for MUTLI driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * * Modifications: * * Raymond Lu * Wednesday, June 23, 2004 * Added test for H5Fget_filesize. * *------------------------------------------------------------------------- */ static herr_t test_multi(void) { hid_t file=(-1), fapl, fapl2=(-1), dset=(-1), space=(-1); hid_t root, attr, aspace, atype; hid_t access_fapl = -1; char filename[1024]; int *fhandle2=NULL, *fhandle=NULL; hsize_t file_size; H5FD_mem_t mt, memb_map[H5FD_MEM_NTYPES]; hid_t memb_fapl[H5FD_MEM_NTYPES]; haddr_t memb_addr[H5FD_MEM_NTYPES]; const char *memb_name[H5FD_MEM_NTYPES]; char sv[H5FD_MEM_NTYPES][32]; hsize_t dims[2]={MULTI_SIZE, MULTI_SIZE}; hsize_t adims[1]={1}; char dname[]="dataset"; char meta[] = "this is some metadata on this file"; int i, j; int buf[MULTI_SIZE][MULTI_SIZE]; TESTING("MULTI file driver"); /* Set file access property list for MULTI driver */ fapl = h5_fileaccess(); HDmemset(memb_map, 0, sizeof memb_map); HDmemset(memb_fapl, 0, sizeof memb_fapl); HDmemset(memb_name, 0, sizeof memb_name); HDmemset(memb_addr, 0, sizeof memb_addr); HDmemset(sv, 0, sizeof sv); for(mt=H5FD_MEM_DEFAULT; mt<H5FD_MEM_NTYPES; H5_INC_ENUM(H5FD_mem_t,mt)) { memb_fapl[mt] = H5P_DEFAULT; memb_map[mt] = H5FD_MEM_SUPER; } memb_map[H5FD_MEM_DRAW] = H5FD_MEM_DRAW; memb_map[H5FD_MEM_BTREE] = H5FD_MEM_BTREE; memb_map[H5FD_MEM_GHEAP] = H5FD_MEM_GHEAP; sprintf(sv[H5FD_MEM_SUPER], "%%s-%c.h5", 's'); memb_name[H5FD_MEM_SUPER] = sv[H5FD_MEM_SUPER]; memb_addr[H5FD_MEM_SUPER] = 0; sprintf(sv[H5FD_MEM_BTREE], "%%s-%c.h5", 'b'); memb_name[H5FD_MEM_BTREE] = sv[H5FD_MEM_BTREE]; memb_addr[H5FD_MEM_BTREE] = HADDR_MAX/4; sprintf(sv[H5FD_MEM_DRAW], "%%s-%c.h5", 'r'); memb_name[H5FD_MEM_DRAW] = sv[H5FD_MEM_DRAW]; memb_addr[H5FD_MEM_DRAW] = HADDR_MAX/2; sprintf(sv[H5FD_MEM_GHEAP], "%%s-%c.h5", 'g'); memb_name[H5FD_MEM_GHEAP] = sv[H5FD_MEM_GHEAP]; memb_addr[H5FD_MEM_GHEAP] = HADDR_MAX*3/4; if(H5Pset_fapl_multi(fapl, memb_map, memb_fapl, memb_name, memb_addr, TRUE) < 0) TEST_ERROR; h5_fixname(FILENAME[4], fapl, filename, sizeof filename); if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Test wrong ways to reopen multi files */ if(test_multi_opens(filename) < 0) TEST_ERROR; /* Reopen the file */ if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Create and write data set */ if((space=H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* Before any data is written, the raw data file is empty. So * the file size is only the size of b-tree + HADDR_MAX/4. */ if(file_size < HADDR_MAX/4 || file_size > HADDR_MAX/2) TEST_ERROR; if((dset=H5Dcreate2(file, dname, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; for(i=0; i<MULTI_SIZE; i++) for(j=0; j<MULTI_SIZE; j++) buf[i][j] = i*10000+j; if(H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) TEST_ERROR; if((fapl2=H5Pcreate(H5P_FILE_ACCESS)) < 0) TEST_ERROR; if(H5Pset_multi_type(fapl2, H5FD_MEM_SUPER) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle) < 0) TEST_ERROR; if(*fhandle<0) TEST_ERROR; if(H5Pset_multi_type(fapl2, H5FD_MEM_DRAW) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle2) < 0) TEST_ERROR; if(*fhandle2<0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* After the data is written, the file size is huge because the * beginning of raw data file is set at HADDR_MAX/2. It's supposed * to be (HADDR_MAX/2 + 128*128*4) */ if(file_size < HADDR_MAX/2 || file_size > HADDR_MAX) TEST_ERROR; if(H5Sclose(space) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Pclose(fapl2) < 0) TEST_ERROR; /* Create and write attribute for the root group. */ if((root = H5Gopen2(file, "/", H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Attribute string. */ if((atype = H5Tcopy(H5T_C_S1)) < 0) TEST_ERROR; if(H5Tset_size(atype, strlen(meta) + 1) < 0) TEST_ERROR; if(H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0) TEST_ERROR; /* Create and write attribute */ if((aspace = H5Screate_simple(1, adims, NULL)) < 0) TEST_ERROR; if((attr = H5Acreate2(root, "Metadata", atype, aspace, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Awrite(attr, atype, meta) < 0) TEST_ERROR; /* Close IDs */ if(H5Tclose(atype) < 0) TEST_ERROR; if(H5Sclose(aspace) < 0) TEST_ERROR; if(H5Aclose(attr) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Sclose(space); H5Dclose(dset); H5Pclose(fapl); H5Pclose(fapl2); H5Fclose(file); } H5E_END_TRY; return -1; }
int main(void) { hid_t fil,spc,set; hid_t cs6, cmp, fix; hid_t cmp1, cmp2, cmp3; hid_t plist; hid_t array_dt; hsize_t dim[2]; hsize_t cdim[4]; char string5[5]; float fok[2] = {1234., 2341.}; float fnok[2] = {5678., 6785.}; float *fptr; char *data; char *mname; int result = 0; printf("%-70s", "Testing alignment in compound datatypes"); strcpy(string5, "Hi!"); HDunlink(fname); fil = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); if (fil < 0) { puts("*FAILED*"); return 1; } H5E_BEGIN_TRY { H5Ldelete(fil, setname, H5P_DEFAULT); } H5E_END_TRY; cs6 = H5Tcopy(H5T_C_S1); H5Tset_size(cs6, sizeof(string5)); H5Tset_strpad(cs6, H5T_STR_NULLPAD); cmp = H5Tcreate(H5T_COMPOUND, sizeof(fok) + sizeof(string5) + sizeof(fnok)); H5Tinsert(cmp, "Awkward length", 0, cs6); cdim[0] = sizeof(fok) / sizeof(float); array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim); H5Tinsert(cmp, "Ok", sizeof(string5), array_dt); H5Tclose(array_dt); cdim[0] = sizeof(fnok) / sizeof(float); array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim); H5Tinsert(cmp, "Not Ok", sizeof(fok) + sizeof(string5), array_dt); H5Tclose(array_dt); fix=h5tools_get_native_type(cmp); cmp1 = H5Tcreate(H5T_COMPOUND, sizeof(fok)); cdim[0] = sizeof(fok) / sizeof(float); array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim); H5Tinsert(cmp1, "Ok", 0, array_dt); H5Tclose(array_dt); cmp2 = H5Tcreate(H5T_COMPOUND, sizeof(string5)); H5Tinsert(cmp2, "Awkward length", 0, cs6); cmp3 = H5Tcreate(H5T_COMPOUND, sizeof(fnok)); cdim[0] = sizeof(fnok) / sizeof(float); array_dt = H5Tarray_create2(H5T_NATIVE_FLOAT, 1, cdim); H5Tinsert(cmp3, "Not Ok", 0, array_dt); H5Tclose(array_dt); plist = H5Pcreate(H5P_DATASET_XFER); H5Pset_preserve(plist, 1); /* * Create a small dataset, and write data into it we write each field * in turn so that we are avoid alignment issues at this point */ dim[0] = 1; spc = H5Screate_simple(1, dim, NULL); set = H5Dcreate2(fil, setname, cmp, spc, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(set, cmp1, spc, H5S_ALL, plist, fok); H5Dwrite(set, cmp2, spc, H5S_ALL, plist, string5); H5Dwrite(set, cmp3, spc, H5S_ALL, plist, fnok); H5Dclose(set); /* Now open the set, and read it back in */ data = malloc(H5Tget_size(fix)); if(!data) { perror("malloc() failed"); abort(); } set = H5Dopen2(fil, setname, H5P_DEFAULT); H5Dread(set, fix, spc, H5S_ALL, H5P_DEFAULT, data); fptr = (float *)(data + H5Tget_member_offset(fix, 1)); if(fok[0] != fptr[0] || fok[1] != fptr[1] || fnok[0] != fptr[2] || fnok[1] != fptr[3]) { result = 1; printf("%14s (%2d) %6s = %s\n", mname = H5Tget_member_name(fix, 0), (int)H5Tget_member_offset(fix,0), string5, (char *)(data + H5Tget_member_offset(fix, 0))); free(mname); fptr = (float *)(data + H5Tget_member_offset(fix, 1)); printf("Data comparison:\n" "%14s (%2d) %6f = %f\n" " %6f = %f\n", mname = H5Tget_member_name(fix, 1), (int)H5Tget_member_offset(fix,1), fok[0], fptr[0], fok[1], fptr[1]); free(mname); fptr = (float *)(data + H5Tget_member_offset(fix, 2)); printf("%14s (%2d) %6f = %f\n" " %6f = %6f\n", mname = H5Tget_member_name(fix, 2), (int)H5Tget_member_offset(fix,2), fnok[0], fptr[0], fnok[1], fptr[1]); free(mname); fptr = (float *)(data + H5Tget_member_offset(fix, 1)); printf("\n" "Short circuit\n" " %6f = %f\n" " %6f = %f\n" " %6f = %f\n" " %6f = %f\n", fok[0], fptr[0], fok[1], fptr[1], fnok[0], fptr[2], fnok[1], fptr[3]); puts("*FAILED*"); } else { puts(" PASSED"); } free(data); H5Sclose(spc); H5Tclose(cmp); H5Tclose(cmp1); H5Tclose(cmp2); H5Tclose(cmp3); H5Pclose(plist); H5Fclose(fil); HDunlink(fname); fflush(stdout); return result; }
int NC4_write_ncproperties(NC_FILE_INFO_T* h5) { int retval = NC_NOERR; hid_t hdf5grpid = -1; hid_t attid = -1; hid_t aspace = -1; hid_t atype = -1; char* text = NULL; size_t len = 0; LOG((3, "%s", __func__)); /* If the file is read-only, return an error. */ if (h5->no_write) {retval = NC_EPERM; goto done;} hdf5grpid = ((NC_HDF5_GRP_INFO_T *)(h5->root_grp->format_grp_info))->hdf_grpid; if(H5Aexists(hdf5grpid,NCPROPS) > 0) /* Already exists, no overwrite */ goto done; /* Build the attribute string */ if((retval = NC4_buildpropinfo(&h5->provenance->propattr,&text))) goto done; /* Build the HDF5 string type */ if ((atype = H5Tcopy(H5T_C_S1)) < 0) {retval = NC_EHDFERR; goto done;} if (H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0) {retval = NC_EHDFERR; goto done;} if(H5Tset_cset(atype, H5T_CSET_ASCII) < 0) {retval = NC_EHDFERR; goto done;} len = strlen(text); if(H5Tset_size(atype, len) < 0) {retval = NC_EFILEMETA; goto done;} /* Create NCPROPS attribute */ if((aspace = H5Screate(H5S_SCALAR)) < 0) {retval = NC_EFILEMETA; goto done;} if ((attid = H5Acreate(hdf5grpid, NCPROPS, atype, aspace, H5P_DEFAULT)) < 0) {retval = NC_EFILEMETA; goto done;} if (H5Awrite(attid, atype, text) < 0) {retval = NC_EFILEMETA; goto done;} /* Verify */ #if 0 { hid_t spacev, typev; hsize_t dsize, tsize; typev = H5Aget_type(attid); spacev = H5Aget_space(attid); dsize = H5Aget_storage_size(attid); tsize = H5Tget_size(typev); fprintf(stderr,"dsize=%lu tsize=%lu\n",(unsigned long)dsize,(unsigned long)tsize); } #endif done: if(text != NULL) free(text); /* Close out the HDF5 objects */ if(attid > 0 && H5Aclose(attid) < 0) retval = NC_EHDFERR; if(aspace > 0 && H5Sclose(aspace) < 0) retval = NC_EHDFERR; if(atype > 0 && H5Tclose(atype) < 0) retval = NC_EHDFERR; /* For certain errors, actually fail, else log that attribute was invalid and ignore */ switch (retval) { case NC_ENOMEM: case NC_EHDFERR: case NC_EPERM: case NC_EFILEMETA: case NC_NOERR: break; default: LOG((0,"Invalid _NCProperties attribute")); retval = NC_NOERR; break; } return retval; }
/****if* H5_f/h5init_types_c * NAME * h5init_types_c * PURPOSE * Initialize predefined datatypes in Fortran * INPUTS * types - array with the predefined Native Fortran * type, its element and length must be the * same as the types array defined in the * H5f90global.f90 * floatingtypes - array with the predefined Floating Fortran * type, its element and length must be the * same as the floatingtypes array defined in the * H5f90global.f90 * integertypes - array with the predefined Integer Fortran * type, its element and length must be the * same as the integertypes array defined in the * H5f90global.f90 * RETURNS * 0 on success, -1 on failure * AUTHOR * Elena Pourmal * Tuesday, August 3, 1999 * SOURCE */ int_f nh5init_types_c( hid_t_f * types, hid_t_f * floatingtypes, hid_t_f * integertypes ) /******/ { int ret_value = -1; hid_t c_type_id; size_t tmp_val; /* Fortran INTEGER is may not be the same as C in; do all checking to find an appropriate size */ if (sizeof(int_f) == sizeof(int)) { if ((types[0] = (hid_t_f)H5Tcopy(H5T_NATIVE_INT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_f) == sizeof(long)) { if ((types[0] = (hid_t_f)H5Tcopy(H5T_NATIVE_LONG)) < 0) return ret_value; } /*end if */ else if (sizeof(int_f) == sizeof(long long)) { if ((types[0] = (hid_t_f)H5Tcopy(H5T_NATIVE_LLONG)) < 0) return ret_value; } /*end else */ /* Find appropriate size to store Fortran REAL */ if(sizeof(real_f)==sizeof(float)) { if ((types[1] = (hid_t_f)H5Tcopy(H5T_NATIVE_FLOAT)) < 0) return ret_value; } /* end if */ else if(sizeof(real_f)==sizeof(double)){ if ((types[1] = (hid_t_f)H5Tcopy(H5T_NATIVE_DOUBLE)) < 0) return ret_value; } /* end if */ #if H5_SIZEOF_LONG_DOUBLE!=0 else if (sizeof(real_f) == sizeof(long double)) { if ((types[1] = (hid_t_f)H5Tcopy(H5T_NATIVE_LDOUBLE)) < 0) return ret_value; } /* end else */ #endif /* Find appropriate size to store Fortran DOUBLE */ if(sizeof(double_f)==sizeof(double)) { if ((types[2] = (hid_t_f)H5Tcopy(H5T_NATIVE_DOUBLE)) < 0) return ret_value; }/*end if */ #if H5_SIZEOF_LONG_DOUBLE!=0 else if(sizeof(double_f)==sizeof(long double)) { if ((types[2] = (hid_t_f)H5Tcopy(H5T_NATIVE_LDOUBLE)) < 0) return ret_value; }/*end else */ #endif /* if ((types[3] = H5Tcopy(H5T_NATIVE_UINT8)) < 0) return ret_value; */ if ((c_type_id = H5Tcopy(H5T_FORTRAN_S1)) < 0) return ret_value; tmp_val = 1; if(H5Tset_size(c_type_id, tmp_val) < 0) return ret_value; if(H5Tset_strpad(c_type_id, H5T_STR_SPACEPAD) < 0) return ret_value; types[3] = (hid_t_f)c_type_id; /* if ((types[3] = H5Tcopy(H5T_C_S1)) < 0) return ret_value; if(H5Tset_strpad(types[3],H5T_STR_NULLTERM) < 0) return ret_value; if(H5Tset_size(types[3],1) < 0) return ret_value; */ /* if ((types[3] = H5Tcopy(H5T_STD_I8BE)) < 0) return ret_value; */ if ((types[4] = (hid_t_f)H5Tcopy(H5T_STD_REF_OBJ)) < 0) return ret_value; if ((types[5] = (hid_t_f)H5Tcopy(H5T_STD_REF_DSETREG)) < 0) return ret_value; /* * FIND H5T_NATIVE_INTEGER_1 */ if (sizeof(int_1_f) == sizeof(char)) { if ((types[6] = (hid_t_f)H5Tcopy(H5T_NATIVE_CHAR)) < 0) return ret_value; } /*end if */ else if (sizeof(int_1_f) == sizeof(short)) { if ((types[6] = (hid_t_f)H5Tcopy(H5T_NATIVE_SHORT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_1_f) == sizeof(int)) { if ((types[6] = (hid_t_f)H5Tcopy(H5T_NATIVE_INT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_1_f) == sizeof(long long)) { if ((types[6] = (hid_t_f)H5Tcopy(H5T_NATIVE_LLONG)) < 0) return ret_value; } /*end else */ /* * FIND H5T_NATIVE_INTEGER_2 */ if (sizeof(int_2_f) == sizeof(char)) { if ((types[7] = (hid_t_f)H5Tcopy(H5T_NATIVE_CHAR)) < 0) return ret_value; } /*end if */ else if (sizeof(int_2_f) == sizeof(short)) { if ((types[7] = (hid_t_f)H5Tcopy(H5T_NATIVE_SHORT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_2_f) == sizeof(int)) { if ((types[7] = (hid_t_f)H5Tcopy(H5T_NATIVE_INT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_2_f) == sizeof(long long)) { if ((types[7] = (hid_t_f)H5Tcopy(H5T_NATIVE_LLONG)) < 0) return ret_value; } /*end else */ /* * FIND H5T_NATIVE_INTEGER_4 */ if (sizeof(int_4_f) == sizeof(char)) { if ((types[8] = (hid_t_f)H5Tcopy(H5T_NATIVE_CHAR)) < 0) return ret_value; } /*end if */ else if (sizeof(int_4_f) == sizeof(short)) { if ((types[8] = (hid_t_f)H5Tcopy(H5T_NATIVE_SHORT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_4_f) == sizeof(int)) { if ((types[8] = (hid_t_f)H5Tcopy(H5T_NATIVE_INT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_4_f) == sizeof(long long)) { if ((types[8] = (hid_t_f)H5Tcopy(H5T_NATIVE_LLONG)) < 0) return ret_value; } /*end else */ /* * FIND H5T_NATIVE_INTEGER_8 */ if (sizeof(int_8_f) == sizeof(char)) { if ((types[9] = (hid_t_f)H5Tcopy(H5T_NATIVE_CHAR)) < 0) return ret_value; } /*end if */ else if (sizeof(int_8_f) == sizeof(short)) { if ((types[9] = (hid_t_f)H5Tcopy(H5T_NATIVE_SHORT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_8_f) == sizeof(int)) { if ((types[9] = (hid_t_f)H5Tcopy(H5T_NATIVE_INT)) < 0) return ret_value; } /*end if */ else if (sizeof(int_8_f) == sizeof(long long)) { if ((types[9] = (hid_t_f)H5Tcopy(H5T_NATIVE_LLONG)) < 0) return ret_value; } /*end else */ /* * FIND H5T_NATIVE_REAL_4 */ if (sizeof(real_4_f) == sizeof(float)) { if ((types[10] = (hid_t_f)H5Tcopy(H5T_NATIVE_FLOAT)) < 0) return ret_value; } /*end if */ else if (sizeof(real_4_f) == sizeof(double)) { if ((types[10] = (hid_t_f)H5Tcopy(H5T_NATIVE_DOUBLE)) < 0) return ret_value; } /*end if */ #if H5_SIZEOF_LONG_DOUBLE!=0 else if (sizeof(real_4_f) == sizeof(long double)) { if ((types[10] = (hid_t_f)H5Tcopy(H5T_NATIVE_LDOUBLE)) < 0) return ret_value; } /*end else */ #endif /* * FIND H5T_NATIVE_REAL_8 */ if (sizeof(real_8_f) == sizeof(float)) { if ((types[11] = (hid_t_f)H5Tcopy(H5T_NATIVE_FLOAT)) < 0) return ret_value; } /*end if */ else if (sizeof(real_8_f) == sizeof(double)) { if ((types[11] = (hid_t_f)H5Tcopy(H5T_NATIVE_DOUBLE)) < 0) return ret_value; } /*end if */ #if H5_SIZEOF_LONG_DOUBLE!=0 else if (sizeof(real_8_f) == sizeof(long double)) { if ((types[11] = (hid_t_f)H5Tcopy(H5T_NATIVE_LDOUBLE)) < 0) return ret_value; } /*end else */ #endif /* * FIND H5T_NATIVE_REAL_16 */ if (sizeof(real_16_f) == sizeof(float)) { if ((types[12] = (hid_t_f)H5Tcopy(H5T_NATIVE_FLOAT)) < 0) return ret_value; } /*end if */ else if (sizeof(real_16_f) == sizeof(double)) { if ((types[12] = (hid_t_f)H5Tcopy(H5T_NATIVE_DOUBLE)) < 0) return ret_value; } /*end if */ #if H5_SIZEOF_LONG_DOUBLE!=0 else if (sizeof(real_16_f) == sizeof(long double)) { if ((types[12] = (hid_t_f)H5Tcopy(H5T_NATIVE_LDOUBLE)) < 0) return ret_value; } /*end else */ #endif /* * FIND H5T_NATIVE_B_8 */ if ((types[13] = (hid_t_f)H5Tcopy(H5T_NATIVE_B8)) < 0) return ret_value; if ((types[14] = (hid_t_f)H5Tcopy(H5T_NATIVE_B16)) < 0) return ret_value; if ((types[15] = (hid_t_f)H5Tcopy(H5T_NATIVE_B32)) < 0) return ret_value; if ((types[16] = (hid_t_f)H5Tcopy(H5T_NATIVE_B64)) < 0) return ret_value; if ((floatingtypes[0] = (hid_t_f)H5Tcopy(H5T_IEEE_F32BE)) < 0) return ret_value; if ((floatingtypes[1] = (hid_t_f)H5Tcopy(H5T_IEEE_F32LE)) < 0) return ret_value; if ((floatingtypes[2] = (hid_t_f)H5Tcopy(H5T_IEEE_F64BE)) < 0) return ret_value; if ((floatingtypes[3] = (hid_t_f)H5Tcopy(H5T_IEEE_F64LE)) < 0) return ret_value; if ((integertypes[0] = (hid_t_f)H5Tcopy(H5T_STD_I8BE)) < 0) return ret_value; if ((integertypes[1] = (hid_t_f)H5Tcopy(H5T_STD_I8LE)) < 0) return ret_value; if ((integertypes[2] = (hid_t_f)H5Tcopy(H5T_STD_I16BE)) < 0) return ret_value; if ((integertypes[3] = (hid_t_f)H5Tcopy(H5T_STD_I16LE)) < 0) return ret_value; if ((integertypes[4] = (hid_t_f)H5Tcopy(H5T_STD_I32BE)) < 0) return ret_value; if ((integertypes[5] = (hid_t_f)H5Tcopy(H5T_STD_I32LE)) < 0) return ret_value; if ((integertypes[6] = (hid_t_f)H5Tcopy(H5T_STD_I64BE)) < 0) return ret_value; if ((integertypes[7] = (hid_t_f)H5Tcopy(H5T_STD_I64LE)) < 0) return ret_value; if ((integertypes[8] = (hid_t_f)H5Tcopy(H5T_STD_U8BE)) < 0) return ret_value; if ((integertypes[9] = (hid_t_f)H5Tcopy(H5T_STD_U8LE)) < 0) return ret_value; if ((integertypes[10] = (hid_t_f)H5Tcopy(H5T_STD_U16BE)) < 0) return ret_value; if ((integertypes[11] = (hid_t_f)H5Tcopy(H5T_STD_U16LE)) < 0) return ret_value; if ((integertypes[12] = (hid_t_f)H5Tcopy(H5T_STD_U32BE)) < 0) return ret_value; if ((integertypes[13] = (hid_t_f)H5Tcopy(H5T_STD_U32LE)) < 0) return ret_value; if ((integertypes[14] = (hid_t_f)H5Tcopy(H5T_STD_U64BE)) < 0) return ret_value; if ((integertypes[15] = (hid_t_f)H5Tcopy(H5T_STD_U64LE)) < 0) return ret_value; if ((integertypes[17] = (hid_t_f)H5Tcopy(H5T_STD_B8BE)) < 0) return ret_value; if ((integertypes[18] = (hid_t_f)H5Tcopy(H5T_STD_B8LE)) < 0) return ret_value; if ((integertypes[19] = (hid_t_f)H5Tcopy(H5T_STD_B16BE)) < 0) return ret_value; if ((integertypes[20] = (hid_t_f)H5Tcopy(H5T_STD_B16LE)) < 0) return ret_value; if ((integertypes[21] = (hid_t_f)H5Tcopy(H5T_STD_B32BE)) < 0) return ret_value; if ((integertypes[22] = (hid_t_f)H5Tcopy(H5T_STD_B32LE)) < 0) return ret_value; if ((integertypes[23] = (hid_t_f)H5Tcopy(H5T_STD_B64BE)) < 0) return ret_value; if ((integertypes[24] = (hid_t_f)H5Tcopy(H5T_STD_B64LE)) < 0) return ret_value; if ((integertypes[25] = (hid_t_f)H5Tcopy(H5T_FORTRAN_S1)) < 0) return ret_value; if ((integertypes[26] = (hid_t_f)H5Tcopy(H5T_C_S1)) < 0) return ret_value; /* * Define Fortran H5T_STRING type to store non-fixed size strings */ if ((c_type_id = H5Tcopy(H5T_C_S1)) < 0) return ret_value; if(H5Tset_size(c_type_id, H5T_VARIABLE) < 0) return ret_value; integertypes[16] = c_type_id; ret_value = 0; return ret_value; }
/**************************************************************** ** ** test_vlstring_type(): Test VL string type. ** Tests if VL string is treated as string. ** ****************************************************************/ static void test_vlstring_type(void) { hid_t fid; /* HDF5 File IDs */ hid_t tid_vlstr; H5T_cset_t cset; H5T_str_t pad; htri_t vl_str; /* Whether string is VL */ herr_t ret; /* Output message about test being performed */ MESSAGE(5, ("Testing VL String type\n")); /* Open file */ fid = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT); CHECK(fid, FAIL, "H5Fopen"); /* Create a datatype to refer to */ tid_vlstr = H5Tcopy(H5T_C_S1); CHECK(tid_vlstr, FAIL, "H5Tcopy"); /* Change padding and verify it */ ret = H5Tset_strpad(tid_vlstr, H5T_STR_NULLPAD); CHECK(ret, FAIL, "H5Tset_strpad"); pad = H5Tget_strpad(tid_vlstr); VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad"); /* Convert to variable-length string */ ret = H5Tset_size(tid_vlstr, H5T_VARIABLE); CHECK(ret, FAIL, "H5Tset_size"); /* Check if datatype is VL string */ ret = H5Tget_class(tid_vlstr); VERIFY(ret, H5T_STRING, "H5Tget_class"); ret = H5Tis_variable_str(tid_vlstr); VERIFY(ret, TRUE, "H5Tis_variable_str"); /* Verify that the class detects as a string */ vl_str = H5Tdetect_class(tid_vlstr, H5T_STRING); CHECK(vl_str, FAIL, "H5Tdetect_class"); VERIFY(vl_str, TRUE, "H5Tdetect_class"); /* Check default character set and padding */ cset = H5Tget_cset(tid_vlstr); VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset"); pad = H5Tget_strpad(tid_vlstr); VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad"); /* Commit variable-length string datatype to storage */ ret = H5Tcommit2(fid, VLSTR_TYPE, tid_vlstr, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(ret, FAIL, "H5Tcommit2"); /* Close datatype */ ret = H5Tclose(tid_vlstr); CHECK(ret, FAIL, "H5Tclose"); tid_vlstr = H5Topen2(fid, VLSTR_TYPE, H5P_DEFAULT); CHECK(tid_vlstr, FAIL, "H5Topen2"); ret = H5Tclose(tid_vlstr); CHECK(ret, FAIL, "H5Tclose"); ret = H5Fclose(fid); CHECK(ret, FAIL, "H5Fclose"); fid = H5Fopen(DATAFILE, H5F_ACC_RDWR, H5P_DEFAULT); CHECK(fid, FAIL, "H5Fopen"); /* Open the variable-length string datatype just created */ tid_vlstr = H5Topen2(fid, VLSTR_TYPE, H5P_DEFAULT); CHECK(tid_vlstr, FAIL, "H5Topen2"); /* Verify character set and padding */ cset = H5Tget_cset(tid_vlstr); VERIFY(cset, H5T_CSET_ASCII, "H5Tget_cset"); pad = H5Tget_strpad(tid_vlstr); VERIFY(pad, H5T_STR_NULLPAD, "H5Tget_strpad"); /* Close datatype and file */ ret = H5Tclose(tid_vlstr); CHECK(ret, FAIL, "H5Tclose"); ret = H5Fclose(fid); CHECK(ret, FAIL, "H5Fclose"); } /* end test_vlstring_type() */
/*--------------------------------------------------------------------------- * Name: h5init_types_c * Purpose: Initialize predefined datatypes in Fortran * Inputs: types - array with the predefined Native Fortran * type, its element and length must be the * same as the types array defined in the * H5f90global.f90 * floatingtypes - array with the predefined Floating Fortran * type, its element and length must be the * same as the floatingtypes array defined in the * H5f90global.f90 * integertypes - array with the predefined Integer Fortran * type, its element and length must be the * same as the integertypes array defined in the * H5f90global.f90 * Outputs: None * Returns: 0 on success, -1 on failure * Programmer: Elena Pourmal * Tuesday, August 3, 1999 * Modifications: *---------------------------------------------------------------------------*/ int_f nh5init_types_c( hid_t_f * types, hid_t_f * floatingtypes, hid_t_f * integertypes ) { int ret_value = -1; hid_t c_type_id; if ((types[0] = (hid_t_f)H5Tcopy(H5T_NATIVE_INT)) < 0) return ret_value; /* Accomodate Crays with this check */ if(sizeof(real_f)==sizeof(double)) { if ((types[1] = (hid_t_f)H5Tcopy(H5T_NATIVE_DOUBLE)) < 0) return ret_value; } /* end if */ else { if ((types[1] = (hid_t_f)H5Tcopy(H5T_NATIVE_FLOAT)) < 0) return ret_value; } /* end else */ if ((types[2] = (hid_t_f)H5Tcopy(H5T_NATIVE_DOUBLE)) < 0) return ret_value; /* if ((types[3] = H5Tcopy(H5T_NATIVE_UINT8)) < 0) return ret_value; */ if ((c_type_id = H5Tcopy(H5T_FORTRAN_S1)) < 0) return ret_value; if(H5Tset_size(c_type_id, 1) < 0) return ret_value; if(H5Tset_strpad(c_type_id, H5T_STR_SPACEPAD) < 0) return ret_value; types[3] = (hid_t_f)c_type_id; /* if ((types[3] = H5Tcopy(H5T_C_S1)) < 0) return ret_value; if(H5Tset_strpad(types[3],H5T_STR_NULLTERM) < 0) return ret_value; if(H5Tset_size(types[3],1) < 0) return ret_value; */ /* if ((types[3] = H5Tcopy(H5T_STD_I8BE)) < 0) return ret_value; */ if ((types[4] = (hid_t_f)H5Tcopy(H5T_STD_REF_OBJ)) < 0) return ret_value; if ((types[5] = (hid_t_f)H5Tcopy(H5T_STD_REF_DSETREG)) < 0) return ret_value; if ((floatingtypes[0] = (hid_t_f)H5Tcopy(H5T_IEEE_F32BE)) < 0) return ret_value; if ((floatingtypes[1] = (hid_t_f)H5Tcopy(H5T_IEEE_F32LE)) < 0) return ret_value; if ((floatingtypes[2] = (hid_t_f)H5Tcopy(H5T_IEEE_F64BE)) < 0) return ret_value; if ((floatingtypes[3] = (hid_t_f)H5Tcopy(H5T_IEEE_F64LE)) < 0) return ret_value; if ((integertypes[0] = (hid_t_f)H5Tcopy(H5T_STD_I8BE)) < 0) return ret_value; if ((integertypes[1] = (hid_t_f)H5Tcopy(H5T_STD_I8LE)) < 0) return ret_value; if ((integertypes[2] = (hid_t_f)H5Tcopy(H5T_STD_I16BE)) < 0) return ret_value; if ((integertypes[3] = (hid_t_f)H5Tcopy(H5T_STD_I16LE)) < 0) return ret_value; if ((integertypes[4] = (hid_t_f)H5Tcopy(H5T_STD_I32BE)) < 0) return ret_value; if ((integertypes[5] = (hid_t_f)H5Tcopy(H5T_STD_I32LE)) < 0) return ret_value; if ((integertypes[6] = (hid_t_f)H5Tcopy(H5T_STD_I64BE)) < 0) return ret_value; if ((integertypes[7] = (hid_t_f)H5Tcopy(H5T_STD_I64LE)) < 0) return ret_value; if ((integertypes[8] = (hid_t_f)H5Tcopy(H5T_STD_U8BE)) < 0) return ret_value; if ((integertypes[9] = (hid_t_f)H5Tcopy(H5T_STD_U8LE)) < 0) return ret_value; if ((integertypes[10] = (hid_t_f)H5Tcopy(H5T_STD_U16BE)) < 0) return ret_value; if ((integertypes[11] = (hid_t_f)H5Tcopy(H5T_STD_U16LE)) < 0) return ret_value; if ((integertypes[12] = (hid_t_f)H5Tcopy(H5T_STD_U32BE)) < 0) return ret_value; if ((integertypes[13] = (hid_t_f)H5Tcopy(H5T_STD_U32LE)) < 0) return ret_value; if ((integertypes[14] = (hid_t_f)H5Tcopy(H5T_STD_U64BE)) < 0) return ret_value; if ((integertypes[15] = (hid_t_f)H5Tcopy(H5T_STD_U64LE)) < 0) return ret_value; /* * Define Fortran H5T_STRING type to store non-fixed size strings */ if ((c_type_id = H5Tcopy(H5T_C_S1)) < 0) return ret_value; if(H5Tset_size(c_type_id, H5T_VARIABLE) < 0) return ret_value; integertypes[16] = c_type_id; ret_value = 0; return ret_value; }
int main() { char filename[] = "myfile.h5"; hid_t file_id, dataspace_id; /* identifiers */ /* create a new HDF5 file using default properties. */ file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); //************** general ************************* hid_t string_dataspace = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy (H5T_C_S1); H5Tset_size (atype, 100); H5Tset_strpad(atype,H5T_STR_NULLTERM); hid_t string_dataset_id = H5Dcreate2(file_id, "/prog_version", atype, string_dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(string_dataset_id, atype, H5S_ALL, H5S_ALL, H5P_DEFAULT, "h5test v0.1.0"); H5Dclose(string_dataset_id); string_dataset_id = H5Dcreate2(file_id, "/comment", atype, string_dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(string_dataset_id, atype, H5S_ALL, H5S_ALL, H5P_DEFAULT, "hello world"); H5Dclose(string_dataset_id); //************** save 5x3 array ************************* hid_t pattern_dataset_id; hsize_t dims[2]; /* Create the data space for the pattern. */ dims[0] = 5; dims[1] = 3; //period, angle, phase dataspace_id = H5Screate_simple(2, dims, NULL); /* Create the dataset. */ pattern_dataset_id = H5Dcreate2(file_id, "/pattern", H5T_IEEE_F64LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); double pattern_data[dims[0]][dims[1]]; for (unsigned int i=0; i<dims[0]; ++i) { pattern_data[i][0] = i; pattern_data[i][1] = i*10.01; pattern_data[i][2] = i*100.01; } /* Write the dataset. */ H5Dwrite(pattern_dataset_id, H5T_IEEE_F64LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, pattern_data); //give attributes to /pattern hid_t attr1 = H5Acreate2(pattern_dataset_id, "author", atype, string_dataspace, H5P_DEFAULT, H5P_DEFAULT); H5Awrite(attr1, atype, "this pattern was created by h5test"); H5Aclose(attr1); attr1 = H5Acreate2(pattern_dataset_id, "comment", atype, string_dataspace, H5P_DEFAULT, H5P_DEFAULT); H5Awrite(attr1, atype, "add a comment"); H5Aclose(attr1); H5Sclose(string_dataspace); H5Tclose(atype); /* End access to the dataset and release resources used by it. */ H5Dclose(pattern_dataset_id); /* Terminate access to the data space. */ H5Sclose(dataspace_id); //**************** save int vector ******************** dims[0] = 5; int samples[] = {1, 3, 5, 7, 11}; hid_t cam_dataspace_id = H5Screate_simple(1, dims, NULL); hid_t cam_roi_dataset_id = H5Dcreate2(file_id, "/primes", H5T_STD_I32LE, cam_dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(cam_roi_dataset_id, H5T_STD_I32LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, samples); H5Sclose(cam_dataspace_id); H5Dclose(cam_roi_dataset_id); //**************** save scalar double ******************** double cam_exposure_time = 13.456789; hid_t cam_exposure_dataspace = H5Screate(H5S_SCALAR); hid_t cam_exposure_dataset_id = H5Dcreate2(file_id, "/exposure_time", H5T_IEEE_F64LE, cam_exposure_dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(cam_roi_dataset_id, H5T_IEEE_F64LE, H5S_ALL, H5S_ALL, H5P_DEFAULT, &cam_exposure_time); H5Sclose(cam_exposure_dataspace); H5Dclose(cam_exposure_dataset_id); H5Fclose( file_id ); /*************** create test image ***********************/ /* global buffer for image data */ hsize_t width = 256; hsize_t height = 256; int len = width * height * 3 * sizeof( unsigned char ); unsigned char *gbuf = (unsigned char*) malloc (len); //create image1 for(int n=0; n<len; n+=3) { gbuf[n]=n/3/width; //red gbuf[n+1]=(n/3)%width; //green gbuf[n+2]=0; //blue } append_hdf5_image(filename, "img1", gbuf, width, height); for(int n=0; n<len; n+=3) { gbuf[n]=n/3/width; gbuf[n+1]=(n/3)%width; gbuf[n+2]=gbuf[n]/2 + gbuf[n+1]/2; } append_hdf5_image(filename, "img2", gbuf, width, height); free(gbuf); return EXIT_SUCCESS; }
int main (void) { hid_t file, dataset; /* File and dataset identifiers */ hid_t fid; /* Dataspace identifier */ hid_t attr1, attr2, attr3; /* Attribute identifiers */ hid_t attr; hid_t aid1, aid2, aid3; /* Attribute dataspace identifiers */ hid_t atype, atype_mem; /* Attribute type */ H5T_class_t type_class; hsize_t fdim[] = {SIZE}; hsize_t adim[] = {ADIM1, ADIM2}; /* Dimensions of the first attribute */ float matrix[ADIM1][ADIM2]; /* Attribute data */ herr_t ret; /* Return value */ H5O_info_t oinfo; /* Object info */ unsigned i, j; /* Counters */ char string_out[80]; /* Buffer to read string attribute back */ int point_out; /* Buffer to read scalar attribute back */ /* * Data initialization. */ int vector[] = {1, 2, 3, 4, 5, 6, 7}; /* Dataset data */ int point = 1; /* Value of the scalar attribute */ char string[] = "ABCD"; /* Value of the string attribute */ for (i=0; i < ADIM1; i++) { /* Values of the array attribute */ for (j=0; j < ADIM2; j++) matrix[i][j] = -1.; } /* * Create a file. */ file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create the dataspace for the dataset in the file. */ fid = H5Screate(H5S_SIMPLE); ret = H5Sset_extent_simple(fid, RANK, fdim, NULL); /* * Create the dataset in the file. */ dataset = H5Dcreate2(file, "Dataset", H5T_NATIVE_INT, fid, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Write data to the dataset. */ ret = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL , H5S_ALL, H5P_DEFAULT, vector); /* * Create dataspace for the first attribute. */ aid1 = H5Screate(H5S_SIMPLE); ret = H5Sset_extent_simple(aid1, ARANK, adim, NULL); /* * Create array attribute. */ attr1 = H5Acreate2(dataset, ANAME, H5T_NATIVE_FLOAT, aid1, H5P_DEFAULT, H5P_DEFAULT); /* * Write array attribute. */ ret = H5Awrite(attr1, H5T_NATIVE_FLOAT, matrix); /* * Create scalar attribute. */ aid2 = H5Screate(H5S_SCALAR); attr2 = H5Acreate2(dataset, "Integer attribute", H5T_NATIVE_INT, aid2, H5P_DEFAULT, H5P_DEFAULT); /* * Write scalar attribute. */ ret = H5Awrite(attr2, H5T_NATIVE_INT, &point); /* * Create string attribute. */ aid3 = H5Screate(H5S_SCALAR); atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 5); H5Tset_strpad(atype,H5T_STR_NULLTERM); attr3 = H5Acreate2(dataset, ANAMES, atype, aid3, H5P_DEFAULT, H5P_DEFAULT); /* * Write string attribute. */ ret = H5Awrite(attr3, atype, string); /* * Close attribute and file dataspaces, and datatype. */ ret = H5Sclose(aid1); ret = H5Sclose(aid2); ret = H5Sclose(aid3); ret = H5Sclose(fid); ret = H5Tclose(atype); /* * Close the attributes. */ ret = H5Aclose(attr1); ret = H5Aclose(attr2); ret = H5Aclose(attr3); /* * Close the dataset. */ ret = H5Dclose(dataset); /* * Close the file. */ ret = H5Fclose(file); /* * Reopen the file. */ file = H5Fopen(H5FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT); /* * Open the dataset. */ dataset = H5Dopen2(file, "Dataset", H5P_DEFAULT); /* * Attach to the scalar attribute using attribute name, then read and * display its value. */ attr = H5Aopen(dataset, "Integer attribute", H5P_DEFAULT); ret = H5Aread(attr, H5T_NATIVE_INT, &point_out); printf("The value of the attribute \"Integer attribute\" is %d \n", point_out); ret = H5Aclose(attr); /* * Find string attribute by iterating through all attributes */ ret = H5Oget_info(dataset, &oinfo); for(i = 0; i < (unsigned)oinfo.num_attrs; i++) { attr = H5Aopen_by_idx(dataset, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, (hsize_t)i, H5P_DEFAULT, H5P_DEFAULT); atype = H5Aget_type(attr); type_class = H5Tget_class(atype); if (type_class == H5T_STRING) { atype_mem = H5Tget_native_type(atype, H5T_DIR_ASCEND); ret = H5Aread(attr, atype_mem, string_out); printf("Found string attribute; its index is %d , value = %s \n", i, string_out); ret = H5Tclose(atype_mem); } ret = H5Aclose(attr); ret = H5Tclose(atype); } /* * Get attribute info using iteration function. */ ret = H5Aiterate2(dataset, H5_INDEX_NAME, H5_ITER_INC, NULL, attr_info, NULL); /* * Close the dataset and the file. */ H5Dclose(dataset); H5Fclose(file); return 0; }
int main (int argc, char *argv[]) { inp_t input_params; mdl_t source_mdl; net_t network; int cell_index; int verbose = 1; char *input_file; /* Parse options and command line arguments. Diplay help message if no (or more than one) argument is given. */ { int opt; static struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'V'}, {"verbose", no_argument, NULL, 'v'}, {"quiet", no_argument, NULL, 'q'}, {0, 0, 0, 0} }; while ((opt = getopt_long (argc, argv, "hVvq", longopts, NULL)) != -1) { switch (opt) { case 'h': usage (); return EXIT_SUCCESS; break; case 'V': version (); return EXIT_SUCCESS; break; case 'v': verbose = 2; break; case 'q': verbose = 0; break; default: usage (); return EXIT_FAILURE; } }; argc -= optind; argv += optind; if (argc != 1) { usage (); return EXIT_FAILURE; } input_file = argv[0]; } /* Read the input file */ if( read_input_file_names (input_file, &input_params.files, verbose) != EXIT_SUCCESS ) { return EXIT_FAILURE; } /* Read the chemical network file */ if( read_network (input_params.files.chem_file, &network, verbose) != EXIT_SUCCESS ) { return EXIT_FAILURE; } /* Read the input file */ if( read_input (input_file, &input_params, &network, verbose) != EXIT_SUCCESS ) { return EXIT_FAILURE; } /* Read the source model file */ if( read_source (input_params.files.source_file, &source_mdl, &input_params, verbose) != EXIT_SUCCESS ) { return EXIT_FAILURE; } // Hdf5 files, datatype and dataspace hid_t fid, datatype, dataspace, dataset, tsDataset, tsDataspace, speciesDataset, speciesDataspace, speciesType; datatype = H5Tcopy(H5T_NATIVE_DOUBLE); hsize_t dimsf[ ROUTE_DATASET_RANK ]={ source_mdl.n_cells, source_mdl.ts.n_time_steps, input_params.output.n_output_species, N_OUTPUT_ROUTES }; fid = H5Fcreate( "astrochem_output.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); dataspace = H5Screate_simple( ABUNDANCE_DATASET_RANK, dimsf, NULL); // Add Atributes hid_t simpleDataspace = H5Screate(H5S_SCALAR); hid_t attrType = H5Tcopy(H5T_C_S1); H5Tset_size ( attrType, MAX_CHAR_FILENAME ); H5Tset_strpad(attrType,H5T_STR_NULLTERM); hid_t attrNetwork = H5Acreate( fid, "chem_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT); H5Awrite( attrNetwork, attrType, realpath( input_params.files.chem_file, NULL ) ); H5Aclose( attrNetwork ); hid_t attrModel = H5Acreate( fid, "source_file", attrType, simpleDataspace, H5P_DEFAULT, H5P_DEFAULT); H5Awrite( attrModel, attrType, realpath( input_params.files.source_file, NULL ) ); H5Aclose( attrModel ); H5Tclose( attrType ); H5Sclose( simpleDataspace ); // Define chunk property hsize_t chunk_dims[ ROUTE_DATASET_RANK ] = { 1, 1, input_params.output.n_output_species, N_OUTPUT_ROUTES }; hid_t prop_id = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk(prop_id, ABUNDANCE_DATASET_RANK , chunk_dims); // Create dataset dataset = H5Dcreate(fid, "Abundances", datatype, dataspace, H5P_DEFAULT, prop_id, H5P_DEFAULT); int i; hid_t dataspaceRoute, route_t_datatype, r_t_datatype, route_prop_id, routeGroup; hid_t routeDatasets[ input_params.output.n_output_species ]; if (input_params.output.trace_routes) { // Create route dataspace dataspaceRoute = H5Screate_simple( ROUTE_DATASET_RANK, dimsf, NULL); // Create route datatype r_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(r_t)); H5Tinsert( r_t_datatype, "reaction_number", HOFFSET(r_t, reaction_no ), H5T_NATIVE_INT); H5Tinsert( r_t_datatype, "reaction_rate", HOFFSET(r_t, rate), H5T_NATIVE_DOUBLE); route_t_datatype = H5Tcreate (H5T_COMPOUND, sizeof(rout_t)); H5Tinsert( route_t_datatype, "formation_rate", HOFFSET(rout_t, formation ), r_t_datatype ); H5Tinsert( route_t_datatype, "destruction_rate", HOFFSET(rout_t, destruction ), r_t_datatype ); // Define route chunk property route_prop_id = H5Pcreate(H5P_DATASET_CREATE); H5Pset_chunk( route_prop_id, ROUTE_DATASET_RANK, chunk_dims); // Create each named route dataset routeGroup = H5Gcreate( fid, "Routes", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ); char routeName[6] = "route_"; char tempName[ MAX_CHAR_SPECIES + sizeof( routeName ) ]; for( i = 0; i < input_params.output.n_output_species ; i++ ) { strcpy( tempName, routeName ); strcat( tempName, network.species[input_params.output.output_species_idx[i]].name ); routeDatasets[i] = H5Dcreate( routeGroup, tempName, route_t_datatype, dataspaceRoute, H5P_DEFAULT, route_prop_id, H5P_DEFAULT); } } // Timesteps and species hsize_t n_ts = source_mdl.ts.n_time_steps; hsize_t n_species = input_params.output.n_output_species ; tsDataspace = H5Screate_simple( 1, &n_ts, NULL); speciesDataspace = H5Screate_simple( 1, &n_species, NULL); speciesType = H5Tcopy (H5T_C_S1); H5Tset_size (speciesType, MAX_CHAR_SPECIES ); H5Tset_strpad(speciesType,H5T_STR_NULLTERM); // Create ts and species datasets tsDataset = H5Dcreate(fid, "TimeSteps", datatype, tsDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); speciesDataset = H5Dcreate(fid, "Species", speciesType, speciesDataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); double* convTs = (double*) malloc( sizeof(double)* source_mdl.ts.n_time_steps ); for( i=0; i< source_mdl.ts.n_time_steps; i++ ) { convTs[i] = source_mdl.ts.time_steps[i] / CONST_MKSA_YEAR; } H5Dwrite( tsDataset, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT, convTs ); char speciesName [ input_params.output.n_output_species ][ MAX_CHAR_SPECIES ]; for( i = 0; i < input_params.output.n_output_species ; i++ ) { strcpy( speciesName[i], network.species[input_params.output.output_species_idx[i]].name ); } H5Dwrite( speciesDataset, speciesType, H5S_ALL, H5S_ALL, H5P_DEFAULT, speciesName ); free( convTs ); H5Dclose( tsDataset ); H5Dclose( speciesDataset ); H5Tclose( speciesType ); H5Sclose( tsDataspace ); H5Sclose( speciesDataspace ); #ifdef HAVE_OPENMP /*Initialize lock*/ omp_init_lock(&lock); /* Solve the ODE system for each cell. */ { #pragma omp parallel for schedule (dynamic, 1) #endif for (cell_index = 0; cell_index < source_mdl.n_cells; cell_index++) { if (verbose >= 1) fprintf (stdout, "Computing abundances in cell %d...\n", cell_index); if( full_solve ( fid, dataset, routeDatasets, dataspace, dataspaceRoute, datatype, route_t_datatype, cell_index, &input_params, source_mdl.mode, &source_mdl.cell[cell_index], &network, &source_mdl.ts, verbose) != EXIT_SUCCESS ) { exit (EXIT_FAILURE); } if (verbose >= 1) fprintf (stdout, "Done with cell %d.\n", cell_index); } #ifdef HAVE_OPENMP } /*Finished lock mechanism, destroy it*/ omp_destroy_lock(&lock); #endif /* * Close/release hdf5 resources. */ if (input_params.output.trace_routes) { for( i = 0; i < input_params.output.n_output_species ; i++ ) { H5Dclose(routeDatasets[i] ); } H5Sclose(dataspaceRoute); H5Gclose(routeGroup); H5Pclose(route_prop_id); H5Tclose(r_t_datatype); H5Tclose(route_t_datatype); } H5Dclose(dataset); H5Pclose(prop_id); H5Sclose(dataspace); H5Tclose(datatype); H5Fclose(fid); free_input (&input_params); free_mdl (&source_mdl); free_network (&network); return (EXIT_SUCCESS); }