-
Notifications
You must be signed in to change notification settings - Fork 121
/
SaveGDA.cpp
277 lines (229 loc) · 10.1 KB
/
SaveGDA.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
// 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/SaveGDA.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/WorkspaceProperty.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/Unit.h"
#include <boost/optional.hpp>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <sstream>
namespace Mantid {
namespace DataHandling {
using namespace API;
namespace { // helper functions
const int POINTS_PER_LINE = 4;
double mean(const std::vector<double> &values) {
return std::accumulate(values.begin(), values.end(), 0.0) /
static_cast<double>(values.size());
}
// Compute the mean resolution of the x axis of the input workspace
// Resolution is calculated as the difference between adjacent pairs of values,
// normalised by the second of the two
double computeAverageDeltaTByT(const HistogramData::HistogramX &tValues) {
std::vector<double> deltaTByT;
deltaTByT.reserve(tValues.size() - 1);
std::adjacent_difference(tValues.begin(), tValues.end(),
std::back_inserter(deltaTByT),
[](const double previous, const double current) {
return (previous - current) / current;
});
// First element is just first element of tValues, so remove it
deltaTByT.erase(deltaTByT.begin());
return mean(deltaTByT);
}
std::string generateBankHeader(int bank, int minT, size_t numberBins,
double deltaTByT) {
std::stringstream stream;
const auto numberLines = static_cast<size_t>(
std::ceil(static_cast<double>(numberBins) / POINTS_PER_LINE));
stream << std::setprecision(2) << "BANK " << bank << " " << numberBins << " "
<< numberLines << " RALF " << minT << " 96 " << minT << " "
<< deltaTByT << " ALT";
return stream.str();
}
boost::optional<std::vector<std::string>>
getParamLinesFromGSASFile(const std::string ¶msFilename) {
// ICONS signifies that a line contains TOF to D conversion factors
const static std::string paramLineDelimiter = "ICONS";
std::ifstream paramsFile;
paramsFile.open(paramsFilename);
if (paramsFile.is_open()) {
std::vector<std::string> paramLines;
std::string line;
while (std::getline(paramsFile, line)) {
if (line.find(paramLineDelimiter) != std::string::npos) {
paramLines.emplace_back(line);
}
}
return paramLines;
} else {
return boost::none;
}
}
} // anonymous namespace
DECLARE_ALGORITHM(SaveGDA)
SaveGDA::CalibrationParams::CalibrationParams(const double _difa,
const double _difc,
const double _tzero)
: difa(_difa), difc(_difc), tzero(_tzero) {}
const std::string SaveGDA::name() const { return "SaveGDA"; }
const std::string SaveGDA::summary() const {
return "Save a group of focused banks to the MAUD three-column GDA format";
}
int SaveGDA::version() const { return 1; }
const std::vector<std::string> SaveGDA::seeAlso() const {
return {"SaveBankScatteringAngles", "AlignDetectors"};
}
const std::string SaveGDA::category() const {
return "DataHandling\\Text;Diffraction\\DataHandling";
}
const std::string SaveGDA::PROP_OUTPUT_FILENAME = "OutputFilename";
const std::string SaveGDA::PROP_INPUT_WS = "InputWorkspace";
const std::string SaveGDA::PROP_PARAMS_FILENAME = "GSASParamFile";
const std::string SaveGDA::PROP_GROUPING_SCHEME = "GroupingScheme";
void SaveGDA::init() {
declareProperty(std::make_unique<WorkspaceProperty<WorkspaceGroup>>(
PROP_INPUT_WS, "", Kernel::Direction::Input),
"A GroupWorkspace where every sub-workspace is a "
"single-spectra focused run corresponding to a particular "
"bank");
const static std::vector<std::string> outExts{".gda"};
declareProperty(std::make_unique<FileProperty>(PROP_OUTPUT_FILENAME, "",
FileProperty::Save, outExts),
"The name of the file to save to");
const static std::vector<std::string> paramsExts{".ipf", ".prm", ".parm",
".iprm"};
declareProperty(
std::make_unique<FileProperty>(PROP_PARAMS_FILENAME, "",
FileProperty::Load, paramsExts),
"GSAS calibration file containing conversion factors from D to TOF");
declareProperty(
std::make_unique<Kernel::ArrayProperty<int>>(PROP_GROUPING_SCHEME),
"An array of bank IDs, where the value at element i is the "
"ID of the bank in " +
PROP_PARAMS_FILENAME + " to associate spectrum i with");
}
void SaveGDA::exec() {
const std::string filename = getProperty(PROP_OUTPUT_FILENAME);
std::ofstream outFile(filename.c_str());
if (!outFile) {
throw Kernel::Exception::FileError("Unable to create file: ", filename);
}
outFile << std::fixed << std::setprecision(0) << std::setfill(' ');
const API::WorkspaceGroup_sptr inputWS = getProperty(PROP_INPUT_WS);
const auto calibParams = parseParamsFile();
const std::vector<int> groupingScheme = getProperty(PROP_GROUPING_SCHEME);
for (int i = 0; i < inputWS->getNumberOfEntries(); ++i) {
const auto ws = inputWS->getItem(i);
const auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
const auto &d = matrixWS->x(0);
const size_t bankIndex(groupingScheme[i] - 1);
if (bankIndex >= calibParams.size()) {
throw Kernel::Exception::IndexError(bankIndex, calibParams.size(),
"Bank number out of range");
}
const auto &bankCalibParams = calibParams[bankIndex];
// For historic reasons, TOF is scaled by 32 in MAUD
const static double tofScale = 32;
std::vector<double> tofScaled;
tofScaled.reserve(d.size());
std::transform(d.begin(), d.end(), std::back_inserter(tofScaled),
[&bankCalibParams](const double dVal) {
return (dVal * bankCalibParams.difa +
dVal * dVal * bankCalibParams.difc +
bankCalibParams.tzero) *
tofScale;
});
const auto averageDeltaTByT = computeAverageDeltaTByT(tofScaled);
const auto &intensity = matrixWS->y(0);
const auto &error = matrixWS->e(0);
const auto numPoints =
std::min({tofScaled.size(), intensity.size(), error.size()});
const auto header =
generateBankHeader(i + 1, static_cast<int>(std::round(tofScaled[0])),
numPoints, averageDeltaTByT);
outFile << std::left << std::setw(80) << header << '\n' << std::right;
for (size_t j = 0; j < numPoints; ++j) {
outFile << std::setw(8) << tofScaled[j] << std::setw(7)
<< intensity[j] * 1000 << std::setw(5) << error[j] * 1000;
if (j % POINTS_PER_LINE == POINTS_PER_LINE - 1) {
// new line every 4 points
outFile << '\n';
} else if (j == numPoints - 1) {
// make sure line is 80 characters long
outFile << std::string(80 - (i % POINTS_PER_LINE + 1) * 20, ' ')
<< '\n';
}
}
}
}
std::map<std::string, std::string> SaveGDA::validateInputs() {
std::map<std::string, std::string> issues;
boost::optional<std::string> inputWSIssue;
const API::WorkspaceGroup_sptr inputWS = getProperty(PROP_INPUT_WS);
for (const auto &ws : *inputWS) {
const auto matrixWS = std::dynamic_pointer_cast<MatrixWorkspace>(ws);
if (matrixWS) {
if (matrixWS->getNumberHistograms() != 1) {
inputWSIssue = "The workspace " + matrixWS->getName() +
" has the wrong number of histograms. It "
"should contain data for a single focused "
"spectra";
} else if (matrixWS->getAxis(0)->unit()->unitID() != "dSpacing") {
inputWSIssue = "The workspace " + matrixWS->getName() +
" has incorrect units. SaveGDA "
"expects input workspaces with "
"units of D-spacing";
}
} else { // not matrixWS
inputWSIssue = "The workspace " + ws->getName() +
" is of the wrong type. It should be a MatrixWorkspace";
}
}
if (inputWSIssue) {
issues[PROP_INPUT_WS] = *inputWSIssue;
}
const std::vector<int> groupingScheme = getProperty(PROP_GROUPING_SCHEME);
const auto numSpectraInGroupingScheme = groupingScheme.size();
const auto numSpectraInWS =
static_cast<size_t>(inputWS->getNumberOfEntries());
if (numSpectraInGroupingScheme != numSpectraInWS) {
issues[PROP_GROUPING_SCHEME] =
"The grouping scheme must contain one entry for every focused spectrum "
"in the input workspace. " +
PROP_GROUPING_SCHEME + " has " +
std::to_string(numSpectraInGroupingScheme) + " entries whereas " +
PROP_INPUT_WS + " has " + std::to_string(numSpectraInWS);
}
return issues;
}
std::vector<SaveGDA::CalibrationParams> SaveGDA::parseParamsFile() const {
const std::string paramsFilename = getProperty(PROP_PARAMS_FILENAME);
const auto paramLines = getParamLinesFromGSASFile(paramsFilename);
if (!paramLines) {
g_log.error(strerror(errno));
throw Kernel::Exception::FileError("Could not read GSAS parameter file",
paramsFilename);
}
std::vector<CalibrationParams> calibParams;
for (const auto ¶mLine : *paramLines) {
std::vector<std::string> lineItems;
boost::algorithm::split(lineItems, paramLine, boost::is_any_of("\t "),
boost::token_compress_on);
calibParams.emplace_back(std::stod(lineItems[3]), std::stod(lineItems[4]),
std::stod(lineItems[5]));
}
return calibParams;
}
} // namespace DataHandling
} // namespace Mantid