-
Notifications
You must be signed in to change notification settings - Fork 122
/
LoadMcStasNexus.cpp
179 lines (150 loc) · 6.03 KB
/
LoadMcStasNexus.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
#include "MantidDataHandling/LoadMcStasNexus.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidKernel/Unit.h"
#include <nexus/NeXusException.hpp>
#include <nexus/NeXusFile.hpp>
#include <boost/algorithm/string.hpp>
namespace Mantid {
namespace DataHandling {
using namespace Kernel;
using namespace API;
DECLARE_NEXUS_FILELOADER_ALGORITHM(LoadMcStasNexus)
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string LoadMcStasNexus::name() const { return "LoadMcStasNexus"; }
/// Algorithm's version for identification. @see Algorithm::version
int LoadMcStasNexus::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string LoadMcStasNexus::category() const {
return "DataHandling\\Nexus";
}
/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not
* be used
*/
int LoadMcStasNexus::confidence(Kernel::NexusDescriptor &descriptor) const {
UNUSED_ARG(descriptor)
// To ensure that this loader is somewhat hitten return 0
int confidence(0);
return confidence;
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void LoadMcStasNexus::init() {
const std::vector<std::string> exts{".h5", ".nxs"};
declareProperty(Kernel::make_unique<FileProperty>("Filename", "",
FileProperty::Load, exts),
"The name of the Nexus file to load");
declareProperty(make_unique<WorkspaceProperty<Workspace>>(
"OutputWorkspace", "", Direction::Output),
"An output workspace.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void LoadMcStasNexus::exec() {
std::string filename = getPropertyValue("Filename");
g_log.debug() << "Opening file " << filename << '\n';
::NeXus::File nxFile(filename);
auto entries = nxFile.getEntries();
auto itend = entries.end();
WorkspaceGroup_sptr outputGroup(new WorkspaceGroup);
for (auto it = entries.begin(); it != itend; ++it) {
std::string name = it->first;
std::string type = it->second;
nxFile.openGroup(name, type);
auto dataEntries = nxFile.getEntries();
for (auto &dataEntry : dataEntries) {
const std::string &dataName = dataEntry.first;
const std::string &dataType = dataEntry.second;
if (dataName == "content_nxs" || dataType != "NXdata")
continue;
g_log.debug() << "Opening " << dataName << " " << dataType << '\n';
nxFile.openGroup(dataName, dataType);
// Find the axis names
auto nxdataEntries = nxFile.getEntries();
std::string axis1Name, axis2Name;
for (auto &nxdataEntry : nxdataEntries) {
if (nxdataEntry.second == "NXparameters")
continue;
nxFile.openData(nxdataEntry.first);
if (nxFile.hasAttr("axis")) {
int axisNo(0);
nxFile.getAttr("axis", axisNo);
if (axisNo == 1)
axis1Name = nxdataEntry.first;
else if (axisNo == 2)
axis2Name = nxdataEntry.first;
else
throw std::invalid_argument("Unknown axis number");
}
nxFile.closeData();
}
std::vector<double> axis1Values, axis2Values;
nxFile.readData<double>(axis1Name, axis1Values);
nxFile.readData<double>(axis2Name, axis2Values);
const size_t axis1Length = axis1Values.size();
const size_t axis2Length = axis2Values.size();
g_log.debug() << "Axis lengths=" << axis1Length << " " << axis2Length
<< '\n';
// Require "data" field
std::vector<double> data;
nxFile.readData<double>("data", data);
// Optional errors field
std::vector<double> errors;
try {
nxFile.readData<double>("errors", errors);
} catch (::NeXus::Exception &) {
g_log.information() << "Field " << dataName
<< " contains no error information.\n";
}
MatrixWorkspace_sptr ws = WorkspaceFactory::Instance().create(
"Workspace2D", axis2Length, axis1Length, axis1Length);
Axis *axis1 = ws->getAxis(0);
axis1->title() = axis1Name;
// Set caption
auto lblUnit = boost::make_shared<Units::Label>();
lblUnit->setLabel(axis1Name, "");
axis1->unit() = lblUnit;
Axis *axis2 = new NumericAxis(axis2Length);
axis2->title() = axis2Name;
// Set caption
lblUnit = boost::make_shared<Units::Label>();
lblUnit->setLabel(axis2Name, "");
axis2->unit() = lblUnit;
ws->setYUnit(axis2Name);
ws->replaceAxis(1, axis2);
for (size_t wsIndex = 0; wsIndex < axis2Length; ++wsIndex) {
auto &dataY = ws->dataY(wsIndex);
auto &dataE = ws->dataE(wsIndex);
auto &dataX = ws->dataX(wsIndex);
for (size_t j = 0; j < axis1Length; ++j) {
// Data is stored in column-major order so we are translating to
// row major for Mantid
const size_t fileDataIndex = j * axis2Length + wsIndex;
dataY[j] = data[fileDataIndex];
dataX[j] = axis1Values[j];
if (!errors.empty())
dataE[j] = errors[fileDataIndex];
}
axis2->setValue(wsIndex, axis2Values[wsIndex]);
}
// Make Mantid store the workspace in the group
outputGroup->addWorkspace(ws);
nxFile.closeGroup();
}
nxFile.closeGroup();
}
setProperty("OutputWorkspace", outputGroup);
}
} // namespace DataHandling
} // namespace Mantid