-
Notifications
You must be signed in to change notification settings - Fork 12
/
bag_hdfhelper.cpp
454 lines (403 loc) · 12 KB
/
bag_hdfhelper.cpp
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "bag_exceptions.h"
#include "bag_hdfhelper.h"
#include <array>
#include <H5Cpp.h>
#include <numeric>
namespace BAG {
//! Create an HDF5 CompType based on the layer and group types.
/*!
\param layerType
The type of layer.
The supported types are NODE and ELEVATION.
\param groupType
The group type of the layer.
For NODE, the supported types are: Hypothesis_Strength, Num_Hypotheses.
For ELEVATION, the supported types are: Shoal_Elevation, Std_Dev, Num_Soundings.
\return
The compound type of the specified layer and group type.
*/
::H5::CompType createH5compType(
LayerType layerType,
GroupType groupType)
{
::H5::CompType h5type;
if (groupType == NODE)
{
switch (layerType)
{
case Hypothesis_Strength:
h5type = ::H5::CompType{sizeof(float)};
h5type.insertMember("hyp_strength",
0,
::H5::PredType::NATIVE_FLOAT);
break;
case Num_Hypotheses:
h5type = ::H5::CompType{sizeof(unsigned int)};
h5type.insertMember("num_hypotheses",
0,
::H5::PredType::NATIVE_UINT32);
break;
default:
throw UnsupportedLayerType{};
}
}
else if (groupType == ELEVATION)
{
switch(layerType)
{
case Shoal_Elevation:
h5type = ::H5::CompType{sizeof(float)};
h5type.insertMember("shoal_elevation",
0,
::H5::PredType::NATIVE_FLOAT);
break;
case Std_Dev:
h5type = ::H5::CompType{sizeof(float)};
h5type.insertMember("stddev",
0,
::H5::PredType::NATIVE_FLOAT);
break;
case Num_Soundings:
h5type = ::H5::CompType{sizeof(int)};
h5type.insertMember("num_soundings",
0,
::H5::PredType::NATIVE_INT32);
break;
default:
throw UnsupportedLayerType{};
}
}
else
throw UnsupportedGroupType{};
return h5type;
}
//! Create an HDF5 CompType used for file I/O based upon the Record Definition.
/*!
\param definition
The list of fields making up the record.
\return
The HDF5 CompType to be used when reading and writing to an HDF file.
*/
::H5::CompType createH5fileCompType(
const RecordDefinition& definition)
{
::H5::CompType h5type{getRecordSize(definition)};
size_t fieldOffset = 0;
for (const auto& field : definition)
{
h5type.insertMember(field.name, fieldOffset,
getH5fileType(static_cast<DataType>(field.type)));
fieldOffset += Layer::getElementSize(static_cast<DataType>(field.type));
}
return h5type;
}
//! Create an HDF5 CompType to be used in memory based upon the Record Definition.
/*!
\param definition
The list of fields making up the record.
\return
The HDF5 CompType to be used when reading and writing from memory.
*/
::H5::CompType createH5memoryCompType(
const RecordDefinition& definition)
{
::H5::CompType h5type{getRecordSize(definition)};
size_t fieldOffset = 0;
for (const auto& field : definition)
{
h5type.insertMember(field.name, fieldOffset,
getH5memoryType(static_cast<DataType>(field.type)));
fieldOffset += Layer::getElementSize(static_cast<DataType>(field.type));
}
return h5type;
}
//! Get the chunk size from an HDF5 file.
/*!
\param h5file
The HDF5 file.
\param path
The path to the HDF5 DataSet.
\return
The chunk size of the specified HDF5 DataSet in the HDF5 file.
0 if the HDF5 DataSet does not use chunking.
*/
uint64_t getChunkSize(
const ::H5::H5File& h5file,
const std::string& path)
{
//Get the elevation HD5 dataset.
const auto h5dataset = h5file.openDataSet(path);
const auto h5pList = h5dataset.getCreatePlist();
if (h5pList.getLayout() == H5D_CHUNKED)
{
std::array<hsize_t, kRank> maxDims{};
const int rankChunk = h5pList.getChunk(kRank, maxDims.data());
if (rankChunk == kRank)
// This cast probably only matters on 32-bit systems, but gets rid of a compiler warning from the
// previous code, which was `return {maxDims[0]};
return (uint64_t) maxDims[0];
}
return 0;
}
//! Get the compression level from an HDF5 file.
/*!
\param h5file
The HDF5 file.
\param path
The path to the HDF5 DataSet.
\return
The compression level of the specified HDF5 DataSet in the HDF5 file.
0 if the HDF5 DataSet is not compressed.
*/
int getCompressionLevel(
const ::H5::H5File& h5file,
const std::string& path)
{
//Get the elevation HD5 dataset.
const auto h5dataset = h5file.openDataSet(path);
const auto h5pList = h5dataset.getCreatePlist();
for (int i=0; i<h5pList.getNfilters(); ++i)
{
unsigned int flags = 0;
size_t cdNelmts = 10;
constexpr size_t nameLen = 64;
std::array<unsigned int, 10> cdValues{};
std::array<char, 64> name{};
unsigned int filterConfig = 0;
const auto filter = h5pList.getFilter(i, flags, cdNelmts,
cdValues.data(), nameLen, name.data(), filterConfig);
if (filter == H5Z_FILTER_DEFLATE && cdNelmts >= 1)
return static_cast<int>(cdValues.front());
}
return 0;
}
//! Get the size of a record in memory.
/*!
\param definition
The list of fields in the record.
\return
The record size in memory, ignoring alignment.
*/
size_t getRecordSize(
const RecordDefinition& definition)
{
return std::accumulate(cbegin(definition), cend(definition), 0ULL,
[](size_t sum, const auto& field) {
return sum + Layer::getElementSize(static_cast<DataType>(field.type));
});
}
//! Determine the HDF5 file DataType from the specified data type.
/*!
\param type
The data type to be matched.
\return
The matching HDF5 type used in file reading and writing.
*/
const ::H5::AtomType& getH5fileType(
DataType type)
{
static ::H5::StrType strType{::H5::PredType::C_S1, H5T_VARIABLE};
switch(type)
{
case DT_UINT32:
return ::H5::PredType::NATIVE_UINT32;
case DT_FLOAT32:
return ::H5::PredType::NATIVE_FLOAT;
case DT_UINT8:
return ::H5::PredType::NATIVE_UINT8;
case DT_UINT16:
return ::H5::PredType::NATIVE_UINT16;
case DT_UINT64:
return ::H5::PredType::NATIVE_UINT64;
case DT_BOOLEAN:
return ::H5::PredType::NATIVE_HBOOL;
case DT_STRING:
return strType;
case DT_COMPOUND: //[fallthrough]
case DT_UNKNOWN_DATA_TYPE: //[fallthrough]
default:
throw UnsupportedDataType{};
}
}
//! Determine the HDF5 memory DataType from the specified DataType.
/*!
\param type
The data type to be matched.
\return
The matching HDF5 type used in memory.
*/
const ::H5::AtomType& getH5memoryType(
DataType type)
{
static ::H5::StrType strType{::H5::PredType::C_S1, H5T_VARIABLE};
switch(type)
{
case DT_UINT32:
return H5::PredType::NATIVE_UINT32;
case DT_FLOAT32:
return ::H5::PredType::NATIVE_FLOAT;
case DT_UINT8:
return ::H5::PredType::NATIVE_UINT8;
case DT_UINT16:
return ::H5::PredType::NATIVE_UINT16;
case DT_UINT64:
return ::H5::PredType::NATIVE_UINT64;
case DT_BOOLEAN:
return ::H5::PredType::NATIVE_HBOOL;
case DT_STRING:
return strType;
case DT_COMPOUND: //[fallthrough]
case DT_UNKNOWN_DATA_TYPE: //[fallthrough]
default:
throw UnsupportedDataType{};
}
}
//! Create an attribute on an HDF5 DataSet.
/*!
\param h5dataSet
The HDF5 DataSet to create the attribute on.
\param attributeType
The HDF5 type of the attribute.
\param path
The HDF5 path of the attribute.
The path cannot be nullptr.
\return
The new HDF5 attribute.
*/
::H5::Attribute createAttribute(
const ::H5::DataSet& h5dataSet,
const ::H5::PredType& attributeType,
const char* path)
{
return h5dataSet.createAttribute(path, attributeType, {});
}
//! Create attributes on an HDF5 DataSet.
/*!
\param h5dataSet
The HDF5 DataSet to create the attributes on.
\param attributeType
The HDF5 type of the attributes.
\param paths
The HDF5 paths of the attributes.
If no paths are provided, no attributes are written.
If a path is nullptr, that attribute is not written.
*/
void createAttributes(
const ::H5::DataSet& h5dataSet,
const ::H5::PredType& attributeType,
const std::vector<const char*>& paths)
{
if (paths.empty())
return;
for (const auto& path : paths)
if (path)
createAttribute(h5dataSet, attributeType, path);
}
//! Write an attribute to the specified HDF5 DataSet.
/*!
\param h5dataSet
The HDF5 DataSet to create the attribute on.
\param attributeType
The HDF5 type of the attribute.
\param valiue
The value of the attribute.
\param path
The HDF5 path of the attribute.
The path cannot be nullptr.
*/
template <typename T>
void writeAttribute(
const ::H5::DataSet& h5dataSet,
const ::H5::PredType& attributeType,
T value,
const char* path)
{
const auto att = h5dataSet.openAttribute(path);
att.write(attributeType, &value);
}
//! Write attributes to an HDF5 DataSet.
/*!
\param h5dataSet
The HDF5 DataSet to create the attributes on.
\param attributeType
The HDF5 type of the attributes.
\param paths
The HDF5 paths of the attributes.
If no paths are provided, no attributes are written.
If a path is nullptr, that attribute is not written.
*/
template <typename T>
void writeAttributes(
const ::H5::DataSet& h5dataSet,
const ::H5::PredType& attributeType,
T value,
const std::vector<const char*>& paths)
{
if (paths.empty())
return;
for (const auto& path : paths)
if (path)
writeAttribute(h5dataSet, attributeType, value, path);
}
// Explicit template instantiations.
//! Write a float point value to an attribute.
/*
\param h5dataSet
The HDF5 DataSet to create the attributes on.
\param attributeType
The HDF5 type of the attribute.
\param value
The value to write to the attribute.
\param path
The HDF5 path to the attribute in the HDF5 DataSet.
Cannot be nullptr.
*/
template void writeAttribute<float>(const ::H5::DataSet& h5dataSet,
const ::H5::PredType& attributeType, float value, const char* path);
//! Write an unsigned 32 bit integer value to an attribute.
/*
\param h5dataSet
The HDF5 DataSet to create the attributes on.
\param attributeType
The HDF5 type of the attribute.
\param value
The value to write to the attribute.
\param path
The HDF5 path to the attribute in the HDF5 DataSet.
Cannot be nullptr.
*/
template void writeAttribute<uint32_t>(const ::H5::DataSet& h5dataSet,
const ::H5::PredType& attributeType, uint32_t value, const char* path);
//! Write a floating point value to multiple attributes.
/*
\param h5dataSet
The HDF5 DataSet to create the attributes on.
\param attributeType
The HDF5 type of the attribute.
\param value
The value to write to the attribute.
\param paths
The HDF5 path to the attributes in the HDF5 DataSet.
If no paths provided, no attributes are written.
If any path is nullptr, that attribute is not written.
*/
template void writeAttributes<float>(const ::H5::DataSet& h5dataSet,
const ::H5::PredType& attributeType, float value,
const std::vector<const char*>& paths);
//! Write an unsigned 32 bit integer value to multiple attributes.
/*
\param h5dataSet
The HDF5 DataSet to create the attributes on.
\param attributeType
The HDF5 type of the attribute.
\param value
The value to write to the attribute.
\param paths
The HDF5 path to the attributes in the HDF5 DataSet.
If no paths provided, no attributes are written.
If any path is nullptr, that attribute is not written.
*/
template void writeAttributes<uint32_t>(const ::H5::DataSet& h5dataSet,
const ::H5::PredType& attributeType, uint32_t value,
const std::vector<const char*>& paths);
} // namespace BAG