4 #ifndef ANDRES_HDF5_HXX
5 #define ANDRES_HDF5_HXX
22 template<
bool B>
class HandleCheck;
23 template<>
class HandleCheck<false> {
26 { counter_ = H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL); }
28 { assert(counter_ == H5Fget_obj_count(H5F_OBJ_ALL, H5F_OBJ_ALL)); }
32 template<>
class HandleCheck<true> {
49 throw std::runtime_error(
"No matching HDF5 type.");
65 throw std::runtime_error(
"No matching HDF5 type.");
73 return H5T_IEEE_F32LE;
75 return H5T_IEEE_F64LE;
77 throw std::runtime_error(
"No matching HDF5 type.");
84 {
return uintTypeHelper<unsigned char>(); }
86 {
return uintTypeHelper<unsigned short>(); }
88 {
return uintTypeHelper<unsigned int>(); }
90 {
return uintTypeHelper<unsigned long>(); }
92 {
return uintTypeHelper<unsigned long long>(); }
94 {
return intTypeHelper<signed char>(); }
96 {
return uintTypeHelper<char>(); }
98 {
return intTypeHelper<short>(); }
100 {
return intTypeHelper<int>(); }
102 {
return intTypeHelper<long>(); }
104 {
return intTypeHelper<long long>(); }
106 {
return floatingTypeHelper<float>(); }
108 {
return floatingTypeHelper<double>(); }
121 const std::string& filename,
124 hid_t version = H5P_DEFAULT;
126 version = H5Pcreate(H5P_FILE_ACCESS);
127 H5Pset_libver_bounds(version, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST);
130 hid_t fileHandle = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT, version);
132 throw std::runtime_error(
"Could not create HDF5 file: " + filename);
150 const std::string& filename,
154 hid_t access = H5F_ACC_RDONLY;
156 access = H5F_ACC_RDWR;
159 hid_t version = H5P_DEFAULT;
161 version = H5Pcreate(H5P_FILE_ACCESS);
162 H5Pset_libver_bounds(version, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST);
165 hid_t fileHandle = H5Fopen(filename.c_str(), access, version);
167 throw std::runtime_error(
"Could not open HDF5 file: " + filename);
195 const hid_t& parentHandle,
196 const std::string& groupName
198 hid_t groupHandle = H5Gcreate(parentHandle, groupName.c_str(),
199 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
200 if(groupHandle < 0) {
201 throw std::runtime_error(
"Could not create HDF5 group.");
216 const hid_t& parentHandle,
217 const std::string& groupName
219 hid_t groupHandle = H5Gopen(parentHandle, groupName.c_str(), H5P_DEFAULT);
220 if(groupHandle < 0) {
221 throw std::runtime_error(
"Could not open HDF5 group.");
244 const hid_t parentHandle,
245 const std::string datasetName,
246 const std::vector<T>& data
248 hsize_t shape[] = {data.size()};
249 hid_t dataspace = H5Screate_simple(1, shape, NULL);
251 throw std::runtime_error(
"could not create HDF5 dataspace.");
253 hid_t dataset = H5Dcreate(parentHandle, datasetName.c_str(), hdf5Type<T>(), dataspace, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
256 throw std::runtime_error(
"could not create HDF5 dataset.");
258 hid_t status = H5Dwrite(dataset, hdf5Type<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, data.data());
262 throw std::runtime_error(
"could not write to HDF5 dataset.");
271 const hid_t parentHandle,
272 const std::string datasetName,
273 std::vector<T>& data,
277 hid_t dataset = H5Dopen(parentHandle, datasetName.c_str(), H5P_DEFAULT);
279 throw std::runtime_error(
"could not open HDF5 dataset.");
281 hid_t typeFile = H5Dget_type(dataset);
284 hid_t filespace = H5Dget_space(dataset);
285 int dimension = H5Sget_simple_extent_ndims(filespace);
287 throw std::runtime_error(
"HDF5 dataset is not one-dimensional.");
290 herr_t status = H5Sget_simple_extent_dims(filespace, &size, NULL);
295 throw std::runtime_error(
"could not get shape of HDF5 dataset.");
300 status = H5Dread(dataset, hdf5Type<T>(), H5S_ALL, H5S_ALL, H5P_DEFAULT, data.data());
307 throw std::runtime_error(
"could not read from HDF5 dataset 'points'.");
323 const std::string& filename,
324 const std::string& datasetName,
329 load(file, datasetName, out);
336 #endif // #ifndef ANDRES_HDF5_HXX