Mantid
Loading...
Searching...
No Matches
SavePDFGui.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
9#include "MantidAPI/Axis.h"
12#include "MantidAPI/Run.h"
14#include "MantidKernel/Unit.h"
15
16#include <fstream>
17
18namespace Mantid::DataHandling {
19
22
23// Register the algorithm into the AlgorithmFactory
24DECLARE_ALGORITHM(SavePDFGui)
25
26
27const std::string SavePDFGui::name() const { return "SavePDFGui"; }
28
30int SavePDFGui::version() const { return 1; }
31
33const std::string SavePDFGui::category() const { return "DataHandling\\Text"; }
34
36const std::string SavePDFGui::summary() const { return "Save files readable by PDFGui"; }
37
41 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
42 "An input workspace with units of Atomic Distance.");
43 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::Save, ".gr"),
44 "The filename to use for the saved data");
45}
46
48std::map<std::string, std::string> SavePDFGui::validateInputs() {
49 std::map<std::string, std::string> result;
50
51 // check for null pointers - this is to protect against workspace groups
52 API::MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
53 if (!inputWS) {
54 return result;
55 }
56
57 const auto nHist = static_cast<int>(inputWS->getNumberHistograms());
58 if (nHist != 1) {
59 result["InputWorkspace"] = "Workspace must contain only one spectrum";
60 } else if (std::string(inputWS->getAxis(0)->unit()->label()) != "Angstrom") {
61 result["InputWorkspace"] = "Expected x-units of Angstrom";
62 }
63
64 return result;
65}
66
70 API::MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
71 const std::string filename = getProperty("Filename");
72
73 // --------- open the file
74 std::ofstream out;
75 out.open(filename.c_str(), std::ios_base::out);
76
77 // --------- write the header in the style of
78 //#Comment: neutron, Qmin=0.5, Qmax=31.42, Qdamp=0.017659, Qbroad= 0.0191822,
79 // Temperature = 300
80 writeMetaData(out, inputWS);
81
82 // --------- write the data
83 writeWSData(out, inputWS);
84
85 // --------- close the file
86 out.close();
87}
88
89void SavePDFGui::writeMetaData(std::ofstream &out, const API::MatrixWorkspace_const_sptr &inputWS) {
90 out << "#Comment: neutron";
91 auto run = inputWS->run();
92 if (run.hasProperty("Qmin")) {
93 out << ", Qmin=" << run.getPropertyAsSingleValue("Qmin");
94 }
95 if (run.hasProperty("Qmax")) {
96 out << ", Qmax=" << run.getPropertyAsSingleValue("Qmax");
97 }
98 if (run.hasProperty("Qdamp")) {
99 out << ", Qdamp=" << run.getPropertyAsSingleValue("Qdamp");
100 }
101 if (run.hasProperty("Qbroad")) {
102 out << ", Qbroad=" << run.getPropertyAsSingleValue("Qbroad");
103 }
104 // TODO add the sample temperature
105 out << "\n";
106
107 // --------- write the label for the data
108 out << "##### start data\n";
109 // out << "#O0 rg_int sig_rg_int low_int sig_low_int rmax rhofit\n"; // TODO
110 out << "#S 1 - PDF from Mantid " << Kernel::MantidVersion::version() << "\n";
111 // out << "#P0 -22.03808 1.10131 2556.26392 0.03422 1.50 0.5985\n";
112 // // TODO
113 out << "#L r G(r) dr dG(r)\n";
114}
115
116void SavePDFGui::writeWSData(std::ofstream &out, const API::MatrixWorkspace_const_sptr &inputWS) {
117 const auto &x = inputWS->points(0);
118 const auto &y = inputWS->y(0);
119 const auto &dy = inputWS->e(0);
120 HistogramData::HistogramDx dx(y.size(), 0.0);
121 if (inputWS->sharedDx(0))
122 dx = inputWS->dx(0);
123 for (size_t i = 0; i < x.size(); ++i) {
124 out << " " << x[i] << " " << y[i] << " " << dx[i] << " " << dy[i] << "\n";
125 }
126}
127
128} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
@ Save
to specify a file to write to, the file may or may not exist
Definition: FileProperty.h:49
A property class for workspaces.
SavePDFGui : Saves a workspace containing a pair distrebution function in a format readable by the PD...
Definition: SavePDFGui.h:24
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
Definition: SavePDFGui.cpp:36
void writeWSData(std::ofstream &out, const API::MatrixWorkspace_const_sptr &inputWS)
Definition: SavePDFGui.cpp:116
std::map< std::string, std::string > validateInputs() override
Perform validation of ALL the input properties of the algorithm.
Definition: SavePDFGui.cpp:48
void writeMetaData(std::ofstream &out, const API::MatrixWorkspace_const_sptr &inputWS)
Definition: SavePDFGui.cpp:89
const std::string category() const override
Algorithm's category for identification.
Definition: SavePDFGui.cpp:33
int version() const override
Algorithm's version for identification.
Definition: SavePDFGui.cpp:30
void exec() override
Execute the algorithm.
Definition: SavePDFGui.cpp:69
void init() override
Initialize the algorithm's properties.
Definition: SavePDFGui.cpp:40
static const char * version()
The full version number.
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition: Property.h:50
@ Input
An input workspace.
Definition: Property.h:53