void pyne::Material::_load_comp_protocol1(hid_t db, std::string datapath, int row) { std::string nucpath; hid_t data_set = H5Dopen2(db, datapath.c_str(), H5P_DEFAULT); hsize_t data_offset[1] = {row}; if (row < 0) { // Handle negative row indices hid_t data_space = H5Dget_space(data_set); hsize_t data_dims[1]; H5Sget_simple_extent_dims(data_space, data_dims, NULL); data_offset[0] += data_dims[0]; }; // Grab the nucpath hid_t nuc_attr = H5Aopen(data_set, "nucpath", H5P_DEFAULT); H5A_info_t nuc_info; H5Aget_info(nuc_attr, &nuc_info); hsize_t nuc_attr_len = nuc_info.data_size; hid_t str_attr = H5Tcopy(H5T_C_S1); H5Tset_size(str_attr, nuc_attr_len); char * nucpathbuf = new char [nuc_attr_len]; H5Aread(nuc_attr, str_attr, nucpathbuf); nucpath = std::string(nucpathbuf, nuc_attr_len); delete[] nucpathbuf; // Grab the nuclides std::vector<int> nuclides = h5wrap::h5_array_to_cpp_vector_1d<int>(db, nucpath, H5T_NATIVE_INT); int nuc_size = nuclides.size(); hsize_t nuc_dims[1] = {nuc_size}; // Get the data hyperslab hid_t data_hyperslab = H5Dget_space(data_set); hsize_t data_count[1] = {1}; H5Sselect_hyperslab(data_hyperslab, H5S_SELECT_SET, data_offset, NULL, data_count, NULL); // Get memory space for writing hid_t mem_space = H5Screate_simple(1, data_count, NULL); // Get material type size_t material_struct_size = sizeof(pyne::material_struct) + sizeof(double)*nuc_size; hid_t desc = H5Tcreate(H5T_COMPOUND, material_struct_size); hid_t comp_values_array_type = H5Tarray_create2(H5T_NATIVE_DOUBLE, 1, nuc_dims); // make the data table type H5Tinsert(desc, "mass", HOFFSET(pyne::material_struct, mass), H5T_NATIVE_DOUBLE); H5Tinsert(desc, "density", HOFFSET(pyne::material_struct, density), H5T_NATIVE_DOUBLE); H5Tinsert(desc, "atoms_per_molecule", HOFFSET(pyne::material_struct, atoms_per_mol), H5T_NATIVE_DOUBLE); H5Tinsert(desc, "comp", HOFFSET(pyne::material_struct, comp), comp_values_array_type); // make the data array, have to over-allocate material_struct * mat_data = new material_struct [material_struct_size]; // Finally, get data and put in on this instance H5Dread(data_set, desc, mem_space, data_hyperslab, H5P_DEFAULT, mat_data); mass = (*mat_data).mass; density = (*mat_data).density; atoms_per_molecule = (*mat_data).atoms_per_mol; for (int i = 0; i < nuc_size; i++) comp[nuclides[i]] = (double) (*mat_data).comp[i]; delete[] mat_data; H5Tclose(str_attr); // // Get metadata from associated dataset, if available // std::string attrpath = datapath + "_metadata"; bool attrpath_exists = h5wrap::path_exists(db, attrpath); if (!attrpath_exists) return; hid_t metadatapace, attrtype, metadataet, metadatalab, attrmemspace; int attrrank; hvl_t attrdata [1]; attrtype = H5Tvlen_create(H5T_NATIVE_CHAR); // Get the metadata from the file metadataet = H5Dopen2(db, attrpath.c_str(), H5P_DEFAULT); metadatalab = H5Dget_space(metadataet); H5Sselect_hyperslab(metadatalab, H5S_SELECT_SET, data_offset, NULL, data_count, NULL); attrmemspace = H5Screate_simple(1, data_count, NULL); H5Dread(metadataet, attrtype, attrmemspace, metadatalab, H5P_DEFAULT, attrdata); // convert to in-memory JSON Json::Reader reader; reader.parse((char *) attrdata[0].p, (char *) attrdata[0].p+attrdata[0].len, metadata, false); // close attr data objects H5Fflush(db, H5F_SCOPE_GLOBAL); H5Dclose(metadataet); H5Sclose(metadatapace); H5Tclose(attrtype); // Close out the HDF5 file H5Fclose(db); };
static estatus_t e5_merge_flash_scalars( eid_t e5_file_id, hid_t f5_file_id, const char* f5_list_name, f5_scalar_list_type f5_list_type) { size_t s; hsize_t dimens_1d; hsize_t maxdimens_1d; hid_t string_type; hsize_t f5_scalar_bytes; hid_t f5_dataspace; hid_t f5_memspace; hid_t f5_dataset; hid_t f5_type; void* f5_data; hid_t e5_em_group_id; int hstatus; size_t e5_scalar_bytes; estatus_t estatus = E5_SUCCESS; string_type = H5Tcopy(H5T_C_S1); H5Tset_size(string_type, F5_MAX_STRING_LENGTH); f5_dataset = H5Dopen(f5_file_id, f5_list_name); if (f5_dataset < 0) { estatus = E5_INVALID_DATASET; e5_error(f5_file_id, estatus, "Failed to open dataset '%s'\n", f5_list_name); return estatus; } f5_dataspace = H5Dget_space(f5_dataset); H5Sget_simple_extent_dims(f5_dataspace, &dimens_1d, &maxdimens_1d); e5_em_group_id = e5_open_group(e5_file_id, E5_EMISSIVITY_GROUP_NAME_ABS); switch(f5_list_type) { case F5_SCALAR_LIST_INTEGER: case F5_SCALAR_LIST_LOGICAL: { hid_t f5_int_list_type; f5_int_list_t *f5_int_list; f5_scalar_bytes = dimens_1d * sizeof(f5_int_list_t); f5_int_list = (f5_int_list_t *) e5_malloc(f5_scalar_bytes); if(!f5_int_list) { estatus = E5_OUT_OF_MEMORY; e5_error(f5_file_id, estatus, "Failed to allocate memory for reading '%s' from F5 data file\n", f5_list_name); break; } memset(f5_int_list, 0, f5_scalar_bytes); f5_int_list_type = H5Tcreate(H5T_COMPOUND, sizeof(f5_int_list_t)); H5Tinsert(f5_int_list_type, "name", HOFFSET(f5_int_list_t, name), string_type); H5Tinsert(f5_int_list_type, "value", HOFFSET(f5_int_list_t, value), H5T_NATIVE_INT); f5_memspace = H5Screate_simple(1, &dimens_1d, NULL); hstatus = H5Dread(f5_dataset, f5_int_list_type, f5_memspace, f5_dataspace, H5P_DEFAULT, f5_int_list); if (hstatus < 0) { estatus = E5_READ_FAILURE; e5_error(f5_file_id, estatus, "Failed to read '%s' dataset from data file\n", f5_list_name); break; } f5_data = f5_int_list; f5_type = f5_int_list_type; if(f5_data) { // Add the scalar list as attributes for the top-level Emissivity group e5_scalar_bytes = (dimens_1d + 1) * sizeof(e5_mutable_attr_int); e5_mutable_attr_int* e5_int_scalars = (e5_mutable_attr_int*) e5_malloc(e5_scalar_bytes); memset(e5_int_scalars, 0, e5_scalar_bytes); for(s = 0; s < dimens_1d; s++) { e5_int_scalars[s].value = f5_int_list[s].value; e5_int_scalars[s].name = e5_trim(f5_int_list[s].name, E5_TRUE, E5_TRUE, F5_MAX_STRING_LENGTH); } e5_write_attr_list_int(e5_em_group_id, (e5_attr_int*)e5_int_scalars); e5_free(e5_int_scalars); } break; } case F5_SCALAR_LIST_REAL: { hid_t f5_real_list_type; f5_real_list_t *f5_real_list; f5_scalar_bytes = dimens_1d * sizeof(f5_real_list_t); f5_real_list = (f5_real_list_t *) e5_malloc(f5_scalar_bytes); if(!f5_real_list) { estatus = E5_OUT_OF_MEMORY; e5_error(f5_file_id, estatus, "Failed to allocate memory for reading '%s' from F5 data file\n", f5_list_name); break; } memset(f5_real_list, 0, f5_scalar_bytes); f5_real_list_type = H5Tcreate(H5T_COMPOUND, sizeof(f5_real_list_t)); H5Tinsert(f5_real_list_type, "name", HOFFSET(f5_real_list_t, name), string_type); H5Tinsert(f5_real_list_type, "value", HOFFSET(f5_real_list_t, value), H5T_NATIVE_DOUBLE); f5_memspace = H5Screate_simple(1, &dimens_1d, NULL); hstatus = H5Dread(f5_dataset, f5_real_list_type, f5_memspace, f5_dataspace, H5P_DEFAULT, f5_real_list); if (hstatus < 0) { estatus = E5_READ_FAILURE; e5_error(f5_file_id, estatus, "Failed to read '%s' from F5 data file\n", f5_list_name); break; } f5_data = f5_real_list; f5_type = f5_real_list_type; if(f5_data) { // Add the scalar list as attributes for the top-level Emissivity group e5_scalar_bytes = (dimens_1d + 1) * sizeof(e5_mutable_attr_double); e5_mutable_attr_double* e5_double_scalars = (e5_mutable_attr_double*) e5_malloc(e5_scalar_bytes); memset(e5_double_scalars, 0, e5_scalar_bytes); for(s = 0; s < dimens_1d; s++) { e5_double_scalars[s].value = f5_real_list[s].value; e5_double_scalars[s].name = f5_real_list[s].name; e5_double_scalars[s].name = e5_trim(f5_real_list[s].name, E5_TRUE, E5_TRUE, F5_MAX_STRING_LENGTH); } e5_write_attr_list_double(e5_em_group_id, (e5_attr_double*)e5_double_scalars); e5_free(e5_double_scalars); } break; } case F5_SCALAR_LIST_STRING: { hid_t f5_str_list_type; f5_str_list_t *f5_str_list; f5_scalar_bytes = dimens_1d * sizeof(f5_str_list_t); f5_str_list = (f5_str_list_t *) e5_malloc(f5_scalar_bytes); if(!f5_str_list) { estatus = E5_OUT_OF_MEMORY; e5_error(f5_file_id, estatus, "Failed to allocate memory for reading '%s' from F5 data file\n", f5_list_name); break; } memset(f5_str_list, 0, f5_scalar_bytes); f5_str_list_type = H5Tcreate(H5T_COMPOUND, sizeof(f5_str_list_t)); H5Tinsert(f5_str_list_type, "name", HOFFSET(f5_str_list_t, name), string_type); H5Tinsert(f5_str_list_type, "value", HOFFSET(f5_str_list_t, value), string_type); f5_memspace = H5Screate_simple(1, &dimens_1d, NULL); hstatus = H5Dread(f5_dataset, f5_str_list_type, f5_memspace, f5_dataspace, H5P_DEFAULT, f5_str_list); if (hstatus < 0) { estatus = E5_READ_FAILURE; e5_error(f5_file_id, estatus, "Failed to read '%s' from F5 data file\n", f5_list_name); break; } f5_data = f5_str_list; f5_type = f5_str_list_type; if(f5_data) { // Add the scalar list as attributes for the top-level Emissivity group e5_scalar_bytes = (dimens_1d + 1) * sizeof(e5_mutable_attr_str); e5_mutable_attr_str* e5_str_scalars = (e5_mutable_attr_str*) e5_malloc(e5_scalar_bytes); memset(e5_str_scalars, 0, e5_scalar_bytes); for(s = 0; s < dimens_1d; s++) { char* trimmed = e5_trim(f5_str_list[s].value, E5_TRUE, E5_TRUE, E5_MAX_ATTR_STRING_LENGTH); strncpy( e5_str_scalars[s].value, trimmed, E5_MAX_ATTR_STRING_LENGTH); e5_str_scalars[s].name = e5_trim(f5_str_list[s].name, E5_TRUE, E5_TRUE, F5_MAX_STRING_LENGTH); } e5_write_attr_list_str(e5_em_group_id, (e5_attr_str*)e5_str_scalars); e5_free(e5_str_scalars); } break; } default: { estatus = E5_INVALID_DATASET; e5_error(f5_file_id, estatus, "Unknown scalar list requested '%s' from F5 data file\n", f5_list_name); break; } }; H5Tclose(f5_type); H5Sclose(f5_memspace); H5Sclose(f5_dataspace); H5Dclose(f5_dataset); e5_free(f5_data); e5_close_group(e5_em_group_id); return estatus; }
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; }
/* * test_objnames * Tests that UTF-8 can be used for object names in the file. * Tests groups, datasets, named datatypes, and soft links. * Note that this test doesn't actually mark the names as being * in UTF-8. At the time this test was written, that feature * didn't exist in HDF5, and when the character encoding property * was added to links it didn't change how they were stored in the file, * -JML 2/2/2006 */ void test_objnames(hid_t fid, const char* string) { hid_t grp_id, grp1_id, grp2_id, grp3_id; hid_t type_id, dset_id, space_id; char read_buf[MAX_STRING_LENGTH]; char path_buf[MAX_PATH_LENGTH]; hsize_t dims=1; hobj_ref_t obj_ref; herr_t ret; /* Create a group with a UTF-8 name */ grp_id = H5Gcreate2(fid, string, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(grp_id, FAIL, "H5Gcreate2"); /* Set a comment on the group to test that we can access the group * Also test that UTF-8 comments can be read. */ ret = H5Oset_comment_by_name(fid, string, string, H5P_DEFAULT); CHECK(ret, FAIL, "H5Oset_comment_by_name"); ret = H5Oget_comment_by_name(fid, string, read_buf, (size_t)MAX_STRING_LENGTH, H5P_DEFAULT); CHECK(ret, FAIL, "H5Oget_comment_by_name"); ret = H5Gclose(grp_id); CHECK(ret, FAIL, "H5Gclose"); VERIFY(HDstrcmp(string, read_buf), 0, "strcmp"); /* Create a new dataset with a UTF-8 name */ grp1_id = H5Gcreate2(fid, GROUP1_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(grp1_id, FAIL, "H5Gcreate2"); space_id = H5Screate_simple(RANK, &dims, NULL); CHECK(space_id, FAIL, "H5Screate_simple"); dset_id = H5Dcreate2(grp1_id, string, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(dset_id, FAIL, "H5Dcreate2"); /* Make sure that dataset can be opened again */ ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); ret = H5Sclose(space_id); CHECK(ret, FAIL, "H5Sclose"); dset_id = H5Dopen2(grp1_id, string, H5P_DEFAULT); CHECK(ret, FAIL, "H5Dopen2"); ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); ret = H5Gclose(grp1_id); CHECK(ret, FAIL, "H5Gclose"); /* Do the same for a named datatype */ grp2_id = H5Gcreate2(fid, GROUP2_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(grp2_id, FAIL, "H5Gcreate2"); type_id = H5Tcreate(H5T_OPAQUE, (size_t)1); CHECK(type_id, FAIL, "H5Tcreate"); ret = H5Tcommit2(grp2_id, string, type_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(type_id, FAIL, "H5Tcommit2"); ret = H5Tclose(type_id); CHECK(type_id, FAIL, "H5Tclose"); type_id = H5Topen2(grp2_id, string, H5P_DEFAULT); CHECK(type_id, FAIL, "H5Topen2"); ret = H5Tclose(type_id); CHECK(type_id, FAIL, "H5Tclose"); /* Don't close the group -- use it to test that object references * can refer to objects named in UTF-8 */ space_id = H5Screate_simple(RANK, &dims, NULL); CHECK(space_id, FAIL, "H5Screate_simple"); dset_id = H5Dcreate2(grp2_id, DSET3_NAME, H5T_STD_REF_OBJ, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(ret, FAIL, "H5Dcreate2"); /* Create reference to named datatype */ ret = H5Rcreate(&obj_ref, grp2_id, string, H5R_OBJECT, (hid_t)-1); CHECK(ret, FAIL, "H5Rcreate"); /* Write selection and read it back*/ ret = H5Dwrite(dset_id, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &obj_ref); CHECK(ret, FAIL, "H5Dwrite"); ret = H5Dread(dset_id, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, &obj_ref); CHECK(ret, FAIL, "H5Dread"); /* Ensure that we can open named datatype using object reference */ type_id = H5Rdereference2(dset_id, H5P_DEFAULT, H5R_OBJECT, &obj_ref); CHECK(type_id, FAIL, "H5Rdereference2"); ret = H5Tcommitted(type_id); VERIFY(ret, 1, "H5Tcommitted"); ret = H5Tclose(type_id); CHECK(type_id, FAIL, "H5Tclose"); ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); ret = H5Sclose(space_id); CHECK(ret, FAIL, "H5Sclose"); ret = H5Gclose(grp2_id); CHECK(ret, FAIL, "H5Gclose"); /* Create "group3". Build a hard link from group3 to group2, which has * a datatype with the UTF-8 name. Create a soft link in group3 * pointing through the hard link to the datatype. Give the soft * link a name in UTF-8. Ensure that the soft link works. */ grp3_id = H5Gcreate2(fid, GROUP3_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(grp3_id, FAIL, "H5Gcreate2"); ret = H5Lcreate_hard(fid, GROUP2_NAME, grp3_id, GROUP2_NAME, H5P_DEFAULT, H5P_DEFAULT); CHECK(ret, FAIL, "H5Lcreate_hard"); HDstrcpy(path_buf, GROUP2_NAME); HDstrcat(path_buf, "/"); HDstrcat(path_buf, string); ret = H5Lcreate_hard(grp3_id, path_buf, H5L_SAME_LOC, string, H5P_DEFAULT, H5P_DEFAULT); CHECK(ret, FAIL, "H5Lcreate_hard"); /* Open named datatype using soft link */ type_id = H5Topen2(grp3_id, string, H5P_DEFAULT); CHECK(type_id, FAIL, "H5Topen2"); ret = H5Tclose(type_id); CHECK(type_id, FAIL, "H5Tclose"); ret = H5Gclose(grp3_id); CHECK(ret, FAIL, "H5Gclose"); }
estatus_t e5_read_grid_list( eid_t e5_group_id, const char* list_name, e5_grid_dataset* grid_list) { int i; int d; int log_scale; int close_group; hsize_t h5_max_dim[3]; hsize_t h5_min_dim[3]; eid_t e5_list_group_id; eid_t e5_type_id; eid_t e5_dataset_id; eid_t e5_dataspace_id; eid_t e5_memspace_id; hid_t h5_status; estatus_t e5_status = E5_SUCCESS; if(list_name && strlen(list_name)) { e5_list_group_id = e5_create_group(e5_group_id, list_name); close_group = 1; } else { e5_list_group_id = e5_group_id; close_group = 0; } for(i = 0; grid_list && grid_list[i].name != 0; i++) { e5_grid_dataset* grid = &grid_list[i]; if(grid->name == 0 || strlen(grid->name) < 1) continue; e5_dataset_id = H5Dopen(e5_list_group_id, grid->name); if (e5_dataset_id < 0) { e5_status = E5_INVALID_DATASET; e5_error(e5_list_group_id, e5_status, "Failed to open grid dataset '%s'\n", grid->name); return e5_status; } if(!grid->data) { e5_status = E5_INVALID_POINTER; e5_error(e5_dataspace_id, e5_status, "Failed to provide pointer for reading '%s' from E5 data file\n", grid->name); break; } e5_dataspace_id = H5Dget_space(e5_dataset_id); e5_type_id = H5Dget_type(e5_dataset_id); H5Sget_simple_extent_dims(e5_dataspace_id, h5_min_dim, h5_max_dim); for(d = 0; d < 3; d++) { grid->dim[d] = h5_min_dim[d] >= h5_max_dim[d] ? h5_min_dim[d] : h5_max_dim[d]; grid->dim[d] = grid->dim[d] < 1 ? 1 : grid->dim[d]; } grid->type = e5_convert_hdf_type(e5_type_id); switch(grid->type) { case E5_TYPE_FLOAT: { e5_info(e5_group_id, "Reading grid [type='float', name='%s', dim='%u %u %u']\n", grid->name, grid->dim[0], grid->dim[1], grid->dim[2]); e5_memspace_id = H5Screate_simple(3, h5_min_dim, h5_max_dim); h5_status = H5Dread(e5_dataset_id, H5T_NATIVE_FLOAT, e5_memspace_id, e5_dataspace_id, H5P_DEFAULT, (grid->data)); if (h5_status < 0) { e5_status = E5_READ_FAILURE; e5_error(e5_dataset_id, e5_status, "Failed to read '%s' from F5 data file\n", grid->name); } H5Sclose(e5_memspace_id); break; } case E5_TYPE_DOUBLE: { e5_info(e5_group_id, "Reading grid [type='double', name='%s', dim='%u %u %u']\n", grid->name, grid->dim[0], grid->dim[1], grid->dim[2]); e5_memspace_id = H5Screate_simple(3, h5_min_dim, h5_max_dim); h5_status = H5Dread(e5_dataset_id, H5T_NATIVE_DOUBLE, e5_memspace_id, e5_dataspace_id, H5P_DEFAULT, (grid->data)); if (h5_status < 0) { e5_status = E5_READ_FAILURE; e5_error(e5_dataset_id, e5_status, "Failed to read '%s' from F5 data file\n", grid->name); } H5Sclose(e5_memspace_id); break; } case E5_TYPE_INVALID: default: { e5_status = E5_INVALID_TYPE; e5_error(e5_dataset_id, e5_status, "Invalid type for grid '%s' data\n", grid->name); break; } }; log_scale = 0; if(e5_is_valid_attr(e5_dataset_id, "log10")) e5_read_attr_int(e5_dataset_id, "log10", &log_scale); grid->scale = log_scale ? E5_VALUE_SCALE_LOG10 : E5_VALUE_SCALE_LINEAR; H5Sclose(e5_dataspace_id); H5Dclose(e5_dataset_id); H5Tclose(e5_type_id); } if(list_name) e5_close_group(e5_list_group_id); return e5_status; }
void cow_histogram_dumphdf5(cow_histogram *h, char *fn, char *gn) // ----------------------------------------------------------------------------- // Dumps the histogram to the HDF5 file named `fn`, under the group // `gn`/h->fullname. The function uses rank 0 to do the write. // ----------------------------------------------------------------------------- { #if (COW_HDF5) if (!(h->committed && h->sealed)) { return; } char gname[1024]; int rank = 0; snprintf(gname, 1024, "%s/%s", gn, h->nickname); #if (COW_MPI) if (cow_mpirunning()) { MPI_Comm_rank(h->comm, &rank); } #endif if (rank == 0) { // ------------------------------------------------------------------------- // The write functions assume the file is already created. Have master // create the file if it's not there already. // ------------------------------------------------------------------------- FILE *testf = fopen(fn, "r"); hid_t fid; if (testf == NULL) { fid = H5Fcreate(fn, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); } else { fclose(testf); fid = H5Fopen(fn, H5F_ACC_RDWR, H5P_DEFAULT); } if (H5Lexists_safe(fid, gname)) { printf("[%s] writing histogram as HDF5 to %s/%s (clobber existing)\n", MODULE, fn, gname); H5Gunlink(fid, gname); } else { printf("[%s] writing histogram as HDF5 to %s/%s\n", MODULE, fn, gname); } hid_t gcpl = H5Pcreate(H5P_LINK_CREATE); H5Pset_create_intermediate_group(gcpl, 1); hid_t memb = H5Gcreate(fid, gname, gcpl, H5P_DEFAULT, H5P_DEFAULT); H5Pclose(gcpl); H5Gclose(memb); H5Fclose(fid); } else { return; } // Create a group to represent this histogram, and an attribute to name it // --------------------------------------------------------------------------- hid_t fid = H5Fopen(fn, H5F_ACC_RDWR, H5P_DEFAULT); hid_t grp = H5Gopen(fid, gname, H5P_DEFAULT); if (h->fullname != NULL) { hid_t aspc = H5Screate(H5S_SCALAR); hid_t strn = H5Tcopy(H5T_C_S1); H5Tset_size(strn, strlen(h->fullname)); hid_t attr = H5Acreate(grp, "fullname", strn, aspc, H5P_DEFAULT, H5P_DEFAULT); H5Awrite(attr, strn, h->fullname); // write the full name H5Aclose(attr); H5Tclose(strn); H5Sclose(aspc); } // Create the data sets in the group: binloc (bin centers) and binval (values) // --------------------------------------------------------------------------- double *binlocX = h->binlocx; double *binlocY = h->binlocy; double *binvalV = h->binvalv; hsize_t sizeX[2] = { h->nbinsx }; hsize_t sizeY[2] = { h->nbinsy }; hsize_t sizeZ[2] = { h->nbinsx, h->nbinsy }; hid_t fspcZ = H5Screate_simple(h->n_dims, sizeZ, NULL); if (h->n_dims >= 1) { hid_t fspcX = H5Screate_simple(1, sizeX, NULL); hid_t dsetbinX = H5Dcreate(grp, "binlocX", H5T_NATIVE_DOUBLE, fspcX, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(dsetbinX, H5T_NATIVE_DOUBLE, fspcX, fspcX, H5P_DEFAULT, binlocX); H5Dclose(dsetbinX); H5Sclose(fspcX); } if (h->n_dims >= 2) { hid_t fspcY = H5Screate_simple(1, sizeY, NULL); hid_t dsetbinY = H5Dcreate(grp, "binlocY", H5T_NATIVE_DOUBLE, fspcY, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(dsetbinY, H5T_NATIVE_DOUBLE, fspcY, fspcY, H5P_DEFAULT, binlocY); H5Dclose(dsetbinY); H5Sclose(fspcY); } hid_t dsetvalV = H5Dcreate(grp, "binval", H5T_NATIVE_DOUBLE, fspcZ, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(dsetvalV, H5T_NATIVE_DOUBLE, fspcZ, fspcZ, H5P_DEFAULT, binvalV); H5Dclose(dsetvalV); H5Sclose(fspcZ); H5Gclose(grp); H5Fclose(fid); #endif }
int main(void) { hid_t fid; /* File, group, datasets, datatypes */ hid_t gid_a; /* and dataspaces identifiers */ hid_t did_b, sid_b, tid_b; hid_t did_r, tid_r, sid_r; H5O_type_t obj_type; herr_t status; hobj_ref_t *wbuf; /* buffer to write to disk */ hobj_ref_t *rbuf; /* buffer to read from disk */ hsize_t dim_r[1]; hsize_t dim_b[2]; /* * Create a file using default properties. */ fid = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create group "A" in the file. */ gid_a = H5Gcreate2(fid, "A", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataset "B" in the file. */ dim_b[0] = 2; dim_b[1] = 6; sid_b = H5Screate_simple(2, dim_b, NULL); did_b = H5Dcreate2(fid, "B", H5T_NATIVE_FLOAT, sid_b, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataset "R" to store references to the objects "A" and "B". */ dim_r[0] = 2; sid_r = H5Screate_simple(1, dim_r, NULL); tid_r = H5Tcopy(H5T_STD_REF_OBJ); did_r = H5Dcreate2(fid, "R", tid_r, sid_r, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Allocate write and read buffers. */ wbuf = (hobj_ref_t *)malloc(sizeof(hobj_ref_t) * 2); rbuf = (hobj_ref_t *)malloc(sizeof(hobj_ref_t) * 2); /* * Create references to the group "A" and dataset "B" * and store them in the wbuf. */ H5Rcreate(&wbuf[0], fid, "A", H5R_OBJECT, (hid_t)-1); H5Rcreate(&wbuf[1], fid, "B", H5R_OBJECT, (hid_t)-1); /* * Write dataset R using default transfer properties. */ status = H5Dwrite(did_r, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, wbuf); /* * Close all objects. */ H5Gclose(gid_a); H5Sclose(sid_b); H5Dclose(did_b); H5Tclose(tid_r); H5Sclose(sid_r); H5Dclose(did_r); H5Fclose(fid); /* * Reopen the file. */ fid = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT); /* * Open and read dataset "R". */ did_r = H5Dopen2(fid, "R", H5P_DEFAULT); status = H5Dread(did_r, H5T_STD_REF_OBJ, H5S_ALL, H5S_ALL, H5P_DEFAULT, rbuf); /* * Find the type of referenced objects. */ status = H5Rget_obj_type2(did_r, H5R_OBJECT, &rbuf[0], &obj_type); if(obj_type == H5O_TYPE_GROUP) printf("First dereferenced object is a group. \n"); status = H5Rget_obj_type2(did_r, H5R_OBJECT, &rbuf[1], &obj_type); if(obj_type == H5O_TYPE_DATASET) printf("Second dereferenced object is a dataset. \n"); /* * Get datatype of the dataset "B" */ did_b = H5Rdereference2(did_r, H5P_DEFAULT, H5R_OBJECT, &rbuf[1]); tid_b = H5Dget_type(did_b); if(H5Tequal(tid_b, H5T_NATIVE_FLOAT)) printf("Datatype of the dataset is H5T_NATIVE_FLOAT.\n"); printf("\n"); /* * Close all objects and free memory buffers. */ H5Dclose(did_r); H5Dclose(did_b); H5Tclose(tid_b); H5Fclose(fid); free(rbuf); free(wbuf); return 0; }
/*------------------------------------------------------------------------- * Function: diff_datasetid * * Purpose: check for comparable datasets and read into a compatible * memory type * * Return: Number of differences found * * Programmer: Pedro Vicente, [email protected] * * Date: May 9, 2003 * * Modifications: * * * October 2006: Read by hyperslabs for big datasets. * * A threshold of H5TOOLS_MALLOCSIZE (128 MB) is the limit upon which I/O hyperslab is done * i.e., if the memory needed to read a dataset is greater than this limit, * then hyperslab I/O is done instead of one operation I/O * For each dataset, the memory needed is calculated according to * * memory needed = number of elements * size of each element * * if the memory needed is lower than H5TOOLS_MALLOCSIZE, then the following operations * are done * * H5Dread( input_dataset1 ) * H5Dread( input_dataset2 ) * * with all elements in the datasets selected. If the memory needed is greater than * H5TOOLS_MALLOCSIZE, then the following operations are done instead: * * a strip mine is defined for each dimension k (a strip mine is defined as a * hyperslab whose size is memory manageable) according to the formula * * (1) strip_mine_size[k ] = MIN(dimension[k ], H5TOOLS_BUFSIZE / size of memory type) * * where H5TOOLS_BUFSIZE is a constant currently defined as 1MB. This formula assures * that for small datasets (small relative to the H5TOOLS_BUFSIZE constant), the strip * mine size k is simply defined as its dimension k, but for larger datasets the * hyperslab size is still memory manageable. * a cycle is done until the number of elements in the dataset is reached. In each * iteration, two parameters are defined for the function H5Sselect_hyperslab, * the start and size of each hyperslab, according to * * (2) hyperslab_size [k] = MIN(dimension[k] - hyperslab_offset[k], strip_mine_size [k]) * * where hyperslab_offset [k] is initially set to zero, and later incremented in * hyperslab_size[k] offsets. The reason for the operation * * dimension[k] - hyperslab_offset[k] * * in (2) is that, when using the strip mine size, it assures that the "remaining" part * of the dataset that does not fill an entire strip mine is processed. * *------------------------------------------------------------------------- */ hsize_t diff_datasetid( hid_t did1, hid_t did2, const char *obj1_name, const char *obj2_name, diff_opt_t *options) { hid_t sid1=-1; hid_t sid2=-1; hid_t f_tid1=-1; hid_t f_tid2=-1; hid_t m_tid1=-1; hid_t m_tid2=-1; size_t m_size1; size_t m_size2; H5T_sign_t sign1; H5T_sign_t sign2; int rank1; int rank2; hsize_t nelmts1; hsize_t nelmts2; hsize_t dims1[H5S_MAX_RANK]; hsize_t dims2[H5S_MAX_RANK]; hsize_t maxdim1[H5S_MAX_RANK]; hsize_t maxdim2[H5S_MAX_RANK]; const char *name1=NULL; /* relative names */ const char *name2=NULL; hsize_t storage_size1; hsize_t storage_size2; hsize_t nfound=0; /* number of differences found */ int can_compare=1; /* do diff or not */ void *buf1=NULL; void *buf2=NULL; void *sm_buf1=NULL; void *sm_buf2=NULL; hid_t sm_space; /*stripmine data space */ size_t need; /* bytes needed for malloc */ int i; unsigned int vl_data = 0; /*contains VL datatypes */ h5difftrace("diff_datasetid start\n"); /* Get the dataspace handle */ if ( (sid1 = H5Dget_space(did1)) < 0 ) goto error; /* Get rank */ if ( (rank1 = H5Sget_simple_extent_ndims(sid1)) < 0 ) goto error; /* Get the dataspace handle */ if ( (sid2 = H5Dget_space(did2)) < 0 ) goto error; /* Get rank */ if ( (rank2 = H5Sget_simple_extent_ndims(sid2)) < 0 ) goto error; /* Get dimensions */ if ( H5Sget_simple_extent_dims(sid1,dims1,maxdim1) < 0 ) goto error; /* Get dimensions */ if ( H5Sget_simple_extent_dims(sid2,dims2,maxdim2) < 0 ) { goto error; } /*------------------------------------------------------------------------- * get the file data type *------------------------------------------------------------------------- */ /* Get the data type */ if ( (f_tid1 = H5Dget_type(did1)) < 0 ) goto error; /* Get the data type */ if ( (f_tid2 = H5Dget_type(did2)) < 0 ) { goto error; } /*------------------------------------------------------------------------- * check for empty datasets *------------------------------------------------------------------------- */ h5difftrace("check for empty datasets\n"); storage_size1=H5Dget_storage_size(did1); storage_size2=H5Dget_storage_size(did2); if (storage_size1==0 || storage_size2==0) { if ( (options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name) parallel_print("Not comparable: <%s> or <%s> is an empty dataset\n", obj1_name, obj2_name); can_compare=0; options->not_cmp=1; } /*------------------------------------------------------------------------- * check for comparable TYPE and SPACE *------------------------------------------------------------------------- */ if (diff_can_type(f_tid1, f_tid2, rank1, rank2, dims1, dims2, maxdim1, maxdim2, obj1_name, obj2_name, options, 0)!=1) { can_compare=0; } /*------------------------------------------------------------------------- * memory type and sizes *------------------------------------------------------------------------- */ h5difftrace("check for memory type and sizes\n"); if ((m_tid1=h5tools_get_native_type(f_tid1)) < 0) goto error; if ((m_tid2=h5tools_get_native_type(f_tid2)) < 0) goto error; m_size1 = H5Tget_size( m_tid1 ); m_size2 = H5Tget_size( m_tid2 ); /*------------------------------------------------------------------------- * check for different signed/unsigned types *------------------------------------------------------------------------- */ if (can_compare) { h5difftrace("can_compare for sign\n"); sign1=H5Tget_sign(m_tid1); sign2=H5Tget_sign(m_tid2); if ( sign1 != sign2 ) { h5difftrace("sign1 != sign2\n"); if ((options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name) { parallel_print("Not comparable: <%s> has sign %s ", obj1_name, get_sign(sign1)); parallel_print("and <%s> has sign %s\n", obj2_name, get_sign(sign2)); } can_compare=0; options->not_cmp=1; } } /* Check if type is either VLEN-data or VLEN-string to reclaim any * VLEN memory buffer later */ if( TRUE == h5tools_detect_vlen(m_tid1) ) vl_data = TRUE; /*------------------------------------------------------------------------ * only attempt to compare if possible *------------------------------------------------------------------------- */ if(can_compare) /* it is possible to compare */ { h5difftrace("can_compare attempt\n"); /*----------------------------------------------------------------- * get number of elements *------------------------------------------------------------------ */ nelmts1 = 1; for(i = 0; i < rank1; i++) nelmts1 *= dims1[i]; nelmts2 = 1; for(i = 0; i < rank2; i++) nelmts2 *= dims2[i]; HDassert(nelmts1 == nelmts2); /*----------------------------------------------------------------- * "upgrade" the smaller memory size *------------------------------------------------------------------ */ h5difftrace("upgrade the smaller memory size?\n"); if (FAIL == match_up_memsize (f_tid1, f_tid2, &m_tid1, &m_tid2, &m_size1, &m_size2)) goto error; /* print names */ if(obj1_name) name1 = diff_basename(obj1_name); if(obj2_name) name2 = diff_basename(obj2_name); /*---------------------------------------------------------------- * read/compare *----------------------------------------------------------------- */ need = (size_t)(nelmts1 * m_size1); /* bytes needed */ if(need < H5TOOLS_MALLOCSIZE) { buf1 = HDmalloc(need); buf2 = HDmalloc(need); } /* end if */ if(buf1 != NULL && buf2 != NULL) { h5difftrace("buf1 != NULL && buf2 != NULL\n"); if(H5Dread(did1, m_tid1, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf1) < 0) goto error; if(H5Dread(did2, m_tid2, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf2) < 0) goto error; /* array diff */ nfound = diff_array(buf1, buf2, nelmts1, (hsize_t)0, rank1, dims1, options, name1, name2, m_tid1, did1, did2); /* reclaim any VL memory, if necessary */ if(vl_data) { H5Dvlen_reclaim(m_tid1, sid1, H5P_DEFAULT, buf1); H5Dvlen_reclaim(m_tid2, sid2, H5P_DEFAULT, buf2); } /* end if */ } /* end if */ else /* possibly not enough memory, read/compare by hyperslabs */ { size_t p_type_nbytes = m_size1; /*size of memory type */ hsize_t p_nelmts = nelmts1; /*total selected elmts */ hsize_t elmtno; /*counter */ int carry; /*counter carry value */ /* stripmine info */ hsize_t sm_size[H5S_MAX_RANK]; /*stripmine size */ hsize_t sm_nbytes; /*bytes per stripmine */ hsize_t sm_nelmts; /*elements per stripmine*/ /* hyperslab info */ hsize_t hs_offset[H5S_MAX_RANK]; /*starting offset */ hsize_t hs_size[H5S_MAX_RANK]; /*size this pass */ hsize_t hs_nelmts; /*elements in request */ hsize_t zero[8]; /*vector of zeros */ /* * determine the strip mine size and allocate a buffer. The strip mine is * a hyperslab whose size is manageable. */ sm_nbytes = p_type_nbytes; for(i = rank1; i > 0; --i) { hsize_t size = H5TOOLS_BUFSIZE / sm_nbytes; if(size == 0) /* datum size > H5TOOLS_BUFSIZE */ size = 1; sm_size[i - 1] = MIN(dims1[i - 1], size); sm_nbytes *= sm_size[i - 1]; HDassert(sm_nbytes > 0); } /* end for */ /* malloc return code should be verified. * If fail, need to handle the error. * This else branch should be recoded as a separate function. * Note that there are many "goto error" within this branch * that fails to address freeing other objects created here. * E.g., sm_space. */ sm_buf1 = HDmalloc((size_t)sm_nbytes); HDassert(sm_buf1); sm_buf2 = HDmalloc((size_t)sm_nbytes); HDassert(sm_buf2); sm_nelmts = sm_nbytes / p_type_nbytes; sm_space = H5Screate_simple(1, &sm_nelmts, NULL); /* the stripmine loop */ HDmemset(hs_offset, 0, sizeof hs_offset); HDmemset(zero, 0, sizeof zero); for(elmtno = 0; elmtno < p_nelmts; elmtno += hs_nelmts) { /* calculate the hyperslab size */ if(rank1 > 0) { for(i = 0, hs_nelmts = 1; i < rank1; i++) { hs_size[i] = MIN(dims1[i] - hs_offset[i], sm_size[i]); hs_nelmts *= hs_size[i]; } /* end for */ if(H5Sselect_hyperslab(sid1, H5S_SELECT_SET, hs_offset, NULL, hs_size, NULL) < 0) goto error; if(H5Sselect_hyperslab(sid2, H5S_SELECT_SET, hs_offset, NULL, hs_size, NULL) < 0) goto error; if(H5Sselect_hyperslab(sm_space, H5S_SELECT_SET, zero, NULL, &hs_nelmts, NULL) < 0) goto error; } /* end if */ else hs_nelmts = 1; if(H5Dread(did1,m_tid1,sm_space,sid1,H5P_DEFAULT,sm_buf1) < 0) goto error; if(H5Dread(did2,m_tid2,sm_space,sid2,H5P_DEFAULT,sm_buf2) < 0) goto error; /* get array differences. in the case of hyperslab read, increment the number of differences found in each hyperslab and pass the position at the beggining for printing */ nfound += diff_array(sm_buf1, sm_buf2, hs_nelmts, elmtno, rank1, dims1, options, name1, name2, m_tid1, did1, did2); /* reclaim any VL memory, if necessary */ if(vl_data) { H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf1); H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf2); } /* end if */ /* calculate the next hyperslab offset */ for(i = rank1, carry = 1; i > 0 && carry; --i) { hs_offset[i - 1] += hs_size[i - 1]; if(hs_offset[i - 1] == dims1[i - 1]) hs_offset[i - 1] = 0; else carry = 0; } /* i */ } /* elmtno */ H5Sclose(sm_space); } /* hyperslab read */ } /*can_compare*/ /*------------------------------------------------------------------------- * close *------------------------------------------------------------------------- */ h5difftrace("compare attributes?\n"); /* free */ if(buf1 != NULL) { HDfree(buf1); buf1 = NULL; } /* end if */ if(buf2 != NULL) { HDfree(buf2); buf2 = NULL; } /* end if */ if(sm_buf1 != NULL) { HDfree(sm_buf1); sm_buf1 = NULL; } /* end if */ if(sm_buf2 != NULL) { HDfree(sm_buf2); sm_buf2 = NULL; } /* end if */ H5E_BEGIN_TRY { H5Sclose(sid1); H5Sclose(sid2); H5Tclose(f_tid1); H5Tclose(f_tid2); H5Tclose(m_tid1); H5Tclose(m_tid2); } H5E_END_TRY; h5difftrace("diff_datasetid finish\n"); return nfound; error: options->err_stat=1; /* free */ if (buf1!=NULL) { /* reclaim any VL memory, if necessary */ if(vl_data) H5Dvlen_reclaim(m_tid1, sid1, H5P_DEFAULT, buf1); HDfree(buf1); buf1=NULL; } if (buf2!=NULL) { /* reclaim any VL memory, if necessary */ if(vl_data) H5Dvlen_reclaim(m_tid2, sid2, H5P_DEFAULT, buf2); HDfree(buf2); buf2=NULL; } if (sm_buf1!=NULL) { /* reclaim any VL memory, if necessary */ if(vl_data) H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf1); HDfree(sm_buf1); sm_buf1=NULL; } if (sm_buf2!=NULL) { /* reclaim any VL memory, if necessary */ if(vl_data) H5Dvlen_reclaim(m_tid1, sm_space, H5P_DEFAULT, sm_buf2); HDfree(sm_buf2); sm_buf2=NULL; } /* disable error reporting */ H5E_BEGIN_TRY { H5Sclose(sid1); H5Sclose(sid2); H5Tclose(f_tid1); H5Tclose(f_tid2); H5Tclose(m_tid1); H5Tclose(m_tid2); /* enable error reporting */ } H5E_END_TRY; h5difftrace("diff_datasetid errored\n"); return nfound; }
int diff_can_type( hid_t f_tid1, /* file data type */ hid_t f_tid2, /* file data type */ int rank1, int rank2, hsize_t *dims1, hsize_t *dims2, hsize_t *maxdim1, hsize_t *maxdim2, const char *obj1_name, const char *obj2_name, diff_opt_t *options, int is_compound) { H5T_class_t tclass1; H5T_class_t tclass2; int maxdim_diff=0; /* maximum dimensions are different */ int dim_diff=0; /* current dimensions are different */ int i; int can_compare = 1; /* return value */ /*------------------------------------------------------------------------- * check for the same class *------------------------------------------------------------------------- */ if ((tclass1=H5Tget_class(f_tid1)) < 0) return -1; if ((tclass2=H5Tget_class(f_tid2)) < 0) return -1; if ( tclass1 != tclass2 ) { if ( (options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name) { if ( is_compound ) { parallel_print("Not comparable: <%s> has a class %s and <%s> has a class %s\n", obj1_name, get_class(tclass1), obj2_name, get_class(tclass2) ); } else { parallel_print("Not comparable: <%s> is of class %s and <%s> is of class %s\n", obj1_name, get_class(tclass1), obj2_name, get_class(tclass2) ); } } can_compare = 0; options->not_cmp = 1; return can_compare; } /*------------------------------------------------------------------------- * check for non supported classes *------------------------------------------------------------------------- */ HDassert(tclass1==tclass2); switch (tclass1) { case H5T_INTEGER: case H5T_FLOAT: case H5T_COMPOUND: case H5T_STRING: case H5T_ARRAY: case H5T_BITFIELD: case H5T_OPAQUE: case H5T_ENUM: case H5T_VLEN: case H5T_REFERENCE: break; default: /*H5T_TIME */ if ( (options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name) { parallel_print("Not comparable: <%s> and <%s> are of class %s\n", obj1_name,obj2_name,get_class(tclass2) ); } can_compare = 0; options->not_cmp = 1; return can_compare; } /*------------------------------------------------------------------------- * check for equal file datatype; warning only *------------------------------------------------------------------------- */ if ( (H5Tequal(f_tid1, f_tid2)==0) && (options->m_verbose) && obj1_name && obj2_name) { H5T_class_t cl = H5Tget_class(f_tid1); parallel_print("Warning: different storage datatype\n"); if ( cl == H5T_INTEGER || cl == H5T_FLOAT ) { parallel_print("<%s> has file datatype ", obj1_name); print_type(f_tid1); parallel_print("\n"); parallel_print("<%s> has file datatype ", obj2_name); print_type(f_tid2); parallel_print("\n"); } } /*------------------------------------------------------------------------- * check for the same rank *------------------------------------------------------------------------- */ if ( rank1 != rank2 ) { if ( (options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name) { parallel_print("Not comparable: <%s> has rank %d, dimensions ", obj1_name, rank1); print_dimensions(rank1,dims1); parallel_print(", max dimensions "); print_dimensions(rank1,maxdim1); parallel_print("\n" ); parallel_print("and <%s> has rank %d, dimensions ", obj2_name, rank2); print_dimensions(rank2,dims2); parallel_print(", max dimensions "); print_dimensions(rank2,maxdim2); parallel_print("\n"); } can_compare = 0; options->not_cmp = 1; return can_compare; } /*------------------------------------------------------------------------- * check for different dimensions *------------------------------------------------------------------------- */ HDassert(rank1==rank2); for ( i=0; i<rank1; i++) { if (maxdim1 && maxdim2) { if ( maxdim1[i] != maxdim2[i] ) maxdim_diff=1; } if ( dims1[i] != dims2[i] ) dim_diff=1; } /*------------------------------------------------------------------------- * current dimensions *------------------------------------------------------------------------- */ if (dim_diff==1) { if ( (options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name) { parallel_print("Not comparable: <%s> has rank %d, dimensions ", obj1_name, rank1); print_dimensions(rank1,dims1); if (maxdim1 && maxdim2) { parallel_print(", max dimensions "); print_dimensions(rank1,maxdim1); parallel_print("\n" ); parallel_print("and <%s> has rank %d, dimensions ", obj2_name, rank2); print_dimensions(rank2,dims2); parallel_print(", max dimensions "); print_dimensions(rank2,maxdim2); parallel_print("\n"); } } can_compare = 0; options->not_cmp = 1; return can_compare; } /*------------------------------------------------------------------------- * maximum dimensions; just give a warning *------------------------------------------------------------------------- */ if (maxdim1 && maxdim2 && maxdim_diff==1 && obj1_name ) { if (options->m_verbose) { parallel_print( "Warning: different maximum dimensions\n"); parallel_print("<%s> has max dimensions ", obj1_name); print_dimensions(rank1,maxdim1); parallel_print("\n"); parallel_print("<%s> has max dimensions ", obj2_name); print_dimensions(rank2,maxdim2); parallel_print("\n"); } } if ( tclass1 == H5T_COMPOUND ) { int nmembs1; int nmembs2; int j; hid_t memb_type1; hid_t memb_type2; nmembs1 = H5Tget_nmembers(f_tid1); nmembs2 = H5Tget_nmembers(f_tid2); if ( nmembs1 != nmembs2 ) { if ( (options->m_verbose||options->m_list_not_cmp) && obj1_name && obj2_name) { parallel_print("Not comparable: <%s> has %d members ", obj1_name, nmembs1); parallel_print("<%s> has %d members ", obj2_name, nmembs2); parallel_print("\n"); } can_compare = 0; options->not_cmp = 1; return can_compare; } for (j = 0; j < nmembs1; j++) { memb_type1 = H5Tget_member_type(f_tid1, (unsigned)j); memb_type2 = H5Tget_member_type(f_tid2, (unsigned)j); if (diff_can_type(memb_type1, memb_type2, rank1, rank2, dims1, dims2, maxdim1, maxdim2, obj1_name, obj2_name, options, 1)!=1) { can_compare = 0; options->not_cmp = 1; H5Tclose(memb_type1); H5Tclose(memb_type2); return can_compare; } H5Tclose(memb_type1); H5Tclose(memb_type2); } } return can_compare; }
int add_attrs(hid_t objid) { hid_t scalar_spaceid = -1; hid_t vlstr_typeid = -1, fixstr_typeid = -1; char *vlstr; hid_t attid = -1; /* Create scalar dataspace */ if ((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR_GOTO; /* Create string datatypes */ if ((vlstr_typeid = H5Tcreate(H5T_STRING, (size_t)H5T_VARIABLE)) < 0) ERR_GOTO; if ((fixstr_typeid = H5Tcreate(H5T_STRING, (size_t)10)) < 0) ERR_GOTO; /* Create attribute with VL string datatype on object */ if ((attid = H5Acreate2(objid, VSTR_ATT1_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO; /* No write, use fill value */ if (H5Aclose(attid) < 0) ERR_GOTO; /* Create attribute with VL string datatype on object */ if ((attid = H5Acreate2(objid, VSTR_ATT2_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO; vlstr = NULL; if (H5Awrite(attid, vlstr_typeid, &vlstr) < 0) ERR_GOTO; if (H5Aclose(attid) < 0) ERR_GOTO; /* Create attribute with VL string datatype on object */ if ((attid = H5Acreate2(objid, VSTR_ATT3_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO; vlstr = malloc(10); *vlstr = '\0'; if (H5Awrite(attid, vlstr_typeid, &vlstr) < 0) ERR_GOTO; if (H5Aclose(attid) < 0) ERR_GOTO; /* Create attribute with VL string datatype on object */ if ((attid = H5Acreate2(objid, VSTR_ATT4_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO; strcpy(vlstr, "foo"); if (H5Awrite(attid, vlstr_typeid, &vlstr) < 0) ERR_GOTO; free(vlstr); if (H5Aclose(attid) < 0) ERR_GOTO; /* Create attribute with fixed-length string datatype on object */ if ((attid = H5Acreate2(objid, FSTR_ATT_NAME, fixstr_typeid, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO; if (H5Aclose(attid) < 0) ERR_GOTO; /* Create attribute with native integer datatype on object */ if ((attid = H5Acreate2(objid, INT_ATT_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR_GOTO; if (H5Aclose(attid) < 0) ERR_GOTO; /* Clean up objects created */ if (H5Sclose(scalar_spaceid) < 0) ERR_GOTO; if (H5Tclose(vlstr_typeid) < 0) ERR_GOTO; if (H5Tclose(fixstr_typeid) < 0) ERR_GOTO; return(0); error: H5E_BEGIN_TRY { H5Aclose(attid); H5Sclose(scalar_spaceid); H5Tclose(vlstr_typeid); H5Tclose(fixstr_typeid); } H5E_END_TRY; return(-1); }
/*------------------------------------------------------------------------- * Function: process_cmpd_fields * * Purpose: To check whether the fields selected in "g_list_of_fields" * are valid fields associated with the dataset. * * Return: 0 on success; negative on failure * * Programmer: Vailin Choi; August 2010 * *------------------------------------------------------------------------- */ static herr_t process_cmpd_fields(hid_t fid, char *dsetname) { hid_t did=-1; /* dataset id */ hid_t dtid=-1, tid=-1; /* dataset's data type id */ size_t len; /* number of comma-separated fields in "g_list_of_fields" */ herr_t ret_value = SUCCEED; /* Return value */ HDassert(g_list_of_fields && *g_list_of_fields); /* Open the dataset */ if((did = H5Dopen2(fid, dsetname, H5P_DEFAULT)) < 0) { error_msg("error in opening dataset \"%s\"\n", dsetname); ret_value = FAIL; goto done; } /* Get the dataset's datatype */ if(((dtid = H5Dget_type(did)) < 0) || (tid = H5Tget_native_type(dtid, H5T_DIR_DEFAULT)) < 0) { error_msg("error in getting dataset's datatype\n"); ret_value = FAIL; goto done; } /* Check to make sure that the dataset's datatype is compound type */ if(H5Tget_class(dtid) != H5T_COMPOUND) { error_msg("dataset should be compound type for <list_of_fields>\n"); ret_value = FAIL; goto done; } /* Make a copy of "g_list_of_fields" */ if((g_dup_fields = HDstrdup(g_list_of_fields)) == NULL) { error_msg("error in duplicating g_list_of_fields\n"); ret_value = FAIL; goto done; } /* Estimate the number of comma-separated fields in "g_list of_fields" */ len = HDstrlen(g_list_of_fields)/2 + 2; /* Allocate memory for a list vector of H5LD_memb_t structures to store "g_list_of_fields" info */ if((g_listv = (H5LD_memb_t **)HDcalloc(len, sizeof(H5LD_memb_t *))) == NULL) { error_msg("error in allocating memory for H5LD_memb_t\n"); ret_value = FAIL; goto done; } /* Process and store info for "g_listv" */ if(H5LD_construct_vector(g_dup_fields, g_listv, tid) < 0) { error_msg("error in processing <list_of_fields>\n"); ret_value = FAIL; goto done; } /* Will free memory for g_listv and g_dup_fields when exiting from h5watch */ done: /* Closing */ H5E_BEGIN_TRY H5Tclose(dtid); H5Tclose(tid); H5Dclose(did); H5E_END_TRY return(ret_value); } /* process_cmpd_fields() */
int main() { printf("\n*** Creating file with datasets & attributes that have scalar dataspaces..."); { hid_t fileid; hid_t fcplid; hid_t dsetid; hid_t dcplid; hid_t scalar_spaceid; hid_t vlstr_typeid, fixstr_typeid; hid_t attid; /* Create scalar dataspace */ if ((scalar_spaceid = H5Screate(H5S_SCALAR)) < 0) ERR; /* Set creation ordering for file, so we can revise its contents later */ if ((fcplid = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; if (H5Pset_link_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR; if (H5Pset_attr_creation_order(fcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR; /* Create new file, using default properties */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcplid, H5P_DEFAULT)) < 0) ERR; /* Close file creation property list */ if (H5Pclose(fcplid) < 0) ERR; /* Create variable-length string datatype */ if ((vlstr_typeid = H5Tcreate(H5T_STRING, (size_t)H5T_VARIABLE)) < 0) ERR; /* Create fixed-length string datatype */ if ((fixstr_typeid = H5Tcreate(H5T_STRING, (size_t)10)) < 0) ERR; /* Set creation ordering for dataset, so we can revise its contents later */ if ((dcplid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_attr_creation_order(dcplid, H5P_CRT_ORDER_TRACKED) < 0) ERR; /* Create scalar dataset with VL string datatype */ if ((dsetid = H5Dcreate2(fileid, VSTR_VAR1_NAME, vlstr_typeid, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR; /* Add attributes to dataset */ if (add_attrs(dsetid) < 0) ERR; /* Close VL string dataset */ if (H5Dclose(dsetid) < 0) ERR; /* Create scalar dataset with fixed-length string datatype */ if ((dsetid = H5Dcreate2(fileid, FSTR_VAR_NAME, fixstr_typeid, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR; /* Add attributes to dataset */ if (add_attrs(dsetid) < 0) ERR; /* Close fixed-length string dataset */ if (H5Dclose(dsetid) < 0) ERR; /* Create scalar dataset with native integer datatype */ if ((dsetid = H5Dcreate2(fileid, INT_VAR_NAME, H5T_NATIVE_INT, scalar_spaceid, H5P_DEFAULT, dcplid, H5P_DEFAULT)) < 0) ERR; /* Add attributes to dataset */ if (add_attrs(dsetid) < 0) ERR; /* Close native integer dataset */ if (H5Dclose(dsetid) < 0) ERR; /* Add attributes to root group */ if (add_attrs(fileid) < 0) ERR; /* Close dataset creation property list */ if (H5Pclose(dcplid) < 0) ERR; /* Close string datatypes */ if (H5Tclose(vlstr_typeid) < 0) ERR; if (H5Tclose(fixstr_typeid) < 0) ERR; /* Close rest */ if (H5Sclose(scalar_spaceid) < 0) ERR; if (H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking accessing file through netCDF-4 API..."); { int ncid, varid; size_t len; nc_type type; int ndims; char *vlstr; int x; if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; /* Check the global attributes are OK */ if (check_attrs(ncid, NC_GLOBAL) < 0) ERR; /* Verify that the VL string dataset is present and OK */ if (nc_inq_varid(ncid, VSTR_VAR1_NAME, &varid)) ERR; if (varid != 0) ERR; if (nc_inq_vartype(ncid, varid, &type)) ERR; if (type != NC_STRING) ERR; if (nc_inq_varndims(ncid, varid, &ndims)) ERR; if (ndims != 0) ERR; vlstr = NULL; if (nc_get_var(ncid, varid, &vlstr)) ERR; if (NULL != vlstr) ERR; /* Check the variable's attributes are OK */ if (check_attrs(ncid, varid) < 0) ERR; /* Verify that the fixed-length string dataset is present and OK */ if (nc_inq_varid(ncid, FSTR_VAR_NAME, &varid)) ERR; if (varid != 1) ERR; if (nc_inq_vartype(ncid, varid, &type)) ERR; if (type != NC_STRING) ERR; if (nc_inq_varndims(ncid, varid, &ndims)) ERR; if (ndims != 0) ERR; vlstr = NULL; if (nc_get_var(ncid, varid, &vlstr)) ERR; if ('\0' != *vlstr) ERR; free(vlstr); /* Check the variable's attributes are OK */ if (check_attrs(ncid, varid) < 0) ERR; /* Verify that the integer dataset is present and OK */ if (nc_inq_varid(ncid, INT_VAR_NAME, &varid)) ERR; if (varid != 2) ERR; if (nc_inq_vartype(ncid, varid, &type)) ERR; if (type != NC_INT) ERR; if (nc_inq_varndims(ncid, varid, &ndims)) ERR; if (ndims != 0) ERR; x = -1; if (nc_get_var(ncid, varid, &x)) ERR; if (0 != x) ERR; /* Check the variable's attributes are OK */ if (check_attrs(ncid, varid) < 0) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; printf("*** Checking revising file through netCDF-4 API..."); { int ncid, varid; char *vlstr; if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR; /* Write to the VL string variable */ if (nc_inq_varid(ncid, VSTR_VAR1_NAME, &varid)) ERR; vlstr = NULL; if (nc_put_var(ncid, varid, &vlstr)) ERR; vlstr = malloc(10); *vlstr = '\0'; if (nc_put_var(ncid, varid, &vlstr)) ERR; strcpy(vlstr, "foo"); if (nc_put_var(ncid, varid, &vlstr)) ERR; free(vlstr); /* Write to a VL string attribute */ vlstr = NULL; if (nc_put_att(ncid, varid, VSTR_ATT1_NAME, NC_STRING, 1, &vlstr)) ERR; vlstr = malloc(10); *vlstr = '\0'; if (nc_put_att(ncid, varid, VSTR_ATT1_NAME, NC_STRING, 1, &vlstr)) ERR; strcpy(vlstr, "foo"); if (nc_put_att(ncid, varid, VSTR_ATT1_NAME, NC_STRING, 1, &vlstr)) ERR; free(vlstr); /* Define a new VL string variable */ if (nc_def_var(ncid, VSTR_VAR2_NAME , NC_STRING, 0, NULL, &varid)) ERR; /* Write to the variable's fill-value */ vlstr = NULL; if (nc_put_att(ncid, varid, _FillValue, NC_STRING, 1, &vlstr)) ERR; vlstr = malloc(10); *vlstr = '\0'; if (nc_put_att(ncid, varid, _FillValue, NC_STRING, 1, &vlstr)) ERR; strcpy(vlstr, "foo"); if (nc_put_att(ncid, varid, _FillValue, NC_STRING, 1, &vlstr)) ERR; free(vlstr); if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
static void* _get_all_samples(hid_t gid_series, char* nam_series, uint32_t type, int nsamples) { void* data = NULL; hid_t id_data_set, dtyp_memory, g_sample, sz_dest; herr_t ec; int smpx ,len; void *data_prior = NULL, *data_cur = NULL; char name_sample[MAX_GROUP_NAME+1]; hdf5_api_ops_t* ops; ops = profile_factory(type); if (ops == NULL) { error("Failed to create operations for %s", acct_gather_profile_type_to_string(type)); return NULL; } data = (*(ops->init_job_series))(nsamples); if (data == NULL) { xfree(ops); error("Failed to get memory for combined data"); return NULL; } dtyp_memory = (*(ops->create_memory_datatype))(); if (dtyp_memory < 0) { xfree(ops); xfree(data); error("Failed to create %s memory datatype", acct_gather_profile_type_to_string(type)); return NULL; } for (smpx=0; smpx<nsamples; smpx++) { len = H5Lget_name_by_idx(gid_series, ".", H5_INDEX_NAME, H5_ITER_INC, smpx, name_sample, MAX_GROUP_NAME, H5P_DEFAULT); if (len<1 || len>MAX_GROUP_NAME) { error("Invalid group name %s", name_sample); continue; } g_sample = H5Gopen(gid_series, name_sample, H5P_DEFAULT); if (g_sample < 0) { info("Failed to open %s", name_sample); } id_data_set = H5Dopen(g_sample, get_data_set_name(name_sample), H5P_DEFAULT); if (id_data_set < 0) { H5Gclose(g_sample); error("Failed to open %s dataset", acct_gather_profile_type_to_string(type)); continue; } sz_dest = (*(ops->dataset_size))(); data_cur = xmalloc(sz_dest); if (data_cur == NULL) { H5Dclose(id_data_set); H5Gclose(g_sample); error("Failed to get memory for prior data"); continue; } ec = H5Dread(id_data_set, dtyp_memory, H5S_ALL, H5S_ALL, H5P_DEFAULT, data_cur); if (ec < 0) { xfree(data_cur); H5Dclose(id_data_set); H5Gclose(g_sample); error("Failed to read %s data", acct_gather_profile_type_to_string(type)); continue; } (*(ops->merge_step_series))(g_sample, data_prior, data_cur, data+(smpx)*sz_dest); xfree(data_prior); data_prior = data_cur; H5Dclose(id_data_set); H5Gclose(g_sample); } xfree(data_cur); H5Tclose(dtyp_memory); xfree(ops); return data; }
void H5Attribute::copy(const hid_t src, const hid_t dest, const std::string & name) { hid_t type, stype; hid_t space, sspace; char * data = 0; hsize_t size; hsize_t * dims = 0; hsize_t ndims; sspace = H5Aget_space(src); if (sspace < 0) { throw H5Exception(__LINE__, __FILE__, _("Cannot copy the attribute")); } space = H5Scopy(sspace); H5Sclose(sspace); stype = H5Aget_type(src); if (stype < 0) { H5Sclose(space); throw H5Exception(__LINE__, __FILE__, _("Cannot copy the attribute")); } type = H5Tcopy(stype); H5Tclose(stype); size = H5Tget_size(type); dims = new hsize_t[__SCILAB_HDF5_MAX_DIMS__]; ndims = H5Sget_simple_extent_dims(space, dims, 0); for (unsigned int i = 0; i < ndims; i++) { size *= dims[i]; } data = new char[size]; if (H5Aread(src, type, data) < 0) { H5Sclose(space); H5Tclose(type); delete[] dims; delete[] data; throw H5Exception(__LINE__, __FILE__, _("Cannot read attribute data.")); } try { hid_t attr = create(dest, name, type, type, space, space, data); H5Aclose(attr); H5Sclose(space); H5Tclose(type); delete[] dims; delete[] data; } catch (const H5Exception & /*e*/) { H5Sclose(space); H5Tclose(type); delete[] dims; delete[] data; throw; } }
PyObject *H5UIget_info( hid_t loc_id, const char *dset_name, char *byteorder) { hid_t dataset_id; int rank; hsize_t *dims; hid_t space_id; H5T_class_t class_id; H5T_order_t order; hid_t type_id; PyObject *t; int i; /* Open the dataset. */ if ( (dataset_id = H5Dopen( loc_id, dset_name, H5P_DEFAULT )) < 0 ) { Py_INCREF(Py_None); return Py_None; /* Not chunked, so return None */ } /* Get an identifier for the datatype. */ type_id = H5Dget_type( dataset_id ); /* Get the class. */ class_id = H5Tget_class( type_id ); /* Get the dataspace handle */ if ( (space_id = H5Dget_space( dataset_id )) < 0 ) goto out; /* Get rank */ if ( (rank = H5Sget_simple_extent_ndims( space_id )) < 0 ) goto out; /* Book resources for dims */ dims = (hsize_t *)malloc(rank * sizeof(hsize_t)); /* Get dimensions */ if ( H5Sget_simple_extent_dims( space_id, dims, NULL) < 0 ) goto out; /* Assign the dimensions to a tuple */ t = PyTuple_New(rank); for(i=0;i<rank;i++) { /* I don't know if I should increase the reference count for dims[i]! */ PyTuple_SetItem(t, i, PyLong_FromLong((long)dims[i])); } /* Release resources */ free(dims); /* Terminate access to the dataspace */ if ( H5Sclose( space_id ) < 0 ) goto out; /* Get the byteorder */ /* Only integer, float, time and enum classes can be byteordered */ if ((class_id == H5T_INTEGER) || (class_id == H5T_FLOAT) || (class_id == H5T_BITFIELD) || (class_id == H5T_TIME) || (class_id == H5T_ENUM)) { order = H5Tget_order( type_id ); if (order == H5T_ORDER_LE) strcpy(byteorder, "little"); else if (order == H5T_ORDER_BE) strcpy(byteorder, "big"); else { fprintf(stderr, "Error: unsupported byteorder: %d\n", order); goto out; } } else { strcpy(byteorder, "irrelevant"); } /* End access to the dataset */ H5Dclose( dataset_id ); /* Return the dimensions tuple */ return t; out: H5Tclose( type_id ); H5Dclose( dataset_id ); Py_INCREF(Py_None); return Py_None; /* Not chunked, so return None */ }
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/ void GOME_LV1_WR_H5_PCD( struct param_record param, short nr_pcd, const short *indx_pcd, const struct pcd_gome *pcd ) { register hsize_t ni, nr, nx, ny; unsigned short *usbuff; short *sbuff; float *rbuff; hid_t type_id; hid_t grp_id; hbool_t compress; hsize_t nrpix, dims[2]; struct glr1_gome *glr; struct cr1_gome *cld; /* * check number of PCD records */ if ( nr_pcd == 0 || indx_pcd == NULL || pcd == NULL ) return; /* * set HDF5 boolean variable for compression */ if ( param.flag_deflate == PARAM_SET ) compress = TRUE; else compress = FALSE; /* * open or create group Earth */ grp_id = NADC_OPEN_HDF5_Group( param.hdf_file_id, "/EARTH" ); if ( grp_id < 0 ) NADC_RETURN_ERROR( NADC_ERR_HDF_GRP, "/EARTH" ); (void) H5Gclose( grp_id ); /* * create group /EARTH/PCD */ grp_id = H5Gcreate( param.hdf_file_id, "/EARTH/PCD", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ); if ( grp_id < 0 ) NADC_RETURN_ERROR( NADC_ERR_HDF_GRP, "/EARTH/PCD" ); /* * +++++ create datasets in the /EARTH/PCD group */ dims[0] = (size_t) nr_pcd; /* * Write geolocation information */ type_id = H5Topen( param.hdf_file_id, "glr", H5P_DEFAULT ); glr = (struct glr1_gome *) malloc( dims[0] * sizeof( struct glr1_gome )); if ( glr == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "glr" ); for ( nr = 0; nr < dims[0]; nr++ ) (void) memcpy( glr+nr, &pcd[nr].glr, sizeof( struct glr1_gome ) ); NADC_WR_HDF5_Dataset( compress, grp_id, "glr", type_id, 1, dims, glr ); free( glr ); (void) H5Tclose( type_id ); if ( IS_ERR_STAT_FATAL ) NADC_RETURN_ERROR( NADC_ERR_HDF_WR, "glr" ); /* * Write geolocation information */ type_id = H5Topen( param.hdf_file_id, "cld", H5P_DEFAULT ); cld = (struct cr1_gome *) malloc( dims[0] * sizeof( struct cr1_gome )); if ( cld == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "cld" ); for ( nr = 0; nr < dims[0]; nr++ ) (void) memcpy( cld+nr, &pcd[nr].cld, sizeof( struct cr1_gome ) ); NADC_WR_HDF5_Dataset( compress, grp_id, "cld", type_id, 1, dims, cld ); free( cld ); (void) H5Tclose( type_id ); if ( IS_ERR_STAT_FATAL ) NADC_RETURN_ERROR( NADC_ERR_HDF_WR, "cld" ); /* * Dark current and Noise Correction Factor */ rbuff = (float *) malloc( dims[0] * sizeof( float )); if ( rbuff == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rbuff" ); for ( ny = 0; ny < dims[0]; ny++ ) rbuff[ny] = pcd[indx_pcd[ny]].dark_current; NADC_WR_HDF5_Dataset( compress, grp_id, "DarkCurrent", H5T_NATIVE_FLOAT, 1, dims, rbuff ); for ( ny = 0; ny < dims[0]; ny++ ) rbuff[ny] = pcd[indx_pcd[ny]].noise_factor; NADC_WR_HDF5_Dataset( compress, grp_id, "NoiseCorrection", H5T_NATIVE_FLOAT, 1, dims, rbuff ); /* * Plane of the Polarisation */ for ( ny = 0; ny < dims[0]; ny++ ) rbuff[ny] = pcd[indx_pcd[ny]].polar.chi; NADC_WR_HDF5_Dataset( compress, grp_id, "PolarisationPlaneAngle", H5T_NATIVE_FLOAT, 1, dims, rbuff ); free( rbuff ); /* * Indices */ sbuff = (short *) malloc( dims[0] * sizeof( short )); if ( sbuff == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "sbuff" ); for ( ny = 0; ny < dims[0]; ny++ ) sbuff[ny] = pcd[indx_pcd[ny]].indx_spec; NADC_WR_HDF5_Dataset( compress, grp_id, "SpectralCalibrationIndex", H5T_NATIVE_SHORT, 1, dims, sbuff ); for ( ny = 0; ny < dims[0]; ny++ ) sbuff[ny] = pcd[indx_pcd[ny]].indx_leak; NADC_WR_HDF5_Dataset( compress, grp_id, "LeakageCorrectionIndex", H5T_NATIVE_SHORT, 1, dims, sbuff ); free( sbuff ); /* * Polarisation parameters */ dims[0] = nr_pcd; dims[1] = NUM_POLAR_COEFFS; nrpix = dims[0] * dims[1]; rbuff = (float *) malloc( nrpix * sizeof( float )); if ( rbuff == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rbuff" ); for ( nr = ny = 0; ny < dims[0]; ny++ ) for ( nx = 0; nx < dims[1]; nx++ ) rbuff[nr++] = pcd[indx_pcd[ny]].polar.wv[nx]; NADC_WR_HDF5_Dataset( compress, grp_id, "PolarisationWavelength", H5T_NATIVE_FLOAT, 2, dims, rbuff ); for ( nr = ny = 0; ny < dims[0]; ny++ ) for ( nx = 0; nx < dims[1]; nx++ ) rbuff[nr++] = pcd[indx_pcd[ny]].polar.coeff[nx]; NADC_WR_HDF5_Dataset( compress, grp_id, "PolarisationCoefficient", H5T_NATIVE_FLOAT, 2, dims, rbuff ); for ( nr = ny = 0; ny < dims[0]; ny++ ) for ( nx = 0; nx < dims[1]; nx++ ) rbuff[nr++] = pcd[indx_pcd[ny]].polar.error[nx]; NADC_WR_HDF5_Dataset( compress, grp_id, "PolarisationError", H5T_NATIVE_FLOAT, 2, dims, rbuff ); free( rbuff ); /* * ------------------------- Instrument Header Structure * Sub set counter */ usbuff = (unsigned short *) malloc( dims[0] * sizeof( short )); if ( usbuff == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "usbuff" ); for ( ny = 0; ny < dims[0]; ny++ ) usbuff[ny] = pcd[indx_pcd[ny]].ihr.subsetcounter; NADC_WR_HDF5_Dataset( compress, grp_id, "SubSetCounter", H5T_NATIVE_USHORT, 1, dims, usbuff ); /* * Average Mode */ for ( ny = 0; ny < dims[0]; ny++ ) usbuff[ny] = pcd[indx_pcd[ny]].ihr.averagemode; NADC_WR_HDF5_Dataset( compress, grp_id, "AverageMode", H5T_NATIVE_USHORT, 1, dims, usbuff ); free( usbuff ); /* * Peltier values */ dims[0] = nr_pcd; dims[1] = SCIENCE_CHANNELS; nrpix = dims[0] * dims[1]; sbuff = (short *) malloc( nrpix * sizeof( short )); if ( sbuff == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "sbuff" ); for ( nr = ny = 0; ny < dims[0]; ny++) for ( nx = 0; nx < dims[1]; nx++ ) sbuff[nr++] = pcd[indx_pcd[ny]].ihr.peltier[nx]; NADC_WR_HDF5_Dataset( compress, grp_id, "Peltier", H5T_NATIVE_SHORT, 2, dims, sbuff ); free( sbuff ); /* * Pre-disperser prism temperature */ rbuff = (float *) malloc( dims[0] * sizeof( float )); if ( rbuff == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rbuff" ); for ( ny = 0; ny < dims[0]; ny++ ) rbuff[ny] = (float) (-1.721 + 6.104e-3 * pcd[indx_pcd[ny]].ihr.prism_temp); NADC_WR_HDF5_Dataset( compress, grp_id, "PrismTemperature", H5T_NATIVE_FLOAT, 1, dims, rbuff ); free( rbuff ); (void) H5Gclose( grp_id ); /* * create group /EARTH/PCD */ grp_id = H5Gcreate( param.hdf_file_id, "/EARTH/PMD", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT ); if ( grp_id < 0 ) NADC_RETURN_ERROR( NADC_ERR_HDF_GRP, "/EARTH/PMD" ); /* * write PMD geolocations (only for Earth observations) */ dims[0] = nr_pcd * PMD_IN_GRID; if ( param.write_pmd_geo == PARAM_SET ) { type_id = H5Topen( param.hdf_file_id, "glr", H5P_DEFAULT ); glr = (struct glr1_gome *) malloc( dims[0] * sizeof( struct glr1_gome )); if ( glr == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "glr" ); for ( nr = ny = 0; ny < (hsize_t) nr_pcd; ny++ ) { for ( nx = 0; nx < PMD_IN_GRID; nx++ ) (void) memcpy( &glr[nr++], &pcd[indx_pcd[ny]].pmd[nx].glr, sizeof( struct glr1_gome ) ); } NADC_WR_HDF5_Dataset( compress, grp_id, "glr", type_id, 1, dims, glr ); free( glr ); (void) H5Tclose( type_id ); if ( IS_ERR_STAT_FATAL ) NADC_RETURN_ERROR( NADC_ERR_HDF_WR, "glr" ); } /* * write PMD values */ dims[1] = PMD_NUMBER; nrpix = (size_t) (dims[0] * dims[1]); rbuff = (float *) malloc( nrpix * sizeof( float )); if ( rbuff == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "rbuff" ); ni = 0; nr = 0; do { ny = 0; do { nx = 0; do { rbuff[ni++] = pcd[indx_pcd[nr]].pmd[ny].value[nx]; } while ( ++nx < PMD_NUMBER ); } while ( ++ny < PMD_IN_GRID ); } while ( ++nr < (hsize_t) nr_pcd ); NADC_WR_HDF5_Dataset( compress, grp_id, "value", H5T_NATIVE_FLOAT, 2, dims, rbuff ); free( rbuff ); /* * close interface */ (void) H5Gclose( grp_id ); }
int main( void ) { typedef struct Particle { char name[16]; int lati; int longi; float pressure; double temperature; } Particle; Particle dst_buf[ NRECORDS + NRECORDS_INS ]; /* Define an array of Particles */ Particle p_data[NRECORDS] = { {"zero",0,0, 0.0f, 0.0}, {"one",10,10, 1.0f, 10.0}, {"two", 20,20, 2.0f, 20.0}, {"three",30,30, 3.0f, 30.0}, {"four", 40,40, 4.0f, 40.0}, {"five", 50,50, 5.0f, 50.0}, {"six", 60,60, 6.0f, 60.0}, {"seven",70,70, 7.0f, 70.0} }; /* Calculate the size and the offsets of our struct members in memory */ size_t dst_size = sizeof( Particle ); size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ), HOFFSET( Particle, lati ), HOFFSET( Particle, longi ), HOFFSET( Particle, pressure ), HOFFSET( Particle, temperature )}; size_t dst_sizes[NFIELDS] = { sizeof( p_data[0].name), sizeof( p_data[0].lati), sizeof( p_data[0].longi), sizeof( p_data[0].pressure), sizeof( p_data[0].temperature)}; /* Define an array of Particles to insert */ Particle p_data_insert[NRECORDS_INS] = { {"new",30,30, 3.0f, 30.0}, {"new",40,40, 4.0f, 40.0} }; /* Define field information */ const char *field_names[NFIELDS] = { "Name","Latitude", "Longitude", "Pressure", "Temperature" }; hid_t field_type[NFIELDS]; hid_t string_type; hid_t file_id; hsize_t chunk_size = 10; int compress = 0; int *fill_data = NULL; hsize_t start; /* Record to start reading */ hsize_t nrecords; /* Number of records to insert/delete */ herr_t status; hsize_t nfields_out; hsize_t nrecords_out; int i; /* Initialize the field field_type */ string_type = H5Tcopy( H5T_C_S1 ); H5Tset_size( string_type, 16 ); field_type[0] = string_type; field_type[1] = H5T_NATIVE_INT; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_FLOAT; field_type[4] = H5T_NATIVE_DOUBLE; /* Create a new file using default properties. */ file_id = H5Fcreate( "ex_table_08.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /* Make the table */ status=H5TBmake_table( "Table Title",file_id,TABLE_NAME,NFIELDS,NRECORDS, dst_size,field_names, dst_offset, field_type, chunk_size, fill_data, compress, p_data ); /* Insert records */ start = 3; nrecords = NRECORDS_INS; status=H5TBinsert_record( file_id, TABLE_NAME, start, nrecords, dst_size, dst_offset, dst_sizes, p_data_insert ); /* read the table */ status=H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf ); /* get table info */ status=H5TBget_table_info(file_id,TABLE_NAME, &nfields_out, &nrecords_out ); /* print */ printf ("Table has %d fields and %d records\n",(int)nfields_out,(int)nrecords_out); /* print it by rows */ for (i=0; i<nrecords_out; i++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[i].name, dst_buf[i].lati, dst_buf[i].longi, dst_buf[i].pressure, dst_buf[i].temperature); printf ("\n"); } /* close type */ H5Tclose( string_type ); /* close the file */ H5Fclose( file_id ); return 0; }
/*------------------------------------------------------------------------- * Function: dataset_stats * * Purpose: Gather statistics about the dataset * * Return: Success: 0 * * Failure: -1 * * Programmer: Quincey Koziol * Tuesday, August 16, 2005 * *------------------------------------------------------------------------- */ static herr_t dataset_stats(iter_t *iter, const char *name, const H5O_info_t *oi) { unsigned bin; /* "bin" the number of objects falls in */ hid_t did; /* Dataset ID */ hid_t sid; /* Dataspace ID */ hid_t tid; /* Datatype ID */ hid_t dcpl; /* Dataset creation property list ID */ hsize_t dims[H5S_MAX_RANK];/* Dimensions of dataset */ H5D_layout_t lout; /* Layout of dataset */ unsigned type_found; /* Whether the dataset's datatype was */ /* already found */ int ndims; /* Number of dimensions of dataset */ hsize_t storage; /* Size of dataset storage */ unsigned u; /* Local index variable */ int num_ext; /* Number of external files for a dataset */ int nfltr; /* Number of filters for a dataset */ H5Z_filter_t fltr; /* Filter identifier */ herr_t ret; /* Gather statistics about this type of object */ iter->uniq_dsets++; /* Get object header information */ iter->dset_ohdr_info.total_size += oi->hdr.space.total; iter->dset_ohdr_info.free_size += oi->hdr.space.free; did = H5Dopen2(iter->fid, name, H5P_DEFAULT); HDassert(did > 0); /* Update dataset metadata info */ iter->datasets_index_storage_size += oi->meta_size.obj.index_size; iter->datasets_heap_storage_size += oi->meta_size.obj.heap_size; /* Update attribute metadata info */ ret = attribute_stats(iter, oi); HDassert(ret >= 0); /* Get storage info */ storage = H5Dget_storage_size(did); /* Gather layout statistics */ dcpl = H5Dget_create_plist(did); HDassert(dcpl > 0); lout = H5Pget_layout(dcpl); HDassert(lout >= 0); /* Object header's total size for H5D_COMPACT layout includes raw data size */ /* "storage" also includes H5D_COMPACT raw data size */ if(lout == H5D_COMPACT) iter->dset_ohdr_info.total_size -= storage; /* Track the layout type for dataset */ (iter->dset_layouts[lout])++; /* Get the number of external files for the dataset */ num_ext = H5Pget_external_count(dcpl); assert (num_ext >= 0); /* Accumulate raw data size accordingly */ if(num_ext) { iter->nexternal += (unsigned long)num_ext; iter->dset_external_storage_size += (unsigned long)storage; } else iter->dset_storage_size += storage; /* Gather dataspace statistics */ sid = H5Dget_space(did); HDassert(sid > 0); ndims = H5Sget_simple_extent_dims(sid, dims, NULL); HDassert(ndims >= 0); /* Check for larger rank of dataset */ if((unsigned)ndims > iter->max_dset_rank) iter->max_dset_rank = (unsigned)ndims; /* Track the number of datasets with each rank */ (iter->dset_rank_count[ndims])++; /* Only gather dim size statistics on 1-D datasets */ if(ndims == 1) { iter->max_dset_dims = dims[0]; if(dims[0] < SIZE_SMALL_DSETS) (iter->small_dset_dims[(size_t)dims[0]])++; /* Add dim count to proper bin */ bin = ceil_log10((unsigned long)dims[0]); if((bin + 1) > iter->dset_dim_nbins) { /* Allocate more storage for info about dataset's datatype */ iter->dset_dim_bins = (unsigned long *)HDrealloc(iter->dset_dim_bins, (bin + 1) * sizeof(unsigned long)); HDassert(iter->dset_dim_bins); /* Initialize counts for intermediate bins */ while(iter->dset_dim_nbins < bin) iter->dset_dim_bins[iter->dset_dim_nbins++] = 0; iter->dset_dim_nbins++; /* Initialize count for this bin */ iter->dset_dim_bins[bin] = 1; } /* end if */ else (iter->dset_dim_bins[bin])++; } /* end if */ ret = H5Sclose(sid); HDassert(ret >= 0); /* Gather datatype statistics */ tid = H5Dget_type(did); HDassert(tid > 0); type_found = FALSE; for(u = 0; u < iter->dset_ntypes; u++) if(H5Tequal(iter->dset_type_info[u].tid, tid) > 0) { type_found = TRUE; break; } /* end for */ if(type_found) (iter->dset_type_info[u].count)++; else { unsigned curr_ntype = iter->dset_ntypes; /* Increment # of datatypes seen for datasets */ iter->dset_ntypes++; /* Allocate more storage for info about dataset's datatype */ iter->dset_type_info = (dtype_info_t *)HDrealloc(iter->dset_type_info, iter->dset_ntypes * sizeof(dtype_info_t)); HDassert(iter->dset_type_info); /* Initialize information about datatype */ iter->dset_type_info[curr_ntype].tid = H5Tcopy(tid); HDassert(iter->dset_type_info[curr_ntype].tid > 0); iter->dset_type_info[curr_ntype].count = 1; iter->dset_type_info[curr_ntype].named = 0; /* Set index for later */ u = curr_ntype; } /* end else */ /* Check if the datatype is a named datatype */ if(H5Tcommitted(tid) > 0) (iter->dset_type_info[u].named)++; ret = H5Tclose(tid); HDassert(ret >= 0); /* Track different filters */ if((nfltr = H5Pget_nfilters(dcpl)) >= 0) { if(nfltr == 0) iter->dset_comptype[0]++; for(u = 0; u < (unsigned)nfltr; u++) { fltr = H5Pget_filter2(dcpl, u, 0, 0, 0, 0, 0, NULL); if(fltr >= 0) { if(fltr < (H5_NFILTERS_IMPL - 1)) iter->dset_comptype[fltr]++; else iter->dset_comptype[H5_NFILTERS_IMPL - 1]++; /*other filters*/ } /* end if */ } /* end for */ } /* endif nfltr */ ret = H5Pclose(dcpl); HDassert(ret >= 0); ret = H5Dclose(did); HDassert(ret >= 0); return 0; } /* end dataset_stats() */
/*+++++++++++++++++++++++++ .IDENTifer PYTABLE_append_array .PURPOSE append data to HDF5 dataset, extending the dimension ''extdim'' .INPUT/OUTPUT call as stat = PYTABLE_append_array( locID, dset_name, extdim, count, buffer ); input: hid_t locID : HDF5 identifier of file or group char *dset_name : name of dataset int extdim : dimension to extend int count : number of arrays to write void *buffer : data to write .RETURNS A negative value is returned on failure. .COMMENTS none -------------------------*/ herr_t PYTABLE_append_array( hid_t locID, const char *dset_name, int extdim, int count, const void *buffer ) { int rank; hid_t dataID; hid_t spaceID = -1; hid_t mem_spaceID = -1; hid_t typeID = -1; hsize_t *dims = NULL; hsize_t *dims_ext = NULL; hsize_t *offset = NULL; herr_t stat; /* open the dataset. */ if ( (dataID = H5Dopen( locID, dset_name, H5P_DEFAULT )) < 0 ) return -1; /* get the dataspace handle */ if ( (spaceID = H5Dget_space( dataID )) < 0 ) goto done; /* get rank */ if ( (rank = H5Sget_simple_extent_ndims( spaceID )) < 0 ) goto done; /* get dimensions */ dims = (hsize_t *) malloc( rank * sizeof(hsize_t) ); dims_ext = (hsize_t *) malloc( rank * sizeof(hsize_t) ); offset = (hsize_t *) calloc( rank, sizeof(hsize_t) ); if ( H5Sget_simple_extent_dims( spaceID, dims, NULL ) < 0 ) goto done; offset[extdim] = dims[extdim]; (void) memcpy( dims_ext, dims, rank * sizeof(hsize_t) ); dims_ext[extdim] = count; dims[extdim] += count; /* terminate access to the dataspace */ if ( H5Sclose( spaceID ) < 0 ) goto done; /* extend the dataset */ if ( H5Dset_extent( dataID, dims ) < 0 ) goto done; /* select a hyperslab */ if ( (spaceID = H5Dget_space( dataID )) < 0 ) goto done; stat = H5Sselect_hyperslab( spaceID, H5S_SELECT_SET, offset, NULL, dims_ext, NULL ); free( dims ); free( offset ); if ( stat < 0 ) goto done; /* define memory space */ if ( (mem_spaceID = H5Screate_simple( rank, dims_ext, NULL )) < 0 ) goto done; free( dims_ext ); /* get an identifier for the datatype. */ if ( (typeID = H5Dget_type( dataID )) < 0 ) goto done; /* write the data to the hyperslab */ stat = H5Dwrite( dataID, typeID, mem_spaceID, spaceID, H5P_DEFAULT, buffer ); if ( stat < 0 ) goto done; /* end access to the dataset */ if ( H5Dclose( dataID ) ) goto done; /* terminate access to the datatype */ if ( H5Tclose( typeID ) < 0 ) goto done; /* terminate access to the dataspace */ if ( H5Sclose( mem_spaceID ) < 0 ) goto done; if ( H5Sclose( spaceID ) < 0 ) goto done; return 0; done: if ( dims != 0 ) free( dims ); if ( dims_ext != 0 ) free( dims_ext ); if ( offset != 0 ) free( offset ); if ( typeID > 0 ) (void) H5Tclose( typeID ); if ( spaceID > 0 ) (void) H5Sclose( spaceID ); if ( mem_spaceID > 0 ) (void) H5Sclose( mem_spaceID ); if ( dataID > 0 ) (void) H5Dclose( dataID ); return -1; }
void BAGDataset::LoadMetadata() { /* -------------------------------------------------------------------- */ /* Load the metadata from the file. */ /* -------------------------------------------------------------------- */ hid_t hMDDS = H5Dopen( hHDF5, "/BAG_root/metadata" ); hid_t datatype = H5Dget_type( hMDDS ); hid_t dataspace = H5Dget_space( hMDDS ); hid_t native = H5Tget_native_type( datatype, H5T_DIR_ASCEND ); hsize_t dims[3], maxdims[3]; H5Sget_simple_extent_dims( dataspace, dims, maxdims ); pszXMLMetadata = (char *) CPLCalloc((int) (dims[0]+1),1); H5Dread( hMDDS, native, H5S_ALL, dataspace, H5P_DEFAULT, pszXMLMetadata ); H5Tclose( native ); H5Sclose( dataspace ); H5Tclose( datatype ); H5Dclose( hMDDS ); if( strlen(pszXMLMetadata) == 0 ) return; /* -------------------------------------------------------------------- */ /* Try to get the geotransform. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psRoot = CPLParseXMLString( pszXMLMetadata ); if( psRoot == NULL ) return; CPLStripXMLNamespace( psRoot, NULL, TRUE ); CPLXMLNode *psGeo = CPLSearchXMLNode( psRoot, "=MD_Georectified" ); if( psGeo != NULL ) { char **papszCornerTokens = CSLTokenizeStringComplex( CPLGetXMLValue( psGeo, "cornerPoints.Point.coordinates", "" ), " ,", FALSE, FALSE ); if( CSLCount(papszCornerTokens ) == 4 ) { double dfLLX = atof( papszCornerTokens[0] ); double dfLLY = atof( papszCornerTokens[1] ); double dfURX = atof( papszCornerTokens[2] ); double dfURY = atof( papszCornerTokens[3] ); adfGeoTransform[0] = dfLLX; adfGeoTransform[1] = (dfURX - dfLLX) / (GetRasterXSize()-1); adfGeoTransform[3] = dfURY; adfGeoTransform[5] = (dfLLY - dfURY) / (GetRasterYSize()-1); adfGeoTransform[0] -= adfGeoTransform[1] * 0.5; adfGeoTransform[3] -= adfGeoTransform[5] * 0.5; } CSLDestroy( papszCornerTokens ); } /* -------------------------------------------------------------------- */ /* Try to get the coordinate system. */ /* -------------------------------------------------------------------- */ OGRSpatialReference oSRS; if( OGR_SRS_ImportFromISO19115( &oSRS, pszXMLMetadata ) == OGRERR_NONE ) { oSRS.exportToWkt( &pszProjection ); } else { ParseWKTFromXML( pszXMLMetadata ); } /* -------------------------------------------------------------------- */ /* Fetch acquisition date. */ /* -------------------------------------------------------------------- */ CPLXMLNode *psDateTime = CPLSearchXMLNode( psRoot, "=dateTime" ); if( psDateTime != NULL ) { const char *pszDateTimeValue = CPLGetXMLValue( psDateTime, NULL, "" ); if( pszDateTimeValue ) SetMetadataItem( "BAG_DATETIME", pszDateTimeValue ); } CPLDestroyXMLNode( psRoot ); }
/* * test_strpad * Tests string padding for a UTF-8 string. * Converts strings to shorter and then longer strings. * Borrows heavily from dtypes.c, but is more complicated because * the string is randomly generated. */ void test_strpad(hid_t H5_ATTR_UNUSED fid, const char *string) { /* buf is used to hold the data that H5Tconvert operates on. */ char buf[LONG_BUF_SIZE]; /* cmpbuf holds the output that H5Tconvert should produce, * to compare against the actual output. */ char cmpbuf[LONG_BUF_SIZE]; /* new_string is a slightly modified version of the UTF-8 * string to make the tests run more smoothly. */ char new_string[MAX_STRING_LENGTH + 2]; size_t length; /* Length of new_string in bytes */ size_t small_len; /* Size of the small datatype */ size_t big_len; /* Size of the larger datatype */ hid_t src_type, dst_type; herr_t ret; /* The following tests are simpler if the UTF-8 string contains * the right number of bytes (even or odd, depending on the test). * We create a 'new_string' whose length is convenient by prepending * an 'x' to 'string' when necessary. */ length = HDstrlen(string); if(length % 2 != 1) { HDstrcpy(new_string, "x"); HDstrcat(new_string, string); length++; } else { HDstrcpy(new_string, string); } /* Convert a null-terminated string to a shorter and longer null * terminated string. */ /* Create a src_type that holds the UTF-8 string and its final NULL */ big_len = length + 1; /* +1 byte for final NULL */ HDassert((2*big_len)<=sizeof(cmpbuf)); src_type = mkstr(big_len, H5T_STR_NULLTERM); CHECK(src_type, FAIL, "mkstr"); /* Create a dst_type that holds half of the UTF-8 string and a final * NULL */ small_len = (length + 1) / 2; dst_type = mkstr(small_len, H5T_STR_NULLTERM); CHECK(dst_type, FAIL, "mkstr"); /* Fill the buffer with two copies of the UTF-8 string, each with a * terminating NULL. It will look like "abcdefg\0abcdefg\0". */ strncpy(buf, new_string, big_len); strncpy(&buf[big_len], new_string, big_len); ret = H5Tconvert(src_type, dst_type, (size_t)2, buf, NULL, H5P_DEFAULT); CHECK(ret, FAIL, "H5Tconvert"); /* After conversion, the buffer should look like * "abc\0abc\0abcdefg\0". Note that this is just what the bytes look * like; UTF-8 characters may well have been truncated. * To check that the conversion worked properly, we'll build this * string manually. */ HDstrncpy(cmpbuf, new_string, small_len - 1); cmpbuf[small_len - 1] = '\0'; HDstrncpy(&cmpbuf[small_len], new_string, small_len -1); cmpbuf[2 * small_len - 1] = '\0'; HDstrcpy(&cmpbuf[2 * small_len], new_string); VERIFY(HDmemcmp(buf, cmpbuf, 2*big_len), 0, "HDmemcmp"); /* Now convert from smaller datatype to bigger datatype. This should * leave our buffer looking like: "abc\0\0\0\0\0abc\0\0\0\0\0" */ ret = H5Tconvert(dst_type, src_type, (size_t)2, buf, NULL, H5P_DEFAULT); CHECK(ret, FAIL, "H5Tconvert"); /* First fill the buffer with NULLs */ HDmemset(cmpbuf, '\0', (size_t)LONG_BUF_SIZE); /* Copy in the characters */ HDstrncpy(cmpbuf, new_string, small_len -1); HDstrncpy(&cmpbuf[big_len], new_string, small_len -1); VERIFY(HDmemcmp(buf, cmpbuf, 2*big_len), 0, "HDmemcmp"); ret = H5Tclose(src_type); CHECK(ret, FAIL, "H5Tclose"); ret = H5Tclose(dst_type); CHECK(ret, FAIL, "H5Tclose"); /* Now test null padding. Null-padded strings do *not* need * terminating NULLs, so the sizes of the datatypes are slightly * different and we want a string with an even number of characters. */ length = HDstrlen(string); if(length % 2 != 0) { HDstrcpy(new_string, "x"); HDstrcat(new_string, string); length++; } else { HDstrcpy(new_string, string); } /* Create a src_type that holds the UTF-8 string */ big_len = length; HDassert((2*big_len)<=sizeof(cmpbuf)); src_type = mkstr(big_len, H5T_STR_NULLPAD); CHECK(src_type, FAIL, "mkstr"); /* Create a dst_type that holds half of the UTF-8 string */ small_len = length / 2; dst_type = mkstr(small_len, H5T_STR_NULLPAD); CHECK(dst_type, FAIL, "mkstr"); /* Fill the buffer with two copies of the UTF-8 string. * It will look like "abcdefghabcdefgh". */ strncpy(buf, new_string, big_len); strncpy(&buf[big_len], new_string, big_len); ret = H5Tconvert(src_type, dst_type, (size_t)2, buf, NULL, H5P_DEFAULT); CHECK(ret, FAIL, "H5Tconvert"); /* After conversion, the buffer should look like * "abcdabcdabcdefgh". Note that this is just what the bytes look * like; UTF-8 characters may well have been truncated. * To check that the conversion worked properly, we'll build this * string manually. */ HDstrncpy(cmpbuf, new_string, small_len); HDstrncpy(&cmpbuf[small_len], new_string, small_len); HDstrncpy(&cmpbuf[2 * small_len], new_string, big_len); VERIFY(HDmemcmp(buf, cmpbuf, 2*big_len), 0, "HDmemcmp"); /* Now convert from smaller datatype to bigger datatype. This should * leave our buffer looking like: "abcd\0\0\0\0abcd\0\0\0\0" */ ret = H5Tconvert(dst_type, src_type, (size_t)2, buf, NULL, H5P_DEFAULT); CHECK(ret, FAIL, "H5Tconvert"); /* First fill the buffer with NULLs */ HDmemset(cmpbuf, '\0', (size_t)LONG_BUF_SIZE); /* Copy in the characters */ HDstrncpy(cmpbuf, new_string, small_len); HDstrncpy(&cmpbuf[big_len], new_string, small_len); VERIFY(HDmemcmp(buf, cmpbuf, 2*big_len), 0, "HDmemcmp"); ret = H5Tclose(src_type); CHECK(ret, FAIL, "H5Tclose"); ret = H5Tclose(dst_type); CHECK(ret, FAIL, "H5Tclose"); /* Test space padding. This is very similar to null-padding; we can use the same values of length, small_len, and big_len. */ src_type = mkstr(big_len, H5T_STR_SPACEPAD); CHECK(src_type, FAIL, "mkstr"); dst_type = mkstr(small_len, H5T_STR_SPACEPAD); CHECK(src_type, FAIL, "mkstr"); /* Fill the buffer with two copies of the UTF-8 string. * It will look like "abcdefghabcdefgh". */ HDstrcpy(buf, new_string); HDstrcpy(&buf[big_len], new_string); ret = H5Tconvert(src_type, dst_type, (size_t)2, buf, NULL, H5P_DEFAULT); CHECK(ret, FAIL, "H5Tconvert"); /* After conversion, the buffer should look like * "abcdabcdabcdefgh". Note that this is just what the bytes look * like; UTF-8 characters may have been truncated. * To check that the conversion worked properly, we'll build this * string manually. */ HDstrncpy(cmpbuf, new_string, small_len); HDstrncpy(&cmpbuf[small_len], new_string, small_len); HDstrncpy(&cmpbuf[2 * small_len], new_string, big_len); VERIFY(HDmemcmp(buf, cmpbuf, 2*big_len), 0, "HDmemcmp"); /* Now convert from smaller datatype to bigger datatype. This should * leave our buffer looking like: "abcd abcd " */ ret = H5Tconvert(dst_type, src_type, (size_t)2, buf, NULL, H5P_DEFAULT); CHECK(ret, FAIL, "H5Tconvert"); /* First fill the buffer with spaces */ HDmemset(cmpbuf, ' ', (size_t)LONG_BUF_SIZE); /* Copy in the characters */ HDstrncpy(cmpbuf, new_string, small_len); HDstrncpy(&cmpbuf[big_len], new_string, small_len); VERIFY(HDmemcmp(buf, cmpbuf, 2*big_len), 0, "HDmemcmp"); ret = H5Tclose(src_type); CHECK(ret, FAIL, "H5Tclose"); ret = H5Tclose(dst_type); CHECK(ret, FAIL, "H5Tclose"); }
/*------------------------------------------------------------------------- * test_big_table * * Ensures that a FL packet table will not break when many (BIG_TABLE_SIZE) * packets are used. * *------------------------------------------------------------------------- */ static int test_big_table(hid_t fid) { herr_t err; hid_t table; hid_t part_t; size_t c; particle_t readPart; hsize_t count; TESTING("large packet table"); /* Create a datatype for the particle struct */ part_t = make_particle_type(); HDassert(part_t != -1); /* Create a new table */ table = H5PTcreate_fl(fid, "Packet Test Dataset2", part_t, (hsize_t)33, -1); H5Tclose(part_t); if( H5PTis_valid(table) < 0) goto out; /* Add many particles */ for(c = 0; c < BIG_TABLE_SIZE ; c+=8) { /* Append eight particles at once*/ err = H5PTappend(table, (size_t)8, &(testPart[0])); if( err < 0) goto out; } /* Count the number of packets in the table */ err = H5PTget_num_packets(table, &count); if( err < 0) goto out; if( count != BIG_TABLE_SIZE ) goto out; /* Read particles to ensure that all of them were written correctly */ /* Also, ensure that H5PTcreate_fl set the current packet to */ /* the first packet in the table */ for(c = 0; c < BIG_TABLE_SIZE; c++) { err = H5PTget_next(table, 1, &readPart); if(err < 0) goto out; /* Ensure that particles were read correctly */ if( cmp_par(c % 8, 0, testPart, &readPart) != 0) goto out; } /* Close the table */ err = H5PTclose(table); if( err < 0) goto out; PASSED(); return 0; out: H5_FAILED(); if( H5PTis_valid(table) < 0) H5PTclose(table); return -1; }
/* * test_compound * Test that compound datatypes can have UTF-8 field names. */ void test_compound(hid_t fid, const char * string) { /* Define two compound structures, s1_t and s2_t. * s2_t is a subset of s1_t, with two out of three * fields. * This is stolen from the h5_compound example. */ typedef struct s1_t { int a; double c; float b; } s1_t; typedef struct s2_t { double c; int a; } s2_t; /* Actual variable declarations */ s1_t s1; s2_t s2; hid_t s1_tid, s2_tid; hid_t space_id, dset_id; hsize_t dim = 1; char *readbuf; herr_t ret; /* Initialize compound data */ HDmemset(&s1, 0, sizeof(s1_t)); /* To make purify happy */ s1.a = COMP_INT_VAL; s1.c = COMP_DOUBLE_VAL; s1.b = COMP_FLOAT_VAL; /* Create compound datatypes using UTF-8 field name */ s1_tid = H5Tcreate (H5T_COMPOUND, sizeof(s1_t)); CHECK(s1_tid, FAIL, "H5Tcreate"); ret = H5Tinsert(s1_tid, string, HOFFSET(s1_t, a), H5T_NATIVE_INT); CHECK(ret, FAIL, "H5Tinsert"); /* Check that the field name was stored correctly */ readbuf = H5Tget_member_name(s1_tid, 0); ret = HDstrcmp(readbuf, string); VERIFY(ret, 0, "strcmp"); H5free_memory(readbuf); /* Add the other fields to the datatype */ ret = H5Tinsert(s1_tid, "c_name", HOFFSET(s1_t, c), H5T_NATIVE_DOUBLE); CHECK(ret, FAIL, "H5Tinsert"); ret = H5Tinsert(s1_tid, "b_name", HOFFSET(s1_t, b), H5T_NATIVE_FLOAT); CHECK(ret, FAIL, "H5Tinsert"); /* Create second datatype, with only two fields. */ s2_tid = H5Tcreate (H5T_COMPOUND, sizeof(s2_t)); CHECK(s2_tid, FAIL, "H5Tcreate"); ret = H5Tinsert(s2_tid, "c_name", HOFFSET(s2_t, c), H5T_NATIVE_DOUBLE); CHECK(ret, FAIL, "H5Tinsert"); ret = H5Tinsert(s2_tid, string, HOFFSET(s2_t, a), H5T_NATIVE_INT); CHECK(ret, FAIL, "H5Tinsert"); /* Create the dataspace and dataset. */ space_id = H5Screate_simple(1, &dim, NULL); CHECK(space_id, FAIL, "H5Screate_simple"); dset_id = H5Dcreate2(fid, DSET4_NAME, s1_tid, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); CHECK(dset_id, FAIL, "H5Dcreate2"); /* Write data to the dataset. */ ret = H5Dwrite(dset_id, s1_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &s1); CHECK(ret, FAIL, "H5Dwrite"); /* Ensure that data can be read back by field name into s2 struct */ ret = H5Dread(dset_id, s2_tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, &s2); CHECK(ret, FAIL, "H5Dread"); VERIFY(s2.a, COMP_INT_VAL, "H5Dread"); VERIFY(s2.c, COMP_DOUBLE_VAL, "H5Dread"); /* Clean up */ ret = H5Tclose(s1_tid); CHECK(ret, FAIL, "H5Tclose"); ret = H5Tclose(s2_tid); CHECK(ret, FAIL, "H5Tclose"); ret = H5Sclose(space_id); CHECK(ret, FAIL, "H5Sclose"); ret = H5Dclose(dset_id); CHECK(ret, FAIL, "H5Dclose"); }
/*------------------------------------------------------------------------- * test_compress * * Ensures that a FL packet table can be compressed. * This test creates a file named TEST_COMPRESS_FILE * *------------------------------------------------------------------------- */ static int test_compress(void) { hid_t fid1 = -1; herr_t err; hid_t table = -1; hid_t part_t = -1; hid_t dset_id = -1; hid_t plist_id = -1; size_t c; size_t num_elems = 1; unsigned filter_vals[1]; particle_t readPart[1]; hsize_t count; TESTING("packet table compression"); /* Create a file. */ if((fid1 = H5Fcreate(TEST_COMPRESS_FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; /* Create a datatype for the particle struct */ part_t = make_particle_type(); HDassert(part_t != -1); /* Create a new table with compression level 8 */ table = H5PTcreate_fl(fid1, "Compressed Test Dataset", part_t, (hsize_t)80, 8); if( H5PTis_valid(table) < 0) TEST_ERROR; /* We can now use this table exactly the same way we use a normal uncompressed * packet table, and it should pass the same tests. */ /* Add many particles */ for(c = 0; c < BIG_TABLE_SIZE ; c+=8) { /* Append eight particles at once*/ err = H5PTappend(table, (size_t)8, &(testPart[0])); if( err < 0) TEST_ERROR; } /* Count the number of packets in the table */ err = H5PTget_num_packets(table, &count); if( err < 0) TEST_ERROR; if( count != BIG_TABLE_SIZE ) TEST_ERROR; /* Read particles to ensure that all of them were written correctly */ HDmemset(readPart, 0, sizeof(readPart)); for(c = 0; c < BIG_TABLE_SIZE; c++) { err = H5PTget_next(table, 1, readPart); if(err < 0) TEST_ERROR; /* Ensure that particles were read correctly */ if( cmp_par(c % 8, 0, testPart, readPart) != 0) TEST_ERROR; } /* Close the table */ err = H5PTclose(table); if( err < 0) TEST_ERROR; /* Open the packet table as a regular dataset and make sure that the * compression filter is set. */ dset_id = H5Dopen2(fid1, "Compressed Test Dataset", H5P_DEFAULT); if( dset_id < 0) TEST_ERROR; plist_id = H5Dget_create_plist(dset_id); if( plist_id < 0) TEST_ERROR; err = H5Pget_filter_by_id2(plist_id, H5Z_FILTER_DEFLATE, NULL, &num_elems, filter_vals, 0, NULL, NULL); if( err < 0) TEST_ERROR; /* The compression level should be 8, the value we passed in */ if(filter_vals[0] != 8) TEST_ERROR; /* Clean up */ err = H5Pclose(plist_id); if( err < 0) TEST_ERROR; err = H5Dclose(dset_id); if( err < 0) TEST_ERROR; /* Create a new table without compression. */ table = H5PTcreate_fl(fid1, "Uncompressed Dataset", part_t, (hsize_t)80, -1); if(table < 0) TEST_ERROR; /* Close the packet table */ err = H5PTclose(table); if( err < 0) TEST_ERROR; /* Open the packet table as a regular dataset and make sure that the * compression filter is not set. */ dset_id = H5Dopen2(fid1, "Uncompressed Dataset", H5P_DEFAULT); if( dset_id < 0) TEST_ERROR; plist_id = H5Dget_create_plist(dset_id); if( plist_id < 0) TEST_ERROR; H5E_BEGIN_TRY { err = H5Pget_filter_by_id2(plist_id, H5Z_FILTER_DEFLATE, NULL, &num_elems, filter_vals, 0, NULL, NULL); if( err >= 0) TEST_ERROR; } H5E_END_TRY /* Clean up */ err = H5Pclose(plist_id); if( err < 0) TEST_ERROR; err = H5Dclose(dset_id); if( err < 0) TEST_ERROR; /* Close the datatype and the file */ err = H5Tclose(part_t); if( err < 0) TEST_ERROR; err = H5Fclose(fid1); if( err < 0) TEST_ERROR; PASSED(); return 0; error: H5E_BEGIN_TRY { H5Pclose(plist_id); H5Dclose(dset_id); H5Tclose(part_t); H5PTclose(table); H5Fclose(fid1); } H5E_END_TRY H5_FAILED(); return -1; }
estatus_t e5_read_data_info_list( eid_t e5_group_id, const char* list_name, e5_data_info* info_list) { int i; int d; int log_scale; int close_group; estatus_t status; hsize_t h5_min_dim[3]; hsize_t h5_max_dim[3]; eid_t e5_list_group_id; eid_t e5_type_id; eid_t e5_dataset_id; eid_t e5_dataspace_id; status = E5_SUCCESS; if(list_name && strlen(list_name)) { e5_list_group_id = e5_create_group(e5_group_id, list_name); close_group = 1; } else { e5_list_group_id = e5_group_id; close_group = 0; } for(i = 0; info_list && info_list[i].name != 0; i++) { e5_data_info* info = &info_list[i]; e5_dataset_id = H5Dopen(e5_list_group_id, info->name); if (e5_dataset_id < 0) { status = E5_INVALID_DATASET; e5_error(e5_list_group_id, status, "Failed to open info for dataset '%s'\n", info->name); return status; } e5_dataspace_id = H5Dget_space(e5_dataset_id); e5_type_id = H5Dget_type(e5_dataset_id); H5Sget_simple_extent_dims(e5_dataspace_id, h5_min_dim, h5_max_dim); info->type = e5_convert_hdf_type(e5_type_id); for(d = 0; d < 3; d++) { info->dim[d] = h5_min_dim[d] >= h5_max_dim[d] ? h5_min_dim[d] : h5_max_dim[d]; info->dim[d] = info->dim[d] < 1 ? 1 : info->dim[d]; } log_scale = 0; if(e5_is_valid_attr(e5_group_id, "log10")) e5_read_attr_int(e5_dataset_id, "log10", &log_scale); info->scale = log_scale ? E5_VALUE_SCALE_LOG10 : E5_VALUE_SCALE_LINEAR; e5_info(e5_group_id, "Read data info [type='%s', name='%s', dim='%u %u %u']\n", e5_typename(info->type), info->name, info->dim[0], info->dim[1], info->dim[2]); H5Sclose(e5_dataspace_id); H5Dclose(e5_dataset_id); H5Tclose(e5_type_id); } if(close_group) e5_close_group(e5_list_group_id); return E5_SUCCESS; }
herr_t HDF5AttrIterate( hid_t hH5ObjID, const char *AttrName, void *pDS ) { hid_t hAttrID; hid_t hAttrTypeID; hid_t hAttrNativeType; hid_t hAttrSpace; char *szData = NULL; hsize_t nSize[64]; unsigned int nAttrElmts; hsize_t nAttrSize; hsize_t i; void *buf = NULL; unsigned int nAttrDims; HDF5Dataset *poDS; char *szTemp = (char*) CPLMalloc( 8192 ); char *szValue = NULL; poDS = (HDF5Dataset *) pDS; sprintf( szTemp, "%s:%s", poDS->poH5CurrentObject->pszName, AttrName ); hAttrID = H5Aopen_name( hH5ObjID, AttrName ); hAttrTypeID = H5Aget_type( hAttrID ); hAttrNativeType = H5Tget_native_type( hAttrTypeID, H5T_DIR_DEFAULT ); hAttrSpace = H5Aget_space( hAttrID ); nAttrDims = H5Sget_simple_extent_dims( hAttrSpace, nSize, NULL ); if( H5Tget_class( hAttrNativeType ) == H5T_STRING ) { nAttrSize = H5Aget_storage_size( hAttrID ); szData = (char*) CPLMalloc((size_t) (nAttrSize+1)); szValue = (char*) CPLMalloc((size_t) (nAttrSize+1)); H5Aread( hAttrID, hAttrNativeType, szData ); szData[nAttrSize]='\0'; sprintf( szValue, "%s", szData ); } else { nAttrElmts = 1; for( i=0; i < nAttrDims; i++ ) { nAttrElmts *= (int) nSize[i]; } if( nAttrElmts > 0 ){ buf = (void *) CPLMalloc( nAttrElmts* H5Tget_size( hAttrNativeType )); szData = (char*) CPLMalloc( 8192 ); szValue = (char*) CPLMalloc( MAX_METADATA_LEN ); szData[0]='\0'; szValue[0] ='\0'; H5Aread( hAttrID, hAttrNativeType, buf ); } if( H5Tequal( H5T_NATIVE_CHAR, hAttrNativeType ) ){ for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c ", ((char *) buf)[i]); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UCHAR, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%c", ((char *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_SHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((short *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_USHORT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned short *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_INT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%d ", ((int *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_UINT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ud ", ((unsigned int *) buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_LONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((long *)buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_ULONG, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%ld ", ((unsigned long *)buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_FLOAT, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%f ", ((float *)buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } else if( H5Tequal( H5T_NATIVE_DOUBLE, hAttrNativeType ) ) { for( i=0; i < nAttrElmts; i++ ) { sprintf( szData, "%g ", ((double *)buf)[i] ); if( CPLStrlcat(szValue,szData,MAX_METADATA_LEN) >= MAX_METADATA_LEN ) CPLError( CE_Warning, CPLE_OutOfMemory, "Header data too long. Truncated\n"); } } CPLFree( buf ); } H5Sclose(hAttrSpace); H5Tclose(hAttrNativeType); H5Tclose(hAttrTypeID); H5Aclose( hAttrID ); //printf( "%s = %s\n",szTemp, szValue ); poDS->papszMetadata = CSLSetNameValue( poDS->papszMetadata, szTemp, CPLSPrintf( "%s", szValue ) ); CPLFree( szTemp ); CPLFree( szData ); CPLFree( szValue ); return 0; }
int main(void) { /* handles */ hid_t file, dataset; hid_t datatype, dataspace; hid_t memspace; herr_t status; H5T_class_t t_class; /* data type class */ H5T_order_t order; /* data order */ size_t size; /* size of the data element * stored in the file */ hsize_t dimsm[3]; /* memory space dimensions */ hsize_t dims_out[2]; /* dataset dimensions */ int data_out[NX][NY][NZ]; /* output buffer */ hsize_t count[2]; /* size of the hyperslab in the file */ hsize_t offset[2]; /* hyperslab offset in the file */ hsize_t count_out[3]; /* size of the hyperslab in memory */ hsize_t offset_out[3]; /* hyperslab offset in memory */ int i,j,k; int status_n; int rank; for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) { for (k = 0; k < NZ; k++) { data_out[j][i][k] = 0; } } } /* * Open the file and the dataset. */ file = H5Fopen(H5FILE_NAME, H5F_ACC_RDONLY, H5P_DEFAULT); dataset = H5Dopen(file, DATASET_NAME); /* * Get datatype and dataspace handles and then query * dataset class, order, size, rank and dimensions. */ datatype = H5Dget_type(dataset); /* datatype handle */ t_class = H5Tget_class(datatype); if (t_class == H5T_INTEGER) printf("Dataset has INTEGER type\n"); order = H5Tget_order(datatype); if (order == H5T_ORDER_LE) printf("Little endian order\n"); size = H5Tget_size(datatype); printf("Data size is %d\n", (int)size); dataspace = H5Dget_space(dataset); /* dataspace handle */ rank = H5Sget_simple_extent_ndims(dataspace); status_n = H5Sget_simple_extent_dims(dataspace, dims_out, NULL); printf("rank %d, dimensions %lu x %lu\n", rank, (unsigned long)(dims_out[0]), (unsigned long)(dims_out[1])); /* * Define hyperslab in the dataset. */ offset[0] = 1; offset[1] = 2; count[0] = NX_SUB; count[1] = NY_SUB; status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, offset, NULL, count, NULL); /* * Define the memory dataspace. */ dimsm[0] = NX; dimsm[1] = NY; dimsm[2] = NZ; memspace = H5Screate_simple(RANK_OUT, dimsm, NULL); /* * Define memory hyperslab */ offset_out[0] = 3; offset_out[1] = 0; offset_out[2] = 0; count_out[0] = NX_SUB; count_out[1] = NY_SUB; count_out[2] = 1; status = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, offset_out, NULL, count_out, NULL); /* * Read data from hyperslab in the file into the hyperslab * in memory and display it. */ status = H5Dread(dataset, H5T_NATIVE_INT, memspace, dataspace, H5P_DEFAULT, data_out); for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) { printf("%d ", data_out[j][i][0]); } printf("\n"); } /* Result: * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 0 0 0 0 0 0 0 * 3 4 5 6 0 0 0 * 4 5 6 7 0 0 0 * 5 6 7 8 0 0 0 * 0 0 0 0 0 0 0 */ /* * Close/release resources. */ H5Tclose(datatype); H5Dclose(dataset); H5Sclose(dataspace); H5Sclose(memspace); H5Fclose(file); return 0; }
int main (void) { hid_t file, dataset; /* file and dataset handles */ hid_t dataspace, datatype; /* handles */ hid_t attr; /* attribute identifiers */ herr_t status; int wdata = 45; /* * 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); /* * Describe the size of the array and create the data space for fixed * size dataset. */ dataspace = H5Screate(H5S_SCALAR); /* * Define datatype for the data in the file. */ datatype = H5Tcopy(H5T_NATIVE_INT); /* * Create a new dataset within the file using defined dataspace and * datatype and default dataset creation properties. */ dataset = H5Dcreate2(file, "scalar", datatype, dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Write the data to the dataset using default transfer properties. */ status = H5Dwrite(dataset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, &wdata); /* * Create scalar attributes. */ attr = H5Acreate2(dataset, "value", H5T_NATIVE_INT, dataspace, H5P_DEFAULT, H5P_DEFAULT); status = H5Awrite(attr, H5T_NATIVE_INT, &wdata); /* * Close attributes. */ H5Aclose(attr); /* * Close/release resources. */ H5Sclose(dataspace); H5Tclose(datatype); H5Dclose(dataset); H5Fclose(file); return 0; }
void hdfFileSaver::saveNow(void) { QDir prnt; herr_t status; hsize_t maxdims[1]; hsize_t d_dims[1]; int rank = 1; hsize_t datasetlength; size_t memdims[1]; hsize_t count[1]; /* size of subset in the file */ hsize_t offset[1]; /* subset offset in the file */ hsize_t mcount[1]; hsize_t moffset[1]; hid_t prop; if (events==0) return; if (access(fname.toStdString().c_str(), F_OK) ==-1) { hfile = H5Fcreate( fname.toStdString().c_str(), H5F_ACC_EXCL , H5P_DEFAULT, H5P_DEFAULT ) ; topgroup = H5Gcreate( hfile, "iqdata_raw", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); hid_t aid3 = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 4); hid_t attr3 = H5Acreate1(topgroup, "type", atype, aid3, H5P_DEFAULT); status = H5Awrite(attr3, atype,"dict"); H5Aclose(attr3); H5Tclose(atype); H5Sclose(aid3); } else { //printf("File already exists, will append\n"); hfile = H5Fopen( fname.toStdString().c_str(), H5F_ACC_RDWR, H5P_DEFAULT ); topgroup = H5Gopen(hfile, "iqdata_raw", H5P_DEFAULT); //fprintf(stderr,"Bad Hdf file already existant, cannot open\n"); } if (hfile <=0 || topgroup <=0) { fprintf(stderr,"Bad Hdf file, cannot open\n"); return; } if (true) { //QHash<int,QHash<QString,QList<float> > > events; QList<int> keys1=(*events).keys(); for (int i=0;i<keys1.length();i++) { int chan = keys1[i]; if ((*events)[chan]["bin"].length() > 0) { int bin = (int)((*events)[chan]["bin"][0]); QString dirname = QString("keyint_%1").arg(chan); //turn off errors when we query the group, using open hid_t error_stack = H5Eget_current_stack(); H5E_auto2_t oldfunc; void *old_client_data; H5Eget_auto(error_stack, &oldfunc, &old_client_data); H5Eset_auto(error_stack, NULL, NULL); channelgroup = H5Gopen( topgroup, dirname.toStdString().c_str(), H5P_DEFAULT); //turn errors back on. H5Eset_auto(error_stack, oldfunc, old_client_data); if (channelgroup<0) { channelgroup = H5Gcreate( topgroup, dirname.toStdString().c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); hid_t aid3 = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 4); hid_t attr3 = H5Acreate1(channelgroup, "type", atype, aid3, H5P_DEFAULT); status = H5Awrite(attr3, atype,"dict"); H5Aclose(attr3); H5Tclose(atype); H5Sclose(aid3); } if (channelgroup!=0) { QList<QString> keys2=(*events)[keys1[i]].keys(); for (int i2=0;i2<keys2.length();i2++) { QString dname = QString("keystr_%1").arg(keys2[i2]); datasetlength = (*events)[keys1[i]][keys2[i2]].length(); if (datasetlength>0) { //Try to open dataset if it exists //turn off errors when we query the group, using open hid_t error_stack = H5Eget_current_stack(); H5E_auto2_t oldfunc; void *old_client_data; H5Eget_auto(error_stack, &oldfunc, &old_client_data); H5Eset_auto(error_stack, NULL, NULL); //query or open dataset curdataset = H5Dopen(channelgroup,dname.toStdString().c_str(),H5P_DEFAULT); //turn errors back on. H5Eset_auto(error_stack, oldfunc, old_client_data); //if cannot open dataset, create it, and make it chunked. if (curdataset<=0) { //set up size info, chunks etc... maxdims[0]=H5S_UNLIMITED; rank = 1; d_dims[0]=datasetlength; curdataspace= H5Screate_simple(rank, d_dims,maxdims); prop = H5Pcreate(H5P_DATASET_CREATE); status = H5Pset_chunk(prop, rank, d_dims); if (status) trap(); curdataset = H5Dcreate( channelgroup, dname.toStdString().c_str(), H5T_NATIVE_FLOAT, curdataspace, H5P_DEFAULT, prop, H5P_DEFAULT); hid_t aid3 = H5Screate(H5S_SCALAR); hid_t atype = H5Tcopy(H5T_C_S1); H5Tset_size(atype, 5); hid_t attr3 = H5Acreate1(curdataset, "type", atype, aid3, H5P_DEFAULT); status = H5Awrite(attr3, atype,"array"); H5Aclose(attr3); H5Tclose(atype); H5Sclose(aid3); H5Pclose(prop); }//if (curdataset<=0) else { //get dataspace from exant dataset curdataspace = H5Dget_space(curdataset); H5Sget_simple_extent_dims(curdataspace, d_dims, maxdims); H5Sclose(curdataspace); //if dataset already exist, extend its size d_dims[0] = d_dims[0] + datasetlength; status = H5Dset_extent(curdataset, d_dims); if (status) trap(); curdataspace = H5Dget_space(curdataset); H5Sget_simple_extent_dims(curdataspace, d_dims, maxdims); }//else if (curdataset>0) { // // Set up stride, offset, count block in dastasets... // // stride[0]=1;//hop size between floats in dataset... //block[0]=1;//get 1 slab count[0]=datasetlength;//size of hyperslab or chunk of data offset[0]=d_dims[0] - datasetlength;//offset from beginning of file. status = H5Sselect_hyperslab( curdataspace, H5S_SELECT_SET, offset, NULL, count, NULL); if (status) trap(); rank = 1; memdims[0]=datasetlength; memdataspace= H5Screate_simple(rank, (const hsize_t*)memdims,NULL); mcount[0] = datasetlength; moffset[0] = 0; status = H5Sselect_hyperslab( memdataspace, H5S_SELECT_SET, moffset, NULL, mcount, NULL); if (status) trap(); for (int mmm=0;mmm<datasetlength;mmm++) this->rawdata[mmm]=(*events)[keys1[i]][keys2[i2]][mmm]; status = H5Dwrite( curdataset, H5T_NATIVE_FLOAT, memdataspace, curdataspace, H5P_DEFAULT, this->rawdata); if (status) trap(); H5Sclose(memdataspace); H5Sclose(curdataspace); H5Dclose(curdataset); } //if (curdataset>0) } }//for (int i2=0;i2<keys2.length();i2++) status = H5Gclose(channelgroup); }//if (channelgroup!=0) }//if ((*events)[chan]["bin"].length() > 0) }//for (int i=0;i<keys1.length();i++) }//if (true) status = H5Gclose(topgroup); status = H5Fclose(hfile); }
void pyne::Material::write_hdf5(std::string filename, std::string datapath, std::string nucpath, float row, int chunksize) { int row_num = (int) row; // Turn off annoying HDF5 errors H5Eset_auto2(H5E_DEFAULT, NULL, NULL); //Set file access properties so it closes cleanly hid_t fapl; fapl = H5Pcreate(H5P_FILE_ACCESS); H5Pset_fclose_degree(fapl,H5F_CLOSE_STRONG); // Create new/open datafile. hid_t db; if (pyne::file_exists(filename)) { bool ish5 = H5Fis_hdf5(filename.c_str()); if (!ish5) throw h5wrap::FileNotHDF5(filename); db = H5Fopen(filename.c_str(), H5F_ACC_RDWR, fapl); } else db = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, fapl); // // Read in nuclist if available, write it out if not // bool nucpath_exists = h5wrap::path_exists(db, nucpath); std::vector<int> nuclides; int nuc_size; hsize_t nuc_dims[1]; if (nucpath_exists) { nuclides = h5wrap::h5_array_to_cpp_vector_1d<int>(db, nucpath, H5T_NATIVE_INT); nuc_size = nuclides.size(); nuc_dims[0] = nuc_size; } else { nuclides = std::vector<int>(); for (pyne::comp_iter i = comp.begin(); i != comp.end(); i++) nuclides.push_back(i->first); nuc_size = nuclides.size(); // Create the data if it doesn't exist int nuc_data [nuc_size]; for (int n = 0; n != nuc_size; n++) nuc_data[n] = nuclides[n]; nuc_dims[0] = nuc_size; hid_t nuc_space = H5Screate_simple(1, nuc_dims, NULL); hid_t nuc_set = H5Dcreate2(db, nucpath.c_str(), H5T_NATIVE_INT, nuc_space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); H5Dwrite(nuc_set, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, nuc_data); H5Fflush(db, H5F_SCOPE_GLOBAL); }; // // Write out the data itself to the file // hid_t data_set, data_space, data_hyperslab; int data_rank = 1; hsize_t data_dims[1] = {1}; hsize_t data_max_dims[1] = {H5S_UNLIMITED}; hsize_t data_offset[1] = {0}; size_t material_struct_size = sizeof(pyne::material_struct) + sizeof(double)*nuc_size; hid_t desc = H5Tcreate(H5T_COMPOUND, material_struct_size); hid_t comp_values_array_type = H5Tarray_create2(H5T_NATIVE_DOUBLE, 1, nuc_dims); // make the data table type H5Tinsert(desc, "mass", HOFFSET(pyne::material_struct, mass), H5T_NATIVE_DOUBLE); H5Tinsert(desc, "density", HOFFSET(pyne::material_struct, density), H5T_NATIVE_DOUBLE); H5Tinsert(desc, "atoms_per_molecule", HOFFSET(pyne::material_struct, atoms_per_mol), H5T_NATIVE_DOUBLE); H5Tinsert(desc, "comp", HOFFSET(pyne::material_struct, comp), comp_values_array_type); material_struct * mat_data = new material_struct[material_struct_size]; (*mat_data).mass = mass; (*mat_data).density = density; (*mat_data).atoms_per_mol = atoms_per_molecule; for (int n = 0; n != nuc_size; n++) { if (0 < comp.count(nuclides[n])) (*mat_data).comp[n] = comp[nuclides[n]]; else (*mat_data).comp[n] = 0.0; }; // get / make the data set bool datapath_exists = h5wrap::path_exists(db, datapath); if (datapath_exists) { data_set = H5Dopen2(db, datapath.c_str(), H5P_DEFAULT); data_space = H5Dget_space(data_set); data_rank = H5Sget_simple_extent_dims(data_space, data_dims, data_max_dims); // Determine the row size. if (std::signbit(row)) row_num = data_dims[0] + row; // careful, row is negative if (data_dims[0] <= row_num) { // row == -0, extend to data set so that we can append, or // row_num is larger than current dimension, resize to accomodate. data_dims[0] = row_num + 1; H5Dset_extent(data_set, data_dims); } data_offset[0] = row_num; } else { // Get full space data_space = H5Screate_simple(1, data_dims, data_max_dims); // Make data set properties to enable chunking hid_t data_set_params = H5Pcreate(H5P_DATASET_CREATE); hsize_t chunk_dims[1] ={chunksize}; H5Pset_chunk(data_set_params, 1, chunk_dims); H5Pset_deflate(data_set_params, 1); material_struct * data_fill_value = new material_struct[material_struct_size]; (*data_fill_value).mass = -1.0; (*data_fill_value).density= -1.0; (*data_fill_value).atoms_per_mol = -1.0; for (int n = 0; n != nuc_size; n++) (*data_fill_value).comp[n] = 0.0; H5Pset_fill_value(data_set_params, desc, &data_fill_value); // Create the data set data_set = H5Dcreate2(db, datapath.c_str(), desc, data_space, H5P_DEFAULT, data_set_params, H5P_DEFAULT); H5Dset_extent(data_set, data_dims); // Add attribute pointing to nuc path hid_t nuc_attr_type = H5Tcopy(H5T_C_S1); H5Tset_size(nuc_attr_type, nucpath.length()); hid_t nuc_attr_space = H5Screate(H5S_SCALAR); hid_t nuc_attr = H5Acreate2(data_set, "nucpath", nuc_attr_type, nuc_attr_space, H5P_DEFAULT, H5P_DEFAULT); H5Awrite(nuc_attr, nuc_attr_type, nucpath.c_str()); H5Fflush(db, H5F_SCOPE_GLOBAL); // Remember to de-allocate delete[] data_fill_value; }; // Get the data hyperslab data_hyperslab = H5Dget_space(data_set); hsize_t data_count[1] = {1}; H5Sselect_hyperslab(data_hyperslab, H5S_SELECT_SET, data_offset, NULL, data_count, NULL); // Get a memory space for writing hid_t mem_space = H5Screate_simple(1, data_count, data_max_dims); // Write the row... H5Dwrite(data_set, desc, mem_space, data_hyperslab, H5P_DEFAULT, mat_data); // Close out the Dataset H5Fflush(db, H5F_SCOPE_GLOBAL); H5Dclose(data_set); H5Sclose(data_space); H5Tclose(desc); // // Write out the metadata to the file // std::string attrpath = datapath + "_metadata"; hid_t metadatapace, attrtype, metadataet, metadatalab, attrmemspace; int attrrank; attrtype = H5Tvlen_create(H5T_NATIVE_CHAR); // get / make the data set bool attrpath_exists = h5wrap::path_exists(db, attrpath); if (attrpath_exists) { metadataet = H5Dopen2(db, attrpath.c_str(), H5P_DEFAULT); metadatapace = H5Dget_space(metadataet); attrrank = H5Sget_simple_extent_dims(metadatapace, data_dims, data_max_dims); if (data_dims[0] <= row_num) { // row == -0, extend to data set so that we can append, or // row_num is larger than current dimension, resize to accomodate. data_dims[0] = row_num + 1; H5Dset_extent(metadataet, data_dims); } data_offset[0] = row_num; } else { hid_t metadataetparams; hsize_t attrchunkdims [1]; // Make data set properties to enable chunking metadataetparams = H5Pcreate(H5P_DATASET_CREATE); attrchunkdims[0] = chunksize; H5Pset_chunk(metadataetparams, 1, attrchunkdims); H5Pset_deflate(metadataetparams, 1); hvl_t attrfillvalue [1]; attrfillvalue[0].len = 3; attrfillvalue[0].p = (char *) "{}\n"; H5Pset_fill_value(metadataetparams, attrtype, &attrfillvalue); // make dataset metadatapace = H5Screate_simple(1, data_dims, data_max_dims); metadataet = H5Dcreate2(db, attrpath.c_str(), attrtype, metadatapace, H5P_DEFAULT, metadataetparams, H5P_DEFAULT); H5Dset_extent(metadataet, data_dims); }; // set the attr string hvl_t attrdata [1]; Json::FastWriter writer; std::string metadatatr = writer.write(metadata); attrdata[0].p = (char *) metadatatr.c_str(); attrdata[0].len = metadatatr.length(); // write the attr metadatalab = H5Dget_space(metadataet); H5Sselect_hyperslab(metadatalab, H5S_SELECT_SET, data_offset, NULL, data_count, NULL); attrmemspace = H5Screate_simple(1, data_count, data_max_dims); H5Dwrite(metadataet, attrtype, attrmemspace, metadatalab, H5P_DEFAULT, attrdata); // close attr data objects H5Fflush(db, H5F_SCOPE_GLOBAL); H5Dclose(metadataet); H5Sclose(metadatapace); H5Tclose(attrtype); // Close out the HDF5 file H5Fclose(db); // Remember the milk! // ...by which I mean to deallocate delete[] mat_data; };