forAll(scalarFields_, fieldI) { Info<< " fieldWriteScalar: " << scalarFields_[fieldI] << endl; // Lookup field const volScalarField& field = obr_.lookupObject<volScalarField> ( scalarFields_[fieldI] ); // Initialize a plain continous array for the data ioScalar* scalarData; scalarData = new ioScalar[field.size()]; // Loop through the field and construct the array forAll(field, iter) { scalarData[iter] = field[iter]; } // Create the different datasets (needs to be done collectively) char datasetName[80]; hsize_t dimsf[1]; hid_t fileSpace; hid_t dsetID; hid_t plistID; hid_t plistDCreate; forAll(nCells_, proc) { // Create the dataspace for the dataset dimsf[0] = nCells_[proc]; fileSpace = H5Screate_simple(1, dimsf, NULL); // Set property to create parent groups as neccesary plistID = H5Pcreate(H5P_LINK_CREATE); H5Pset_create_intermediate_group(plistID, 1); // Set chunking, compression and other HDF5 dataset properties plistDCreate = H5Pcreate(H5P_DATASET_CREATE); dsetSetProps(1, sizeof(ioScalar), nCells_[proc], plistDCreate); // Create the dataset for points sprintf ( datasetName, "FIELDS/%s/processor%i/%s", mesh_.time().timeName().c_str(), proc, scalarFields_[fieldI].c_str() ); dsetID = H5Dcreate2 ( fileID_, datasetName, H5T_SCALAR, fileSpace, plistID, plistDCreate, H5P_DEFAULT ); H5Dclose(dsetID); H5Pclose(plistID); H5Pclose(plistDCreate); H5Sclose(fileSpace); }
/*------------------------------------------------------------------------- * Function: create_nbit_dsets_float * * Purpose: Create a dataset of FLOAT datatype with nbit filter * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * 29 March 2011 * * Modifications: * *------------------------------------------------------------------------- */ int create_nbit_dsets_float(hid_t fid, hid_t fsid, hid_t msid) { hid_t dataset; /* dataset handles */ hid_t datatype; hid_t dcpl; size_t precision, offset; float data[NX][NY]; /* data to write */ float fillvalue = -2.2f; hsize_t chunk[RANK] = {CHUNK0, CHUNK1}; int i, j; /* * Data and output buffer initialization. */ for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) data[j][i] = ((float)(i + j + 1))/3; } /* * Create the dataset creation property list, add the Scale-Offset * filter, set the chunk size, and set the fill value. */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR if(H5Pset_nbit(dcpl) < 0) TEST_ERROR if(H5Pset_chunk(dcpl, RANK, chunk) < 0) TEST_ERROR if(H5Pset_fill_value(dcpl, H5T_NATIVE_FLOAT, &fillvalue) < 0) TEST_ERROR /* Define user-defined single-precision floating-point type for dataset. * A 20-bit little-endian data type. */ if((datatype = H5Tcopy(H5T_IEEE_F32LE)) < 0) TEST_ERROR if(H5Tset_fields(datatype, (size_t)26, (size_t)20, (size_t)6, (size_t)7, (size_t)13) < 0) TEST_ERROR offset = 7; if(H5Tset_offset(datatype,offset) < 0) TEST_ERROR precision = 20; if(H5Tset_precision(datatype,precision) < 0) TEST_ERROR if(H5Tset_size(datatype, (size_t)4) < 0) TEST_ERROR if(H5Tset_ebias(datatype, (size_t)31) < 0) TEST_ERROR /* * Create a new dataset within the file using defined dataspace, * user-defined datatype, and default dataset creation properties. */ if((dataset = H5Dcreate2(fid, DATASETNAME22, datatype, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR /* * Write the data to the dataset using default transfer properties. */ if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR /* Close dataset */ if(H5Dclose(dataset) < 0) TEST_ERROR /* Now create a dataset with a big-endian type */ if(H5Tset_order(datatype, H5T_ORDER_BE) < 0) TEST_ERROR if((dataset = H5Dcreate2(fid, DATASETNAME23, datatype, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR if(H5Dclose(dataset) < 0) TEST_ERROR /* * Close/release resources. */ if(H5Pclose(dcpl) < 0) TEST_ERROR return 0; error: H5E_BEGIN_TRY { H5Pclose(dcpl); H5Dclose(dataset); } H5E_END_TRY; return -1; }
/*------------------------------------------------------------------------- * Function: create_deflate_dsets_float * * Purpose: Create a dataset of FLOAT datatype with deflate filter * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * 29 March 2011 * * Modifications: * *------------------------------------------------------------------------- */ int create_deflate_dsets_float(hid_t fid, hid_t fsid, hid_t msid) { #ifdef H5_HAVE_FILTER_DEFLATE hid_t dataset; /* dataset handles */ hid_t dcpl; float data[NX][NY]; /* data to write */ float fillvalue = -2.2f; hsize_t chunk[RANK] = {CHUNK0, CHUNK1}; int i, j; /* * Data and output buffer initialization. */ for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) data[j][i] = ((float)(i + j + 1))/3; } /* * Create the dataset creation property list, add the Scale-Offset * filter, set the chunk size, and set the fill value. */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR if(H5Pset_deflate (dcpl, 6) < 0) TEST_ERROR if(H5Pset_chunk(dcpl, RANK, chunk) < 0) TEST_ERROR if(H5Pset_fill_value(dcpl, H5T_NATIVE_FLOAT, &fillvalue) < 0) TEST_ERROR /* * Create a new dataset within the file using defined dataspace, little * endian datatype and default dataset creation properties. */ if((dataset = H5Dcreate2(fid, DATASETNAME16, H5T_IEEE_F32LE, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR /* * Write the data to the dataset using default transfer properties. */ if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR /* Close dataset */ if(H5Dclose(dataset) < 0) TEST_ERROR /* Now create a dataset with a big-endian type */ if((dataset = H5Dcreate2(fid, DATASETNAME17, H5T_IEEE_F32BE, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR if(H5Dclose(dataset) < 0) TEST_ERROR /* * Close/release resources. */ if(H5Pclose(dcpl) < 0) TEST_ERROR #else /* H5_HAVE_FILTER_DEFLATE */ const char *not_supported= "Deflate filter is not enabled. Can't create the dataset."; puts(not_supported); #endif /* H5_HAVE_FILTER_DEFLATE */ return 0; #ifdef H5_HAVE_FILTER_DEFLATE error: H5E_BEGIN_TRY { H5Pclose(dcpl); H5Dclose(dataset); } H5E_END_TRY; return -1; #endif /* H5_HAVE_FILTER_DEFLATE */ }
bool WriteHDF5file(char* fname, dtype* outArray, int* dims, float* pixel, float* expo, float* waveLen, float* dist, float* beamxy, float* flux, bool compress, unsigned short* badmask) { #ifdef _HDF5_H hid_t out_type_id = H5T_NATIVE_FLOAT; hid_t file_id = H5Fcreate(fname, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); hid_t gid = H5Gcreate1(file_id,"LCLS",0); hid_t dataspace_id, dataset_id; int ndims = 1; hsize_t dimsH[2]; dimsH[0] = 1; dimsH[1] = 0; dataspace_id = H5Screate_simple(1, dimsH, NULL); dataset_id = H5Dcreate1(gid, "detectorPosition", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id, out_type_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, dist)< 0) printf("Error writing 1D data to file\n"); H5Dclose(dataset_id); dataset_id = H5Dcreate1(gid, "photon_wavelength_A", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id, out_type_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, waveLen)< 0) printf("Error writing 1D data to file\n"); H5Dclose(dataset_id); dataset_id = H5Dcreate1(gid, "exposure_s", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id, out_type_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, expo)< 0) printf("Error writing 1D data to file\n"); H5Dclose(dataset_id); dataset_id = H5Dcreate1(gid, "pixelX_m", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id, out_type_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, &pixel[0])< 0) printf("Error writing 1D data to file\n"); H5Dclose(dataset_id); dataset_id = H5Dcreate1(gid, "pixelY_m", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id, out_type_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, &pixel[1])< 0) printf("Error writing 1D data to file\n"); H5Dclose(dataset_id); dataset_id = H5Dcreate1(gid, "flux_ph_s", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id, out_type_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, flux)< 0) printf("Error writing 1D data to file\n"); H5Dclose(dataset_id); dataset_id = H5Dcreate1(gid, "beamX_px", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id, out_type_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, &beamxy[0])< 0) printf("Error writing 1D data to file\n"); H5Dclose(dataset_id); dataset_id = H5Dcreate1(gid, "beamY_px", out_type_id, dataspace_id, H5P_DEFAULT); if(H5Dwrite(dataset_id, out_type_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, &beamxy[1])< 0) printf("Error writing 1D data to file\n"); H5Dclose(dataset_id); H5Sclose(dataspace_id); ndims = 2; dimsH[0] = dims[1]; dimsH[1] = dims[0]; hid_t gid2 = H5Gcreate1(file_id,"data",0); dataspace_id = H5Screate_simple(ndims, dimsH, NULL); //COMPRESSION hid_t dcpl = H5Pcreate (H5P_DATASET_CREATE); // hsize_t chunk[2] = {dimsH[0], dimsH[1]}; //#ifdef ZLIB_H //CASS hsize_t chunk[2] = {40,2}; hsize_t chunk[2] = {64, 64}; if (compress) { H5Pset_deflate (dcpl, 9); H5Pset_chunk (dcpl, 2, chunk); } //#endif // H5Pset_szip (dcpl, H5_SZIP_NN_OPTION_MASK, 8); // H5Pset_chunk (dcpl, 2, chunk); dataset_id = H5Dcreate(file_id, "/data/data", out_type_id, dataspace_id, H5P_DEFAULT, dcpl, H5P_DEFAULT); // 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, outArray)< 0) printf("Error writing 2D data to file\n"); H5Dclose(dataset_id); H5Sclose(dataspace_id); H5Gclose(gid2); //Here badmask H5Gclose(gid); H5Fclose(file_id); return true; #endif return false; }
/*------------------------------------------------------------------------- * Function: create_attrs_3 * * Purpose: Attempts to create some attributes for each dataset in a * loop. * * Return: Success: 0 * * Failure: -1 * * Programmer: Raymond Lu * Friday, Oct 3, 2003 * * Modifications: * *------------------------------------------------------------------------- */ static herr_t create_attrs_3(void) { hid_t file, dataset, attr; char filename[128]; char dset_name[64]; char attr_name[128]; int loop_num; int i, j, k; p_time attr_t = {0, 0, 0, 1000000, 0, ""}; p_time open_t = {0, 0, 0, 1000000, 0, "H5Dopen2"}; p_time close_t = {0, 0, 0, 1000000, 0, ""}; #ifdef H5_HAVE_PARALLEL /* need the rank for printing data */ int mpi_rank; if(facc_type == FACC_MPIO) MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank); #endif /*H5_HAVE_PARALLEL*/ h5_fixname(FILENAME[2], fapl, filename, sizeof filename); if ((file=H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0) goto error; if(create_dsets(file) < 0) goto error; /* * Create some(user specifies the number) attributes for each dataset * in a loop */ loop_num = NUM_ATTRS/BATCH_ATTRS; for(i = 0; i < loop_num; i++) { for(j = 0; j < NUM_DSETS; j++) { sprintf(dset_name, "dataset %d", j); open_t.start = retrieve_time(); if((dataset = H5Dopen2(file, dset_name, H5P_DEFAULT)) < 0) goto error; perf(&open_t, open_t.start, retrieve_time()); for(k = 0; k < BATCH_ATTRS; k++) { sprintf(attr_name, "some attrs for each dset %d %d", i, k); attr_t.start = retrieve_time(); if((attr = H5Acreate2(dataset, attr_name, H5T_NATIVE_DOUBLE, small_space, H5P_DEFAULT, H5P_DEFAULT)) < 0) goto error; if(H5Aclose(attr) < 0) goto error; perf(&attr_t, attr_t.start, retrieve_time()); if(flush_attr && H5Fflush(file, H5F_SCOPE_LOCAL) < 0) goto error; } /* end for */ close_t.start = retrieve_time(); if(H5Dclose(dataset) < 0) goto error; perf(&close_t, close_t.start, retrieve_time()); if(flush_dset && H5Fflush(file, H5F_SCOPE_LOCAL) < 0) goto error; } /* end for */ } /* end for */ #ifdef H5_HAVE_PARALLEL if(facc_type == FACC_MPIO) MPI_Barrier(MPI_COMM_WORLD); #endif /*H5_HAVE_PARALLEL*/ #ifdef H5_HAVE_PARALLEL /* only process 0 reports if parallel */ if (facc_type == FACC_DEFAULT || (facc_type != FACC_DEFAULT && MAINPROCESS)) #endif /*H5_HAVE_PARALLEL*/ { /* Calculate the average time */ open_t.avg = open_t.total / (loop_num*NUM_DSETS); close_t.avg = close_t.total / (loop_num*NUM_DSETS); attr_t.avg = attr_t.total / (NUM_ATTRS*NUM_DSETS); /* Print out the performance result */ fprintf(stderr, "3. Create %d attributes for each of %d existing datasets for %d times\n", BATCH_ATTRS, NUM_DSETS, loop_num); print_perf(open_t, close_t, attr_t); } if (H5Fclose(file) < 0) goto error; return 0; 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); }
/* * 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); }
/*------------------------------------------------------------------------- * Function: test_multi_compat * * Purpose: Tests the backward compatibility for MULTI driver. * See if we can open files created with v1.6 library. * The source file was created by the test/file_handle.c * of the v1.6 library. This test verifies the fix for * Issue 2598. In v1.6 library, there was EOA for the whole * MULTI file saved in the super block. We took it out in * v1.8 library because it's meaningless for the MULTI file. * v1.8 library saves the EOA for the metadata file, instead. * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * 21 June 2011 * *------------------------------------------------------------------------- */ static herr_t test_multi_compat(void) { hid_t file=(-1), fapl, dset=(-1), space=(-1); char newname[1024]; char filename_s[1024], newname_s[1024]; char filename_r[1024], newname_r[1024]; 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}; int i, j; int buf[MULTI_SIZE][MULTI_SIZE]; TESTING("MULTI file driver backward compatibility"); /* 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_map[mt] = H5FD_MEM_SUPER; memb_map[H5FD_MEM_DRAW] = H5FD_MEM_DRAW; memb_fapl[H5FD_MEM_SUPER] = H5P_DEFAULT; sprintf(sv[H5FD_MEM_SUPER], "%%s-%c.h5", 's'); memb_name[H5FD_MEM_SUPER] = sv[H5FD_MEM_SUPER]; memb_addr[H5FD_MEM_SUPER] = 0; memb_fapl[H5FD_MEM_DRAW] = H5P_DEFAULT; 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; if(H5Pset_fapl_multi(fapl, memb_map, memb_fapl, memb_name, memb_addr, TRUE)<0) TEST_ERROR; h5_fixname(FILENAME[9], fapl, newname, sizeof newname); /* Make copy for the data file in the build directory, to protect the * original file in the source directory */ sprintf(filename_s, "%s-%c.h5", MULTI_COMPAT_BASENAME, 's'); sprintf(newname_s, "%s-%c.h5", FILENAME[9], 's'); h5_make_local_copy(filename_s, newname_s); sprintf(filename_r, "%s-%c.h5", MULTI_COMPAT_BASENAME, 'r'); sprintf(newname_r, "%s-%c.h5", FILENAME[9], 'r'); h5_make_local_copy(filename_r, newname_r); /* Reopen the file for read only. Verify 1.8 library can open file * created with 1.6 library. */ if((file=H5Fopen(newname, H5F_ACC_RDONLY, fapl)) < 0) TEST_ERROR; if((dset = H5Dopen2(file, DSET1_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Make sure we can reopen the file for read and write */ if((file=H5Fopen(newname, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; if((dset = H5Dopen2(file, DSET1_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Reopen the file for adding another dataset. The new EOA for metadata file * should be written to the file */ if((file=H5Fopen(newname, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; /* Create and write data set */ if((space=H5Screate_simple(2, dims, NULL)) < 0) TEST_ERROR; if((dset=H5Dcreate2(file, DSET3_NAME, 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(H5Dclose(dset) < 0) TEST_ERROR; if(H5Sclose(space) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Reopen the file for read only again. Verify the library can handle * the EOA correctly */ if((file=H5Fopen(newname, H5F_ACC_RDONLY, fapl)) < 0) TEST_ERROR; if((dset = H5Dopen2(file, DSET1_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if((dset = H5Dopen2(file, DSET3_NAME, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Dclose(dset) < 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); H5Fclose(file); } H5E_END_TRY; return -1; }
/*------------------------------------------------------------------------- * 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*/ }
/*! This function reads a snapshot file and distributes the data it contains * to tasks 'readTask' to 'lastTask'. */ void read_file(char *fname, int readTask, int lastTask) { int blockmaxlen; int i, n_in_file, n_for_this_task, ntask, pc, offset = 0, task; int blksize1, blksize2; MPI_Status status; FILE *fd = 0; int nall; int type; char label[4]; int nstart, bytes_per_blockelement, npart, nextblock, typelist[6]; enum iofields blocknr; #ifdef HAVE_HDF5 char buf[500]; int rank, pcsum; hid_t hdf5_file, hdf5_grp[6], hdf5_dataspace_in_file; hid_t hdf5_datatype, hdf5_dataspace_in_memory, hdf5_dataset; hsize_t dims[2], count[2], start[2]; #endif #define SKIP {my_fread(&blksize1,sizeof(int),1,fd);} #define SKIP2 {my_fread(&blksize2,sizeof(int),1,fd);} if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) { if(!(fd = fopen(fname, "r"))) { printf("can't open file `%s' for reading initial conditions.\n", fname); endrun(123); } if(All.ICFormat == 2) { SKIP; my_fread(&label, sizeof(char), 4, fd); my_fread(&nextblock, sizeof(int), 1, fd); printf("Reading header => '%c%c%c%c' (%d byte)\n", label[0], label[1], label[2], label[3], nextblock); SKIP2; } SKIP; my_fread(&header, sizeof(header), 1, fd); SKIP2; if(blksize1 != 256 || blksize2 != 256) { printf("incorrect header format\n"); fflush(stdout); endrun(890); } } #ifdef HAVE_HDF5 if(All.ICFormat == 3) { read_header_attributes_in_hdf5(fname); hdf5_file = H5Fopen(fname, H5F_ACC_RDONLY, H5P_DEFAULT); for(type = 0; type < 6; type++) { if(header.npart[type] > 0) { sprintf(buf, "/PartType%d", type); hdf5_grp[type] = H5Gopen(hdf5_file, buf); } } } #endif for(task = readTask + 1; task <= lastTask; task++) MPI_Ssend(&header, sizeof(header), MPI_BYTE, task, TAG_HEADER, MPI_COMM_WORLD); } else MPI_Recv(&header, sizeof(header), MPI_BYTE, readTask, TAG_HEADER, MPI_COMM_WORLD, &status); if(All.TotNumPart == 0) { if(header.num_files <= 1) for(i = 0; i < 6; i++) header.npartTotal[i] = header.npart[i]; All.TotN_gas = header.npartTotal[0] + (((long long) header.npartTotalHighWord[0]) << 32); for(i = 0, All.TotNumPart = 0; i < 6; i++) { All.TotNumPart += header.npartTotal[i]; All.TotNumPart += (((long long) header.npartTotalHighWord[i]) << 32); } for(i = 0; i < 6; i++) All.MassTable[i] = header.mass[i]; All.MaxPart = All.PartAllocFactor * (All.TotNumPart / NTask); /* sets the maximum number of particles that may */ All.MaxPartSph = All.PartAllocFactor * (All.TotN_gas / NTask); /* sets the maximum number of particles that may reside on a processor */ allocate_memory(); if(RestartFlag == 2) All.Time = All.TimeBegin = header.time; } if(ThisTask == readTask) { for(i = 0, n_in_file = 0; i < 6; i++) n_in_file += header.npart[i]; printf("\nreading file `%s' on task=%d (contains %d particles.)\n" "distributing this file to tasks %d-%d\n" "Type 0 (gas): %8d (tot=%6d%09d) masstab=%g\n" "Type 1 (halo): %8d (tot=%6d%09d) masstab=%g\n" "Type 2 (disk): %8d (tot=%6d%09d) masstab=%g\n" "Type 3 (bulge): %8d (tot=%6d%09d) masstab=%g\n" "Type 4 (stars): %8d (tot=%6d%09d) masstab=%g\n" "Type 5 (bndry): %8d (tot=%6d%09d) masstab=%g\n\n", fname, ThisTask, n_in_file, readTask, lastTask, header.npart[0], (int) (header.npartTotal[0] / 1000000000), (int) (header.npartTotal[0] % 1000000000), All.MassTable[0], header.npart[1], (int) (header.npartTotal[1] / 1000000000), (int) (header.npartTotal[1] % 1000000000), All.MassTable[1], header.npart[2], (int) (header.npartTotal[2] / 1000000000), (int) (header.npartTotal[2] % 1000000000), All.MassTable[2], header.npart[3], (int) (header.npartTotal[3] / 1000000000), (int) (header.npartTotal[3] % 1000000000), All.MassTable[3], header.npart[4], (int) (header.npartTotal[4] / 1000000000), (int) (header.npartTotal[4] % 1000000000), All.MassTable[4], header.npart[5], (int) (header.npartTotal[5] / 1000000000), (int) (header.npartTotal[5] % 1000000000), All.MassTable[5]); fflush(stdout); } ntask = lastTask - readTask + 1; /* to collect the gas particles all at the beginning (in case several snapshot files are read on the current CPU) we move the collisionless particles such that a gap of the right size is created */ for(type = 0, nall = 0; type < 6; type++) { n_in_file = header.npart[type]; n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; nall += n_for_this_task; } memmove(&P[N_gas + nall], &P[N_gas], (NumPart - N_gas) * sizeof(struct particle_data)); nstart = N_gas; for(blocknr = 0; blocknr < IO_NBLOCKS; blocknr++) { if(blockpresent(blocknr)) { if(RestartFlag == 0 && blocknr > IO_U) continue; /* ignore all other blocks in initial conditions */ bytes_per_blockelement = get_bytes_per_blockelement(blocknr); blockmaxlen = ((int) (All.BufferSize * 1024 * 1024)) / bytes_per_blockelement; npart = get_particles_in_block(blocknr, &typelist[0]); if(npart > 0) { if(ThisTask == readTask) { if(All.ICFormat == 2) { SKIP; my_fread(&label, sizeof(char), 4, fd); my_fread(&nextblock, sizeof(int), 1, fd); printf("Reading header => '%c%c%c%c' (%d byte)\n", label[0], label[1], label[2], label[3], nextblock); SKIP2; if(strncmp(label, Tab_IO_Labels[blocknr], 4) != 0) { printf("incorrect block-structure!\n"); printf("expected '%c%c%c%c' but found '%c%c%c%c'\n", label[0], label[1], label[2], label[3], Tab_IO_Labels[blocknr][0], Tab_IO_Labels[blocknr][1], Tab_IO_Labels[blocknr][2], Tab_IO_Labels[blocknr][3]); fflush(stdout); endrun(1890); } } if(All.ICFormat == 1 || All.ICFormat == 2) SKIP; } for(type = 0, offset = 0; type < 6; type++) { n_in_file = header.npart[type]; #ifdef HAVE_HDF5 pcsum = 0; #endif if(typelist[type] == 0) { n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; offset += n_for_this_task; } else { for(task = readTask; task <= lastTask; task++) { n_for_this_task = n_in_file / ntask; if((task - readTask) < (n_in_file % ntask)) n_for_this_task++; if(task == ThisTask) if(NumPart + n_for_this_task > All.MaxPart) { printf("too many particles\n"); endrun(1313); } do { pc = n_for_this_task; if(pc > blockmaxlen) pc = blockmaxlen; if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) my_fread(CommBuffer, bytes_per_blockelement, pc, fd); #ifdef HAVE_HDF5 if(All.ICFormat == 3) { get_dataset_name(blocknr, buf); hdf5_dataset = H5Dopen(hdf5_grp[type], buf); dims[0] = header.npart[type]; dims[1] = get_values_per_blockelement(blocknr); if(dims[1] == 1) rank = 1; else rank = 2; hdf5_dataspace_in_file = H5Screate_simple(rank, dims, NULL); dims[0] = pc; hdf5_dataspace_in_memory = H5Screate_simple(rank, dims, NULL); start[0] = pcsum; start[1] = 0; count[0] = pc; count[1] = get_values_per_blockelement(blocknr); pcsum += pc; H5Sselect_hyperslab(hdf5_dataspace_in_file, H5S_SELECT_SET, start, NULL, count, NULL); switch (get_datatype_in_block(blocknr)) { case 0: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT); break; case 1: hdf5_datatype = H5Tcopy(H5T_NATIVE_FLOAT); break; case 2: hdf5_datatype = H5Tcopy(H5T_NATIVE_UINT64); break; } H5Dread(hdf5_dataset, hdf5_datatype, hdf5_dataspace_in_memory, hdf5_dataspace_in_file, H5P_DEFAULT, CommBuffer); H5Tclose(hdf5_datatype); H5Sclose(hdf5_dataspace_in_memory); H5Sclose(hdf5_dataspace_in_file); H5Dclose(hdf5_dataset); } #endif } if(ThisTask == readTask && task != readTask) MPI_Ssend(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, task, TAG_PDATA, MPI_COMM_WORLD); if(ThisTask != readTask && task == ThisTask) MPI_Recv(CommBuffer, bytes_per_blockelement * pc, MPI_BYTE, readTask, TAG_PDATA, MPI_COMM_WORLD, &status); if(ThisTask == task) { empty_read_buffer(blocknr, nstart + offset, pc, type); offset += pc; } n_for_this_task -= pc; } while(n_for_this_task > 0); } } } if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) { SKIP2; if(blksize1 != blksize2) { printf("incorrect block-sizes detected!\n"); printf("Task=%d blocknr=%d blksize1=%d blksize2=%d\n", ThisTask, blocknr, blksize1, blksize2); fflush(stdout); endrun(1889); } } } } } } for(type = 0; type < 6; type++) { n_in_file = header.npart[type]; n_for_this_task = n_in_file / ntask; if((ThisTask - readTask) < (n_in_file % ntask)) n_for_this_task++; NumPart += n_for_this_task; if(type == 0) N_gas += n_for_this_task; } if(ThisTask == readTask) { if(All.ICFormat == 1 || All.ICFormat == 2) fclose(fd); #ifdef HAVE_HDF5 if(All.ICFormat == 3) { for(type = 5; type >= 0; type--) if(header.npart[type] > 0) H5Gclose(hdf5_grp[type]); H5Fclose(hdf5_file); } #endif } }
herr_t arma_H5Dclose(hid_t dataset_id) { return H5Dclose(dataset_id); }
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; }
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 ImageBase::readHDF5(size_t select_img) { bool isStack = false; H5infoProvider provider = getProvider(fhdf5); // Provider name int errCode = 0; hid_t dataset; /* Dataset and datatype identifiers */ hid_t filespace; hsize_t dims[4]; // We are not going to support more than 4 dimensions, at this moment. hsize_t nobjEman; hid_t cparms; int rank; String dsname = filename.getBlockName(); // Setting default dataset name if (dsname.empty()) { dsname = provider.second; switch (provider.first) { case EMAN: // Images in stack are stored in separated groups hid_t grpid; grpid = H5Gopen(fhdf5,"/MDF/images/", H5P_DEFAULT); /*herr_t err = */ H5Gget_num_objs(grpid, &nobjEman); dsname = formatString(dsname.c_str(), IMG_INDEX(select_img)); H5Gclose(grpid); break; default: break; } } else { switch (provider.first) { case EMAN: // Images in stack are stored in separated groups nobjEman=1; break; default: break; } } dataset = H5Dopen2(fhdf5, dsname.c_str(), H5P_DEFAULT); if( dataset < 0) REPORT_ERROR(ERR_IO_NOTEXIST, formatString("readHDF5: Dataset '%s' not found",dsname.c_str())); cparms = H5Dget_create_plist(dataset); /* Get properties handle first. */ // Get dataset rank and dimension. filespace = H5Dget_space(dataset); /* Get filespace handle first. */ // rank = H5Sget_simple_extent_ndims(filespace); rank = H5Sget_simple_extent_dims(filespace, dims, NULL); // Offset only set when it is possible to access to data directly offset = (H5D_CONTIGUOUS == H5Pget_layout(cparms))? H5Dget_offset(dataset) : 0; // status = H5Dread(dataset, tid, H5S_ALL, H5S_ALL, H5P_DEFAULT, bm_out); hid_t h5datatype = H5Dget_type(dataset); // Reading byte order switch(H5Tget_order(h5datatype)) { case H5T_ORDER_ERROR: REPORT_ERROR(ERR_IO, "readHDF5: error reading endianness."); break; case H5T_ORDER_LE: swap = IsBigEndian(); break; case H5T_ORDER_BE: swap = IsLittleEndian(); break; default: REPORT_ERROR(ERR_IO, "readHDF5: unknown endianness type, maybe mixed types."); break; } DataType datatype = datatypeH5(h5datatype); MDMainHeader.setValue(MDL_DATATYPE,(int) datatype); // Setting isStack depending on provider switch (provider.first) { case MISTRAL: // rank 3 arrays are stacks isStack = true; break; // case EMAN: // Images in stack are stored in separated groups default: break; } ArrayDim aDim; size_t nDimFile; aDim.xdim = dims[rank-1]; aDim.ydim = (rank>1)?dims[rank-2]:1; aDim.zdim = (rank>3 || (rank==3 && !isStack))?dims[rank-3]:1; if ( provider.first == EMAN ) nDimFile = nobjEman; else nDimFile = ( rank<3 || !isStack )?1:dims[0] ; if (select_img > nDimFile) REPORT_ERROR(ERR_INDEX_OUTOFBOUNDS, formatString("readHDF5 (%s): Image number %lu exceeds stack size %lu", filename.c_str(), select_img, nDimFile)); aDim.ndim = replaceNsize = (select_img == ALL_IMAGES)? nDimFile :1 ; setDimensions(aDim); //Read header only if(dataMode == HEADER || (dataMode == _HEADER_ALL && aDim.ndim > 1)) return errCode; // EMAN stores each image in a separate dataset if ( provider.first == EMAN ) select_img = 1; size_t imgStart = IMG_INDEX(select_img); size_t imgEnd = (select_img != ALL_IMAGES) ? imgStart + 1 : aDim.ndim; MD.clear(); MD.resize(imgEnd - imgStart,MDL::emptyHeader); if (dataMode < DATA) // Don't read data if not necessary but read the header return errCode; if ( H5Pget_layout(cparms) == H5D_CONTIGUOUS ) //We can read it directly readData(fimg, select_img, datatype, 0); else // We read it by hyperslabs { // Allocate memory for image data (Assume xdim, ydim, zdim and ndim are already set //if memory already allocated use it (no resize allowed) mdaBase->coreAllocateReuse(); hid_t memspace; hsize_t offset[4]; // Hyperslab offset in the file hsize_t count[4]; // Size of the hyperslab in the file // Define the offset and count of the hyperslab to be read. switch (rank) { case 4: count[0] = 1; case 3: // if (stack) count[rank-3] = aDim.zdim; offset[rank-2] = 0; case 2: count[rank-2] = aDim.ydim; offset[rank-2] = 0; break; } count[rank-1] = aDim.xdim; offset[rank-1] = 0; aDim.xdim = dims[rank-1]; aDim.ydim = (rank>1)?dims[rank-2]:1; aDim.zdim = (rank == 4)?dims[1]:1; // size_t nDimFile = (rank>2)?dims[0]:1 ; // Define the memory space to read a hyperslab. memspace = H5Screate_simple(rank,count,NULL); size_t data = (size_t) this->mdaBase->getArrayPointer(); size_t pad = aDim.zyxdim*gettypesize(myT()); for (size_t idx = imgStart, imN = 0; idx < imgEnd; ++idx, ++imN) { // Set the offset of the hyperslab to be read offset[0] = idx; if ( H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL) < 0 ) REPORT_ERROR(ERR_IO_NOREAD, formatString("readHDF5: Error selecting hyperslab %d from filename %s", imgStart, filename.c_str())); // movePointerTo(ALL_SLICES,imN); // Read if ( H5Dread(dataset, H5Datatype(myT()), memspace, filespace, H5P_DEFAULT, (void*)(data + pad*imN)) < 0 ) REPORT_ERROR(ERR_IO_NOREAD,formatString("readHDF5: Error reading hyperslab %d from filename %s", imgStart, filename.c_str())); } H5Sclose(memspace); } H5Pclose(cparms); H5Sclose(filespace); H5Dclose(dataset); return errCode; }
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: 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; }
/* Rename a dimension, for those who like to prevaricate. */ int NC4_rename_dim(int ncid, int dimid, const char *name) { NC *nc; NC_GRP_INFO_T *grp; NC_HDF5_FILE_INFO_T *h5; NC_DIM_INFO_T *dim, *tmp_dim; char norm_name[NC_MAX_NAME + 1]; int retval; if (!name) return NC_EINVAL; LOG((2, "%s: ncid 0x%x dimid %d name %s", __func__, ncid, dimid, name)); /* Find info for this file and group, and set pointer to each. */ if ((retval = nc4_find_nc_grp_h5(ncid, &nc, &grp, &h5))) return retval; assert(nc); assert(h5 && grp); /* Trying to write to a read-only file? No way, Jose! */ if (h5->no_write) return NC_EPERM; #if 0 /*def USE_PNETCDF*/ /* Take care of files created/opened with parallel-netcdf library. */ if (h5->pnetcdf_file) return ncmpi_rename_dim(nc->int_ncid, dimid, name); #endif /* USE_PNETCDF */ /* Make sure this is a valid netcdf name. */ if ((retval = nc4_check_name(name, norm_name))) return retval; /* Check if name is in use, and retain a pointer to the correct dim */ tmp_dim = NULL; for (dim = grp->dim; dim; dim = dim->l.next) { if (!strncmp(dim->name, norm_name, NC_MAX_NAME)) return NC_ENAMEINUSE; if (dim->dimid == dimid) tmp_dim = dim; } if (!tmp_dim) return NC_EBADDIM; dim = tmp_dim; /* Check for renaming dimension w/o variable */ if (dim->hdf_dimscaleid) { /* Sanity check */ assert(!dim->coord_var); /* Close the HDF5 dataset */ if (H5Dclose(dim->hdf_dimscaleid) < 0) return NC_EHDFERR; dim->hdf_dimscaleid = 0; /* Now delete the dataset (it will be recreated later, if necessary) */ if (H5Gunlink(grp->hdf_grpid, dim->name) < 0) return NC_EDIMMETA; } /* Give the dimension its new name in metadata. UTF8 normalization * has been done. */ if(dim->name) free(dim->name); if (!(dim->name = malloc((strlen(norm_name) + 1) * sizeof(char)))) return NC_ENOMEM; strcpy(dim->name, norm_name); /* Check if dimension was a coordinate variable, but names are different now */ if (dim->coord_var && strcmp(dim->name, dim->coord_var->name)) { /* Break up the coordinate variable */ if ((retval = nc4_break_coord_var(grp, dim->coord_var, dim))) return retval; } /* Check if dimension should become a coordinate variable */ if (!dim->coord_var) { NC_VAR_INFO_T *var; /* Attempt to find a variable with the same name as the dimension in * the current group. */ if ((retval = nc4_find_var(grp, dim->name, &var))) return retval; /* Check if we found a variable and the variable has the dimension in * index 0. */ if (var && var->dim[0] == dim) { /* Sanity check */ assert(var->dimids[0] == dim->dimid); /* Reform the coordinate variable */ if ((retval = nc4_reform_coord_var(grp, var, dim))) return retval; } } return NC_NOERR; }
/*------------------------------------------------------------------------- * 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; }
int RD_verify_dataset(hid_t file_id, const char *dset_name, int ndims, int *dims, hid_t dtype, int dims_known) { hid_t dset_id, dspc_id, dtyp_id; herr_t err; /* data sizes */ hsize_t *this_dims; int i; /* get dataset */ dset_id = H5Dopen2(file_id, dset_name, H5P_DEFAULT); if (!RD_ID_ISVALID(dset_id)) return RD_ERROR_DSETNOTVALID; /* get dataspace */ dspc_id = H5Dget_space(dset_id); if (!RD_ID_ISVALID(dspc_id)) return RD_ERROR_DSPACENOTVALID; /* check number of dimensions */ if (H5Sget_simple_extent_ndims(dspc_id) != ndims) return RD_ERROR_DSETBADNDIMS; /* get dimensions */ this_dims = malloc(sizeof(*this_dims)*ndims); if (H5Sget_simple_extent_dims(dspc_id, this_dims, NULL) < 0) return RD_ERROR_DSETBADDIMS; /* copy into or check agains dims */ if (dims_known) { for (i=0; i<ndims; i++) { if (dims[i] != (int)(this_dims[i])) return RD_ERROR_DSETBADDIMS; } } else { for (i=0; i<ndims; i++) { dims[i] = (int)(this_dims[i]); } } /* clean up */ free(this_dims); /* close dataspace */ err = H5Sclose(dspc_id); if (err < 0) return RD_ERROR_DSPACENOTVALID; /* get datatype */ dtyp_id = H5Dget_type(dset_id); if (!RD_ID_ISVALID(dtyp_id)) return RD_ERROR_DTYPENOTVALID; if (H5Tequal(dtyp_id, dtype) <= 0) return RD_ERROR_DTYPENOTVALID; /* close dataset */ err = H5Tclose(dtyp_id); if (err < 0) return RD_ERROR_DTYPENOTVALID; /* close dataset */ err = H5Dclose(dset_id); if (err < 0) return RD_ERROR_DSETNOTVALID; return 0; }
/*------------------------------------------------------------------------- * Function: test_family_compat * * Purpose: Tests the backward compatibility for FAMILY driver. * See if we can open files created with v1.6 library. * The source file was created by the test/file_handle.c * of the v1.6 library. Then tools/misc/h5repart.c was * used to concantenated. The command was "h5repart -m 5k * family_file%05d.h5 family_v16_%05d.h5". * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * June 3, 2005 * *------------------------------------------------------------------------- */ static herr_t test_family_compat(void) { hid_t file = (-1), fapl; hid_t dset; char dname[]="dataset"; char filename[1024]; char pathname[1024], pathname_individual[1024]; char newname[1024], newname_individual[1024]; int counter = 0; TESTING("FAMILY file driver backward compatibility"); /* Set property list and file name for FAMILY driver */ fapl = h5_fileaccess(); if(H5Pset_fapl_family(fapl, (hsize_t)FAMILY_SIZE2, H5P_DEFAULT) < 0) TEST_ERROR; h5_fixname(COMPAT_BASENAME, fapl, filename, sizeof filename); h5_fixname(FILENAME[3], fapl, newname, sizeof newname); pathname[0] = '\0'; HDstrcat(pathname, filename); /* The following code makes the copies of the family files in the source directory. * Since we're going to open the files with write mode, this protects the original * files. */ HDsnprintf(newname_individual, sizeof(newname_individual), newname, counter); HDsnprintf(pathname_individual, sizeof(pathname_individual), pathname, counter); while(h5_make_local_copy(pathname_individual, newname_individual) >= 0) { counter++; HDsnprintf(newname_individual, sizeof(newname_individual), newname, counter); HDsnprintf(pathname_individual, sizeof(pathname_individual), pathname, counter); } /* Make sure we can open the file. Use the read and write mode to flush the * superblock. */ if((file = H5Fopen(newname, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; if((dset = H5Dopen2(file, dname, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; /* Open the file again to make sure it isn't corrupted. */ if((file = H5Fopen(newname, H5F_ACC_RDWR, fapl)) < 0) TEST_ERROR; if((dset = H5Dopen2(file, dname, H5P_DEFAULT)) < 0) TEST_ERROR; if(H5Dclose(dset) < 0) TEST_ERROR; if(H5Fclose(file) < 0) TEST_ERROR; h5_cleanup(FILENAME, fapl); PASSED(); return 0; error: H5E_BEGIN_TRY { H5Fclose(file); H5Pclose(fapl); } H5E_END_TRY; return -1; } /* end test_family_compat() */
void cData2d::readHDF5(char* filename, char* fieldname){ // Open the file hid_t file_id; file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); if(file_id < 0){ printf("ERROR: Could not open file %s\n",filename); return; } // Open the dataset hid_t dataset_id; hid_t dataspace_id; dataset_id = H5Dopen1(file_id, fieldname); dataspace_id = H5Dget_space(dataset_id); // Test if 2D data int ndims; ndims = H5Sget_simple_extent_ndims(dataspace_id); if(ndims != 2) { printf("2dData::readHDF5: Not 2D data set, ndims=%i\n",ndims); exit(0); } // Get dimensions of data set (nx, ny, nn) hsize_t dims[ndims]; H5Sget_simple_extent_dims(dataspace_id,dims,NULL); ny = dims[0]; nx = dims[1]; nn = 1; for(int i = 0;i<ndims;i++) nn *= dims[i]; // Create space for the new data free(data); data = NULL; data = (tData2d *) calloc(nn, sizeof(tData2d)); // Read in data after setting up a temporary buffer of the appropriate variable type // Somehow this works best when split out accordint to different data types // Fix into general form later hid_t datatype_id; H5T_class_t dataclass; size_t size; datatype_id = H5Dget_type(dataset_id); dataclass = H5Tget_class(datatype_id); size = H5Tget_size(datatype_id); if(dataclass == H5T_FLOAT){ if (size == sizeof(float)) { float* buffer = (float *) calloc(nn, sizeof(float)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(double)) { double* buffer = (double *) calloc(nn, sizeof(double)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown floating point type, size=%i\n",(int) size); return; } } else if(dataclass == H5T_INTEGER){ if (size == sizeof(char)) { char* buffer = (char*) calloc(nn, sizeof(char)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(short)) { short* buffer = (short*) calloc(nn, sizeof(short)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(int)) { int* buffer = (int *) calloc(nn, sizeof(int)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(long)) { long* buffer = (long *) calloc(nn, sizeof(long)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown integer type, size=%lu\n",size); exit(1); } } else { printf("2dData::readHDF5: unknown HDF5 data type\n"); return; } // Close and cleanup 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); }
/*------------------------------------------------------------------------- * 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; }
bool ReadHDF5file(char* filename, char* fieldname, dtype** outArray, int* dims) { #ifdef _HDF5_H // Open the file hid_t file_id; file_id = H5Fopen(filename,H5F_ACC_RDONLY,H5P_DEFAULT); //? file_id = H5Fopen(filename,H5F_ACC_RDONLY,faplist_id); if(file_id < 0){ printf("ERROR: Could not open file %s\n",filename); return false; } // Open the dataset hid_t dataset_id; hid_t dataspace_id; dataset_id = H5Dopen1(file_id, fieldname); if(dataset_id < 0){ printf("ERROR: Could not open the data field %s\n",fieldname); return false; } dataspace_id = H5Dget_space(dataset_id); // Test if 2D data int ndims; ndims = H5Sget_simple_extent_ndims(dataspace_id); // Get dimensions of data set (nx, ny, nn) hsize_t* dimsl = new hsize_t[ndims]; H5Sget_simple_extent_dims(dataspace_id,dimsl,NULL); for(int i = 0;(i<ndims&&i<3);i++) dims[i] = dimsl[ndims-1-i]; //!!!!!!!! NOT SURE size_t nn = 1; for(int i = 0;i<ndims;i++) nn *= dimsl[i]; // Create space for the new data dtype* data = *outArray; if (data!=NULL) delete data;//free(data); *outArray = new dtype[nn]; data = *outArray; hid_t datatype_id; H5T_class_t dataclass; size_t size; datatype_id = H5Dget_type(dataset_id); dataclass = H5Tget_class(datatype_id); size = H5Tget_size(datatype_id); int rrr = sizeof(int); if(dataclass == H5T_FLOAT){ if (size == sizeof(float)) { float* buffer = (float *) calloc(nn, sizeof(float)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(double)) { double* buffer = (double *) calloc(nn, sizeof(double)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown floating point type, size=%i\n",(int) size); return false; } } else if(dataclass == H5T_INTEGER){ if (size == sizeof(short)) { short* buffer = (short*) calloc(nn, sizeof(short)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(int)) { int* buffer = (int *) calloc(nn, sizeof(int)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else if (size == sizeof(long)) { long* buffer = (long *) calloc(nn, sizeof(long)); H5Dread(dataset_id, datatype_id, H5S_ALL,H5S_ALL, H5P_DEFAULT, buffer); for(long i=0; i<nn; i++) data[i] = buffer[i]; free(buffer); } else { printf("2dData::readHDF5: unknown integer type, size=%i\n",(int) size); return false; } } else { printf("2dData::readHDF5: unknown HDF5 data type\n"); return false; } // Close and cleanup 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); return true; #endif return false; }
void h5_write_bnd_box_max_sp_(hid_t* file_identifier, int* maximum_blocks, int* bnd_box, int* local_blocks, int* total_blocks, int* global_offset) { hid_t dataspace, dataset, memspace, dxfer_template; herr_t status; int rank; hsize_t dimens_2d[2], dimens_3d[3]; hsize_t start_2d[2], start_3d[3]; hsize_t stride_2d[2], stride_3d[3], count_2d[2], count_3d[3]; int ierr; /* set the dimensions of the dataset */ rank = 2; dimens_2d[0] = *total_blocks; dimens_2d[1] = NDIM; dataspace = H5Screate_simple(rank, dimens_2d, NULL); /* create the dataset */ dataset = H5Dcreate(*file_identifier, "bounding box maximum", H5T_NATIVE_FLOAT, dataspace, H5P_DEFAULT); /* create the hyperslab -- this will differ on the different processors */ start_2d[0] = (hsize_t) (*global_offset); start_2d[1] = 0; stride_2d[0] = 1; stride_2d[1] = 1; count_2d[0] = (hsize_t) (*local_blocks); count_2d[1] = NDIM; status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start_2d, stride_2d, count_2d, NULL); /* create the memory space */ rank = 3; dimens_3d[0] = *maximum_blocks; dimens_3d[1] = MDIM; dimens_3d[2] = 2; memspace = H5Screate_simple(rank, dimens_3d, NULL); start_3d[0] = 0; start_3d[1] = 0; start_3d[2] = 1; /* we want the maximum */ stride_3d[0] = 1; stride_3d[1] = 1; stride_3d[2] = 1; count_3d[0] = *local_blocks; count_3d[1] = NDIM; count_3d[2] = 1; ierr = H5Sselect_hyperslab(memspace, H5S_SELECT_SET, start_3d, stride_3d, count_3d, NULL); flash_tune_plist(&dxfer_template ); /* write the data */ status = H5Dwrite(dataset, H5T_NATIVE_FLOAT, memspace, dataspace, dxfer_template, bnd_box); H5Pclose(dxfer_template); H5Sclose(memspace); H5Sclose(dataspace); H5Dclose(dataset); }
int main(int argc,char *argv[]) { struct options opts = { 0, 1, 0, 0, -1, 1, 0, -1, 1, NULL, }; getopts(argc,argv,&opts); H5Eset_auto2(H5E_DEFAULT,NULL,NULL); hid_t fid = H5Fopen(opts.filename,H5F_ACC_RDONLY,H5P_DEFAULT); if(fid < 0) { fprintf(stderr,"Failed to open %s.\n",opts.filename); return fid; } if(opts.info) { // count the number of trials in the file hid_t trial; int num_trials = 0; std::stringstream trial_name; for(;;) { trial_name.str(""); trial_name << "/Trial" << num_trials+1; if((trial = H5Gopen(fid,trial_name.str().c_str(),H5P_DEFAULT)) < 0) break; else { print_trial_info(trial,++num_trials); H5Gclose(trial); } } H5Fclose(fid); return 0; } std::stringstream data_name; data_name << "/Trial" << opts.trial << "/Synchronous Data/Channel Data"; hid_t table = H5Dopen(fid,data_name.str().c_str(),H5P_DEFAULT); if(table < 0) { fprintf(stderr,"Requested trial #%d does not exist.\n",opts.trial); return table; } hsize_t ncols = H5Tget_size(H5Dget_type(table))/sizeof(double); H5Dclose(table); table = H5PTopen(fid,data_name.str().c_str()); hsize_t nrows; H5PTget_num_packets(table,&nrows); // validate column and row ranges if(opts.cols_end == -1 || opts.cols_end >= ncols) opts.cols_end = ncols-1; if(opts.rows_end == -1 || opts.rows_end >= nrows) opts.rows_end = nrows-1; opts.cols_end -= (opts.cols_end-opts.cols_start)%opts.cols_step; opts.rows_end -= (opts.rows_end-opts.rows_start)%opts.rows_step; if((opts.cols_start-opts.cols_end)*opts.cols_step > 0) opts.cols_step *= -1; if((opts.rows_start-opts.rows_end)*opts.rows_step > 0) opts.rows_step *= -1; int row_idx = opts.rows_start; do { H5PTset_index(table,row_idx); double data[ncols]; H5PTget_next(table,1,data); int col_idx = opts.cols_start; do { if(opts.binary) { write(1,data+col_idx,sizeof(double)); } else { printf("%e ",data[col_idx]); } if(col_idx == opts.cols_end) break; col_idx += opts.cols_step; } while(1); if(!opts.binary) printf("\n"); if(row_idx == opts.rows_end) break; row_idx += opts.rows_step; } while(1); H5PTclose(table); H5Fclose(fid); return 0; }
void h5_write_unknowns_sp_(hid_t* file_identifier, int* index, /* index of var to write */ int* nvar, /* total number of variables */ int* nxb, /* # of zones to store in x */ int* nyb, /* # of zones to store in y */ int* nzb, /* # of zones to store in z */ int* nguard, /* # of guardcells in pass */ int* maximum_blocks, /* maximum num of blocks */ float* unknowns, /* [mblk][NZB][NYB][NXB][nvar] */ char record_label[5],/* add char-null termination */ int* local_blocks, int* total_blocks, int* global_offset) { hid_t dataspace, dataset, memspace, dxfer_template, dataset_plist; herr_t status; int rank; hsize_t dimens_4d[4], dimens_5d[5]; hsize_t start_4d[4]; hsize_t stride_4d[4], count_4d[4]; #ifdef CHUNK hsize_t dimens_chunk[4]; #endif char record_label_new[5]; int ierr; /* the variable names are 4 characters long -- copy this into record_label_new, the 5th character is for the \0 termination */ strncpy(record_label_new, record_label,4); *(record_label_new + 4) = '\0'; /* set the dimensions of the dataset */ rank = 4; dimens_4d[0] = *total_blocks; dimens_4d[1] = *nzb; dimens_4d[2] = *nyb; dimens_4d[3] = *nxb; dataspace = H5Screate_simple(rank, dimens_4d, NULL); #ifdef DEBUG_IO printf("UNKNOWNS: dataspace = %d\n", (int) dataspace); #endif dataset_plist = H5Pcreate(H5P_DATASET_CREATE); #ifdef CHUNK /* set the layout to chunked */ ierr = H5Pset_layout(dataset_plist, H5D_CHUNKED); /* create a chunk containing 10 blocks worth of data */ dimens_chunk[0] = 10; dimens_chunk[1] = *nzb; dimens_chunk[2] = *nyb; dimens_chunk[3] = *nxb; ierr = H5Pset_chunk(dataset_plist, 4, dimens_chunk); #endif dataset = H5Dcreate(*file_identifier, record_label_new, H5T_NATIVE_FLOAT, dataspace, dataset_plist); #ifdef DEBUG_IO printf("UNKNOWNS: dataset = %d\n", (int) dataset); #endif /* create the hyperslab -- this will differ on the different processors */ start_4d[0] = (hsize_t) (*global_offset); start_4d[1] = 0; start_4d[2] = 0; start_4d[3] = 0; stride_4d[0] = 1; stride_4d[1] = 1; stride_4d[2] = 1; stride_4d[3] = 1; count_4d[0] = (hsize_t) (*local_blocks); count_4d[1] = *nzb; count_4d[2] = *nyb; count_4d[3] = *nxb; status = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start_4d, stride_4d, count_4d, NULL); #ifdef DEBUG_IO printf("UNKNOWNS: hyperslab selection = %d\n", (int) status); #endif /* create the memory space -- we can get away with a simple memory space for the unknowns, since we are passing a contiguous block of memory now, and the block count is the last index in FORTRAN */ rank = 5; dimens_5d[0] = *local_blocks; dimens_5d[1] = *nzb+(*nguard)*2*k3d; dimens_5d[2] = *nyb+(*nguard)*2*k2d; dimens_5d[3] = *nxb+(*nguard)*2; dimens_5d[4] = *nvar; memspace = H5Screate_simple(rank, dimens_5d, NULL); #ifdef DEBUG_IO printf("UNKNOWNS: memspace = %d\n", (int) memspace); #endif /* setting the transfer template */ flash_tune_plist(&dxfer_template); #ifdef DEBUG_IO printf("UNKNOWNS: dxfer_template = %d\n", (int) dxfer_template); #endif /* write the data */ status = H5Dwrite(dataset, H5T_NATIVE_FLOAT, memspace, dataspace, dxfer_template, unknowns); #ifdef DEBUG_IO printf("UNKNOWNS: wrote unknowns, status = %d\n", (int) status); #endif H5Pclose(dxfer_template); H5Sclose(memspace); H5Sclose(dataspace); H5Dclose(dataset); }
/*------------------------------------------------------------------------- * Function: create_scale_offset_dset_long_long * * Purpose: Create a dataset of LONG LONG datatype with scale-offset * filter * * Return: Success: 0 * Failure: -1 * * Programmer: Neil Fortner * 27 January 2011 * * Modifications: * *------------------------------------------------------------------------- */ int create_scale_offset_dsets_long_long(hid_t fid, hid_t fsid, hid_t msid) { hid_t dataset; /* dataset handles */ hid_t dcpl; long long data[NX][NY]; /* data to write */ long long fillvalue = -2; hsize_t chunk[RANK] = {CHUNK0, CHUNK1}; int i, j; /* * Data and output buffer initialization. */ for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) data[j][i] = i + j; } /* * 0 1 2 3 4 5 * 1 2 3 4 5 6 * 2 3 4 5 6 7 * 3 4 5 6 7 8 * 4 5 6 7 8 9 * 5 6 7 8 9 10 */ /* * Create the dataset creation property list, add the Scale-Offset * filter, set the chunk size, and set the fill value. */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR if(H5Pset_scaleoffset(dcpl, H5Z_SO_INT, H5Z_SO_INT_MINBITS_DEFAULT) < 0) TEST_ERROR if(H5Pset_chunk(dcpl, RANK, chunk) < 0) TEST_ERROR if(H5Pset_fill_value(dcpl, H5T_NATIVE_LLONG, &fillvalue) < 0) TEST_ERROR /* * Create a new dataset within the file using defined dataspace, little * endian datatype and default dataset creation properties. */ if((dataset = H5Dcreate2(fid, DATASETNAME12, H5T_STD_I64LE, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR /* * Write the data to the dataset using default transfer properties. */ if(H5Dwrite(dataset, H5T_NATIVE_LLONG, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR /* Close dataset */ if(H5Dclose(dataset) < 0) TEST_ERROR /* Now create a dataset with a big-endian type */ if((dataset = H5Dcreate2(fid, DATASETNAME13, H5T_STD_I64BE, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR if(H5Dwrite(dataset, H5T_NATIVE_LLONG, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR if(H5Dclose(dataset) < 0) TEST_ERROR /* * Close/release resources. */ if(H5Pclose(dcpl) < 0) TEST_ERROR return 0; error: H5E_BEGIN_TRY { H5Pclose(dcpl); H5Dclose(dataset); } H5E_END_TRY; return -1; }
void h5_write_header_info_sp_(int* MyPE, int* nvar_out, /* num vars to store */ hid_t* file_identifier, /* file handle */ char file_creation_time[],/* time / date stamp */ char flash_version[], /* FLASH version num */ char run_comment[], /* runtime comment */ int* total_blocks, /* total # of blocks */ float* time, /* simulation time */ float* timestep, /* current timestep */ int* nsteps, /* # of timestep */ int nzones_block[3], /* nxb, nyb, nzb */ char unk_labels[][5]) /* unknown labels */ { hid_t dataspace, dataset; herr_t status; int rank; hsize_t dimens_1d, dimens_2d[2]; int string_size; hid_t string_type, sp_type; typedef struct sim_params_t { int total_blocks; int nsteps; int nxb; int nyb; int nzb; float time; float timestep; } sim_params_t; sim_params_t sim_params; /* file creation time */ rank = 1; dimens_1d = 1; /* manually set the string size, and make it null terminated */ string_size = 40; /* setup the datatype for this string length */ string_type = H5Tcopy(H5T_C_S1); H5Tset_size(string_type, string_size); dataspace = H5Screate_simple(rank, &dimens_1d, NULL); dataset = H5Dcreate(*file_identifier, "file creation time", string_type, dataspace, H5P_DEFAULT); if (*MyPE == 0) { status = H5Dwrite(dataset, string_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, file_creation_time); #ifdef DEBUG_IO printf("MyPE = %d, wrote file creation time, status = %d\n", *MyPE, (int) status); #endif } H5Tclose(string_type); H5Sclose(dataspace); H5Dclose(dataset); /* FLASH version */ /* manually set the string size, and make it null terminated */ string_size = 20; /* setup the datatype for this string length */ string_type = H5Tcopy(H5T_C_S1); H5Tset_size(string_type, string_size); dataspace = H5Screate_simple(rank, &dimens_1d, NULL); dataset = H5Dcreate(*file_identifier, "FLASH version", string_type, dataspace, H5P_DEFAULT); if (*MyPE == 0) { status = H5Dwrite(dataset, string_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, flash_version); #ifdef DEBUG_IO printf("wrote flash version, status = %d\n", (int) status); #endif } H5Tclose(string_type); H5Sclose(dataspace); H5Dclose(dataset); /*----------------------------------------------------------------------- create a compound datatype to hold the simulation parameters -- this will cut down on the number of writes issued, and should improve performance ------------------------------------------------------------------------- */ sim_params.total_blocks = *total_blocks; sim_params.time = *time; sim_params.timestep = *timestep; sim_params.nsteps = *nsteps; sim_params.nxb = nzones_block[0]; sim_params.nyb = nzones_block[1]; sim_params.nzb = nzones_block[2]; rank = 1; dimens_1d = 1; rank = 1; dimens_1d = 1; dataspace = H5Screate_simple(rank, &dimens_1d, NULL); sp_type = H5Tcreate(H5T_COMPOUND, sizeof(sim_params_t)); H5Tinsert(sp_type, "total blocks", offsetof(sim_params_t, total_blocks), H5T_NATIVE_INT); H5Tinsert(sp_type, "time", offsetof(sim_params_t, time), H5T_NATIVE_FLOAT); H5Tinsert(sp_type, "timestep", offsetof(sim_params_t, timestep), H5T_NATIVE_FLOAT); H5Tinsert(sp_type, "number of steps", offsetof(sim_params_t, nsteps), H5T_NATIVE_INT); H5Tinsert(sp_type, "nxb", offsetof(sim_params_t, nxb), H5T_NATIVE_INT); H5Tinsert(sp_type, "nyb", offsetof(sim_params_t, nyb), H5T_NATIVE_INT); H5Tinsert(sp_type, "nzb", offsetof(sim_params_t, nzb), H5T_NATIVE_INT); dataset = H5Dcreate(*file_identifier, "simulation parameters", sp_type, dataspace, H5P_DEFAULT); if (*MyPE == 0) { status = H5Dwrite(dataset, sp_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, &sim_params); } H5Sclose(dataspace); H5Dclose(dataset); H5Tclose(sp_type); /* unknown names */ rank = 2; dimens_2d[0] = (hsize_t) *nvar_out; dimens_2d[1] = 1; /* manually set the string size */ string_size = 4; /* setup the datatype for this string length */ string_type = H5Tcopy(H5T_C_S1); status = H5Tset_size(string_type, string_size); #ifdef DEBUG_IO printf("MyPE = %d, string type = %d, set size status = %d\n", *MyPE, (int) string_type, (int) status); #endif dataspace = H5Screate_simple(rank, dimens_2d, NULL); dataset = H5Dcreate(*file_identifier, "unknown names", string_type, dataspace, H5P_DEFAULT); if (*MyPE == 0) { status = H5Dwrite(dataset, string_type, H5S_ALL, H5S_ALL, H5P_DEFAULT, unk_labels); } H5Tclose(string_type); H5Sclose(dataspace); H5Dclose(dataset); }
/*------------------------------------------------------------------------- * Function: create_szip_dsets_float * * Purpose: Create a dataset of FLOAT datatype with szip filter * * Return: Success: 0 * Failure: -1 * * Programmer: Raymond Lu * 29 March 2011 * * Modifications: * *------------------------------------------------------------------------- */ int create_szip_dsets_float(hid_t fid, hid_t fsid, hid_t msid) { hid_t dataset; /* dataset handles */ hid_t dcpl; float data[NX][NY]; /* data to write */ float fillvalue = -2.2f; hsize_t chunk[RANK] = {CHUNK0, CHUNK1}; int i, j; /* * Data and output buffer initialization. */ for (j = 0; j < NX; j++) { for (i = 0; i < NY; i++) data[j][i] = ((float)(i + j + 1))/3; } /* * Create the dataset creation property list, add the Scale-Offset * filter, set the chunk size, and set the fill value. */ if((dcpl = H5Pcreate(H5P_DATASET_CREATE)) < 0) TEST_ERROR if(H5Pset_szip(dcpl, H5_SZIP_NN_OPTION_MASK, 4) < 0) TEST_ERROR if(H5Pset_chunk(dcpl, RANK, chunk) < 0) TEST_ERROR if(H5Pset_fill_value(dcpl, H5T_NATIVE_FLOAT, &fillvalue) < 0) TEST_ERROR /* * Create a new dataset within the file using defined dataspace, little * endian datatype and default dataset creation properties. */ if((dataset = H5Dcreate2(fid, DATASETNAME18, H5T_IEEE_F32LE, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR /* * Write the data to the dataset using default transfer properties. */ if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR /* Close dataset */ if(H5Dclose(dataset) < 0) TEST_ERROR /* Now create a dataset with a big-endian type */ if((dataset = H5Dcreate2(fid, DATASETNAME19, H5T_IEEE_F32BE, fsid, H5P_DEFAULT, dcpl, H5P_DEFAULT)) < 0) TEST_ERROR if(H5Dwrite(dataset, H5T_NATIVE_FLOAT, msid, fsid, H5P_DEFAULT, data) < 0) TEST_ERROR if(H5Dclose(dataset) < 0) TEST_ERROR /* * Close/release resources. */ if(H5Pclose(dcpl) < 0) TEST_ERROR return 0; error: H5E_BEGIN_TRY { H5Pclose(dcpl); H5Dclose(dataset); } H5E_END_TRY; return -1; }
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; }