-
Notifications
You must be signed in to change notification settings - Fork 30
/
HDF5_IO.h
169 lines (131 loc) · 4.92 KB
/
HDF5_IO.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
// This file contains helper functions and interfaces for reading/writing HDF5
#ifndef included_HDF5_h
#define included_HDF5_h
#include "common/ArraySize.h"
#include <cstring>
#include <string>
// Include the headers and define some basic types
#ifdef USE_HDF5
// Using HDF5
#include "hdf5.h"
#else
// Not using HDF5
typedef int hid_t;
typedef size_t hsize_t;
#endif
namespace IO {
namespace HDF5 {
enum class Compression : uint8_t { None, GZIP, SZIP };
/**
* \brief Open an HDF5 file
* \details This function opens and HDF5 file for reading/writing.
* Once complete, we must close the file using closeHDF5
* @param[in] filename File to open
* @param[in] mode C string containing a file access mode. It can be:
* "r" read: Open file for input operations. The file must exist.
* "w" write: Create an empty file for output operations.
* If a file with the same name already exists, its contents
* are discarded and the file is treated as a new empty file.
* "rw" read+write: Open file for reading and writing. The file must exist.
* @param[in] compress Default compression
* @return Return a handle to the file.
*/
hid_t openHDF5(
const std::string &filename, const char *mode, Compression compress = Compression::None );
/**
* \brief Open an HDF5 file
* \details This function opens and HDF5 file for reading/writing
* @param[in] fid File to open
*/
void closeHDF5( hid_t fid );
/**
* \brief Retrun the the default compression
* \details This function returns the default compression used when the file was created
* @param[in] fid File/Group id
*/
Compression defaultCompression( hid_t fid );
/**
* \brief Open an HDF5 file
* \details This function create a chunk for HDF5
* @param[in] dims Chunk size
* @param[in] compress Compression to use
* @return Return a handle to the file.
*/
hid_t createChunk( const std::vector<hsize_t> &dims, Compression compress );
/**
* \brief Write a structure to HDF5
* \details This function writes a C++ class/struct to HDF5.
* This is a templated function and users can impliment their own data
* types by creating explicit instantiations for a given type.
* There is no default instantiation except when compiled without HDF5 which is a no-op.
* @param[in] fid File or group to write to
* @param[in] name The name of the variable
* @param[in] data The structure to write
*/
template<class T>
void writeHDF5( hid_t fid, const std::string &name, const T &data );
/**
* \brief Read a structure from HDF5
* \details This function reads a C++ class/struct from HDF5.
* This is a templated function and users can impliment their own data
* types by creating explicit instantiations for a given type.
* There is no default instantiation except when compiled without HDF5 which is a no-op.
* @param[in] fid File or group to read from
* @param[in] name The name of the variable
* @param[out] data The structure to read
*/
template<class T>
void readHDF5( hid_t fid, const std::string &name, T &data );
/**
* \brief Check if group exists
* \details This function checks if an HDF5 group exists in the file
* @param[in] fid ID of group or database to read
* @param[in] name The name of the group
*/
bool H5Gexists( hid_t fid, const std::string &name );
/**
* \brief Check if dataset exists
* \details This function checks if an HDF5 dataset exists in the file
* @param[in] fid File to open
* @param[in] name The name of the dataset
*/
bool H5Dexists( hid_t fid, const std::string &name );
/**
* \brief Create a group
* \details This function creates a new HDF5 group
* @param[in] fid File or group to write to
* @param[in] name The name of the group
*/
hid_t createGroup( hid_t fid, const std::string &name );
/**
* \brief Open a group
* \details This function opens an HDF5 group
* @param[in] fid File or group to write to
* @param[in] name The name of the group
*/
hid_t openGroup( hid_t fid, const std::string &name );
/**
* \brief Close a group
* \details This function closes an HDF5 group
* @param[in] fid Group to close
*/
void closeGroup( hid_t fid );
/**
* \brief Get HDF5 data type
* \details This function returns the id of the data type
*/
template<class T>
hid_t getHDF5datatype();
// Default no-op implimentations for use without HDF5
// clang-format off
#ifndef USE_HDF5
template<class T> void readHDF5( hid_t, const std::string&, T& ) {}
template<class T> void writeHDF5( hid_t, const std::string&, const T& ) {}
template<class T> void readHDF5Array( hid_t, const std::string&, Array<T>& ) {}
template<class T> void writeHDF5Array( hid_t, const std::string&, const Array<T>& ) {}
template<class T> hid_t getHDF5datatype() { return 0; }
#endif
// clang-format on
} // namespace HDF5
} // namespace IO
#endif