group_particles
Loading...
Searching...
No Matches
hdf5_utils.hpp
Go to the documentation of this file.
1
6#ifndef READ_FIELDS_HPP
7#define READ_FIELDS_HPP
8
9#include <cassert>
10#include <memory>
11#include <string>
12#include <cstdlib>
13
14#include "H5Cpp.h"
15
17namespace hdf5Utils {
18
29template<typename TH5, typename Trequ>
30static Trequ
31read_scalar_attr (H5::Group &header, const std::string &name)
32{// {{{
33 TH5 out;
34 auto attr = header.openAttribute(name);
35 assert (attr.getDataType().getSize() == sizeof(TH5));
36 attr.read(attr.getDataType(), &out);
37 return (Trequ)(out);
38}// }}}
39
51template<typename TH5, typename Trequ>
52static Trequ
53read_vector_attr (H5::Group &header, const std::string &name, size_t idx)
54{// {{{
55 auto attr = header.openAttribute(name);
56 auto aspace = attr.getSpace();
57 hsize_t dim_lenghts[16];
58 auto Ndims = aspace.getSimpleExtentDims(dim_lenghts);
59 assert(Ndims == 1);
60 assert(dim_lenghts[0] > idx);
61 assert(attr.getDataType().getSize() == sizeof(TH5));
62 TH5 out[dim_lenghts[0]];
63 attr.read(attr.getDataType(), out);
64 return (Trequ)(out[idx]);
65}// }}}
66
78template<typename TH5, typename Trequ>
79static size_t
80read_vector_attr (H5::Group &header, const std::string &name, Trequ *out)
81{// {{{
82 auto attr = header.openAttribute(name);
83 auto aspace = attr.getSpace();
84 hsize_t dim_lenghts[16];
85 auto Ndims = aspace.getSimpleExtentDims(dim_lenghts);
86 assert(Ndims == 1);
87 assert(attr.getDataType().getSize() == sizeof(TH5));
88
89 if constexpr (std::is_same_v<TH5, Trequ>)
90 attr.read(attr.getDataType(), out);
91 else
92 {
93 TH5 out1[dim_lenghts[0]];
94 attr.read(attr.getDataType(), out1);
95 for (size_t ii=0; ii != dim_lenghts[0]; ++ii)
96 out[ii] = (Trequ)out1[ii];
97 }
98
99 return dim_lenghts[0];
100}// }}}
101
102} // namespace hdf5Utils
103
104#endif // READ_FIELDS_HPP
some utility functions to read attributes from hdf5 files
Definition hdf5_utils.hpp:17
static Trequ read_vector_attr(H5::Group &header, const std::string &name, size_t idx)
reads element of vector attribute from an hdf5 group
Definition hdf5_utils.hpp:53
static Trequ read_scalar_attr(H5::Group &header, const std::string &name)
reads scalar attribute from an hdf5 group
Definition hdf5_utils.hpp:31