Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
hdfutilities.hh
Go to the documentation of this file.
1
8#ifndef UTOPIA_DATAIO_HDFUTILITIES_HH
9#define UTOPIA_DATAIO_HDFUTILITIES_HH
10
11#include <array>
12#include <cmath>
13#include <iostream>
14#include <string>
15#include <string_view>
16#include <type_traits>
17#include <vector>
18
19#include <hdf5.h>
20#include <hdf5_hl.h>
21
23
24// Functions for determining if a type is an STL-container are provided here.
25// This is used if we wish to make hdf5 types for storing such data in an
26// hdf5 dataset.
27namespace Utopia
28{
29namespace DataIO
30{
72bool
73check_validity(htri_t valid, const std::string_view object_name)
74{
75 if (valid > 0)
76 {
77 return true;
78 }
79
80 else if (valid < 0)
81 {
82 throw std::runtime_error("Object " + std::string(object_name) + ": " +
83 "Error in validity check");
84 return false;
85 }
86 else
87 {
88 return false;
89 }
90}
91
93
106htri_t
108 std::string path,
110{
111 // Position of the segment cursor; all characters before are checked
112 // For absolute paths, search always needs to start behind index 1
113 std::size_t seg_pos = (path.find("/") == 0) ? 1 : 0;
114
115 // A buffer for the return value of H5Lexists
116 htri_t rv;
117
118 // Go over all segments until the whole string is
119 while (seg_pos != std::string::npos)
120 {
121 // Find the position of the next "/", strictly after the current
122 // cursor position
123 seg_pos = path.find("/", seg_pos + 1);
124
125 // Check for existence of the subpath. If seg_pos is string::npos,
126 // the substring is the full path and this is the last loop iteration.
127 rv = H5Lexists(
128 loc_id, path.substr(0, seg_pos).c_str(), link_property_list);
129
130 // If this segment does not exists, need to return already
131 if (rv <= 0)
132 {
133 return rv;
134 }
135 }
136
137 // Checked the full path. Can return the last return value now:
138 return rv;
139}
140
149auto
150path_is_valid(hid_t id, std::string path)
151{
152 auto test = H5LTpath_valid(id, path.c_str(), true);
153 if (test > 0)
154 {
155 return true;
156 }
157 else if (test == 0)
158 {
159 return false;
160 }
161 else
162 {
163 throw std::runtime_error("Error when checking path " + path);
164 return false;
165 }
166}
167
181
188template < HDFCategory category >
189std::string
191{
192 switch (category)
193 {
195 return "file";
197 return "group";
199 return "datatype";
201 return "dataspace";
203 return "dataset";
205 return "attribute";
206 }
207}
208
218template < class Object >
219std::string
221{
222 return "category " + category_to_string< Object::category >() + " at '" +
223 object.get_path() + "'";
224}
225
233template < typename Object >
234hid_t
236{
237 if constexpr (std::decay_t< Object >::category == HDFCategory::dataset)
238 {
239 return H5Dget_space(object.get_C_id());
240 }
241 else if constexpr (std::decay_t< Object >::category ==
243 {
244 return H5Aget_space(object.get_C_id());
245 }
246 else
247 {
248 return -1;
249 }
250}
251
259template < typename Object >
260hid_t
262{
263 if constexpr (std::decay_t< Object >::category == HDFCategory::dataset)
264 {
265 return H5Dget_type(object.get_C_id());
266 }
267 else if constexpr (std::decay_t< Object >::category ==
269 {
270 return H5Aget_type(object.get_C_id());
271 }
272 else
273 {
274 return -1;
275 }
276}
277
// end of group HDF5 // end of group DataIO
280
281} // namespace DataIO
282} // namespace Utopia
283#endif
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
hid_t open_dataspace(Object &&object)
Depending on object category, opens a dataset or attribute dataspace.
Definition hdfutilities.hh:235
bool check_validity(htri_t valid, const std::string_view object_name)
Check for validity of a hdf5 htri_t type or similar.
Definition hdfutilities.hh:73
std::string generate_object_name(const Object &object)
Use category and path variable of object to make a string that identifies the object it is applied to...
Definition hdfutilities.hh:220
auto path_is_valid(hid_t id, std::string path)
Check if the path given relative to the object identified by 'id' exists and points to a valid hdf5 o...
Definition hdfutilities.hh:150
htri_t path_exists(hid_t loc_id, std::string path, hid_t link_property_list=H5P_DEFAULT)
Checks iteratively if each segment of a path exists.
Definition hdfutilities.hh:107
std::string category_to_string()
Turn category enum into a string that names it.
Definition hdfutilities.hh:190
hid_t open_type(Object &&object)
Depending on object category, invokes H5Dget_type or H5Aget_type.
Definition hdfutilities.hh:261
HDFCategory
Enumerate the different HDF5 object types for use in HDFObject class.
Definition hdfutilities.hh:173
Definition agent.hh:11