/* * Class: hdf_hdf5lib_H5 * Method: H5Aopen_idx * Signature: (JI)J */ JNIEXPORT jlong JNICALL Java_hdf_hdf5lib_H5__1H5Aopen_1idx (JNIEnv *env, jclass clss, jlong loc_id, jint idx) { hid_t attr_id = H5Aopen_idx((hid_t)loc_id, (unsigned int) idx); if (attr_id < 0) h5libraryError(env); return (jlong)attr_id; } /* end Java_hdf_hdf5lib_H5__1H5Aopen_1idx */
/*---------------------------------------------------------------------------- * Name: h5aopen_idx_c * Purpose: Call H5Aopen_idx to open an attribute * Inputs: obj_id - object identifier * idx - attribute index ( zero based) * Outputs: attr_id - attribute identifier * Returns: 0 on success, -1 on failure * Programmer: Elena Pourmal * Thursday, August 12, 1999 * Modifications: *---------------------------------------------------------------------------*/ int_f nh5aopen_idx_c (hid_t_f *obj_id, int_f *idx, hid_t_f *attr_id) { int_f ret_value=0; /* Return value */ /* * Call H5Aopen_idx function. */ if ((*attr_id = (hid_t_f)H5Aopen_idx((hid_t)*obj_id, (unsigned)*idx)) < 0) HGOTO_DONE(FAIL); done: return ret_value; }
char HBase::hasNamedAttr(const char * attrName) { hsize_t nattr = H5Aget_num_attrs(fObjectId); //std::cout<<"\n "<<fObjectPath<<" has "<<nattr<<" attrs\n"; hsize_t i; for(i = 0; i < nattr; i++) { hid_t aid = H5Aopen_idx(fObjectId, (unsigned int)i ); //std::cout<<getAttrName(aid)<<"\n"; if(getAttrName(aid) == attrName) { //std::cout<<"found "<<attrName; H5Aclose(aid); return 1; } H5Aclose(aid); } return 0; }
std::vector<std::string> list_attributes(hdf5dataset& hdataset) { H5O_info_t info; H5Oget_info(hdataset.handle(), &info); std::size_t num_attr = info.num_attrs; std::vector<std::string> result(num_attr); const int MAX_LEN = 256; char attr_name[MAX_LEN]; for(std::size_t i = 0; i < num_attr; ++i) { hid_t attribute_id = H5Aopen_idx(hdataset.handle(), i); H5Aget_name(attribute_id, MAX_LEN, attr_name); result[i] = std::string(attr_name); H5Aclose(attribute_id); } return result; }
int main() { printf("\n*** Checking HDF5 attribute functions some more.\n"); printf("*** Creating tst_xplatform2_3.nc with HDF only..."); { hid_t fapl_id, fcpl_id; size_t chunk_cache_size = MY_CHUNK_CACHE_SIZE; size_t chunk_cache_nelems = CHUNK_CACHE_NELEMS; float chunk_cache_preemption = CHUNK_CACHE_PREEMPTION; hid_t fileid, grpid, attid, spaceid; hid_t s1_typeid, vlen_typeid, s3_typeid; hid_t file_typeid1[NUM_OBJ], native_typeid1[NUM_OBJ]; hid_t file_typeid2, native_typeid2; hsize_t num_obj; H5O_info_t obj_info; char obj_name[STR_LEN + 1]; hsize_t dims[1] = {ATT_LEN}; /* netcdf attributes always 1-D. */ struct s1 { float x; double y; }; struct s3 { hvl_t data[NUM_VL]; }; /* cvc stands for "Compound with Vlen of Compound." */ struct s3 cvc_out[ATT_LEN]; int i, j, k; /* Create some output data: a struct s3 array (length ATT_LEN) * which holds an array of vlen (length NUM_VL) of struct s1. */ for (i = 0; i < ATT_LEN; i++) for (j = 0; j < NUM_VL; j++) { cvc_out[i].data[j].len = i + 1; if (!(cvc_out[i].data[j].p = calloc(sizeof(struct s1), cvc_out[i].data[j].len))) ERR; for (k = 0; k < cvc_out[i].data[j].len; k++) { ((struct s1 *)cvc_out[i].data[j].p)[k].x = 42.42; ((struct s1 *)cvc_out[i].data[j].p)[k].y = 2.0; } } /* Create the HDF5 file, with cache control, creation order, and * all the timmings. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if (H5Pset_fclose_degree(fapl_id, H5F_CLOSE_STRONG)) ERR; if (H5Pset_cache(fapl_id, 0, chunk_cache_nelems, chunk_cache_size, chunk_cache_preemption) < 0) ERR; if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR; if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; if (H5Pclose(fapl_id) < 0) ERR; if (H5Pclose(fcpl_id) < 0) ERR; /* Open the root group. */ if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; /* Create the compound type for struct s1. */ if ((s1_typeid = H5Tcreate(H5T_COMPOUND, sizeof(struct s1))) < 0) ERR; if (H5Tinsert(s1_typeid, X_NAME, offsetof(struct s1, x), H5T_NATIVE_FLOAT) < 0) ERR; if (H5Tinsert(s1_typeid, Y_NAME, offsetof(struct s1, y), H5T_NATIVE_DOUBLE) < 0) ERR; if (H5Tcommit(grpid, S1_TYPE_NAME, s1_typeid) < 0) ERR; /* Create a vlen type. Its a vlen of struct s1. */ if ((vlen_typeid = H5Tvlen_create(s1_typeid)) < 0) ERR; if (H5Tcommit(grpid, VLEN_TYPE_NAME, vlen_typeid) < 0) ERR; /* Create the struct s3 type, which contains the vlen. */ if ((s3_typeid = H5Tcreate(H5T_COMPOUND, sizeof(struct s3))) < 0) ERR; if (H5Tinsert(s3_typeid, VL_NAME, offsetof(struct s3, data), vlen_typeid) < 0) ERR; if (H5Tcommit(grpid, S3_TYPE_NAME, s3_typeid) < 0) ERR; /* Create an attribute of this new type. */ if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR; if ((attid = H5Acreate(grpid, S3_ATT_NAME, s3_typeid, spaceid, H5P_DEFAULT)) < 0) ERR; if (H5Awrite(attid, s3_typeid, cvc_out) < 0) ERR; /* Close the types. */ if (H5Tclose(s1_typeid) < 0 || H5Tclose(vlen_typeid) < 0 || H5Tclose(s3_typeid) < 0) ERR; /* Close the att. */ if (H5Aclose(attid) < 0) ERR; /* Close the space. */ if (H5Sclose(spaceid) < 0) ERR; /* Close the group and file. */ if (H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* Reopen the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, "/")) < 0) ERR; /* How many objects in this group? (There should be 3, the * types. Atts don't count as objects to HDF5.) */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; if (num_obj != NUM_OBJ) ERR; /* For each object in the group... */ for (i = 0; i < num_obj; i++) { /* Get the name, and make sure this is a type. */ if (H5Oget_info_by_idx(grpid, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, i, &obj_info, H5P_DEFAULT) < 0) ERR; if (H5Lget_name_by_idx(grpid, ".", H5_INDEX_NAME, H5_ITER_INC, i, obj_name, STR_LEN + 1, H5P_DEFAULT) < 0) ERR; if (obj_info.type != H5O_TYPE_NAMED_DATATYPE) ERR; /* Get the typeid and native typeid. */ if ((file_typeid1[i] = H5Topen2(grpid, obj_name, H5P_DEFAULT)) < 0) ERR; if ((native_typeid1[i] = H5Tget_native_type(file_typeid1[i], H5T_DIR_DEFAULT)) < 0) ERR; } /* There is one att: open it by index. */ if ((attid = H5Aopen_idx(grpid, 0)) < 0) ERR; /* Get file and native typeids of the att. */ if ((file_typeid2 = H5Aget_type(attid)) < 0) ERR; if ((native_typeid2 = H5Tget_native_type(file_typeid2, H5T_DIR_DEFAULT)) < 0) ERR; /* Close the attribute. */ if (H5Aclose(attid) < 0) ERR; /* Close the typeids. */ for (i = 0; i < NUM_OBJ; i++) { if (H5Tclose(file_typeid1[i]) < 0) ERR; if (H5Tclose(native_typeid1[i]) < 0) ERR; } if (H5Tclose(file_typeid2) < 0) ERR; if (H5Tclose(native_typeid2) < 0) ERR; /* Close the group and file. */ if (H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* Deallocate our vlens. */ for (i = 0; i < ATT_LEN; i++) for (j = 0; j < NUM_VL; j++) free(cvc_out[i].data[j].p); } SUMMARIZE_ERR; printf("*** Checking vlen of compound file..."); { #define NUM_OBJ_1 1 #define ATT_NAME "Poseidon" hid_t fapl_id, fcpl_id; hid_t fileid, grpid, attid, spaceid; hid_t vlen_typeid; hid_t file_typeid1[NUM_OBJ_1], native_typeid1[NUM_OBJ_1]; hid_t file_typeid2, native_typeid2; hsize_t num_obj; H5O_info_t obj_info; char obj_name[STR_LEN + 1]; hsize_t dims[1] = {ATT_LEN}; /* netcdf attributes always 1-D. */ /* vc stands for "Vlen of Compound." */ hvl_t vc_out[ATT_LEN]; int i, k; /* Create some output data: an array of vlen (length ATT_LEN) of * int. */ for (i = 0; i < ATT_LEN; i++) { vc_out[i].len = i + 1; if (!(vc_out[i].p = calloc(sizeof(int), vc_out[i].len))) ERR; for (k = 0; k < vc_out[i].len; k++) ((int *)vc_out[i].p)[k] = 42; } /* Create the HDF5 file with creation order. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; if (H5Pset_link_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; if (H5Pset_attr_creation_order(fcpl_id, (H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED)) < 0) ERR; if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; if (H5Pclose(fapl_id) < 0) ERR; if (H5Pclose(fcpl_id) < 0) ERR; /* Open the root group. */ if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; /* Create a vlen type. Its a vlen of int. */ if ((vlen_typeid = H5Tvlen_create(H5T_NATIVE_INT)) < 0) ERR; if (H5Tcommit(grpid, VLEN_TYPE_NAME, vlen_typeid) < 0) ERR; /* Create an attribute of this new type. */ if ((spaceid = H5Screate_simple(1, dims, NULL)) < 0) ERR; if ((attid = H5Acreate(grpid, ATT_NAME, vlen_typeid, spaceid, H5P_DEFAULT)) < 0) ERR; if (H5Awrite(attid, vlen_typeid, vc_out) < 0) ERR; /* Close the type. */ if (H5Tclose(vlen_typeid) < 0) ERR; /* Close the att. */ if (H5Aclose(attid) < 0) ERR; /* Close the space. */ if (H5Sclose(spaceid) < 0) ERR; /* Close the group and file. */ if (H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* Reopen the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, "/")) < 0) ERR; /* How many objects in this group? (There should be 2, the * types. Atts don't count as objects to HDF5.) */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; if (num_obj != NUM_OBJ_1) ERR; /* For each object in the group... */ for (i = 0; i < num_obj; i++) { /* Get the name, and make sure this is a type. */ if (H5Oget_info_by_idx(grpid, ".", H5_INDEX_CRT_ORDER, H5_ITER_INC, i, &obj_info, H5P_DEFAULT) < 0) ERR; if (H5Lget_name_by_idx(grpid, ".", H5_INDEX_NAME, H5_ITER_INC, i, obj_name, STR_LEN + 1, H5P_DEFAULT) < 0) ERR; if (obj_info.type != H5O_TYPE_NAMED_DATATYPE) ERR; /* Get the typeid and native typeid. */ if ((file_typeid1[i] = H5Topen2(grpid, obj_name, H5P_DEFAULT)) < 0) ERR; if ((native_typeid1[i] = H5Tget_native_type(file_typeid1[i], H5T_DIR_DEFAULT)) < 0) ERR; } /* There is one att: open it by index. */ if ((attid = H5Aopen_idx(grpid, 0)) < 0) ERR; /* Get file and native typeids of the att. */ if ((file_typeid2 = H5Aget_type(attid)) < 0) ERR; if ((native_typeid2 = H5Tget_native_type(file_typeid2, H5T_DIR_DEFAULT)) < 0) ERR; /* Close the attribute. */ if (H5Aclose(attid) < 0) ERR; /* Close the typeids. */ for (i = 0; i < NUM_OBJ_1; i++) { if (H5Tclose(file_typeid1[i]) < 0) ERR; if (H5Tclose(native_typeid1[i]) < 0) ERR; } if (H5Tclose(file_typeid2) < 0) ERR; if (H5Tclose(native_typeid2) < 0) ERR; /* Close the group and file. */ if (H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* Deallocate our vlens. */ for (i = 0; i < ATT_LEN; i++) free(vc_out[i].p); } SUMMARIZE_ERR; FINAL_RESULTS; }
void read_amuse_hdf5_header(char *filename, int *npart, int *ncol, int *ndim, int *ndimV, double *time, int *ierr) { hid_t file_id; hid_t group_id, group_id1, group_id2; hid_t attrib_id; herr_t status; herr_t HDF5_error = -1; *ierr = 0; *ndim = 0; *ndimV = 0; if (debug) printf("DEBUG: opening %s \n",filename); file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); if (file_id == HDF5_error) { printf("ERROR opening %s \n",filename); *ierr = 1; return; } char *maingroup = "particles"; /* * Open the "particles" dataset and read the number of particles attribute * */ if (!checkfordataset(file_id,maingroup)) { printf(" ERROR: \"%s\" dataset not found in AMUSE HDF5 file\n",maingroup); *ierr = 2; return; } #if H5_VERSION_GE(1,8,0) group_id1 = H5Gopen2(file_id,maingroup,H5P_DEFAULT); #else group_id1 = H5Gopen(file_id,maingroup); #endif if (group_id1 == HDF5_error) { printf("ERROR opening %s data set \n",maingroup); *ierr = 2; return; } #if H5_VERSION_GE(1,8,0) group_id = H5Gopen2(group_id1,"0000000001",H5P_DEFAULT); #else group_id = H5Gopen(group_id1,"0000000001"); #endif if (group_id == HDF5_error) { printf("ERROR opening 00000000001 data set \n"); *ierr = 2; return; } int nattrib; int i; char name[256]; nattrib = H5Aget_num_attrs(group_id); if (debug) printf("number of attributes found = %i\n",nattrib); /* * Read through all of the attributes in the header, so we * can still spit out the values even if they are not used by SPLASH */ for(i=0; i < nattrib; i++) { attrib_id = H5Aopen_idx(group_id,i); ssize_t attr_status; attr_status = H5Aget_name(attrib_id, 256, name); hid_t type_id; type_id = H5Aget_type(attrib_id); /*type_class = H5Tget_native_type(type_id,H5T_DIR_ASCEND);*/ if (strcmp(name,"time")==0) { status = H5Aread(attrib_id,H5T_NATIVE_DOUBLE,time); } else if (strcmp(name,"number_of_particles")==0) { status = H5Aread(attrib_id,H5T_NATIVE_INT,npart); } else { if (debug) printf("DEBUG: unknown attribute %s \n",name); } if (status==HDF5_error) { printf(" ERROR reading attribute %s \n",name); } status = H5Aclose(attrib_id); } /* * Now we need to get the number of data columns in the file * (from the number of datasets in the "attributes" group) */ #if H5_VERSION_GE(1,8,0) group_id2 = H5Gopen2(group_id,"attributes",H5P_DEFAULT); #else group_id2 = H5Gopen(group_id,"attributes"); #endif if (group_id2 == HDF5_error) { printf("ERROR opening %s data set \n","attributes"); *ierr = 2; return; } hsize_t ndatasets; status = H5Gget_num_objs(group_id2, &ndatasets); if (debug) printf("DEBUG: number of datasets = %i \n",(int)ndatasets); *ncol = (int)ndatasets; int idim; /* check that coordinates are present in file */ idim = get_rank_by_name(group_id2,"x"); if (idim <= 0) { printf("ERROR: x positions not found\n"); *ierr = 3; } idim = get_rank_by_name(group_id2,"y"); if (idim <= 0) { printf("ERROR: y positions not found\n"); *ierr = 3; } idim = get_rank_by_name(group_id2,"z"); if (idim <= 0) { printf("z positions not found, assuming file is 2D \n"); *ndim = 2; *ndimV = 2; } else { *ndim = 3; *ndimV = 3; } /* finish, close all open datasets and close file */ status = H5Gclose(group_id2); if (status == HDF5_error) { printf("ERROR closing attributes data set \n"); *ierr = 3; return; } status = H5Gclose(group_id); if (status == HDF5_error) { printf("ERROR closing %s data set \n",maingroup); *ierr = 3; return; } status = H5Gclose(group_id1); if (status == HDF5_error) { printf("ERROR closing 0001 data set \n"); *ierr = 3; return; } status = H5Fclose( file_id ); if (status == HDF5_error) { printf("ERROR closing file \n"); *ierr = 7; } if (debug) printf("DEBUG: finished header read \n"); }
void rdiff(const char *name, hid_t f1, hid_t f2) { hid_t g1 = H5Gopen(f1, name, H5P_DEFAULT); hid_t g2 = H5Gopen(f2, name, H5P_DEFAULT); if (g1 >= 0 && g2 >= 0) { int n1 = H5Aget_num_attrs(g1); for (int i = 0; i < n1; i++) { char aname[MAXNAME]; hid_t a1 = H5Aopen_idx(g1, i); assert(H5Aget_name(a1, MAXNAME, aname) < MAXNAME); H5Aclose(a1); if (!H5LTfind_attribute(g2, aname)) { printf("Only in %s[%s%s]\n", file1, name, aname); continue; } int d1, d2; H5LTget_attribute_ndims(f1, name, aname, &d1); H5LTget_attribute_ndims(f2, name, aname, &d2); assert(d1 <= 1 && d2 <= 1); hsize_t dims1, dims2; H5T_class_t t1, t2; size_t ts1, ts2; H5LTget_attribute_info(f1, name, aname, &dims1, &t1, &ts1); H5LTget_attribute_info(f2, name, aname, &dims2, &t2, &ts2); assert(t1 == t2); assert(t1 == H5T_INTEGER || t1 == H5T_FLOAT || t1 == H5T_STRING); if (t1 == H5T_INTEGER) { assert(d1==0 || (dims1 == 1 && dims2 == 1)); assert(ts1 == 4 && ts2 == 4); int v1, v2; H5LTget_attribute_int(f1, name, aname, &v1); H5LTget_attribute_int(f2, name, aname, &v2); if (v1 != v2) { printf("%s[%s%s]=%d %s[%s%s]=%d\n", file1, name, aname, v1, file2, name, aname, v2); } } if (t1 == H5T_FLOAT) { assert(d1==0 || (dims1 == 1 && dims2 == 1)); assert(ts1 == 4 && ts2 == 4); float v1, v2; H5LTget_attribute_float(f1, name, aname, &v1); H5LTget_attribute_float(f2, name, aname, &v2); if (v1 != v2) { printf("%s[%s%s]=%g %s[%s%s]=%g\n", file1, name, aname, v1, file2, name, aname, v2); } } if (t1 == H5T_STRING) { assert(ts1 < 256 && ts2 < 256); char buf1[256]; char buf2[256]; H5LTget_attribute_string(f1, name, aname, buf1); H5LTget_attribute_string(f2, name, aname, buf2); if (strcmp(buf1, buf2)) { printf("%s[%s%s]=%s %s[%s%s]=%s\n", file1, name, aname, buf1, file2, name, aname, buf2); } } } int n2 = H5Aget_num_attrs(g2); for (int i = 0; i < n2; i++) { char aname[MAXNAME]; hid_t a2 = H5Aopen_idx(g2, i); assert(H5Aget_name(a2, MAXNAME, aname) < MAXNAME); H5Aclose(a2); if (!H5LTfind_attribute(g1, aname)) { printf("Only in %s[%s%s]\n", file2, name, aname); continue; } } hsize_t nobj; H5Gget_num_objs(g1, &nobj); for (int i = 0; i < nobj; i++) { char oname[MAXNAME]; assert(H5Gget_objname_by_idx(g1, i, oname, MAXNAME) < MAXNAME); int otype = H5Gget_objtype_by_idx(g1, i); assert(otype == H5G_DATASET); if (!H5LTfind_dataset(g2, oname)) { printf("Only in %s[%s%s]\n", file1, name, oname); continue; } hsize_t dims1[2], dims2[2]; H5T_class_t t1, t2; size_t ts1, ts2; H5LTget_dataset_info(g1, oname, dims1, &t1, &ts1); H5LTget_dataset_info(g2, oname, dims2, &t2, &ts2); if (dims1[0] != dims2[0] || dims1[1] != dims2[1]) { printf("%s[%s%s](%d,%d) != %s[%s%s](%d,%d)\n", file1, name, oname, dims1[1], dims1[0], file2, name, oname, dims2[1], dims2[0]); continue; } float *data1 = malloc(dims1[0]*dims1[1]*sizeof(float)); float *data2 = malloc(dims1[0]*dims1[1]*sizeof(float)); H5LTread_dataset_float(g1, oname, data1); H5LTread_dataset_float(g2, oname, data2); float maxdiff = 0; for (int i = dims1[0]*dims1[1]-1; i >= 0; i--) { float d = data1[i] - data2[i]; if (d < 0) d = -d; if (d > maxdiff) maxdiff = d; } printf("max |%s[%s%s] - %s[%s%s]| = %g\n", file1, name, oname, file2, name, oname, maxdiff); free(data1); free(data2); } H5Gget_num_objs(g2, &nobj); for (int i = 0; i < nobj; i++) { char oname[MAXNAME]; assert(H5Gget_objname_by_idx(g2, i, oname, MAXNAME) < MAXNAME); int otype = H5Gget_objtype_by_idx(g2, i); assert(otype == H5G_DATASET); if (!H5LTfind_dataset(g1, oname)) { printf("Only in %s[%s%s]\n", file2, name, oname); continue; } } H5Gclose(g1); H5Gclose(g2); } else if (g1 >= 0) { printf("Only in %s:%s\n", file1, name); H5Gclose(g1); } else if (g2 >= 0) { printf("Only in %s:%s\n", file2, name); H5Gclose(g2); } else { printf("Group %s does not exist in either file.\n", name); } }
/* ****************************************************************************************************************************** */ int main(int argc, char *argv[]) { hid_t fileID, dataSetID; herr_t hErrVal; int i; hsize_t dims[1024], maxDims[1024]; H5T_class_t class; char classStr[32]; hid_t dataTypeID; size_t dataSize; H5T_order_t order; int rank; /* Note this is an int, not an hssize_t */ int intVal; hid_t dataSpaceID; hid_t rootGroupID; hsize_t numInRootGrp, firstDataSetIdx, foundFirstDataSet; char attrName[1024], firstDataSetName[1024]; ssize_t objectNameSize, attrNameSize; H5G_stat_t objectStatInfo; int numAttrs; int curAttrIdx; hid_t attrID; hsize_t numDataPoints; unsigned majnum, minnum, relnum; /* Load the library -- not required most platforms. */ hErrVal = H5open(); mjrHDF5_chkError(hErrVal); /* Get the library version */ hErrVal = H5get_libversion(&majnum, &minnum, &relnum); mjrHDF5_chkError(hErrVal); printf("Lib Version: v%lu.%lur%lu\n", (unsigned long)majnum, (unsigned long)minnum, (unsigned long)relnum); /* Open an existing file. */ fileID = H5Fopen(TST_FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT); mjrHDF5_chkError(fileID); /* Get the ID for the "root" group -- every HDF5 has one */ rootGroupID = H5Gopen(fileID, "/", H5P_DEFAULT); mjrHDF5_chkError(rootGroupID); /* Get the number of objects in the root group. */ hErrVal = H5Gget_num_objs(rootGroupID, &numInRootGrp); mjrHDF5_chkError(hErrVal); printf("The root group contains %lu object%c\n", (unsigned long)numInRootGrp, (numInRootGrp==1?' ':'s')); if(numInRootGrp < 1) { printf("As the file contains NO objects, I have nothing left to do...\n"); exit(1); } /* end if */ /* Find the first dataset in the root group. */ for(foundFirstDataSet=0,firstDataSetIdx=0; (!foundFirstDataSet)&&(firstDataSetIdx<numInRootGrp); firstDataSetIdx++) { /* Get object name from the index. */ objectNameSize = H5Gget_objname_by_idx(rootGroupID, firstDataSetIdx, firstDataSetName, 1024); mjrHDF5_chkError(objectNameSize); if(objectNameSize == 0) { /* Need to check for zero return too */ printf("ERROR: Object with index %lu doesn't exist in root group!\n", (unsigned long)firstDataSetIdx); exit(1); } /* end if */ /* Now use the object name to get info about the object... */ hErrVal = H5Gget_objinfo(rootGroupID, firstDataSetName, 0, &objectStatInfo); mjrHDF5_chkError(hErrVal); /* If the object is a dataset, then print out some info. */ if(objectStatInfo.type == H5G_DATASET) { printf("Object %luth (%s) is a dataset!\n", (unsigned long)firstDataSetIdx, firstDataSetName); printf("The name of the %luth object of the root group is: %s\n", (unsigned long)firstDataSetIdx, firstDataSetName); foundFirstDataSet = 1; printf("Info for the %s dataset:\n", firstDataSetName); printf(" Modify time: %lu\n", (unsigned long)objectStatInfo.mtime); printf(" Type: %lu\n", (unsigned long)objectStatInfo.type); printf(" Link count: %lu\n", (unsigned long)objectStatInfo.nlink); } /* end if */ } /* end for */ /* Note: At this point index of the dataset will be: firstDataSetIdx-- */ if(!foundFirstDataSet) { printf("ERROR: Could not find a dataset in the root group\n"); exit(1); } /* end if */ /* Open the dataset we found -- we know it exists. */ dataSetID = H5Dopen(rootGroupID, firstDataSetName, H5P_DEFAULT); mjrHDF5_chkError(dataSetID); /* ****************************************************************************************************************************** */ /* Get some info regarding the TYPE of the dataset. */ dataTypeID = H5Dget_type(dataSetID); mjrHDF5_chkError(dataTypeID); /* Get the class of the data */ class = H5Tget_class(dataTypeID); mjrHDF5_Tclass2str(class, classStr); printf(" Object class: %s\n", classStr); /* Get the size of the type */ dataSize = H5Tget_size(dataTypeID); if(dataSize == 0) { printf("ERROR: Failure in H5Tget_size().\n"); exit(1); } /* end if */ printf(" Size of data type: %lu\n", (unsigned long)dataSize); /* Get the byte order */ order = H5Tget_order(dataTypeID); printf(" Byte Order: "); switch(order) { case H5T_ORDER_ERROR : printf("ERROR\n"); break; case H5T_ORDER_LE : printf("Little Endian\n"); break; case H5T_ORDER_BE : printf("Big Endian\n"); break; case H5T_ORDER_VAX : printf("VAX mixed endian\n"); break; case H5T_ORDER_MIXED : printf("Mixed endian\n"); break; case H5T_ORDER_NONE : printf("particular order\n"); break; } /* end switch */ /* We are done with the datatype. */ hErrVal = H5Tclose(dataTypeID); mjrHDF5_chkError(hErrVal); /* ****************************************************************************************************************************** */ /* Figure out the size of the dataset. */ dataSpaceID = H5Dget_space(dataSetID); mjrHDF5_chkError(dataSpaceID); /* Get the number of dims. */ rank = H5Sget_simple_extent_ndims(dataSpaceID); mjrHDF5_chkError(rank); if(rank > 1024) { /* This can't really happen (limit is 32) */ printf("ERROR: rank too large.\n"); exit(1); } /* end if */ /* Get the size of each dim. */ intVal = H5Sget_simple_extent_dims(dataSpaceID, dims, maxDims); mjrHDF5_chkError(intVal); printf(" Dataspace Rank %lu\n", (unsigned long)rank); printf(" Dim Lengths: "); for(i=0; i<rank; i++) if(dims[i] == H5S_UNLIMITED) { printf("%s ", "UNLIMITED"); } else { printf("%ld ", (long)(dims[i])); } /* end if/else */ printf("\n"); printf(" Max Dim Lengths: "); for(i=0; i<rank; i++) if(maxDims[i] == H5S_UNLIMITED) { printf("%s ", "UNLIMITED"); } else { printf("%ld ", (long)(maxDims[i])); } /* end if/else */ printf("\n"); numDataPoints = H5Sget_simple_extent_npoints(dataSpaceID); if(numDataPoints == 0) { printf("ERROR: Call to H5Sget_simple_extent_npoints failed.\n"); exit(1); } /* end if */ printf("Number of data points: %lu\n", (unsigned long)numDataPoints); /* We are done with the dataSpaceID */ hErrVal = H5Sclose(dataSpaceID); mjrHDF5_chkError(hErrVal); /* Get the number of attributes for the dataSet. */ numAttrs = H5Aget_num_attrs(dataSetID); mjrHDF5_chkError(numAttrs); printf(" Number of attrs: %lu\n", (unsigned long)numAttrs); /* If we have any attributes, we get info for them */ if(numAttrs > 0) { printf(" Attribute info:\n"); for(curAttrIdx=0; curAttrIdx<numAttrs; curAttrIdx++) { attrID = H5Aopen_idx(dataSetID, curAttrIdx); mjrHDF5_chkError(attrID); attrNameSize = H5Aget_name(attrID, 1024, attrName); mjrHDF5_chkError(attrNameSize); printf(" Number %3lu: ", (unsigned long)curAttrIdx); dataTypeID = H5Aget_type(attrID); mjrHDF5_chkError(dataTypeID); /* Get the class for the type. */ class = H5Tget_class(dataTypeID); mjrHDF5_Tclass2str(class, classStr); printf(" Class: %-16s", classStr); /* Get the size of the type */ dataSize = H5Tget_size(dataTypeID); if(dataSize == 0) { printf("ERROR: Failure in H5Tget_size().\n"); exit(1); } /* end if */ printf(" Size: %3lu ", (unsigned long)dataSize); hErrVal = H5Tclose(dataTypeID); mjrHDF5_chkError(hErrVal); printf(" Name: %s \n", attrName); hErrVal = H5Aclose(attrID); mjrHDF5_chkError(hErrVal); } /* end for */ } /* end if */