Mantid
Loading...
Searching...
No Matches
LoadParameterFile.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 +
10#include "MantidAPI/Progress.h"
15
16#include <Poco/DOM/AutoPtr.h>
17#include <Poco/DOM/DOMParser.h>
18#include <Poco/DOM/Document.h>
19#include <Poco/DOM/Element.h>
20#include <Poco/DOM/NodeFilter.h>
21#include <Poco/DOM/NodeIterator.h>
22#include <Poco/DOM/NodeList.h>
23#include <filesystem>
25using Poco::XML::AutoPtr;
26using Poco::XML::Document;
27using Poco::XML::DOMParser;
28using Poco::XML::Element;
29
30namespace Mantid::DataHandling {
31
32DECLARE_ALGORITHM(LoadParameterFile)
33
34using namespace Kernel;
35using namespace API;
36using Geometry::Instrument;
38
41 // When used as a Child Algorithm the workspace name is not used - hence the
42 // "Anonymous" to satisfy the validator
43 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("Workspace", "Anonymous", Direction::InOut),
44 "The name of the workspace to load the instrument parameters into.");
45 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::OptionalLoad, ".xml"),
46 "The filename (including its full or relative path) of a parameter "
47 "definition file. The file extension must either be .xml or .XML.");
48 declareProperty("ParameterXML", "", "The parameter definition XML as a string.");
49}
50
59 // Retrieve the filename from the properties
60 std::string filename = getPropertyValue("Filename");
61
62 // Retrieve the parameter XML string from the properties
63 const Property *const parameterXMLProperty = getProperty("ParameterXML"); // to check whether it is default
64 const std::string parameterXML = getPropertyValue("ParameterXML");
65
66 // Check the two properties (at least one must be set)
67 if (filename.empty() && parameterXMLProperty->isDefault()) {
68 throw Kernel::Exception::FileError("Either the Filename or ParameterXML "
69 "property of LoadParameterFile most be "
70 "specified to load an IDF",
71 filename);
72 }
73
74 // Get the input workspace
75 const MatrixWorkspace_sptr localWorkspace = getProperty("Workspace");
76
77 // TODO: Refactor to remove the need for the const cast (ticket #8521)
78 Instrument_sptr instrument = std::const_pointer_cast<Instrument>(localWorkspace->getInstrument()->baseInstrument());
79
80 // Set up the DOM parser and parse xml file
81 DOMParser pParser;
82 AutoPtr<Document> pDoc;
83
84 // Progress reporting object
85 Progress prog(this, 0.0, 1.0, 100);
86
87 prog.report("Parsing XML");
88 // If we've been given an XML string parse that instead
89 if (!parameterXMLProperty->isDefault()) {
90 try {
91 g_log.information("Parsing from ParameterXML property.");
92 pDoc = pParser.parseString(parameterXML);
93 } catch (Poco::Exception &exc) {
94 throw Kernel::Exception::FileError(exc.displayText() + ". Unable to parse parameter XML string", "ParameterXML");
95 } catch (...) {
96 throw Kernel::Exception::FileError("Unable to parse parameter XML string", "ParameterXML");
97 }
98 } else {
99 try {
100 // First see if the file exists
101 std::filesystem::path ipfFile(filename);
102 if (!std::filesystem::exists(ipfFile)) {
103 std::filesystem::path filePath(filename);
104 filename =
105 (std::filesystem::path(Kernel::ConfigService::Instance().getInstrumentDirectory()) / filePath.filename())
106 .string();
107 }
108 g_log.information() << "Parsing from XML file: " << filename << '\n';
109 pDoc = pParser.parse(filename);
110 } catch (Poco::Exception &exc) {
111 throw Kernel::Exception::FileError(exc.displayText() + ". Unable to parse File:", filename);
112 } catch (...) {
113 throw Kernel::Exception::FileError("Unable to parse File:", std::move(filename));
114 }
115 }
116
117 // Get pointer to root element
118 Element *pRootElem = pDoc->documentElement();
119 if (!pRootElem->hasChildNodes()) {
120 throw Kernel::Exception::InstrumentDefinitionError("No root element in XML Parameter file", filename);
121 }
122
123 // Set all parameters that specified in all component-link elements of
124 // pRootElem
126 loadInstr.setComponentLinks(instrument, pRootElem, &prog, localWorkspace->getWorkspaceStartDate());
127
128 // populate parameter map of workspace
129 localWorkspace->populateInstrumentParameters();
130 if (!filename.empty()) {
131 localWorkspace->instrumentParameters().addParameterFilename(filename);
132 }
133
134 prog.resetNumSteps(1, 0.0, 1.0);
135 prog.report("Done");
136}
137
138} // 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.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
Helper class for reporting progress from algorithms.
Definition Progress.h:25
A property class for workspaces.
void init() override
Initialisation method.
void exec() override
Executes the algorithm.
Creates an instrument data from a XML instrument description file.
void setComponentLinks(std::shared_ptr< Geometry::Instrument > &instrument, Poco::XML::Element *pRootElem, Kernel::ProgressBase *progress=nullptr, const std::string &requestedDate=std::string())
Add/overwrite any parameters specified in instrument with param values specified in <component-link> ...
Records the filename and the description of failure.
Definition Exception.h:98
Exception for errors associated with the instrument definition.
Definition Exception.h:220
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
void resetNumSteps(int64_t nsteps, double start, double end)
Change the number of steps between start/end.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Base class for properties.
Definition Property.h:94
virtual bool isDefault() const =0
Overriden function that returns if property has the same value that it was initialised with,...
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Instrument > Instrument_sptr
Shared pointer to an instrument object.
@ InOut
Both an input & output workspace.
Definition Property.h:55