added: support reading a 3D array from HDF5

This commit is contained in:
Arne Morten Kvarving 2019-05-21 06:08:03 +02:00
parent c1f3ea9e65
commit 6a08d1bbcc
2 changed files with 50 additions and 1 deletions

View File

@ -13,6 +13,7 @@
#include "HDF5Reader.h"
#include <array>
#include <iostream>
#include <sstream>
@ -26,7 +27,8 @@
HDF5Reader::HDF5Reader (const std::string& name, const ProcessAdm& adm)
: HDF5Base((name.find(".hdf5") == std::string::npos ? name+".hdf5": name), adm)
: HDF5Base(((name.find(".hdf5") == std::string::npos &&
name.find(".h5") == std::string::npos) ? name+".hdf5": name), adm)
{
}
@ -120,6 +122,45 @@ bool HDF5Reader::readString (const std::string& name, std::string& out)
}
bool HDF5Reader::read3DArray (const std::string& name, Matrix3D& out)
{
#ifdef HAS_HDF5
if (!this->openFile(H5F_ACC_RDONLY))
return false;
hid_t set = H5Dopen2(m_file,name.c_str(),H5P_DEFAULT);
hid_t space = H5Dget_space(set);
hsize_t dims = H5Sget_simple_extent_ndims(space);
if (dims != 3) {
std::cerr << "HDF5Reader: Wrong dimension for field " << name << std::endl;
return false;
}
std::array<hsize_t, 3> ndim;
H5Sget_simple_extent_dims(space, ndim.data(), nullptr);
out.resize(ndim[0],ndim[1],ndim[2]);
std::vector<double> test(ndim[0]*ndim[1]*ndim[2]);
H5Dread(set,H5T_NATIVE_DOUBLE,H5S_ALL,H5S_ALL,H5P_DEFAULT,test.data());
H5Dclose(set);
auto it = test.begin();
for (hsize_t i = 0; i < ndim[0]; ++i)
for (hsize_t j = 0; j < ndim[1]; ++j)
for (hsize_t k = 0; k < ndim[2]; ++k, ++it)
out(i+1, j+1, k+1) = *it;
return true;
#else
std::cerr << "HDF5Reader: compiled without HDF5 support, no data read" << std::endl;
return false;
#endif
}
int HDF5Reader::getFieldSize (const std::string& fieldPath)
{
#ifdef HAS_HDF5

View File

@ -15,9 +15,12 @@
#define _HDF5_READER_H
#include "HDF5Base.h"
#include "MatVec.h"
#include <string>
#include <vector>
/*!
\brief Read data from a HDF5 file.
*/
@ -50,6 +53,11 @@ public:
//! \param[out] out The string to read data into
bool readString(const std::string& name, std::string& out);
//! \brief Reads a 3D array.
//! \param[in] name The name (path in HDF5 file) to the string
//! \param[out] data The 3D array to read data into
bool read3DArray(const std::string& name, Matrix3D& data);
//! \brief Returns number of patches for a field.
//! \param[in] fieldPath Path to field in hdf5 file
int getFieldSize(const std::string& fieldPath);