Mantid
Loading...
Searching...
No Matches
SaveNexus.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 +
7// SaveNeXus
8// @author Freddie Akeroyd, STFC ISIS Faility
9// @author Ronald Fowler, STFC eScience. Modified to fit with SaveNexusProcessed
16
17#include <cmath>
18#include <memory>
19
20namespace Mantid::DataHandling {
21
22// Register the algorithm into the algorithm factory
23DECLARE_ALGORITHM(SaveNexus)
24
25using namespace Kernel;
26using namespace API;
27using namespace DataObjects;
28
33 // Declare required parameters, filename with ext {.nxs,.nx5,.xml} and input
34 // workspace
35 declareProperty(std::make_unique<WorkspaceProperty<Workspace>>("InputWorkspace", "", Direction::Input),
36 "Name of the workspace to be saved");
37
38 const std::vector<std::string> fileExts{".nxs", ".nx5", ".xml"};
39 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Save, fileExts),
40 "The name of the Nexus file to write, as a full or relative\n"
41 "path");
42 //
43 // Declare optional input parameters
44 // These are:
45 // Title - string to describe data
46 // EntryNumber - integer >0 to be used in entry name "mantid_workspace_<n>"
47 // Within a file the entries will be sequential from
48 // 1.
49 // This option should allow overwrite of existing
50 // entry,
51 // *not* addition of out-of-sequence entry numbers.
52 // spectrum_min, spectrum_max - range of "spectra" numbers to write
53 // spectrum_list list of spectra values to write
54 //
55 declareProperty("Title", "", std::make_shared<NullValidator>(), "A title to describe the saved workspace");
56
57 auto mustBePositive = std::make_shared<BoundedValidator<int>>();
58 mustBePositive->setLower(0);
59 // declareProperty("EntryNumber", Mantid::EMPTY_INT(), mustBePositive,
60 // "(Not implemented yet) The index number of the workspace within the Nexus
61 // file\n"
62 // "(default leave unchanged)" );
63 declareProperty("WorkspaceIndexMin", 0, mustBePositive,
64 "Number of first WorkspaceIndex to read, only for single period data.\n"
65 "Not yet implemented");
66 declareProperty("WorkspaceIndexMax", Mantid::EMPTY_INT(), mustBePositive,
67 "Number of last WorkspaceIndex to read, only for single period data.\n"
68 "Not yet implemented.");
69 declareProperty(std::make_unique<ArrayProperty<int>>("WorkspaceIndexList"),
70 "List of WorkspaceIndex numbers to read, only for single period data.\n"
71 "Not yet implemented");
72 declareProperty("Append", false,
73 "Determines whether .nxs file needs to be\n"
74 "over written or appended");
75 // option which might be required in future - should be a choice e.g.
76 // MantidProcessed/Muon1
77 // declareProperty("Filetype","",new NullValidator<std::string>);
78}
79
86 // Retrieve the filename from the properties
87 m_filename = getPropertyValue("FileName");
88 m_inputWorkspace = getProperty("InputWorkspace");
89
91}
98void SaveNexus::setOtherProperties(IAlgorithm *alg, const std::string &propertyName, const std::string &propertyValue,
99 int perioidNum) {
100 if (propertyName == "Append") {
101 if (perioidNum != 1) {
102 alg->setPropertyValue(propertyName, "1");
103 } else
104 alg->setPropertyValue(propertyName, propertyValue);
105 } else
106 Algorithm::setOtherProperties(alg, propertyName, propertyValue, perioidNum);
107}
109 IAlgorithm_sptr saveNexusPro = createChildAlgorithm("SaveNexusProcessed", 0.0, 1.0, true);
110 // Pass through the same output filename
111 saveNexusPro->setPropertyValue("Filename", m_filename);
112 // Set the workspace property
113 std::string inputWorkspace = "InputWorkspace";
114 saveNexusPro->setProperty(inputWorkspace, m_inputWorkspace);
115 //
116 std::vector<int> specList = getProperty("WorkspaceIndexList");
117 if (!specList.empty())
118 saveNexusPro->setPropertyValue("WorkspaceIndexList", getPropertyValue("WorkspaceIndexList"));
119 //
120 int specMax = getProperty("WorkspaceIndexMax");
121 if (specMax != Mantid::EMPTY_INT()) {
122 saveNexusPro->setPropertyValue("WorkspaceIndexMax", getPropertyValue("WorkspaceIndexMax"));
123 saveNexusPro->setPropertyValue("WorkspaceIndexMin", getPropertyValue("WorkspaceIndexMin"));
124 }
125 std::string title = getProperty("Title");
126 if (!title.empty())
127 saveNexusPro->setPropertyValue("Title", getPropertyValue("Title"));
128
129 // Pass through the append property
130 saveNexusPro->setProperty<bool>("Append", getProperty("Append"));
131
132 // If we're tracking history, add the entry before we save it to file
133 if (trackingHistory()) {
134 m_history->fillAlgorithmHistory(this, Mantid::Types::Core::DateAndTime::getCurrentTime(), 0,
136 if (!isChild()) {
137 m_inputWorkspace->history().addHistory(m_history);
138 }
139 // this is a child algorithm, but we still want to keep the history.
141 m_parentHistory->addChildHistory(m_history);
142 }
143 }
144 // Now execute the Child Algorithm. Catch and log any error, but don't stop.
145 try {
146 saveNexusPro->execute();
147 } catch (std::runtime_error &) {
148 g_log.error("Unable to successfully run SaveNexusprocessed Child Algorithm");
149 }
150 if (!saveNexusPro->isExecuted())
151 g_log.error("Unable to successfully run SaveNexusProcessed Child Algorithm");
152 //
153 progress(1);
154}
155
160 this->exec();
161
162 return true;
163}
164
165} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
std::shared_ptr< AlgorithmHistory > m_parentHistory
Pointer to the parent history object (if set)
Definition Algorithm.h:425
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
std::shared_ptr< AlgorithmHistory > m_history
Pointer to the history for the algorithm being executed.
Definition Algorithm.h:418
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
bool isChild() const override
To query whether algorithm is a child.
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.
virtual void setOtherProperties(IAlgorithm *alg, const std::string &propertyName, const std::string &propertyValue, int periodNum)
Virtual method to set the non workspace properties for this algorithm.
bool trackingHistory()
get whether we are tracking the history for this algorithm,
bool isRecordingHistoryForChild()
Definition Algorithm.h:225
static size_t g_execCount
Counter to keep track of algorithm execution order.
Definition Algorithm.h:410
@ Save
to specify a file to write to, the file may or may not exist
IAlgorithm is the interface implemented by the Algorithm base class.
Definition IAlgorithm.h:45
A property class for workspaces.
void setOtherProperties(IAlgorithm *alg, const std::string &propertyName, const std::string &propertyValue, int perioidNum) override
sets non workspace properties for the algorithm
Definition SaveNexus.cpp:98
std::string m_filename
The name and path of the input file.
Definition SaveNexus.h:55
void exec() override
Overwrites Algorithm method.
Definition SaveNexus.cpp:85
void runSaveNexusProcessed()
Method to execute SNP Child Algorithm.
void init() override
Overwrites Algorithm method.
Definition SaveNexus.cpp:32
bool processGroups() override
Override process groups.
API::Workspace_sptr m_inputWorkspace
Pointer to the local workspace.
Definition SaveNexus.h:63
Support for a property that holds an array of values.
virtual void setPropertyValue(const std::string &name, const std::string &value)=0
Sets property value from a string.
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
constexpr int EMPTY_INT() noexcept
Returns what we consider an "empty" integer within a property.
Definition EmptyValues.h:24
@ Input
An input workspace.
Definition Property.h:53