Newer
Older
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
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/SaveAscii.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/ArrayProperty.h"
#include <set>
#include <fstream>
#include <boost/tokenizer.hpp>
namespace Mantid
{
namespace DataHandling
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(SaveAscii)
using namespace Kernel;
using namespace API;
// Initialise the logger
Logger& SaveAscii::g_log = Logger::get("SaveAscii");
/// Initialisation method.
void SaveAscii::init()
{
std::vector<std::string> exts;
exts.push_back("dat");
exts.push_back("txt");
exts.push_back("csv");
declareProperty("Filename","",new FileValidator(exts,false),"A comma separated Ascii file that will be created");
declareProperty(new WorkspaceProperty<DataObjects::Workspace2D>("Workspace",
"",Direction::Input), "The name of the workspace that will be saved.");
BoundedValidator<int> *mustBePositive = new BoundedValidator<int>();
mustBePositive->setLower(1);
declareProperty("SpectrumMin", 1, mustBePositive);
declareProperty("SpectrumMax", EMPTY_INT(), mustBePositive->clone());
declareProperty(new ArrayProperty<int>("SpectrumList"));
declareProperty("Precision", EMPTY_INT(), mustBePositive->clone());
}
/**
* Executes the algorithm.
*/
void SaveAscii::exec()
{
// Get the workspace
DataObjects::Workspace2D_sptr ws = getProperty("Workspace");
Russell Taylor
committed
int nSpectra = ws->getNumberHistograms();
int nBins = ws->blocksize();
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
// Get the properties
std::vector<int> spec_list = getProperty("SpectrumList");
int spec_min = getProperty("SpectrumMin");
int spec_max = getProperty("SpectrumMax");
// 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(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 = 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());
// Write the column captions
file << "X";
if (idx.empty())
for(int spec=0;spec<nSpectra;spec++)
{
file << " , Y" << spec << " , E" << spec;
}
else
for(std::set<int>::const_iterator spec=idx.begin();spec!=idx.end();spec++)
{
file << " , Y" << *spec << " , E" << *spec;
}
file << '\n';
bool isHistogram = ws->isHistogramData();
// Set the number precision
int prec = getProperty("Precision");
if (prec != EMPTY_INT()) file.precision(prec);
Progress progress(this,0,1,nBins);
for(int bin=0;bin<nBins;bin++)
{
if (isHistogram) // bin centres
{
file << ( ws->readX(0)[bin] + ws->readX(0)[bin+1] )/2;
}
else // data points
{
file << ws->readX(0)[bin];
}
if (idx.empty())
for(int spec=0;spec<nSpectra;spec++)
{
file << " , " << ws->readY(spec)[bin] << " , " << ws->readE(spec)[bin];
}
else
for(std::set<int>::const_iterator spec=idx.begin();spec!=idx.end();spec++)
{
file << " , " << ws->readY(*spec)[bin] << " , " << ws->readE(*spec)[bin];
}
file << '\n';
progress.report();
}
}
} // namespace DataHandling
} // namespace Mantid