-
Notifications
You must be signed in to change notification settings - Fork 122
/
LoadDaveGrp.cpp
248 lines (208 loc) · 8.05 KB
/
LoadDaveGrp.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
// 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 +
#include "MantidDataHandling/LoadDaveGrp.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/Progress.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/PropertyWithValue.h"
#include "MantidKernel/UnitFactory.h"
#include <vector>
namespace Mantid {
namespace DataHandling {
DECLARE_FILELOADER_ALGORITHM(LoadDaveGrp)
LoadDaveGrp::LoadDaveGrp() : ifile(), line(), nGroups(0), xLength(0) {}
/**
* 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 LoadDaveGrp::confidence(Kernel::FileDescriptor &descriptor) const {
const std::string &extn = descriptor.extension();
if (extn != ".grp" && extn != ".sqe" && extn != ".txt" && extn != ".dat")
return 0;
if (!descriptor.isAscii())
return 0;
bool daveGrp(false);
std::string curline;
// First line is a comment: #
std::getline(descriptor.data(), curline);
if (curline.substr(0, 1) == "#") {
daveGrp = true;
} else
return 0;
// Second line is an integer
std::getline(descriptor.data(), curline);
unsigned int value;
std::istringstream is(curline);
is >> value;
daveGrp = daveGrp && !is.fail();
// Third line is a comment: #
std::getline(descriptor.data(), curline);
if (curline.substr(0, 1) != "#")
return 0;
// Fourth line is an integer
std::getline(descriptor.data(), curline);
// Clear all stream bits regardless of what happened before
is.clear();
is.str(curline);
is >> value;
daveGrp = daveGrp && !is.fail();
if (daveGrp)
return 80;
else
return 0;
}
void LoadDaveGrp::init() {
std::vector<std::string> exts{".grp", ".sqe", ".txt", ".dat"};
declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::Load, exts),
"A DAVE grouped ASCII file");
declareProperty(std::make_unique<API::WorkspaceProperty<>>("OutputWorkspace", "", Kernel::Direction::Output),
"The name of the workspace that will be created.");
// Extract the current contents of the UnitFactory to be the allowed values
// of the X-Axis property
auto allowedUnits = std::make_shared<Kernel::StringListValidator>(Kernel::UnitFactory::Instance().getKeys());
declareProperty("XAxisUnits", "DeltaE", allowedUnits,
"The name of the units for the X-Axis (must be one of "
"those registered in "
"the Unit Factory)");
// Extract the current contents of the UnitFactory to be the allowed values
// of the Y-Axis property
declareProperty("YAxisUnits", "MomentumTransfer", allowedUnits,
"The name of the units for the Y-Axis (must be one of "
"those registered in "
"the Unit Factory)");
declareProperty(std::make_unique<Kernel::PropertyWithValue<bool>>("IsMicroEV", false, Kernel::Direction::Input),
"Original file is in units of micro-eV for DeltaE");
declareProperty(
std::make_unique<Kernel::PropertyWithValue<bool>>("ConvertToHistogram", false, Kernel::Direction::Input),
"Convert output workspace to histogram data.");
}
void LoadDaveGrp::exec() {
const std::string filename = getProperty("Filename");
std::vector<double> xAxis;
std::vector<double> yAxis;
API::MatrixWorkspace_sptr outputWorkspace;
ifile.open(filename.c_str());
if (ifile.is_open()) {
try {
// Size of x axis
getAxisLength(xLength);
// Size of y axis
getAxisLength(nGroups);
} catch (boost::bad_lexical_cast &) {
throw std::runtime_error("LoadDaveGrp: Failed to parse axis length from file.");
}
outputWorkspace = setupWorkspace();
// Read in the x axis values
getAxisValues(xAxis, xLength);
// Read in the y axis values
getAxisValues(yAxis, nGroups);
// Read in the data
getData(outputWorkspace);
ifile.close();
} else {
throw std::runtime_error("LoadDaveGrp: Failed to open file.");
}
// Scale the x-axis if it is in micro-eV to get it to meV
const bool isUeV = getProperty("IsMicroEV");
if (isUeV) {
for (auto &value : xAxis)
value /= 1000.0;
}
setWorkspaceAxes(outputWorkspace, xAxis, yAxis);
// convert output workspace to histogram data
const bool convertToHistogram = getProperty("ConvertToHistogram");
if (convertToHistogram)
outputWorkspace = convertWorkspaceToHistogram(outputWorkspace);
outputWorkspace->mutableRun().addProperty("Filename", filename);
setProperty("OutputWorkspace", outputWorkspace);
}
API::MatrixWorkspace_sptr LoadDaveGrp::setupWorkspace() const {
// Create workspace
API::MatrixWorkspace_sptr outputWorkspace = std::dynamic_pointer_cast<API::MatrixWorkspace>(
API::WorkspaceFactory::Instance().create("Workspace2D", nGroups, xLength, xLength));
// Force the workspace to be a distribution
outputWorkspace->setDistribution(true);
// Set the x-axis units
outputWorkspace->getAxis(0)->unit() = Kernel::UnitFactory::Instance().create(getProperty("XAxisUnits"));
auto verticalAxis = std::make_unique<API::NumericAxis>(nGroups);
// Set the y-axis units
verticalAxis->unit() = Kernel::UnitFactory::Instance().create(getProperty("YAxisUnits"));
outputWorkspace->replaceAxis(1, std::move(verticalAxis));
return outputWorkspace;
}
void LoadDaveGrp::setWorkspaceAxes(const API::MatrixWorkspace_sptr &workspace, const std::vector<double> &xAxis,
const std::vector<double> &yAxis) const {
auto verticalAxis = workspace->getAxis(1);
auto sharedX = Kernel::make_cow<HistogramData::HistogramX>(xAxis);
for (size_t i = 0; i < nGroups; i++) {
workspace->setSharedX(i, sharedX);
verticalAxis->setValue(i, yAxis.at(i));
}
}
API::MatrixWorkspace_sptr LoadDaveGrp::convertWorkspaceToHistogram(API::MatrixWorkspace_sptr workspace) {
auto convert2HistAlg = createChildAlgorithm("ConvertToHistogram");
convert2HistAlg->setProperty("InputWorkspace", workspace);
convert2HistAlg->setProperty("OutputWorkspace", workspace);
convert2HistAlg->execute();
workspace = convert2HistAlg->getProperty("OutputWorkspace");
auto convertFromDistAlg = createChildAlgorithm("ConvertFromDistribution");
convertFromDistAlg->setProperty("Workspace", workspace);
convertFromDistAlg->execute();
return workspace;
}
void LoadDaveGrp::readLine() { std::getline(ifile, line); }
void LoadDaveGrp::getAxisLength(size_t &length) {
// Skip a comment line
readLine();
// Get the axis length from the file
readLine();
std::istringstream is(line);
std::string strLength;
is >> strLength;
length = boost::lexical_cast<size_t>(strLength);
}
void LoadDaveGrp::getAxisValues(std::vector<double> &axis, const std::size_t length) {
// Skip a comment line
readLine();
// Get the axis values from the file
double value;
for (std::size_t i = 0; i < length; i++) {
readLine();
std::istringstream is(line);
is >> value;
axis.emplace_back(value);
}
}
void LoadDaveGrp::getData(const API::MatrixWorkspace_sptr &workspace) {
double data_val = 0.0;
double err_val = 0.0;
API::Progress progress(this, 0.0, 1.0, nGroups);
for (size_t j = 0; j < nGroups; j++) {
// Skip the group comment line
readLine();
// Read the data block
auto &dataY = workspace->mutableY(j);
auto &dataE = workspace->mutableE(j);
for (std::size_t k = 0; k < xLength; k++) {
readLine();
std::istringstream is(line);
is >> data_val >> err_val;
dataY[k] = data_val;
dataE[k] = err_val;
}
progress.report();
}
}
} // namespace DataHandling
} // namespace Mantid