38 #ifndef __HDF5_UTILS_CXX
39 #define __HDF5_UTILS_CXX
41 #include "HDF5Utils.h"
48 #include "CommonTypes.h"
49 #include "Exceptions.h"
59 std::ifstream ifile(filename.c_str());
64 file = H5::H5File( filename.c_str(), H5F_ACC_EXCL);
67 file = H5::H5File( filename.c_str(), H5F_ACC_RDWR);
81 size_t nextpos = path.find_first_of(
"/", curpos);
82 H5::Group g = file.openGroup(
"/");
84 std::string name = path.substr(curpos, nextpos-1);
86 while (curpos != std::string::npos && name !=
"") {
89 g = g.openGroup(name);
92 g = g.createGroup(name);
94 std::string msg = std::string(
"the path ") +path +
" does not exist";
100 nextpos = path.find_first_of(
"/", curpos);
101 if ( nextpos != std::string::npos )
102 name = path.substr(curpos, nextpos-curpos);
104 name = path.substr(curpos);
118 void HDF5Utils::readMatrixOfType<unsigned int>(
const H5::CommonFG& fg,
const char* name, GenericEigenType<unsigned int>::MatrixType& matrix) {
119 H5::DataSet ds = fg.openDataSet( name );
121 ds.getSpace().getSimpleExtentDims(dims, NULL);
124 matrix.resize(dims[0], dims[1]);
125 ds.read(matrix.data(), H5::PredType::NATIVE_UINT);
130 void HDF5Utils::readMatrixOfType<float>(
const H5::CommonFG& fg,
const char* name, GenericEigenType<float>::MatrixType& matrix) {
131 H5::DataSet ds = fg.openDataSet( name );
133 ds.getSpace().getSimpleExtentDims(dims, NULL);
136 matrix.resize(dims[0], dims[1]);
137 ds.read(matrix.data(), H5::PredType::NATIVE_FLOAT);
142 void HDF5Utils::readMatrixOfType<double>(
const H5::CommonFG& fg,
const char* name, GenericEigenType<double>::MatrixType& matrix) {
143 H5::DataSet ds = fg.openDataSet( name );
145 ds.getSpace().getSimpleExtentDims(dims, NULL);
148 matrix.resize(dims[0], dims[1]);
149 ds.read(matrix.data(), H5::PredType::NATIVE_DOUBLE);
155 readMatrixOfType<ScalarType>(fg, name, matrix);
161 H5::DataSet ds = fg.openDataSet( name );
163 ds.getSpace().getSimpleExtentDims(dims, NULL);
165 hsize_t nRows = dims[0];
166 hsize_t nCols = std::min(dims[1], static_cast<hsize_t>(maxNumColumns));
168 hsize_t offset[2] = {0,0};
173 H5::DataSpace dataspace = ds.getSpace();
174 dataspace.selectHyperslab( H5S_SELECT_SET, count, offset );
180 H5::DataSpace memspace( 2, dimsm );
183 hsize_t offset_out[2] = {0, 0};
184 hsize_t count_out[2];
186 count_out[0] = nRows;
187 count_out[1] = nCols;
188 memspace.selectHyperslab( H5S_SELECT_SET, count_out, offset_out );
190 matrix.resize(nRows, nCols);
191 ds.read(matrix.data(), H5::PredType::NATIVE_FLOAT, memspace, dataspace);
203 H5::DataSet HDF5Utils::writeMatrixOfType<unsigned int>(
const H5::CommonFG& fg,
const char* name,
const GenericEigenType<unsigned int>::MatrixType& matrix) {
206 if (matrix.rows() == 0 || matrix.cols() == 0) {
210 hsize_t dims[2] = {matrix.rows(), matrix.cols()};
211 H5::DataSet ds = fg.createDataSet( name, H5::PredType::NATIVE_UINT, H5::DataSpace(2, dims));
212 ds.write( matrix.data(), H5::PredType::NATIVE_UINT );
218 H5::DataSet HDF5Utils::writeMatrixOfType<float>(
const H5::CommonFG& fg,
const char* name,
const GenericEigenType<float>::MatrixType& matrix) {
221 if (matrix.rows() == 0 || matrix.cols() == 0) {
222 throw StatisticalModelException(
"Empty matrix provided to writeMatrix");
225 hsize_t dims[2] = {matrix.rows(), matrix.cols()};
226 H5::DataSet ds = fg.createDataSet( name, H5::PredType::NATIVE_FLOAT, H5::DataSpace(2, dims));
227 ds.write( matrix.data(), H5::PredType::NATIVE_FLOAT );
233 H5::DataSet HDF5Utils::writeMatrixOfType<double>(
const H5::CommonFG& fg,
const char* name,
const GenericEigenType<double>::MatrixType& matrix) {
236 if (matrix.rows() == 0 || matrix.cols() == 0) {
237 throw StatisticalModelException(
"Empty matrix provided to writeMatrix");
240 hsize_t dims[2] = {matrix.rows(), matrix.cols()};
241 H5::DataSet ds = fg.createDataSet( name, H5::PredType::NATIVE_DOUBLE, H5::DataSpace(2, dims));
242 ds.write( matrix.data(), H5::PredType::NATIVE_DOUBLE );
249 return writeMatrixOfType<ScalarType>(fg, name, matrix);
255 void HDF5Utils::readVectorOfType(
const H5::CommonFG& fg,
const char* name,
typename GenericEigenType<T>::VectorType& vector) {
261 void HDF5Utils::readVectorOfType<double>(
const H5::CommonFG& fg,
const char* name, GenericEigenType<double>::VectorType& vector) {
262 H5::DataSet ds = fg.openDataSet( name );
264 ds.getSpace().getSimpleExtentDims(dims, NULL);
265 vector.resize(dims[0], 1);
266 ds.read(vector.data(), H5::PredType::NATIVE_DOUBLE);
271 void HDF5Utils::readVectorOfType<float>(
const H5::CommonFG& fg,
const char* name, GenericEigenType<float>::VectorType& vector) {
272 H5::DataSet ds = fg.openDataSet( name );
274 ds.getSpace().getSimpleExtentDims(dims, NULL);
275 vector.resize(dims[0], 1);
276 ds.read(vector.data(), H5::PredType::NATIVE_FLOAT);
281 void HDF5Utils::readVectorOfType<int>(
const H5::CommonFG& fg,
const char* name, GenericEigenType<int>::VectorType& vector) {
282 H5::DataSet ds = fg.openDataSet( name );
284 ds.getSpace().getSimpleExtentDims(dims, NULL);
285 vector.resize(dims[0], 1);
286 ds.read(vector.data(), H5::PredType::NATIVE_INT);
291 readVectorOfType<ScalarType>(fg, name, vector);
297 H5::DataSet ds = fg.openDataSet( name );
299 ds.getSpace().getSimpleExtentDims(dims, NULL);
301 hsize_t nElements = std::min(dims[0], static_cast<hsize_t>(maxNumElements));
303 hsize_t offset[1] = {0};
305 count[0] = nElements;
307 H5::DataSpace dataspace = ds.getSpace();
308 dataspace.selectHyperslab( H5S_SELECT_SET, count, offset );
312 dimsm[0] = nElements;
313 H5::DataSpace memspace( 1, dimsm );
316 hsize_t offset_out[1] = {0};
317 hsize_t count_out[1];
319 count_out[0] = nElements;
320 memspace.selectHyperslab( H5S_SELECT_SET, count_out, offset_out );
322 vector.resize(nElements);
323 ds.read(vector.data(), H5::PredType::NATIVE_FLOAT, memspace, dataspace);
331 H5::DataSet HDF5Utils::writeVectorOfType(
const H5::CommonFG& fg,
const char* name,
const typename GenericEigenType<T>::VectorType& vector) {
337 H5::DataSet HDF5Utils::writeVectorOfType<double>(
const H5::CommonFG& fg,
const char* name,
const GenericEigenType<double>::VectorType& vector) {
338 hsize_t dims[1] = {vector.size()};
339 H5::DataSet ds = fg.createDataSet( name, H5::PredType::NATIVE_DOUBLE, H5::DataSpace(1, dims));
340 ds.write( vector.data(), H5::PredType::NATIVE_DOUBLE );
346 H5::DataSet HDF5Utils::writeVectorOfType<float>(
const H5::CommonFG& fg,
const char* name,
const GenericEigenType<float>::VectorType& vector) {
347 hsize_t dims[1] = {vector.size()};
348 H5::DataSet ds = fg.createDataSet( name, H5::PredType::NATIVE_FLOAT, H5::DataSpace(1, dims));
349 ds.write( vector.data(), H5::PredType::NATIVE_FLOAT );
355 H5::DataSet HDF5Utils::writeVectorOfType<int>(
const H5::CommonFG& fg,
const char* name,
const GenericEigenType<int>::VectorType& vector) {
356 hsize_t dims[1] = {vector.size()};
357 H5::DataSet ds = fg.createDataSet( name, H5::PredType::NATIVE_INT, H5::DataSpace(1, dims));
358 ds.write( vector.data(), H5::PredType::NATIVE_INT );
364 return writeVectorOfType<ScalarType>(fg, name, vector);
370 H5::StrType fls_type(H5::PredType::C_S1, s.length() + 1);
371 H5::DataSet ds = fg.createDataSet(name, fls_type, H5::DataSpace(H5S_SCALAR));
372 ds.write(s, fls_type);
380 H5std_string outputString;
381 H5::DataSet ds = fg.openDataSet(name);
382 ds.read(outputString, ds.getStrType());
388 H5::StrType strdatatype(H5::PredType::C_S1, s.length() + 1 );
389 H5::Attribute att = fg.createAttribute(name, strdatatype, H5::DataSpace(H5S_SCALAR));
390 att.write(strdatatype, s);
398 H5std_string outputString;
400 H5::Attribute myatt_out = fg.openAttribute(name);
401 myatt_out.read(myatt_out.getStrType(), outputString);
407 H5::IntType int_type(H5::PredType::NATIVE_INT32);
408 H5::DataSpace att_space(H5S_SCALAR);
409 H5::Attribute att = fg.createAttribute(name, int_type, att_space );
410 att.write( int_type, &value);
417 H5::IntType fls_type(H5::PredType::NATIVE_INT32);
419 H5::Attribute myatt_out = fg.openAttribute(name);
420 myatt_out.read(fls_type, &value);
427 H5::IntType fls_type(H5::PredType::NATIVE_INT32);
428 H5::DataSet ds = fg.createDataSet(name, fls_type, H5::DataSpace(H5S_SCALAR));
429 ds.write(&value, fls_type);
435 H5::IntType fls_type(H5::PredType::NATIVE_INT32);
436 H5::DataSet ds = fg.openDataSet( name );
439 ds.read(&value, fls_type);
445 H5::FloatType fls_type(H5::PredType::NATIVE_FLOAT);
446 H5::DataSet ds = fg.createDataSet(name, fls_type, H5::DataSpace(H5S_SCALAR));
447 ds.write(&value, fls_type);
453 H5::FloatType fls_type(H5::PredType::NATIVE_FLOAT);
454 H5::DataSet ds = fg.openDataSet( name );
457 ds.read(&value, fls_type);
463 H5::DataSet ds = fg.openDataSet( name );
465 ds.getSpace().getSimpleExtentDims(dims, NULL);
466 std::vector<char> buffer(dims[0]);
467 if(!buffer.empty()) ds.read(&buffer[0], H5::PredType::NATIVE_CHAR);
469 typedef std::ostream_iterator<char> ostream_iterator;
470 std::ofstream ofile(filename, std::ios::binary);
472 std::string s= std::string(
"could not open file ") +filename;
476 std::copy(buffer.begin(), buffer.end(), ostream_iterator(ofile));
484 typedef std::istream_iterator<char> istream_iterator;
486 std::ifstream ifile(filename, std::ios::binary);
488 std::string s= std::string(
"could not open file ") +filename;
492 std::vector<char> buffer;
493 ifile >> std::noskipws;
494 std::copy(istream_iterator(ifile), istream_iterator(), std::back_inserter(buffer));
498 hsize_t dims[] = {buffer.size()};
499 H5::DataSet ds = fg.createDataSet( name, H5::PredType::NATIVE_CHAR, H5::DataSpace(1, dims));
500 ds.write( &buffer[0], H5::PredType::NATIVE_CHAR );
523 H5::DataSet ds = fg.openDataSet( name );
525 ds.getSpace().getSimpleExtentDims(dims, NULL);
526 array.resize(dims[0]);
527 ds.read( &array[0], H5::PredType::NATIVE_INT32);
534 hsize_t dims[1] = {array.size()};
535 H5::DataSet ds = fg.createDataSet( name, H5::PredType::NATIVE_INT32, H5::DataSpace(1, dims));
536 ds.write( &array[0], H5::PredType::NATIVE_INT32 );
543 for (hsize_t i = 0; i < fg.getNumObjs(); ++i) {
544 std::string objname= fg.getObjnameByIdx(i);
545 if (objname == name) {
static bool existsObjectWithName(const H5::CommonFG &fg, const std::string &name)
Definition: HDF5Utils.hxx:542
static H5::DataSet writeArray(const H5::CommonFG &fg, const char *name, std::vector< T > const &array)
Definition: HDF5Utils.hxx:515
static void writeIntAttribute(const H5::H5Object &fg, const char *name, int value)
Definition: HDF5Utils.hxx:406
static H5::H5File openOrCreateFile(const std::string filename)
Definition: HDF5Utils.hxx:56
static void readMatrix(const H5::CommonFG &fg, const char *name, MatrixType &matrix)
Definition: HDF5Utils.hxx:154
static H5::DataSet writeMatrix(const H5::CommonFG &fg, const char *name, const MatrixType &matrix)
Definition: HDF5Utils.hxx:248
static void dumpFileToHDF5(const char *filename, const H5::CommonFG &fg, const char *name)
Definition: HDF5Utils.hxx:482
static std::string readString(const H5::CommonFG &fg, const char *name)
Definition: HDF5Utils.hxx:379
static std::string readStringAttribute(const H5::H5Object &group, const char *name)
Definition: HDF5Utils.hxx:397
static void readArray(const H5::CommonFG &fg, const char *name, std::vector< T > &array)
Definition: HDF5Utils.hxx:507
static void writeStringAttribute(const H5::H5Object &group, const char *name, const std::string &s)
Definition: HDF5Utils.hxx:387
static H5::DataSet writeString(const H5::CommonFG &fg, const char *name, const std::string &s)
Definition: HDF5Utils.hxx:369
static float readFloat(const H5::CommonFG &fg, const char *name)
Definition: HDF5Utils.hxx:452
static H5::DataSet writeInt(const H5::CommonFG &fg, const char *name, int value)
Definition: HDF5Utils.hxx:426
Generic Exception class for the statismo Library.
Definition: Exceptions.h:68
static H5::DataSet writeMatrixOfType(const H5::CommonFG &fg, const char *name, const typename GenericEigenType< T >::MatrixType &matrix)
Definition: HDF5Utils.hxx:197
static H5::Group openPath(H5::H5File &fg, const std::string &path, bool createPath=false)
Definition: HDF5Utils.hxx:76
static void readVector(const H5::CommonFG &fg, const char *name, unsigned nElements, VectorType &vector)
Definition: HDF5Utils.hxx:296
static H5::DataSet writeVector(const H5::CommonFG &fg, const char *name, const VectorType &vector)
Definition: HDF5Utils.hxx:363
static H5::DataSet writeFloat(const H5::CommonFG &fg, const char *name, float value)
Definition: HDF5Utils.hxx:444
static void getFileFromHDF5(const H5::CommonFG &fg, const char *name, const char *filename)
Definition: HDF5Utils.hxx:462
static void readMatrixOfType(const H5::CommonFG &fg, const char *name, typename GenericEigenType< T >::MatrixType &matrix)
Definition: HDF5Utils.hxx:112
static int readInt(const H5::CommonFG &fg, const char *name)
Definition: HDF5Utils.hxx:434
static int readIntAttribute(const H5::H5Object &group, const char *name)
Definition: HDF5Utils.hxx:416