Mantid
Loading...
Searching...
No Matches
LegacyNexusDescriptor.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include "MantidLegacyNexus/NeXusException.hpp"
9#include "MantidLegacyNexus/NeXusFile.hpp"
10
11#include <algorithm>
12#include <cstring>
13#include <filesystem>
14#include <string>
15
16namespace Mantid::Kernel {
17
18//---------------------------------------------------------------------------------------------------------------------------
19// NexusDescriptor public methods
20//---------------------------------------------------------------------------------------------------------------------------
30 : m_filename(), m_extension(), m_firstEntryNameType(), m_rootAttrs(), m_pathsToTypes(), m_file(nullptr) {
31 if (filename.empty()) {
32 throw std::invalid_argument("NexusDescriptor() - Empty filename '" + filename + "'");
33 }
34 if (!std::filesystem::exists(filename)) {
35 throw std::invalid_argument("NexusDescriptor() - File '" + filename + "' does not exist");
36 }
37 try {
38 // this is very expesive as it walk the entire file
40 } catch (Mantid::LegacyNexus::Exception &e) {
41 throw std::invalid_argument("NexusDescriptor::initialize - File '" + filename +
42 "' does not look like a HDF file.\n Error was: " + e.what());
43 }
44}
45
47
49const std::pair<std::string, std::string> &LegacyNexusDescriptor::firstEntryNameType() const {
51}
52
58bool LegacyNexusDescriptor::pathExists(const std::string &path) const {
59 return (m_pathsToTypes.find(path) != m_pathsToTypes.end());
60}
61
62//---------------------------------------------------------------------------------------------------------------------------
63// NexusDescriptor private methods
64//---------------------------------------------------------------------------------------------------------------------------
65
69void LegacyNexusDescriptor::initialize(const std::string &filename) {
71 m_extension = std::filesystem::path(filename).extension().string();
72
73 m_file = std::make_unique<Mantid::LegacyNexus::File>(this->filename());
74
75 m_file->openPath("/");
76 m_rootAttrs.clear();
77 m_pathsToTypes.clear();
78 walkFile(*m_file, "", "", m_pathsToTypes, 0);
79}
80
89void LegacyNexusDescriptor::walkFile(Mantid::LegacyNexus::File &file, const std::string &rootPath,
90 const std::string &className, std::map<std::string, std::string> &pmap,
91 int level) {
92 if (!rootPath.empty()) {
93 pmap.emplace(rootPath, className);
94 }
95
96 auto dirents = file.getEntries();
97 auto itend = dirents.end();
98 for (auto it = dirents.begin(); it != itend; ++it) {
99 const std::string &entryName = it->first;
100 const std::string &entryClass = it->second;
101 const std::string entryPath = std::string(rootPath).append("/").append(entryName);
102 if (entryClass == "SDS" || entryClass == "ILL_data_scan_vars" || entryClass == "NXill_data_scan_vars") {
103 pmap.emplace(entryPath, entryClass);
104 } else if (entryClass == "CDF0.0") {
105 // Do nothing with this
106 } else {
107 if (level == 0)
108 m_firstEntryNameType = (*it); // copy first entry name & type
109 if (!entryClass.empty()) { // handles buggy files
110 file.openGroup(entryName, entryClass);
111 walkFile(file, entryPath, entryClass, pmap, level + 1);
112 }
113 }
114 }
115 file.closeGroup();
116}
117
118} // namespace Mantid::Kernel
std::map< std::string, std::string > m_pathsToTypes
Map of full path strings to types. Can check if path exists quickly.
void walkFile(Mantid::LegacyNexus::File &file, const std::string &rootPath, const std::string &className, std::map< std::string, std::string > &pmap, int level)
Walk the tree and cache the structure.
std::pair< std::string, std::string > m_firstEntryNameType
First entry name/type.
std::unordered_set< std::string > m_rootAttrs
Root attributes.
LegacyNexusDescriptor()=delete
Disable default constructor.
const std::pair< std::string, std::string > & firstEntryNameType() const
Returns the name & type of the first entry in the file.
const std::string & filename() const
Access the filename.
std::unique_ptr< Mantid::LegacyNexus::File > m_file
Open NeXus handle.
void initialize(const std::string &filename)
Initialize object with filename.
bool pathExists(const std::string &path) const
Query if a path exists.
Generate a tableworkspace to store the calibration results.