Mantid
Loading...
Searching...
No Matches
SaveRMCProfile.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2020 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"
15#include "MantidKernel/Unit.h"
16
17#include <fstream>
18
19namespace Mantid::DataHandling {
20
23
24// Register the algorithm into the AlgorithmFactory
25DECLARE_ALGORITHM(SaveRMCProfile)
26
27
28const std::string SaveRMCProfile::name() const { return "SaveRMCProfile"; }
29
31int SaveRMCProfile::version() const { return 1; }
32
34const std::string SaveRMCProfile::category() const { return "DataHandling\\Text"; }
35
37const std::string SaveRMCProfile::summary() const { return "Save files readable by RMCProfile"; }
38
42 declareProperty(std::make_unique<WorkspaceProperty<>>("InputWorkspace", "", Direction::Input),
43 "An input workspace to be saved.");
44
45 declareProperty("InputType", "", "To identify what input function is being used.");
46
47 declareProperty("Title", "", "The title line for the output file.");
48
49 declareProperty(std::make_unique<API::FileProperty>("Filename", "", API::FileProperty::Save, ".fq"),
50 "The filename to use for the saved data");
51}
52
54std::map<std::string, std::string> SaveRMCProfile::validateInputs() {
55 std::map<std::string, std::string> result;
56
57 // check for null pointers - this is to protect against workspace groups
58 API::MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
59 if (!inputWS) {
60 result["InputWorkspace"] = "Workspace not found";
61 return result;
62 }
63
64 const auto nHist = static_cast<int>(inputWS->getNumberHistograms());
65 if (nHist != 1) {
66 result["InputWorkspace"] = "Workspace must contain only one spectrum";
67 }
68
69 return result;
70}
71
75 API::MatrixWorkspace_const_sptr inputWS = getProperty("InputWorkspace");
76 const std::string filename = getProperty("Filename");
77
78 // --------- open the file
79 std::ofstream out;
80 out.open(filename.c_str(), std::ios_base::out);
81
82 // --------- write the header in the style of required metadata
83 writeMetaData(out, inputWS);
84
85 // --------- write the data
86 writeWSData(out, inputWS);
87
88 // --------- close the file
89 out.close();
90}
91
92void SaveRMCProfile::writeMetaData(std::ofstream &out, const API::MatrixWorkspace_const_sptr &inputWS) {
93 const auto &y = inputWS->y(0);
94 const std::string title = getProperty("Title");
95 const std::string inputType = getProperty("InputType");
96 out << y.size() << std::endl;
97 out << "rmc " << inputType << " # " << title << std::endl;
98 std::cout << y.size() << std::endl;
99 std::cout << "rmc " << inputType << " # " << title << std::endl;
100}
101
102void SaveRMCProfile::writeWSData(std::ofstream &out, const API::MatrixWorkspace_const_sptr &inputWS) {
103 const auto &x = inputWS->points(0);
104 const auto &y = inputWS->y(0);
105 for (size_t i = 0; i < x.size(); ++i) {
106 out << " " << x[i] << " " << y[i] << "\n";
107 }
108}
109
110} // 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.
SaveRMCProfile : Saves a workspace containing a spectral density in a format readable by the RMCProfi...
const std::string category() const override
Algorithm's category for identification.
void writeMetaData(std::ofstream &out, const API::MatrixWorkspace_const_sptr &inputWS)
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
std::map< std::string, std::string > validateInputs() override
Perform validation of ALL the input properties of the algorithm.
void exec() override
Execute the algorithm.
void init() override
Initialize the algorithm's properties.
void writeWSData(std::ofstream &out, const API::MatrixWorkspace_const_sptr &inputWS)
int version() const override
Algorithm's version for identification.
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