-
Notifications
You must be signed in to change notification settings - Fork 122
/
LoadNexus.cpp
259 lines (228 loc) · 10.5 KB
/
LoadNexus.cpp
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
// 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 +
// LoadNexus
// @author Freddie Akeroyd, STFC ISIS Faility
// @author Ronald Fowler, e_Science - updated to be wrapper to either
// LoadMuonNeuxs or LoadNexusProcessed
// Dropped the upper case X from Algorithm name (still in filenames)
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/LoadNexus.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidNexus/NexusClasses.h"
#include "MantidNexus/NexusFileIO.h"
#include <cmath>
#include <memory>
namespace Mantid::DataHandling {
const std::string LoadNexus::muonTD = "muonTD";
const std::string LoadNexus::pulsedTD = "pulsedTD";
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadNexus)
using namespace Kernel;
using namespace API;
using namespace DataObjects;
/// Empty default constructor
LoadNexus::LoadNexus() : Algorithm(), m_filename() {}
/** Initialisation method.
*
*/
void LoadNexus::init() {
// Declare required input parameters for all Child Algorithms
const std::vector<std::string> exts{".nxs", ".nx5", ".xml", ".n*"};
declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
"The name of the Nexus file to read, as a full or relative path.");
declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("OutputWorkspace", "", Direction::Output),
"The name of the workspace to be created as the output of "
"the algorithm. A workspace of this name will be created "
"and stored in the Analysis Data Service. For multiperiod "
"files, one workspace will be generated for each period.");
// Declare optional input parameters
auto mustBePositive = std::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(0);
declareProperty("SpectrumMin", 1, mustBePositive, "Number of first spectrum to read, only for single period data.");
declareProperty("SpectrumMax", Mantid::EMPTY_INT(), mustBePositive,
"Number of last spectrum to read, only for single period data.");
declareProperty(std::make_unique<ArrayProperty<int>>("SpectrumList"),
"List of spectrum numbers to read, only for single period data.");
declareProperty("EntryNumber", 0, mustBePositive,
"0 indicates that every entry is loaded, into a separate "
"workspace within a group. "
"A positive number identifies one entry to be loaded, into "
"one workspace");
}
/** Executes the algorithm. Reading in the file and creating and populating
* the output workspace
*
* @throw runtime_error Thrown if algorithm cannot execute
*/
void LoadNexus::exec() {
// Retrieve the filename and output workspace name from the properties
m_filename = getPropertyValue("Filename");
m_workspace = getPropertyValue("OutputWorkspace");
// Test the file of the given file name as described in the
// documentation of this algorithm.
std::vector<std::string> entryName, definition;
int count = Mantid::NeXus::getNexusEntryTypes(m_filename, entryName, definition);
if (count <= -1) {
g_log.error("Error reading file " + m_filename);
throw Exception::FileError("Unable to read data in File:", m_filename);
} else if (count == 0) {
g_log.error("Error no entries found in " + m_filename);
throw Exception::FileError("Error no entries found in ", m_filename);
}
if (definition[0] == muonTD || definition[0] == pulsedTD) {
runLoadMuonNexus();
} else if (entryName[0] == "mantid_workspace_1") {
runLoadNexusProcessed();
} else if (entryName[0] == "raw_data_1") {
runLoadIsisNexus();
} else {
Mantid::NeXus::NXRoot root(m_filename);
Mantid::NeXus::NXEntry entry = root.openEntry(root.groups().front().nxname);
try {
Mantid::NeXus::NXChar nxc = entry.openNXChar("instrument/SNSdetector_calibration_id");
} catch (...) {
g_log.error("File " + m_filename + " is a currently unsupported type of NeXus file");
throw Exception::FileError("Unable to read File:", m_filename);
}
runLoadTOFRawNexus();
}
}
void LoadNexus::runLoadMuonNexus() {
auto loadMuonNexus = createChildAlgorithm("LoadMuonNexus", 0., 1.);
// Pass through the same input filename
loadMuonNexus->setPropertyValue("Filename", m_filename);
// Set the workspace property
std::string outputWorkspace = "OutputWorkspace";
loadMuonNexus->setPropertyValue(outputWorkspace, m_workspace);
loadMuonNexus->setPropertyValue("DeadTimeTable", m_workspace + "_DeadTimeTable");
loadMuonNexus->setPropertyValue("DetectorGroupingTable", m_workspace + "DetectorGroupingTable");
loadMuonNexus->setPropertyValue("TimeZeroTable", m_workspace + "TimeZeroTable");
// Get the array passed in the spectrum_list, if an empty array was passed use
// the default
std::vector<int> specList = getProperty("SpectrumList");
if (!specList.empty())
loadMuonNexus->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
//
int specMax = getProperty("SpectrumMax");
if (specMax != Mantid::EMPTY_INT()) {
loadMuonNexus->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
loadMuonNexus->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
}
loadMuonNexus->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
// Now execute the Child Algorithm. Catch and log any error, but don't stop.
// try
// {
loadMuonNexus->execute();
// }
// catch (std::runtime_error&)
// {
// g_log.error("Unable to successfully run LoadMuonNexus Child Algorithm");
// }
if (!loadMuonNexus->isExecuted())
g_log.error("Unable to successfully run LoadMuonNexus2 Child Algorithm");
setOutputWorkspace(loadMuonNexus);
}
void LoadNexus::runLoadNexusProcessed() {
auto loadNexusPro = createChildAlgorithm("LoadNexusProcessed", 0., 1.);
// Pass through the same input filename
loadNexusPro->setPropertyValue("Filename", m_filename);
// Set the workspace property
loadNexusPro->setPropertyValue("OutputWorkspace", m_workspace);
loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
// Get the array passed in the spectrum_list, if an empty array was passed use
// the default
loadNexusPro->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
// Now execute the Child Algorithm. Catch and log any error, but don't stop.
loadNexusPro->execute();
if (!loadNexusPro->isExecuted())
g_log.error("Unable to successfully run LoadNexusProcessed Child Algorithm");
setOutputWorkspace(loadNexusPro);
}
void LoadNexus::runLoadIsisNexus() {
auto loadNexusPro = createChildAlgorithm("LoadISISNexus", 0., 1.);
// Pass through the same input filename
loadNexusPro->setPropertyValue("Filename", m_filename);
// Set the workspace property
std::string outputWorkspace = "OutputWorkspace";
loadNexusPro->setPropertyValue(outputWorkspace, m_workspace);
// Get the array passed in the spectrum_list, if an empty array was passed use
// the default
std::vector<int> specList = getProperty("SpectrumList");
if (!specList.empty())
loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
//
int specMax = getProperty("SpectrumMax");
if (specMax != Mantid::EMPTY_INT()) {
loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
}
loadNexusPro->setPropertyValue("EntryNumber", getPropertyValue("EntryNumber"));
loadNexusPro->execute();
if (!loadNexusPro->isExecuted())
g_log.error("Unable to successfully run LoadISISNexus Child Algorithm");
setOutputWorkspace(loadNexusPro);
}
void LoadNexus::runLoadTOFRawNexus() {
auto loadNexusPro = createChildAlgorithm("LoadTOFRawNexus", 0., 1.);
// Pass through the same input filename
loadNexusPro->setPropertyValue("Filename", m_filename);
// Set the workspace property
std::string outputWorkspace = "OutputWorkspace";
loadNexusPro->setPropertyValue(outputWorkspace, m_workspace);
// Get the array passed in the spectrum_list, if an empty array was passed use
// the default
std::vector<int> specList = getProperty("SpectrumList");
if (!specList.empty())
loadNexusPro->setPropertyValue("SpectrumList", getPropertyValue("SpectrumList"));
//
int specMax = getProperty("SpectrumMax");
if (specMax != Mantid::EMPTY_INT()) {
loadNexusPro->setPropertyValue("SpectrumMax", getPropertyValue("SpectrumMax"));
loadNexusPro->setPropertyValue("SpectrumMin", getPropertyValue("SpectrumMin"));
}
// Now execute the Child Algorithm. Catch and log any error, but don't stop.
try {
loadNexusPro->execute();
} catch (std::runtime_error &) {
g_log.error("Unable to successfully run LoadTOFRawNexus Child Algorithm");
}
if (!loadNexusPro->isExecuted())
g_log.error("Unable to successfully run LoadTOFRawNexus Child Algorithm");
setOutputWorkspace(loadNexusPro);
}
/**
* Set the output workspace(s) if the load's return workspace has type
* API::Workspace
* @param loader :: Shared pointer to load algorithm
*/
void LoadNexus::setOutputWorkspace(const API::IAlgorithm_sptr &loader) {
// Go through each OutputWorkspace property and check whether we need to make
// a counterpart here
const std::vector<Property *> &loaderProps = loader->getProperties();
const size_t count = loader->propertyCount();
for (size_t i = 0; i < count; ++i) {
Property *prop = loaderProps[i];
if (dynamic_cast<IWorkspaceProperty *>(prop) && prop->direction() == Direction::Output) {
const std::string &name = prop->name();
if (!this->existsProperty(name)) {
declareProperty(
std::make_unique<WorkspaceProperty<Workspace>>(name, loader->getPropertyValue(name), Direction::Output));
}
Workspace_sptr wkspace = loader->getProperty(name);
setProperty(name, wkspace);
}
}
}
} // namespace Mantid::DataHandling