TEST_F(ObservableTest, ParticleNumbersRunAndSaveToFile) { boost::filesystem::path pathH5("pNumbers_test.h5"); boost::filesystem::path pathDat("pNumbers_test.dat"); boost::filesystem::remove(pathH5); boost::filesystem::remove(pathDat); // actual test World w; Config c; Simulation s(&w, &c, ""); c.new_Type("testparticle0", 1., 1.); c.new_Type("testparticle1", 1., 1.); std::vector<double> pos = {0.,0.,0.}; w.addParticle(pos, 0); w.addParticle(pos, 0); w.addParticle(pos, 1); s.new_ParticleNumbers(1, "pNumbers_test.h5", 0); s.new_ParticleNumbers(1, "pNumbers_test.dat", 0); s.run(4); s.writeAllObservablesToFile(); // check h5 file hid_t fileId = H5Fopen("pNumbers_test.h5", H5F_ACC_RDONLY, H5P_DEFAULT); hsize_t dims[1]; H5LTget_dataset_info(fileId, "/particleNumbers", dims, NULL, NULL); EXPECT_EQ(dims[0], 5) << "expecting 5 entries"; unsigned long pNumbers[5]; H5LTread_dataset(fileId, "/particleNumbers", H5T_NATIVE_ULONG, pNumbers); EXPECT_THAT(pNumbers, ::testing::ElementsAre(2,2,2,2,2)) << "number of 0-particles should be 2 for all times"; H5Fclose(fileId); }
TEST_F(ObservableTest, IncrementsRunAndSaveToFile) { boost::filesystem::path path("increments_test.h5"); boost::filesystem::remove(path); //actual test World w; Config c; Simulation s(&w, &c, ""); c.new_Type("testparticle", 1., 1.); std::vector<double> pos = {0.,0.,0.}; w.addParticle(pos, 0); w.addParticle(pos, 0); w.addParticle(pos, 0); // increments observable s.new_Increments(1, 30, "increments_test.h5", 0); // run s.run(10); s.writeAllObservablesToFile(); // check h5 files hid_t fileId = H5Fopen("increments_test.h5", H5F_ACC_RDONLY, H5P_DEFAULT); hsize_t dims[3]; H5LTget_dataset_info(fileId, "/increments", dims, NULL, NULL); EXPECT_EQ(dims[0], 10) << "expect 10 timesteps"; EXPECT_EQ(dims[1], 3) << "and 3 particles"; EXPECT_EQ(dims[2], 3) << "and 3 coordinates"; double incs[10][3][3]; H5LTread_dataset(fileId, "/increments", H5T_NATIVE_DOUBLE, incs); H5Fclose(fileId); }
TEST_F(ObservableTest, MSDRunAndSaveToFile) { // if files exists, delete them first boost::filesystem::path pathH5("msd_test.h5"); boost::filesystem::path pathDat("msd_test.dat"); boost::filesystem::remove(pathH5); boost::filesystem::remove(pathDat); // now actual test. default implementation World w; Config c; Simulation s(&w, &c, ""); c.new_Type("testparticle", 1., 1.); std::vector<double> pos = {0.,0.,0.}; w.addParticle(pos, 0); s.new_MeanSquaredDisplacement(1, "msd_test.h5", 0); s.new_MeanSquaredDisplacement(1, "msd_test.dat", 0); s.run(4); s.writeAllObservablesToFile(); // check h5 file hid_t fileId = H5Fopen("msd_test.h5", H5F_ACC_RDONLY, H5P_DEFAULT); hsize_t dims[1]; H5LTget_dataset_info(fileId, "/meanSquaredDisplacements", dims, NULL, NULL); EXPECT_EQ(dims[0], 5) << "expecting 5 entries in MSD"; double msd[5]; H5LTread_dataset_double(fileId, "/meanSquaredDisplacements", msd); EXPECT_EQ(msd[0], 0.) << "initially the displacement must be zero"; EXPECT_NE(msd[4], 0.) << "the final value is usually non-zero"; unsigned int numbers[5]; H5LTread_dataset(fileId, "/numberOfParticles", H5T_NATIVE_UINT, numbers); EXPECT_THAT(numbers, ::testing::ElementsAre(1,1,1,1,1)) << "number of particles should be 1 for all times"; H5Fclose(fileId); }
int_f nh5ltread_dataset_c (hid_t_f *loc_id, int_f *namelen, _fcd name, hid_t_f *type_id, void *buf, hsize_t_f *dims) { int ret_value = -1; herr_t ret; hid_t c_loc_id; hid_t c_type_id; char *c_name; int c_namelen; /* * Convert FORTRAN name to C name */ c_namelen = *namelen; c_name = (char *)HD5f2cstring(name, c_namelen); if (c_name == NULL) return ret_value; /* * Call H5LTread_dataset function. */ c_loc_id = (hid_t)*loc_id; c_type_id = (hid_t)*type_id; ret = H5LTread_dataset(c_loc_id, c_name, c_type_id, buf ); if (ret < 0) return ret_value; ret_value = 0; return ret_value; }
/*-------------------------------------------------------------------------*/ int FTI_RecoverVarHDF5(FTIT_execution* FTI_Exec, FTIT_checkpoint* FTI_Ckpt, FTIT_dataset* FTI_Data, int id) { char str[FTI_BUFS], fn[FTI_BUFS]; snprintf(fn, FTI_BUFS, "%s/%s", FTI_Ckpt[FTI_Exec->ckptLvel].dir, FTI_Exec->meta[FTI_Exec->ckptLvel].ckptFile); sprintf(str, "Trying to load FTI checkpoint file (%s)...", fn); FTI_Print(str, FTI_DBUG); hid_t file_id = H5Fopen(fn, H5F_ACC_RDONLY, H5P_DEFAULT); if (file_id < 0) { FTI_Print("Could not open FTI checkpoint file.", FTI_EROR); return FTI_NREC; } FTI_Exec->H5groups[0]->h5groupID = file_id; FTIT_H5Group* rootGroup = FTI_Exec->H5groups[0]; int i; for (i = 0; i < FTI_Exec->H5groups[0]->childrenNo; i++) { FTI_OpenGroup(FTI_Exec->H5groups[rootGroup->childrenID[i]], file_id, FTI_Exec->H5groups); } for (i = 0; i < FTI_Exec->nbVar; i++) { if (FTI_Data[i].id == id) { break; } } hid_t h5Type = FTI_Data[i].type->h5datatype; if (FTI_Data[i].type->id > 10 && FTI_Data[i].type->structure == NULL) { //if used FTI_InitType() save as binary h5Type = H5Tcopy(H5T_NATIVE_CHAR); H5Tset_size(h5Type, FTI_Data[i].size); } herr_t res = H5LTread_dataset(FTI_Data[i].h5group->h5groupID, FTI_Data[i].name, h5Type, FTI_Data[i].ptr); if (res < 0) { FTI_Print("Could not read FTI checkpoint file.", FTI_EROR); int j; for (j = 0; j < FTI_Exec->H5groups[0]->childrenNo; j++) { FTI_CloseGroup(FTI_Exec->H5groups[rootGroup->childrenID[j]], FTI_Exec->H5groups); } H5Fclose(file_id); return FTI_NREC; } int j; for (j = 0; j < FTI_Exec->H5groups[0]->childrenNo; j++) { FTI_CloseGroup(FTI_Exec->H5groups[rootGroup->childrenID[j]], FTI_Exec->H5groups); } if (H5Fclose(file_id) < 0) { FTI_Print("Could not close FTI checkpoint file.", FTI_EROR); return FTI_NREC; } return FTI_SCES; }
TEST_F(ObservableTest, RadialDistrRunAndSaveToFile) { boost::filesystem::path pathH5("radial_test.h5"); boost::filesystem::path pathDat("radial_test.dat"); boost::filesystem::remove(pathH5); boost::filesystem::remove(pathDat); // actual test World w; Config c; Simulation s(&w, &c, ""); c.new_Type("testparticle0", 1., 1.); std::vector<double> pos = {0.,0.,0.}; w.addParticle(pos, 0); w.addParticle(pos, 0); // radial distribution with two bins [0,3), [3,5) std::vector<double> ranges = {0,3,5}; std::array<unsigned,2> consideredElem = {0,0}; std::vector<std::array<unsigned,2>> considered = {consideredElem}; s.new_RadialDistribution(1, "radial_test.h5", ranges, considered); s.new_RadialDistribution(1, "radial_test.dat", ranges, considered); // run for 2 timesteps s.run(2); s.writeAllObservablesToFile(); // check h5 files hid_t fileId = H5Fopen("radial_test.h5", H5F_ACC_RDONLY, H5P_DEFAULT); // check binCenters hsize_t dims[1]; H5LTget_dataset_info(fileId, "/binCenters", dims, NULL, NULL); EXPECT_EQ(dims[0], 2) << "expecting 2 bins"; double binCenters[2]; H5LTread_dataset(fileId, "/binCenters", H5T_NATIVE_DOUBLE, binCenters); EXPECT_THAT(binCenters, ::testing::ElementsAre(1.5, 4)) << "binCenters according to ranges [0,3,5]"; // check bins (content) H5LTget_dataset_info(fileId, "/bins", dims, NULL, NULL); EXPECT_EQ(dims[0], 2) << "expecting 2 bins"; double bins[2]; H5LTread_dataset(fileId, "/bins", H5T_NATIVE_DOUBLE, bins); EXPECT_EQ(bins[1], 0) << "expecting 0 counts in outer bin, since particles start at 0 distance."; H5Fclose(fileId); }
//! Test write dataset. // Data extracted from http://www.hdfgroup.org/ftp/HDF5/examples/examples-by-api/hdf5-examples/1_8/C/H5T/h5ex_t_string.c char *test_write_complex_dataset() { hid_t file_id, filetype, memtype, space, dset; size_t sdim; hsize_t dims[1] = {2}; int ndims, i, j; int rank; int length; size_t type_size; float * buf; hid_t real_id_type; herr_t status; hsize_t *newdims; AH5_complex_t cplx[2]; file_id = AH5_auto_test_file(); cplx[0].re=10.; cplx[0].im=20.; cplx[1].re=10.5; cplx[1].im=20.5; mu_assert("Write complex dataset.", AH5_write_cpx_dataset(file_id,"dataset_name", 2, cplx)); // Test the written data using hdf5 API. real_id_type = create_type_id(H5T_NATIVE_FLOAT); status = H5LTget_dataset_ndims(file_id, "dataset_name", &rank); newdims = (hsize_t *) malloc(rank * sizeof(hsize_t)); status = H5LTget_dataset_info(file_id,"dataset_name" , newdims, NULL, NULL); length = newdims[0]; for (i = 1; i < rank; i++) length = length * newdims[i]; buf = (float *) malloc(2 * length * sizeof(float)); status = H5LTread_dataset(file_id, "dataset_name", real_id_type, buf); j = 0; for (i = 0; i < length; i++) { printf("Real parts : %f %f\n", cplx[i].re, buf[j]); printf("Imaginary parts : %f %f\n", cplx[i].im, buf[j+1]); mu_assert_equal("Check the real values.", cplx[i].re, buf[j]); mu_assert_equal("Check the imaginary value.", cplx[i].im, buf[j+1]); j = j + 2; } free(buf); return MU_FINISHED_WITHOUT_ERRORS; }
TEST_F(ObservableTest, ProbDensRunAndSaveToFile) { boost::filesystem::path pathH5("probdens_test.h5"); boost::filesystem::path pathDat("probdens_test.dat"); boost::filesystem::remove(pathH5); boost::filesystem::remove(pathDat); // actual test World w; Config c; Simulation s(&w, &c, ""); c.new_Type("testparticle0", 1., 1.); std::vector<double> pos = {0.,0.,0.}; w.addParticle(pos, 0); w.addParticle(pos, 0); // prob density in z direction with 3 bins [-5,-3), [-3,3), [3,5) std::vector<double> ranges = {-5, -3, +3, +5}; unsigned coord = 2, pTypeId = 0; s.new_ProbabilityDensity(1, "probdens_test.h5", pTypeId, ranges, coord); s.new_ProbabilityDensity(1, "probdens_test.dat", pTypeId, ranges, coord); // run for 2 timesteps s.run(2); s.writeAllObservablesToFile(); // check h5 file hid_t fileId = H5Fopen("probdens_test.h5", H5F_ACC_RDONLY, H5P_DEFAULT); // check binCenters hsize_t dims[1]; H5LTget_dataset_info(fileId, "/binCenters", dims, NULL, NULL); EXPECT_EQ(dims[0], 3) << "expecting 3 bins"; double binCenters[3]; H5LTread_dataset(fileId, "/binCenters", H5T_NATIVE_DOUBLE, binCenters); EXPECT_THAT(binCenters, ::testing::ElementsAre(-4, 0, 4)) << "binCenters according to ranges [-5,-3,3,5]"; // check bins (content) H5LTget_dataset_info(fileId, "/bins", dims, NULL, NULL); EXPECT_EQ(dims[0], 3) << "expecting 3 bins"; double bins[3]; H5LTread_dataset(fileId, "/bins", H5T_NATIVE_DOUBLE, bins); EXPECT_THAT(bins, ::testing::ElementsAre(0, 6, 0)) << "bin contents after 2 timesteps of two particles."; H5Fclose(fileId); }
float_complex *read_complex_dataset(hid_t loc_id, const char* path) { float_complex *values; int rank; int i; hsize_t *dims; int length; size_t type_size; float * buf; hid_t real_id_type; herr_t status; real_id_type = create_real_type_id(); status = H5LTget_dataset_ndims(loc_id, path, &rank); if (status < 0) { printf("Can't read rank \n"); } dims = (hsize_t *) malloc(rank * sizeof(hsize_t)); status = H5LTget_dataset_info(loc_id, path, dims, NULL, NULL); if (status < 0) { printf("Can't read dataset info\n"); } length = dims[0]; for (i = 1; i < rank; i++) length = length * dims[i]; buf = (float *) malloc(2 * length * sizeof(float)); status = H5LTread_dataset(loc_id, path, real_id_type, buf); if (status < 0) { printf("Can't read dataset\n"); } values = (float_complex *) malloc(length*sizeof(float_complex)); int j = 0; for (i = 0; i < length; i++) { values[i].re = buf[j]; values[i].im = buf[j + 1]; j = j + 2; } free(buf); free(dims); return values; }
/** * Write data from the dataset. * * @param [in] DatasetName * @param [in] DimensionSizes * @param [out] Data * @throw ios::failure */ void THDF5_File::ReadCompleteDataset (const char * DatasetName, const TDimensionSizes & DimensionSizes,long * Data){ if (GetDatasetDimensionSizes(DatasetName).GetElementCount() != DimensionSizes.GetElementCount()) { char ErrorMessage[256]; sprintf(ErrorMessage,HDF5_ERR_FMT_WrongDimensionSizes,DatasetName); throw ios::failure(ErrorMessage); } /* read dataset */ herr_t status = H5LTread_dataset(HDF5_FileId,DatasetName,H5T_STD_U64LE, Data); if (status < 0) { char ErrorMessage[256]; sprintf(ErrorMessage,HDF5_ERR_FMT_CouldNotReadFrom,DatasetName); throw ios::failure(ErrorMessage); } }// end of ReadDataset(long)
bool MaskedImage::readMaskHDF5File(const UString & fileName) { hid_t fileID = H5Fopen(fileName, H5F_ACC_RDONLY, H5P_DEFAULT); if(fileID < 0) { fprintf(stderr, "MaskedImage::readMaskHDF5File: Could not open file %s\n", (const char *)fileName); return false; } UString dataSetName = "/mask"; hsize_t dimensions[2]; H5T_class_t typeClass; size_t typeSize; herr_t status = H5LTget_dataset_info(fileID, dataSetName, dimensions, &typeClass, &typeSize); if(status < 0) { dataSetName.makeUpper(); status = H5LTget_dataset_info(fileID, dataSetName, dimensions, &typeClass, &typeSize); } if((status < 0) || (typeClass != H5T_INTEGER) || (typeSize != 1) || (getYSize() != (SInt32)dimensions[0]) || (getXSize() != (SInt32)dimensions[1])) { fprintf(stderr, "MaskedImage::readMaskHDF5File: Could not get dataset info for %s in file %s\n", (const char *)dataSetName, (const char *)fileName); return false; } mask.setSize(getXSize()*getYSize()); status = H5LTread_dataset(fileID, dataSetName, H5T_NATIVE_UCHAR, mask.getData()); if(status < 0) { fprintf(stderr, "MaskedImage::readMaskHDF5File: Could not read dataset %s in file %s\n", (const char *)dataSetName, (const char *)fileName); return false; } status = H5Fclose(fileID); return true; }
/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/ int IDL_STDCALL _SDMF_RD_PT_CLUSDEF( int argc, void *argv[] ) { IDL_STRING *dbName; struct clusdef_rec *ClusDef; hid_t fid = -1; hid_t data_id = -1; hid_t type_id = -1; /* * check number of parameters */ if ( argc != 2 ) NADC_GOTO_ERROR( NADC_ERR_PARAM, err_msg ); dbName = (IDL_STRING *) argv[0]; ClusDef = (struct clusdef_rec *) argv[1]; fid = H5Fopen( dbName->s, H5F_ACC_RDONLY, H5P_DEFAULT ); if ( fid < 0 ) NADC_GOTO_ERROR( NADC_ERR_HDF_FILE, dbName->s ); if ( (data_id = H5Dopen( fid, "ClusDef", H5P_DEFAULT )) < 0 ) NADC_GOTO_ERROR( NADC_ERR_HDF_DATA, "ClusDef" ); if ( (type_id = H5Dget_type( data_id )) < 0 ) NADC_GOTO_ERROR( NADC_ERR_HDF_DTYPE, "ClusDef" ); if ( H5LTread_dataset( fid, "ClusDef", type_id, ClusDef ) < 0 ) NADC_GOTO_ERROR( NADC_ERR_HDF_RD, "ClusDef" ); (void) H5Tclose( type_id ); (void) H5Dclose( data_id ); (void) H5Fclose( fid ); return 1; done: H5E_BEGIN_TRY { (void) H5Tclose( type_id ); (void) H5Dclose( data_id ); (void) H5Fclose( fid ); } H5E_END_TRY; return -1; }
int_f h5ltread_dataset_c (hid_t_f *loc_id, size_t_f *namelen, _fcd name, hid_t_f *type_id, void *buf) { int ret_value = -1; herr_t ret; hid_t c_loc_id; hid_t c_type_id; char *c_name = NULL; /* * convert FORTRAN name to C name */ c_name = (char *)HD5f2cstring(name, (size_t)*namelen); if (c_name == NULL) goto done; /* * call H5LTread_dataset function. */ c_loc_id = (hid_t)*loc_id; c_type_id = (hid_t)*type_id; ret = H5LTread_dataset(c_loc_id, c_name, c_type_id, buf ); if (ret < 0) goto done; ret_value = 0; done: if(c_name!=NULL) HDfree(c_name); return ret_value; }
bool MaskedImage::readTimeHDF5File(const UString & fileName, double & outTime) { hid_t fileID = H5Fopen(fileName, H5F_ACC_RDONLY, H5P_DEFAULT); if(fileID < 0) { fprintf(stderr, "MaskedImage::readTimeHDF5File: Could not open file %s\n", (const char *)fileName); return false; } UString dataSetName = "/time"; hsize_t dimensions[1]; H5T_class_t typeClass; size_t typeSize; dimensions[0] = 1; herr_t status = H5LTget_dataset_info(fileID, dataSetName, dimensions, &typeClass, &typeSize); if(status < 0) { dataSetName.makeUpper(); status = H5LTget_dataset_info(fileID, dataSetName, dimensions, &typeClass, &typeSize); } if((status < 0) || (typeClass != H5T_FLOAT) || (typeSize != 8) || (dimensions[0] != 1)) { fprintf(stderr, "MaskedImage::readTimeHDF5File: Problem with dataset info for %s in file %s\n", (const char *)dataSetName, (const char *)fileName); return false; } status = H5LTread_dataset(fileID, dataSetName, H5T_NATIVE_DOUBLE, &outTime); if(status < 0) { fprintf(stderr, "MaskedImage::readTimeHDF5File: Could not read dataset %s in file %s\n", (const char *)dataSetName, (const char *)fileName); return false; } status = H5Fclose(fileID); return true; }
void stfio::importHDF5File(const std::string& fName, Recording& ReturnData, ProgressInfo& progDlg) { /* Create a new file using default properties. */ hid_t file_id = H5Fopen(fName.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT); /* H5TBread_table const int NRECORDS = 1;*/ const int NFIELDS = 3; /* Calculate the size and the offsets of our struct members in memory */ size_t rt_offset[NFIELDS] = { HOFFSET( rt, channels ), HOFFSET( rt, date ), HOFFSET( rt, time )}; rt rt_buf[1]; size_t rt_sizes[NFIELDS] = { sizeof( rt_buf[0].channels), sizeof( rt_buf[0].date), sizeof( rt_buf[0].time)}; herr_t status=H5TBread_table( file_id, "description", sizeof(rt), rt_offset, rt_sizes, rt_buf ); if (status < 0) { std::string errorMsg("Exception while reading description in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } int numberChannels =rt_buf[0].channels; if ( ReturnData.SetDate(rt_buf[0].date) || ReturnData.SetTime(rt_buf[0].time) ) { std::cout << "Warning HDF5: could not decode date/time " << rt_buf[0].date << " " << rt_buf[0].time << std::endl; } /* Create the data space for the dataset. */ hsize_t dims; H5T_class_t class_id; size_t type_size; std::string description, comment; hid_t group_id = H5Gopen2(file_id, "/comment", H5P_DEFAULT); status = H5Lexists(group_id, "/comment/description", 0); if (status==1) { status = H5LTget_dataset_info( file_id, "/comment/description", &dims, &class_id, &type_size ); if (status >= 0) { description.resize( type_size ); status = H5LTread_dataset_string (file_id, "/comment/description", &description[0]); if (status < 0) { std::string errorMsg("Exception while reading description in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } } } ReturnData.SetFileDescription(description); status = H5Lexists(group_id, "/comment/comment", 0); if (status==1) { status = H5LTget_dataset_info( file_id, "/comment/comment", &dims, &class_id, &type_size ); if (status >= 0) { comment.resize( type_size ); status = H5LTread_dataset_string (file_id, "/comment/comment", &comment[0]); if (status < 0) { std::string errorMsg("Exception while reading comment in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } } } ReturnData.SetComment(comment); double dt = 1.0; std::string yunits = ""; for (int n_c=0;n_c<numberChannels;++n_c) { /* Calculate the size and the offsets of our struct members in memory */ size_t ct_offset[NFIELDS] = { HOFFSET( ct, n_sections ) }; ct ct_buf[1]; size_t ct_sizes[NFIELDS] = { sizeof( ct_buf[0].n_sections) }; /* Read channel name */ hsize_t cdims; H5T_class_t cclass_id; size_t ctype_size; std::ostringstream desc_path; desc_path << "/channels/ch" << (n_c); status = H5LTget_dataset_info( file_id, desc_path.str().c_str(), &cdims, &cclass_id, &ctype_size ); if (status < 0) { std::string errorMsg("Exception while reading channel in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } hid_t string_typec= H5Tcopy( H5T_C_S1 ); H5Tset_size( string_typec, ctype_size ); std::vector<char> szchannel_name(ctype_size); // szchannel_name.reset( new char[ctype_size] ); status = H5LTread_dataset(file_id, desc_path.str().c_str(), string_typec, &szchannel_name[0] ); if (status < 0) { std::string errorMsg("Exception while reading channel name in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } std::ostringstream channel_name; for (std::size_t c=0; c<ctype_size; ++c) { channel_name << szchannel_name[c]; } std::ostringstream channel_path; channel_path << "/" << channel_name.str(); hid_t channel_group = H5Gopen2(file_id, channel_path.str().c_str(), H5P_DEFAULT ); status=H5TBread_table( channel_group, "description", sizeof(ct), ct_offset, ct_sizes, ct_buf ); if (status < 0) { std::string errorMsg("Exception while reading channel description in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } Channel TempChannel(ct_buf[0].n_sections); TempChannel.SetChannelName( channel_name.str() ); int max_log10 = 0; if (ct_buf[0].n_sections > 1) { max_log10 = int(log10((double)ct_buf[0].n_sections-1.0)); } for (int n_s=0; n_s < ct_buf[0].n_sections; ++n_s) { int progbar = // Channel contribution: (int)(((double)n_c/(double)numberChannels)*100.0+ // Section contribution: (double)(n_s)/(double)ct_buf[0].n_sections*(100.0/numberChannels)); std::ostringstream progStr; progStr << "Reading channel #" << n_c + 1 << " of " << numberChannels << ", Section #" << n_s+1 << " of " << ct_buf[0].n_sections; progDlg.Update(progbar, progStr.str()); // construct a number with leading zeros: int n10 = 0; if (n_s > 0) { n10 = int(log10((double)n_s)); } std::ostringstream strZero; strZero << ""; for (int n_z=n10; n_z < max_log10; ++n_z) { strZero << "0"; } // construct a section name: std::ostringstream section_name; section_name << "sec" << n_s; // create a child group in the channel: std::ostringstream section_path; section_path << channel_path.str() << "/" << "section_" << strZero.str() << n_s; hid_t section_group = H5Gopen2(file_id, section_path.str().c_str(), H5P_DEFAULT ); std::ostringstream data_path; data_path << section_path.str() << "/data"; hsize_t sdims; H5T_class_t sclass_id; size_t stype_size; status = H5LTget_dataset_info( file_id, data_path.str().c_str(), &sdims, &sclass_id, &stype_size ); if (status < 0) { std::string errorMsg("Exception while reading data information in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } Vector_float TempSection(sdims); status = H5LTread_dataset(file_id, data_path.str().c_str(), H5T_IEEE_F32LE, &TempSection[0]); if (status < 0) { std::string errorMsg("Exception while reading data in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } Section TempSectionT(TempSection.size(), section_name.str()); for (std::size_t cp = 0; cp < TempSectionT.size(); ++cp) { TempSectionT[cp] = double(TempSection[cp]); } // std::copy(TempSection.begin(),TempSection.end(),&TempSectionT[0]); try { TempChannel.InsertSection(TempSectionT,n_s); } catch (...) { throw; } /* H5TBread_table const int NSRECORDS = 1; */ const int NSFIELDS = 3; /* Calculate the size and the offsets of our struct members in memory */ size_t st_offset[NSFIELDS] = { HOFFSET( st, dt ), HOFFSET( st, xunits ), HOFFSET( st, yunits )}; st st_buf[1]; size_t st_sizes[NSFIELDS] = { sizeof( st_buf[0].dt), sizeof( st_buf[0].xunits), sizeof( st_buf[0].yunits)}; status=H5TBread_table( section_group, "description", sizeof(st), st_offset, st_sizes, st_buf ); if (status < 0) { std::string errorMsg("Exception while reading data description in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } dt = st_buf[0].dt; yunits = st_buf[0].yunits; H5Gclose( section_group ); } try { if ((int)ReturnData.size()<numberChannels) { ReturnData.resize(numberChannels); } ReturnData.InsertChannel(TempChannel,n_c); ReturnData[n_c].SetYUnits( yunits ); } catch (...) { ReturnData.resize(0); throw; } H5Gclose( channel_group ); } ReturnData.SetXScale(dt); /* Terminate access to the file. */ status = H5Fclose(file_id); if (status < 0) { std::string errorMsg("Exception while closing file in stfio::importHDF5File"); throw std::runtime_error(errorMsg); } /* Release all hdf5 resources */ status = H5close(); if (status < 0) { std::string errorMsg("Exception while closing file in stfio::exportHDF5File"); throw std::runtime_error(errorMsg); } }
static int test_dsets( void ) { int rank = 2; hsize_t dims[2] = {2,3}; hid_t file_id; hid_t dataset_id; char data_char_in[DIM] = {1,2,3,4,5,6}; char data_char_out[DIM]; short data_short_in[DIM] = {1,2,3,4,5,6}; short data_short_out[DIM]; int data_int_in[DIM] = {1,2,3,4,5,6}; int data_int_out[DIM]; long data_long_in[DIM] = {1,2,3,4,5,6}; long data_long_out[DIM]; float data_float_in[DIM] = {1,2,3,4,5,6}; float data_float_out[DIM]; double data_double_in[DIM] = {1,2,3,4,5,6}; double data_double_out[DIM]; const char *data_string_in = "This is a string"; char data_string_out[20]; int i; /* Create a new file using default properties. */ file_id = H5Fcreate( FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /*------------------------------------------------------------------------- * H5LTmake_dataset test *------------------------------------------------------------------------- */ TESTING("H5LTmake_dataset"); /* Make dataset */ if ( H5LTmake_dataset( file_id, DSET0_NAME, rank, dims, H5T_NATIVE_INT, data_int_in ) < 0 ) goto out; /* Read dataset using the basic HDF5 API */ if ( ( dataset_id = H5Dopen2(file_id, DSET0_NAME, H5P_DEFAULT) ) < 0 ) goto out; if ( H5Dread ( dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT, data_int_out ) < 0 ) goto out; if ( H5Dclose( dataset_id ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_int_in[i] != data_int_out[i] ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * read using the LT function H5LTread_dataset *------------------------------------------------------------------------- */ TESTING("H5LTread_dataset"); if ( H5LTread_dataset( file_id, DSET0_NAME, H5T_NATIVE_INT, data_int_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_int_in[i] != data_int_out[i] ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * test the H5LTmake_dataset_ functions *------------------------------------------------------------------------- */ /*------------------------------------------------------------------------- * H5LTmake_dataset_char *------------------------------------------------------------------------- */ TESTING("H5LTmake_dataset_char"); /* Make dataset char */ if ( H5LTmake_dataset_char( file_id, DSET1_NAME, rank, dims, data_char_in ) < 0 ) goto out; /* Read dataset */ if ( H5LTread_dataset( file_id, DSET1_NAME, H5T_NATIVE_CHAR, data_char_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_char_in[i] != data_char_out[i] ) { goto out; } } /* Read dataset */ if ( H5LTread_dataset_char( file_id, DSET1_NAME, data_char_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_char_in[i] != data_char_out[i] ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * H5LTmake_dataset_short *------------------------------------------------------------------------- */ TESTING("H5LTmake_dataset_short"); /* Make dataset short */ if ( H5LTmake_dataset_short( file_id, DSET2_NAME, rank, dims, data_short_in ) < 0 ) goto out; /* Read dataset */ if ( H5LTread_dataset( file_id, DSET2_NAME, H5T_NATIVE_SHORT, data_short_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_short_in[i] != data_short_out[i] ) { goto out; } } /* Read dataset */ if ( H5LTread_dataset_short( file_id, DSET2_NAME, data_short_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_short_in[i] != data_short_out[i] ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * H5LTmake_dataset_int *------------------------------------------------------------------------- */ TESTING("H5LTmake_dataset_int"); /* Make dataset int */ if ( H5LTmake_dataset_int( file_id, DSET3_NAME, rank, dims, data_int_in ) < 0 ) goto out; /* Read dataset */ if ( H5LTread_dataset( file_id, DSET3_NAME, H5T_NATIVE_INT, data_int_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_int_in[i] != data_int_out[i] ) { goto out; } } /* Read dataset */ if ( H5LTread_dataset_int( file_id, DSET3_NAME, data_int_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_int_in[i] != data_int_out[i] ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * H5LTmake_dataset_long *------------------------------------------------------------------------- */ TESTING("H5LTmake_dataset_long"); /* Make dataset long */ if ( H5LTmake_dataset_long( file_id, DSET4_NAME, rank, dims, data_long_in ) < 0 ) goto out; /* Read dataset */ if ( H5LTread_dataset( file_id, DSET4_NAME, H5T_NATIVE_LONG, data_long_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_long_in[i] != data_long_out[i] ) { goto out; } } /* Read dataset */ if ( H5LTread_dataset_long( file_id, DSET4_NAME, data_long_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_long_in[i] != data_long_out[i] ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * H5LTmake_dataset_float *------------------------------------------------------------------------- */ TESTING("H5LTmake_dataset_float"); /* Make dataset float */ if ( H5LTmake_dataset_float( file_id, DSET5_NAME, rank, dims, data_float_in ) < 0 ) goto out; /* Read dataset */ if ( H5LTread_dataset( file_id, DSET5_NAME, H5T_NATIVE_FLOAT, data_float_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_float_in[i] != data_float_out[i] ) { goto out; } } /* Read dataset */ if ( H5LTread_dataset_float( file_id, DSET5_NAME, data_float_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_float_in[i] != data_float_out[i] ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * H5LTmake_dataset_double *------------------------------------------------------------------------- */ TESTING("H5LTmake_dataset_double"); /* Make dataset double */ if ( H5LTmake_dataset_double( file_id, DSET6_NAME, rank, dims, data_double_in ) < 0 ) goto out; /* Read dataset */ if ( H5LTread_dataset( file_id, DSET6_NAME, H5T_NATIVE_DOUBLE, data_double_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_double_in[i] != data_double_out[i] ) { goto out; } } /* Read dataset */ if ( H5LTread_dataset_double( file_id, DSET6_NAME, data_double_out ) < 0 ) goto out; for (i = 0; i < DIM; i++) { if ( data_double_in[i] != data_double_out[i] ) { goto out; } } PASSED(); /*------------------------------------------------------------------------- * H5LTmake_dataset_string *------------------------------------------------------------------------- */ TESTING("H5LTmake_dataset_string"); /* Make dataset string */ if ( H5LTmake_dataset_string(file_id,DSET7_NAME,data_string_in) < 0 ) goto out; /* Read dataset */ if ( H5LTread_dataset_string(file_id,DSET7_NAME,data_string_out) < 0 ) goto out; if ( strcmp(data_string_in,data_string_out) != 0 ) goto out; /*------------------------------------------------------------------------- * end tests *------------------------------------------------------------------------- */ /* Close the file. */ H5Fclose( file_id ); PASSED(); return 0; out: /* Close the file. */ H5_FAILED(); return -1; }