-
Notifications
You must be signed in to change notification settings - Fork 122
/
LoadSNSspec.cpp
212 lines (183 loc) · 6.77 KB
/
LoadSNSspec.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
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/LoadSNSspec.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/StringTokenizer.h"
#include "MantidKernel/UnitFactory.h"
#include <cstring>
#include <fstream>
namespace Mantid {
namespace DataHandling {
DECLARE_FILELOADER_ALGORITHM(LoadSNSspec)
/**
* 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 LoadSNSspec::confidence(Kernel::FileDescriptor &descriptor) const {
if (!descriptor.isAscii())
return 0;
auto &file = descriptor.data();
int confidence(0);
size_t axiscols(0), datacols(0);
std::string str;
typedef Mantid::Kernel::StringTokenizer tokenizer;
const std::string sep = " ";
bool snsspec(false);
while (std::getline(file, str)) {
// File is opened in binary mode so getline will leave a \r at the end of an
// empty line if it exists
if (str.empty() || str == "\r")
continue;
try {
// if it's comment line
tokenizer tok(str, sep,
Mantid::Kernel::StringTokenizer::TOK_IGNORE_EMPTY);
if (str.at(0) == '#') {
if (str.at(1) == 'L') {
axiscols = tok.count();
// if the file contains a comment line starting with "#L" followed
// by three columns this could be loadsnsspec file
if (axiscols > 2) {
snsspec = true;
}
}
} else {
// check first data line is a 3 column line
datacols = tok.count();
break;
}
} catch (std::out_of_range &) {
}
}
if (snsspec && datacols == 3) // three column data
{
confidence = 80;
}
return confidence;
}
using namespace Kernel;
using namespace API;
/// Empty constructor
LoadSNSspec::LoadSNSspec() {}
/// Initialisation method.
void LoadSNSspec::init() {
const std::vector<std::string> exts{".dat", ".txt"};
declareProperty(Kernel::make_unique<FileProperty>("Filename", "",
FileProperty::Load, exts),
"The name of the text file to read, including its full or "
"relative path. The file extension must be .txt or .dat.");
declareProperty(
make_unique<WorkspaceProperty<>>("OutputWorkspace", "",
Direction::Output),
"The name of the workspace that will be created, filled with the read-in "
"data and stored in the [[Analysis Data Service]].");
std::vector<std::string> units = UnitFactory::Instance().getKeys();
declareProperty("Unit", "Energy",
boost::make_shared<Kernel::StringListValidator>(units),
"The unit to assign to the X axis (anything known to the "
"[[Unit Factory]] or \"Dimensionless\") (default: Energy)");
}
/**
* Executes the algorithm.
*/
void LoadSNSspec::exec() {
std::string filename = getProperty("Filename");
std::ifstream file(filename.c_str());
file.seekg(0, std::ios::end);
Progress progress(this, 0, 1, static_cast<int>(file.tellg()));
file.seekg(0, std::ios::beg);
std::string str;
std::vector<DataObjects::Histogram1D> spectra;
int nBins = 0; // number of rows
// bool numeric = true;
std::vector<double> input;
// determine the number of lines starting by #L
// as there is one per set of data
int spectra_nbr = 0;
while (getline(file, str)) {
if (str.empty())
continue;
if (str[0] == '#' && str[1] == 'L') {
spectra_nbr++;
}
}
spectra.resize(spectra_nbr, DataObjects::Histogram1D(
HistogramData::Histogram::XMode::BinEdges,
HistogramData::Histogram::YMode::Counts));
file.clear(); // end of file has been reached so we need to clear file state
file.seekg(0, std::ios::beg); // go back to beginning of file
int working_with_spectrum_nbr = -1; // spectrum number
while (getline(file, str)) {
progress.report(static_cast<int>(file.tellg()));
// line with data, need to be parsed by white spaces
if (!str.empty() && str[0] != '#') {
typedef Mantid::Kernel::StringTokenizer tokenizer;
const std::string sep = " ";
tokenizer tok(str, sep,
Mantid::Kernel::StringTokenizer::TOK_IGNORE_EMPTY);
for (const auto &beg : tok) {
input.push_back(std::stod(beg));
}
}
if (str.empty()) {
if (working_with_spectrum_nbr != -1) {
for (int j = 0; j < static_cast<int>(input.size() - 1); j++) {
spectra[working_with_spectrum_nbr].dataX().push_back(input[j]);
j++;
spectra[working_with_spectrum_nbr].dataY().push_back(input[j]);
j++;
spectra[working_with_spectrum_nbr].dataE().push_back(input[j]);
nBins = j / 3;
}
spectra[working_with_spectrum_nbr].dataX().push_back(input.back());
}
working_with_spectrum_nbr++;
input.clear();
}
} // end of read file
try {
if (spectra_nbr == 0)
throw "Undefined number of spectra";
if (working_with_spectrum_nbr == (spectra_nbr - 1)) {
for (int j = 0; j < static_cast<int>(input.size() - 1); j++) {
spectra[working_with_spectrum_nbr].dataX().push_back(input[j]);
j++;
spectra[working_with_spectrum_nbr].dataY().push_back(input[j]);
j++;
spectra[working_with_spectrum_nbr].dataE().push_back(input[j]);
nBins = j / 3;
}
spectra[working_with_spectrum_nbr].dataX().push_back(input.back());
}
} catch (...) {
}
try {
int nSpectra = spectra_nbr;
MatrixWorkspace_sptr localWorkspace =
boost::dynamic_pointer_cast<MatrixWorkspace>(
WorkspaceFactory::Instance().create("Workspace2D", nSpectra,
nBins + 1, nBins));
localWorkspace->getAxis(0)->unit() =
UnitFactory::Instance().create(getProperty("Unit"));
for (int i = 0; i < nSpectra; i++) {
localWorkspace->dataX(i) = spectra[i].dataX();
localWorkspace->dataY(i) = spectra[i].dataY();
localWorkspace->dataE(i) = spectra[i].dataE();
// Just have spectrum number start at 1 and count up
localWorkspace->getSpectrum(i).setSpectrumNo(i + 1);
}
setProperty("OutputWorkspace", localWorkspace);
} catch (Exception::NotFoundError &) {
// Asked for dimensionless workspace (obviously not in unit factory)
}
}
} // namespace DataHandling
} // namespace Mantid