forked from lmb-freiburg/demon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleh5file.h
355 lines (279 loc) · 10.7 KB
/
simpleh5file.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// DeMoN - Depth Motion Network
// Copyright (C) 2017 Benjamin Ummenhofer, Huizhong Zhou
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#ifndef SIMPLEH5FILE_H_
#define SIMPLEH5FILE_H_
#include <vector>
#include <string>
#include <hdf5.h>
/*!
* This class provides basic functions to manipulate hdf5 files
*/
class SimpleH5File
{
public:
//! File modes
enum FileMode { TRUNCATE, //!< Create or overwrite file
READ, //!< Read only file access
READ_WRITE //!< Read write access
};
//! Compression settings
enum Compression { UNCOMPRESSED, /*GZIP_1, GZIP_2,...*/ };
/*!
* ctor.
* \param use_locking If true uses mutexes to allow accessing the object
* from multiple threads.
*/
SimpleH5File(bool use_locking=false);
/*!
* Creates a SimpleH5File object and opens the specified hdf5 file.
*
* \param filename Filename of the hdf5 file. E.g. 'myfile.h5'
* \param mode The mode for opening the file.
* \param use_locking If true uses mutexes to allow accessing the object
* from multiple threads.
*/
SimpleH5File( const std::string& filename, FileMode mode=READ, bool use_locking=false );
/*!
* dtor. Closes the opened file.
*/
virtual ~SimpleH5File();
/*!
* Opens the specified hdf5 file. If a hdf5 file was already opened it is
* closed before opening the new file.
*
* \param filename Filename of the hdf5 file. E.g. 'myfile.h5'
* \param mode The mode for opening the file.
*/
void open( const std::string& filename, FileMode mode=READ );
/*!
* Returns whether a file is open.
*/
bool isOpen() const;
/*!
* Returns whether the object uses locking to allow multiple thread using
* this object simultaneously.
*/
bool useLocking() const;
/*!
* Closes the file. Has no effect if no file is open.
*/
void close();
/*!
* Creates a new group and creates parent directories if necessary.
*
* \param path Path of the new group. E.g. '/group1/group2/newGroup'
* creates 'newGroup' and 'group1', 'group2' if they dont exist.
*/
void makeGroup( const std::string& path );
/*!
* Removes a group or dataset
*
* \param path The path of the group or dataset to be removed.
* E.g. '/group/mydataset' removes 'mydataset'
*/
void remove( const std::string& path );
/*!
* Returns whether the object with the specified path is a group.
*/
bool isGroup( const std::string& path );
/*!
* Returns whether the object with the specified path is a dataset.
*/
bool isDataset( const std::string& path );
/*!
* Returns true if the native type and the dataset type match.
* If 'path' is not a dataset then false is returned.
*/
template <class NATIVE_TYPE>
bool datasetDataType( const std::string& path );
/*!
* Returns whether the path points to a dataset or group
*/
bool exists( const std::string& path );
/*!
* Lists all objects (datasets and groups) with the parent specified by path.
*
* \param path E.g. '/' lists all groups and datasets of the root group.
*/
std::vector<std::string> listObjects( const std::string& path );
/*!
* Lists all datasets with the parent specified by path.
*
* \param path E.g. '/' lists all datasets of the root group.
*/
std::vector<std::string> listDatasets( const std::string& path );
/*!
* Lists all groups with the parent specified by path.
*
* \param path E.g. '/' lists all groups of the root group.
*/
std::vector<std::string> listGroups( const std::string& path );
/*!
* Lists all attributes of a dataset or group specified by path.
*
* \param path E.g. '/mydataset' lists all attributes of 'mydataset'.
*/
std::vector<std::string> listAttributes( const std::string& path );
/*!
* Writes a dataset. Any existing dataset will be overwritten.
* This command will also create parent groups if necessary.
*
* \param data Pointer to the data
* \param dims Dimensions of the dataset to write. The extent of each
* dimension is defined in elements.
* \param path Path to the dataset e.g. '/group/dataset'.
* \param compress Reserved for future use to specify the compression filter
*/
template <class T>
void writeDataset( const T* data, const std::vector<size_t>& dims,
const std::string& path,
Compression compress = UNCOMPRESSED );
/*!
* Reads the dataset to the given buffer.
*
* \param data The buffer for reading the dataset. The buffer must be
* allocated by the user. Use getDatasetExtents() to retrieve
* the size of the dataset.
* \param path Path to the dataset e.g. '/group/dataset'.
*/
template <class T>
void readDataset( T* data, const std::string& path );
/*!
* Returns the byte offset of the dataset in the file and the number
* of elements.
*
* \param path Path to the dataset e.g. '/group/dataset'.
* \return Returns the byte offset of the dataset in the file and the number
* of elements
*/
std::pair<size_t,size_t> getDatasetOffsetAndSize( const std::string& path );
/*!
* Returns whether the dataset is contiguous or not.
*
* \param path Path to the dataset e.g. '/group/dataset'.
* \return Returns true if the dataset is contiguous
*/
bool isDatasetContiguous( const std::string& path );
/*!
* Returns the extents of the dataset.
*
* \param path Path to the dataset e.g. '/group/dataset'.
* \return Returns a vector containing the extents. The size of the vector
* corresponds to the number of dimensions of the dataset
*/
std::vector<size_t> getDatasetExtents( const std::string& path );
/*!
* Writes an attribute. An attribute is attached to a group or a dataset.
* Overwrites existing attributes.
*
* \param value The value of the attribute.
* \param attr_name The name of the attribute e.g. 'my_int_attribute'
* \param path The path of the group or dataset e.g. '/group'
*/
template <class T>
void writeAttribute( const T& value,
const std::string& attr_name, const std::string& path );
//! \sa writeAttribute(const T& value, const std::string&, const std::string&)
template <class T>
void writeAttribute( const std::vector<T>& value,
const std::string& attr_name, const std::string& path );
//! \sa writeAttribute(const T& value, const std::string&, const std::string&)
void writeAttribute( const char str[],
const std::string& attr_name, const std::string& path );
//! \sa writeAttribute(const T& value, const std::string&, const std::string&)
void writeAttribute( const std::string& str,
const std::string& attr_name, const std::string& path );
/*!
* Reads an attribute. An attribute is attached to a group or a dataset.
*
* \param value The value that is written to the attribute
* \param attr_name The name of the attribute e.g. 'my_int_attribute'
* \param path The path of the group or dataset e.g. '/group'
*/
template <class T>
void readAttribute( T& value,
const std::string& attr_name, const std::string& path );
//! \sa readAttribute(T& value, const std::string&, const std::string&)
template <class T>
void readAttribute( std::vector<T>& value,
const std::string& attr_name, const std::string& path );
//! \sa readAttribute(T& value, const std::string&, const std::string&)
void readAttribute( std::string& str,
const std::string& attr_name, const std::string& path );
/*!
* Removes an attribute.
*
* \param attr_name Name of the attribute.
* \param path The path of the group or dataset e.g. '/group'
*/
void removeAttribute( const std::string& attr_name, const std::string& path );
/*!
* Checks the existence of an attribute.
*
* \param attr_name Name of the attribute.
* \param path The path of the group or dataset e.g. '/group'
* \return Returns true if the attribute exists.
*/
bool existsAttribute( const std::string& attr_name, const std::string& path );
/*!
* Returns the H5O_info_t struct for the object with the specified path.
*
* \param path Path to an object (group or dataset) e.g. '/mydataset'
* \return The H5O_info_t struct of the object.
*/
H5O_info_t getObjectInfo( const std::string& path );
/*!
* Checks if a file is a hdf5 file
*
* \param filename path to the file
* \return Returns true if the file is a hdf5 file.
* Returns false if the file is not a hdf5 file.
* Returns false if the file does not exist or reading fails.
*/
static bool isHDF5( const std::string& filename );
/*!
* Simplifies a hdf5 path. This function removes leading and trailing
* whitespaces and removes rendundant multiple '/'.
*
* \return The simplified path.
*/
static std::string simplifyPath( const std::string& path );
protected:
FileMode mode;
hid_t file_id; //! hdf5 file identifier
private:
SimpleH5File( const SimpleH5File& other ):use_locking(false) {}
SimpleH5File& operator=( const SimpleH5File& other ) { return *this; }
/*!
* Creates a dataset. This command will also create parent groups if
* necessary.
*
* \param dataset_path Path to the dataset e.g. '/group/dataset'.
* \param dims Dimensions of the dataset to write. The extent of
* each dimension is defined in elements.
* \param compress Reserved for future use to specify the compression
* filter
*/
template <class T>
void createDataset( const std::string& dataset_path,
const std::vector<size_t>& dims,
Compression compress = UNCOMPRESSED );
bool is_open;
const bool use_locking;
};
#endif /* SIMPLEH5FILE_H_ */