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}
33
37 // Get the workspace
38 MatrixWorkspace_const_sptr ws = getProperty("InputWorkspace");
39 std::size_t nSpectra = ws->getNumberHistograms();
40 std::size_t nBins = ws->blocksize();
41 if (nSpectra * nBins == 0)
42 throw std::invalid_argument("Either the number of bins or the number of histograms is 0");
43 std::string xcaption = ws->getAxis(0)->unit()->caption();
44 std::string ycaption = ws->getAxis(1)->unit()->caption();
45 if (xcaption.length() == 0)
46 xcaption = "X";
47 if (ycaption.length() == 0 || ycaption == "Spectrum")
48 ycaption = "Y";
49
50 std::string filename = getProperty("Filename");
51 std::ofstream file(filename.c_str());
52 if (!file) {
53 g_log.error("Unable to create file: " + filename);
54 throw Exception::FileError("Unable to create file: ", filename);
55 }
56
57 file << "# Number of " << xcaption << " values\n";
58 file << nBins << '\n';
59 file << "# Number of " << ycaption << " values\n";
60 file << nSpectra << '\n';
61
62 bool toMicroeV = getProperty("ToMicroEV");
63 bool xToMicroeV = false, yToMicroeV = false;
64
65 std::string xunit = ws->getAxis(0)->unit()->label();
66 std::string yunit = ws->getAxis(1)->unit()->label();
67 if (yunit == "Angstrom^-1")
68 yunit = "1/Angstroms"; // backwards compatability with old versions
69 if (toMicroeV && (xunit == "meV"))
70 xToMicroeV = true;
71 if (toMicroeV && (yunit == "meV"))
72 yToMicroeV = true;
73
74 if (xToMicroeV)
75 xunit = "micro eV";
76 file << "# " << xcaption << " (" << xunit << ") values\n";
77 auto x = ws->points(0);
78 for (std::size_t i = 0; i < nBins; i++) {
79 double xvalue = x[i];
80 if (xToMicroeV)
81 xvalue *= 1000.;
82 file << xvalue << '\n';
83 }
84
85 if (yToMicroeV)
86 yunit = "micro eV";
87 file << "# " << ycaption << " (" << yunit << ") values\n";
88 double yvalue;
89 if ((*ws->getAxis(1)).length() == (nSpectra + 1)) {
90 for (std::size_t i = 0; i < nSpectra; i++) {
91 yvalue = 0.5 * (((*ws->getAxis(1))(i)) + ((*ws->getAxis(1))(i + 1)));
92 if (yToMicroeV)
93 yvalue *= 1000.;
94 file << yvalue << '\n';
95 }
96 } else {
97 for (std::size_t i = 0; i < nSpectra; i++) {
98 yvalue = (*ws->getAxis(1))(i);
99 if (yToMicroeV)
100 yvalue *= 1000.;
101 file << yvalue << '\n';
102 }
103 }
104 Progress progress(this, 0.0, 1.0, nSpectra);
105 for (std::size_t i = 0; i < nSpectra; i++) {
106 file << "# Group " << i << '\n';
107 auto &Y = ws->y(i);
108 auto &E = ws->e(i);
109 auto itE = E.cbegin();
110 std::for_each(Y.cbegin(), Y.cend(), [&itE, &file](const double y) { file << y << " " << *itE++ << "\n"; });
111
112 progress.report();
113 }
114 file.close();
115}
116
117} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
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.
Kernel::Logger & g_log
Definition Algorithm.h:422
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.
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