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/System.h"
12#include "MantidKernel/Unit.h"
14#include <fstream>
15
16namespace Mantid::DataHandling {
17
18// Register the algorithm into the AlgorithmFactory
19DECLARE_ALGORITHM(SaveDaveGrp)
20
21using namespace Mantid::Kernel;
22using namespace Mantid::API;
23
27 this->declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
28 "An input workspace.");
29 this->declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, ".grp"),
30 "A DAVE grouped data format file that will be created");
31 this->declareProperty(std::make_unique<Kernel::PropertyWithValue<bool>>("ToMicroEV", false, Kernel::Direction::Input),
32 "Transform all energy units from milli eV to micro eV");
33}
34
38 // Get the workspace
39 MatrixWorkspace_const_sptr ws = getProperty("InputWorkspace");
40 std::size_t nSpectra = ws->getNumberHistograms();
41 std::size_t nBins = ws->blocksize();
42 if (nSpectra * nBins == 0)
43 throw std::invalid_argument("Either the number of bins or the number of histograms is 0");
44 std::string xcaption = ws->getAxis(0)->unit()->caption();
45 std::string ycaption = ws->getAxis(1)->unit()->caption();
46 if (xcaption.length() == 0)
47 xcaption = "X";
48 if (ycaption.length() == 0 || ycaption == "Spectrum")
49 ycaption = "Y";
50
51 std::string filename = getProperty("Filename");
52 std::ofstream file(filename.c_str());
53 if (!file) {
54 g_log.error("Unable to create file: " + filename);
55 throw Exception::FileError("Unable to create file: ", filename);
56 }
57
58 file << "# Number of " << xcaption << " values\n";
59 file << nBins << '\n';
60 file << "# Number of " << ycaption << " values\n";
61 file << nSpectra << '\n';
62
63 bool toMicroeV = getProperty("ToMicroEV");
64 bool xToMicroeV = false, yToMicroeV = false;
65
66 std::string xunit = ws->getAxis(0)->unit()->label();
67 std::string yunit = ws->getAxis(1)->unit()->label();
68 if (yunit == "Angstrom^-1")
69 yunit = "1/Angstroms"; // backwards compatability with old versions
70 if (toMicroeV && (xunit == "meV"))
71 xToMicroeV = true;
72 if (toMicroeV && (yunit == "meV"))
73 yToMicroeV = true;
74
75 if (xToMicroeV)
76 xunit = "micro eV";
77 file << "# " << xcaption << " (" << xunit << ") values\n";
78 auto x = ws->points(0);
79 for (std::size_t i = 0; i < nBins; i++) {
80 double xvalue = x[i];
81 if (xToMicroeV)
82 xvalue *= 1000.;
83 file << xvalue << '\n';
84 }
85
86 if (yToMicroeV)
87 yunit = "micro eV";
88 file << "# " << ycaption << " (" << yunit << ") values\n";
89 double yvalue;
90 if ((*ws->getAxis(1)).length() == (nSpectra + 1)) {
91 for (std::size_t i = 0; i < nSpectra; i++) {
92 yvalue = 0.5 * (((*ws->getAxis(1))(i)) + ((*ws->getAxis(1))(i + 1)));
93 if (yToMicroeV)
94 yvalue *= 1000.;
95 file << yvalue << '\n';
96 }
97 } else {
98 for (std::size_t i = 0; i < nSpectra; i++) {
99 yvalue = (*ws->getAxis(1))(i);
100 if (yToMicroeV)
101 yvalue *= 1000.;
102 file << yvalue << '\n';
103 }
104 }
105 Progress progress(this, 0.0, 1.0, nSpectra);
106 for (std::size_t i = 0; i < nSpectra; i++) {
107 file << "# Group " << i << '\n';
108 auto &Y = ws->y(i);
109 auto &E = ws->e(i);
110 auto itE = E.cbegin();
111 std::for_each(Y.cbegin(), Y.cend(), [&itE, &file](const double y) { file << y << " " << *itE++ << "\n"; });
112
113 progress.report();
114 }
115 file.close();
116}
117
118} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
int nSpectra
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
void exec() override
Run the algorithm.
Definition: SaveDaveGrp.cpp:37
void init() override
Initialise the properties.
Definition: SaveDaveGrp.cpp:26
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:77
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