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//----------------------------------------------------------------------
23
24#include <cmath>
25#include <memory>
26
27namespace Mantid::DataHandling {
28
29const std::string LoadNexus::muonTD = "muonTD";
30const std::string LoadNexus::pulsedTD = "pulsedTD";
31
32// Register the algorithm into the algorithm factory
33DECLARE_ALGORITHM(LoadNexus)
34
35using namespace Kernel;
36using namespace API;
37using namespace DataObjects;
38
40LoadNexus::LoadNexus() : Algorithm(), m_filename() {}
41
46 // Declare required input parameters for all Child Algorithms
47 const std::vector<std::string> exts{".nxs", ".nx5", ".xml", ".n*"};
48 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
49 "The name of the Nexus file to read, as a full or relative path.");
50
51 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
52 "The name of the workspace to be created as the output of "
53 "the algorithm. A workspace of this name will be created "
54 "and stored in the Analysis Data Service. For multiperiod "
55 "files, one workspace will be generated for each period.");
56
57 // Declare optional input parameters
58 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
59 mustBePositive->setLower(0);
60 declareProperty("SpectrumMin", 1, mustBePositive, "Number of first spectrum to read, only for single period data.");
61 declareProperty("SpectrumMax", Mantid::EMPTY_INT(), mustBePositive,
62 "Number of last spectrum to read, only for single period data.");
63 declareProperty(std::make_unique<ArrayProperty<int>>("SpectrumList"),
64 "List of spectrum numbers to read, only for single period data.");
65
66 declareProperty("EntryNumber", 0, mustBePositive,
67 "0 indicates that every entry is loaded, into a separate "
68 "workspace within a group. "
69 "A positive number identifies one entry to be loaded, into "
70 "one workspace");
71}
72
79 // Retrieve the filename and output workspace name from the properties
80 m_filename = getPropertyValue("Filename");
81 m_workspace = getPropertyValue("OutputWorkspace");
82
83 // Test the file of the given file name as described in the
84 // documentation of this algorithm.
85
86 std::vector<std::string> entryName, definition;
87 int count = Mantid::NeXus::getNexusEntryTypes(m_filename, entryName, definition);
88 if (count <= -1) {
89 g_log.error("Error reading file " + m_filename);
90 throw Exception::FileError("Unable to read data in File:", m_filename);
91 } else if (count == 0) {
92 g_log.error("Error no entries found in " + m_filename);
93 throw Exception::FileError("Error no entries found in ", m_filename);
94 }
95 if (definition[0] == muonTD || definition[0] == pulsedTD) {
97 } else if (entryName[0] == "mantid_workspace_1") {
99 } else if (entryName[0] == "raw_data_1") {
101 } else {
103 Mantid::NeXus::NXEntry entry = root.openEntry(root.groups().front().nxname);
104 try {
105 Mantid::NeXus::NXChar nxc = entry.openNXChar("instrument/SNSdetector_calibration_id");
106 } catch (...) {
107 g_log.error("File " + m_filename + " is a currently unsupported type of NeXus file");
108 throw Exception::FileError("Unable to read File:", m_filename);
109 }
111 }
112}
113
115 auto loadMuonNexus = createChildAlgorithm("LoadMuonNexus", 0., 1.);
116 // Pass through the same input filename
117 loadMuonNexus->setPropertyValue("Filename", m_filename);
118 // Set the workspace property
119 std::string outputWorkspace = "OutputWorkspace";
120 loadMuonNexus->setPropertyValue(outputWorkspace, m_workspace);
121 loadMuonNexus->setPropertyValue("DeadTimeTable", m_workspace + "_DeadTimeTable");
122 loadMuonNexus->setPropertyValue("DetectorGroupingTable", m_workspace + "DetectorGroupingTable");
123 loadMuonNexus->setPropertyValue("TimeZeroTable", m_workspace + "TimeZeroTable");
124
125 // Get the array passed in the spectrum_list, if an empty array was passed use
126 // the default
127 std::vector<int> specList = getProperty("SpectrumList");
128 if (!specList.empty())
129 loadMuonNexus->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
130 //
131 int specMax = getProperty("SpectrumMax");
132 if (specMax != Mantid::EMPTY_INT()) {
133 loadMuonNexus->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
134 loadMuonNexus->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
135 }
136 loadMuonNexus->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
137
138 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
139 // try
140 // {
141 loadMuonNexus->execute();
142 // }
143 // catch (std::runtime_error&)
144 // {
145 // g_log.error("Unable to successfully run LoadMuonNexus Child Algorithm");
146 // }
147 if (!loadMuonNexus->isExecuted())
148 g_log.error("Unable to successfully run LoadMuonNexus2 Child Algorithm");
149
150 setOutputWorkspace(loadMuonNexus);
151}
152
154 auto loadNexusPro = createChildAlgorithm("LoadNexusProcessed", 0., 1.);
155 // Pass through the same input filename
156 loadNexusPro->setPropertyValue("Filename", m_filename);
157 // Set the workspace property
158 loadNexusPro->setPropertyValue("OutputWorkspace", m_workspace);
159
160 loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
161 loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
162 loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
163
164 // Get the array passed in the spectrum_list, if an empty array was passed use
165 // the default
166
167 loadNexusPro->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
168 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
169 loadNexusPro->execute();
170 if (!loadNexusPro->isExecuted())
171 g_log.error("Unable to successfully run LoadNexusProcessed Child Algorithm");
172
173 setOutputWorkspace(loadNexusPro);
174}
175
177 auto loadNexusPro = createChildAlgorithm("LoadISISNexus", 0., 1.);
178 // Pass through the same input filename
179 loadNexusPro->setPropertyValue("Filename", m_filename);
180 // Set the workspace property
181 std::string outputWorkspace = "OutputWorkspace";
182 loadNexusPro->setPropertyValue(outputWorkspace, m_workspace);
183 // Get the array passed in the spectrum_list, if an empty array was passed use
184 // the default
185 std::vector<int> specList = getProperty("SpectrumList");
186 if (!specList.empty())
187 loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
188 //
189 int specMax = getProperty("SpectrumMax");
190 if (specMax != Mantid::EMPTY_INT()) {
191 loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
192 loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
193 }
194 loadNexusPro->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
195
196 loadNexusPro->execute();
197
198 if (!loadNexusPro->isExecuted())
199 g_log.error("Unable to successfully run LoadISISNexus Child Algorithm");
200
201 setOutputWorkspace(loadNexusPro);
202}
203
205 auto loadNexusPro = createChildAlgorithm("LoadTOFRawNexus", 0., 1.);
206 // Pass through the same input filename
207 loadNexusPro->setPropertyValue("Filename", m_filename);
208 // Set the workspace property
209 std::string outputWorkspace = "OutputWorkspace";
210 loadNexusPro->setPropertyValue(outputWorkspace, m_workspace);
211 // Get the array passed in the spectrum_list, if an empty array was passed use
212 // the default
213 std::vector<int> specList = getProperty("SpectrumList");
214 if (!specList.empty())
215 loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
216 //
217 int specMax = getProperty("SpectrumMax");
218 if (specMax != Mantid::EMPTY_INT()) {
219 loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
220 loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
221 }
222
223 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
224 try {
225 loadNexusPro->execute();
226 } catch (std::runtime_error &) {
227 g_log.error("Unable to successfully run LoadTOFRawNexus Child Algorithm");
228 }
229 if (!loadNexusPro->isExecuted())
230 g_log.error("Unable to successfully run LoadTOFRawNexus Child Algorithm");
231
232 setOutputWorkspace(loadNexusPro);
233}
234
241 // Go through each OutputWorkspace property and check whether we need to make
242 // a counterpart here
243 const std::vector<Property *> &loaderProps = loader->getProperties();
244 const size_t count = loader->propertyCount();
245 for (size_t i = 0; i < count; ++i) {
246 Property *prop = loaderProps[i];
247 if (dynamic_cast<IWorkspaceProperty *>(prop) && prop->direction() == Direction::Output) {
248 const std::string &name = prop->name();
249 if (!this->existsProperty(name)) {
251 std::make_unique<WorkspaceProperty<Workspace>>(name, loader->getPropertyValue(name), Direction::Output));
252 }
253 Workspace_sptr wkspace = loader->getProperty(name);
254 setProperty(name, wkspace);
255 }
256 }
257}
258
259} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
int count
counter
Definition: Matrix.cpp:37
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:85
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
bool existsProperty(const std::string &name) const override
Checks whether the named property is already in the list of managed property.
Definition: Algorithm.cpp:2008
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
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.
Definition: Algorithm.cpp:842
Kernel::Logger & g_log
Definition: Algorithm.h:451
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
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:77
void exec() override
Overwrites Algorithm method.
Definition: LoadNexus.cpp:78
void runLoadIsisNexus()
run LoadIsisNexus
Definition: LoadNexus.cpp:176
void runLoadNexusProcessed()
run LoadNexusProcessed
Definition: LoadNexus.cpp:153
void runLoadTOFRawNexus()
run LoadTOFRawNexus
Definition: LoadNexus.cpp:204
std::string m_workspace
The name of the output workspace.
Definition: LoadNexus.h:80
LoadNexus()
Default constructor.
Definition: LoadNexus.cpp:40
static const std::string muonTD
Definition: LoadNexus.h:66
void setOutputWorkspace(const API::IAlgorithm_sptr &loader)
set the output workspaces from the child algorithms
Definition: LoadNexus.cpp:240
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
Definition: LoadNexus.h:48
void runLoadMuonNexus()
run LoadMuonNexus
Definition: LoadNexus.cpp:114
void init() override
Overwrites Algorithm method.
Definition: LoadNexus.cpp:45
static const std::string pulsedTD
Definition: LoadNexus.h:67
Support for a property that holds an array of values.
Definition: ArrayProperty.h:28
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:77
Base class for properties.
Definition: Property.h:94
unsigned int direction() const
returns the direction of the property
Definition: Property.h:172
const std::string & name() const
Get the property's name.
Definition: Property.cpp:60
std::vector< NXClassInfo > & groups() const
Returns a list of all classes (or groups) in this NXClass.
Definition: NexusClasses.h:589
NXChar openNXChar(const std::string &name) const
Creates and opens a char dataset.
Definition: NexusClasses.h:561
Templated class implementation of NXDataSet.
Definition: NexusClasses.h:203
Implements NXentry Nexus class.
Definition: NexusClasses.h:898
Implements NXroot Nexus class.
Definition: NexusClasses.h:922
NXEntry openEntry(const std::string &name)
Opens an entry – a topmost Nexus class.
Definition: NexusClasses.h:939
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
MANTID_NEXUS_DLL 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.
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition: EmptyValues.h:25
@ Output
An output workspace.
Definition: Property.h:54