forked from funkey/util
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdf5_helpers.h
152 lines (118 loc) · 3.74 KB
/
hdf5_helpers.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
#ifndef UTIL_HDF5_H__
#define UTIL_HDF5_H__
#include <config.h>
#ifdef HAVE_HDF5
#include <vector>
#include <hdf5.h>
#include <H5Cpp.h>
#include "hdf5_traits.h"
namespace hdf5 {
/**
* Discover all groups in the given H5File or Group and return them in a vector.
*
* @param fg An H5File or Group object.
* @return A vector of Groups in the given object.
*/
std::vector<H5::Group>
groups(H5::CommonFG& fg);
/**
* Write a vector of data to a given group.
*
* @param fg An H5File or Group object.
* @param name The name of the data set to create.
* @param data The data to write.
* @param dims The dimensions of the data (optional, defaults to 1D array).
* @param datatype An HDF5 DataType object describing the data (optional, for
* compound data types).
*/
template <typename T, typename SizeType>
void
write(
H5::CommonFG& fg,
std::string name,
const std::vector<T>& data,
const std::vector<SizeType>& dims,
H5::DataType datatype = typename detail::hdf5_traits<T>::data_type(detail::hdf5_traits<T>::pred_type)) {
// create an hdf5 dim vector
std::vector<hsize_t> hdims;
for (unsigned int i = 0; i < dims.size(); i++)
hdims.push_back(dims[i]);
// create the data space
H5::DataSpace dataspace(dims.size(), &hdims[0]);
// create a data set from data space and data type
H5::DataSet dataset = fg.createDataSet(name, datatype, dataspace);
// write the data into the data set
dataset.write(&data[0], datatype);
}
/**
* Write a vector of variable length data to a given group.
*
* @param fg An H5File or Group object.
* @param name The name of the data set to create.
* @param data The data to write.
* @param dims The dimensions of the data.
*/
template <typename T>
void
write(H5::CommonFG& fg, std::string name, const std::vector<std::vector<T> >& data, const std::vector<size_t>& dims) {
// get the HDF5 data and pred type for T
typedef typename detail::hdf5_traits<T>::data_type hdf5_data_type;
const H5::PredType hdf5_pred_type = detail::hdf5_traits<T>::pred_type;
// create the data type
hdf5_data_type datatype(hdf5_pred_type);
// create the varlen data type
H5::VarLenType varlentype(&datatype);
// create an hdf5 dim vector
std::vector<hsize_t> hdims;
for (int i = 0; i < dims.size(); i++)
hdims.push_back(dims[i]);
// create the data space
H5::DataSpace dataspace(dims.size(), &hdims[0]);
// create a data set from data space and data type
H5::DataSet dataset = fg.createDataSet(name, varlentype, dataspace);
// create the varlen data vector
std::vector<hvl_t> varlendata(data.size());
// fill it with pointers to the actual data
for (int i = 0; i < data.size(); i++) {
varlendata[i].p = (void*)(&data[i][0]);
varlendata[i].len = data[i].size();
}
// write the varlen data
dataset.write(&varlendata[0], varlentype);
}
/**
* Get the dimensions of a dataset.
*
* @param fg An H5File or Group object.
* @param name The name to the data set to read.
* @return A vector with the size of each dimension.
*/
std::vector<size_t>
dimensions(H5::CommonFG& fg, std::string name);
/**
* Read a vector of data from a group or file.
*
* @param fg An H5File or Group object.
* @param name The name to the data set to read.
* @return A vector containing the data.
*/
template <typename T>
std::vector<T>
read(H5::CommonFG& fg, std::string name) {
// open the dataset
H5::DataSet dataset = fg.openDataSet(name);
// get the dimensions
std::vector<size_t> dims = dimensions(fg, name);
// compute the size
unsigned int size = 1;
for (unsigned int d = 0; d < dims.size(); d++)
size *= dims[d];
// create a data buffer
std::vector<T> data(size);
// read the data
dataset.read(&data[0], detail::hdf5_traits<T>::pred_type);
return data;
}
} // namespace hdf5
#endif // HAVE_HDF5
#endif // UTIL_HDF5_H__