13 #define H5Gopen_vers 1
15 #include "mdal_utils.hpp"
16 #include "mdal_logger.hpp"
18 typedef unsigned char uchar;
28 #define HDF_MAX_NAME 1024
31 char data [HDF_MAX_NAME];
34 template <
int TYPE>
inline void hdfClose( hid_t
id ) { MDAL_UNUSED(
id ); assert(
false ); }
35 template <>
inline void hdfClose<H5I_FILE>( hid_t
id ) { H5Fclose(
id ); }
36 template <>
inline void hdfClose<H5I_GROUP>( hid_t
id ) { H5Gclose(
id ); }
37 template <>
inline void hdfClose<H5I_DATASET>( hid_t
id ) { H5Dclose(
id ); }
38 template <>
inline void hdfClose<H5I_ATTR>( hid_t
id ) { H5Aclose(
id ); }
39 template <>
inline void hdfClose<H5I_DATASPACE>( hid_t
id ) { H5Sclose(
id ); }
40 template <>
inline void hdfClose<H5I_DATATYPE>( hid_t
id ) { H5Tclose(
id ); }
46 HdfH( hid_t hid ) : id( hid ) {}
47 HdfH(
const HdfH &other ) : id( other.id ) { }
48 ~
HdfH() {
if (
id >= 0 ) hdfClose<TYPE>(
id ); }
70 HdfFile(
const std::string &path, HdfFile::Mode mode );
75 inline std::vector<std::string> groups()
const;
77 inline HdfGroup group(
const std::string &path )
const;
78 inline HdfDataset dataset(
const std::string &path )
const;
79 inline HdfAttribute attribute(
const std::string &attr_name )
const;
80 inline bool pathExists(
const std::string &path )
const;
81 std::string filePath()
const;
84 std::shared_ptr<Handle> d;
93 HdfDataType( hid_t type,
bool isNativeType =
true );
97 static HdfDataType createString(
int size = HDF_MAX_NAME );
103 std::shared_ptr<Handle> d;
104 hid_t mNativeId = -1;
112 static HdfGroup create( hid_t file,
const std::string &path );
113 HdfGroup( hid_t file,
const std::string &path );
114 HdfGroup( std::shared_ptr<Handle> handle );
116 bool isValid()
const;
118 hid_t file_id()
const;
120 std::string name()
const;
122 std::vector<std::string> groups()
const;
123 std::vector<std::string> datasets()
const;
124 std::vector<std::string> objects()
const;
126 std::string childPath(
const std::string &childName )
const;
128 inline HdfGroup group(
const std::string &groupName )
const;
129 inline HdfDataset dataset(
const std::string &dsName )
const;
130 inline HdfAttribute attribute(
const std::string &attr_name )
const;
131 inline bool pathExists(
const std::string &path )
const;
133 std::vector<std::string> objects( H5G_obj_t type )
const;
136 std::shared_ptr<Handle> d;
148 HdfAttribute( hid_t obj_id,
const std::string &attr_name );
150 bool isValid()
const;
153 std::string readString()
const;
154 double readDouble()
const;
156 void write(
const std::string &value );
157 void write(
int value );
160 std::shared_ptr<Handle> d;
182 const std::vector<hsize_t> counts );
184 bool isValid()
const;
188 std::shared_ptr<Handle> d;
204 HdfDataset( hid_t file,
const std::string &path );
206 bool isValid()
const;
209 std::vector<hsize_t> dims()
const;
211 hsize_t elementCount()
const;
213 H5T_class_t type()
const;
219 std::vector<float> readArray()
const;
220 std::vector<double> readArrayDouble()
const;
221 std::vector<int> readArrayInt()
const;
222 std::vector<std::string> readArrayString()
const;
228 std::vector<uchar>
readArrayUint8(
const std::vector<hsize_t> offsets,
const std::vector<hsize_t> counts )
const;
229 std::vector<float> readArray(
const std::vector<hsize_t> offsets,
const std::vector<hsize_t> counts )
const;
230 std::vector<double> readArrayDouble(
const std::vector<hsize_t> offsets,
const std::vector<hsize_t> counts )
const;
231 std::vector<int> readArrayInt(
const std::vector<hsize_t> offsets,
const std::vector<hsize_t> counts )
const;
233 inline bool hasAttribute(
const std::string &attr_name )
const;
234 inline HdfAttribute attribute(
const std::string &attr_name )
const;
236 template <
typename T> std::vector<T> readArray( hid_t mem_type_id )
const
238 hsize_t cnt = elementCount();
239 std::vector<T> data( cnt );
240 herr_t status = H5Dread( d->id, mem_type_id, H5S_ALL, H5S_ALL, H5P_DEFAULT, data.data() );
243 MDAL::Log::debug(
"Failed to read data!" );
244 return std::vector<T>();
249 template <
typename T> std::vector<T> readArray( hid_t mem_type_id,
250 const std::vector<hsize_t> offsets,
251 const std::vector<hsize_t> counts )
const
254 dataspace.selectHyperslab( offsets, counts );
256 hsize_t totalItems = 1;
257 for (
auto it = counts.begin(); it != counts.end(); ++it )
260 std::vector<hsize_t> dims = {totalItems};
262 memspace.selectHyperslab( 0, totalItems );
264 std::vector<T> data( totalItems );
265 herr_t status = H5Dread( d->id, mem_type_id, memspace.id(), dataspace.id(), H5P_DEFAULT, data.data() );
268 MDAL::Log::debug(
"Failed to read data!" );
269 return std::vector<T>();
281 void write(
const std::string &value );
284 void write( std::vector<float> &value );
285 void write(
float value );
288 void write( std::vector<double> &value );
291 std::shared_ptr<Handle> d;
295 inline std::vector<std::string> HdfFile::groups()
const {
return group(
"/" ).groups(); }
297 inline HdfGroup HdfFile::group(
const std::string &path )
const {
return HdfGroup( d->id, path ); }
299 inline HdfDataset HdfFile::dataset(
const std::string &path )
const {
return HdfDataset( d->id, path ); }
301 inline HdfGroup HdfGroup::group(
const std::string &groupName )
const {
return HdfGroup( file_id(), childPath( groupName ) ); }
303 inline HdfDataset HdfGroup::dataset(
const std::string &dsName )
const {
return HdfDataset( file_id(), childPath( dsName ) ); }
305 inline bool HdfDataset::hasAttribute(
const std::string &attr_name )
const
307 htri_t res = H5Aexists( d->id, attr_name.c_str() );
311 inline HdfAttribute HdfFile::attribute(
const std::string &attr_name )
const {
return HdfAttribute( d->id, attr_name ); }
313 inline HdfAttribute HdfGroup::attribute(
const std::string &attr_name )
const {
return HdfAttribute( d->id, attr_name ); }
315 inline HdfAttribute HdfDataset::attribute(
const std::string &attr_name )
const {
return HdfAttribute( d->id, attr_name ); }
317 inline bool HdfFile::pathExists(
const std::string &path )
const {
return H5Lexists( d->id, path.c_str(), H5P_DEFAULT ) > 0; }
319 inline bool HdfGroup::pathExists(
const std::string &path )
const {
return H5Lexists( d->id, path.c_str(), H5P_DEFAULT ) > 0; }
Definition: mdal_hdf5.hpp:140
HdfAttribute(hid_t obj_id, const std::string &attr_name, HdfDataType type)
Create new attribute for writing for 1 item.
Definition: mdal_hdf5.cpp:95
Definition: mdal_hdf5.hpp:89
Definition: mdal_hdf5.hpp:192
std::string readString() const
Reads string value.
Definition: mdal_hdf5.cpp:316
void write(const std::string &value)
Writes string dataset with single entry.
Definition: mdal_hdf5.cpp:301
float readFloat() const
Reads float value.
Definition: mdal_hdf5.cpp:253
std::vector< uchar > readArrayUint8() const
Reads full array into vector Array can have any number of dimenstions and it is fully read into 1D ve...
Definition: mdal_hdf5.cpp:229
HdfDataset()=default
creates invalid dataset
Definition: mdal_hdf5.hpp:169
HdfDataspace(const std::vector< hsize_t > &dims)
memory dataspace for simple N-D array
Definition: mdal_hdf5.cpp:335
void selectHyperslab(hsize_t start, hsize_t count)
select from 1D array
Definition: mdal_hdf5.cpp:353
Definition: mdal_hdf5.hpp:60
Definition: mdal_hdf5.hpp:108
Definition: mdal_hdf5.hpp:44
Definition: mdal_hdf5.hpp:30