-
Notifications
You must be signed in to change notification settings - Fork 122
/
SaveDiffCal.cpp
250 lines (210 loc) · 8.66 KB
/
SaveDiffCal.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
// 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/SaveDiffCal.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/ITableWorkspace.h"
#include "MantidDataHandling/H5Util.h"
#include "MantidDataObjects/GroupingWorkspace.h"
#include "MantidDataObjects/MaskWorkspace.h"
#include <H5Cpp.h>
#include <Poco/File.h>
#include <Poco/Path.h>
namespace Mantid {
namespace DataHandling {
using Mantid::API::FileProperty;
using Mantid::API::ITableWorkspace;
using Mantid::API::ITableWorkspace_const_sptr;
using Mantid::API::PropertyMode;
using Mantid::API::WorkspaceProperty;
using Mantid::DataObjects::GroupingWorkspace;
using Mantid::DataObjects::GroupingWorkspace_const_sptr;
using Mantid::DataObjects::MaskWorkspace;
using Mantid::DataObjects::MaskWorkspace_const_sptr;
using Mantid::Kernel::Direction;
using namespace H5;
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SaveDiffCal)
//----------------------------------------------------------------------------------------------
/// Algorithms name for identification. @see Algorithm::name
const std::string SaveDiffCal::name() const { return "SaveDiffCal"; }
/// Algorithm's version for identification. @see Algorithm::version
int SaveDiffCal::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string SaveDiffCal::category() const {
return "DataHandling\\Instrument;Diffraction\\DataHandling";
}
/// Algorithm's summary for use in the GUI and help. @see Algorithm::summary
const std::string SaveDiffCal::summary() const {
return "Saves a calibration file for powder diffraction";
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void SaveDiffCal::init() {
declareProperty(std::make_unique<WorkspaceProperty<ITableWorkspace>>(
"CalibrationWorkspace", "", Direction::Input),
"An output workspace.");
declareProperty(
std::make_unique<WorkspaceProperty<GroupingWorkspace>>(
"GroupingWorkspace", "", Direction::Input, PropertyMode::Optional),
"Optional: An GroupingWorkspace workspace giving the grouping info.");
declareProperty(
std::make_unique<WorkspaceProperty<MaskWorkspace>>(
"MaskWorkspace", "", Direction::Input, PropertyMode::Optional),
"Optional: An Workspace workspace giving which detectors are masked.");
declareProperty(
std::make_unique<FileProperty>("Filename", "", FileProperty::Save, ".h5"),
"Path to the .h5 file that will be created.");
}
std::map<std::string, std::string> SaveDiffCal::validateInputs() {
std::map<std::string, std::string> result;
ITableWorkspace_const_sptr calibrationWS =
getProperty("CalibrationWorkspace");
if (!bool(calibrationWS)) {
result["CalibrationWorkspace"] = "Cannot save empty table";
} else {
size_t numRows = calibrationWS->rowCount();
if (numRows == 0) {
result["CalibrationWorkspace"] = "Cannot save empty table";
} else {
GroupingWorkspace_const_sptr groupingWS =
getProperty("GroupingWorkspace");
if (bool(groupingWS) && numRows < groupingWS->getNumberHistograms()) {
result["GroupingWorkspace"] =
"Must have same number of spectra as the table has rows";
}
MaskWorkspace_const_sptr maskWS = getProperty("MaskWorkspace");
if (bool(maskWS) && numRows < maskWS->getNumberHistograms()) {
result["MaskWorkspace"] =
"Must have same number of spectra as the table has rows";
}
}
}
return result;
}
void SaveDiffCal::writeDoubleFieldFromTable(H5::Group &group,
const std::string &name) {
auto column = m_calibrationWS->getColumn(name);
// cppcheck-suppress compareBoolExpressionWithInt
auto data = column->numeric_fill<>(m_numValues);
H5Util::writeArray1D(group, name, data);
}
void SaveDiffCal::writeIntFieldFromTable(H5::Group &group,
const std::string &name) {
auto column = m_calibrationWS->getColumn(name);
auto data = column->numeric_fill<int32_t>(m_numValues);
H5Util::writeArray1D(group, name, data);
}
// TODO should flip for mask
void SaveDiffCal::writeIntFieldFromSVWS(
H5::Group &group, const std::string &name,
const DataObjects::SpecialWorkspace2D_const_sptr &ws) {
const bool isMask = (name == "use");
// output array defaults to all one (one group, use the pixel)
std::vector<int32_t> values(m_numValues, 1);
if (bool(ws)) {
for (size_t i = 0; i < m_numValues; ++i) {
auto &ids = ws->getSpectrum(i).getDetectorIDs();
auto found = m_detidToIndex.find(*(ids.begin()));
if (found != m_detidToIndex.end()) {
auto value = static_cast<int32_t>(ws->getValue(found->first));
// in maskworkspace 0=use, 1=dontuse - backwards from the file
if (isMask) {
if (value == 0)
value = 1;
else
value = 0;
}
values[found->second] = value;
}
}
}
H5Util::writeArray1D(group, name, values);
}
void SaveDiffCal::generateDetidToIndex() {
m_detidToIndex.clear();
auto detidCol = m_calibrationWS->getColumn("detid");
auto detids = detidCol->numeric_fill<detid_t>();
const size_t numDets = detids.size();
for (size_t i = 0; i < numDets; ++i) {
m_detidToIndex[static_cast<detid_t>(detids[i])] = i;
}
}
bool SaveDiffCal::tableHasColumn(const std::string &ColumnName) const {
const std::vector<std::string> names = m_calibrationWS->getColumnNames();
return std::any_of(
names.cbegin(), names.cend(),
[&ColumnName](const auto &name) { return name == ColumnName; });
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void SaveDiffCal::exec() {
m_calibrationWS = getProperty("CalibrationWorkspace");
this->generateDetidToIndex();
GroupingWorkspace_const_sptr groupingWS = getProperty("GroupingWorkspace");
MaskWorkspace_const_sptr maskWS = getProperty("MaskWorkspace");
std::string filename = getProperty("Filename");
m_numValues = m_calibrationWS->rowCount();
if (bool(groupingWS) && groupingWS->getNumberHistograms() < m_numValues) {
m_numValues = groupingWS->getNumberHistograms();
}
if (bool(maskWS) && maskWS->getNumberHistograms() < m_numValues) {
m_numValues = maskWS->getNumberHistograms();
}
// delete the file if it already exists
if (Poco::File(filename).exists()) {
Poco::File(filename).remove();
}
H5File file(filename, H5F_ACC_EXCL);
auto calibrationGroup =
H5Util::createGroupNXS(file, "calibration", "NXentry");
this->writeDoubleFieldFromTable(calibrationGroup, "difc");
this->writeDoubleFieldFromTable(calibrationGroup, "difa");
this->writeDoubleFieldFromTable(calibrationGroup, "tzero");
this->writeIntFieldFromTable(calibrationGroup, "detid");
if (this->tableHasColumn("dasid")) // optional field
this->writeIntFieldFromTable(calibrationGroup, "dasid");
else
g_log.information("Not writing out values for \"dasid\"");
this->writeIntFieldFromSVWS(calibrationGroup, "group", groupingWS);
this->writeIntFieldFromSVWS(calibrationGroup, "use", maskWS);
if (this->tableHasColumn("offset")) // optional field
this->writeDoubleFieldFromTable(calibrationGroup, "offset");
else
g_log.information("Not writing out values for \"offset\"");
// get the instrument information
std::string instrumentName;
std::string instrumentSource;
if (bool(groupingWS)) {
instrumentName = groupingWS->getInstrument()->getName();
instrumentSource = groupingWS->getInstrument()->getFilename();
}
if (bool(maskWS)) {
if (instrumentName.empty()) {
instrumentName = maskWS->getInstrument()->getName();
}
if (instrumentSource.empty()) {
instrumentSource = maskWS->getInstrument()->getFilename();
}
}
if (!instrumentSource.empty()) {
instrumentSource = Poco::Path(instrumentSource).getFileName();
}
// add the instrument information
auto instrumentGroup = calibrationGroup.createGroup("instrument");
H5Util::writeStrAttribute(instrumentGroup, "NX_class", "NXinstrument");
if (!instrumentName.empty()) {
H5Util::write(instrumentGroup, "name", instrumentName);
}
if (!instrumentSource.empty()) {
H5Util::write(instrumentGroup, "instrument_source", instrumentSource);
}
file.close();
}
} // namespace DataHandling
} // namespace Mantid