/*+++++++++++++++++++++++++ Main Program or Function +++++++++++++++*/ unsigned int SCIA_RD_H5_LADS( struct param_record param, struct lads_scia *lads ) /*@globals lads_size, lads_offs@*/ { hid_t ads_id; hsize_t nfields, num_lads; const size_t lads_sizes[NFIELDS] = { sizeof( lads->mjd ), sizeof( lads->flag_mds ), sizeof( lads->corner ) }; /* * create/open group /ADS/LADS */ ads_id = NADC_OPEN_HDF5_Group( param.hdf_file_id, "/ADS" ); if ( ads_id < 0 ) NADC_GOTO_ERROR( NADC_ERR_HDF_GRP, "/ADS" ); /* * read info_h5 records */ (void) H5TBget_table_info( ads_id, TBL_NAME, &nfields, &num_lads ); (void) H5TBread_table( ads_id, TBL_NAME, lads_size, lads_offs, lads_sizes, lads ); (void) H5Gclose( ads_id ); return (unsigned int) num_lads; done: return 0u; }
/*+++++++++++++++++++++++++ .IDENTifer SDMF_rd_simudarkTable .PURPOSE read metaTable records from SDMF simultaneous dark signal parameter database .INPUT/OUTPUT call as SDMF_rd_simudarkTable( locID, &numIndx, metaIndx, &mtbl ); input: hid_t locID : HDF5 identifier of file or group int *metaIndx : array with indices to requested records in/output: int *numIndx : input: dimension metaIndx (or zero) output: number records read output: struct mtbl_simudark_rec **mtbl : State meta-data records to read .RETURNS nothing, error status passed by global variable ``nadc_stat'' .COMMENTS none -------------------------*/ void SDMF_rd_simudarkTable( hid_t locID, int *numIndx, int *metaIndx, struct mtbl_simudark_rec **mtbl_out ) { hsize_t nfields, nrecords; herr_t stat; struct mtbl_simudark_rec *mtbl; /* * initialize return values */ *mtbl_out = NULL; /* * does the table already exist? */ H5E_BEGIN_TRY { hid_t dataID = H5Dopen( locID, tableName, H5P_DEFAULT ); if ( dataID < 0 ) return; (void) H5Dclose( dataID ); } H5E_END_TRY; /* * obtain table info */ stat = H5TBget_table_info(locID, tableName, &nfields, &nrecords ); if ( *numIndx == 0 ) *numIndx = (int) nrecords; if ( stat < 0 || nrecords == 0 ) return; /* * allocate memory to store metaTable records */ mtbl = (struct mtbl_simudark_rec *) malloc( (size_t) (*numIndx) * mtbl_size ); if ( mtbl == NULL ) NADC_RETURN_ERROR( NADC_ERR_ALLOC, "mtbl" ); /* * read table */ if ( (*numIndx) == (int) nrecords ) { stat = H5TBread_table( locID, tableName, mtbl_size, mtbl_offs, mtbl_simudark_sizes, mtbl ); if ( stat < 0 ) { free( mtbl ); NADC_RETURN_ERROR( NADC_ERR_HDF_RD, tableName ); } } else { register int nm; for ( nm = 0; nm < (*numIndx); nm++ ) { stat = H5TBread_records( locID, tableName, metaIndx[nm], 1, mtbl_size, mtbl_offs, mtbl_simudark_sizes, mtbl+nm ); if ( stat < 0 ) { free( mtbl ); NADC_RETURN_ERROR( NADC_ERR_HDF_RD, tableName ); } } } *mtbl_out = mtbl; }
int SpIO_H5ReadTau(hid_t h5f_id, Zone *zone) /* Write data of all children to an HDF5 table */ { SpPhys *pp = zone->data; /* Just in case the programmer did something stupid */ Deb_ASSERT(pp->mol != NULL); Deb_ASSERT(pp->tau != NULL); int status = 0; size_t i, j, nrad = pp->mol->nrad, record_size = sizeof(double) * nrad, field_sizes[nrad], field_offsets[nrad]; double *tau = Mem_CALLOC(zone->nchildren * nrad, tau); herr_t hstatus; /* Init fields */ for(i = 0; i < nrad; i++) { field_offsets[i] = i * sizeof(double); field_sizes[i] = sizeof(double); } /* Read tau table */ hstatus = H5TBread_table(h5f_id, "TAU", record_size, field_offsets, field_sizes, tau); if(hstatus < 0) status = Err_SETSTRING("Error reading HDF5 `%s' table", "TAU"); #define TAU(i, j)\ tau[(j) + nrad * (i)] for(i = 0; i < zone->nchildren; i++) { pp = zone->children[i]->data; for(j = 0; j < nrad; j++) { pp->tau[j] = TAU(i, j); } } #undef TAU free(tau); return status; }
int SpIO_H5ReadPops(hid_t h5f_id, Zone *zone) /* Write data of all children to an HDF5 table */ { SpPhys *pp = zone->data; /* Just in case the programmer did something stupid */ Deb_ASSERT(pp->mol != NULL); Deb_ASSERT(pp->pops[0] != NULL); int status = 0; size_t i, j, nlev = pp->mol->nlev, record_size = sizeof(double) * nlev, field_sizes[nlev], field_offsets[nlev]; double *pops = Mem_CALLOC(zone->nchildren * nlev, pops); herr_t hstatus; /* Init fields */ for(i = 0; i < nlev; i++) { field_offsets[i] = i * sizeof(double); field_sizes[i] = sizeof(double); } /* Read pops table */ hstatus = H5TBread_table(h5f_id, "POPS", record_size, field_offsets, field_sizes, pops); if(hstatus < 0) status = Err_SETSTRING("Error reading HDF5 `%s' table", "POPS"); #define POPS(i, j)\ pops[(j) + nlev * (i)] for(i = 0; i < zone->nchildren; i++) { pp = zone->children[i]->data; for(j = 0; j < nlev; j++) { pp->pops[0][j] = POPS(i, j); } } #undef POPS free(pops); return status; }
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); } }
int main( void ) { typedef struct Particle { char name[16]; int lati; int longi; float pressure; double temperature; } Particle; Particle dst_buf[ NRECORDS + NRECORDS_INS ]; /* Calculate the size and the offsets of our struct members in memory */ size_t dst_size = sizeof( Particle ); size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ), HOFFSET( Particle, lati ), HOFFSET( Particle, longi ), HOFFSET( Particle, pressure ), HOFFSET( Particle, temperature )}; size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name), sizeof( dst_buf[0].lati), sizeof( dst_buf[0].longi), sizeof( dst_buf[0].pressure), sizeof( dst_buf[0].temperature)}; /* Define an array of Particles */ Particle p_data[NRECORDS] = { {"zero",0,0, 0.0f, 0.0}, {"one",10,10, 1.0f, 10.0}, {"two", 20,20, 2.0f, 20.0}, {"three",30,30, 3.0f, 30.0}, {"four", 40,40, 4.0f, 40.0}, {"five", 50,50, 5.0f, 50.0}, {"six", 60,60, 6.0f, 60.0}, {"seven",70,70, 7.0f, 70.0} }; /* Define field information */ const char *field_names[NFIELDS] = { "Name","Latitude", "Longitude", "Pressure", "Temperature" }; hid_t field_type[NFIELDS]; hid_t string_type; hid_t file_id; hsize_t chunk_size = 10; int compress = 0; Particle fill_data[1] = { {"no data",-1,-1, -99.0f, -99.0} }; /* Fill value particle */ hsize_t start1; /* Record to start reading from 1st table */ hsize_t nrecords; /* Number of records to insert */ hsize_t start2; /* Record to start writing in 2nd table */ herr_t status; int i; hsize_t nfields_out; hsize_t nrecords_out; /* Initialize the field field_type */ string_type = H5Tcopy( H5T_C_S1 ); H5Tset_size( string_type, 16 ); field_type[0] = string_type; field_type[1] = H5T_NATIVE_INT; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_FLOAT; field_type[4] = H5T_NATIVE_DOUBLE; /* Create a new file using default properties. */ file_id = H5Fcreate( "ex_table_09.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /* Make 2 tables: TABLE2_NAME is empty */ status=H5TBmake_table( "Table Title",file_id,TABLE1_NAME,NFIELDS,NRECORDS, dst_size,field_names, dst_offset, field_type, chunk_size, fill_data, compress, p_data ); status=H5TBmake_table( "Table Title",file_id,TABLE2_NAME,NFIELDS,NRECORDS, dst_size,field_names, dst_offset, field_type, chunk_size, fill_data, compress, NULL ); /* Add 2 records from TABLE1_NAME to TABLE2_NAME */ start1 = 3; nrecords = NRECORDS_INS; start2 = 6; status=H5TBadd_records_from( file_id, TABLE1_NAME, start1, nrecords, TABLE2_NAME, start2 ); /* read TABLE2_NAME: it should have 2 more records now */ status=H5TBread_table( file_id, TABLE2_NAME, dst_size, dst_offset, dst_sizes, dst_buf ); /* Get table info */ status=H5TBget_table_info (file_id,TABLE2_NAME, &nfields_out, &nrecords_out ); /* print */ printf ("Table has %d fields and %d records\n",(int)nfields_out,(int)nrecords_out); /* print it by rows */ for (i=0; i<nrecords_out; i++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[i].name, dst_buf[i].lati, dst_buf[i].longi, dst_buf[i].pressure, dst_buf[i].temperature); printf ("\n"); } /* Close the file. */ H5Fclose( file_id ); return 0; }
int main( void ) { Particle dst_buf[NRECORDS]; /* Calculate the size and the offsets of our struct members in memory */ size_t dst_size = sizeof( Particle ); size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ), HOFFSET( Particle, lati ), HOFFSET( Particle, longi ), HOFFSET( Particle, pressure ), HOFFSET( Particle, temperature )}; Particle p = {"zero",0,0, 0.0f, 0.0}; size_t dst_sizes[NFIELDS] = { sizeof( p.name), sizeof( p.lati), sizeof( p.longi), sizeof( p.pressure), sizeof( p.temperature)}; /* Fill value particle */ Particle fill_data[1] = { {"no data",-1,-1, -99.0f, -99.0} }; hid_t field_type[NFIELDS]; hid_t string_type; hid_t file_id; hsize_t chunk_size = 10; hsize_t start; /* Record to start reading/writing */ hsize_t nrecords; /* Number of records to read/write */ herr_t status; int i; /* Define 2 new particles to write */ Particle particle_in[NRECORDS_WRITE] = { {"zero",0,0, 0.0f, 0.0}, {"one",10,10, 1.0f, 10.0} }; /* Initialize the field field_type */ string_type = H5Tcopy( H5T_C_S1 ); H5Tset_size( string_type, 16 ); field_type[0] = string_type; field_type[1] = H5T_NATIVE_INT; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_FLOAT; field_type[4] = H5T_NATIVE_DOUBLE; /* Create a new file using default properties. */ file_id = H5Fcreate( "h5_table_03.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /* Make the table */ status=H5TBmake_table( "Table Title", file_id, TABLE_NAME, NFIELDS, NRECORDS, dst_size, field_names, dst_offset, field_type, chunk_size, fill_data, 0, /* no compression */ NULL ); /* no data written */ /* Overwrite 2 records starting at record 0 */ start = 0; nrecords = NRECORDS_WRITE; status = H5TBwrite_records (file_id, TABLE_NAME, start, nrecords, dst_size, dst_offset, dst_sizes, particle_in); /* read the table */ status = H5TBread_table (file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf); /* print it by rows */ for (i=0; i<NRECORDS; i++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[i].name, dst_buf[i].lati, dst_buf[i].longi, dst_buf[i].pressure, dst_buf[i].temperature); printf ("\n"); } /* close type */ H5Tclose( string_type ); /* close the file */ H5Fclose( file_id ); return 0; }
IAS_RLUT_LINEARIZATION_PARAMS *ias_rlut_read_linearization_params ( const IAS_RLUT_IO *rlut, /* I: Open RLUT file */ int band_number, /* I: Current RLUT band number */ int sca_number, /* I: Current SCA number */ int num_detectors /* I: Number of detectors in the current band/SCA */ ) { IAS_RLUT_LINEARIZATION_PARAMS *linearization_params = NULL; /* Pointer to an array of data structures containing the linearization parameters for all detectors in the current band/SCA */ char bandsca_parameter_name[IAS_RLUT_BANDSCA_GROUP_NAME_LENGTH + 1]; /* Linearization parameter group name for the current band/SCA */ const char *field_names[IAS_RLUT_PARAM_NFIELDS]; /* Name of each linearization parameter */ size_t offsets[IAS_RLUT_PARAM_NFIELDS]; /* Data offsets in LINEARIZATION_PARAMS data structure for each field*/ size_t field_sizes[IAS_RLUT_PARAM_NFIELDS];/* Size of each field */ hid_t field_types[IAS_RLUT_PARAM_NFIELDS]; /* Data type for each field */ hid_t fields_to_close[IAS_RLUT_PARAM_NFIELDS]; /* Flags indicating open fields needing to be closed */ hid_t linearization_param_group_id; /* Root LINEARIZATION_PARAMETERS group */ hid_t bandsca_group_id; /* SCA group handle */ hsize_t nfields = 0; /* Number of fields in the table description */ hsize_t nrecords = 0; /* Number of records in the table description (should equal the number of detectors) */ hsize_t type_size; /* Size of entire data structure to be read into */ herr_t hdf_status; /* HDF5 error status flag */ int status; /* IAS status */ /* Make sure the RLUT file is actually open */ if ((rlut == NULL) || (rlut->file_id < 0)) { IAS_LOG_ERROR("NULL pointer to IAS_RLUT_IO data block, or no RLUT " "file has been opened"); return NULL; } /* Construct the group name for the current band/SCA */ status = snprintf(bandsca_parameter_name, sizeof(bandsca_parameter_name), "%s/Band%02d_SCA%02d", LINEARIZATION_PARAMS_GROUP_NAME, band_number, sca_number); if ((status < 0) || (status >= sizeof(bandsca_parameter_name))) { IAS_LOG_ERROR("Creating group name for band %d SCA %d " "linearization parameters", band_number, sca_number); return NULL; } /* Open the root group*/ linearization_param_group_id = H5Gopen(rlut->file_id, LINEARIZATION_PARAMS_GROUP_NAME, H5P_DEFAULT); if (linearization_param_group_id < 0) { IAS_LOG_ERROR("Opening root linearization parameters group"); return NULL; } /* Try to open the group for the current band/SCA */ bandsca_group_id = H5Gopen(linearization_param_group_id, bandsca_parameter_name, H5P_DEFAULT); if (bandsca_group_id < 0) { IAS_LOG_ERROR("Opening band %d SCA %d linearization parameter group", band_number, sca_number); H5Gclose(linearization_param_group_id); return NULL; } /* Build the table definition */ status = ias_rlut_build_linearization_params_table_description(offsets, field_names, field_types, fields_to_close, field_sizes); if (status != SUCCESS) { IAS_LOG_ERROR("Building linearization parameter table description"); H5Gclose(bandsca_group_id); H5Gclose(linearization_param_group_id); return NULL; } /* Get the number of fields and records */ hdf_status = H5TBget_table_info(bandsca_group_id, LINEARIZATION_PARAMS_DATASET_NAME, &nfields, &nrecords); if (hdf_status < 0) { IAS_LOG_ERROR("Getting parameter table information for band %d SCA " "%d", band_number, sca_number); ias_rlut_cleanup_table_description(fields_to_close, IAS_RLUT_PARAM_NFIELDS); H5Gclose(bandsca_group_id); H5Gclose(linearization_param_group_id); return NULL; } else if (nfields != IAS_RLUT_PARAM_NFIELDS) { IAS_LOG_ERROR("Number of defined fields %d not equal to number of " "returned fields %d", IAS_RLUT_PARAM_NFIELDS, (int)nfields); ias_rlut_cleanup_table_description(fields_to_close, IAS_RLUT_PARAM_NFIELDS); H5Gclose(bandsca_group_id); H5Gclose(linearization_param_group_id); return NULL; } else if (nrecords != num_detectors) { IAS_LOG_ERROR("Band %d SCA %d parameter table should have %d " "records, found %d records instead", band_number, sca_number, num_detectors, (int)nrecords); ias_rlut_cleanup_table_description(fields_to_close, IAS_RLUT_PARAM_NFIELDS); H5Gclose(bandsca_group_id); H5Gclose(linearization_param_group_id); return NULL; } /* Allocate the parameter data buffer for the current band/SCA */ linearization_params = malloc( num_detectors * sizeof(IAS_RLUT_LINEARIZATION_PARAMS)); if (linearization_params == NULL) { IAS_LOG_ERROR("Allocating linearization parameter data buffer for " "band %d SCA %d", band_number, sca_number); ias_rlut_cleanup_table_description(fields_to_close, IAS_RLUT_PARAM_NFIELDS); H5Gclose(bandsca_group_id); H5Gclose(linearization_param_group_id); return NULL; } /* Read the parameter set for the current band/SCA */ type_size = sizeof(*linearization_params); hdf_status = H5TBread_table(bandsca_group_id, LINEARIZATION_PARAMS_DATASET_NAME, type_size, offsets, field_sizes, linearization_params); /* Cleanup the table description */ ias_rlut_cleanup_table_description(fields_to_close, IAS_RLUT_PARAM_NFIELDS); /* Check the return status from the read */ if (hdf_status < 0) { IAS_LOG_ERROR("Reading parameters for band %d SCA %d", band_number, sca_number); free(linearization_params); linearization_params = NULL; } /* Close the group for the current band/SCA */ hdf_status = H5Gclose(bandsca_group_id); if (hdf_status < 0) { IAS_LOG_ERROR("Closing band %d SCA %d linearization parameter group", band_number, sca_number); if (linearization_params) { free(linearization_params); linearization_params = NULL; } } /* Close the main linearization parameter group */ hdf_status = H5Gclose(linearization_param_group_id); if (hdf_status < 0) { IAS_LOG_ERROR("Closing root linearization parameters group"); if (linearization_params) { free(linearization_params); linearization_params = NULL; } } return linearization_params; } /* END ias_rlut_read_linearization_params */
int main( void ) { typedef struct Particle { char name[16]; int lati; int longi; float pressure; double temperature; } Particle; Particle dst_buf[NRECORDS+NRECORDS_ADD]; /* Define an array of Particles */ Particle p_data[NRECORDS] = { {"zero",0,0, 0.0f, 0.0}, {"one",10,10, 1.0f, 10.0}, {"two", 20,20, 2.0f, 20.0}, {"three",30,30, 3.0f, 30.0}, {"four", 40,40, 4.0f, 40.0}, {"five", 50,50, 5.0f, 50.0}, {"six", 60,60, 6.0f, 60.0}, {"seven",70,70, 7.0f, 70.0} }; /* Calculate the size and the offsets of our struct members in memory */ size_t dst_size = sizeof( Particle ); size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ), HOFFSET( Particle, lati ), HOFFSET( Particle, longi ), HOFFSET( Particle, pressure ), HOFFSET( Particle, temperature )}; size_t dst_sizes[NFIELDS] = { sizeof( p_data[0].name), sizeof( p_data[0].lati), sizeof( p_data[0].longi), sizeof( p_data[0].pressure), sizeof( p_data[0].temperature)}; /* Define field information */ const char *field_names[NFIELDS] = { "Name","Latitude", "Longitude", "Pressure", "Temperature" }; hid_t field_type[NFIELDS]; hid_t string_type; hid_t file_id; hsize_t chunk_size = 10; int *fill_data = NULL; int compress = 0; herr_t status; int i; /* Append particles */ Particle particle_in[ NRECORDS_ADD ] = {{ "eight",80,80, 8.0f, 80.0}, {"nine",90,90, 9.0f, 90.0} }; /* Initialize the field field_type */ string_type = H5Tcopy( H5T_C_S1 ); H5Tset_size( string_type, 16 ); field_type[0] = string_type; field_type[1] = H5T_NATIVE_INT; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_FLOAT; field_type[4] = H5T_NATIVE_DOUBLE; /* Create a new file using default properties. */ file_id = H5Fcreate( "ex_table_02.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /* make a table */ status=H5TBmake_table( "Table Title",file_id,TABLE_NAME,NFIELDS,NRECORDS, dst_size, field_names, dst_offset, field_type, chunk_size, fill_data, compress, p_data ); /* append two records */ status=H5TBappend_records(file_id, TABLE_NAME,NRECORDS_ADD, dst_size, dst_offset, dst_sizes, &particle_in ); /* read the table */ status=H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf ); /* print it by rows */ for (i=0; i<NRECORDS+NRECORDS_ADD; i++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[i].name, dst_buf[i].lati, dst_buf[i].longi, dst_buf[i].pressure, dst_buf[i].temperature); printf ("\n"); } /* Close the file. */ H5Fclose( file_id ); return 0; }
int main( void ) { typedef struct Particle { char name[16]; int lati; int longi; float pressure; double temperature; } Particle; /* Define an array of Particles */ Particle p_data[NRECORDS] = { {"zero",0,0, 0.0f, 0.0}, {"one",10,10, 1.0f, 10.0}, {"two", 20,20, 2.0f, 20.0}, {"three",30,30, 3.0f, 30.0}, {"four", 40,40, 4.0f, 40.0}, {"five", 50,50, 5.0f, 50.0}, {"six", 60,60, 6.0f, 60.0}, {"seven",70,70, 7.0f, 70.0} }; Particle dst_buf[ 2 * NRECORDS ]; /* Calculate the size and the offsets of our struct members in memory */ size_t dst_size = sizeof( Particle ); size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ), HOFFSET( Particle, lati ), HOFFSET( Particle, longi ), HOFFSET( Particle, pressure ), HOFFSET( Particle, temperature )}; size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name), sizeof( dst_buf[0].lati), sizeof( dst_buf[0].longi), sizeof( dst_buf[0].pressure), sizeof( dst_buf[0].temperature)}; /* Define field information */ const char *field_names[NFIELDS] = { "Name","Latitude", "Longitude", "Pressure", "Temperature" }; hid_t field_type[NFIELDS]; hid_t string_type; hid_t file_id; hsize_t chunk_size = 10; int compress = 0; int *fill_data = NULL; herr_t status; hsize_t nfields_out; hsize_t nrecords_out; int i; /* Initialize the field field_type */ string_type = H5Tcopy( H5T_C_S1 ); H5Tset_size( string_type, 16 ); field_type[0] = string_type; field_type[1] = H5T_NATIVE_INT; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_FLOAT; field_type[4] = H5T_NATIVE_DOUBLE; /* Create a new file using default properties. */ file_id = H5Fcreate( "ex_table_10.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /* Make two tables */ status=H5TBmake_table( "Table Title",file_id,TABLE1_NAME,NFIELDS,NRECORDS, dst_size,field_names, dst_offset, field_type, chunk_size, fill_data, compress, p_data ); status=H5TBmake_table( "Table Title",file_id,TABLE2_NAME,NFIELDS,NRECORDS, dst_size,field_names, dst_offset, field_type, chunk_size, fill_data, compress, p_data ); /* Combine the two tables into a third in the same file */ status=H5TBcombine_tables( file_id, TABLE1_NAME, file_id, TABLE2_NAME, TABLE3_NAME ); /* read the combined table */ status=H5TBread_table( file_id, TABLE3_NAME, dst_size, dst_offset, dst_sizes, dst_buf ); /* Get table info */ status=H5TBget_table_info (file_id,TABLE3_NAME, &nfields_out, &nrecords_out ); /* print */ printf ("Table has %d fields and %d records\n",(int)nfields_out,(int)nrecords_out); /* print it by rows */ for (i=0; i<nrecords_out; i++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[i].name, dst_buf[i].lati, dst_buf[i].longi, dst_buf[i].pressure, dst_buf[i].temperature); printf ("\n"); } /* close type */ H5Tclose( string_type ); /* close the file */ H5Fclose( file_id ); return 0; }
void UPCdata::readHDFfile(char* filename, int count){ // count: 11040 for segmentation and 14640 for classification #if HDF5_AVAILABLE==2 // based on http://www.hdfgroup.org/HDF5/doc/cpplus_RM/readdata_8cpp-example.html // maybe first deleting all data? does it do that automatically or is there some possible memory leak? // I need to know first the data set sizes... so I create one dummy sample... this is only a hack samples.resize(0); /* Sample dummy; dummy.data.resize(1020,0); dummy.concentrations[0]=1;dummy.concentrations[1]=1;dummy.concentrations[2]=1; dummy.samplenr=1; dummy.set=Sample::Training; dummy.compoundnr=1; */ const int NFIELDS = 5; hsize_t chunk_size = 10; // ?? typedef struct _samplestruct{ int concentrations[3]; int samplenr; int set; int compoundnr; float data[1020]; // having difficulties here; dynamic allocation? }samplestruct; samplestruct *sampledata; void *sampleptr = malloc(NRECORDS*sizeof(samplestruct)); sampledata = (samplestruct*)sampleptr; if (sampleptr == NULL) { // Memory could not be allocated, the program should handle the error here as appropriate. cout << "memory could not be allocated!"; return; } // ... // free(samplestruct); /* size_t dst_size = sizeof(Sample); size_t dst_offset[NFIELDS] = { HOFFSET(Sample, concentrations), HOFFSET(Sample, samplenr), HOFFSET(Sample, set), HOFFSET(Sample, compoundnr), HOFFSET(Sample, data) }; size_t dst_sizes[NFIELDS] = { sizeof( samples[0].data), sizeof(samples[0].concentrations), sizeof(samples[0].samplenr), sizeof(samples[0].set), sizeof(samples[0].compoundnr)}; */ size_t dst_size = sizeof(samplestruct); size_t dst_offset[NFIELDS] = {HOFFSET(samplestruct, concentrations), HOFFSET(samplestruct, samplenr), HOFFSET(samplestruct, set), HOFFSET(samplestruct, compoundnr), HOFFSET(samplestruct, data), }; size_t dst_sizes[NFIELDS] = { sizeof( sampledata[0].data), sizeof(sampledata[0].concentrations), sizeof(sampledata[0].samplenr), sizeof(sampledata[0].set), sizeof(sampledata[0].compoundnr) }; hid_t file_id; int *fill_data = NULL; int compress = 1; herr_t status; //const hsize_t datadim[] = {samples[0].data.size()}; const hsize_t datadim[] = {1020*sizeof(float)}; const hsize_t concdim[] = {3}; hid_t floatarray = H5Tarray_create(H5T_NATIVE_FLOAT, 1, datadim); hid_t concarray = H5Tarray_create(H5T_NATIVE_FLOAT, 1, concdim); // Initialize field_type hid_t field_type[NFIELDS]; field_type[0] = floatarray; field_type[1] = concarray; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_INT; field_type[4] = H5T_NATIVE_INT; // opening file file_id=H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT); if ( file_id < 0 ){ std::cout << "UPCdata::readHDFfile(): Error opening " << filename <<": " << file_id << std::endl; return; } //const int NRECORDS=samples.size(); // how to get the data set size? // is this the way? hid_t dataset_id=H5Dopen1(file_id, "Dataset"); hsize_t size = H5Dget_storage_size(dataset_id); //vector<Sample> buf(NRECORDS+1); //vector<Sample> buf(static_cast<int>(size+1), 0x00); status=H5TBread_table(file_id, "Dataset", dst_size, dst_offset, dst_sizes, sampledata); cout << "converting data format" << endl; for(int i=0;i<NRECORDS;i++){ Sample dummysample; dummysample.concentrations[0]=sampledata[i].concentrations[0]; dummysample.concentrations[1]=sampledata[i].concentrations[1]; dummysample.concentrations[2]=sampledata[i].concentrations[2]; dummysample.samplenr=sampledata[i].samplenr; dummysample.set=(Sample::SetType)sampledata[i].set; dummysample.compoundnr=sampledata[i].compoundnr; dummysample.data.resize(1020); for(int j=0;j<1020;j++) dummysample.data[j]=sampledata[i].data[j]; samples.push_back(dummysample); } // close type(s)?? // close the file / H5Fclose( file_id ); #endif }
int main( void ) { Particle dst_buf[NRECORDS]; /* Calculate the size and the offsets of our struct members in memory */ size_t dst_size = sizeof( Particle ); size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ), HOFFSET( Particle, lati ), HOFFSET( Particle, longi ), HOFFSET( Particle, pressure ), HOFFSET( Particle, temperature )}; size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name), sizeof( dst_buf[0].lati), sizeof( dst_buf[0].longi), sizeof( dst_buf[0].pressure), sizeof( dst_buf[0].temperature)}; size_t field_offset_pos[2] = { HOFFSET( Position, lati ), HOFFSET( Position, longi )}; hid_t field_type[NFIELDS]; hid_t string_type; hid_t fileID; hsize_t chunk_size = 10; Particle fill_data[1] = { {"no data",-1,-1, -99.0f, -99.0} }; /* Fill value particle */ hsize_t start; /* Record to start reading/writing */ hsize_t nrecords; /* Number of records to read/write */ int compress = 0; int n; herr_t status; Particle *p_data = NULL; /* Initially no data */ float pressure_in [NRECORDS_ADD] = /* Define new values for the field "Pressure" */ { 0.0f,1.0f,2.0f}; Position position_in[NRECORDS_ADD] = {/* Define new values for "Latitude,Longitude" */ {0,0}, {10,10}, {20,20}}; NamePressure namepre_in[NRECORDS_ADD] =/* Define new values for "Name,Pressure" */ { {"zero",0.0f}, {"one", 1.0f}, {"two", 2.0f}, }; size_t field_sizes_pos[2]= { sizeof(position_in[0].longi), sizeof(position_in[0].lati) }; size_t field_sizes_pre[1]= { sizeof(namepre_in[0].pressure) }; /* Initialize the field field_type */ string_type = H5Tcopy( H5T_C_S1 ); H5Tset_size( string_type, 16 ); field_type[0] = string_type; field_type[1] = H5T_NATIVE_INT; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_FLOAT; field_type[4] = H5T_NATIVE_DOUBLE; /*__________________________________________________________________ Create a new file using default properties. */ fileID = H5Fcreate ("h5_table_04.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT); /*__________________________________________________________________ Make the table */ status = H5TBmake_table ("Table Title", fileID, TABLE_NAME, NFIELDS, NRECORDS, dst_size, field_names, dst_offset, field_type, chunk_size, fill_data, compress, p_data); /*__________________________________________________________________ Write the pressure field starting at record 2. */ start = 2; nrecords = NRECORDS_ADD; status = H5TBwrite_fields_name (fileID, TABLE_NAME, "Pressure", start, nrecords, sizeof( float ), 0, field_sizes_pre, pressure_in); /*__________________________________________________________________ Write the new longitude and latitude information starting at record 2. */ start = 2; nrecords = NRECORDS_ADD; status = H5TBwrite_fields_name (fileID, TABLE_NAME, "Latitude,Longitude", start, nrecords, sizeof( Position ), field_offset_pos, field_sizes_pos, position_in); /*__________________________________________________________________ Read the table */ status = H5TBread_table (fileID, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf); /* print it by rows */ for (n=0; n<NRECORDS; n++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[n].name, dst_buf[n].lati, dst_buf[n].longi, dst_buf[n].pressure, dst_buf[n].temperature); printf ("\n"); } /*------------------------------------------------------------------------- * end *------------------------------------------------------------------------- */ /* close type */ H5Tclose( string_type ); /* close the file */ H5Fclose (fileID); return 0; }
/*---------------------------------------------------------------------- NAME: read_ephemeris_data PURPOSE: Reads ephemeris data from HDF5-formatted tables in the ancillary data file RETURNS: Pointer to an allocated/populated IAS_ANC_EPHEMERIS_DATA structure if successful, NULL pointer if allocation fails or data read fails ------------------------------------------------------------------------*/ static IAS_ANC_EPHEMERIS_DATA *read_ephemeris_data ( hid_t hdf_file_id, /* I: HDF5 ancillary data file handle */ int file_format_version /* I: current file format version */ ) { const char *field_names[EPHEMERIS_NFIELDS]; double epoch_time[3] = {0.0, 0.0, 0.0}; /* Temporary buffer for epoch time data */ int status = SUCCESS; /* Function return status code */ size_t field_offsets[EPHEMERIS_NFIELDS]; size_t field_sizes[EPHEMERIS_NFIELDS]; herr_t hdf_error_status = -1; /* HDF5 I/O error status */ hid_t field_types[EPHEMERIS_NFIELDS]; hid_t fields_to_close[EPHEMERIS_NFIELDS]; hsize_t nfields = 0; /* Number of table fields per record */ hsize_t nrecords = 0; /* Number of records in table */ hsize_t type_size = 0; /* Size of data structure to read */ IAS_ANC_EPHEMERIS_DATA *data = NULL; /* Pointer to attitude data buffer */ /* Read the attitude epoch time from the ancillary data file. */ status = read_epoch_time(hdf_file_id, EPHEMERIS_EPOCH_TIME_ATTRIBUTE_NAME, epoch_time); if (status != SUCCESS) { IAS_LOG_ERROR("Reading ephemeris epoch time attribute"); return NULL; } /* Build the ephemeris table definition. */ status = ias_ancillary_build_ephemeris_table_definition(field_names, field_offsets, field_types, field_sizes, fields_to_close); if (status != SUCCESS) { IAS_LOG_ERROR("Building ephemeris table definition"); return NULL; } /* Get the number of records in the ephemeris data table. We need this before we can allocate the proper-sized IAS_ANC_EPHEMERIS_DATA buffer. If the table doesn't exist or there's a table defined with 0 records, consider it an error. */ hdf_error_status = H5TBget_table_info(hdf_file_id, EPHEMERIS_DATA_DATASET_NAME, &nfields, &nrecords); if (hdf_error_status < 0) { IAS_LOG_ERROR("Obtaining ephemeris table information"); ias_ancillary_cleanup_table_definition(fields_to_close, EPHEMERIS_NFIELDS); return NULL; } else if (nrecords < 1) { IAS_LOG_ERROR("No records found in ephemeris data table"); ias_ancillary_cleanup_table_definition(fields_to_close, EPHEMERIS_NFIELDS); return NULL; } /* Allocate the ephemeris data buffer. */ data = ias_ancillary_allocate_ephemeris(nrecords); if (data == NULL) { IAS_LOG_ERROR("Allocating ephemeris data buffer"); ias_ancillary_cleanup_table_definition(fields_to_close, EPHEMERIS_NFIELDS); return NULL; } else { /* Copy the attitude epoch time info to the data structure. */ memcpy(data->utc_epoch_time, epoch_time, sizeof(epoch_time)); /* Read the table contents into the data structure. */ type_size = sizeof(data->records); hdf_error_status = H5TBread_table(hdf_file_id, EPHEMERIS_DATA_DATASET_NAME, type_size, field_offsets, field_sizes, data->records); if (hdf_error_status < 0) { IAS_LOG_ERROR("Reading ephemeris data table"); ias_ancillary_free_ephemeris(data); ias_ancillary_cleanup_table_definition(fields_to_close, EPHEMERIS_NFIELDS); return NULL; } } /* Close any "open" datatype field objects. */ ias_ancillary_cleanup_table_definition(fields_to_close, EPHEMERIS_NFIELDS); /* Done */ return data; }
int main( void ) { typedef struct Particle { char name[16]; int lati; int longi; float pressure; double temperature; } Particle; /* Define a subset of Particle, with latitude and longitude fields */ typedef struct Position { int lati; int longi; } Position; /* Define a subset of Particle, with name and pressure fields */ typedef struct NamePressure { char name[16]; float pressure; } NamePressure; /* Calculate the type_size and the offsets of our struct members */ Particle dst_buf[NRECORDS]; size_t dst_size = sizeof( Particle ); size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ), HOFFSET( Particle, lati ), HOFFSET( Particle, longi ), HOFFSET( Particle, pressure ), HOFFSET( Particle, temperature )}; size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name), sizeof( dst_buf[0].lati), sizeof( dst_buf[0].longi), sizeof( dst_buf[0].pressure), sizeof( dst_buf[0].temperature)}; size_t field_offset_pos[2] = { HOFFSET( Position, lati ), HOFFSET( Position, longi )}; /* Initially no data */ Particle *p_data = NULL; /* Define field information */ const char *field_names[NFIELDS] = { "Name","Latitude", "Longitude", "Pressure", "Temperature" }; hid_t field_type[NFIELDS]; hid_t string_type; hid_t file_id; hsize_t chunk_size = 10; Particle fill_data[1] = { {"no data",-1,-1, -99.0f, -99.0} }; /* Fill value particle */ int compress = 0; hsize_t nfields; hsize_t start; /* Record to start reading/writing */ hsize_t nrecords; /* Number of records to read/write */ herr_t status; int i; /* Define new values for the field "Pressure" */ float pressure_in [NRECORDS_ADD] = { 0.0f,1.0f,2.0f}; int field_index_pre[1] = { 3 }; int field_index_pos[2] = { 1,2 }; /* Define new values for the fields "Latitude,Longitude" */ Position position_in[NRECORDS_ADD] = { {0,0}, {10,10}, {20,20} }; size_t field_sizes_pos[2]= { sizeof(position_in[0].longi), sizeof(position_in[0].lati) }; size_t field_sizes_pre[1]= { sizeof(float) }; /* Initialize the field field_type */ string_type = H5Tcopy( H5T_C_S1 ); H5Tset_size( string_type, 16 ); field_type[0] = string_type; field_type[1] = H5T_NATIVE_INT; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_FLOAT; field_type[4] = H5T_NATIVE_DOUBLE; /* Create a new file using default properties. */ file_id = H5Fcreate( "ex_table_05.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /* Make the table */ status=H5TBmake_table( "Table Title", file_id, TABLE_NAME,NFIELDS,NRECORDS, dst_size,field_names, dst_offset, field_type, chunk_size, fill_data, compress, p_data ); /* Write the pressure field starting at record 2 */ nfields = 1; start = 2; nrecords = NRECORDS_ADD; status=H5TBwrite_fields_index( file_id, TABLE_NAME, nfields, field_index_pre, start, nrecords, sizeof( float ), 0, field_sizes_pre, pressure_in ); /* Write the new longitude and latitude information starting at record 2 */ nfields = 2; start = 2; nrecords = NRECORDS_ADD; status=H5TBwrite_fields_index( file_id, TABLE_NAME, nfields, field_index_pos, start, nrecords, sizeof( Position ), field_offset_pos, field_sizes_pos, position_in ); /* read the table */ status=H5TBread_table( file_id, TABLE_NAME, dst_size, dst_offset, dst_sizes, dst_buf ); /* print it by rows */ for (i=0; i<NRECORDS; i++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[i].name, dst_buf[i].lati, dst_buf[i].longi, dst_buf[i].pressure, dst_buf[i].temperature); printf ("\n"); } /* close type */ H5Tclose( string_type ); /* close the file */ H5Fclose( file_id ); return 0; }
int main( void ) { typedef struct Particle { char name[16]; int lati; int longi; float pressure; double temperature; } Particle; Particle dst_buf[NRECORDS]; /* Calculate the size and the offsets of our struct members in memory */ size_t dst_size = sizeof( Particle ); size_t dst_offset[NFIELDS] = { HOFFSET( Particle, name ), HOFFSET( Particle, lati ), HOFFSET( Particle, longi ), HOFFSET( Particle, pressure ), HOFFSET( Particle, temperature )}; size_t dst_sizes[NFIELDS] = { sizeof( dst_buf[0].name), sizeof( dst_buf[0].lati), sizeof( dst_buf[0].longi), sizeof( dst_buf[0].pressure), sizeof( dst_buf[0].temperature)}; /* Define an array of Particles */ Particle p_data[NRECORDS] = { {"zero",0,0, 0.0f, 0.0}, {"one",10,10, 1.0f, 10.0}, {"two", 20,20, 2.0f, 20.0}, {"three",30,30, 3.0f, 30.0}, {"four", 40,40, 4.0f, 40.0}, {"five", 50,50, 5.0f, 50.0}, {"six", 60,60, 6.0f, 60.0}, {"seven",70,70, 7.0f, 70.0} }; /* Define field information */ const char *field_names[NFIELDS] = { "Name","Latitude", "Longitude", "Pressure", "Temperature" }; hid_t field_type[NFIELDS]; hid_t string_type; hid_t file_id; hsize_t chunk_size = 10; int *fill_data = NULL; int compress = 0; herr_t status; int i; /* Initialize field_type */ string_type = H5Tcopy( H5T_C_S1 ); H5Tset_size( string_type, 16 ); field_type[0] = string_type; field_type[1] = H5T_NATIVE_INT; field_type[2] = H5T_NATIVE_INT; field_type[3] = H5T_NATIVE_FLOAT; field_type[4] = H5T_NATIVE_DOUBLE; /* Create a new file using default properties. */ file_id = H5Fcreate( "h5_table_01.h5", H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT ); /*------------------------------------------------------------------------- * H5TBmake_table *------------------------------------------------------------------------- */ status=H5TBmake_table ("Table Title", /* Title of the table */ file_id, /* HDF5 object identifier to the file */ TABLE_NAME, /* Name of the table */ NFIELDS, /* Number of fields/columns */ NRECORDS, /* */ dst_size, /* Size of the struct/table row */ field_names, /* */ dst_offset, /* */ field_type, /* Type of the individual table fields */ chunk_size, /* Chunking size */ fill_data, /* */ compress, /* Enable/disable compression */ p_data /* Array with the table data */ ); /*------------------------------------------------------------------------- * H5TBread_table *------------------------------------------------------------------------- */ status = H5TBread_table (file_id, /* HDF5 object identifier */ TABLE_NAME, /* Name of the table */ dst_size, /* */ dst_offset, /* */ dst_sizes, /* */ dst_buf); /* Buffer to return the data */ /* print it by rows */ for (i=0; i<NRECORDS; i++) { printf ("%-5s %-5d %-5d %-5f %-5f", dst_buf[i].name, dst_buf[i].lati, dst_buf[i].longi, dst_buf[i].pressure, dst_buf[i].temperature); printf ("\n"); } /*------------------------------------------------------------------------- * end *------------------------------------------------------------------------- */ /* Release object identifiers */ H5Tclose (string_type); H5Fclose (file_id); return 0; }