Skip to content
Snippets Groups Projects
Commit 64ba4b96 authored by Brown's avatar Brown
Browse files

write header to h5 file as well

appended to group called meta
parent 8b3f1b30
No related branches found
No related tags found
No related merge requests found
......@@ -3,8 +3,6 @@ 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)
......@@ -42,7 +40,7 @@ TRIBITS_ADD_TEST_DIRECTORIES(tests)
TRIBITS_PACKAGE_POSTPROCESS()
target_link_libraries(SammyIOUtilsLib ${h5lib} ${h5lib_CPP})
target_link_libraries(SammyIOUtilsLib ${HDF5_LIBRARIES} ) # from find_package()
# --------------------------------------------
# Build separate executable to convert ODF
......
#include "hdf5IO.h"
#include "H5Cpp.h"
#define MAX_NAME_LEN 32
namespace sammy {
......@@ -17,7 +14,7 @@ namespace sammy {
std::cerr << "Reading of ODF files with < 11 sections"
<< "has not been implemented yet. " << std::endl;
std::cerr << "---------------------------------------" << std::endl;
return;
throw std::runtime_error("Unrecognized ODF file.");
}
// HDF5 works best with set-size char arrays
......@@ -31,13 +28,12 @@ namespace sammy {
std::copy(data[i].begin(),data[i].end(), dset_data[i]);
}
// write 2 datasets: data and data section names
// write 3 datasets: data, data section names, and header
// 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");
......@@ -45,28 +41,74 @@ namespace sammy {
// section names space
hsize_t sdim[1];
sdim[0] = head.numSection;
int srank = sizeof(sdim) / sizeof(hsize_t);
std::string sectionnames("/data/sectnames");
// header
const int numMembers = 26;
sammy::odfMetaKey h5head[numMembers];
sammy::hdf5IO h5writer;
h5writer.fillHDF5header(head,h5head);
hsize_t mdim[1];
mdim[0] = sizeof(h5head) / sizeof(sammy::odfMetaKey);
int mrank = sizeof(mdim) / sizeof(hsize_t);
const std::string headername("/meta/header");
const std::string headKeyword("keyword");
const std::string headVal("val");
// define compound type for header struct
H5::CompType htype(sizeof(sammy::odfMetaKey));
htype.insertMember(headKeyword, HOFFSET(sammy::odfMetaKey, keyword),H5::StrType(H5::PredType::C_S1, MAX_NAME_LEN));
htype.insertMember(headVal, HOFFSET(sammy::odfMetaKey, value ),H5::PredType::NATIVE_INT);
// open file and write
H5::DataSpace dspace(drank,ddim);
H5::DataSpace sspace(srank,sdim);
H5::DataSpace mspace(mrank,mdim);
H5::H5File *file = new H5::H5File(hdf5Filename,H5F_ACC_TRUNC);
H5::Group *group = new H5::Group( file->createGroup( "/data" ));
H5::Group *metaGroup = new H5::Group( file->createGroup( "/meta" ));
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));
H5::DataSet *metaset = new H5::DataSet(file->createDataSet(headername,htype,mspace));
dataset->write( dset_data, H5::PredType::NATIVE_DOUBLE );
nameset->write( sect11, H5::StrType(H5::PredType::C_S1, MAX_NAME_LEN) );
metaset->write( h5head, htype );
// sweep, sweep
delete file;
delete dataset;
delete nameset;
delete metaset;
}
void hdf5IO::fillHDF5header(sammy::odfHeader &head,
sammy::odfMetaKey h5head[]){
const int numMembers = 26;
char desc[numMembers][MAX_NAME_LEN] = {"mode","source","irun","ncblks",
"ncwrds","nsblks","nswrds","ncstrt","ncntrs","nxstrt",
"nxwrds","npblks","npwrds","ndtype","numSectio","ifb",
"numChan","zan","awr","mat","mf","mt","varswt","dctswt",
"strt","ndwend"};
int vals[numMembers] = {head.mode,head.source,head.irun,head.ncblks,
head.ncwrds,head.nsblks,head.nswrds,head.ncstrt,
head.ncntrs,head.nxstrt,head.nxwrds,head.npblks,
head.npwrds,head.ndtype,head.numSection,head.ifb,
head.numChan,head.zan,head.awr,head.mat,head.mf,
head.mt,head.varswt,head.dctswt,head.strt,
head.ndwend};
for( int i=0;i<numMembers;++i ){
std::strncpy(h5head[i].keyword, desc[i], MAX_NAME_LEN );
h5head[i].value = vals[i];
}
}
}
\ No newline at end of file
......@@ -22,6 +22,9 @@ namespace sammy{
std::vector<std::vector<double>> &data,
std::string hdf5Filename,
bool elevenSect);
void fillHDF5header(sammy::odfHeader &head,
sammy::odfMetaKey h5head[]);
};
}
......
......@@ -5,6 +5,8 @@
#include <fstream> // ifstream
#include <iostream>
#define MAX_NAME_LEN 32
namespace sammy{
/****************************************
......@@ -21,6 +23,11 @@ namespace sammy{
std::vector<int> ndtbl;
};
struct odfMetaKey{
char keyword[MAX_NAME_LEN];
int value;
};
class odfIO {
public:
......
......@@ -53,5 +53,7 @@ int main(int argc, char const *argv[])
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