Mantid
Loading...
Searching...
No Matches
LoadGaussCube.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2023 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 +
7
14
15#include <fstream>
16
17using namespace Mantid::API;
18using namespace Mantid::Kernel::Strings;
19
20namespace Mantid {
21namespace MDAlgorithms {
24
25// Register the algorithm into the AlgorithmFactory
26DECLARE_ALGORITHM(LoadGaussCube)
27
28//----------------------------------------------------------------------------------------------
29
30
31const std::string LoadGaussCube::name() const { return "LoadGaussCube"; }
32
34int LoadGaussCube::version() const { return 1; }
35
37const std::string LoadGaussCube::category() const { return "MDAlgorithms"; }
38
40const std::string LoadGaussCube::summary() const {
41 return "Algorithm to load gauss cube files and output a 3D MDHistoWorkspace.";
42}
43
44//----------------------------------------------------------------------------------------------
48 auto algcreateMD = AlgorithmManager::Instance().createUnmanaged("CreateMDHistoWorkspace");
49 algcreateMD->initialize();
50
51 const std::vector<std::string> exts{".cube"};
52 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load, exts),
53 "Path to gauss cube file (with extension .cube). Note algorithm assumes XYZ ordering.");
54
55 declareProperty(std::make_unique<WorkspaceProperty<IMDHistoWorkspace>>("OutputWorkspace", "", Direction::Output),
56 "3D MDHistoWorkspace containing the data in the .cube file.");
57
58 copyProperty(algcreateMD, "Names");
59 copyProperty(algcreateMD, "Frames");
60 copyProperty(algcreateMD, "Units");
61}
62
63//----------------------------------------------------------------------------------------------
66std::map<std::string, std::string> LoadGaussCube::validateInputs() {
67 // check three dimensions specified
68 std::map<std::string, std::string> errors;
69 std::vector<std::string> prop_names = {"Names", "Frames", "Units"};
70 for (auto name : prop_names) {
71 std::vector<std::string> prop;
72 if (name == "Names") {
74 } else {
75 prop = getProperty(name);
76 }
77 if (prop.size() != 3) {
78 errors.emplace(name, "Property must contain three elements (workspace must have three dimensions).");
79 }
80 }
81 return errors;
82}
83
84//----------------------------------------------------------------------------------------------
88 std::string Filename = getProperty("Filename");
89
90 // open file
91 std::ifstream in(Filename.c_str());
92 // skip header
93 for (int indexOfLine = 0; indexOfLine < 2; ++indexOfLine) {
94 in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
95 }
96
97 // read lower extent
98 std::vector<double> extents(6, 0.0);
99 getWord(in, false); // ignore the first element of this row
100 std::string str;
101 for (size_t indexOfDim = 0; indexOfDim < 3; ++indexOfDim) {
102 str = getWord(in, false);
103 if (str.length() < 1) {
104 throw std::logic_error(std::string(
105 "Third line must contain 4 elements (first isignored) and subsequent 3 are the lower extents of workspace."));
106 }
107 extents[2 * indexOfDim] = std::stod(str);
108 }
109 readToEndOfLine(in, true);
110
111 // read nbins and upper extent
112 std::vector<int> nbins(3, 0);
113 for (size_t indexOfDim = 0; indexOfDim < 3; ++indexOfDim) {
114 nbins[indexOfDim] = std::stoi(getWord(in, false)); // first element in row
115 for (size_t indexOfWordToSkip = 0; indexOfWordToSkip < indexOfDim; ++indexOfWordToSkip) {
116 str = getWord(in, true); // ignore
117 }
118 extents[2 * indexOfDim + 1] = extents[2 * indexOfDim] + std::stod(getWord(in, true));
119 if (indexOfDim < 2) {
120 readToEndOfLine(in, true); // already at EOL for indexOfDim==2
121 }
122 }
123
124 // read signal array
125 size_t nBinsTotal = nbins[0] * nbins[1] * nbins[2];
126 std::vector<double> signal(nBinsTotal, 0.0);
127 std::vector<double> error(nBinsTotal, 0.0);
128
129 for (size_t indexOfBin = 0; indexOfBin < nBinsTotal && in.good(); ++indexOfBin) {
130 signal[indexOfBin] = std::stod(getWord(in, true));
131 }
132
133 // make output workspace
135 alg.initialize();
136 alg.setProperty("SignalInput", signal);
137 alg.setProperty("ErrorInput", error);
138 alg.setProperty("Dimensionality", 3);
139 alg.setProperty("NumberOfBins", nbins);
140 alg.setProperty("Extents", extents);
141 alg.setProperty("Names", parseNames(getProperty("Names")));
142 alg.setPropertyValue("Frames", getPropertyValue("Frames"));
143 alg.setPropertyValue("Units", getPropertyValue("Units"));
144 alg.setPropertyValue("OutputWorkspace", getPropertyValue("OutputWorkspace"));
145 alg.execute();
146
147 setPropertyValue("OutputWorkspace", alg.getPropertyValue("OutputWorkspace"));
148}
149
150} // namespace MDAlgorithms
151} // namespace Mantid
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
double error
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
void initialize() override
Initialization method invoked by the framework.
bool execute() override final
The actions to be performed by the algorithm on a dataset.
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
@ Load
allowed here which will be passed to the algorithm
void copyProperty(const API::Algorithm_sptr &alg, const std::string &name)
Copy a property from an existing algorithm.
Kernel::IPropertyManager::TypedValue getProperty(const std::string &name) const override
Get the property held by this object.
std::string getPropertyValue(const std::string &name) const override
Get the property held by this object.
A property class for workspaces.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
LoadGaussCube : TODO: DESCRIPTION.
void exec() override
Execute the algorithm.
void init() override
Initialize the algorithm's properties.
int version() const override
Algorithm's version for identification.
const std::string name() const override
Algorithms name for identification.
std::map< std::string, std::string > validateInputs() override
Validate input.
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
const std::string category() const override
Algorithm's category for identification.
Holds support functions for strings.
MANTID_KERNEL_DLL void readToEndOfLine(std::istream &in, bool ConsumeEOL)
Eat everything from the stream until the next EOL.
Definition Strings.cpp:982
MANTID_KERNEL_DLL std::string getWord(std::istream &in, bool consumeEOL)
Returns the next word in the stream.
Definition Strings.cpp:931
std::vector< std::string > MANTID_MDALGORITHMS_DLL parseNames(const std::string &names_string)
Helper class which provides the Collimation Length for SANS instruments.
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50
@ Output
An output workspace.
Definition Property.h:54