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