Skip to content
Snippets Groups Projects
Commit 8b3f1b30 authored by Brown's avatar Brown
Browse files

move hdf5 data writing to hdf5 class

parent 7db2e07a
No related branches found
No related tags found
No related merge requests found
......@@ -3,12 +3,13 @@ INCLUDE(SammyPackageSetup)
TRIBITS_PACKAGE(io)
find_package(HDF5 REQUIRED COMPONENTS C CXX HL)
find_library( h5lib HDF5 )
find_library( h5lib_CPP HDF5_CPP )
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../sammy_conf.h.in sammy_conf.h @ONLY)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR}
${HDF5_INCLUDE_DIR})
${HDF5_INCLUDE_DIRS}) # from find_package()
# Set headers
SET(HEADERS ExperimentalParametersIO.h
......@@ -41,6 +42,7 @@ TRIBITS_ADD_TEST_DIRECTORIES(tests)
TRIBITS_PACKAGE_POSTPROCESS()
target_link_libraries(SammyIOUtilsLib ${h5lib} ${h5lib_CPP})
# --------------------------------------------
# Build separate executable to convert ODF
......@@ -48,7 +50,6 @@ TRIBITS_PACKAGE_POSTPROCESS()
# --------------------------------------------
add_executable(readodf readodf.cpp)
target_link_libraries(readodf PRIVATE HDF5::HDF5
SammyIOUtilsLib)
target_link_libraries(readodf SammyIOUtilsLib)
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/readodf DESTINATION "bin/")
#include "hdf5IO.h"
#include "H5Cpp.h"
#define MAX_NAME_LEN 32
namespace sammy {
hdf5IO::hdf5IO() {}
void hdf5IO::writeODFtoHDF5(){
std::cout << "hello world" << std::endl;
void hdf5IO::writeODFtoHDF5(odfHeader &head,
std::vector<std::vector<double>> &data,
std::string hdf5Filename,
bool elevenSect){
if( !elevenSect ){
std::cerr << "---------------------------------------" << std::endl;
std::cerr << "Reading of ODF files with < 11 sections"
<< "has not been implemented yet. " << std::endl;
std::cerr << "---------------------------------------" << std::endl;
return;
}
// HDF5 works best with set-size char arrays
char sect11[11][MAX_NAME_LEN] = {"energy","trans","dtrans",
"cro","dcro","cs","zero","co","zero","idk","dcs"};
char sect9[9][MAX_NAME_LEN] = {};
// HDF5 accepts set-size double arrays for datasets: copy over
double dset_data[data.size()][data[0].size()];
for( size_t i=0;i<data.size();++i ){
std::copy(data[i].begin(),data[i].end(), dset_data[i]);
}
// write 2 datasets: data and data section names
// data space
hsize_t ddim[2];
ddim[0] = head.numSection;
ddim[1] = head.numChan;
int drank = sizeof(ddim) / sizeof(hsize_t);
std::string dataname("/data/sections");
// section names space
hsize_t sdim[1];
sdim[0] = head.numSection;
int srank = sizeof(sdim) / sizeof(hsize_t);
std::string sectionnames("/data/sectnames");
// open file and write
H5::DataSpace dspace(drank,ddim);
H5::DataSpace sspace(srank,sdim);
H5::H5File *file = new H5::H5File(hdf5Filename,H5F_ACC_TRUNC);
H5::Group *group = new H5::Group( file->createGroup( "/data" ));
H5::DataSet *dataset = new H5::DataSet(file->createDataSet(dataname,H5::PredType::NATIVE_DOUBLE,dspace));
H5::DataSet *nameset = new H5::DataSet(file->createDataSet(sectionnames,H5::StrType(H5::PredType::C_S1, MAX_NAME_LEN),sspace));
dataset->write( dset_data, H5::PredType::NATIVE_DOUBLE );
nameset->write( sect11, H5::StrType(H5::PredType::C_S1, MAX_NAME_LEN) );
// sweep, sweep
delete file;
delete dataset;
delete nameset;
}
}
\ No newline at end of file
......@@ -4,7 +4,8 @@
#include <vector>
#include <fstream> // ifstream
#include <iostream>
#include "hdf5.h"
#include "odfIO.h"
#include "H5Cpp.h"
namespace sammy{
......@@ -17,8 +18,10 @@ namespace sammy{
/****************************************
* Write data from ODF file into HDF5
***************************************/
void writeODFtoHDF5();
void writeODFtoHDF5(sammy::odfHeader &head,
std::vector<std::vector<double>> &data,
std::string hdf5Filename,
bool elevenSect);
};
}
......
......@@ -142,7 +142,6 @@ namespace sammy{
in.read( (char*)&head.strt, intsize ); // starting word number of each dataset in data section (mode 0 only)
in.read( (char*)&head.ndwend, intsize ); // ending word number of last word written in block NDBEND
/* the following doesn't work properly yet */
// ------------------------------------
// --- Read crunch table
......@@ -159,6 +158,8 @@ namespace sammy{
comments.resize( bytes_in_comments );
in.seekg(bytes_to_comments,std::ios::beg);
in.ignore(bytes_per_word);
for( int i=0;i<bytes_in_comments;++i ){
in.read( (char*)&comments[i], 1 );
comments[i] = comments[i];
......
......@@ -2,6 +2,7 @@
// #include "hdf5.h"
#include "H5Cpp.h"
#include "odfIO.h"
#include "hdf5IO.h"
#define MAX_NAME_LEN 32
......@@ -48,116 +49,9 @@ int main(int argc, char const *argv[])
// -------------------------------------------------------------------------
// --- Output HDF5 file
// -------------------------------------------------------------------------
char sectnames[11][MAX_NAME_LEN] = {"energy","trans","dtrans",
"cro","dcro","cs","zero","co","zero","idk","dcs"};
// make dataset
double dset_data[data.size()][data[0].size()];
for( size_t i=0;i<data.size();++i ){
std::copy(data[i].begin(),data[i].end(), dset_data[i]);
}
// write 2 datasets: data and data section names
// data space
hsize_t ddim[2];
ddim[0] = head.numSection;
ddim[1] = head.numChan;
int drank = sizeof(ddim) / sizeof(hsize_t);
std::string dataname("/data/sections");
// section names space
hsize_t sdim[1];
sdim[0] = head.numSection;
int srank = sizeof(sdim) / sizeof(hsize_t);
std::string sectionnames("/data/sectnames");
// open file and write
H5::DataSpace dspace(drank,ddim);
H5::DataSpace sspace(srank,sdim);
H5::H5File *file = new H5::H5File(hdf5Filename,H5F_ACC_TRUNC);
H5::Group *group = new H5::Group( file->createGroup( "/data" ));
H5::DataSet *dataset = new H5::DataSet(file->createDataSet(dataname,H5::PredType::NATIVE_DOUBLE,dspace));
H5::DataSet *nameset = new H5::DataSet(file->createDataSet(sectionnames,H5::StrType(H5::PredType::C_S1, MAX_NAME_LEN),sspace));
dataset->write(dset_data,H5::PredType::NATIVE_DOUBLE);
nameset->write(sectnames,H5::StrType(H5::PredType::C_S1, MAX_NAME_LEN));
delete file;
delete dataset;
delete nameset;
/* old
// --- setup with HDF types ---
hid_t file_id, dataspace_id, dataset_id, group_id, name_dataspace_id;
hid_t name_dataset_id;
herr_t status;
hsize_t dims[2];
//energy,trans,dtrans,cro,dcro,cs,zero,co,zero,idk,dcs
dims[0] = data.size();
dims[1] = data[0].size();
dataspace_id = H5Screate_simple(2, dims, NULL);
// --- open file ---
file_id = H5Fcreate(hdf5Filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
// --- make a dataset group ---
group_id = H5Gcreate2(file_id,"data",H5P_DEFAULT,H5P_DEFAULT,H5P_DEFAULT);
// --- create dataset ---
// H5T_IEEE_F64LE is an HDF5 predefined datatype for 64 bit floating point,
// little endian. Might replace with H5T_NATIVE_DOUBLE
dataset_id = H5Dcreate2(file_id, "/data/sections", H5T_IEEE_F64LE, dataspace_id,
H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
double dset_data[data.size()][data[0].size()];
for( size_t i=0;i<data.size();++i ){
std::copy(data[i].begin(),data[i].end(), dset_data[i]);
}
// --- Write the dataset to file ---
status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
dset_data);
// --- write column names for dataset ---
std::vector<std::string> colnames;
colnames.resize(11);
colnames = {"energy","trans","dtrans","cro","dcro","cs",
"zero","co","zero","idk","dcs"};
std::string name = "energy";
const char* cname = name.c_str();
hid_t datatype = H5Tcopy (H5T_C_S1);
size_t MAX_NAME_LENGTH = 32;
H5Tset_size (datatype, H5T_VARIABLE);
hsize_t name_dims[1];
name_dims[0] = 1;
name_dataspace_id = H5Screate_simple(1, name_dims, NULL);
name_dataset_id = H5Dcreate2(file_id, "/data/colnames", datatype,
name_dataspace_id, H5P_DEFAULT, H5P_DEFAULT,
H5P_DEFAULT);
for( auto name : colnames ){
const char* cname = name.c_str();
status = H5Dwrite(name_dataset_id, datatype, H5S_ALL, H5S_ALL, H5P_DEFAULT,
&cname);
}
status = H5Fclose(file_id);
*/
sammy::hdf5IO h5writer;
h5writer.writeODFtoHDF5(head,data,hdf5Filename,true);
return 0;
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment