HDF5 I/O read/write files

class h5stream

C++ Header-only library for simple HDF5 input/output.

How to use

Just include the h5stream.hpp into your your main file.

How to Compile

g++ -lhdf5 -lhdf5_cpp -std=c++1z example.cpp

Example

Create a File with a mode.

- "tr":   Create a file, truncate if it exists, Default
- "r":    Readonly, the file must exist
- "rw": Read/write, the file must exist
- "x":   Create a file, fail if exists


@code{.cpp}
h5stream::h5stream file("sample.h5", "tr");
// or
h5stream::h5stream file("sample.h5");
@endcode

write and read std::vector

Create a vector and write it to the file


@code{.cpp}
std::vector<double> matrix { 1, 2, 3282, 932 };
file.write<double>(matrix, "matrix");
@endcode

write and read Metadata

Write Attributes( Metadata) to the to the same data space


@code{.cpp}
auto dspace = file.get_dataspace("matrix");
dspace.write_atr<double>(1.2, "Units");
@endcode

Read data from the file

@code{.cpp}
auto xx = file.read_vector<double>("matrix");
//OR
file.read<double>(xx, "matrix");
@endcode

Read Attribute (Metadata)

double x = 0;
dspace.read_atr<double>(x, "Units");
std::cout << "Attribute : " << x << std::endl;
std::cout << "HDF file size (MB): " << file.file_size() <<
  std::endl;

Public Functions

inline void setFileName(const H5std_string &fileName, const std::string &rw = std::string("tr"))

create and set the file name

“rw”: Read-write access. If the file is currently open for read-only access then it will be reopened. Absence of this flag implies read-only access.

“x”: Fail if file already exists. “r” and “tr” are mutually exclusive

“tr”: Truncate file, if it already exists, erasing all data previously stored in the file.: default

Parameters:
  • fileName

  • rw – : Possible values are “r”: read only,

template<typename T = double, template<typename...> class vec = std::vector>
inline void write(const vec<T> &data, const H5std_string &datasetName)
Parameters:
  • data

  • datasetName

template<typename T = double>
inline void write(const H5std_string &datasetName, const T *data, unsigned data_size)
Parameters:
  • datasetName

  • data

  • data_size

template<typename T = double, template<typename...> class vec = std::vector>
inline void read(vec<T> &data, const H5std_string &datasetName)
Parameters:
  • data

  • datasetName

template<typename T = double, template<typename...> class vec>
inline void read(std::vector<vec<T>> &data, const H5std_string &datasetName)

Read the data.

Parameters:
  • data

  • datasetName

inline void close()

close the file

inline double fileSize() const

Return the filesize in MB.

Returns:

inline dspace getDataspace(const H5std_string &dataset_name)
Parameters:

dataset_name

Returns:

inline gspace getGroup(const H5std_string &dataset_name)
Parameters:

dataset_name

Returns:

inline auto createGroup(const H5std_string &group_name)

Create a group name.

Parameters:

group_name

Returns:

template<typename T>
inline void writeMetadata(const T &data, const H5std_string &label)

Write Metadata.

Template Parameters:

Tdatatype

Parameters:
  • data

  • label

template<typename T>
inline void readMetadata(T &data, const H5std_string &label)
Template Parameters:

T

Parameters:
  • data

  • label