-
Notifications
You must be signed in to change notification settings - Fork 124
/
LoadNXSPE.cpp
411 lines (357 loc) · 12.6 KB
/
LoadNXSPE.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "MantidDataHandling/LoadNXSPE.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/DeltaEMode.h"
#include "MantidAPI/ExperimentInfo.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidAPI/SpectrumInfo.h"
#include "MantidAPI/WorkspaceFactory.h"
#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
#include "MantidNexus/NexusClasses.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidGeometry/Instrument/Goniometer.h"
#include "MantidGeometry/Surfaces/Plane.h"
#include "MantidGeometry/Surfaces/Sphere.h"
#include <boost/regex.hpp>
#include <Poco/File.h>
#include <map>
#include <sstream>
#include <string>
#include <vector>
namespace Mantid {
namespace DataHandling {
DECLARE_NEXUS_FILELOADER_ALGORITHM(LoadNXSPE)
using namespace Mantid::Kernel;
using namespace Mantid::API;
/**
* Calculate the confidence in the string value. This is used for file
* identification.
* @param value
* @return confidence 0 - 100%
*/
int LoadNXSPE::identiferConfidence(const std::string &value) {
int confidence = 0;
if (value.compare("NXSPE") == 0) {
confidence = 99;
} else {
boost::regex re("^NXSP", boost::regex::icase);
if (boost::regex_match(value, re)) {
confidence = 95;
}
}
return confidence;
}
/**
* 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 LoadNXSPE::confidence(Kernel::NexusDescriptor &descriptor) const {
int confidence(0);
typedef std::map<std::string, std::string> string_map_t;
try {
::NeXus::File file = ::NeXus::File(descriptor.filename());
string_map_t entries = file.getEntries();
for (string_map_t::const_iterator it = entries.begin(); it != entries.end();
++it) {
if (it->second == "NXentry") {
file.openGroup(it->first, it->second);
file.openData("definition");
const std::string value = file.getStrData();
confidence = identiferConfidence(value);
}
}
} catch (::NeXus::Exception &) {
}
return confidence;
}
/** Initialize the algorithm's properties.
*/
void LoadNXSPE::init() {
const std::vector<std::string> exts{".nxspe", ""};
declareProperty(Kernel::make_unique<FileProperty>("Filename", "",
FileProperty::Load, exts),
"An NXSPE file");
declareProperty(make_unique<WorkspaceProperty<>>("OutputWorkspace", "",
Direction::Output),
"The name of the workspace that will be created.");
}
/** Execute the algorithm.
*/
void LoadNXSPE::exec() {
std::string filename = getProperty("Filename");
// quicly check if it's really nxspe
try {
::NeXus::File file(filename);
std::string mainEntry = (*(file.getEntries().begin())).first;
file.openGroup(mainEntry, "NXentry");
file.openData("definition");
if (identiferConfidence(file.getStrData()) < 1) {
throw std::invalid_argument("Not NXSPE");
}
file.close();
} catch (...) {
throw std::invalid_argument("Not NeXus or not NXSPE");
}
// Load the data
::NeXus::File file(filename);
std::string mainEntry = (*(file.getEntries().begin())).first;
file.openGroup(mainEntry, "NXentry");
file.openGroup("NXSPE_info", "NXcollection");
std::map<std::string, std::string> entries = file.getEntries();
std::vector<double> temporary;
double fixed_energy, psi = 0.;
if (!entries.count("fixed_energy")) {
throw std::invalid_argument("fixed_energy field was not found");
}
file.openData("fixed_energy");
file.getData(temporary);
fixed_energy = temporary.at(0);
file.closeData();
if (entries.count("psi")) {
file.openData("psi");
file.getData(temporary);
psi = temporary.at(0);
file.closeData();
}
int kikfscaling = 0;
if (entries.count("ki_over_kf_scaling")) {
file.openData("ki_over_kf_scaling");
std::vector<int> temporaryint;
file.getData(temporaryint);
kikfscaling = temporaryint.at(0);
file.closeData();
}
file.closeGroup(); // NXSPE_Info
file.openGroup("data", "NXdata");
entries = file.getEntries();
if (!entries.count("data")) {
throw std::invalid_argument("data field was not found");
}
file.openData("data");
::NeXus::Info info = file.getInfo();
std::size_t numSpectra = static_cast<std::size_t>(info.dims.at(0));
std::size_t numBins = static_cast<std::size_t>(info.dims.at(1));
std::vector<double> data;
file.getData(data);
file.closeData();
if (!entries.count("error")) {
throw std::invalid_argument("error field was not found");
}
file.openData("error");
std::vector<double> error;
file.getData(error);
file.closeData();
if (!entries.count("energy")) {
throw std::invalid_argument("energy field was not found");
}
file.openData("energy");
std::vector<double> energies;
file.getData(energies);
file.closeData();
if (!entries.count("azimuthal")) {
throw std::invalid_argument("azimuthal field was not found");
}
file.openData("azimuthal");
std::vector<double> azimuthal;
file.getData(azimuthal);
file.closeData();
if (!entries.count("azimuthal_width")) {
throw std::invalid_argument("azimuthal_width field was not found");
}
file.openData("azimuthal_width");
std::vector<double> azimuthal_width;
file.getData(azimuthal_width);
file.closeData();
if (!entries.count("polar")) {
throw std::invalid_argument("polar field was not found");
}
file.openData("polar");
std::vector<double> polar;
file.getData(polar);
file.closeData();
if (!entries.count("polar_width")) {
throw std::invalid_argument("polar_width field was not found");
}
file.openData("polar_width");
std::vector<double> polar_width;
file.getData(polar_width);
file.closeData();
// distance might not have been saved in all NXSPE files
std::vector<double> distance;
if (entries.count("distance")) {
file.openData("distance");
file.getData(distance);
file.closeData();
}
file.closeGroup(); // data group
file.openGroup("instrument", "NXinstrument");
entries = file.getEntries();
std::string instrument_name;
if (entries.count("name")) {
file.openData("name");
instrument_name = file.getStrData();
file.closeData();
}
file.closeGroup(); // instrument group
file.closeGroup(); // Main entry
file.close();
// check if dimensions of the vectors are correct
if ((error.size() != data.size()) || (azimuthal.size() != numSpectra) ||
(azimuthal_width.size() != numSpectra) || (polar.size() != numSpectra) ||
(polar_width.size() != numSpectra) ||
((energies.size() != numBins) && (energies.size() != numBins + 1))) {
throw std::invalid_argument(
"incompatible sizes of fields in the NXSPE file");
}
MatrixWorkspace_sptr outputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(
WorkspaceFactory::Instance().create("Workspace2D", numSpectra,
energies.size(), numBins));
// Need to get hold of the parameter map
outputWS->getAxis(0)->unit() = UnitFactory::Instance().create("DeltaE");
outputWS->setYUnit("SpectraNumber");
// add logs
outputWS->mutableRun().addLogData(
new PropertyWithValue<double>("Ei", fixed_energy));
outputWS->mutableRun().addLogData(new PropertyWithValue<double>("psi", psi));
outputWS->mutableRun().addLogData(new PropertyWithValue<std::string>(
"ki_over_kf_scaling", kikfscaling == 1 ? "true" : "false"));
// Set Goniometer
Geometry::Goniometer gm;
gm.pushAxis("psi", 0, 1, 0, psi);
outputWS->mutableRun().setGoniometer(gm, true);
// generate instrument
Geometry::Instrument_sptr instrument(new Geometry::Instrument(
instrument_name.empty() ? "NXSPE" : instrument_name));
Geometry::ObjComponent *source = new Geometry::ObjComponent("source");
source->setPos(0.0, 0.0, -10.);
instrument->add(source);
instrument->markAsSource(source);
Geometry::ObjComponent *sample = new Geometry::ObjComponent("sample");
instrument->add(sample);
instrument->markAsSamplePos(sample);
Geometry::Object_const_sptr cuboid(
createCuboid(0.1, 0.1, 0.1)); // FIXME: memory hog on rendering. Also,
// make each detector separate size
for (std::size_t i = 0; i < numSpectra; ++i) {
double r = 1.0;
if (!distance.empty()) {
r = distance.at(i);
}
Kernel::V3D pos;
pos.spherical(r, polar.at(i), azimuthal.at(i));
Geometry::Detector *det =
new Geometry::Detector("pixel", static_cast<int>(i + 1), sample);
det->setPos(pos);
det->setShape(cuboid);
instrument->add(det);
instrument->markAsDetector(det);
}
outputWS->setInstrument(instrument);
std::vector<double>::iterator itdata = data.begin(), iterror = error.begin(),
itdataend, iterrorend;
auto &spectrumInfo = outputWS->mutableSpectrumInfo();
API::Progress prog = API::Progress(this, 0.0, 0.9, numSpectra);
for (std::size_t i = 0; i < numSpectra; ++i) {
itdataend = itdata + numBins;
iterrorend = iterror + numBins;
outputWS->dataX(i) = energies;
if ((!std::isfinite(*itdata)) || (*itdata <= -1e10)) // masked bin
{
spectrumInfo.setMasked(i, true);
} else {
outputWS->dataY(i) = std::vector<double>(itdata, itdataend);
outputWS->dataE(i) = std::vector<double>(iterror, iterrorend);
}
itdata = (itdataend);
iterror = (iterrorend);
prog.report();
}
// If an instrument name is defined, load instrument parameter file for Emode
// NB. LoadParameterFile must be used on a workspace with an instrument
if (!instrument_name.empty() && instrument_name != "NXSPE") {
std::string IDF_filename =
ExperimentInfo::getInstrumentFilename(instrument_name);
std::string instrument_parfile =
IDF_filename.substr(0, IDF_filename.find("_Definition")) +
"_Parameters.xml";
if (Poco::File(instrument_parfile).exists()) {
try {
IAlgorithm_sptr loadParamAlg =
createChildAlgorithm("LoadParameterFile");
loadParamAlg->setProperty("Filename", instrument_parfile);
loadParamAlg->setProperty("Workspace", outputWS);
loadParamAlg->execute();
} catch (...) {
g_log.information("Cannot load the instrument parameter file.");
}
}
}
setProperty("OutputWorkspace", outputWS);
}
Geometry::Object_sptr LoadNXSPE::createCuboid(double dx, double dy, double dz) {
dx = 0.5 * std::fabs(dx);
dy = 0.5 * std::fabs(dy);
dz = 0.5 * std::fabs(dz);
/*
std::stringstream planeName;
planeName.str("px ");planeName<<-dx;
std::string C1=planeName.str();
planeName.str("px ");planeName<<dx;
std::string C2=planeName.str();
planeName.str("px ");planeName<<-dy;
std::string C3=planeName.str();
planeName.str("px ");planeName<<dy;
std::string C4=planeName.str();
planeName.str("px ");planeName<<-dz;
std::string C5=planeName.str();
planeName.str("px ");planeName<<dz;
std::string C6=planeName.str();
// Create surfaces
std::map<int,Geometry::Surface*> CubeSurMap;
CubeSurMap[1]=new Geometry::Plane();
CubeSurMap[2]=new Geometry::Plane();
CubeSurMap[3]=new Geometry::Plane();
CubeSurMap[4]=new Geometry::Plane();
CubeSurMap[5]=new Geometry::Plane();
CubeSurMap[6]=new Geometry::Plane();
CubeSurMap[1]->setSurface(C1);
CubeSurMap[2]->setSurface(C2);
CubeSurMap[3]->setSurface(C3);
CubeSurMap[4]->setSurface(C4);
CubeSurMap[5]->setSurface(C5);
CubeSurMap[6]->setSurface(C6);
CubeSurMap[1]->setName(1);
CubeSurMap[2]->setName(2);
CubeSurMap[3]->setName(3);
CubeSurMap[4]->setName(4);
CubeSurMap[5]->setName(5);
CubeSurMap[6]->setName(6);
// Cube (id 68)
// using surface ids: 1-6
std::string ObjCube="1 -2 3 -4 5 -6";
Geometry::Object_sptr retVal = Geometry::Object_sptr(new Geometry::Object);
retVal->setObject(68,ObjCube);
retVal->populate(CubeSurMap);
*/
std::string S41 = "so 0.01"; // Sphere at origin radius 0.01
// First create some surfaces
std::map<int, boost::shared_ptr<Geometry::Surface>> SphSurMap;
SphSurMap[41] = boost::make_shared<Geometry::Sphere>();
SphSurMap[41]->setSurface(S41);
SphSurMap[41]->setName(41);
// A sphere
std::string ObjSphere = "-41";
Geometry::Object_sptr retVal = boost::make_shared<Geometry::Object>();
retVal->setObject(41, ObjSphere);
retVal->populate(SphSurMap);
return retVal;
}
} // namespace Mantid
} // namespace DataHandling