int main() { printf("\n*** Checking HDF5 dimscales detach.\n"); printf("*** Creating a file with two vars with one dimension scale..."); { hid_t fileid, grpid, spaceid, var1_id, var2_id, dimscaleid, cparmsid; hid_t fcpl_id, fapl_id, create_propid, access_propid; hsize_t dims[NDIMS] = {DIM_LEN}; char dimscale_wo_var[STR_LEN]; float data = 42; /* Create file. */ 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_18, H5F_LIBVER_18) < 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; if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; /* Create dimension scale. */ if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED| H5P_CRT_ORDER_INDEXED) < 0) ERR; if ((spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; if ((dimscaleid = H5Dcreate1(grpid, DIMSCALE_NAME, H5T_IEEE_F32BE, spaceid, create_propid)) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; if (H5Pclose(create_propid) < 0) ERR; sprintf(dimscale_wo_var, "%s%10d", DIM_WITHOUT_VARIABLE, DIM_LEN); if (H5DSset_scale(dimscaleid, dimscale_wo_var) < 0) ERR; /* Create a variable that uses this dimension scale. */ if ((access_propid = H5Pcreate(H5P_DATASET_ACCESS)) < 0) ERR; if (H5Pset_chunk_cache(access_propid, CHUNK_CACHE_NELEMS, CHUNK_CACHE_SIZE, CHUNK_CACHE_PREEMPTION) < 0) ERR; if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_fill_value(create_propid, H5T_NATIVE_FLOAT, &data) < 0) ERR; if (H5Pset_layout(create_propid, H5D_CONTIGUOUS) < 0) ERR; if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED| H5P_CRT_ORDER_INDEXED) < 0) ERR; if ((spaceid = H5Screate_simple(NDIMS, dims, dims)) < 0) ERR; if ((var1_id = H5Dcreate2(grpid, VAR1_NAME, H5T_NATIVE_FLOAT, spaceid, H5P_DEFAULT, create_propid, access_propid)) < 0) ERR; if (H5Pclose(create_propid) < 0) ERR; if (H5Pclose(access_propid) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; if (H5DSattach_scale(var1_id, dimscaleid, 0) < 0) ERR; /* Create another variable that uses this dimension scale. */ if ((access_propid = H5Pcreate(H5P_DATASET_ACCESS)) < 0) ERR; if (H5Pset_chunk_cache(access_propid, CHUNK_CACHE_NELEMS, CHUNK_CACHE_SIZE, CHUNK_CACHE_PREEMPTION) < 0) ERR; if ((create_propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_fill_value(create_propid, H5T_NATIVE_FLOAT, &data) < 0) ERR; if (H5Pset_layout(create_propid, H5D_CONTIGUOUS) < 0) ERR; if (H5Pset_attr_creation_order(create_propid, H5P_CRT_ORDER_TRACKED| H5P_CRT_ORDER_INDEXED) < 0) ERR; if ((spaceid = H5Screate_simple(NDIMS, dims, dims)) < 0) ERR; if ((var2_id = H5Dcreate2(grpid, VAR2_NAME, H5T_NATIVE_FLOAT, spaceid, H5P_DEFAULT, create_propid, access_propid)) < 0) ERR; if (H5Pclose(create_propid) < 0) ERR; if (H5Pclose(access_propid) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; if (H5DSattach_scale(var2_id, dimscaleid, 0) < 0) ERR; /* Now detach the scales and remove the dimscale. This doesn't * work if I reverse the order of the statements. */ if (H5DSdetach_scale(var2_id, dimscaleid, 0) < 0) ERR; if (H5DSdetach_scale(var1_id, dimscaleid, 0) < 0) ERR; /* Fold up our tents. */ if (H5Dclose(var1_id) < 0) ERR; if (H5Dclose(dimscaleid) < 0) ERR; if (H5Gclose(grpid) < 0) ERR; if (H5Fclose(fileid) < 0) ERR; /* /\* Now read the file and check it. *\/ */ /* { */ /* hid_t fileid, spaceid = 0, datasetid = 0; */ /* hsize_t num_obj, i; */ /* int obj_class; */ /* char obj_name[STR_LEN + 1]; */ /* char dimscale_name[STR_LEN+1]; */ /* htri_t is_scale; */ /* char label[STR_LEN+1]; */ /* int num_scales; */ /* hsize_t dims[1], maxdims[1]; */ /* H5G_stat_t statbuf; */ /* HDF5_OBJID_T dimscale_obj, vars_dimscale_obj; */ /* struct nc_hdf5_link_info link_info; */ /* hsize_t idx = 0; */ /* /\* Open the file. *\/ */ /* if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; */ /* if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; */ /* /\* Loop through objects in the root group. *\/ */ /* if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; */ /* for (i = 0; i < num_obj; i++) */ /* { */ /* if (H5Literate(grpid, H5_INDEX_CRT_ORDER, H5_ITER_INC, */ /* &idx, visit_link, (void *)&link_info) < 0) ERR; */ /* printf("Encountered: HDF5 object link_info.name %s\n", link_info.name); */ /* /\* Deal with object based on its obj_class. *\/ */ /* switch(link_info.obj_type) */ /* { */ /* case H5I_GROUP: */ /* break; */ /* case H5I_DATASET: */ /* /\* Open the dataset. *\/ */ /* if ((datasetid = H5Dopen1(fileid, link_info.name)) < 0) ERR; */ /* if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; */ /* if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; */ /* if (maxdims[0] != DIM_LEN) ERR; */ /* if (H5Sclose(spaceid) < 0) ERR; */ /* /\* Is this a dimscale? *\/ */ /* if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; */ /* if (is_scale && strcmp(link_info.name, DIMSCALE_NAME)) ERR; */ /* if (is_scale) */ /* { */ /* /\* A dimscale comes with a NAME attribute, in */ /* * addition to its real name. *\/ */ /* if (H5DSget_scale_name(datasetid, dimscale_name, STR_LEN) < 0) ERR; */ /* if (strcmp(dimscale_name, dimscale_wo_var)) ERR; */ /* /\* fileno and objno uniquely identify an object and a */ /* * HDF5 file. *\/ */ /* if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; */ /* dimscale_obj.fileno[0] = statbuf.fileno[0]; */ /* dimscale_obj.objno[0] = statbuf.objno[0]; */ /* dimscale_obj.fileno[1] = statbuf.fileno[1]; */ /* dimscale_obj.objno[1] = statbuf.objno[1]; */ /* /\*printf("scale statbuf.fileno = %d statbuf.objno = %d\n", */ /* statbuf.fileno, statbuf.objno);*\/ */ /* } */ /* else */ /* { */ /* /\* Here's how to get the number of scales attached */ /* * to the dataset's dimension 0. *\/ */ /* if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; */ /* if (num_scales != 1) ERR; */ /* /\* Go through all dimscales for this var and learn about them. *\/ */ /* if (H5DSiterate_scales(datasetid, 0, NULL, alien_visitor, */ /* &vars_dimscale_obj) < 0) ERR; */ /* /\*printf("vars_dimscale_obj.fileno = %d vars_dimscale_obj.objno = %d\n", */ /* vars_dimscale_obj.fileno, vars_dimscale_obj.objno);*\/ */ /* /\* if (vars_dimscale_obj.fileno[0] != dimscale_obj.fileno[0] || *\/ */ /* /\* vars_dimscale_obj.objno[0] != dimscale_obj.objno[0] || *\/ */ /* /\* vars_dimscale_obj.fileno[1] != dimscale_obj.fileno[1] || *\/ */ /* /\* vars_dimscale_obj.objno[1] != dimscale_obj.objno[1]) ERR; *\/ */ /* /\* There's also a label for dimension 0. *\/ */ /* if (H5DSget_label(datasetid, 0, label, STR_LEN) < 0) ERR; */ /* /\*printf("found non-scale dataset %s, label %s\n", link_info.name, label);*\/ */ /* } */ /* if (H5Dclose(datasetid) < 0) ERR; */ /* break; */ /* case H5I_DATATYPE: */ /* break; */ /* default: */ /* printf("Unknown object!"); */ /* ERR; */ /* } */ /* } */ /* /\* Close up the shop. *\/ */ /* if (H5Fclose(fileid) < 0) ERR; */ /* }*/ } SUMMARIZE_ERR; FINAL_RESULTS; }
/* initializes hdf5 struct and writes some of the initially known problem properties such as state variable names into hdf5 file*/ hdf5block_t* /*freshly allocated struct with ids and size parameters*/ h5block_init(char *output_file, /*will create this file for writing*/ ode_model_parameters *omp, /*contains MCMC problem description*/ size_t Samples, /* MCMC sample size*/ const char **x_name, /*ODE model State Variable names, array of strings*/ const char **p_name, /*ODE model parameter names, array of strings*/ const char **f_name)/*ODE model output function names, array of strings*/ { hsize_t *size=malloc(sizeof(hsize_t)*2); hsize_t *chunk_size=malloc(sizeof(hsize_t)*2); hid_t file_id = H5Fcreate(output_file, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); assert(file_id>0); int D=get_number_of_MCMC_variables(omp); int N=get_number_of_state_variables(omp); int P=get_number_of_model_parameters(omp); int F=get_number_of_model_outputs(omp); char *x_names=flatten(x_name, (size_t) N, "; "); char *p_names=flatten(p_name, (size_t) P, "; "); char *f_names=flatten(f_name, (size_t) F, "; "); herr_t NameWriteError=0; NameWriteError&=H5LTmake_dataset_string(file_id,"StateVariableNames",x_names); NameWriteError&=H5LTmake_dataset_string(file_id,"ParameterNames",p_names); NameWriteError&=H5LTmake_dataset_string(file_id,"OutputFunctionNames",f_names); if (NameWriteError){ fprintf(stderr,"[h5block_init] writing (x,p,f)-names into hdf5 file failed."); }/* else { printf("# [main] all names have been written to file «%s».\n",output_file); }*/ free(x_names); free(p_names); free(f_names); hid_t para_property_id = H5Pcreate(H5P_DATASET_CREATE); hid_t post_property_id = H5Pcreate(H5P_DATASET_CREATE); /* here we set a «chunk size», which will coincide with the hyperslabs we select to write output*/ // parameter sample chunk size: chunk_size[0]=CHUNK; chunk_size[1]=D; H5Pset_chunk(para_property_id, 2, chunk_size); hid_t para_chunk_id=H5Screate_simple(2, chunk_size, NULL); // a dataspace to write chunks/hyperslabs // posterior probability distribution chunk size: chunk_size[0]=CHUNK; chunk_size[1]=1; H5Pset_chunk(post_property_id, 2, chunk_size); hid_t post_chunk_id=H5Screate_simple(2, chunk_size, NULL); // hyperslab selection hsize_t *offset, *stride, *count, *block; offset=malloc(sizeof(hsize_t)*2); stride=malloc(sizeof(hsize_t)*2); count=malloc(sizeof(hsize_t)*2); block=malloc(sizeof(hsize_t)*2); // hsize_t offset[2]={0,0}, stride[2]={1,1}, count[2]={1,1}, block[2]; int i; for (i=0;i<2;i++) offset[i]=0; for (i=0;i<2;i++) stride[i]=1; for (i=0;i<2;i++) count[i]=1; block[0]=CHUNK; block[1]=D; // hdf5 file setup size[0]=Samples; size[1]=D; hid_t para_dataspace_id=H5Screate_simple(2, size, NULL); size[0]=Samples; size[1]=1; hid_t post_dataspace_id=H5Screate_simple(2, size, NULL); assert(post_dataspace_id>0 && para_dataspace_id>0); hid_t parameter_set_id = H5Dcreate2(file_id, "LogParameters", H5T_NATIVE_DOUBLE, para_dataspace_id, H5P_DEFAULT, para_property_id, H5P_DEFAULT); hid_t posterior_set_id = H5Dcreate2(file_id, "LogPosterior", H5T_NATIVE_DOUBLE, post_dataspace_id, H5P_DEFAULT, post_property_id, H5P_DEFAULT); assert(parameter_set_id>0 && posterior_set_id>0); //copy ids to struct hdf5block_t *h5block; h5block = malloc(sizeof(hdf5block_t)); h5block->file_id = file_id; h5block->para_property_id = para_property_id; h5block->post_property_id = post_property_id; h5block->para_chunk_id = para_chunk_id; h5block->post_chunk_id = post_chunk_id; h5block->para_dataspace_id = para_dataspace_id; h5block->post_dataspace_id = post_dataspace_id; h5block->parameter_set_id = parameter_set_id; h5block->posterior_set_id = posterior_set_id; h5block->size = size; h5block->chunk_size = chunk_size; h5block->offset = offset; h5block->stride = stride; h5block->count = count; h5block->block = block; return h5block; }
int main( void ) { Particle dst_buf[NRECORDS]; /* 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( dst_buf[0].name), sizeof( dst_buf[0].lati), sizeof( dst_buf[0].longi), sizeof( dst_buf[0].pressure), sizeof( dst_buf[0].temperature)}; size_t field_offset_pos[2] = { HOFFSET( Position, lati ), HOFFSET( Position, longi )}; hid_t field_type[NFIELDS]; hid_t string_type; hid_t fileID; hsize_t chunk_size = 10; Particle fill_data[1] = { {"no data",-1,-1, -99.0f, -99.0} }; /* Fill value particle */ hsize_t start; /* Record to start reading/writing */ hsize_t nrecords; /* Number of records to read/write */ int compress = 0; int n; herr_t status; Particle *p_data = NULL; /* Initially no data */ float pressure_in [NRECORDS_ADD] = /* Define new values for the field "Pressure" */ { 0.0f,1.0f,2.0f}; Position position_in[NRECORDS_ADD] = {/* Define new values for "Latitude,Longitude" */ {0,0}, {10,10}, {20,20}}; NamePressure namepre_in[NRECORDS_ADD] =/* Define new values for "Name,Pressure" */ { {"zero",0.0f}, {"one", 1.0f}, {"two", 2.0f}, }; size_t field_sizes_pos[2]= { sizeof(position_in[0].longi), sizeof(position_in[0].lati) }; size_t field_sizes_pre[1]= { sizeof(namepre_in[0].pressure) }; /* 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. */ fileID = H5Fcreate ("h5_table_04.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /*__________________________________________________________________ Make the table */ status = H5TBmake_table ("Table Title", fileID, TABLE_NAME, NFIELDS, NRECORDS, dst_size, field_names, dst_offset, field_type, chunk_size, fill_data, compress, p_data); /*__________________________________________________________________ Write the pressure field starting at record 2. */ start = 2; nrecords = NRECORDS_ADD; status = H5TBwrite_fields_name (fileID, TABLE_NAME, "Pressure", start, nrecords, sizeof( float ), 0, field_sizes_pre, pressure_in); /*__________________________________________________________________ Write the new longitude and latitude information starting at record 2. */ start = 2; nrecords = NRECORDS_ADD; status = H5TBwrite_fields_name (fileID, TABLE_NAME, "Latitude,Longitude", start, nrecords, sizeof( Position ), field_offset_pos, field_sizes_pos, position_in); /*__________________________________________________________________ Read the table */ status = H5TBread_table (fileID, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf); /* print it by rows */ for (n=0; n<NRECORDS; n++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[n].name, dst_buf[n].lati, dst_buf[n].longi, dst_buf[n].pressure, dst_buf[n].temperature); printf ("\n"); } /*------------------------------------------------------------------------- * end *------------------------------------------------------------------------- */ /* close type */ H5Tclose( string_type ); /* close the file */ H5Fclose (fileID); return 0; }
/*------------------------------------------------------------------------- * Function: test_core * * Purpose: Tests the file handle interface for CORE driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * *------------------------------------------------------------------------- */ static herr_t test_core(void) { hid_t file=(-1), fapl, access_fapl = -1; char filename[1024]; void *fhandle=NULL; hsize_t file_size; int *points = NULL, *check = NULL, *p1, *p2; hid_t dset1=-1, space1=-1; hsize_t dims1[2]; int i, j, n; TESTING("CORE file driver"); /* Set property list and file name for CORE driver */ fapl = h5_fileaccess(); if(H5Pset_fapl_core(fapl, (size_t)CORE_INCREMENT, TRUE) < 0) TEST_ERROR; h5_fixname(FILENAME[1], fapl, filename, sizeof filename); if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* Check that the driver is correct */ if(H5FD_CORE != H5Pget_driver(access_fapl)) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, H5P_DEFAULT, &fhandle) < 0) TEST_ERROR; if(fhandle==NULL) { printf("fhandle==NULL\n"); TEST_ERROR; } /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* There is no garantee the size of metadata in file is constant. * Just try to check if it's reasonable. Why is this 4KB? */ if(file_size<2*KB || file_size>6*KB) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Open the file with backing store off for read and write. * Changes won't be saved in file. */ if(H5Pset_fapl_core(fapl, (size_t)CORE_INCREMENT, FALSE) < 0) TEST_ERROR; if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Allocate memory for data set. */ if(NULL == (points = (int *)HDmalloc(DSET1_DIM1 * DSET1_DIM2 * sizeof(int)))) TEST_ERROR; if(NULL == (check = (int *)HDmalloc(DSET1_DIM1 * DSET1_DIM2 * sizeof(int)))) TEST_ERROR; /* Initialize the dset1 */ p1 = points; for(i = n = 0; i < DSET1_DIM1; i++) for(j = 0; j < DSET1_DIM2; j++) *p1++ = n++; /* Create the data space1 */ dims1[0] = DSET1_DIM1; dims1[1] = DSET1_DIM2; if((space1 = H5Screate_simple(2, dims1, NULL)) < 0) TEST_ERROR; /* Create the dset1 */ if((dset1 = H5Dcreate2(file, DSET1_NAME, H5T_NATIVE_INT, space1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; /* Write the data to the dset1 */ if(H5Dwrite(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0) TEST_ERROR; if(H5Dclose(dset1) < 0) TEST_ERROR; if((dset1 = H5Dopen2(file, DSET1_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; /* Read the data back from dset1 */ if(H5Dread(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, check) < 0) TEST_ERROR; /* Check that the values read are the same as the values written */ p1 = points; p2 = check; for(i = 0; i < DSET1_DIM1; i++) for(j = 0; j < DSET1_DIM2; j++) if(*p1++ != *p2++) { H5_FAILED(); printf(" Read different values than written in data set 1.\n"); printf(" At index %d,%d\n", i, j); TEST_ERROR; } /* end if */ if(H5Dclose(dset1) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Open the file with backing store on for read and write. * Changes will be saved in file. */ if(H5Pset_fapl_core(fapl, (size_t)CORE_INCREMENT, TRUE) < 0) TEST_ERROR; if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Create the dset1 */ if((dset1 = H5Dcreate2(file, DSET1_NAME, H5T_NATIVE_INT, space1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; /* Write the data to the dset1 */ if(H5Dwrite(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0) TEST_ERROR; if(H5Dclose(dset1) < 0) TEST_ERROR; if((dset1 = H5Dopen2(file, DSET1_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; /* Reallocate memory for reading buffer. */ HDassert(check); HDfree(check); if(NULL == (check = (int *)HDmalloc(DSET1_DIM1 * DSET1_DIM2 * sizeof(int)))) TEST_ERROR; /* Read the data back from dset1 */ if(H5Dread(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, check) < 0) TEST_ERROR; /* Check that the values read are the same as the values written */ p1 = points; p2 = check; for(i = 0; i < DSET1_DIM1; i++) for(j = 0; j < DSET1_DIM2; j++) if(*p1++ != *p2++) { H5_FAILED(); printf(" Read different values than written in data set 1.\n"); printf(" At index %d,%d\n", i, j); TEST_ERROR; } /* end if */ /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* There is no garantee the size of metadata in file is constant. * Just try to check if it's reasonable. */ if(file_size<64*KB || file_size>256*KB) TEST_ERROR; if(H5Sclose(space1) < 0) TEST_ERROR; if(H5Dclose(dset1) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; HDassert(points); HDfree(points); HDassert(check); HDfree(check); h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Pclose(fapl); H5Fclose(file); } H5E_END_TRY; if(points) HDfree(points); if(check) HDfree(check); return -1; }
/*------------------------------------------------------------------------- * Function: test_sec2 * * Purpose: Tests the file handle interface for SEC2 driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * *------------------------------------------------------------------------- */ static herr_t test_sec2(void) { hid_t file = -1; hid_t fapl = -1; hid_t access_fapl = -1; char filename[1024]; int *fhandle = NULL; hsize_t file_size = 0; TESTING("SEC2 file driver"); /* Set property list and file name for SEC2 driver. */ fapl = h5_fileaccess(); if(H5Pset_fapl_sec2(fapl) < 0) TEST_ERROR; h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* Check that the driver is correct */ if(H5FD_SEC2 != H5Pget_driver(access_fapl)) TEST_ERROR; /* ...and close the property list */ if(H5Pclose(access_fapl) < 0) TEST_ERROR; /* Check file handle API */ if(H5Fget_vfd_handle(file, H5P_DEFAULT, (void **)&fhandle) < 0) TEST_ERROR; if(*fhandle < 0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* There is no guarantee the size of metadata in file is constant. * Just try to check if it's reasonable. It's 2KB right now. */ if(file_size < 1 * KB || file_size > 4 * KB) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Pclose(fapl); H5Fclose(file); } H5E_END_TRY; return -1; }
int main (void) { hid_t file, dataset2; /* file and dataset handles */ hid_t datatype16; /* handles */ hid_t dataspace2; /* handles */ hsize_t dimsf2[2]; /* dataset dimensions */ hid_t aid; /* dataspace identifiers */ hid_t attr2; /* attribute identifiers */ herr_t status; int16_t data2[SIZE][SIZE]; /* data to write*/ int i, j, n; n = 0; for(i = 0; i < SIZE; i++) for(j = 0; j < SIZE; j++) data2[i][j] = n++; /* * Assigns minimal and maximal values of int16 to data2 and * they will be used to check boudary values. */ data2[0][0] = -32768; data2[1][1] = 32767; /* * 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); /* * Set each dimension size to 0. */ dimsf2[0] = 0; dimsf2[1] = 0; dataspace2 = H5Screate_simple(2, dimsf2, NULL); /* * Define datatype for the data in the file. */ datatype16 = H5Tcopy(H5T_NATIVE_SHORT); /* * Create a new dataset within the file using defined dataspace and * datatype and default dataset creation properties. */ dataset2 = H5Dcreate2(file, "dataset_2d", datatype16, dataspace2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Write the data although it has no effect because each dim size is 0. */ status = H5Dwrite(dataset2, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data2); /* * Create 2D attributes. */ attr2 = H5Acreate2(dataset2, "attribute_2d", datatype16, dataspace2, H5P_DEFAULT, H5P_DEFAULT); /* * Write the data although it has no effect because each dim size is 0. */ status = H5Awrite(attr2, datatype16, data2); H5Aclose(attr2); /* * Close/release resources. */ H5Dclose(dataset2); H5Tclose(datatype16); H5Sclose(dataspace2); H5Fclose(file); return 0; }
hid_t arma_H5Fcreate(const char* name, unsigned flags, hid_t fcpl_id, hid_t fapl_id) { return H5Fcreate(name, flags, fcpl_id, fapl_id); }
int main(void) { hid_t file=(-1), fapl, space=(-1), dset=(-1); char dname[]="dataset"; int i, j; int buf[FAMILY_NUMBER][FAMILY_SIZE]; hsize_t dims[2]={FAMILY_NUMBER, FAMILY_SIZE}; /* Set property list and file name for FAMILY driver */ if ((fapl=H5Pcreate(H5P_FILE_ACCESS)) < 0) { perror ("H5Pcreate"); exit (1); } if(H5Pset_fapl_family(fapl, (hsize_t)FAMILY_SIZE, H5P_DEFAULT) < 0) { perror ("H5Pset_fapl_family"); exit (1); } if((file = H5Fcreate(FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) { perror("H5Fcreate"); exit(1); } /* Create and write dataset */ if((space = H5Screate_simple(2, dims, NULL)) < 0) { perror("H5Screate_simple"); exit(1); } if((dset = H5Dcreate2(file, dname, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) { perror("H5Dcreate2"); exit(1); } for(i = 0; i<FAMILY_NUMBER; i++) for(j = 0; j<FAMILY_SIZE; j++) buf[i][j] = i * 10000 + j; if(H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) { perror("H5Dwrite"); exit(1); } if(H5Sclose(space) < 0) { perror ("H5Sclose"); exit (1); } if(H5Dclose(dset) < 0) { perror ("H5Dclose"); exit (1); } if(H5Pclose(fapl) < 0) { perror ("H5Pclose"); exit (1); } if(H5Fclose(file) < 0) { perror ("H5Fclose"); exit (1); } puts(" PASSED"); fflush(stdout); return 0; }
int main (void) { hid_t file, src_space, vspace, dset; /* Handles */ hid_t dcpl; herr_t status; hsize_t vdsdims[3] = {VDSDIM0, VDSDIM1, VDSDIM2}, vdsdims_max[3] = {H5S_UNLIMITED, VDSDIM1, VDSDIM1}, dims[3] = {DIM0, DIM1, DIM2}, start[3], /* Hyperslab parameters */ stride[3], count[3], block[3]; hsize_t start_out[3], /* Hyperslab parameter out */ stride_out[3], count_out[3], block_out[3]; int i; H5D_layout_t layout; /* Storage layout */ size_t num_map; /* Number of mappings */ ssize_t len; /* Length of the string; also a return value */ char *filename; char *dsetname; file = H5Fcreate (FILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create VDS dataspace. */ vspace = H5Screate_simple (RANK, vdsdims, vdsdims_max); /* Create dataspaces for the source dataset. */ src_space = H5Screate_simple (RANK, dims, NULL); /* Create VDS creation property */ dcpl = H5Pcreate (H5P_DATASET_CREATE); /* Initialize hyperslab values */ start[0] = 0; start[1] = 0; start[2] = 0; stride[0] = DIM0; stride[1] = 1; stride[2] = 1; count[0] = H5S_UNLIMITED; count[1] = 1; count[2] = 1; block[0] = DIM0; block[1] = DIM1; block[2] = DIM2; /* * Build the mappings * */ status = H5Sselect_hyperslab (vspace, H5S_SELECT_SET, start, stride, count, block); status = H5Pset_virtual (dcpl, vspace, "f-%b.h5", "/A", src_space); /* Create a virtual dataset */ dset = H5Dcreate2 (file, DATASET, H5T_NATIVE_INT, vspace, H5P_DEFAULT, dcpl, H5P_DEFAULT); status = H5Sclose (vspace); status = H5Sclose (src_space); status = H5Dclose (dset); status = H5Fclose (file); /* * Now we begin the read section of this example. */ /* * Open file and dataset using the default properties. */ file = H5Fopen (FILE, H5F_ACC_RDONLY, H5P_DEFAULT); dset = H5Dopen2 (file, DATASET, H5P_DEFAULT); /* * Get creation property list and mapping properties. */ dcpl = H5Dget_create_plist (dset); /* * Get storage layout. */ layout = H5Pget_layout (dcpl); if (H5D_VIRTUAL == layout) printf(" Dataset has a virtual layout \n"); else printf(" Wrong layout found \n"); /* * Find number of mappings. */ status = H5Pget_virtual_count (dcpl, &num_map); printf(" Number of mappings is %d\n", (int)num_map); /* * Get mapping parameters for each mapping. */ for (i = 0; i < (int)num_map; i++) { printf(" Mapping %d \n", i); printf(" Selection in the virtual dataset \n"); /* Get selection in the virttual dataset */ vspace = H5Pget_virtual_vspace (dcpl, (size_t)i); if (H5Sget_select_type(vspace) == H5S_SEL_HYPERSLABS) { if (H5Sis_regular_hyperslab(vspace)) { status = H5Sget_regular_hyperslab (vspace, start_out, stride_out, count_out, block_out); printf(" start = [%llu, %llu, %llu] \n", (unsigned long long)start_out[0], (unsigned long long)start_out[1], (unsigned long long)start_out[2]); printf(" stride = [%llu, %llu, %llu] \n", (unsigned long long)stride_out[0], (unsigned long long)stride_out[1], (unsigned long long)stride_out[2]); printf(" count = [%llu, %llu, %llu] \n", (unsigned long long)count_out[0], (unsigned long long)count_out[1], (unsigned long long)count_out[2]); printf(" block = [%llu, %llu, %llu] \n", (unsigned long long)block_out[0], (unsigned long long)block_out[1], (unsigned long long)block_out[2]); } } /* Get source file name */ len = H5Pget_virtual_filename (dcpl, (size_t)i, NULL, 0); filename = (char *)malloc((size_t)len*sizeof(char)+1); H5Pget_virtual_filename (dcpl, (size_t)i, filename, len+1); printf(" Source filename %s\n", filename); /* Get source dataset name */ len = H5Pget_virtual_dsetname (dcpl, (size_t)i, NULL, 0); dsetname = (char *)malloc((size_t)len*sizeof(char)+1); H5Pget_virtual_dsetname (dcpl, (size_t)i, dsetname, len+1); printf(" Source dataset name %s\n", dsetname); /* Get selection in the source dataset */ printf(" Selection in the source dataset "); src_space = H5Pget_virtual_srcspace (dcpl, (size_t)i); if(H5Sget_select_type(src_space) == H5S_SEL_ALL) { printf("H5S_ALL \n"); } /* EIP read data back */ H5Sclose(vspace); H5Sclose(src_space); free(filename); free(dsetname); } /* * Close and release resources. */ status = H5Pclose (dcpl); status = H5Dclose (dset); status = H5Fclose (file); return 0; }
int main() { printf("\n*** Checking HDF5 dimscales some more.\n"); printf("*** Creating a file with one var with one dimension scale..."); { hid_t fileid, spaceid, datasetid, dimscaleid, cparmsid; hsize_t dims[NDIMS] = {DIM1_LEN}, maxdims[NDIMS] = {H5S_UNLIMITED}; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; /* Create the space that will be used both for the dimscale and * the 1D dataset that will attach it. */ if ((spaceid = H5Screate_simple(NDIMS, dims, maxdims)) < 0) ERR; /* Modify dataset creation properties, i.e. enable chunking. */ dims[0] = 1; if ((cparmsid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_chunk(cparmsid, NDIMS, dims) < 0) ERR; /* Create our dimension scale, as an unlimited dataset. */ if ((dimscaleid = H5Dcreate(fileid, DIMSCALE_NAME, H5T_NATIVE_INT, spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(dimscaleid, NAME_ATTRIBUTE) < 0) ERR; /* Create a variable which uses it. */ if ((datasetid = H5Dcreate(fileid, VAR1_NAME, H5T_NATIVE_INT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(datasetid, dimscaleid, 0) < 0) ERR; if (H5DSset_label(datasetid, 0, DIMSCALE_LABEL) < 0) ERR; /* Fold up our tents. */ if (H5Dclose(dimscaleid) < 0 || H5Dclose(datasetid) < 0 || H5Sclose(spaceid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking that one var, one dimscale file can be read..."); { hid_t fileid, spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; char dimscale_name[NC_MAX_NAME+1]; htri_t is_scale; char label[NC_MAX_NAME+1]; int num_scales; hsize_t dims[1], maxdims[1]; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj, vars_dimscale_obj; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(fileid, i)) < 0) ERR; if (H5Gget_objname_by_idx(fileid, i, obj_name, NC_MAX_NAME) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(fileid, obj_name)) < 0) ERR; /* This should be an unlimited dataset. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if (maxdims[0] != H5S_UNLIMITED) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale && strcmp(obj_name, DIMSCALE_NAME)) ERR; if (is_scale) { /* A dimscale comes with a NAME attribute, in * addition to its real name. */ if (H5DSget_scale_name(datasetid, dimscale_name, NC_MAX_NAME) < 0) ERR; if (strcmp(dimscale_name, NAME_ATTRIBUTE)) ERR; /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj.fileno[0] = statbuf.fileno[0]; dimscale_obj.objno[0] = statbuf.objno[0]; dimscale_obj.fileno[1] = statbuf.fileno[1]; dimscale_obj.objno[1] = statbuf.objno[1]; /*printf("statbuf.fileno = %d statbuf.objno = %d\n", statbuf.fileno, statbuf.objno);*/ } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ if (H5DSiterate_scales(datasetid, 0, NULL, alien_visitor, &vars_dimscale_obj) < 0) ERR; /*printf("vars_dimscale_obj.fileno = %d vars_dimscale_obj.objno = %d\n", vars_dimscale_obj.fileno, vars_dimscale_obj.objno);*/ if (vars_dimscale_obj.fileno[0] != dimscale_obj.fileno[0] || vars_dimscale_obj.objno[0] != dimscale_obj.objno[0] || vars_dimscale_obj.fileno[1] != dimscale_obj.fileno[1] || vars_dimscale_obj.objno[1] != dimscale_obj.objno[1]) ERR; /* There's also a label for dimension 0. */ if (H5DSget_label(datasetid, 0, label, NC_MAX_NAME) < 0) ERR; /*printf("found non-scale dataset %s, label %s\n", obj_name, label);*/ } if (H5Dclose(datasetid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Sclose(spaceid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating a file with one var with two dimension scales..."); { #define LAT_LEN 3 #define LON_LEN 2 #define DIMS_2 2 #define LAT_NAME "lat" #define LON_NAME "lon" #define PRES_NAME "pres" hid_t fileid, lat_spaceid, lon_spaceid, pres_spaceid; hid_t pres_datasetid, lat_dimscaleid, lon_dimscaleid; hsize_t dims[DIMS_2]; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; /* Create the spaces that will be used for the dimscales. */ dims[0] = LAT_LEN; if ((lat_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LON_LEN; if ((lon_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; /* Create the space for the dataset. */ dims[0] = LAT_LEN; dims[1] = LON_LEN; if ((pres_spaceid = H5Screate_simple(DIMS_2, dims, dims)) < 0) ERR; /* Create our dimension scales. */ if ((lat_dimscaleid = H5Dcreate(fileid, LAT_NAME, H5T_NATIVE_INT, lat_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lat_dimscaleid, NULL) < 0) ERR; if ((lon_dimscaleid = H5Dcreate(fileid, LON_NAME, H5T_NATIVE_INT, lon_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lon_dimscaleid, NULL) < 0) ERR; /* Create a variable which uses these two dimscales. */ if ((pres_datasetid = H5Dcreate(fileid, PRES_NAME, H5T_NATIVE_FLOAT, pres_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lat_dimscaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lon_dimscaleid, 1) < 0) ERR; /* Fold up our tents. */ if (H5Dclose(lat_dimscaleid) < 0 || H5Dclose(lon_dimscaleid) < 0 || H5Dclose(pres_datasetid) < 0 || H5Sclose(lat_spaceid) < 0 || H5Sclose(lon_spaceid) < 0 || H5Sclose(pres_spaceid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking that one var, two dimscales file can be read..."); { #define NDIMS2 2 hid_t fileid, spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; htri_t is_scale; int num_scales; hsize_t dims[NDIMS2], maxdims[NDIMS2]; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj[2], vars_dimscale_obj[2]; int dimscale_cnt = 0; int d, ndims; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(fileid, i)) < 0) ERR; if (H5Gget_objname_by_idx(fileid, i, obj_name, NC_MAX_NAME) < 0) ERR; /* printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", */ /* obj_class, obj_name); */ /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(fileid, obj_name)) < 0) ERR; /* Get space info. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; if (ndims > NDIMS2) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj[dimscale_cnt].fileno[0] = statbuf.fileno[0]; dimscale_obj[dimscale_cnt].objno[0] = statbuf.objno[0]; dimscale_obj[dimscale_cnt].fileno[1] = statbuf.fileno[1]; dimscale_obj[dimscale_cnt].objno[1] = statbuf.objno[1]; /* printf("dimscale_obj[%d].fileno = %d dimscale_obj[%d].objno = %d\n", */ /* dimscale_cnt, dimscale_obj[dimscale_cnt].fileno, dimscale_cnt, */ /* dimscale_obj[dimscale_cnt].objno); */ dimscale_cnt++; } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0 and 1. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; if ((num_scales = H5DSget_num_scales(datasetid, 1)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) { if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor2, &(vars_dimscale_obj[d])) < 0) ERR; /* Verify that the object ids passed from the * alien_visitor2 function match the ones we found * for the lat and lon datasets. */ if (vars_dimscale_obj[d].fileno[0] != dimscale_obj[d].fileno[0] || vars_dimscale_obj[d].objno[0] != dimscale_obj[d].objno[0]) ERR; if (vars_dimscale_obj[d].fileno[1] != dimscale_obj[d].fileno[1] || vars_dimscale_obj[d].objno[1] != dimscale_obj[d].objno[1]) ERR; } } if (H5Dclose(datasetid) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating a file with one var with two unlimited dimension scales..."); { #define U1_LEN 3 #define U2_LEN 2 #define DIMS2 2 #define U1_NAME "u1" #define U2_NAME "u2" #define VNAME "v1" hid_t fapl_id, fcpl_id, grpid, plistid, plistid2; hid_t fileid, lat_spaceid, lon_spaceid, pres_spaceid; hid_t pres_datasetid, lat_dimscaleid, lon_dimscaleid; hsize_t dims[DIMS2], maxdims[DIMS2], chunksize[DIMS2] = {10, 10}; hid_t spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; htri_t is_scale; int num_scales; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj[2], vars_dimscale_obj[2]; int dimscale_cnt = 0; int d, ndims; /* Create file access and create property lists. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; /* Set latest_format in access propertly list. This ensures that * the latest, greatest, HDF5 versions are used in the file. */ if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR; /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This * turns on HDF5 creation ordering in the file. */ 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; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; /* Open the root group. */ if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; /* Create the spaces that will be used for the dimscales. */ dims[0] = 0; maxdims[0] = H5S_UNLIMITED; if ((lat_spaceid = H5Screate_simple(1, dims, maxdims)) < 0) ERR; if ((lon_spaceid = H5Screate_simple(1, dims, maxdims)) < 0) ERR; /* Create the space for the dataset. */ dims[0] = 0; dims[1] = 0; maxdims[0] = H5S_UNLIMITED; maxdims[1] = H5S_UNLIMITED; if ((pres_spaceid = H5Screate_simple(DIMS2, dims, maxdims)) < 0) ERR; /* Set up the dataset creation property list for the two dimensions. */ if ((plistid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_chunk(plistid, 1, chunksize) < 0) ERR; if (H5Pset_attr_creation_order(plistid, H5P_CRT_ORDER_TRACKED| H5P_CRT_ORDER_INDEXED) < 0) ERR; /* Create our dimension scales. */ if ((lat_dimscaleid = H5Dcreate(grpid, U1_NAME, H5T_NATIVE_INT, lat_spaceid, plistid)) < 0) ERR; if (H5DSset_scale(lat_dimscaleid, NULL) < 0) ERR; if ((lon_dimscaleid = H5Dcreate(grpid, U2_NAME, H5T_NATIVE_INT, lon_spaceid, plistid)) < 0) ERR; if (H5DSset_scale(lon_dimscaleid, NULL) < 0) ERR; /* Set up the dataset creation property list for the variable. */ if ((plistid2 = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_chunk(plistid2, DIMS2, chunksize) < 0) ERR; if (H5Pset_attr_creation_order(plistid2, H5P_CRT_ORDER_TRACKED| H5P_CRT_ORDER_INDEXED) < 0) ERR; /* Create a variable which uses these two dimscales. */ if ((pres_datasetid = H5Dcreate(grpid, VNAME, H5T_NATIVE_DOUBLE, pres_spaceid, plistid2)) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lat_dimscaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lon_dimscaleid, 1) < 0) ERR; /* Close down the show. */ if (H5Pclose(fapl_id) < 0 || H5Pclose(fcpl_id) < 0 || H5Dclose(lat_dimscaleid) < 0 || H5Dclose(lon_dimscaleid) < 0 || H5Dclose(pres_datasetid) < 0 || H5Sclose(lat_spaceid) < 0 || H5Sclose(lon_spaceid) < 0 || H5Sclose(pres_spaceid) < 0 || H5Pclose(plistid) < 0 || H5Pclose(plistid2) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen2(fileid, "/", H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i = 0; i < num_obj; i++) { /*Get the type (i.e. group, dataset, etc.), and the name of the object. */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, NC_MAX_NAME) < 0) ERR; /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(grpid, obj_name)) < 0) ERR; /* Get space info. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj[dimscale_cnt].fileno[0] = statbuf.fileno[0]; dimscale_obj[dimscale_cnt].objno[0] = statbuf.objno[0]; dimscale_obj[dimscale_cnt].fileno[1] = statbuf.fileno[1]; dimscale_obj[dimscale_cnt].objno[1] = statbuf.objno[1]; dimscale_cnt++; } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0 and 1. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; if ((num_scales = H5DSget_num_scales(datasetid, 1)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) { if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor2, &(vars_dimscale_obj[d])) < 0) ERR; /* Verify that the object ids passed from the * alien_visitor2 function match the ones we found * for the lat and lon datasets. */ if (vars_dimscale_obj[d].fileno[0] != dimscale_obj[d].fileno[0] || vars_dimscale_obj[d].objno[0] != dimscale_obj[d].objno[0]) ERR; if (vars_dimscale_obj[d].fileno[1] != dimscale_obj[d].fileno[1] || vars_dimscale_obj[d].objno[1] != dimscale_obj[d].objno[1]) ERR; } } if (H5Dclose(datasetid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Check the dimension lengths. */ { hid_t spaceid1; hsize_t h5dimlen[DIMS2], h5dimlenmax[DIMS2]; int dataset_ndims; /* Check U1. */ if ((datasetid = H5Dopen1(grpid, U1_NAME)) < 0) ERR; if ((spaceid1 = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid1, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 1 || h5dimlen[0] != 0 || h5dimlenmax[0] != H5S_UNLIMITED) ERR; if (H5Dclose(datasetid) || H5Sclose(spaceid1)) ERR; /* Check U2. */ if ((datasetid = H5Dopen1(grpid, U2_NAME)) < 0) ERR; if ((spaceid1 = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid1, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 1 || h5dimlen[0] != 0 || h5dimlenmax[0] != H5S_UNLIMITED) ERR; if (H5Dclose(datasetid) || H5Sclose(spaceid1)) ERR; /* Check V1. */ if ((datasetid = H5Dopen1(grpid, VNAME)) < 0) ERR; if ((spaceid1 = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(spaceid1, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 0 || h5dimlen[1] != 0 || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* All done. */ if (H5Dclose(datasetid) || H5Sclose(spaceid1)) ERR; } /* Write two hyperslabs. */ { #define NUM_VALS 3 hid_t file_spaceid, mem_spaceid; hsize_t h5dimlen[DIMS2], h5dimlenmax[DIMS2], xtend_size[DIMS2] = {1, NUM_VALS}; hsize_t start[DIMS2] = {0, 0}; hsize_t count[DIMS2] = {1, NUM_VALS}; double value[NUM_VALS]; int dataset_ndims; int i; /* Set up phony data. */ for (i = 0; i < NUM_VALS; i++) value[i] = (float)i; /* Open the dataset, check its dimlens. */ if ((datasetid = H5Dopen1(grpid, VNAME)) < 0) ERR; if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(file_spaceid, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 0 || h5dimlen[1] != 0 || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* Extend the size of the dataset. */ if (H5Dextend(datasetid, xtend_size) < 0) ERR; if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; /* Check the size. */ if ((dataset_ndims = H5Sget_simple_extent_dims(file_spaceid, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 1 || h5dimlen[1] != NUM_VALS || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* Set up the file and memory spaces. */ if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, count, NULL) < 0) ERR; if ((mem_spaceid = H5Screate_simple(DIMS2, count, NULL)) < 0) ERR; /* Write a slice of data. */ if (H5Dwrite(datasetid, H5T_NATIVE_DOUBLE, mem_spaceid, file_spaceid, H5P_DEFAULT, value) < 0) /* Check the size. */ if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(file_spaceid, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 1 || h5dimlen[1] != NUM_VALS || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* Extend the size of the dataset for the second slice. */ xtend_size[0]++; if (H5Dextend(datasetid, xtend_size) < 0) ERR; if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; /* Set up the file and memory spaces for a second slice. */ start[0]++; if (H5Sselect_hyperslab(file_spaceid, H5S_SELECT_SET, start, NULL, count, NULL) < 0) ERR; if ((mem_spaceid = H5Screate_simple(DIMS2, count, NULL)) < 0) ERR; /* Write a second slice of data. */ if (H5Dwrite(datasetid, H5T_NATIVE_DOUBLE, mem_spaceid, file_spaceid, H5P_DEFAULT, value) < 0) /* Check the size again. */ if ((file_spaceid = H5Dget_space(datasetid)) < 0) ERR; if ((dataset_ndims = H5Sget_simple_extent_dims(file_spaceid, h5dimlen, h5dimlenmax)) < 0) ERR; if (dataset_ndims != 2 || h5dimlen[0] != 2 || h5dimlen[1] != NUM_VALS || h5dimlenmax[0] != H5S_UNLIMITED || h5dimlenmax[1] != H5S_UNLIMITED) ERR; /* All done. */ if (H5Dclose(datasetid) || H5Sclose(mem_spaceid) || H5Sclose(file_spaceid)) ERR; } /* Close up the shop. */ if (H5Sclose(spaceid)) ERR; if (H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking dimension scales with attached dimension scales..."); { #define LAT_LEN 3 #define LON_LEN 2 #define TIME_LEN 5 #define LEN_LEN 10 #define DIMS_3 3 #define NUM_DIMSCALES1 4 #define LAT_NAME "lat" #define LON_NAME "lon" #define PRES_NAME1 "z_pres" #define TIME_NAME "time" #define LEN_NAME "u_len" hid_t fileid, lat_spaceid, lon_spaceid, time_spaceid, pres_spaceid, len_spaceid; hid_t pres_datasetid, lat_dimscaleid, lon_dimscaleid, time_dimscaleid, len_dimscaleid; hid_t fapl_id, fcpl_id; hsize_t dims[DIMS_3]; hid_t spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; htri_t is_scale; int num_scales; hsize_t maxdims[DIMS_3]; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj[NUM_DIMSCALES1], vars_dimscale_obj[NUM_DIMSCALES1]; int dimscale_cnt = 0; int d, ndims; /* Create file access and create property lists. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; /* Set latest_format in access propertly list. This ensures that * the latest, greatest, HDF5 versions are used in the file. */ if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR; /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This * turns on HDF5 creation ordering in the file. */ 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; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; /* Create the spaces that will be used for the dimscales. */ dims[0] = LAT_LEN; if ((lat_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LON_LEN; if ((lon_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = TIME_LEN; if ((time_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LEN_LEN; if ((len_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; /* Create the space for the dataset. */ dims[0] = LAT_LEN; dims[1] = LON_LEN; dims[2] = TIME_LEN; if ((pres_spaceid = H5Screate_simple(DIMS_3, dims, dims)) < 0) ERR; /* Create our dimension scales. */ if ((lat_dimscaleid = H5Dcreate1(fileid, LAT_NAME, H5T_NATIVE_INT, lat_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lat_dimscaleid, NULL) < 0) ERR; if ((lon_dimscaleid = H5Dcreate1(fileid, LON_NAME, H5T_NATIVE_INT, lon_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lon_dimscaleid, NULL) < 0) ERR; if ((time_dimscaleid = H5Dcreate1(fileid, TIME_NAME, H5T_NATIVE_INT, time_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(time_dimscaleid, NULL) < 0) ERR; if ((len_dimscaleid = H5Dcreate1(fileid, LEN_NAME, H5T_NATIVE_INT, len_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(len_dimscaleid, NULL) < 0) ERR; /* Create a variable which uses these three dimscales. */ if ((pres_datasetid = H5Dcreate1(fileid, PRES_NAME1, H5T_NATIVE_FLOAT, pres_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lat_dimscaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lon_dimscaleid, 1) < 0) ERR; if (H5DSattach_scale(pres_datasetid, time_dimscaleid, 2) < 0) ERR; /* Attach a dimscale to a dimscale. Unfortunately, HDF5 does not * allow this. Woe is me. */ /*if (H5DSattach_scale(time_dimscaleid, len_dimscaleid, 0) < 0) ERR;*/ /* Fold up our tents. */ if (H5Dclose(lat_dimscaleid) < 0 || H5Dclose(lon_dimscaleid) < 0 || H5Dclose(time_dimscaleid) < 0 || H5Dclose(len_dimscaleid) < 0 || H5Dclose(pres_datasetid) < 0 || H5Sclose(lat_spaceid) < 0 || H5Sclose(lon_spaceid) < 0 || H5Sclose(time_spaceid) < 0 || H5Sclose(pres_spaceid) < 0 || H5Sclose(len_spaceid) < 0 || H5Pclose(fapl_id) < 0 || H5Pclose(fcpl_id) < 0 || H5Fclose(fileid) < 0) ERR; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(fileid, i)) < 0) ERR; if (H5Gget_objname_by_idx(fileid, i, obj_name, NC_MAX_NAME) < 0) ERR; /* printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", */ /* obj_class, obj_name); */ /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(fileid, obj_name)) < 0) ERR; /* Get space info. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj[dimscale_cnt].fileno[0] = statbuf.fileno[0]; dimscale_obj[dimscale_cnt].objno[0] = statbuf.objno[0]; dimscale_obj[dimscale_cnt].fileno[1] = statbuf.fileno[1]; dimscale_obj[dimscale_cnt].objno[1] = statbuf.objno[1]; /* printf("dimscale_obj[%d].fileno = %d dimscale_obj[%d].objno = %d\n", */ /* dimscale_cnt, dimscale_obj[dimscale_cnt].fileno, dimscale_cnt, */ /* dimscale_obj[dimscale_cnt].objno); */ dimscale_cnt++; } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0 and 1. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; if ((num_scales = H5DSget_num_scales(datasetid, 1)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) { if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor2, &(vars_dimscale_obj[d])) < 0) ERR; /* Verify that the object ids passed from the * alien_visitor2 function match the ones we found * for the lat and lon datasets. */ if (vars_dimscale_obj[d].fileno[0] != dimscale_obj[d].fileno[0] || vars_dimscale_obj[d].objno[0] != dimscale_obj[d].objno[0]) ERR; if (vars_dimscale_obj[d].fileno[1] != dimscale_obj[d].fileno[1] || vars_dimscale_obj[d].objno[1] != dimscale_obj[d].objno[1]) ERR; } } if (H5Dclose(datasetid) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking cration ordering of datasets which are also dimension scales..."); { #define LAT_LEN 3 #define LON_LEN 2 #define TIME_LEN 5 #define LEN_LEN 10 #define DIMS_3 3 #define NUM_DIMSCALES2 4 #define LAT_NAME "lat" #define LON_NAME "lon" #define PRES_NAME1 "z_pres" #define TIME_NAME "time" #define LEN_NAME "u_len" hid_t fileid, lat_spaceid, lon_spaceid, time_spaceid, pres_spaceid, len_spaceid; hid_t pres_datasetid, lat_dimscaleid, lon_dimscaleid, time_dimscaleid, len_dimscaleid; hid_t fapl_id, fcpl_id; hsize_t dims[DIMS_3]; hid_t spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[NC_MAX_NAME + 1]; htri_t is_scale; int num_scales; hsize_t maxdims[DIMS_3]; H5G_stat_t statbuf; HDF5_OBJID_T dimscale_obj[NUM_DIMSCALES2], vars_dimscale_obj[NUM_DIMSCALES2]; int dimscale_cnt = 0; int d, ndims; /* Create file access and create property lists. */ if ((fapl_id = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if ((fcpl_id = H5Pcreate(H5P_FILE_CREATE)) < 0) ERR; /* Set latest_format in access propertly list. This ensures that * the latest, greatest, HDF5 versions are used in the file. */ if (H5Pset_libver_bounds(fapl_id, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) ERR; /* Set H5P_CRT_ORDER_TRACKED in the creation property list. This * turns on HDF5 creation ordering in the file. */ 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; /* Create file. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, fcpl_id, fapl_id)) < 0) ERR; /* Create the spaces that will be used for the dimscales. */ dims[0] = LAT_LEN; if ((lat_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LON_LEN; if ((lon_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = TIME_LEN; if ((time_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; dims[0] = LEN_LEN; if ((len_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; /* Create the space for the dataset. */ dims[0] = LAT_LEN; dims[1] = LON_LEN; dims[2] = TIME_LEN; if ((pres_spaceid = H5Screate_simple(DIMS_3, dims, dims)) < 0) ERR; /* Create our dimension scales. */ if ((lat_dimscaleid = H5Dcreate1(fileid, LAT_NAME, H5T_NATIVE_INT, lat_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lat_dimscaleid, NULL) < 0) ERR; if ((lon_dimscaleid = H5Dcreate1(fileid, LON_NAME, H5T_NATIVE_INT, lon_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lon_dimscaleid, NULL) < 0) ERR; if ((time_dimscaleid = H5Dcreate1(fileid, TIME_NAME, H5T_NATIVE_INT, time_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(time_dimscaleid, NULL) < 0) ERR; if ((len_dimscaleid = H5Dcreate1(fileid, LEN_NAME, H5T_NATIVE_INT, len_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(len_dimscaleid, NULL) < 0) ERR; /* Create a variable which uses these three dimscales. */ if ((pres_datasetid = H5Dcreate1(fileid, PRES_NAME1, H5T_NATIVE_FLOAT, pres_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lat_dimscaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_datasetid, lon_dimscaleid, 1) < 0) ERR; if (H5DSattach_scale(pres_datasetid, time_dimscaleid, 2) < 0) ERR; /* Attach a dimscale to a dimscale. Unfortunately, HDF5 does not * allow this. Woe is me. */ /*if (H5DSattach_scale(time_dimscaleid, len_dimscaleid, 0) < 0) ERR;*/ /* Fold up our tents. */ if (H5Dclose(lat_dimscaleid) < 0 || H5Dclose(lon_dimscaleid) < 0 || H5Dclose(time_dimscaleid) < 0 || H5Dclose(len_dimscaleid) < 0 || H5Dclose(pres_datasetid) < 0 || H5Sclose(lat_spaceid) < 0 || H5Sclose(lon_spaceid) < 0 || H5Sclose(time_spaceid) < 0 || H5Sclose(pres_spaceid) < 0 || H5Sclose(len_spaceid) < 0 || H5Pclose(fapl_id) < 0 || H5Pclose(fcpl_id) < 0 || H5Fclose(fileid) < 0) ERR; /* Open the file. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; /* Loop through objects in the root group. */ if (H5Gget_num_objs(fileid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(fileid, i)) < 0) ERR; if (H5Gget_objname_by_idx(fileid, i, obj_name, NC_MAX_NAME) < 0) ERR; /* printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", */ /* obj_class, obj_name); */ /* Deal with object based on its obj_class. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen1(fileid, obj_name)) < 0) ERR; /* Get space info. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if ((ndims = H5Sget_simple_extent_ndims(spaceid)) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { /* fileno and objno uniquely identify an object and a * HDF5 file. */ if (H5Gget_objinfo(datasetid, ".", 1, &statbuf) < 0) ERR; dimscale_obj[dimscale_cnt].fileno[0] = statbuf.fileno[0]; dimscale_obj[dimscale_cnt].objno[0] = statbuf.objno[0]; dimscale_obj[dimscale_cnt].fileno[1] = statbuf.fileno[1]; dimscale_obj[dimscale_cnt].objno[1] = statbuf.objno[1]; /* printf("dimscale_obj[%d].fileno = %d dimscale_obj[%d].objno = %d\n", */ /* dimscale_cnt, dimscale_obj[dimscale_cnt].fileno, dimscale_cnt, */ /* dimscale_obj[dimscale_cnt].objno); */ dimscale_cnt++; } else { /* Here's how to get the number of scales attached * to the dataset's dimension 0 and 1. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; if ((num_scales = H5DSget_num_scales(datasetid, 1)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ for (d = 0; d < ndims; d++) { if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor2, &(vars_dimscale_obj[d])) < 0) ERR; /* Verify that the object ids passed from the * alien_visitor2 function match the ones we found * for the lat and lon datasets. */ if (vars_dimscale_obj[d].fileno[0] != dimscale_obj[d].fileno[0] || vars_dimscale_obj[d].objno[0] != dimscale_obj[d].objno[0]) ERR; if (vars_dimscale_obj[d].fileno[1] != dimscale_obj[d].fileno[1] || vars_dimscale_obj[d].objno[1] != dimscale_obj[d].objno[1]) ERR; } } if (H5Dclose(datasetid) < 0) ERR; if (H5Sclose(spaceid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; FINAL_RESULTS; }
int create_file(char * name){ hid_t file_id; file_id = H5Fcreate(name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); return 0; }
int main( void ) { typedef struct Particle { char name[16]; int lati; int longi; float pressure; double temperature; } Particle; /* Define a subset of Particle, with latitude and longitude fields */ typedef struct Position { int lati; int longi; } Position; /* Define a subset of Particle, with name and pressure fields */ typedef struct NamePressure { char name[16]; float pressure; } NamePressure; /* Calculate the type_size and the offsets of our struct members */ Particle dst_buf[NRECORDS]; 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( dst_buf[0].name), sizeof( dst_buf[0].lati), sizeof( dst_buf[0].longi), sizeof( dst_buf[0].pressure), sizeof( dst_buf[0].temperature)}; size_t field_offset_pos[2] = { HOFFSET( Position, lati ), HOFFSET( Position, longi )}; /* Initially no data */ Particle *p_data = NULL; /* 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; Particle fill_data[1] = { {"no data",-1,-1, -99.0f, -99.0} }; /* Fill value particle */ int compress = 0; hsize_t nfields; hsize_t start; /* Record to start reading/writing */ hsize_t nrecords; /* Number of records to read/write */ herr_t status; int i; /* Define new values for the field "Pressure" */ float pressure_in [NRECORDS_ADD] = { 0.0f,1.0f,2.0f}; int field_index_pre[1] = { 3 }; int field_index_pos[2] = { 1,2 }; /* Define new values for the fields "Latitude,Longitude" */ Position position_in[NRECORDS_ADD] = { {0,0}, {10,10}, {20,20} }; size_t field_sizes_pos[2]= { sizeof(position_in[0].longi), sizeof(position_in[0].lati) }; size_t field_sizes_pre[1]= { sizeof(float) }; /* 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_05.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 ); /* Write the pressure field starting at record 2 */ nfields = 1; start = 2; nrecords = NRECORDS_ADD; status=H5TBwrite_fields_index( file_id, TABLE_NAME, nfields, field_index_pre, start, nrecords, sizeof( float ), 0, field_sizes_pre, pressure_in ); /* Write the new longitude and latitude information starting at record 2 */ nfields = 2; start = 2; nrecords = NRECORDS_ADD; status=H5TBwrite_fields_index( file_id, TABLE_NAME, nfields, field_index_pos, start, nrecords, sizeof( Position ), field_offset_pos, field_sizes_pos, position_in ); /* read the table */ status=H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf ); /* print it by rows */ for (i=0; i<NRECORDS; 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; }
int main(void) { hid_t src_sid = -1; /* source dataset's dataspace ID */ hid_t src_dcplid = -1; /* source dataset property list ID */ hid_t vds_sid = -1; /* VDS dataspace ID */ hid_t vds_dcplid = -1; /* VDS dataset property list ID */ hid_t fid = -1; /* HDF5 file ID */ hid_t did = -1; /* dataset ID */ hid_t msid = -1; /* memory dataspace ID */ hid_t fsid = -1; /* file dataspace ID */ hsize_t extent[RANK]; /* source dataset extents */ hsize_t start[RANK]; /* starting point for hyperslab */ hsize_t stride[RANK]; /* hypserslab stride */ hsize_t count[RANK]; /* hypserslab count */ int map_start = -1; /* starting point in the VDS map */ int *buffer = NULL; /* data buffer */ int value = -1; /* value written to datasets */ hsize_t n = 0; /* number of elements in a plane */ int i; /* iterator */ int j; /* iterator */ int k; /* iterator */ /* Start by creating the virtual dataset (VDS) dataspace and creation * property list. The individual source datasets are then created * and the VDS map (stored in the VDS property list) is updated. */ /* Create VDS dcpl */ if((vds_dcplid = H5Pcreate(H5P_DATASET_CREATE)) < 0) UC_ERROR if(H5Pset_fill_value(vds_dcplid, UC_5_VDS_DATATYPE, &UC_5_VDS_FILL_VALUE) < 0) UC_ERROR /* Create VDS dataspace */ if((vds_sid = H5Screate_simple(RANK, UC_5_VDS_DIMS, UC_5_VDS_MAX_DIMS)) < 0) UC_ERROR /********************************* * Map source files and datasets * *********************************/ /* Hyperslab array setup */ start[0] = 0; start[1] = 0; start[2] = 0; map_start = 0; stride[0] = UC_5_N_SOURCES; stride[1] = 1; stride[2] = 1; count[0] = H5S_UNLIMITED; count[1] = 1; count[2] = 1; extent[0] = UC_5_SRC_PLANES; extent[1] = UC_5_HEIGHT; extent[2] = UC_5_WIDTH; for(i = 0; i < UC_5_N_SOURCES; i++) { /* source dataset dcpl */ if((src_dcplid = H5Pcreate(H5P_DATASET_CREATE)) < 0) UC_ERROR if(H5Pset_chunk(src_dcplid, RANK, UC_5_PLANE) < 0) UC_ERROR if(H5Pset_fill_value(src_dcplid, UC_5_SOURCE_DATATYPE, &UC_5_FILL_VALUES[i]) < 0) UC_ERROR if(H5Pset_deflate(src_dcplid, COMPRESSION_LEVEL) < 0) UC_ERROR /* Create source file, dataspace, and dataset */ if((fid = H5Fcreate(UC_5_FILE_NAMES[i], H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) UC_ERROR if((src_sid = H5Screate_simple(RANK, UC_5_SOURCE_DIMS, UC_5_SOURCE_MAX_DIMS)) < 0) UC_ERROR if((did = H5Dcreate2(fid, UC_5_SOURCE_DSET_NAME, UC_5_SOURCE_DATATYPE, src_sid, H5P_DEFAULT, src_dcplid, H5P_DEFAULT)) < 0) UC_ERROR /* Set the dataset's extent */ if(H5Dset_extent(did, extent) < 0) UC_ERROR /* Create a data buffer that represents a plane */ n = UC_5_PLANE[1] * UC_5_PLANE[2]; if(NULL == (buffer = (int *)malloc(n * sizeof(int)))) UC_ERROR /* Create the memory dataspace */ if((msid = H5Screate_simple(RANK, UC_5_PLANE, NULL)) < 0) UC_ERROR /* Get the file dataspace */ if((fsid = H5Dget_space(did)) < 0) UC_ERROR /* Write planes to the dataset */ for(j = 0; j < UC_5_SRC_PLANES; j++) { value = ((i + 1) * 10) + j; for(k = 0; k < n; k++) buffer[k] = value; start[0] = j; start[1] = 0; start[2] = 0; if(H5Sselect_hyperslab(fsid, H5S_SELECT_SET, start, NULL, UC_5_PLANE, NULL) < 0) UC_ERROR if(H5Dwrite(did, H5T_NATIVE_INT, msid, fsid, H5P_DEFAULT, buffer) < 0) UC_ERROR } /* end for */ /* set up hyperslabs for source and destination datasets */ start[0] = 0; start[1] = 0; start[2] = 0; if(H5Sselect_hyperslab(src_sid, H5S_SELECT_SET, start, NULL, UC_5_SOURCE_MAX_DIMS, NULL) < 0) UC_ERROR start[0] = map_start; if(H5Sselect_hyperslab(vds_sid, H5S_SELECT_SET, start, stride, count, UC_5_PLANE) < 0) UC_ERROR map_start += 1; /* Add VDS mapping */ if(H5Pset_virtual(vds_dcplid, vds_sid, UC_5_FILE_NAMES[i], UC_5_SOURCE_DSET_PATH, src_sid) < 0) UC_ERROR /* close */ if(H5Sclose(msid) < 0) UC_ERROR if(H5Sclose(fsid) < 0) UC_ERROR if(H5Sclose(src_sid) < 0) UC_ERROR if(H5Pclose(src_dcplid) < 0) UC_ERROR if(H5Dclose(did) < 0) UC_ERROR if(H5Fclose(fid) < 0) UC_ERROR free(buffer); } /* end for */ /******************* * Create VDS file * *******************/ /* file */ if((fid = H5Fcreate(UC_5_VDS_FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) UC_ERROR /* dataset */ if((did = H5Dcreate2(fid, UC_5_VDS_DSET_NAME, UC_5_VDS_DATATYPE, vds_sid, H5P_DEFAULT, vds_dcplid, H5P_DEFAULT)) < 0) UC_ERROR /* close */ if(H5Pclose(vds_dcplid) < 0) UC_ERROR if(H5Sclose(vds_sid) < 0) UC_ERROR if(H5Dclose(did) < 0) UC_ERROR if(H5Fclose(fid) < 0) UC_ERROR return EXIT_SUCCESS; error: H5E_BEGIN_TRY { if(src_sid >= 0) (void)H5Sclose(src_sid); if(src_dcplid >= 0) (void)H5Pclose(src_dcplid); if(vds_sid >= 0) (void)H5Sclose(vds_sid); if(vds_dcplid >= 0) (void)H5Pclose(vds_dcplid); if(fid >= 0) (void)H5Fclose(fid); if(did >= 0) (void)H5Dclose(did); if(msid >= 0) (void)H5Sclose(msid); if(fsid >= 0) (void)H5Sclose(fsid); if(buffer != NULL) free(buffer); } H5E_END_TRY return EXIT_FAILURE; } /* end main() */
int main( void ) { typedef struct Particle { char name[16]; int lati; int longi; float pressure; double temperature; } Particle; /* 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 )}; /* 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; Particle fill_data[1] = { {"no data",-1,-1, -99.0f, -99.0} }; /* Fill value particle */ int compress = 0; hsize_t nfields_out; hsize_t nrecords_out; /* Initialize 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_06.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /* Make a table */ H5TBmake_table( "Table Title",file_id,TABLE_NAME,NFIELDS,NRECORDS,dst_size, field_names, dst_offset, field_type, chunk_size, fill_data, compress, NULL); /* Get table info */ 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); /* close type */ H5Tclose( string_type ); /* close the file */ H5Fclose( file_id ); return 0; }
int output (Array3D < zone > grid, Array3D < zone > fx, Array3D < zone > fy, int time, char *filename) { #ifdef USE_HDF5 hid_t file, dataset; /* file and dataset handles */ hid_t datatype, dataspace; /* handles */ hsize_t dimsf[2]; /* dataset dimensions */ herr_t status; double data[nx][ny]; char *names[] = { "Density", "Velx", "Vely", "Velz", "Energy", "Bx", "By", "Bz" }; int ll = 0; stringstream hdf5_stream_filename; string hdf5_filename; #endif ofstream fout; ofstream gout; double gammam1 = gammag - 1; double rl, ri; double px; double py; double pz; double pressure; double bx; double by; double bz; double bsquared; double et, ul, vl, wl, ke, al; int ii = 0; int jj = 0; int kk = 0; char outputdir[50] = "output/"; // char filename[50] = "out_2d_"; stringstream s; stringstream stream_filename; stringstream stream_temp_b; string str_file_tag; string str_output_filename; string str_input_filename; double ki = 24296.3696; double mp = 1.67262158; double mpi = 1.0 / mp; double nt = 0; double nt2 = 0; double temperature = 0; s.clear (); s.width (5); s.fill ('0'); s << time; s >> str_file_tag; stream_filename.clear (); stream_filename << outputdir << filename << str_file_tag; stream_filename >> str_input_filename; #ifdef USE_HDF5 hdf5_stream_filename << outputdir << "hdf5_" << filename << str_file_tag << ".h5"; hdf5_stream_filename >> hdf5_filename; file = H5Fcreate (hdf5_filename.c_str (), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); for (ll = 0; ll < ne; ll++) { dimsf[0] = nx; dimsf[1] = ny; dataspace = H5Screate_simple (RANK, dimsf, NULL); /* * Define datatype for the data in the file. * We will store little endian DOUBLE numbers. */ datatype = H5Tcopy (H5T_NATIVE_DOUBLE); status = H5Tset_order (datatype, H5T_ORDER_LE); /* * Create a new dataset within the file using defined dataspace and * datatype and default dataset creation properties. */ dataset = H5Dcreate (file, names[ll], datatype, dataspace, H5P_DEFAULT); for (jj = 0; jj < ny; jj++) { for (ii = 0; ii < nx; ii++) data[ii][jj] = grid[ii][jj][kk].array[ll]; } /* * Write the data to the dataset using default transfer properties. */ status = H5Dwrite (dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); /* * Close/release resources. */ H5Sclose (dataspace); H5Tclose (datatype); H5Dclose (dataset); } dimsf[0] = nx; dimsf[1] = ny; dataspace = H5Screate_simple (RANK, dimsf, NULL); /* * Define datatype for the data in the file. * We will store little endian DOUBLE numbers. */ datatype = H5Tcopy (H5T_NATIVE_DOUBLE); status = H5Tset_order (datatype, H5T_ORDER_LE); /* * Create a new dataset within the file using defined dataspace and * datatype and default dataset creation properties. */ dataset = H5Dcreate (file, "Pressure", datatype, dataspace, H5P_DEFAULT); for (jj = 0; jj < ny; jj++) { for (ii = 0; ii < nx; ii++) { rl = grid[ii][jj][kk] _MASS; px = grid[ii][jj][kk] _MOMX; py = grid[ii][jj][kk] _MOMY; pz = grid[ii][jj][kk] _MOMZ; et = grid[ii][jj][kk] _ENER; bx = grid[ii][jj][kk] _B_X; by = grid[ii][jj][kk] _B_Y; bz = grid[ii][jj][kk] _B_Z; ri = 1.0 / rl; ul = px * ri; vl = py * ri; wl = pz * ri; ke = 0.5 * rl * (ul * ul + vl * vl + wl * wl); bsquared = bx * bx + by * by + bz * bz; pressure = et - ke - 0.5 * bsquared; pressure = pressure * gammam1; al = sqrt (gammag * pressure * ri); nt = 2 * mpi * rl; nt2 = nt * nt; temperature = ki * pressure / nt; temperature = log10 (temperature); data[ii][jj] = pressure; } } /* * Write the data to the dataset using default transfer properties. */ status = H5Dwrite (dataset, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); /* * Close/release resources. */ H5Sclose (dataspace); H5Tclose (datatype); H5Dclose (dataset); H5Fclose (file); #endif /* HDF5 or not */ fout.open (str_input_filename.c_str ()); if (!fout) { cerr << "unable to open file " << endl; } jj = 0; // Determine Div B Array2D < double >divb (nx, ny); double bx1, bx2, by1, by2, bz1, bz2; for (ii = 1; ii < nx - 2; ii++) { for (jj = 1; jj < ny - 2; jj++) { bx1 = (grid[ii][jj][kk] _B_X + grid[ii - 1][jj][kk] _B_X); bx2 = (grid[ii + 1][jj][kk] _B_X + grid[ii][jj][kk] _B_X); by1 = (grid[ii][jj][kk] _B_Y + grid[ii][jj - 1][kk] _B_Y); by2 = (grid[ii][jj + 1][kk] _B_Y + grid[ii][jj][kk] _B_Y); // bz1 = ( grid[ii ][jj ][kk ]_B_Z + grid[ii ][jj ][kk-1]_B_Z ); // bz2 = ( grid[ii ][jj ][kk+1]_B_Z + grid[ii ][jj ][kk ]_B_Z ); //divb = (1/delta_x)*(bx2- bx1 + by2 -by1 +bz2 -bz1); divb[ii][jj] = (0.5 / delta_x) * (bx2 - bx1 + by2 - by1); } } for (ii = 0; ii < nx; ii++) { for (jj = 0; jj < ny; jj++) { rl = grid[ii][jj][kk] _MASS; px = grid[ii][jj][kk] _MOMX; py = grid[ii][jj][kk] _MOMY; pz = grid[ii][jj][kk] _MOMZ; et = grid[ii][jj][kk] _ENER; bx = grid[ii][jj][kk] _B_X; by = grid[ii][jj][kk] _B_Y; bz = grid[ii][jj][kk] _B_Z; ri = 1.0 / rl; ul = px * ri; vl = py * ri; wl = pz * ri; ke = 0.5 * rl * (ul * ul + vl * vl + wl * wl); bsquared = bx * bx + by * by + bz * bz; pressure = et - ke - 0.5 * bsquared; pressure = pressure * gammam1; al = sqrt (gammag * pressure * ri); #ifdef DEBUG_BC if (ii == 2 && jj == 2 && px != 0) { cout << px << endl; cout << ul << endl; cout << "wtf?" << endl; } #endif /* DEBUG_BC */ fout << setiosflags (ios::scientific) << " " << (rl) << " " << ul << " " << vl << " " << wl << " " << et << " " << bx << " " << by << " " << bz << " " << (pressure) << " " << (al) << " " << divb[ii][jj] << endl; } #ifdef TWODIM fout << endl; #endif /* TWODIM */ } fout.close (); gout.open ("gm.general"); gout << "file = /home/gmurphy/mhdvanleer-0.0.1/" << str_input_filename << endl; gout << "grid = " << nx << " x " << ny << endl; gout << "format = ascii" << endl; gout << "interleaving = field" << endl; gout << "majority = row" << endl; gout << "field = V_sound, E_tot, Rho, Vel_X, Vel_Y, Pressure" << endl; gout << "structure = scalar, scalar, scalar, scalar, scalar, scalar" << endl; gout << "type = double, double, double, double, double, double" << endl; gout << "dependency = positions, positions, positions, positions, positions, positions" << endl; gout << "positions = regular, regular, 0, 1, 0, 1" << endl; gout << "" << endl; gout << "end" << endl; gout.close (); return 0; }
/*------------------------------------------------------------------------- * Function: test * * Purpose: The guts of the test * * Return: Success: 0 * * Failure: number of errors * * Programmer: Robb Matzke * Wednesday, September 30, 1998 * * Modifications: * *------------------------------------------------------------------------- */ static int test(fill_t fill_style, const double splits[], hbool_t verbose, hbool_t use_rdcc) { hid_t file = (-1), fapl = (-1), dcpl = (-1), xfer = (-1), mspace = (-1), fspace = (-1), dset = (-1); hsize_t ch_size[1] = {1}; /*chunk size */ hsize_t cur_size[1] = {1000}; /*current dataset size */ hsize_t max_size[1] = {H5S_UNLIMITED}; /*maximum dataset size */ hsize_t hs_start[1]; /*hyperslab start offset*/ hsize_t hs_count[1] = {1}; /*hyperslab nelmts */ int fd = (-1); /*h5 file direct */ int *had = NULL; /*for random filling */ const char *sname=NULL; /*fill style nam */ int mdc_nelmts; /*num meta objs to cache*/ hsize_t i; int j; h5_stat_t sb; if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) goto error; if(!use_rdcc) { if(H5Pget_cache(fapl, &mdc_nelmts, NULL, NULL, NULL) < 0) goto error; if(H5Pset_cache(fapl, mdc_nelmts, 0, 0, 0.0F) < 0) goto error; } if((file = H5Fcreate(FILE_NAME_1, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) goto error; if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) goto error; if(H5Pset_chunk(dcpl, 1, ch_size) < 0) goto error; if((xfer = H5Pcreate(H5P_DATASET_XFER)) < 0) goto error; if(H5Pset_btree_ratios(xfer, splits[0], splits[1], splits[2]) < 0) goto error; if((fspace = H5Screate_simple(1, cur_size, max_size)) < 0) goto error; if((mspace = H5Screate_simple(1, ch_size, ch_size)) < 0) goto error; if((dset = H5Dcreate2(file, "chunked", H5T_NATIVE_INT, fspace, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) goto error; if ((fd=HDopen(FILE_NAME_1, O_RDONLY, 0666)) < 0) goto error; if(FILL_RANDOM==fill_style) had = calloc((size_t)cur_size[0], sizeof(int)); for (i=1; i<=cur_size[0]; i++) { /* Decide which chunk to write to */ switch (fill_style) { case FILL_FORWARD: hs_start[0] = i-1; break; case FILL_REVERSE: hs_start[0] = cur_size[0]-i; break; case FILL_INWARD: hs_start[0] = i%2 ? i/2 : cur_size[0]-i/2; break; case FILL_OUTWARD: j = (int)(cur_size[0]-i)+1; hs_start[0] = j%2 ? j/2 : (hssize_t)cur_size[0]-j/2; break; case FILL_RANDOM: for (j=HDrand()%(int)cur_size[0]; had[j]; j=(j+1)%(int)cur_size[0]) /*void*/; hs_start[0] = j; had[j] = 1; break; case FILL_ALL: abort(); default: /* unknown request */ HDfprintf(stderr, "Unknown fill style\n"); goto error; break; } /* Write the chunk */ if (H5Sselect_hyperslab(fspace, H5S_SELECT_SET, hs_start, NULL, hs_count, NULL) < 0) goto error; if (H5Dwrite(dset, H5T_NATIVE_INT, mspace, fspace, xfer, &i) < 0) { goto error; } /* Determine overhead */ if (verbose) { if (H5Fflush(file, H5F_SCOPE_LOCAL) < 0) goto error; if (HDfstat(fd, &sb) < 0) goto error; /* * The extra cast in the following statement is a bug workaround * for the Win32 version 5.0 compiler. * 1998-11-06 ptl */ printf("%4lu %8.3f ***\n", (unsigned long)i, (double)(hssize_t)(sb.st_size-i*sizeof(int))/(hssize_t)i); } } if(had) { free(had); had = NULL; } /* end if */ H5Dclose(dset); H5Sclose(mspace); H5Sclose(fspace); H5Pclose(dcpl); H5Pclose(xfer); H5Fclose(file); if (!verbose) { switch (fill_style) { case FILL_FORWARD: sname = "forward"; break; case FILL_REVERSE: sname = "reverse"; break; case FILL_INWARD: sname = "inward"; break; case FILL_OUTWARD: sname = "outward"; break; case FILL_RANDOM: sname = "random"; break; case FILL_ALL: abort(); default: /* unknown request */ HDfprintf(stderr, "Unknown fill style\n"); goto error; break; } if (HDfstat(fd, &sb) < 0) goto error; printf("%-7s %8.3f\n", sname, (double)(hssize_t)(sb.st_size-cur_size[0]*sizeof(int))/ (hssize_t)cur_size[0]); } HDclose(fd); return 0; error: H5Dclose(dset); H5Sclose(mspace); H5Sclose(fspace); H5Pclose(dcpl); H5Pclose(xfer); H5Fclose(file); if(had) free(had); HDclose(fd); return 1; }
int main(void) { hid_t fid; hid_t fapl; hid_t did; hid_t space; hsize_t dim[1] = {DIM}; unsigned data[DIM]; unsigned u; herr_t ret; /* Generic return value */ /* Initialize the data */ for(u = 0; u < DIM; u++) data[u] = u; /* Create a FAPL with the metadata and small data aggregators turned off */ fapl = H5Pcreate(H5P_FILE_ACCESS); assert(fapl > 0); ret = H5Pset_meta_block_size(fapl, (hsize_t)0); assert(ret >= 0); ret = H5Pset_small_data_block_size(fapl, (hsize_t)0); assert(ret >= 0); /* Create file */ fid = H5Fcreate(FILENAME, H5F_ACC_TRUNC, H5P_DEFAULT, fapl); assert(fid > 0); /* Close FAPL */ ret = H5Pclose(fapl); assert(ret >= 0); /* Create dataspace */ space = H5Screate_simple(1, dim, NULL); assert(space > 0); /* Create dataset #1 */ did = H5Dcreate2(fid, "dset1", H5T_NATIVE_UINT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); assert(did > 0); ret = H5Dwrite(did, H5T_NATIVE_UINT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); assert(ret >= 0); ret = H5Dclose(did); assert(ret >= 0); /* Create dataset #2 */ did = H5Dcreate2(fid, "dset2", H5T_NATIVE_UINT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); assert(did > 0); ret = H5Dwrite(did, H5T_NATIVE_UINT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data); assert(ret >= 0); ret = H5Dclose(did); assert(ret >= 0); /* Close dataspace */ ret = H5Sclose(space); assert(ret >= 0); /* Close file */ ret = H5Fclose(fid); assert(ret >= 0); return 0; }
int main(int argc, char **argv) { printf("\n*** Testing HDF5/NetCDF-4 interoperability...\n"); printf("*** testing HDF5 compatibility..."); { #define GRPA_NAME "grpa" #define VAR_NAME "vara" #define NDIMS 2 int nrowCur = 7; /* current size */ int ncolCur = 3; int nrowMax = nrowCur + 0; /* maximum size */ int ncolMax = ncolCur + 0; hid_t xdimId; hid_t ydimId; hsize_t xscaleDims[1]; hsize_t yscaleDims[1]; hid_t xdimSpaceId, spaceId; hid_t fileId; hid_t fapl; hsize_t curDims[2]; hsize_t maxDims[2]; hid_t dataTypeId, dsPropertyId, grpaId, grpaPropId, dsId; hid_t ydimSpaceId; const char * dimNameBase = "This is a netCDF dimension but not a netCDF variable."; char dimNameBuf[1000]; char *varaName = "/grpa/vara"; short amat[nrowCur][ncolCur]; int ii, jj; xscaleDims[0] = nrowCur; yscaleDims[0] = ncolCur; if ((xdimSpaceId = H5Screate_simple(1, xscaleDims, NULL)) < 0) ERR; /* With the SEMI close degree, the HDF5 file close will fail if * anything is left open. */ if ((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) ERR; if (H5Pset_fclose_degree(fapl, H5F_CLOSE_SEMI)) ERR; /* Create file */ if((fileId = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5Pcreate(H5P_FILE_CREATE), fapl)) < 0) ERR; if (H5Pclose(fapl) < 0) ERR; /* Create data space */ curDims[0] = nrowCur; curDims[1] = ncolCur; maxDims[0] = nrowMax; maxDims[1] = ncolMax; if ((spaceId = H5Screate_simple(2, curDims, maxDims)) < 0) ERR; if ((dataTypeId = H5Tcopy(H5T_NATIVE_SHORT)) < 0) ERR; if ((dsPropertyId = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if ((grpaPropId = H5Pcreate(H5P_GROUP_CREATE)) < 0) ERR; if ((grpaId = H5Gcreate2(fileId, GRPA_NAME, H5P_DEFAULT, grpaPropId, H5P_DEFAULT)) < 0) ERR; if (H5Pclose(grpaPropId) < 0) ERR; /* Create vara dataset */ if ((dsId = H5Dcreate2(fileId, varaName, dataTypeId, spaceId, H5P_DEFAULT, dsPropertyId, H5P_DEFAULT)) < 0) ERR; if (H5Pclose(dsPropertyId) < 0) ERR; if (H5Tclose(dataTypeId) < 0) ERR; if ((ydimSpaceId = H5Screate_simple(1, yscaleDims, NULL)) < 0) ERR; /* Create xdim dimension dataset */ if ((xdimId = H5Dcreate2(fileId, "/xdim", H5T_IEEE_F32BE, xdimSpaceId, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if (H5Sclose(xdimSpaceId) < 0) ERR; /* Create ydim dimension dataset */ if ((ydimId = H5Dcreate2(fileId, "/ydim", H5T_IEEE_F32BE, ydimSpaceId, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if (H5Sclose(ydimSpaceId) < 0) ERR; /* Create xdim scale */ sprintf(dimNameBuf, "%s%10d", dimNameBase, nrowCur); if (H5DSset_scale(xdimId, dimNameBuf) < 0) ERR; /* Create ydim scale */ sprintf(dimNameBuf, "%s%10d", dimNameBase, ncolCur); if (H5DSset_scale(ydimId, dimNameBuf) < 0) ERR; /* Attach dimension scales to the dataset */ if (H5DSattach_scale(dsId, xdimId, 0) < 0) ERR; if (H5DSattach_scale(dsId, ydimId, 1) < 0) ERR; /* Close stuff. */ if (H5Dclose(xdimId) < 0) ERR; if (H5Dclose(ydimId) < 0) ERR; if (H5Dclose(dsId) < 0) ERR; if (H5Gclose(grpaId) < 0) ERR; if (H5Sclose(spaceId) < 0) ERR; if (H5Fclose(fileId) < 0) ERR; /* Create some data */ for (ii = 0; ii < nrowCur; ii++) for (jj = 0; jj < ncolCur; jj++) amat[ii][jj] = 100 * ii + jj; /* Re-open file */ if ((fileId = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpaId = H5Gopen2(fileId, GRPA_NAME, H5P_DEFAULT)) < 0) ERR; if ((dsId = H5Dopen2(grpaId, varaName, H5P_DEFAULT)) < 0) ERR; /* Write dataset */ if (H5Dwrite(dsId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, amat) < 0) ERR; /* Write dimension values for both xdim, ydim */ { short xydimMat[ nrowCur >= ncolCur ? nrowCur : ncolCur]; for (ii = 0; ii < nrowCur; ii++) xydimMat[ii] = 0; /*#### 100 * ii; */ /* Write xdim */ if ((xdimId = H5Dopen2(fileId, "/xdim", H5P_DEFAULT)) < 0) ERR; if (H5Dwrite(xdimId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, xydimMat) < 0) ERR; if (H5Dclose(xdimId) < 0) ERR; /* Write ydim */ if ((ydimId = H5Dopen2(fileId, "/ydim", H5P_DEFAULT)) < 0) ERR; if (H5Dwrite(ydimId, H5T_NATIVE_SHORT, H5S_ALL, H5S_ALL, H5P_DEFAULT, xydimMat) < 0) ERR; if (H5Dclose(ydimId) < 0) ERR; } if (H5Dclose(dsId) < 0) ERR; if (H5Gclose(grpaId) < 0) ERR; if (H5Fclose(fileId) < 0) ERR; { int ncid, grpid, nvars, ngatts, ndims, unlimdimid, ngrps; char name_in[NC_MAX_NAME + 1]; nc_type xtype_in; int ndims_in, natts_in, dimid_in[NDIMS]; /* nc_set_log_level(5);*/ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (ndims != 2 || nvars != 0 || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_grps(ncid, &ngrps, &grpid)) ERR; if (ngrps != 1) ERR; if (nc_inq(grpid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR; if (ndims != 0 || nvars != 1 || ngatts != 0 || unlimdimid != -1) ERR; if (nc_inq_var(grpid, 0, name_in, &xtype_in, &ndims_in, dimid_in, &natts_in)) ERR; if (strcmp(name_in, VAR_NAME) || xtype_in != NC_SHORT || ndims_in != NDIMS || dimid_in[0] != 0 || dimid_in[1] != 1 || natts_in != 0) ERR; if (nc_close(ncid)) ERR; } } SUMMARIZE_ERR; #ifdef USE_SZIP printf("*** testing HDF5 compatibility with szip..."); { #define DEFLATE_LEVEL 9 #define MAX_NAME 100 #define NUM_CD_ELEM 10 /* HDF5 defines this... */ #define DEFLATE_NAME "deflate" #define DIM1_LEN 3000 #define GRP_NAME "George_Washington" #define BATTLE_RECORD "Battle_Record" hid_t fileid, grpid, spaceid, datasetid; int data_out[DIM1_LEN], data_in[DIM1_LEN]; hsize_t dims[1] = {DIM1_LEN}; H5Z_filter_t filter; int num_filters; hid_t propid; unsigned int flags, cd_values[NUM_CD_ELEM], filter_config; size_t cd_nelems = NUM_CD_ELEM; size_t namelen = MAX_NAME; char name[MAX_NAME + 1], name_in[MAX_NAME + 1]; int ncid, ndims_in, nvars_in, ngatts_in, unlimdimid_in, ngrps_in; int dimid_in[1], natts_in; nc_type xtype_in; int i; for (i = 0; i < DIM1_LEN; i++) data_out[i] = i; /* Open file and create group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; /* Write an array of bools, with szip compression. */ if ((propid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_layout(propid, H5D_CHUNKED)) ERR; if (H5Pset_chunk(propid, 1, dims)) ERR; if (H5Pset_szip(propid, H5_SZIP_EC_OPTION_MASK, 32)) ERR; if ((spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; if ((datasetid = H5Dcreate(grpid, BATTLE_RECORD, H5T_NATIVE_INT, spaceid, propid)) < 0) ERR; if (H5Dwrite(datasetid, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data_out) < 0) ERR; if (H5Dclose(datasetid) < 0 || H5Pclose(propid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* Open the file with netCDF and check it. */ if (nc_open(FILE_NAME, NC_NOWRITE, &ncid)) ERR; if (nc_inq(ncid, &ndims_in, &nvars_in, &ngatts_in, &unlimdimid_in)) ERR; if (ndims_in != 0 || nvars_in != 0 || ngatts_in != 0 || unlimdimid_in != -1) ERR; if (nc_inq_grps(ncid, &ngrps_in, &grpid)) ERR; if (ngrps_in != 1) ERR; if (nc_inq(grpid, &ndims_in, &nvars_in, &ngatts_in, &unlimdimid_in)) ERR; if (ndims_in != 1 || nvars_in != 1 || ngatts_in != 0 || unlimdimid_in != -1) ERR; /* Check the variable. */ if (nc_inq_var(grpid, 0, name_in, &xtype_in, &ndims_in, dimid_in, &natts_in)) ERR; if (strcmp(name_in, BATTLE_RECORD) || xtype_in != NC_INT || ndims_in != 1 || dimid_in[0] != 0 || natts_in != 0) ERR; /* Check the data. */ if (nc_get_var(grpid, 0, data_in)) ERR; for (i = 0; i < DIM1_LEN; i++) if (data_in[i] != data_out[i]) ERR; if (nc_close(ncid)) ERR; } SUMMARIZE_ERR; #endif /* USE_SZIP */ FINAL_RESULTS; }
/* * Write 2D data to HDF5 file */ void cData2d::writeHDF5(char* filename){ // Figure out the HDF5 data type hid_t out_type_id = 0; if(sizeof(tData2d) == sizeof(float)) out_type_id = H5T_NATIVE_FLOAT; else if(sizeof(tData2d) == sizeof(double)) out_type_id = H5T_NATIVE_DOUBLE; else { printf("2dData::writeHDF5: unsuppoted data type\n"); exit(1); } // Create the file and data group hid_t file_id; file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); H5Gcreate1(file_id,"data",0); // Data space dimensions int ndims = 2; hsize_t dims[ndims]; dims[0] = ny; dims[1] = nx; // Write the data hid_t dataspace_id; hid_t dataset_id; dataspace_id = H5Screate_simple(ndims, dims, NULL); dataset_id = H5Dcreate1(file_id, "/data/data", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id,out_type_id , H5S_ALL, H5S_ALL,H5P_DEFAULT, data)< 0){ printf("2dData::writeHDF5: Error writing data to file\n"); exit(1); } // Close and exit H5Dclose(dataset_id); // Cleanup stale IDs hid_t ids[256]; int n_ids = H5Fget_obj_ids(file_id, H5F_OBJ_ALL, 256, ids); for (long i=0; i<n_ids; i++ ) { hid_t id; H5I_type_t type; id = ids[i]; type = H5Iget_type(id); if ( type == H5I_GROUP ) H5Gclose(id); if ( type == H5I_DATASET ) H5Dclose(id); if ( type == H5I_DATASPACE ) H5Sclose(id); //if ( type == H5I_DATATYPE ) // H5Dclose(id); } H5Fclose(file_id); }
int main() { printf("\n*** Checking HDF5 dimension scales.\n"); #define GRP_NAME "simple_scales" #define DIMSCALE_NAME "dimscale" #define NAME_ATTRIBUTE "Billy-Bob" #define VAR1_NAME "var1" #define VAR2_NAME "var2" #define VAR3_NAME "var3" #define DIM1_LEN 3 #define DIM2_LEN 2 #define FIFTIES_SONG "Mamma said they'll be days like this. They'll be days like this, my mamma said." printf("*** Creating simple dimension scales file..."); { hid_t fileid, grpid, dimscaleid; hid_t dimscale_spaceid, var1_spaceid, var3_spaceid; hid_t var1_datasetid, var2_datasetid, var3_datasetid; hsize_t dims[2] = {DIM1_LEN, DIM2_LEN}; hsize_t dimscale_dims[1] = {DIM1_LEN}; /* Open file and create group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; /* Create our dimension scale. Use the built-in NAME attribute * on the dimscale. */ if ((dimscale_spaceid = H5Screate_simple(1, dimscale_dims, dimscale_dims)) < 0) ERR; if ((dimscaleid = H5Dcreate(grpid, DIMSCALE_NAME, H5T_NATIVE_INT, dimscale_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(dimscaleid, NAME_ATTRIBUTE) < 0) ERR; /* Create a 1D variable which uses the dimscale. Attach a label * to this scale. */ if ((var1_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; if ((var1_datasetid = H5Dcreate(grpid, VAR1_NAME, H5T_NATIVE_INT, var1_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(var1_datasetid, dimscaleid, 0) < 0) ERR; if (H5DSset_label(var1_datasetid, 0, FIFTIES_SONG) < 0) ERR; /* Create a 1D variabls that doesn't use the dimension scale. */ if ((var2_datasetid = H5Dcreate(grpid, VAR2_NAME, H5T_NATIVE_INT, var1_spaceid, H5P_DEFAULT)) < 0) ERR; /* Create a 2D dataset which uses the scale for one of its * dimensions. */ if ((var3_spaceid = H5Screate_simple(2, dims, dims)) < 0) ERR; if ((var3_datasetid = H5Dcreate(grpid, VAR3_NAME, H5T_NATIVE_INT, var3_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(var3_datasetid, dimscaleid, 0) < 0) ERR; /* Close up the shop. */ if (H5Dclose(dimscaleid) < 0 || H5Dclose(var1_datasetid) < 0 || H5Dclose(var2_datasetid) < 0 || H5Dclose(var3_datasetid) < 0 || H5Sclose(var1_spaceid) < 0 || H5Sclose(var3_spaceid) < 0 || H5Sclose(dimscale_spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; /* HELP! If you are reading this in the future, and time * machines have been invented, please come back to July 10, * 2005, the Java Java coffee shop in Lafayette, 8:00 am MST +- * 20 minutes. Bring back some advanced weapons systems to * destroy the sound system here, which is playing 50's rock and * roll. Do-op, do-op, la-ma la-ma, ding dong. Save me!!! (Mind * you, James Brown is a different story!) */ } SUMMARIZE_ERR; printf("*** Checking that simple dimscale file can be read..."); { hid_t fileid, grpid, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[STR_LEN + 1]; htri_t is_scale; int num_scales; /* Reopen the file and group. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR; /* Loop through datasets to find variables. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of the * object. Confusingly, this is a different type than the type * of a variable. This type might be better called "class" or * "type of type" */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, STR_LEN) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with groups and datasets. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /*Close the last datasetid, if one is open. */ if (datasetid > 0) { H5Dclose(datasetid); } if ((datasetid = H5Dopen(grpid, obj_name)) < 0) ERR; if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale && strcmp(obj_name, DIMSCALE_NAME)) ERR; if (is_scale) { char nom_de_quincey[STR_LEN+1]; /* A dimscale comes with a NAME attribute, in * addition to its real name. */ if (H5DSget_scale_name(datasetid, nom_de_quincey, STR_LEN) < 0) ERR; if (strcmp(nom_de_quincey, NAME_ATTRIBUTE)) ERR; /*printf("found scale %s, NAME %s\n", obj_name, nom_de_quincey);*/ } else { char label[STR_LEN+1]; /* Here's how to get the number of scales attached * to the dataset. I would think that this would * return 0 scales for a dataset that doesn't have * scales, but instead it errors. So take an error * to be the same as no dimension scales. */ num_scales = H5DSget_num_scales(datasetid, 0); if (strcmp(obj_name, VAR1_NAME) == 0 && num_scales != 1) ERR; if (strcmp(obj_name, VAR2_NAME) == 0 && num_scales > 0) ERR; if (strcmp(obj_name, VAR3_NAME) == 0 && num_scales != 1) ERR; /* There's also a label for dimension 0 of var1. */ if (strcmp(obj_name, VAR1_NAME) == 0) { if (H5DSget_label(datasetid, 0, label, STR_LEN) < 0) ERR; if (strcmp(label, FIFTIES_SONG)) ERR; } } break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Dclose(datasetid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating simple dimension scales file with lots of datasets..."); #define NUM_DATASETS 500 { hid_t fileid, grpid, dimscaleid; hid_t dimscale_spaceid, var1_spaceid; hid_t var1_datasetid[NUM_DATASETS]; hsize_t dims[2] = {DIM1_LEN, DIM2_LEN}; hsize_t dimscale_dims[1] = {DIM1_LEN}; char var_name[STR_LEN + 1]; int v; /* Open file and create group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; /* Create our dimension scale. Use the built-in NAME attribute * on the dimscale. */ if ((dimscale_spaceid = H5Screate_simple(1, dimscale_dims, dimscale_dims)) < 0) ERR; if ((dimscaleid = H5Dcreate(grpid, DIMSCALE_NAME, H5T_NATIVE_INT, dimscale_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(dimscaleid, NAME_ATTRIBUTE) < 0) ERR; /* Create many 1D datasets which use the dimscale. */ if ((var1_spaceid = H5Screate_simple(1, dims, dims)) < 0) ERR; for (v = 0; v < NUM_DATASETS; v++) { sprintf(var_name, "var_%d", v); if ((var1_datasetid[v] = H5Dcreate(grpid, var_name, H5T_NATIVE_INT, var1_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSattach_scale(var1_datasetid[v], dimscaleid, 0) < 0) ERR; } /* Close up the shop. */ for (v = 0; v < NUM_DATASETS; v++) if (H5Dclose(var1_datasetid[v]) < 0) ERR; if (H5Dclose(dimscaleid) < 0 || H5Sclose(var1_spaceid) < 0 || H5Sclose(dimscale_spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating a file with an unlimited dimension scale..."); { hid_t fileid, grpid, spaceid, datasetid, dimscaleid, cparmsid; hsize_t dims[1] = {1}, maxdims[1] = {H5S_UNLIMITED}; /* Create file and group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; if ((spaceid = H5Screate_simple(1, dims, maxdims)) < 0) ERR; /* Modify dataset creation properties, i.e. enable chunking */ if ((cparmsid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; if (H5Pset_chunk(cparmsid, 1, dims) < 0) ERR; /* Create our dimension scale, as an unlimited dataset. */ if ((dimscaleid = H5Dcreate(grpid, DIMSCALE_NAME, H5T_NATIVE_INT, spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(dimscaleid, NAME_ATTRIBUTE) < 0) ERR; /* Create a variable which uses it. */ if ((datasetid = H5Dcreate(grpid, VAR1_NAME, H5T_NATIVE_INT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(datasetid, dimscaleid, 0) < 0) ERR; if (H5DSset_label(datasetid, 0, "dimension label") < 0) ERR; /* Close up the shop. */ if (H5Dclose(dimscaleid) < 0 || H5Dclose(datasetid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; #ifdef EXTRA_TESTS printf("*** Checking that unlimited dimscale file can be read..."); { hid_t fileid, grpid, spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[STR_LEN + 1]; htri_t is_scale; int num_scales; hsize_t dims[1], maxdims[1]; /* Reopen the file and group. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR; /* Loop through datasets to find variables. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, STR_LEN) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with groups and datasets. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /*Close the last datasetid, if one is open. */ if (datasetid > 0) { H5Dclose(datasetid); datasetid = 0; } /* Open the dataset. */ if ((datasetid = H5Dopen(grpid, obj_name)) < 0) ERR; /* This should be an unlimited dataset. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, maxdims) < 0) ERR; if (maxdims[0] != H5S_UNLIMITED) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale && strcmp(obj_name, DIMSCALE_NAME)) ERR; if (is_scale) { char nom_de_quincey[STR_LEN+1]; /* A dimscale comes with a NAME attribute, in * addition to its real name. */ if (H5DSget_scale_name(datasetid, nom_de_quincey, STR_LEN) < 0) ERR; /*printf("found scale %s, NAME %s\n", obj_name, nom_de_quincey);*/ } else { char label[STR_LEN+1]; int visitor_data = 0; /* Here's how to get the number of scales attached * to the dataset's dimension 0. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn about them. */ if (H5DSiterate_scales(datasetid, 0, NULL, alien_visitor, &visitor_data) < 0) ERR; /* There's also a label for dimension 0. */ if (H5DSget_label(datasetid, 0, label, STR_LEN) < 0) ERR; /*printf("found non-scale dataset %s, label %s\n", obj_name, label);*/ } break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Dclose(datasetid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating some 3D datasets using shared dimscales..."); { #define NDIMS 3 #define TIME_DIM 0 #define LAT_DIM 1 #define LON_DIM 2 #define LAT_LEN 2 #define LON_LEN 3 #define LAT_NAME "Lat" #define LON_NAME "Lon" #define TIME_NAME "Time" #define PRES_NAME "Pressure" #define TEMP_NAME "Temperature" hid_t fileid, grpid, lat_spaceid, lon_spaceid, time_spaceid, spaceid; hid_t lat_scaleid, lon_scaleid, time_scaleid; hid_t pres_dsid, temp_dsid, cparmsid; hsize_t dims[NDIMS], max_dims[NDIMS]; /* Create file and group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gcreate(fileid, GRP_NAME, 0)) < 0) ERR; /* Create 3 1D spaces for the 3 dimension scale datasets. Time * starts out as size 0. It's an unlimited dimension scale. */ dims[0] = 0; max_dims[0] = H5S_UNLIMITED; if ((time_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; dims[0] = LAT_LEN; max_dims[0] = LAT_LEN; if ((lat_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; dims[0] = LON_LEN; max_dims[0] = LON_LEN; if ((lon_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; /* Enable chunking for unlimited time scale. */ if ((cparmsid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; dims[TIME_DIM] = 1; if (H5Pset_chunk(cparmsid, 1, dims) < 0) ERR; /* Create our dimension scales. */ if ((time_scaleid = H5Dcreate(grpid, TIME_NAME, H5T_NATIVE_INT, time_spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(time_scaleid, TIME_NAME) < 0) ERR; if ((lat_scaleid = H5Dcreate(grpid, LAT_NAME, H5T_NATIVE_FLOAT, lat_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lat_scaleid, LAT_NAME) < 0) ERR; if ((lon_scaleid = H5Dcreate(grpid, LON_NAME, H5T_NATIVE_FLOAT, lon_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(lon_scaleid, LON_NAME) < 0) ERR; /* Create a space coresponding to these three dimensions. */ dims[TIME_DIM] = 0; dims[LAT_DIM] = LAT_LEN; dims[LON_DIM] = LON_LEN; max_dims[TIME_DIM] = H5S_UNLIMITED; max_dims[LAT_DIM] = LAT_LEN; max_dims[LON_DIM] = LON_LEN; if ((spaceid = H5Screate_simple(NDIMS, dims, max_dims)) < 0) ERR; /* Create two variables which use them, and attach the dimension scales. */ dims[TIME_DIM] = 1; if (H5Pset_chunk(cparmsid, NDIMS, dims) < 0) ERR; if ((pres_dsid = H5Dcreate(grpid, PRES_NAME, H5T_NATIVE_FLOAT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(pres_dsid, time_scaleid, 0) < 0) ERR; if (H5DSattach_scale(pres_dsid, lat_scaleid, 1) < 0) ERR; if (H5DSattach_scale(pres_dsid, lon_scaleid, 2) < 0) ERR; if (H5DSset_label(pres_dsid, TIME_DIM, TIME_NAME) < 0) ERR; if (H5DSset_label(pres_dsid, LAT_DIM, LAT_NAME) < 0) ERR; if (H5DSset_label(pres_dsid, LON_DIM, LON_NAME) < 0) ERR; if ((temp_dsid = H5Dcreate(grpid, TEMP_NAME, H5T_NATIVE_FLOAT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(temp_dsid, time_scaleid, 0) < 0) ERR; if (H5DSattach_scale(temp_dsid, lat_scaleid, 1) < 0) ERR; if (H5DSattach_scale(temp_dsid, lon_scaleid, 2) < 0) ERR; if (H5DSset_label(temp_dsid, TIME_DIM, TIME_NAME) < 0) ERR; if (H5DSset_label(temp_dsid, LAT_DIM, LAT_NAME) < 0) ERR; if (H5DSset_label(temp_dsid, LON_DIM, LON_NAME) < 0) ERR; /* Close up the shop. */ if (H5Dclose(pres_dsid) < 0 || H5Dclose(temp_dsid) < 0 || H5Dclose(lat_scaleid) < 0 || H5Dclose(lon_scaleid) < 0 || H5Dclose(time_scaleid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking 3D datasets created with shared dimscales..."); { hid_t fileid, grpid, spaceid = 0, datasetid = 0; hsize_t num_obj, i; int obj_class; char obj_name[STR_LEN + 1]; htri_t is_scale; int num_scales; hsize_t dims[NDIMS], max_dims[NDIMS]; int d; /* Reopen the file and group. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, GRP_NAME)) < 0) ERR; /* Loop through datasets to find variables. */ if (H5Gget_num_objs(grpid, &num_obj) < 0) ERR; for (i=0; i<num_obj; i++) { /* Get the type (i.e. group, dataset, etc.), and the name of * the object. */ if ((obj_class = H5Gget_objtype_by_idx(grpid, i)) < 0) ERR; if (H5Gget_objname_by_idx(grpid, i, obj_name, STR_LEN) < 0) ERR; /*printf("\nEncountered: HDF5 object obj_class %d obj_name %s\n", obj_class, obj_name);*/ /* Deal with groups and datasets. */ switch(obj_class) { case H5G_GROUP: break; case H5G_DATASET: /* Open the dataset. */ if ((datasetid = H5Dopen(grpid, obj_name)) < 0) ERR; /*printf("\nobj_name %s\n", obj_name);*/ /* Get the dimensions of this dataset. */ if ((spaceid = H5Dget_space(datasetid)) < 0) ERR; if (H5Sget_simple_extent_dims(spaceid, dims, max_dims) < 0) ERR; /* Is this a dimscale? */ if ((is_scale = H5DSis_scale(datasetid)) < 0) ERR; if (is_scale) { char nom_de_quincey[STR_LEN+1]; /* A dimscale comes with a NAME attribute, in * addition to its real name. */ if (H5DSget_scale_name(datasetid, nom_de_quincey, STR_LEN) < 0) ERR; /*printf("found scale %s, NAME %s id 0x%x\n", obj_name, nom_de_quincey, datasetid);*/ /* Check size depending on name. */ if ((!strcmp(obj_name, LAT_NAME) && dims[TIME_DIM] != LAT_LEN) || (!strcmp(obj_name, LON_NAME) && dims[TIME_DIM] != LON_LEN) || (!strcmp(obj_name, TIME_NAME) && max_dims[TIME_DIM] != H5S_UNLIMITED)) ERR; } else { char label[STR_LEN+1]; int visitor_data = 0; /* SHould have these dimensions... */ if (dims[TIME_DIM] != 0 || dims[LAT_DIM] != LAT_LEN || dims[LON_DIM] != LON_LEN) ERR; if (max_dims[TIME_DIM] != H5S_UNLIMITED) ERR; /* Here's how to get the number of scales attached * to the dataset's dimension 0. */ if ((num_scales = H5DSget_num_scales(datasetid, 0)) < 0) ERR; if (num_scales != 1) ERR; /* Go through all dimscales for this var and learn * about them. What I want is the dataset id of each * dimscale. Then... */ for (d = 0; d < NDIMS; d++) if (H5DSiterate_scales(datasetid, d, NULL, alien_visitor, &visitor_data) < 0) ERR; /*printf("visitor_data: 0x%x\n", visitor_data);*/ /* There's also a label for each dimension. */ if (H5DSget_label(datasetid, 0, label, STR_LEN) < 0) ERR; if (strcmp(label, TIME_NAME)) ERR; if (H5DSget_label(datasetid, 1, label, STR_LEN) < 0) ERR; if (strcmp(label, LAT_NAME)) ERR; if (H5DSget_label(datasetid, 2, label, STR_LEN) < 0) ERR; if (strcmp(label, LON_NAME)) ERR; } if (H5Dclose(datasetid) < 0) ERR; break; case H5G_TYPE: break; case H5G_LINK: break; default: printf("Unknown object class %d!", obj_class); } } /* Close up the shop. */ if (H5Sclose(spaceid) < 0 || H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Creating 3D datasets using shared dimscales in groups..."); { #define FATHER "Adam" #define GOOD_CHILD "Able" #define BAD_CHILD "Cain" #define DISTANCE_LEN 3 #define SMELLINESS_NAME "Smelliness" #define DISTANCE_NAME "Distance" #define TIME_NAME "Time" #define TIME_DIM 0 #define SMELLINESS_DIM 1 #define DISTANCE_DIM 2 #define GOAT_NAME "Billy_goat_gruff" #define CAMEL_NAME "Grumpy_the_camel" hid_t fileid, smelliness_spaceid, distance_spaceid, time_spaceid, spaceid; hid_t adam_grpid, able_grpid, cain_grpid; hid_t time_scaleid, smelliness_scaleid, distance_scaleid; hid_t goat_dsid, camel_dsid, cparmsid; hsize_t dims[NDIMS], max_dims[NDIMS]; /* Create file and group. */ if ((fileid = H5Fcreate(FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT)) < 0) ERR; if ((adam_grpid = H5Gcreate(fileid, FATHER, 0)) < 0) ERR; if ((able_grpid = H5Gcreate(adam_grpid, GOOD_CHILD, 0)) < 0) ERR; if ((cain_grpid = H5Gcreate(adam_grpid, BAD_CHILD, 0)) < 0) ERR; /* Create 3 1D spaces for the 3 dimension scale datasets. Time * and smelliness starts out as 0. They are unlimited dimension * scales. */ dims[0] = 0; max_dims[0] = H5S_UNLIMITED; if ((time_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; dims[0] = 0; max_dims[0] = H5S_UNLIMITED; if ((smelliness_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; dims[0] = DISTANCE_LEN; max_dims[0] = DISTANCE_LEN; if ((distance_spaceid = H5Screate_simple(1, dims, max_dims)) < 0) ERR; /* Enable chunking for unlimited time and smelliness scale. */ if ((cparmsid = H5Pcreate(H5P_DATASET_CREATE)) < 0) ERR; dims[0] = 1; if (H5Pset_chunk(cparmsid, 1, dims) < 0) ERR; /* Create our dimension scales. */ if ((time_scaleid = H5Dcreate(adam_grpid, TIME_NAME, H5T_NATIVE_INT, time_spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(time_scaleid, TIME_NAME) < 0) ERR; if ((smelliness_scaleid = H5Dcreate(adam_grpid, SMELLINESS_NAME, H5T_NATIVE_FLOAT, smelliness_spaceid, cparmsid)) < 0) ERR; if (H5DSset_scale(smelliness_scaleid, SMELLINESS_NAME) < 0) ERR; if ((distance_scaleid = H5Dcreate(adam_grpid, DISTANCE_NAME, H5T_NATIVE_FLOAT, distance_spaceid, H5P_DEFAULT)) < 0) ERR; if (H5DSset_scale(distance_scaleid, DISTANCE_NAME) < 0) ERR; /* Create a space coresponding to these three dimensions. */ dims[TIME_DIM] = 0; dims[SMELLINESS_DIM] = 0; dims[DISTANCE_DIM] = DISTANCE_LEN; max_dims[TIME_DIM] = H5S_UNLIMITED; max_dims[SMELLINESS_DIM] = H5S_UNLIMITED; max_dims[DISTANCE_DIM] = DISTANCE_LEN; if ((spaceid = H5Screate_simple(NDIMS, dims, max_dims)) < 0) ERR; /* Set up chunking for our 3D vars. */ dims[TIME_DIM] = 1; dims[SMELLINESS_DIM] = 1; if (H5Pset_chunk(cparmsid, NDIMS, dims) < 0) ERR; /* Create two variables which use them, and attach the dimension scales. */ if ((goat_dsid = H5Dcreate(able_grpid, GOAT_NAME, H5T_NATIVE_FLOAT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(goat_dsid, time_scaleid, 0) < 0) ERR; if (H5DSattach_scale(goat_dsid, smelliness_scaleid, 1) < 0) ERR; if (H5DSattach_scale(goat_dsid, distance_scaleid, 2) < 0) ERR; if ((camel_dsid = H5Dcreate(cain_grpid, CAMEL_NAME, H5T_NATIVE_FLOAT, spaceid, cparmsid)) < 0) ERR; if (H5DSattach_scale(camel_dsid, time_scaleid, 0) < 0) ERR; if (H5DSattach_scale(camel_dsid, smelliness_scaleid, 1) < 0) ERR; if (H5DSattach_scale(camel_dsid, distance_scaleid, 2) < 0) ERR; /* Close up the shop. */ if (H5Dclose(goat_dsid) < 0 || H5Dclose(camel_dsid) < 0 || H5Dclose(smelliness_scaleid) < 0 || H5Dclose(distance_scaleid) < 0 || H5Dclose(time_scaleid) < 0 || H5Sclose(spaceid) < 0 || H5Gclose(cain_grpid) < 0 || H5Gclose(able_grpid) < 0 || H5Gclose(adam_grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; printf("*** Checking 3D datasets in groups created with shared dimscales..."); { hid_t fileid, grpid; /* Reopen the file and group. */ if ((fileid = H5Fopen(FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT)) < 0) ERR; if ((grpid = H5Gopen(fileid, FATHER)) < 0) ERR; /* If we can't scan the group, crash into a flaming heap of * smoking, smoldering rubbish. */ if (rec_scan_group(grpid)) ERR; /* Close up the shop. */ if (H5Gclose(grpid) < 0 || H5Fclose(fileid) < 0) ERR; } SUMMARIZE_ERR; #endif FINAL_RESULTS; }
/*------------------------------------------------------------------------- * Function: test_direct * * Purpose: Tests the file handle interface for DIRECT I/O driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Wednesday, 20 September 2006 * *------------------------------------------------------------------------- */ static herr_t test_direct(void) { #ifdef H5_HAVE_DIRECT hid_t file=(-1), fapl, access_fapl = -1; hid_t dset1=-1, dset2=-1, space1=-1, space2=-1; char filename[1024]; int *fhandle=NULL; hsize_t file_size; hsize_t dims1[2], dims2[1]; size_t mbound; size_t fbsize; size_t cbsize; int *points = NULL, *check = NULL, *p1, *p2; int wdata2[DSET2_DIM] = {11,12,13,14}; int rdata2[DSET2_DIM]; int i, j, n; #endif /*H5_HAVE_DIRECT*/ TESTING("DIRECT I/O file driver"); #ifndef H5_HAVE_DIRECT SKIPPED(); return 0; #else /*H5_HAVE_DIRECT*/ /* Set property list and file name for Direct driver. Set memory alignment boundary * and file block size to 512 which is the minimum for Linux 2.6. */ fapl = h5_fileaccess(); if(H5Pset_fapl_direct(fapl, MBOUNDARY, FBSIZE, CBSIZE) < 0) TEST_ERROR; h5_fixname(FILENAME[5], fapl, filename, sizeof filename); /* Verify the file access properties */ if(H5Pget_fapl_direct(fapl, &mbound, &fbsize, &cbsize) < 0) TEST_ERROR; if(mbound != MBOUNDARY || fbsize != FBSIZE || cbsize != CBSIZE) TEST_ERROR; if(H5Pset_alignment(fapl, (hsize_t)THRESHOLD, (hsize_t)FBSIZE) < 0) TEST_ERROR; H5E_BEGIN_TRY { file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl); } H5E_END_TRY; if(file<0) { H5Pclose (fapl); SKIPPED(); printf(" Probably the file system doesn't support Direct I/O\n"); return 0; } /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* Check that the driver is correct */ if(H5FD_DIRECT != H5Pget_driver(access_fapl)) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; /* Check file handle API */ if(H5Fget_vfd_handle(file, H5P_DEFAULT, (void **)&fhandle) < 0) TEST_ERROR; if(*fhandle<0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* There is no guarantee of the number of metadata allocations, but it's * 4 currently and the size of the file should be between 3 & 4 file buffer * sizes.. */ if(file_size < (FBSIZE * 3) || file_size >= (FBSIZE * 4)) TEST_ERROR; /* Allocate aligned memory for data set 1. For data set 1, everything is aligned including * memory address, size of data, and file address. */ if(0 != HDposix_memalign(&points, (size_t)FBSIZE, (size_t)(DSET1_DIM1 * DSET1_DIM2 * sizeof(int)))) TEST_ERROR; if(0 != HDposix_memalign(&check, (size_t)FBSIZE, (size_t)(DSET1_DIM1 * DSET1_DIM2 * sizeof(int)))) TEST_ERROR; /* Initialize the dset1 */ p1 = points; for(i = n = 0; i < DSET1_DIM1; i++) for(j = 0; j < DSET1_DIM2; j++) *p1++ = n++; /* Create the data space1 */ dims1[0] = DSET1_DIM1; dims1[1] = DSET1_DIM2; if((space1 = H5Screate_simple(2, dims1, NULL)) < 0) TEST_ERROR; /* Create the dset1 */ if((dset1 = H5Dcreate2(file, DSET1_NAME, H5T_NATIVE_INT, space1, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; /* Write the data to the dset1 */ if(H5Dwrite(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, points) < 0) TEST_ERROR; if(H5Dclose(dset1) < 0) TEST_ERROR; if((dset1 = H5Dopen2(file, DSET1_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; /* Read the data back from dset1 */ if(H5Dread(dset1, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, check) < 0) TEST_ERROR; /* Check that the values read are the same as the values written */ p1 = points; p2 = check; for(i = 0; i < DSET1_DIM1; i++) for(j = 0; j < DSET1_DIM2; j++) if(*p1++ != *p2++) { H5_FAILED(); printf(" Read different values than written in data set 1.\n"); printf(" At index %d,%d\n", i, j); TEST_ERROR; } /* end if */ /* Create the data space2. For data set 2, memory address and data size are not aligned. */ dims2[0] = DSET2_DIM; if((space2 = H5Screate_simple(1, dims2, NULL)) < 0) TEST_ERROR; /* Create the dset2 */ if((dset2 = H5Dcreate2(file, DSET2_NAME, H5T_NATIVE_INT, space2, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; /* Write the data to the dset1 */ if(H5Dwrite(dset2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata2) < 0) TEST_ERROR; if(H5Dclose(dset2) < 0) TEST_ERROR; if((dset2 = H5Dopen2(file, DSET2_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; /* Read the data back from dset1 */ if(H5Dread(dset2, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, rdata2) < 0) TEST_ERROR; /* Check that the values read are the same as the values written */ for(i = 0; i < DSET2_DIM; i++) if(wdata2[i] != rdata2[i]) { H5_FAILED(); printf(" Read different values than written in data set 2.\n"); printf(" At index %d\n", i); TEST_ERROR; } /* end if */ if(H5Sclose(space1) < 0) TEST_ERROR; if(H5Dclose(dset1) < 0) TEST_ERROR; if(H5Sclose(space2) < 0) TEST_ERROR; if(H5Dclose(dset2) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; HDassert(points); HDfree(points); HDassert(check); HDfree(check); h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Pclose(fapl); H5Sclose(space1); H5Dclose(dset1); H5Sclose(space2); H5Dclose(dset2); H5Fclose(file); } H5E_END_TRY; if(points) HDfree(points); if(check) HDfree(check); return -1; #endif /*H5_HAVE_DIRECT*/ }
/* * Create the skeleton use case file for testing. * It has one 3d dataset using chunked storage. * The dataset is (unlimited, chunksize, chunksize). * Dataset type is 2 bytes integer. * It starts out "empty", i.e., first dimension is 0. * * Return: 0 succeed; -1 fail. */ static int create_file(void) { hsize_t dims[3]; /* Dataset starting dimensions */ hid_t fid; /* File ID for new HDF5 file */ hid_t dcpl; /* Dataset creation property list */ hid_t sid; /* Dataspace ID */ hid_t dsid; /* Dataset ID */ hid_t fapl; /* File access property list */ H5D_chunk_index_t idx_type; /* Chunk index type */ /* Create the file */ if((fapl = H5Pcreate(H5P_FILE_ACCESS)) < 0) return -1; if(H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0) return -1; if((fid = H5Fcreate(filename_g, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) return -1; /* Set up dimension sizes */ dims[0] = 0; dims[1] = dims[2] = max_dims_g[1]; /* Create dataspace for creating datasets */ if((sid = H5Screate_simple(3, dims, max_dims_g)) < 0) return -1; /* Create dataset creation property list */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) return -1; if(H5Pset_chunk(dcpl, 3, chunkdims_g) < 0) return -1; /* create dataset of progname */ if((dsid = H5Dcreate2(fid, progname_g, UC_DATATYPE, sid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) return -1; /* Check that the chunk index type is not version 1 B-tree. * Version 1 B-trees are not supported under SWMR. */ if(H5D__layout_idx_type_test(dsid, &idx_type) < 0) return -1; if(idx_type == H5D_CHUNK_IDX_BTREE) { fprintf(stderr, "ERROR: Chunk index is version 1 B-tree: aborting.\n"); return -1; } /* Close everything */ if(H5Dclose(dsid) < 0) return -1; if(H5Pclose(fapl) < 0) return -1; if(H5Pclose(dcpl) < 0) return -1; if(H5Sclose(sid) < 0) return -1; if(H5Fclose(fid) < 0) return -1; return 0; } /* create_file() */
/*------------------------------------------------------------------------- * Function: test_family * * Purpose: Tests the file handle interface for FAMILY driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * *------------------------------------------------------------------------- */ static herr_t test_family(void) { hid_t file=(-1), fapl, fapl2=(-1), space=(-1), dset=(-1); hid_t access_fapl = -1; char filename[1024]; char dname[]="dataset"; unsigned int i, j; int *fhandle=NULL, *fhandle2=NULL; int buf[FAMILY_NUMBER][FAMILY_SIZE]; hsize_t dims[2]={FAMILY_NUMBER, FAMILY_SIZE}; hsize_t file_size; TESTING("FAMILY file driver"); /* Set property list and file name for FAMILY driver */ fapl = h5_fileaccess(); if(H5Pset_fapl_family(fapl, (hsize_t)FAMILY_SIZE, H5P_DEFAULT) < 0) TEST_ERROR; h5_fixname(FILENAME[2], fapl, filename, sizeof filename); if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Test different wrong ways to reopen family files where there's only * one member file existing. */ if(test_family_opens(filename, fapl) < 0) TEST_ERROR; /* Reopen the file with default member file size */ if(H5Pset_fapl_family(fapl, (hsize_t)H5F_FAMILY_DEFAULT, H5P_DEFAULT) < 0) TEST_ERROR; if((file=H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* The file size is supposed to be about 800 bytes right now. */ if(file_size < (KB / 2) || file_size > KB) TEST_ERROR; /* Create and write dataset */ if((space=H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* Check that the driver is correct */ if(H5FD_FAMILY != H5Pget_driver(access_fapl)) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; if((dset=H5Dcreate2(file, dname, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; for(i = 0; i < FAMILY_NUMBER; i++) for(j = 0; j < FAMILY_SIZE; j++) buf[i][j] = (int)((i * 10000) + j); if(H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) TEST_ERROR; /* check file handle API */ if((fapl2 = H5Pcreate(H5P_FILE_ACCESS)) < 0) TEST_ERROR; if(H5Pset_family_offset(fapl2, (hsize_t)0) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle) < 0) TEST_ERROR; if(*fhandle < 0) TEST_ERROR; if(H5Pset_family_offset(fapl2, (hsize_t)(FAMILY_SIZE*2)) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle2) < 0) TEST_ERROR; if(*fhandle2 < 0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* Some data has been written. The file size should be bigger (18KB+976) * bytes if int size is 4 bytes) now. */ #if H5_SIZEOF_INT <= 4 if(file_size < (18 * KB) || file_size > (20 * KB)) TEST_ERROR; #elif H5_SIZEOF_INT >= 8 if(file_size < (32 * KB) || file_size > (40 * KB)) TEST_ERROR; #endif if(H5Sclose(space) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Pclose(fapl2) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Test different wrong ways to reopen family files when there're multiple * member files existing. */ if(test_family_opens(filename, fapl) < 0) TEST_ERROR; /* Reopen the file with correct member file size. */ if(H5Pset_fapl_family(fapl, (hsize_t)FAMILY_SIZE, H5P_DEFAULT) < 0) TEST_ERROR; if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Sclose(space); H5Dclose(dset); H5Pclose (fapl2); H5Fclose(file); } H5E_END_TRY; return -1; }
OPENPSTD_SHARED_EXPORT void HDF5::ExportData(std::string format, std::shared_ptr <PSTDFile> file, std::string output, std::vector<int> domains, int startFrame, int endFrame) { hid_t file_id; Kernel::MockKernel k; file_id = H5Fcreate(output.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); hid_t frame_dir_id = H5Gcreate2(file_id, "/frame", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); hid_t receiver_dir_id = H5Gcreate2(file_id, "/receiver", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); k.initialize_kernel(file->GetSceneConf(), std::make_shared<OpenPSTD::Kernel::KernelCallbackLog>()); auto metadata = k.get_metadata(); if (domains.size() == 0) { int domainCount = file->GetResultsDomainCount(); for (int d = 0; d < domainCount; ++d) { domains.push_back(d); } } for(int d : domains) { std::string domainLoc = "/frame/" + boost::lexical_cast<std::string>(d); hid_t frame_index_id = H5Gcreate2(file_id, domainLoc.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); if (startFrame == -1) startFrame = 0; if (endFrame == -1) endFrame = file->GetResultsFrameCount(d) - 1; for (int f = startFrame; f <= endFrame; ++f) { std::string location = domainLoc + "/" + boost::lexical_cast<std::string>(f); std::vector<hsize_t> size; size.push_back(metadata.DomainMetadata[d][0]); size.push_back(metadata.DomainMetadata[d][1]); //get data and write to file auto data = file->GetResultsFrame(f, d); H5LTmake_dataset(file_id, location.c_str(), 2, size.data(), H5T_NATIVE_FLOAT, data->data()); } H5Gclose(frame_index_id); } int receiverCount = file->GetResultsReceiverCount(); for(int r = 0; r < receiverCount; r++) { std::string receiverLoc = "/receiver/" + boost::lexical_cast<std::string>(r); auto data = file->GetReceiverData(r); std::vector<hsize_t> size; size.push_back(data->size()); H5LTmake_dataset(file_id, receiverLoc.c_str(), 1, size.data(), H5T_NATIVE_FLOAT, data->data()); } /* close file */ H5Gclose(frame_dir_id); H5Gclose(receiver_dir_id); H5Fclose (file_id); }
/*------------------------------------------------------------------------- * Function: test_multi * * Purpose: Tests the file handle interface for MUTLI driver * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * Tuesday, Sept 24, 2002 * *------------------------------------------------------------------------- */ static herr_t test_multi(void) { hid_t file=(-1), fapl, fapl2=(-1), dset=(-1), space=(-1); hid_t root, attr, aspace, atype; hid_t access_fapl = -1; char filename[1024]; int *fhandle2=NULL, *fhandle=NULL; hsize_t file_size; H5FD_mem_t mt, memb_map[H5FD_MEM_NTYPES]; hid_t memb_fapl[H5FD_MEM_NTYPES]; haddr_t memb_addr[H5FD_MEM_NTYPES]; const char *memb_name[H5FD_MEM_NTYPES]; char sv[H5FD_MEM_NTYPES][32]; hsize_t dims[2]={MULTI_SIZE, MULTI_SIZE}; hsize_t adims[1]={1}; char dname[]="dataset"; char meta[] = "this is some metadata on this file"; int i, j; int buf[MULTI_SIZE][MULTI_SIZE]; TESTING("MULTI file driver"); /* Set file access property list for MULTI driver */ fapl = h5_fileaccess(); HDmemset(memb_map, 0, sizeof memb_map); HDmemset(memb_fapl, 0, sizeof memb_fapl); HDmemset(memb_name, 0, sizeof memb_name); HDmemset(memb_addr, 0, sizeof memb_addr); HDmemset(sv, 0, sizeof sv); for(mt=H5FD_MEM_DEFAULT; mt<H5FD_MEM_NTYPES; H5_INC_ENUM(H5FD_mem_t,mt)) { memb_fapl[mt] = H5P_DEFAULT; memb_map[mt] = H5FD_MEM_SUPER; } memb_map[H5FD_MEM_DRAW] = H5FD_MEM_DRAW; memb_map[H5FD_MEM_BTREE] = H5FD_MEM_BTREE; memb_map[H5FD_MEM_GHEAP] = H5FD_MEM_GHEAP; sprintf(sv[H5FD_MEM_SUPER], "%%s-%c.h5", 's'); memb_name[H5FD_MEM_SUPER] = sv[H5FD_MEM_SUPER]; memb_addr[H5FD_MEM_SUPER] = 0; sprintf(sv[H5FD_MEM_BTREE], "%%s-%c.h5", 'b'); memb_name[H5FD_MEM_BTREE] = sv[H5FD_MEM_BTREE]; memb_addr[H5FD_MEM_BTREE] = HADDR_MAX/4; sprintf(sv[H5FD_MEM_DRAW], "%%s-%c.h5", 'r'); memb_name[H5FD_MEM_DRAW] = sv[H5FD_MEM_DRAW]; memb_addr[H5FD_MEM_DRAW] = HADDR_MAX/2; sprintf(sv[H5FD_MEM_GHEAP], "%%s-%c.h5", 'g'); memb_name[H5FD_MEM_GHEAP] = sv[H5FD_MEM_GHEAP]; memb_addr[H5FD_MEM_GHEAP] = (HADDR_MAX/4)*3; if(H5Pset_fapl_multi(fapl, memb_map, memb_fapl, memb_name, memb_addr, TRUE) < 0) TEST_ERROR; h5_fixname(FILENAME[4], fapl, filename, sizeof filename); if((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Test wrong ways to reopen multi files */ if(test_multi_opens(filename) < 0) TEST_ERROR; /* Reopen the file */ if((file = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Create and write data set */ if((space = H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR; /* Retrieve the access property list... */ if ((access_fapl = H5Fget_access_plist(file)) < 0) TEST_ERROR; /* Check that the driver is correct */ if(H5FD_MULTI != H5Pget_driver(access_fapl)) TEST_ERROR; /* ...and close the property list */ if (H5Pclose(access_fapl) < 0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* Before any data is written, the raw data file is empty. So * the file size is only the size of b-tree + HADDR_MAX/4. */ if(file_size < HADDR_MAX/4 || file_size > HADDR_MAX/2) TEST_ERROR; if((dset=H5Dcreate2(file, dname, H5T_NATIVE_INT, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; for(i=0; i<MULTI_SIZE; i++) for(j=0; j<MULTI_SIZE; j++) buf[i][j] = i*10000+j; if(H5Dwrite(dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, buf) < 0) TEST_ERROR; if((fapl2=H5Pcreate(H5P_FILE_ACCESS)) < 0) TEST_ERROR; if(H5Pset_multi_type(fapl2, H5FD_MEM_SUPER) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle) < 0) TEST_ERROR; if(*fhandle<0) TEST_ERROR; if(H5Pset_multi_type(fapl2, H5FD_MEM_DRAW) < 0) TEST_ERROR; if(H5Fget_vfd_handle(file, fapl2, (void **)&fhandle2) < 0) TEST_ERROR; if(*fhandle2<0) TEST_ERROR; /* Check file size API */ if(H5Fget_filesize(file, &file_size) < 0) TEST_ERROR; /* After the data is written, the file size is huge because the * beginning of raw data file is set at HADDR_MAX/2. It's supposed * to be (HADDR_MAX/2 + 128*128*4) */ if(file_size < HADDR_MAX/2 || file_size > HADDR_MAX) TEST_ERROR; if(H5Sclose(space) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Pclose(fapl2) < 0) TEST_ERROR; /* Create and write attribute for the root group. */ if((root = H5Gopen2(file, "/", H5P_DEFAULT)) < 0) FAIL_STACK_ERROR /* Attribute string. */ if((atype = H5Tcopy(H5T_C_S1)) < 0) TEST_ERROR; if(H5Tset_size(atype, strlen(meta) + 1) < 0) TEST_ERROR; if(H5Tset_strpad(atype, H5T_STR_NULLTERM) < 0) TEST_ERROR; /* Create and write attribute */ if((aspace = H5Screate_simple(1, adims, NULL)) < 0) TEST_ERROR; if((attr = H5Acreate2(root, "Metadata", atype, aspace, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Awrite(attr, atype, meta) < 0) TEST_ERROR; /* Close IDs */ if(H5Tclose(atype) < 0) TEST_ERROR; if(H5Sclose(aspace) < 0) TEST_ERROR; if(H5Aclose(attr) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Sclose(space); H5Dclose(dset); H5Pclose(fapl); H5Pclose(fapl2); H5Fclose(file); } H5E_END_TRY; return -1; }
/* * Open a file through the HDF5 interface. */ static void *HDF5_Open(char *testFileName, IOR_param_t * param) { hid_t accessPropList, createPropList; hsize_t memStart[NUM_DIMS], dataSetDims[NUM_DIMS], memStride[NUM_DIMS], memCount[NUM_DIMS], memBlock[NUM_DIMS], memDataSpaceDims[NUM_DIMS]; int tasksPerDataSet; unsigned fd_mode = (unsigned)0; hid_t *fd; MPI_Comm comm; MPI_Info mpiHints = MPI_INFO_NULL; fd = (hid_t *) malloc(sizeof(hid_t)); if (fd == NULL) ERR("malloc() failed"); /* * HDF5 uses different flags than those for POSIX/MPIIO */ if (param->open == WRITE) { /* WRITE flags */ param->openFlags = IOR_TRUNC; } else { /* READ or check WRITE/READ flags */ param->openFlags = IOR_RDONLY; } /* set IOR file flags to HDF5 flags */ /* -- file open flags -- */ if (param->openFlags & IOR_RDONLY) { fd_mode |= H5F_ACC_RDONLY; } if (param->openFlags & IOR_WRONLY) { fprintf(stdout, "File write only not implemented in HDF5\n"); } if (param->openFlags & IOR_RDWR) { fd_mode |= H5F_ACC_RDWR; } if (param->openFlags & IOR_APPEND) { fprintf(stdout, "File append not implemented in HDF5\n"); } if (param->openFlags & IOR_CREAT) { fd_mode |= H5F_ACC_CREAT; } if (param->openFlags & IOR_EXCL) { fd_mode |= H5F_ACC_EXCL; } if (param->openFlags & IOR_TRUNC) { fd_mode |= H5F_ACC_TRUNC; } if (param->openFlags & IOR_DIRECT) { fprintf(stdout, "O_DIRECT not implemented in HDF5\n"); } /* set up file creation property list */ createPropList = H5Pcreate(H5P_FILE_CREATE); HDF5_CHECK(createPropList, "cannot create file creation property list"); /* set size of offset and length used to address HDF5 objects */ HDF5_CHECK(H5Pset_sizes (createPropList, sizeof(hsize_t), sizeof(hsize_t)), "cannot set property list properly"); /* set up file access property list */ accessPropList = H5Pcreate(H5P_FILE_ACCESS); HDF5_CHECK(accessPropList, "cannot create file access property list"); /* * someday HDF5 implementation will allow subsets of MPI_COMM_WORLD */ /* store MPI communicator info for the file access property list */ if (param->filePerProc) { comm = MPI_COMM_SELF; } else { comm = testComm; } SetHints(&mpiHints, param->hintsFileName); /* * note that with MP_HINTS_FILTERED=no, all key/value pairs will * be in the info object. The info object that is attached to * the file during MPI_File_open() will only contain those pairs * deemed valid by the implementation. */ /* show hints passed to file */ if (rank == 0 && param->showHints) { fprintf(stdout, "\nhints passed to access property list {\n"); ShowHints(&mpiHints); fprintf(stdout, "}\n"); } HDF5_CHECK(H5Pset_fapl_mpio(accessPropList, comm, mpiHints), "cannot set file access property list"); /* set alignment */ HDF5_CHECK(H5Pset_alignment(accessPropList, param->setAlignment, param->setAlignment), "cannot set alignment"); /* open file */ if (param->open == WRITE) { /* WRITE */ *fd = H5Fcreate(testFileName, fd_mode, createPropList, accessPropList); HDF5_CHECK(*fd, "cannot create file"); } else { /* READ or CHECK */ *fd = H5Fopen(testFileName, fd_mode, accessPropList); HDF5_CHECK(*fd, "cannot open file"); } /* show hints actually attached to file handle */ if (param->showHints || (1) /* WEL - this needs fixing */ ) { if (rank == 0 && (param->showHints) /* WEL - this needs fixing */ ) { WARN("showHints not working for HDF5"); } } else { MPI_Info mpiHintsCheck = MPI_INFO_NULL; hid_t apl; apl = H5Fget_access_plist(*fd); HDF5_CHECK(H5Pget_fapl_mpio(apl, &comm, &mpiHintsCheck), "cannot get info object through HDF5"); if (rank == 0) { fprintf(stdout, "\nhints returned from opened file (HDF5) {\n"); ShowHints(&mpiHintsCheck); fprintf(stdout, "}\n"); if (1 == 1) { /* request the MPIIO file handle and its hints */ MPI_File *fd_mpiio; HDF5_CHECK(H5Fget_vfd_handle (*fd, apl, (void **)&fd_mpiio), "cannot get MPIIO file handle"); MPI_CHECK(MPI_File_get_info (*fd_mpiio, &mpiHintsCheck), "cannot get info object through MPIIO"); fprintf(stdout, "\nhints returned from opened file (MPIIO) {\n"); ShowHints(&mpiHintsCheck); fprintf(stdout, "}\n"); } } MPI_CHECK(MPI_Barrier(testComm), "barrier error"); } /* this is necessary for resetting various parameters needed for reopening and checking the file */ newlyOpenedFile = TRUE; HDF5_CHECK(H5Pclose(createPropList), "cannot close creation property list"); HDF5_CHECK(H5Pclose(accessPropList), "cannot close access property list"); /* create property list for serial/parallel access */ xferPropList = H5Pcreate(H5P_DATASET_XFER); HDF5_CHECK(xferPropList, "cannot create transfer property list"); /* set data transfer mode */ if (param->collective) { HDF5_CHECK(H5Pset_dxpl_mpio(xferPropList, H5FD_MPIO_COLLECTIVE), "cannot set collective data transfer mode"); } else { HDF5_CHECK(H5Pset_dxpl_mpio (xferPropList, H5FD_MPIO_INDEPENDENT), "cannot set independent data transfer mode"); } /* set up memory data space for transfer */ memStart[0] = (hsize_t) 0; memCount[0] = (hsize_t) 1; memStride[0] = (hsize_t) (param->transferSize / sizeof(IOR_size_t)); memBlock[0] = (hsize_t) (param->transferSize / sizeof(IOR_size_t)); memDataSpaceDims[0] = (hsize_t) param->transferSize; memDataSpace = H5Screate_simple(NUM_DIMS, memDataSpaceDims, NULL); HDF5_CHECK(memDataSpace, "cannot create simple memory data space"); /* define hyperslab for memory data space */ HDF5_CHECK(H5Sselect_hyperslab(memDataSpace, H5S_SELECT_SET, memStart, memStride, memCount, memBlock), "cannot create hyperslab"); /* set up parameters for fpp or different dataset count */ if (param->filePerProc) { tasksPerDataSet = 1; } else { if (param->individualDataSets) { /* each task in segment has single data set */ tasksPerDataSet = 1; } else { /* share single data set across all tasks in segment */ tasksPerDataSet = param->numTasks; } } dataSetDims[0] = (hsize_t) ((param->blockSize / sizeof(IOR_size_t)) * tasksPerDataSet); /* create a simple data space containing information on size and shape of data set, and open it for access */ dataSpace = H5Screate_simple(NUM_DIMS, dataSetDims, NULL); HDF5_CHECK(dataSpace, "cannot create simple data space"); return (fd); }
int main (void) { hid_t vfile, file, src_space, mem_space, vspace, vdset, dset; /* Handles */ hid_t dcpl; herr_t status; hsize_t vdsdims[3] = {4*DIM0_1, VDSDIM1, VDSDIM2}, vdsdims_max[3] = {VDSDIM0, VDSDIM1, VDSDIM2}, dims[3] = {DIM0_1, DIM1, DIM2}, extdims[3] = {2*DIM0_1, DIM1, DIM2}, chunk_dims[3] = {DIM0_1, DIM1, DIM2}, dims_max[3] = {DIM0, DIM1, DIM2}, vdsdims_out[3], vdsdims_max_out[3], start[3], /* Hyperslab parameters */ stride[3], count[3], src_count[3], block[3]; hsize_t start_out[3], /* Hyperslab parameter out */ stride_out[3], count_out[3], block_out[3]; int i, j, k; H5D_layout_t layout; /* Storage layout */ size_t num_map; /* Number of mappings */ ssize_t len; /* Length of the string; also a return value */ char *filename; char *dsetname; int wdata[DIM0_1*DIM1*DIM2]; int rdata[80][10][10]; int a_rdata[20][10][10]; /* * Create source files and datasets. This step is optional. */ for (i=0; i < PLANE_STRIDE; i++) { /* * Initialize data for i-th source dataset. */ for (j = 0; j < DIM0_1*DIM1*DIM2; j++) wdata[j] = i+1; /* * Create the source files and datasets. Write data to each dataset and * close all resources. */ file = H5Fcreate (SRC_FILE[i], H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); src_space = H5Screate_simple (RANK, dims, dims_max); dcpl = H5Pcreate(H5P_DATASET_CREATE); status = H5Pset_chunk (dcpl, RANK, chunk_dims); dset = H5Dcreate2 (file, SRC_DATASET[i], H5T_NATIVE_INT, src_space, H5P_DEFAULT, dcpl, H5P_DEFAULT); status = H5Dwrite (dset, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, wdata); status = H5Sclose (src_space); status = H5Pclose (dcpl); status = H5Dclose (dset); status = H5Fclose (file); } vfile = H5Fcreate (VFILE, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* Create VDS dataspace. */ vspace = H5Screate_simple (RANK, vdsdims, vdsdims_max); /* Create dataspaces for the source dataset. */ src_space = H5Screate_simple (RANK, dims, dims_max); /* Create VDS creation property */ dcpl = H5Pcreate (H5P_DATASET_CREATE); /* Initialize hyperslab values */ start[0] = 0; start[1] = 0; start[2] = 0; stride[0] = PLANE_STRIDE; /* we will select every fifth plane in VDS */ stride[1] = 1; stride[2] = 1; count[0] = H5S_UNLIMITED; count[1] = 1; count[2] = 1; src_count[0] = H5S_UNLIMITED; src_count[1] = 1; src_count[2] = 1; block[0] = 1; block[1] = DIM1; block[2] = DIM2; /* * Build the mappings * */ status = H5Sselect_hyperslab (src_space, H5S_SELECT_SET, start, NULL, src_count, block); for (i=0; i < PLANE_STRIDE; i++) { status = H5Sselect_hyperslab (vspace, H5S_SELECT_SET, start, stride, count, block); status = H5Pset_virtual (dcpl, vspace, SRC_FILE[i], SRC_DATASET[i], src_space); start[0]++; } H5Sselect_none(vspace); /* Create a virtual dataset */ vdset = H5Dcreate2 (vfile, DATASET, H5T_NATIVE_INT, vspace, H5P_DEFAULT, dcpl, H5P_DEFAULT); status = H5Sclose (vspace); status = H5Sclose (src_space); status = H5Pclose (dcpl); /* Let's get space of the VDS and its dimension; we should get 40x10x10 */ vspace = H5Dget_space (vdset); H5Sget_simple_extent_dims (vspace, vdsdims_out, vdsdims_max_out); printf ("VDS dimensions first time \n"); printf (" Current: "); for (i=0; i<RANK; i++) printf (" %d ", (int)vdsdims_out[i]); printf ("\n"); /* Let's add data to the source datasets and check new dimensions for VDS */ for (i=0; i < PLANE_STRIDE; i++) { /* * Initialize data for i-th source dataset. */ for (j = 0; j < DIM0_1*DIM1*DIM2; j++) wdata[j] = 10*(i+1); /* * Create the source files and datasets. Write data to each dataset and * close all resources. */ file = H5Fopen (SRC_FILE[i], H5F_ACC_RDWR, H5P_DEFAULT); dset = H5Dopen2 (file, SRC_DATASET[i], H5P_DEFAULT); status = H5Dset_extent (dset, extdims); src_space = H5Dget_space (dset); start[0] = DIM0_1; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = 1; count[2] = 1; block[0] = DIM0_1; block[1] = DIM1; block[2] = DIM2; mem_space = H5Screate_simple(RANK, dims, NULL); status = H5Sselect_hyperslab (src_space, H5S_SELECT_SET, start, NULL, count, block); status = H5Dwrite (dset, H5T_NATIVE_INT, mem_space, src_space, H5P_DEFAULT, wdata); status = H5Sclose (src_space); status = H5Dclose (dset); status = H5Fclose (file); } status = H5Dclose (vdset); status = H5Fclose (vfile); /* * Now we begin the read section of this example. */ /* * Open file and dataset using the default properties. */ vfile = H5Fopen (VFILE, H5F_ACC_RDONLY, H5P_DEFAULT); vdset = H5Dopen2 (vfile, DATASET, H5P_DEFAULT); /* * Get creation property list and mapping properties. */ dcpl = H5Dget_create_plist (vdset); /* * Get storage layout. */ layout = H5Pget_layout (dcpl); if (H5D_VIRTUAL == layout) printf(" Dataset has a virtual layout \n"); else printf(" Wrong layout found \n"); /* * Find number of mappings. */ status = H5Pget_virtual_count (dcpl, &num_map); printf(" Number of mappings is %lu\n", (unsigned long)num_map); /* * Get mapping parameters for each mapping. */ for (i = 0; i < (int)num_map; i++) { printf(" Mapping %d \n", i); printf(" Selection in the virtual dataset \n"); /* Get selection in the virttual dataset */ vspace = H5Pget_virtual_vspace (dcpl, (size_t)i); if (H5Sget_select_type(vspace) == H5S_SEL_HYPERSLABS) { if (H5Sis_regular_hyperslab(vspace)) { status = H5Sget_regular_hyperslab (vspace, start_out, stride_out, count_out, block_out); printf(" start = [%llu, %llu, %llu] \n", (unsigned long long)start_out[0], (unsigned long long)start_out[1], (unsigned long long)start_out[2]); printf(" stride = [%llu, %llu, %llu] \n", (unsigned long long)stride_out[0], (unsigned long long)stride_out[1], (unsigned long long)stride_out[2]); printf(" count = [%llu, %llu, %llu] \n", (unsigned long long)count_out[0], (unsigned long long)count_out[1], (unsigned long long)count_out[2]); printf(" block = [%llu, %llu, %llu] \n", (unsigned long long)block_out[0], (unsigned long long)block_out[1], (unsigned long long)block_out[2]); } } /* Get source file name */ len = H5Pget_virtual_filename (dcpl, (size_t)i, NULL, 0); filename = (char *)malloc((size_t)len*sizeof(char)+1); H5Pget_virtual_filename (dcpl, (size_t)i, filename, len+1); printf(" Source filename %s\n", filename); /* Get source dataset name */ len = H5Pget_virtual_dsetname (dcpl, (size_t)i, NULL, 0); dsetname = (char *)malloc((size_t)len*sizeof(char)+1); H5Pget_virtual_dsetname (dcpl, (size_t)i, dsetname, len+1); printf(" Source dataset name %s\n", dsetname); /* Get selection in the source dataset */ printf(" Selection in the source dataset \n"); src_space = H5Pget_virtual_srcspace (dcpl, (size_t)i); if (H5Sget_select_type(src_space) == H5S_SEL_HYPERSLABS) { if (H5Sis_regular_hyperslab(src_space)) { status = H5Sget_regular_hyperslab (src_space, start_out, stride_out, count_out, block_out); printf(" start = [%llu, %llu, %llu] \n", (unsigned long long)start_out[0], (unsigned long long)start_out[1], (unsigned long long)start_out[2]); printf(" stride = [%llu, %llu, %llu] \n", (unsigned long long)stride_out[0], (unsigned long long)stride_out[1], (unsigned long long)stride_out[2]); printf(" count = [%llu, %llu, %llu] \n", (unsigned long long)count_out[0], (unsigned long long)count_out[1], (unsigned long long)count_out[2]); printf(" block = [%llu, %llu, %llu] \n", (unsigned long long)block_out[0], (unsigned long long)block_out[1], (unsigned long long)block_out[2]); } } H5Sclose(vspace); H5Sclose(src_space); free(filename); free(dsetname); } /* * Read data from VDS. */ vspace = H5Dget_space (vdset); H5Sget_simple_extent_dims (vspace, vdsdims_out, vdsdims_max_out); printf ("VDS dimensions second time \n"); printf (" Current: "); for (i=0; i<RANK; i++) printf (" %d ", (int)vdsdims_out[i]); printf ("\n"); /* Read all VDS data */ /* EIP We should be able to do it by using H5S_ALL instead of making selection * or using H5Sselect_all from vspace. */ start[0] = 0; start[1] = 0; start[2] = 0; count[0] = 1; count[1] = 1; count[2] = 1; block[0] = vdsdims_out[0]; block[1] = vdsdims_out[1]; block[2] = vdsdims_out[2]; status = H5Sselect_hyperslab (vspace, H5S_SELECT_SET, start, NULL, count, block); mem_space = H5Screate_simple(RANK, vdsdims_out, NULL); status = H5Dread (vdset, H5T_NATIVE_INT, mem_space, vspace, H5P_DEFAULT, rdata); printf (" All data: \n"); for (i=0; i < (int)vdsdims_out[0]; i++) { for (j=0; j < (int)vdsdims_out[1]; j++) { printf ("(%d, %d, 0)", i, j); for (k=0; k < (int)vdsdims_out[2]; k++) printf (" %d ", rdata[i][j][k]); printf ("\n"); } } /* Read VDS, but only data mapeed to dataset a.h5 */ start[0] = 0; start[1] = 0; start[2] = 0; stride[0] = PLANE_STRIDE; stride[1] = 1; stride[2] = 1; count[0] = 2*DIM0_1; count[1] = 1; count[2] = 1; block[0] = 1; block[1] = vdsdims_out[1]; block[2] = vdsdims_out[2]; dims[0] = 2*DIM0_1; status = H5Sselect_hyperslab (vspace, H5S_SELECT_SET, start, stride, count, block); mem_space = H5Screate_simple(RANK, dims, NULL); status = H5Dread (vdset, H5T_NATIVE_INT, mem_space, vspace, H5P_DEFAULT, a_rdata); printf (" All data: \n"); for (i=0; i < 2*DIM0_1; i++) { for (j=0; j < (int)vdsdims_out[1]; j++) { printf ("(%d, %d, 0)", i, j); for (k=0; k < (int)vdsdims_out[2]; k++) printf (" %d ", a_rdata[i][j][k]); printf ("\n"); } } /* * Close and release resources. */ status = H5Sclose(mem_space); status = H5Pclose (dcpl); status = H5Dclose (vdset); status = H5Fclose (vfile); return 0; }
/*------------------------------------------------------------------------- * Function: main * * Purpose: H5O_mtime_decode() test. * * Return: Success: * * Failure: * * Programmer: Robb Matzke * Thursday, July 30, 1998 * * Modifications: * Added checks for old and new modification time messages * in pre-created datafiles (generated with gen_old_mtime.c and * gen_new_mtime.c). * Quincey Koziol * Friday, January 3, 2003 * *------------------------------------------------------------------------- */ int main(void) { hid_t fapl, file, space, dset; hsize_t size[1] = {2}; time_t now; struct tm *tm; H5O_info_t oi1, oi2; signed char buf1[32], buf2[32]; char filename[1024]; h5_reset(); fapl = h5_fileaccess(); TESTING("modification time messages"); /* Create the file, create a dataset, then close the file */ h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if((file = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) TEST_ERROR; if((space = H5Screate_simple(1, size, NULL)) < 0) TEST_ERROR; if((dset = H5Dcreate2(file, "dset", H5T_NATIVE_SCHAR, space, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT)) < 0) TEST_ERROR; now = HDtime(NULL); if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Sclose(space) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* * Open the file and get the modification time. We'll test the * H5Oget_info() arguments too: being able to stat something without * knowing its name. */ h5_fixname(FILENAME[0], fapl, filename, sizeof filename); if((file = H5Fopen(filename, H5F_ACC_RDONLY, fapl)) < 0) TEST_ERROR; if(H5Oget_info_by_name(file, "dset", &oi1, H5P_DEFAULT) < 0) TEST_ERROR; if((dset = H5Dopen2(file, "dset", H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Oget_info(dset, &oi2) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Compare addresses & times from the two ways of calling H5Oget_info() */ if(oi1.addr != oi2.addr || oi1.ctime != oi2.ctime) { H5_FAILED(); puts(" Calling H5Oget_info() with the dataset ID returned"); puts(" different values than calling it with a file and dataset"); puts(" name."); goto error; } /* Compare times -- they must be within 60 seconds of one another */ if(0 == oi1.ctime) { SKIPPED(); puts(" The modification time could not be decoded on this OS."); puts(" Modification times will be mantained in the file but"); puts(" cannot be queried on this system. See H5O_mtime_decode()."); return 0; } else if(HDfabs(HDdifftime(now, oi1.ctime)) > 60.0F) { H5_FAILED(); tm = HDlocaltime(&(oi1.ctime)); HDstrftime((char*)buf1, sizeof buf1, "%Y-%m-%d %H:%M:%S", tm); tm = HDlocaltime(&now); HDstrftime((char*)buf2, sizeof buf2, "%Y-%m-%d %H:%M:%S", tm); printf(" got: %s\n ans: %s\n", buf1, buf2); goto error; } PASSED(); /* Check opening existing file with old-style modification time information * and make certain that the time is correct */ TESTING("accessing old modification time messages"); { const char *testfile = H5_get_srcdir_filename(TESTFILE1); /* Corrected test file name */ file = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT); if(file >= 0){ if(H5Oget_info_by_name(file, "/Dataset1", &oi1, H5P_DEFAULT) < 0) TEST_ERROR; if(oi1.ctime != MTIME1) { H5_FAILED(); /* If this fails, examine H5Omtime.c. Modification time is very * system dependant (e.g., on Windows DST must be hardcoded). */ puts(" Old modification time incorrect"); goto error; } if(H5Fclose(file) < 0) TEST_ERROR; } else { H5_FAILED(); printf("***cannot open the pre-created old modification test file (%s)\n", testfile); goto error; } /* end else */ } PASSED(); /* Check opening existing file with new-style modification time information * and make certain that the time is correct */ TESTING("accessing new modification time messages"); { const char *testfile = H5_get_srcdir_filename(TESTFILE2); /* Corrected test file name */ file = H5Fopen(testfile, H5F_ACC_RDONLY, H5P_DEFAULT); if(file >= 0){ if(H5Oget_info_by_name(file, "/Dataset1", &oi2, H5P_DEFAULT) < 0) TEST_ERROR; if(oi2.ctime != MTIME2) { H5_FAILED(); puts(" Modification time incorrect."); goto error; } if(H5Fclose(file) < 0) TEST_ERROR; } else { H5_FAILED(); printf("***cannot open the pre-created old modification test file (%s)\n", testfile); goto error; } /* end else */ } PASSED(); /* Verify symbol table messages are cached */ if(h5_verify_cached_stabs(FILENAME, fapl) < 0) TEST_ERROR /* All looks good */ puts("All modification time tests passed."); h5_cleanup(FILENAME, fapl); return 0; /* Something broke */ error: return 1; }
int write_data2(int n) { hid_t file_id1, dataset_a, dataset_b; /* file identifier */ hid_t fid1, dataset_ia, dataset_ib; hid_t fid2, dataset_Nx, dataset_Ny, dataset_Nz, dataset_dt, dataset_Re, dataset_mpg; hid_t complex_id; hsize_t fdim[] = { dimR, Nz, Nx / 2 }; hsize_t fdim2[] = { 1 }; herr_t ret; char filename[50]; extern mcomplex ****C, ****IC; extern int qpts, Nx, Nz, dimR, dimQ; extern double dt, re, mpg; int x, y, z; int Ny = qpts * 2 / 3; /* define compound datatype for the complex number */ typedef struct { double re; /*real part */ double im; /*imaginary part */ } complex_t; complex_id = H5Tcreate(H5T_COMPOUND, sizeof(complex_t)); H5Tinsert(complex_id, "real", HOFFSET(complex_t, re), H5T_NATIVE_DOUBLE); H5Tinsert(complex_id, "imaginary", HOFFSET(complex_t, im), H5T_NATIVE_DOUBLE); /* define some temporal matrix to store the data to the hdf file */ complex_t Matrix1[dimR][Nz][Nx / 2]; complex_t Matrix2[dimR][Nz][Nx / 2]; complex_t IMatrix1[dimR][Nz][Nx / 2]; complex_t IMatrix2[dimR][Nz][Nx / 2]; for (y = 0; y < dimR; y++) { for (z = 0; z < Nz; ++z) { for (x = 0; x < Nx / 2; ++x) { Matrix1[y][z][x].re = Re(C[z][ALPHA][y][x]); /* storing solved solutions to Matrix */ Matrix1[y][z][x].im = Im(C[z][ALPHA][y][x]); Matrix2[y][z][x].re = Re(C[z][BETA][y][x]); Matrix2[y][z][x].im = Im(C[z][BETA][y][x]); IMatrix1[y][z][x].re = Re(IC[z][ALPHA][y][x]); /* storing solved solutions to Matrix */ IMatrix1[y][z][x].im = Im(IC[z][ALPHA][y][x]); IMatrix2[y][z][x].re = Re(IC[z][BETA][y][x]); IMatrix2[y][z][x].im = Im(IC[z][BETA][y][x]); } } } sprintf(filename, "data_t=%d.h5", n); /* create File data.h5 for three data sets */ file_id1 = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* create the dataspace */ fid1 = H5Screate_simple(3, fdim, NULL); fid2 = H5Screate_simple(1, fdim2, NULL); /* create datasets with name u v w */ dataset_a = H5Dcreate1(file_id1, "/data_alpha", complex_id, fid1, H5P_DEFAULT); dataset_b = H5Dcreate1(file_id1, "/data_beta", complex_id, fid1, H5P_DEFAULT); dataset_ia = H5Dcreate1(file_id1, "/data_ialpha", complex_id, fid1, H5P_DEFAULT); dataset_ib = H5Dcreate1(file_id1, "/data_ibeta", complex_id, fid1, H5P_DEFAULT); dataset_Nx = H5Dcreate1(file_id1, "data_Nx", H5T_STD_I32LE, fid2, H5P_DEFAULT); dataset_Ny = H5Dcreate1(file_id1, "data_Ny", H5T_STD_I32LE, fid2, H5P_DEFAULT); dataset_Nz = H5Dcreate1(file_id1, "data_Nz", H5T_STD_I32LE, fid2, H5P_DEFAULT); dataset_dt = H5Dcreate1(file_id1, "data_dt", H5T_IEEE_F64LE, fid2, H5P_DEFAULT); dataset_Re = H5Dcreate1(file_id1, "data_Re", H5T_IEEE_F64LE, fid2, H5P_DEFAULT); dataset_mpg = H5Dcreate1(file_id1, "data_mpg", H5T_IEEE_F64LE, fid2, H5P_DEFAULT); /* write data to corresponding datasets */ ret = H5Dwrite(dataset_a, complex_id, H5S_ALL, fid1, H5P_DEFAULT, Matrix1); ret = H5Dwrite(dataset_b, complex_id, H5S_ALL, fid1, H5P_DEFAULT, Matrix2); ret = H5Dwrite(dataset_ia, complex_id, H5S_ALL, fid1, H5P_DEFAULT, IMatrix1); ret = H5Dwrite(dataset_ib, complex_id, H5S_ALL, fid1, H5P_DEFAULT, IMatrix2); ret = H5Dwrite(dataset_Nx, H5T_NATIVE_INT, H5S_ALL, fid2, H5P_DEFAULT, &Nx); ret = H5Dwrite(dataset_Ny, H5T_NATIVE_INT, H5S_ALL, fid2, H5P_DEFAULT, &Ny); ret = H5Dwrite(dataset_Nz, H5T_NATIVE_INT, H5S_ALL, fid2, H5P_DEFAULT, &Nz); ret = H5Dwrite(dataset_dt, H5T_IEEE_F64LE, H5S_ALL, fid2, H5P_DEFAULT, &dt); ret = H5Dwrite(dataset_Re, H5T_IEEE_F64LE, H5S_ALL, fid2, H5P_DEFAULT, &re); ret = H5Dwrite(dataset_mpg, H5T_IEEE_F64LE, H5S_ALL, fid2, H5P_DEFAULT, &mpg); /* close datasets and file */ ret = H5Dclose(dataset_a); ret = H5Dclose(dataset_b); ret = H5Dclose(dataset_ia); ret = H5Dclose(dataset_ib); ret = H5Dclose(dataset_Nx); ret = H5Dclose(dataset_Ny); ret = H5Dclose(dataset_Nz); ret = H5Dclose(dataset_dt); ret = H5Dclose(dataset_Re); ret = H5Dclose(dataset_mpg); ret = H5Sclose(fid1); ret = H5Sclose(fid2); ret = H5Fclose(file_id1); return (EXIT_SUCCESS); }
int main(void) { hid_t file_id; /* file identifier */ hid_t space_id; /* dataspace identifiers */ hid_t spacer_id; hid_t dsetv_id; /*dataset identifiers*/ hid_t dsetr_id; hsize_t dims[2] = {2,9}; hsize_t dimsr[1] = {2}; int rank = 2; int rankr =1; herr_t status; hdset_reg_ref_t ref[2]; hdset_reg_ref_t ref_out[2]; int data[2][9] = {{1,1,2,3,3,4,5,5,6},{1,2,2,3,4,4,5,6,6}}; int data_out[2][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}}; hsize_t start[2]; hsize_t count[2]; hsize_t coord[2][3] = {{0, 0, 1}, {6, 0, 8}}; unsigned num_points = 3; int i, j; size_t name_size1, name_size2; char buf1[10], buf2[10]; /* * Create file with default file access and file creation properties. */ file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /* * Create dataspace for datasets. */ space_id = H5Screate_simple(rank, dims, NULL); spacer_id = H5Screate_simple(rankr, dimsr, NULL); /* * Create integer dataset. */ dsetv_id = H5Dcreate2(file_id, dsetnamev, H5T_NATIVE_INT, space_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Write data to the dataset. */ status = H5Dwrite(dsetv_id, H5T_NATIVE_INT, H5S_ALL , H5S_ALL, H5P_DEFAULT,data); status = H5Dclose(dsetv_id); /* * Dataset with references. */ dsetr_id = H5Dcreate2(file_id, dsetnamer, H5T_STD_REF_DSETREG, spacer_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT); /* * Create a reference to the hyperslab. */ start[0] = 0; start[1] = 3; count[0] = 2; count[1] = 3; status = H5Sselect_hyperslab(space_id, H5S_SELECT_SET, start, NULL, count, NULL); status = H5Rcreate(&ref[0], file_id, dsetnamev, H5R_DATASET_REGION, space_id); /* * Create a reference to elements selection. */ status = H5Sselect_none(space_id); status = H5Sselect_elements(space_id, H5S_SELECT_SET, num_points, (const hsize_t *)coord); status = H5Rcreate(&ref[1], file_id, dsetnamev, H5R_DATASET_REGION, space_id); /* * Write dataset with the references. */ status = H5Dwrite(dsetr_id, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT,ref); /* * Close all objects. */ status = H5Sclose(space_id); status = H5Sclose(spacer_id); status = H5Dclose(dsetr_id); status = H5Fclose(file_id); /* * Reopen the file to read selections back. */ file_id = H5Fopen(filename, H5F_ACC_RDWR, H5P_DEFAULT); /* * Reopen the dataset with object references and read references * to the buffer. */ dsetr_id = H5Dopen2(file_id, dsetnamer, H5P_DEFAULT); status = H5Dread(dsetr_id, H5T_STD_REF_DSETREG, H5S_ALL, H5S_ALL, H5P_DEFAULT, ref_out); /* * Dereference the first reference. */ dsetv_id = H5Rdereference2(dsetr_id, H5P_DEFAULT, H5R_DATASET_REGION, &ref_out[0]); /* * Get name of the dataset the first region reference points to * using H5Rget_name */ name_size1 = H5Rget_name(dsetr_id, H5R_DATASET_REGION, &ref_out[0], (char*)buf1, 10); printf(" Dataset's name (returned by H5Rget_name) the reference points to is %s, name length is %d\n", buf1, (int)name_size1); /* * Get name of the dataset the first region reference points to * using H5Iget_name */ name_size2 = H5Iget_name(dsetv_id, (char*)buf2, 10); printf(" Dataset's name (returned by H5Iget_name) the reference points to is %s, name length is %d\n", buf2, (int)name_size2); space_id = H5Rget_region(dsetr_id, H5R_DATASET_REGION,&ref_out[0]); /* * Read and display hyperslab selection from the dataset. */ status = H5Dread(dsetv_id, H5T_NATIVE_INT, H5S_ALL, space_id, H5P_DEFAULT, data_out); printf("Selected hyperslab: "); for (i = 0; i <= 1; i++) { printf("\n"); for (j = 0; j <= 8; j++) printf("%d ", data_out[i][j]); } printf("\n"); /* * Close dataspace and the dataset. */ status = H5Sclose(space_id); status = H5Dclose(dsetv_id); /* * Initialize data_out array again to get point selection. */ for (i = 0; i <= 1; i++) for (j = 0; j <= 8; j++) data_out[i][j] = 0; /* * Dereference the second reference. */ dsetv_id = H5Rdereference2(dsetr_id, H5P_DEFAULT, H5R_DATASET_REGION, &ref_out[1]); space_id = H5Rget_region(dsetv_id, H5R_DATASET_REGION,&ref_out[1]); /* * Read selected data from the dataset. */ status = H5Dread(dsetv_id, H5T_NATIVE_INT, H5S_ALL, space_id, H5P_DEFAULT, data_out); printf("Selected points: "); for (i = 0; i <= 1; i++) { printf("\n"); for (j = 0; j <= 8; j++) printf("%d ", data_out[i][j]); } printf("\n"); /* * Close dataspace and the dataset. */ status = H5Sclose(space_id); status = H5Dclose(dsetv_id); status = H5Dclose(dsetr_id); status = H5Fclose(file_id); return 0; }