9#include "MantidNexusGeometry/H5ForwardCompatibility.h"
10#include "MantidNexusGeometry/NexusGeometryDefinitions.h"
14#include <boost/filesystem.hpp>
27namespace NexusGeometry {
31 std::string pathString =
"";
32 for (
const std::string &grp : path) {
33 pathString +=
"/" + grp;
42 const auto typeClass = data.getTypeClass();
43 const size_t sizeOfType = data.getDataType().getSize();
45 if (std::is_floating_point<ExpectedT>::value) {
46 if (H5T_FLOAT != typeClass) {
47 throw std::runtime_error(
"Storage type mismatch. Expecting to extract a "
48 "floating point number");
50 if (sizeOfType !=
sizeof(ExpectedT)) {
51 throw std::runtime_error(
"Storage type mismatch for floats. This operation "
52 "is dangerous. Nexus stored has byte size:" +
55 }
else if (std::is_integral<ExpectedT>::value) {
56 if (H5T_INTEGER != typeClass) {
57 throw std::runtime_error(
"Storage type mismatch. Expecting to extract a integer");
59 if (sizeOfType >
sizeof(ExpectedT)) {
61 throw std::runtime_error(
"Storage type mismatch for integer. Result "
62 "would result in truncation. Nexus stored has byte size:" +
76 boost::filesystem::path
tmp = fullPath;
78 if (!boost::filesystem::exists(
tmp)) {
79 throw std::invalid_argument(
"no such file.\n");
81 m_file.openFile(fullPath, H5F_ACC_RDONLY);
89 for (hsize_t i = 0; i < parentGroup.getNumObjs(); ++i) {
90 if (parentGroup.getObjTypeByIdx(i) == GROUP_TYPE) {
91 H5std_string childPath = parentGroup.getObjnameByIdx(i);
93 auto childGroup = parentGroup.openGroup(childPath);
95 for (uint32_t attribute_index = 0; attribute_index < static_cast<uint32_t>(childGroup.getNumAttrs());
98 H5::Attribute attribute = childGroup.openAttribute(attribute_index);
99 if (attribute.getName() == NX_CLASS) {
101 H5::DataType dataType = attribute.getDataType();
103 H5std_string classType;
104 attribute.read(dataType, classType);
106 if (classType == nxClass) {
117 template <
typename T>
120 std::vector<T> dataInFile;
124 H5::DataSet dataset = parentGroup.openDataSet(dataSetName);
126 validateStorageType<T>(dataset);
127 auto space = dataset.getSpace();
129 dataInFile.resize(space.getSelectNpoints());
130 dataset.read(dataInFile.data(), dataset.getDataType(), space);
139 H5::Group parent =
m_file.openGroup(pathList[0]);
141 for (
size_t i = 1; i < pathList.size(); ++i) {
142 child = parent.openGroup(pathList[i]);
154 H5::Group rootGroup =
m_file.openGroup(DEFAULT_ROOT_ENTRY_NAME);
158 if (parentNX_CLASS_TYPE == NX_ENTRY) {
160 for (
size_t i = 0; i < rootGroup.getNumObjs(); ++i) {
161 if (rootGroup.getObjTypeByIdx(i) == GROUP_TYPE) {
162 std::string childPath = rootGroup.getObjnameByIdx(i);
164 H5::Group childGroup = rootGroup.openGroup(childPath);
166 H5::Attribute attribute = childGroup.openAttribute(NX_CLASS);
168 attribute.read(attribute.getDataType(), attrVal);
169 if (attrVal == childNX_CLASS_TYPE) {
177 for (
size_t i = 0; i < rootGroup.getNumObjs(); ++i) {
178 if (rootGroup.getObjTypeByIdx(i) == GROUP_TYPE) {
179 std::string childPath = rootGroup.getObjnameByIdx(i);
181 H5::Group childGroup = rootGroup.openGroup(childPath);
184 H5::Attribute parentAttribute = childGroup.openAttribute(NX_CLASS);
185 std::string parentAttrVal;
186 parentAttribute.read(parentAttribute.getDataType(), parentAttrVal);
187 if (parentAttrVal == parentNX_CLASS_TYPE) {
188 for (
size_t i = 0; i < childGroup.getNumObjs(); ++i) {
189 if (childGroup.getObjTypeByIdx(i) == GROUP_TYPE) {
190 std::string grandchildPath = childGroup.getObjnameByIdx(i);
192 H5::Group grandchildGroup = childGroup.openGroup(grandchildPath);
194 H5::Attribute grandchildAttribute = grandchildGroup.openAttribute(NX_CLASS);
195 std::string grandchildAttrVal;
196 grandchildAttribute.read(grandchildAttribute.getDataType(), grandchildAttrVal);
197 if (childNX_CLASS_TYPE == grandchildAttrVal) {
212 hsize_t dims[
static_cast<hsize_t
>(1)];
213 dims[0] =
static_cast<hsize_t
>(1);
215 H5::DataSpace space = H5Screate_simple(rank, dims,
nullptr);
219 H5::DataSet dataset = parentGroup.openDataSet(datasetName);
220 dataset.read(&
value, H5::PredType::NATIVE_DOUBLE, space);
230 H5::DataSet dataset = parentGroup.openDataSet(datasetName);
232 H5::Attribute attribute = dataset.openAttribute(attrName);
234 H5::DataType dataType = attribute.getDataType();
235 H5::DataSpace dataSpace = attribute.getSpace();
237 std::vector<double>
value;
238 value.resize(dataSpace.getSelectNpoints());
240 attribute.read(dataType,
value.data());
248 H5::Group parentGroup =
m_file.openGroup(pathToGroup);
249 auto numOfChildren = parentGroup.getNumObjs();
250 for (
size_t i = 0; i < numOfChildren; i++) {
251 if (parentGroup.getObjTypeByIdx(i) == DATASET_TYPE) {
252 std::string dSetName = parentGroup.getObjnameByIdx(i);
253 H5::DataSet dSet = parentGroup.openDataSet(dSetName);
254 if (dSet.attrExists(NX_CLASS)) {
255 H5::Attribute attribute = dSet.openAttribute(NX_CLASS);
256 std::string attributeValue;
257 attribute.read(attribute.getDataType(), attributeValue);
258 if (attributeValue == nx_attributeVal)
268 const std::string &attrName) {
270 H5::Group parentGroup =
m_file.openGroup(pathToGroup);
271 auto numOfChildren = parentGroup.getNumObjs();
272 for (
size_t i = 0; i < numOfChildren; i++) {
273 if (parentGroup.getObjTypeByIdx(i) == DATASET_TYPE) {
274 std::string dSetName = parentGroup.getObjnameByIdx(i);
275 H5::DataSet dSet = parentGroup.openDataSet(dSetName);
276 if (dSet.attrExists(NX_CLASS)) {
277 H5::Attribute attribute = dSet.openAttribute(attrName);
278 std::string attributeValue;
279 attribute.read(attribute.getDataType(), attributeValue);
280 if (attributeValue == attributeVal)
292 auto numOfChildren = parentGroup.getNumObjs();
293 for (
size_t i = 0; i < numOfChildren; i++) {
294 if (parentGroup.getObjTypeByIdx(i) == DATASET_TYPE) {
295 std::string dataSetName = parentGroup.getObjnameByIdx(i);
296 if (dsetName == dataSetName) {
304 bool groupHasNxClass(
const std::string &attrVal,
const std::string &pathToGroup)
const {
306 H5::Attribute attribute;
307 H5::Group parentGroup =
m_file.openGroup(pathToGroup);
308 attribute = parentGroup.openAttribute(NX_CLASS);
309 std::string attributeValue;
310 attribute.read(attribute.getDataType(), attributeValue);
312 return attributeValue == attrVal;
321 H5::DataSet dataSet = parentGroup.openDataSet(dataSetName);
322 std::string dataSetVal;
323 auto type = dataSet.getDataType();
324 dataSet.read(dataSetVal, type);
325 dataSetVal.resize(type.getSize());
326 return dataSetVal == dataSetValue;
327 }
catch (H5::DataSetIException &) {
337 H5::Attribute attribute = parentGroup.openAttribute(attrName);
338 std::string attributeValue;
339 auto type = attribute.getDataType();
340 attribute.read(type, attributeValue);
341 attributeValue.resize(type.getSize());
342 return attributeValue == attrVal;
349 H5::Attribute attribute = parentGroup.openAttribute(NX_CLASS);
350 std::string attributeValue;
351 attribute.read(attribute.getDataType(), attributeValue);
353 return attributeValue == attrVal;
359 H5::Attribute attribute;
361 H5::DataSet dataSet = parentGroup.openDataSet(dataSetName);
362 attribute = dataSet.openAttribute(attrName);
363 std::string attributeValue;
364 attribute.read(attribute.getDataType(), attributeValue);
366 return attributeValue == attrVal;
371 H5::Attribute attribute;
373 H5::DataSet dataSet = parentGroup.openDataSet(dataSetName);
374 attribute = dataSet.openAttribute(NX_CLASS);
375 std::string attributeValue;
376 attribute.read(attribute.getDataType(), attributeValue);
378 return attributeValue == attrVal;
double value
The value of the point.
int countNXgroup(const FullNXPath &pathToGroup, const std::string &nxClass)
bool hasDatasetWithAttribute(const std::string &pathToGroup, const std::string &attributeVal, const std::string &attrName)
bool dataSetHasStrValue(const std::string &dataSetName, const std::string &dataSetValue, const FullNXPath &pathToGroup) const
H5::Group openfullH5Path(const FullNXPath &pathList) const
std::vector< T > readDataSetMultidimensional(FullNXPath &pathToGroup, const std::string &dataSetName)
bool hasNXAttributeInGroup(const std::string &attrVal, const FullNXPath &pathToGroup)
bool hasAttributeInGroup(const std::string &attrName, const std::string &attrVal, const FullNXPath &pathToGroup)
std::vector< double > readDoubleVectorFrom_d_Attribute(const std::string &attrName, const std::string &datasetName, const FullNXPath &pathToGroup)
bool hasAttributeInDataSet(const std::string &dataSetName, const std::string &attrName, const std::string &attrVal, const FullNXPath &pathToGroup)
NexusFileReader(const std::string &fullPath)
bool hasDatasetWithNXAttribute(const std::string &pathToGroup, const std::string &nx_attributeVal)
bool hasNXAttributeInDataSet(const std::string &dataSetName, const std::string &attrVal, const FullNXPath &pathToGroup)
bool groupHasNxClass(const std::string &attrVal, const std::string &pathToGroup) const
bool hasDataset(const std::string &dsetName, const FullNXPath &pathToGroup)
double readDoubleFromDataset(const std::string &datasetName, const FullNXPath &pathToGroup)
bool parentNXgroupHasChildNXgroup(const std::string &parentNX_CLASS_TYPE, const std::string &childNX_CLASS_TYPE)
std::string toNXPathString(FullNXPath &path)
void validateStorageType(const H5::DataSet &data)
std::vector< std::string > FullNXPath
Helper class which provides the Collimation Length for SANS instruments.
std::string to_string(const wide_integer< Bits, Signed > &n)