-
Notifications
You must be signed in to change notification settings - Fork 122
/
SaveAscii.cpp
215 lines (187 loc) · 7.46 KB
/
SaveAscii.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
// 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 +
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/SaveAscii.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/VisibleWhenProperty.h"
#include <boost/tokenizer.hpp>
#include <fstream>
#include <set>
namespace Mantid::DataHandling {
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(SaveAscii)
using namespace Kernel;
using namespace API;
/// Empty constructor
SaveAscii::SaveAscii() : m_separatorIndex() {}
/// Initialisation method.
void SaveAscii::init() {
declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
"The name of the workspace containing the data you want to save to a "
"Ascii file.");
const std::vector<std::string> asciiExts{".dat", ".txt", ".csv"};
declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, asciiExts),
"The filename of the output Ascii file.");
auto mustBeNonNegative = std::make_shared<BoundedValidator<int>>();
mustBeNonNegative->setLower(0);
declareProperty("WorkspaceIndexMin", 0, mustBeNonNegative, "The starting workspace index.");
declareProperty("WorkspaceIndexMax", EMPTY_INT(), mustBeNonNegative, "The ending workspace index.");
declareProperty(std::make_unique<ArrayProperty<int>>("SpectrumList"), "List of workspace indices to save.");
declareProperty("Precision", EMPTY_INT(), mustBeNonNegative, "Precision of output double values.");
declareProperty("WriteXError", false, "If true, the error on X will be written as the fourth column.");
declareProperty("CommentIndicator", "", "Character(s) to put in front of comment lines.");
// For the ListValidator
std::string spacers[6][2] = {{"CSV", ","}, {"Tab", "\t"}, {"Space", " "},
{"Colon", ":"}, {"SemiColon", ";"}, {"UserDefined", "UserDefined"}};
std::vector<std::string> sepOptions;
for (auto &spacer : spacers) {
std::string option = spacer[0];
m_separatorIndex.insert(std::pair<std::string, std::string>(option, spacer[1]));
sepOptions.emplace_back(option);
}
declareProperty("Separator", "CSV", std::make_shared<StringListValidator>(sepOptions),
"Character(s) to put as separator between X, Y, E values.");
declareProperty(std::make_unique<PropertyWithValue<std::string>>("CustomSeparator", "", Direction::Input),
"If present, will override any specified choice given to Separator.");
getPointerToProperty("CustomSeparator")->setAutoTrim(false);
setPropertySettings("CustomSeparator",
std::make_unique<VisibleWhenProperty>("Separator", IS_EQUAL_TO, "UserDefined"));
declareProperty("ColumnHeader", true, "If true, put column headers into file. ");
declareProperty("ICEFormat", false, "If true, special column headers for ICE in file. ");
}
/**
* Executes the algorithm.
*/
void SaveAscii::exec() {
// Get the workspace
MatrixWorkspace_const_sptr ws = getProperty("InputWorkspace");
auto nSpectra = static_cast<int>(ws->getNumberHistograms());
auto nBins = static_cast<int>(ws->blocksize());
// Get the properties
std::vector<int> spec_list = getProperty("SpectrumList");
int spec_min = getProperty("WorkspaceIndexMin");
int spec_max = getProperty("WorkspaceIndexMax");
bool writeHeader = getProperty("ColumnHeader");
// Check whether we need to write the fourth column
bool write_dx = getProperty("WriteXError");
std::string choice = getPropertyValue("Separator");
std::string custom = getPropertyValue("CustomSeparator");
std::string sep;
// If the custom separator property is not empty, then we use that under any
// circumstance.
if (!custom.empty()) {
sep = custom;
}
// Else if the separator drop down choice is not UserDefined then we use that.
else if (choice != "UserDefined") {
auto it = m_separatorIndex.find(choice);
sep = it->second;
}
// If we still have nothing, then we are forced to use a default.
if (sep.empty()) {
g_log.notice() << "\"UserDefined\" has been selected, but no custom "
"separator has been entered. Using default instead.";
sep = " , ";
}
std::string comment = getPropertyValue("CommentIndicator");
std::string errstr = "E";
std::string errstr2;
std::string comstr = " , ";
bool ice = getProperty("ICEFormat");
if (ice) {
// overwrite properties so file can be read by ICE
errstr = "Y";
errstr2 = "_error";
comstr = ", ";
writeHeader = true;
write_dx = false;
comment = "#features:";
}
// Create an spectra index list for output
std::set<int> idx;
// Add spectra interval into the index list
if (spec_max != EMPTY_INT() && spec_min != EMPTY_INT()) {
if (spec_min >= nSpectra || spec_max >= nSpectra || spec_min > spec_max)
throw std::invalid_argument("Inconsistent spectra interval");
for (int spec = spec_min; spec <= spec_max; spec++)
idx.insert(spec);
}
// Add spectra list into the index list
if (!spec_list.empty()) {
for (auto &spec : spec_list) {
if (spec >= nSpectra)
throw std::invalid_argument("Inconsistent spectra list");
else
idx.insert(spec);
}
}
if (!idx.empty())
nSpectra = static_cast<int>(idx.size());
if (nBins == 0 || nSpectra == 0)
throw std::runtime_error("Trying to save an empty workspace");
std::string filename = getProperty("Filename");
std::ofstream file(filename.c_str());
if (!file) {
g_log.error("Unable to create file: " + filename);
throw Exception::FileError("Unable to create file: ", filename);
}
// Write the column captions
if (writeHeader) {
file << comment << "X";
if (idx.empty())
for (int spec = 0; spec < nSpectra; spec++) {
file << comstr << "Y" << spec << comstr << errstr << spec << errstr2;
if (write_dx)
file << " , DX" << spec;
}
else
for (auto spec : idx) {
file << comstr << "Y" << spec << comstr << errstr << spec << errstr2;
if (write_dx)
file << " , DX" << spec;
}
file << '\n';
}
// Set the number precision
int prec = getProperty("Precision");
if (prec != EMPTY_INT())
file.precision(prec);
Progress progress(this, 0.0, 1.0, nBins);
auto pointDeltas = ws->pointStandardDeviations(0);
auto points = ws->points(0);
for (int bin = 0; bin < nBins; bin++) {
file << points[bin];
if (idx.empty())
for (int spec = 0; spec < nSpectra; spec++) {
file << sep;
file << ws->y(spec)[bin];
file << sep;
file << ws->e(spec)[bin];
}
else
for (auto spec : idx) {
file << sep;
file << ws->y(spec)[bin];
file << sep;
file << ws->e(spec)[bin];
}
if (write_dx) {
file << sep;
file << pointDeltas[bin];
}
file << '\n';
progress.report();
}
}
} // namespace Mantid::DataHandling