-
Notifications
You must be signed in to change notification settings - Fork 122
/
SaveDaveGrp.cpp
118 lines (106 loc) · 3.79 KB
/
SaveDaveGrp.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
#include "MantidDataHandling/SaveDaveGrp.h"
#include "MantidKernel/System.h"
#include "MantidAPI/Axis.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidKernel/Unit.h"
#include "MantidKernel/UnitFactory.h"
#include <fstream>
namespace Mantid {
namespace DataHandling {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(SaveDaveGrp)
using namespace Mantid::Kernel;
using namespace Mantid::API;
/** Initialize the algorithm's properties.
*/
void SaveDaveGrp::init() {
this->declareProperty(
make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
"An input workspace.");
this->declareProperty(
make_unique<FileProperty>("Filename", "", FileProperty::Save, ".grp"),
"A DAVE grouped data format file that will be created");
this->declareProperty(make_unique<Kernel::PropertyWithValue<bool>>(
"ToMicroEV", false, Kernel::Direction::Input),
"Transform all energy units from milli eV to micro eV");
}
/** Execute the algorithm.
*/
void SaveDaveGrp::exec() {
// Get the workspace
MatrixWorkspace_const_sptr ws = getProperty("InputWorkspace");
std::size_t nSpectra = ws->getNumberHistograms();
std::size_t nBins = ws->blocksize();
if (nSpectra * nBins == 0)
throw std::invalid_argument(
"Either the number of bins or the number of histograms is 0");
bool isHist = ws->isHistogramData();
std::string xcaption = ws->getAxis(0)->unit()->caption();
std::string ycaption = ws->getAxis(1)->unit()->caption();
if (xcaption.length() == 0)
xcaption = "X";
if (ycaption.length() == 0 || ycaption == "Spectrum")
ycaption = "Y";
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);
}
file << "# Number of " << xcaption << " values\n";
file << nBins << '\n';
file << "# Number of " << ycaption << " values\n";
file << nSpectra << '\n';
bool toMicroeV = getProperty("ToMicroEV");
bool xToMicroeV = false, yToMicroeV = false;
std::string xunit = ws->getAxis(0)->unit()->label();
std::string yunit = ws->getAxis(1)->unit()->label();
if (yunit == "Angstrom^-1")
yunit = "1/Angstroms"; // backwards compatability with old versions
if (toMicroeV && (xunit == "meV"))
xToMicroeV = true;
if (toMicroeV && (yunit == "meV"))
yToMicroeV = true;
if (xToMicroeV)
xunit = "micro eV";
file << "# " << xcaption << " (" << xunit << ") values\n";
std::vector<double> x = ws->readX(0);
for (std::size_t i = 0; i < nBins; i++) {
double xvalue = (isHist) ? (x[i] + x[i + 1]) * 0.5 : x[i];
if (xToMicroeV)
xvalue *= 1000.;
file << xvalue << '\n';
}
if (yToMicroeV)
yunit = "micro eV";
file << "# " << ycaption << " (" << yunit << ") values\n";
double yvalue;
if ((*ws->getAxis(1)).length() == (nSpectra + 1)) {
for (std::size_t i = 0; i < nSpectra; i++) {
yvalue = 0.5 * (((*ws->getAxis(1))(i)) + ((*ws->getAxis(1))(i + 1)));
if (yToMicroeV)
yvalue *= 1000.;
file << yvalue << '\n';
}
} else {
for (std::size_t i = 0; i < nSpectra; i++) {
yvalue = (*ws->getAxis(1))(i);
if (yToMicroeV)
yvalue *= 1000.;
file << yvalue << '\n';
}
}
Progress progress(this, 0, 1, nSpectra);
for (std::size_t i = 0; i < nSpectra; i++) {
file << "# Group " << i << '\n';
std::vector<double> y = ws->readY(i), er = ws->readE(i);
std::vector<double>::iterator ity, iter;
for (ity = y.begin(), iter = er.begin(); ity != y.end(); ++ity, ++iter)
file << (*ity) << " " << (*iter) << '\n';
progress.report();
}
file.close();
}
} // namespace Mantid
} // namespace DataHandling