Mantid
Loading...
Searching...
No Matches
SaveDaveGrp.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
8#include "MantidAPI/Axis.h"
11#include "MantidKernel/Unit.h"
13#include <fstream>
14
15namespace Mantid::DataHandling {
16
17// Register the algorithm into the AlgorithmFactory
18DECLARE_ALGORITHM(SaveDaveGrp)
19
20using namespace Mantid::Kernel;
21using namespace Mantid::API;
22
26 this->declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
27 "An input workspace.");
28 this->declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, ".grp"),
29 "A DAVE grouped data format file that will be created");
30 this->declareProperty(std::make_unique<Kernel::PropertyWithValue<bool>>("ToMicroEV", false, Kernel::Direction::Input),
31 "Transform all energy units from milli eV to micro eV");
32 this->declareProperty(
33 std::make_unique<Kernel::PropertyWithValue<bool>>("ToQsInQENSData", false, Kernel::Direction::Input),
34 "Transform the spectrum numbers to Q values");
35}
36
37std::map<std::string, std::string> SaveDaveGrp::validateInputs() {
38 std::map<std::string, std::string> result;
39
40 const bool toMicroEV = getProperty("ToMicroEV");
41 const bool toQsInQENSData = getProperty("ToQsInQENSData");
42 if (toMicroEV && toQsInQENSData) {
43 result["ToQsInQENSData"] = "'ToMicroEV' and 'ToQsInQENSData' can't be set to True at the same time."
44 "Set 'ToQsInQENSData' to False";
45 }
46 return result;
47}
48
52 // Get the workspace
53 MatrixWorkspace_const_sptr ws = getProperty("InputWorkspace");
54 std::size_t nSpectra = ws->getNumberHistograms();
55 std::size_t nBins = ws->blocksize();
56 if (nSpectra * nBins == 0)
57 throw std::invalid_argument("Either the number of bins or the number of histograms is 0");
58 std::string xcaption = ws->getAxis(0)->unit()->caption();
59 std::string ycaption = ws->getAxis(1)->unit()->caption();
60 const bool toQsInQENSData = getProperty("ToQsInQENSData");
61
62 if (xcaption.length() == 0)
63 xcaption = "X";
64 if (ycaption.length() == 0 || ycaption == "Spectrum")
65 ycaption = toQsInQENSData ? "Q" : "Y";
66
67 std::string filename = getProperty("Filename");
68 std::ofstream file(filename.c_str());
69 if (!file) {
70 g_log.error("Unable to create file: " + filename);
71 throw Exception::FileError("Unable to create file: ", filename);
72 }
73
74 file << "# Number of " << xcaption << " values\n";
75 file << nBins << '\n';
76 file << "# Number of " << ycaption << " values\n";
77 file << nSpectra << '\n';
78
79 bool toMicroeV = getProperty("ToMicroEV");
80 bool xToMicroeV = false, yToMicroeV = false;
81
82 std::string xunit = ws->getAxis(0)->unit()->label();
83 std::string yunit = ws->getAxis(1)->unit()->label();
84 if (yunit == "Angstrom^-1")
85 yunit = "1/Angstroms"; // backwards compatability with old versions
86 if (toMicroeV && (xunit == "meV"))
87 xToMicroeV = true;
88 if (toMicroeV && (yunit == "meV"))
89 yToMicroeV = true;
90
91 if (xToMicroeV)
92 xunit = "micro eV";
93 file << "# " << xcaption << " (" << xunit << ") values\n";
94 auto x = ws->points(0);
95 for (std::size_t i = 0; i < nBins; i++) {
96 double xvalue = x[i];
97 if (xToMicroeV)
98 xvalue *= 1000.;
99 file << xvalue << '\n';
100 }
101
102 if (yToMicroeV)
103 yunit = "micro eV";
104 file << "# " << ycaption << " (" << yunit << ") values\n";
105 double yvalue;
106 if (toQsInQENSData) {
107 auto qsInQENSData = createChildAlgorithm("GetQsInQENSData");
108 qsInQENSData->initialize();
109 qsInQENSData->setProperty("InputWorkspace", ws->getName());
110 qsInQENSData->execute();
111 std::vector<double> qvalues = qsInQENSData->getProperty("Qvalues");
112 for (const auto &qvalue : qvalues) {
113 file << qvalue << '\n';
114 }
115 } else if ((*ws->getAxis(1)).length() == (nSpectra + 1)) {
116 for (std::size_t i = 0; i < nSpectra; i++) {
117 yvalue = 0.5 * (((*ws->getAxis(1))(i)) + ((*ws->getAxis(1))(i + 1)));
118 if (yToMicroeV)
119 yvalue *= 1000.;
120 file << yvalue << '\n';
121 }
122 } else {
123 for (std::size_t i = 0; i < nSpectra; i++) {
124 yvalue = (*ws->getAxis(1))(i);
125 if (yToMicroeV)
126 yvalue *= 1000.;
127 file << yvalue << '\n';
128 }
129 }
130 Progress progress(this, 0.0, 1.0, nSpectra);
131 for (std::size_t i = 0; i < nSpectra; i++) {
132 file << "# Group " << i << '\n';
133 auto &Y = ws->y(i);
134 auto &E = ws->e(i);
135 auto itE = E.cbegin();
136 std::for_each(Y.cbegin(), Y.cend(), [&itE, &file](const double y) { file << y << " " << *itE++ << "\n"; });
137
138 progress.report();
139 }
140 file.close();
141}
142
143} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:542
int64_t nSpectra
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Kernel::Logger & g_log
Definition Algorithm.h:423
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
@ Save
to specify a file to write to, the file may or may not exist
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
void exec() override
Run the algorithm.
std::map< std::string, std::string > validateInputs() override
Perform validation of ALL the input properties of the algorithm.
void init() override
Initialise the properties.
Records the filename and the description of failure.
Definition Exception.h:98
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
The concrete, templated class for properties.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
@ Input
An input workspace.
Definition Property.h:53