-
Notifications
You must be signed in to change notification settings - Fork 122
/
SaveAscii2.cpp
331 lines (296 loc) · 11.5 KB
/
SaveAscii2.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
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/SaveAscii2.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/ArrayProperty.h"
#include "MantidAPI/FileProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/VisibleWhenProperty.h"
#include "MantidKernel/ListValidator.h"
#include <set>
#include <fstream>
#include <boost/tokenizer.hpp>
#include <boost/regex.hpp>
namespace Mantid
{
namespace DataHandling
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(SaveAscii2)
using namespace Kernel;
using namespace API;
/// Empty constructor
SaveAscii2::SaveAscii2() : m_separatorIndex()
{
}
/// Initialisation method.
void SaveAscii2::init()
{
declareProperty(new WorkspaceProperty<>("InputWorkspace",
"",Direction::Input), "The name of the workspace containing the data you want to save to a Ascii file.");
std::vector<std::string> exts;
exts.push_back(".dat");
exts.push_back(".txt");
exts.push_back(".csv");
declareProperty(new FileProperty("Filename", "", FileProperty::Save, exts),
"The filename of the output Ascii file.");
auto mustBePositive = boost::make_shared<BoundedValidator<int> >();
mustBePositive->setLower(1);
auto mustBeZeroGreater = boost::make_shared<BoundedValidator<int> >();
mustBeZeroGreater->setLower(0);
declareProperty("WorkspaceIndexMin", EMPTY_INT(), mustBeZeroGreater,"The starting workspace index.");
declareProperty("WorkspaceIndexMax", EMPTY_INT(), mustBeZeroGreater,"The ending workspace index.");
declareProperty(new ArrayProperty<int>("SpectrumList"),"List of workspace indices to save.");
declareProperty("Precision", EMPTY_INT(), mustBePositive,"Precision of output double values.");
declareProperty("ScientificFormat", false, "If true, the values will be written to the file in scientific notation.");
declareProperty("WriteXError", false, "If true, the error on X will be written as the fourth column.");
declareProperty("WriteSpectrumID", true, "If false, the spectrum ID will not be written for single-spectrum workspaces. "
"It is always written for workspaces with multiple spectra.");
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( size_t i = 0; i < 6; ++i )
{
std::string option = spacers[i][0];
m_separatorIndex.insert(std::pair<std::string,std::string>(option, spacers[i][1]));
sepOptions.push_back(option);
}
declareProperty("Separator", "CSV", boost::make_shared<StringListValidator>(sepOptions),
"The separator between data columns in the data file. The possible values are \"CSV\", \"Tab\", "
"\"Space\", \"SemiColon\", \"Colon\" or \"UserDefined\".");
declareProperty(new PropertyWithValue<std::string>("CustomSeparator", "", Direction::Input),
"If present, will override any specified choice given to Separator.");
setPropertySettings("CustomSeparator", new VisibleWhenProperty("Separator", IS_EQUAL_TO, "UserDefined") );
declareProperty("ColumnHeader", true, "If true, put column headers into file. ");
declareProperty("AppendToFile", false, "If true, don't overwrite the file. Append to the end of it. ");
declareProperty("RaggedWorkspace", true, "If true, ensure that more than one xspectra is used. "); //in testing
}
/**
* Executes the algorithm.
*/
void SaveAscii2::exec()
{
// Get the workspace
m_ws = getProperty("InputWorkspace");
int nSpectra = static_cast<int>(m_ws->getNumberHistograms());
m_nBins = static_cast<int>(m_ws->blocksize());
m_isHistogram = m_ws->isHistogramData();
m_isCommonBins = m_ws->isCommonBins(); //checking for ragged workspace
m_writeID = getProperty("WriteSpectrumID");
if (nSpectra != 1) m_writeID = true;
// Get the properties
std::vector<int> spec_list = getProperty("SpectrumList");
int spec_min = getProperty("WorkspaceIndexMin");
int spec_max = getProperty("WorkspaceIndexMax");
bool writeHeader = getProperty("ColumnHeader");
bool appendToFile = getProperty("AppendToFile");
// Check whether we need to write the fourth column
m_writeDX = getProperty("WriteXError");
std::string choice = getPropertyValue("Separator");
std::string custom = getPropertyValue("CustomSeparator");
// If the custom separator property is not empty, then we use that under any circumstance.
if(custom != "")
{
m_sep = custom;
}
// Else if the separator drop down choice is not UserDefined then we use that.
else if(choice != "UserDefined")
{
std::map<std::string, std::string>::iterator it = m_separatorIndex.find(choice);
m_sep = it->second;
}
// If we still have nothing, then we are forced to use a default.
if(m_sep.empty())
{
g_log.notice() << "\"UserDefined\" has been selected, but no custom separator has been entered."
" Using default instead.";
m_sep = ",";
}
// e + and - are included as they're part of the scientific notation
if (!boost::regex_match(m_sep.begin(), m_sep.end(), boost::regex("[^0-9e+-]+", boost::regex::perl)))
{
throw std::invalid_argument("Separators cannot contain numeric characters, plus signs, hyphens or 'e'");
}
std::string comment = getPropertyValue("CommentIndicator");
if (comment.at(0) == m_sep.at(0)||!boost::regex_match(comment.begin(), comment.end(), boost::regex("[^0-9e" + m_sep + "+-]+", boost::regex::perl)))
{
throw std::invalid_argument("Comment markers cannot contain numeric characters, plus signs, hyphens,"
" 'e' or the selected separator character");
}
// 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 < 0 || spec_max < 0 || spec_min > spec_max)
{
throw std::invalid_argument("Inconsistent spectra interval");
}
for(int i=spec_min;i<=spec_max;i++)
{
idx.insert(i);
}
}
//figure out how to read in readX and have them be seperate lists
// Add spectra list into the index list
if (!spec_list.empty())
{
for(size_t i=0;i<spec_list.size();i++)
{
if (spec_list[i] >= nSpectra)
{
throw std::invalid_argument("Inconsistent spectra list");
}
else
{
idx.insert(spec_list[i]);
}
}
}
if (!idx.empty())
{
nSpectra = static_cast<int>(idx.size());
}
if (m_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(), (appendToFile ? std::ios::app : std::ios::out));
if (!file)
{
g_log.error("Unable to create file: " + filename);
throw Exception::FileError("Unable to create file: " , filename);
}
// Set the number precision
int prec = getProperty("Precision");
if (prec != EMPTY_INT())
{
file.precision(prec);
}
bool scientific = getProperty("ScientificFormat");
if (scientific)
{
file << std::scientific;
}
if( writeHeader)
{
file << comment << " X "<< m_sep << " Y "<< m_sep <<" E";
if (m_writeDX)
{
file << " "<< m_sep <<" DX";
}
file << std::endl;
}
if (idx.empty())
{
Progress progress(this,0,1,nSpectra);
for(int i=0;i<nSpectra;i++)
{
writeSpectra(i, file);
progress.report();
}
}
else
{
Progress progress(this,0,1,idx.size());
for(std::set<int>::const_iterator i=idx.begin();i!=idx.end();++i)
{
writeSpectra(i, file);
progress.report();
}
}
file.unsetf(std::ios_base::floatfield);
file.close();
}
/**writes a spectra to the file using an iterator
@param spectraItr :: a set<int> iterator pointing to a set of workspace IDs to be saved
@param file :: the file writer object
*/
void SaveAscii2::writeSpectra(const std::set<int>::const_iterator & spectraItr, std::ofstream & file)
{
auto spec = m_ws->getSpectrum(*spectraItr);
auto specNo = spec->getSpectrumNo();
if (m_writeID) file << specNo << std::endl;
for(int bin=0;bin<m_nBins;bin++)
{
if (!m_isCommonBins) //checking for ragged workspace
{
file << m_ws->readX(*spectraItr)[bin];
}
if (m_isHistogram & m_isCommonBins) // bin centres
{
file << ( m_ws->readX(0)[bin] + m_ws->readX(0)[bin+1] )/2;
}
else // data points
{
file << m_ws->readX(0)[bin];
}
file << m_sep;
file << m_ws->readY(*spectraItr)[bin];
file << m_sep;
file << m_ws->readE(*spectraItr)[bin];
if (m_writeDX)
{
if (m_isHistogram) // bin centres
{
file << m_sep;
file << ( m_ws->readDx(0)[bin] + m_ws->readDx(0)[bin+1] )/2;
}
else // data points
{
file << m_sep;
file << m_ws->readDx(0)[bin];
}
}
file << std::endl;
}
}
/**writes a spectra to the file using a workspace ID
@param spectraIndex :: an integer relating to a workspace ID
@param file :: the file writer object
*/
void SaveAscii2::writeSpectra(const int & spectraIndex, std::ofstream & file)
{
auto spec = m_ws->getSpectrum(spectraIndex);
auto specNo = spec->getSpectrumNo();
if (m_writeID) file << specNo << std::endl;
for(int bin=0;bin<m_nBins;bin++)
{
if (m_isHistogram & m_isCommonBins) // bin centres
{
file << ( m_ws->readX(0)[bin] + m_ws->readX(1)[bin+1] )/2;
}
if (!m_isCommonBins) //checking for ragged workspace
{
file << m_ws->readX(spectraIndex)[bin];
}
else // data points
{
file << m_ws->readX(0)[bin];
}
file << m_sep;
file << m_ws->readY(spectraIndex)[bin];
file << m_sep;
file << m_ws->readE(spectraIndex)[bin];
if (m_writeDX)
{
if (m_isHistogram) // bin centres
{
file << m_sep;
file << ( m_ws->readDx(0)[bin] + m_ws->readDx(0)[bin+1] )/2;
}
else // data points
{
file << m_sep;
file << m_ws->readDx(0)[bin];
}
}
file << std::endl;
}
}
} // namespace DataHandling
} // namespace Mantid