Mantid
Loading...
Searching...
No Matches
LoadNexus.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 +
7// LoadNexus
8// @author Freddie Akeroyd, STFC ISIS Faility
9// @author Ronald Fowler, e_Science - updated to be wrapper to either
10// LoadMuonNeuxs or LoadNexusProcessed
11// Dropped the upper case X from Algorithm name (still in filenames)
12//----------------------------------------------------------------------
13// Includes
14//----------------------------------------------------------------------
21#include "MantidLegacyNexus/NeXusException.hpp"
22#include "MantidLegacyNexus/NeXusFile.hpp"
23#include "MantidLegacyNexus/NexusClasses.h"
24
25#include <cmath>
26#include <memory>
27
28namespace Mantid::DataHandling {
29
30const std::string LoadNexus::muonTD = "muonTD";
31const std::string LoadNexus::pulsedTD = "pulsedTD";
32
33// Register the algorithm into the algorithm factory
34DECLARE_ALGORITHM(LoadNexus)
35
36using namespace Kernel;
37using namespace API;
38using namespace DataObjects;
39
41LoadNexus::LoadNexus() : Algorithm(), m_filename() {}
42
47 // Declare required input parameters for all Child Algorithms
48 const std::vector<std::string> exts{".nxs", ".nx5", ".xml", ".n*"};
49 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
50 "The name of the Nexus file to read, as a full or relative path.");
51
52 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
53 "The name of the workspace to be created as the output of "
54 "the algorithm. A workspace of this name will be created "
55 "and stored in the Analysis Data Service. For multiperiod "
56 "files, one workspace will be generated for each period.");
57
58 // Declare optional input parameters
59 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
60 mustBePositive->setLower(0);
61 declareProperty("SpectrumMin", 1, mustBePositive, "Number of first spectrum to read, only for single period data.");
62 declareProperty("SpectrumMax", Mantid::EMPTY_INT(), mustBePositive,
63 "Number of last spectrum to read, only for single period data.");
64 declareProperty(std::make_unique<ArrayProperty<int>>("SpectrumList"),
65 "List of spectrum numbers to read, only for single period data.");
66
67 declareProperty("EntryNumber", 0, mustBePositive,
68 "0 indicates that every entry is loaded, into a separate "
69 "workspace within a group. "
70 "A positive number identifies one entry to be loaded, into "
71 "one workspace");
72}
73
80 // Retrieve the filename and output workspace name from the properties
81 m_filename = getPropertyValue("Filename");
82 m_workspace = getPropertyValue("OutputWorkspace");
83
84 // Test the file of the given file name as described in the
85 // documentation of this algorithm.
86
87 std::vector<std::string> entryName, definition;
88 int count = getNexusEntryTypes(m_filename, entryName, definition);
89 if (count <= -1) {
90 g_log.error("Error reading file " + m_filename);
91 throw Exception::FileError("Unable to read data in File:", m_filename);
92 } else if (count == 0) {
93 g_log.error("Error no entries found in " + m_filename);
94 throw Exception::FileError("Error no entries found in ", m_filename);
95 }
96 if (definition[0] == muonTD || definition[0] == pulsedTD) {
98 } else if (entryName[0] == "mantid_workspace_1") {
100 } else if (entryName[0] == "raw_data_1") {
102 } else {
103 Mantid::LegacyNexus::NXRoot root(m_filename);
104 Mantid::LegacyNexus::NXEntry entry = root.openEntry(root.groups().front().nxname);
105 try {
106 Mantid::LegacyNexus::NXChar nxc = entry.openNXChar("instrument/SNSdetector_calibration_id");
107 } catch (...) {
108 g_log.error("File " + m_filename + " is a currently unsupported type of NeXus file");
109 throw Exception::FileError("Unable to read File:", m_filename);
110 }
112 }
113}
114
116 // The Load algorithm will choose the correct LoadMuonNexus algorithm
117 auto loadMuonNexus = createChildAlgorithm("Load", 0., 1.);
118
119 // Pass through the same input filename
120 loadMuonNexus->setPropertyValue("Filename", m_filename);
121 // Set the workspace property
122 std::string outputWorkspace = "OutputWorkspace";
123 loadMuonNexus->setPropertyValue(outputWorkspace, m_workspace);
124 loadMuonNexus->setPropertyValue("DeadTimeTable", m_workspace + "_DeadTimeTable");
125 loadMuonNexus->setPropertyValue("DetectorGroupingTable", m_workspace + "DetectorGroupingTable");
126 loadMuonNexus->setPropertyValue("TimeZeroTable", m_workspace + "TimeZeroTable");
127
128 // Get the array passed in the spectrum_list, if an empty array was passed use
129 // the default
130 std::vector<int> specList = getProperty("SpectrumList");
131 if (!specList.empty())
132 loadMuonNexus->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
133 //
134 int specMax = getProperty("SpectrumMax");
135 if (specMax != Mantid::EMPTY_INT()) {
136 loadMuonNexus->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
137 loadMuonNexus->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
138 }
139 loadMuonNexus->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
140
141 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
142 // try
143 // {
144 loadMuonNexus->execute();
145 // }
146 // catch (std::runtime_error&)
147 // {
148 // g_log.error("Unable to successfully run LoadMuonNexus Child Algorithm");
149 // }
150 if (!loadMuonNexus->isExecuted())
151 g_log.error("Unable to successfully run LoadMuonNexus2 Child Algorithm");
152
153 setOutputWorkspace(loadMuonNexus);
154}
155
157 auto loadNexusPro = createChildAlgorithm("LoadNexusProcessed", 0., 1.);
158 // Pass through the same input filename
159 loadNexusPro->setPropertyValue("Filename", m_filename);
160 // Set the workspace property
161 loadNexusPro->setPropertyValue("OutputWorkspace", m_workspace);
162
163 loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
164 loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
165 loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
166
167 // Get the array passed in the spectrum_list, if an empty array was passed use
168 // the default
169
170 loadNexusPro->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
171 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
172 loadNexusPro->execute();
173 if (!loadNexusPro->isExecuted())
174 g_log.error("Unable to successfully run LoadNexusProcessed Child Algorithm");
175
176 setOutputWorkspace(loadNexusPro);
177}
178
180 auto loadNexusPro = createChildAlgorithm("LoadISISNexus", 0., 1.);
181 // Pass through the same input filename
182 loadNexusPro->setPropertyValue("Filename", m_filename);
183 // Set the workspace property
184 std::string outputWorkspace = "OutputWorkspace";
185 loadNexusPro->setPropertyValue(outputWorkspace, m_workspace);
186 // Get the array passed in the spectrum_list, if an empty array was passed use
187 // the default
188 std::vector<int> specList = getProperty("SpectrumList");
189 if (!specList.empty())
190 loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
191 //
192 int specMax = getProperty("SpectrumMax");
193 if (specMax != Mantid::EMPTY_INT()) {
194 loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
195 loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
196 }
197 loadNexusPro->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
198
199 loadNexusPro->execute();
200
201 if (!loadNexusPro->isExecuted())
202 g_log.error("Unable to successfully run LoadISISNexus Child Algorithm");
203
204 setOutputWorkspace(loadNexusPro);
205}
206
208 auto loadNexusPro = createChildAlgorithm("LoadTOFRawNexus", 0., 1.);
209 // Pass through the same input filename
210 loadNexusPro->setPropertyValue("Filename", m_filename);
211 // Set the workspace property
212 std::string outputWorkspace = "OutputWorkspace";
213 loadNexusPro->setPropertyValue(outputWorkspace, m_workspace);
214 // Get the array passed in the spectrum_list, if an empty array was passed use
215 // the default
216 std::vector<int> specList = getProperty("SpectrumList");
217 if (!specList.empty())
218 loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
219 //
220 int specMax = getProperty("SpectrumMax");
221 if (specMax != Mantid::EMPTY_INT()) {
222 loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
223 loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
224 }
225
226 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
227 try {
228 loadNexusPro->execute();
229 } catch (std::runtime_error &) {
230 g_log.error("Unable to successfully run LoadTOFRawNexus Child Algorithm");
231 }
232 if (!loadNexusPro->isExecuted())
233 g_log.error("Unable to successfully run LoadTOFRawNexus Child Algorithm");
234
235 setOutputWorkspace(loadNexusPro);
236}
237
244 // Go through each OutputWorkspace property and check whether we need to make
245 // a counterpart here
246 const std::vector<Property *> &loaderProps = loader->getProperties();
247 const size_t count = loader->propertyCount();
248 for (size_t i = 0; i < count; ++i) {
249 Property *prop = loaderProps[i];
250 if (dynamic_cast<IWorkspaceProperty *>(prop) && prop->direction() == Direction::Output) {
251 const std::string &propName = prop->name();
252 if (!this->existsProperty(propName)) {
253 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>(propName, loader->getPropertyValue(propName),
255 }
256 Workspace_sptr wkspace = loader->getProperty(propName);
257 setProperty(propName, wkspace);
258 }
259 }
260}
261
275int LoadNexus::getNexusEntryTypes(const std::string &fileName, std::vector<std::string> &entryName,
276 std::vector<std::string> &definition) {
277 std::unique_ptr<Mantid::LegacyNexus::File> fileH;
278
279 try {
280 fileH = std::make_unique<Mantid::LegacyNexus::File>(fileName);
281 } catch (Mantid::LegacyNexus::Exception &) {
282 return -1;
283 }
284 entryName.clear();
285 definition.clear();
286
287 //
288 // Loop through all entries looking for the definition section in each (or
289 // analysis for MuonV1)
290 //
291 std::vector<std::string> entryList;
292
293 std::pair<std::string, std::string> entry;
294 while (true) {
295 entry = fileH->getNextEntry();
296 if (entry == LegacyNexus::EOD_ENTRY)
297 break;
298
299 if (entry.second == "NXentry")
300 entryList.emplace_back(entry.first);
301 }
302
303 // for each entry found, look for "analysis" or "definition" text data fields
304 // and return value plus entry name
305
306 for (auto &item : entryList) {
307 fileH->openGroup(item, "NXentry");
308 // loop through field names in this entry
309 while (true) {
310 entry = fileH->getNextEntry();
311 if (entry == LegacyNexus::EOD_ENTRY)
312 break;
313 // if a data field
314 if (entry.second == "SDS") {
315 // if one of the two names we are looking for
316 if (entry.first == "definition" || entry.first == "analysis") {
317 std::string value;
318 fileH->readData(entry.first, value);
319 definition.emplace_back(value);
320 entryName.emplace_back(item);
321 }
322 }
323 }
324 fileH->closeGroup();
325 }
326
327 return (static_cast<int>(entryName.size()));
328}
329
330} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
double value
The value of the point.
Definition FitMW.cpp:51
int count
counter
Definition Matrix.cpp:37
Base class from which all concrete algorithm classes should be derived.
Definition Algorithm.h:76
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
bool existsProperty(const std::string &name) const override
Checks whether the named property is already in the list of managed property.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Kernel::Logger & g_log
Definition Algorithm.h:422
@ Load
allowed here which will be passed to the algorithm
An interface that is implemented by WorkspaceProperty.
A property class for workspaces.
std::string m_filename
The name and path of the input file.
Definition LoadNexus.h:80
void exec() override
Overwrites Algorithm method.
Definition LoadNexus.cpp:79
void runLoadIsisNexus()
run LoadIsisNexus
void runLoadNexusProcessed()
run LoadNexusProcessed
static int getNexusEntryTypes(const std::string &fileName, std::vector< std::string > &entryName, std::vector< std::string > &definition)
Get all the Nexus entry types for a file.
void runLoadTOFRawNexus()
run LoadTOFRawNexus
std::string m_workspace
The name of the output workspace.
Definition LoadNexus.h:83
LoadNexus()
Default constructor.
Definition LoadNexus.cpp:41
static const std::string muonTD
Definition LoadNexus.h:66
void setOutputWorkspace(const API::IAlgorithm_sptr &loader)
set the output workspaces from the child algorithms
void runLoadMuonNexus()
run LoadMuonNexus
void init() override
Overwrites Algorithm method.
Definition LoadNexus.cpp:46
static const std::string pulsedTD
Definition LoadNexus.h:67
Support for a property that holds an array of values.
Records the filename and the description of failure.
Definition Exception.h:98
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
Base class for properties.
Definition Property.h:94
unsigned int direction() const
returns the direction of the property
Definition Property.h:177
const std::string & name() const
Get the property's name.
Definition Property.cpp:61
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition EmptyValues.h:24
@ Output
An output workspace.
Definition Property.h:54