forked from AmbaPant/mantid
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NexusFileReader.h
395 lines (330 loc) · 13.8 KB
/
NexusFileReader.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
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once
#include "MantidNexusGeometry/H5ForwardCompatibility.h"
#include "MantidNexusGeometry/NexusGeometryDefinitions.h"
#include <Eigen/Dense>
#include <H5Cpp.h>
#include <boost/filesystem.hpp>
#include <string>
#include <vector>
/*
* NexusFileReader: Test utility for unit testing in
* NexusGeometrySave::saveInstrument.
*
* @author Takudzwa Makoni, RAL (UKRI), ISIS
* @date 06/08/2019
*/
namespace Mantid {
namespace NexusGeometry {
using FullNXPath = std::vector<std::string>;
// get Nexus file path as string. Used in Nexus Geometry unit tests.
std::string toNXPathString(FullNXPath &path) {
std::string pathString = "";
for (const std::string &grp : path) {
pathString += "/" + grp;
}
return pathString;
}
// ported from NexusGeometryParser, for validating storage type of dataset
// before reading its contents into a container
template <typename ExpectedT> void validateStorageType(const H5::DataSet &data) {
const auto typeClass = data.getTypeClass();
const size_t sizeOfType = data.getDataType().getSize();
// Early check to prevent reinterpretation of underlying data.
if (std::is_floating_point<ExpectedT>::value) {
if (H5T_FLOAT != typeClass) {
throw std::runtime_error("Storage type mismatch. Expecting to extract a "
"floating point number");
}
if (sizeOfType != sizeof(ExpectedT)) {
throw std::runtime_error("Storage type mismatch for floats. This operation "
"is dangerous. Nexus stored has byte size:" +
std::to_string(sizeOfType));
}
} else if (std::is_integral<ExpectedT>::value) {
if (H5T_INTEGER != typeClass) {
throw std::runtime_error("Storage type mismatch. Expecting to extract a integer");
}
if (sizeOfType > sizeof(ExpectedT)) {
// endianness not checked
throw std::runtime_error("Storage type mismatch for integer. Result "
"would result in truncation. Nexus stored has byte size:" +
std::to_string(sizeOfType));
}
}
}
// test utility used for validation of the structure of a nexus file as needed
// for unit tests in Nexus Geometry.
class NexusFileReader {
bool m_open = false;
public:
NexusFileReader(const std::string &fullPath) {
boost::filesystem::path tmp = fullPath;
if (!boost::filesystem::exists(tmp)) {
throw std::invalid_argument("no such file.\n");
} else {
m_file.openFile(fullPath, H5F_ACC_RDONLY);
m_open = true;
}
}
int countNXgroup(const FullNXPath &pathToGroup, const std::string &nxClass) {
int counter = 0;
H5::Group parentGroup = openfullH5Path(pathToGroup);
for (hsize_t i = 0; i < parentGroup.getNumObjs(); ++i) {
if (parentGroup.getObjTypeByIdx(i) == GROUP_TYPE) {
H5std_string childPath = parentGroup.getObjnameByIdx(i);
// Open the sub group
auto childGroup = parentGroup.openGroup(childPath);
// Iterate through attributes to find NX_class
for (uint32_t attribute_index = 0; attribute_index < static_cast<uint32_t>(childGroup.getNumAttrs());
++attribute_index) {
// Test attribute at current index for NX_class
H5::Attribute attribute = childGroup.openAttribute(attribute_index);
if (attribute.getName() == NX_CLASS) {
// Get attribute data type
H5::DataType dataType = attribute.getDataType();
// Get the NX_class type
H5std_string classType;
attribute.read(dataType, classType);
// If group of correct type, return the childGroup
if (classType == nxClass) {
counter++;
}
}
}
}
}
return counter;
}
// read a multidimensional dataset and returns vector containing the data
template <typename T>
std::vector<T> readDataSetMultidimensional(FullNXPath &pathToGroup, const std::string &dataSetName) {
std::vector<T> dataInFile;
// open dataset and read.
H5::Group parentGroup = openfullH5Path(pathToGroup);
H5::DataSet dataset = parentGroup.openDataSet(dataSetName);
validateStorageType<T>(dataset);
auto space = dataset.getSpace();
dataInFile.resize(space.getSelectNpoints());
dataset.read(dataInFile.data(), dataset.getDataType(), space);
return dataInFile;
}
/* safely open a HDF5 group path with additional helpful
debug information to output where open fails) */
H5::Group openfullH5Path(const FullNXPath &pathList) const {
H5::Group child;
H5::Group parent = m_file.openGroup(pathList[0]);
for (size_t i = 1; i < pathList.size(); ++i) {
child = parent.openGroup(pathList[i]);
parent = child;
}
return child;
}
// moves down the index through groups starting at root, and if
// child has expected CLASS_TYPE, and is in parent group with expected
// parent
bool parentNXgroupHasChildNXgroup(const std::string &parentNX_CLASS_TYPE, const std::string &childNX_CLASS_TYPE) {
H5::Group rootGroup = m_file.openGroup(DEFAULT_ROOT_ENTRY_NAME);
// if specified parent NX class type is NX entry, check the top level of
// file structure only. (dont take extra step to look for parent group)
if (parentNX_CLASS_TYPE == NX_ENTRY) {
for (size_t i = 0; i < rootGroup.getNumObjs(); ++i) {
if (rootGroup.getObjTypeByIdx(i) == GROUP_TYPE) {
std::string childPath = rootGroup.getObjnameByIdx(i);
// Open the sub group
H5::Group childGroup = rootGroup.openGroup(childPath);
// Test attribute at current index for NX_class
H5::Attribute attribute = childGroup.openAttribute(NX_CLASS);
std::string attrVal;
attribute.read(attribute.getDataType(), attrVal);
if (attrVal == childNX_CLASS_TYPE) {
return true;
}
}
}
}
// Iterate over children of root group, and determine if a group
for (size_t i = 0; i < rootGroup.getNumObjs(); ++i) {
if (rootGroup.getObjTypeByIdx(i) == GROUP_TYPE) {
std::string childPath = rootGroup.getObjnameByIdx(i);
// Open the sub group
H5::Group childGroup = rootGroup.openGroup(childPath);
// check current child group going down from root has the specified
// NX_CLASS parent group
H5::Attribute parentAttribute = childGroup.openAttribute(NX_CLASS);
std::string parentAttrVal;
parentAttribute.read(parentAttribute.getDataType(), parentAttrVal);
if (parentAttrVal == parentNX_CLASS_TYPE) {
for (size_t i = 0; i < childGroup.getNumObjs(); ++i) {
if (childGroup.getObjTypeByIdx(i) == GROUP_TYPE) {
std::string grandchildPath = childGroup.getObjnameByIdx(i);
// Open the sub group
H5::Group grandchildGroup = childGroup.openGroup(grandchildPath);
// check NX class
H5::Attribute grandchildAttribute = grandchildGroup.openAttribute(NX_CLASS);
std::string grandchildAttrVal;
grandchildAttribute.read(grandchildAttribute.getDataType(), grandchildAttrVal);
if (childNX_CLASS_TYPE == grandchildAttrVal) {
return true;
}
}
}
}
}
}
return false;
} // namespace
double readDoubleFromDataset(const std::string &datasetName, const FullNXPath &pathToGroup) {
double value;
int rank = 1;
hsize_t dims[static_cast<hsize_t>(1)];
dims[0] = static_cast<hsize_t>(1);
H5::DataSpace space = H5Screate_simple(rank, dims, nullptr);
// open dataset and read.
H5::Group parentGroup = openfullH5Path(pathToGroup);
H5::DataSet dataset = parentGroup.openDataSet(datasetName);
dataset.read(&value, H5::PredType::NATIVE_DOUBLE, space);
return value;
}
// HERE
std::vector<double> readDoubleVectorFrom_d_Attribute(const std::string &attrName, const std::string &datasetName,
const FullNXPath &pathToGroup) {
// open dataset and read.
H5::Group parentGroup = openfullH5Path(pathToGroup);
H5::DataSet dataset = parentGroup.openDataSet(datasetName);
H5::Attribute attribute = dataset.openAttribute(attrName);
H5::DataType dataType = attribute.getDataType();
H5::DataSpace dataSpace = attribute.getSpace();
std::vector<double> value;
value.resize(dataSpace.getSelectNpoints());
attribute.read(dataType, value.data());
return value;
}
// HERE
bool hasDatasetWithNXAttribute(const std::string &pathToGroup, const std::string &nx_attributeVal) {
H5::Group parentGroup = m_file.openGroup(pathToGroup);
auto numOfChildren = parentGroup.getNumObjs();
for (size_t i = 0; i < numOfChildren; i++) {
if (parentGroup.getObjTypeByIdx(i) == DATASET_TYPE) {
std::string dSetName = parentGroup.getObjnameByIdx(i);
H5::DataSet dSet = parentGroup.openDataSet(dSetName);
if (dSet.attrExists(NX_CLASS)) {
H5::Attribute attribute = dSet.openAttribute(NX_CLASS);
std::string attributeValue;
attribute.read(attribute.getDataType(), attributeValue);
if (attributeValue == nx_attributeVal)
return true;
}
}
}
return false;
}
// HERE
bool hasDatasetWithAttribute(const std::string &pathToGroup, const std::string &attributeVal,
const std::string &attrName) {
H5::Group parentGroup = m_file.openGroup(pathToGroup);
auto numOfChildren = parentGroup.getNumObjs();
for (size_t i = 0; i < numOfChildren; i++) {
if (parentGroup.getObjTypeByIdx(i) == DATASET_TYPE) {
std::string dSetName = parentGroup.getObjnameByIdx(i);
H5::DataSet dSet = parentGroup.openDataSet(dSetName);
if (dSet.attrExists(NX_CLASS)) {
H5::Attribute attribute = dSet.openAttribute(attrName);
std::string attributeValue;
attribute.read(attribute.getDataType(), attributeValue);
if (attributeValue == attributeVal)
return true;
}
}
}
return false;
}
bool hasDataset(const std::string &dsetName, const FullNXPath &pathToGroup) {
H5::Group parentGroup = openfullH5Path(pathToGroup);
auto numOfChildren = parentGroup.getNumObjs();
for (size_t i = 0; i < numOfChildren; i++) {
if (parentGroup.getObjTypeByIdx(i) == DATASET_TYPE) {
std::string dataSetName = parentGroup.getObjnameByIdx(i);
if (dsetName == dataSetName) {
return true;
}
}
}
return false;
}
bool groupHasNxClass(const std::string &attrVal, const std::string &pathToGroup) const {
H5::Attribute attribute;
H5::Group parentGroup = m_file.openGroup(pathToGroup);
attribute = parentGroup.openAttribute(NX_CLASS);
std::string attributeValue;
attribute.read(attribute.getDataType(), attributeValue);
return attributeValue == attrVal;
}
bool dataSetHasStrValue(const std::string &dataSetName, const std::string &dataSetValue,
const FullNXPath &pathToGroup /*where the dataset lives*/) const {
H5::Group parentGroup = openfullH5Path(pathToGroup);
try {
H5::DataSet dataSet = parentGroup.openDataSet(dataSetName);
std::string dataSetVal;
auto type = dataSet.getDataType();
dataSet.read(dataSetVal, type);
dataSetVal.resize(type.getSize());
return dataSetVal == dataSetValue;
} catch (H5::DataSetIException &) {
return false;
}
}
// check if dataset or group has name-specific attribute
bool hasAttributeInGroup(const std::string &attrName, const std::string &attrVal, const FullNXPath &pathToGroup) {
H5::Group parentGroup = openfullH5Path(pathToGroup);
H5::Attribute attribute = parentGroup.openAttribute(attrName);
std::string attributeValue;
auto type = attribute.getDataType();
attribute.read(type, attributeValue);
attributeValue.resize(type.getSize());
return attributeValue == attrVal;
}
bool hasNXAttributeInGroup(const std::string &attrVal, const FullNXPath &pathToGroup) {
H5::Group parentGroup = openfullH5Path(pathToGroup);
H5::Attribute attribute = parentGroup.openAttribute(NX_CLASS);
std::string attributeValue;
attribute.read(attribute.getDataType(), attributeValue);
return attributeValue == attrVal;
}
bool hasAttributeInDataSet(const std::string &dataSetName, const std::string &attrName, const std::string &attrVal,
const FullNXPath &pathToGroup /*where the dataset lives*/) {
H5::Attribute attribute;
H5::Group parentGroup = openfullH5Path(pathToGroup);
H5::DataSet dataSet = parentGroup.openDataSet(dataSetName);
attribute = dataSet.openAttribute(attrName);
std::string attributeValue;
attribute.read(attribute.getDataType(), attributeValue);
return attributeValue == attrVal;
}
bool hasNXAttributeInDataSet(const std::string &dataSetName, const std::string &attrVal,
const FullNXPath &pathToGroup) {
H5::Attribute attribute;
H5::Group parentGroup = openfullH5Path(pathToGroup);
H5::DataSet dataSet = parentGroup.openDataSet(dataSetName);
attribute = dataSet.openAttribute(NX_CLASS);
std::string attributeValue;
attribute.read(attribute.getDataType(), attributeValue);
return attributeValue == attrVal;
}
void close() {
if (m_open) {
m_file.close();
}
m_open = false;
}
~NexusFileReader() { close(); }
private:
H5::H5File m_file;
}; // NexusFileReader
} // namespace NexusGeometry
} // namespace Mantid