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 <Poco/File.h>
24
26using Poco::XML::AutoPtr;
27using Poco::XML::Document;
28using Poco::XML::DOMParser;
29using Poco::XML::Element;
30
31namespace Mantid::DataHandling {
32
33DECLARE_ALGORITHM(LoadParameterFile)
34
35using namespace Kernel;
36using namespace API;
37using Geometry::Instrument;
39
42 // When used as a Child Algorithm the workspace name is not used - hence the
43 // "Anonymous" to satisfy the validator
44 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("Workspace", "Anonymous", Direction::InOut),
45 "The name of the workspace to load the instrument parameters into.");
46 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::OptionalLoad, ".xml"),
47 "The filename (including its full or relative path) of a parameter "
48 "definition file. The file extension must either be .xml or .XML.");
49 declareProperty("ParameterXML", "", "The parameter definition XML as a string.");
50}
51
60 // Retrieve the filename from the properties
61 std::string filename = getPropertyValue("Filename");
62
63 // Retrieve the parameter XML string from the properties
64 const Property *const parameterXMLProperty = getProperty("ParameterXML"); // to check whether it is default
65 const std::string parameterXML = getPropertyValue("ParameterXML");
66
67 // Check the two properties (at least one must be set)
68 if (filename.empty() && parameterXMLProperty->isDefault()) {
69 throw Kernel::Exception::FileError("Either the Filename or ParameterXML "
70 "property of LoadParameterFile most be "
71 "specified to load an IDF",
72 filename);
73 }
74
75 // Get the input workspace
76 const MatrixWorkspace_sptr localWorkspace = getProperty("Workspace");
77
78 // TODO: Refactor to remove the need for the const cast (ticket #8521)
79 Instrument_sptr instrument = std::const_pointer_cast<Instrument>(localWorkspace->getInstrument()->baseInstrument());
80
81 // Set up the DOM parser and parse xml file
82 DOMParser pParser;
83 AutoPtr<Document> pDoc;
84
85 // Progress reporting object
86 Progress prog(this, 0.0, 1.0, 100);
87
88 prog.report("Parsing XML");
89 // If we've been given an XML string parse that instead
90 if (!parameterXMLProperty->isDefault()) {
91 try {
92 g_log.information("Parsing from ParameterXML property.");
93 pDoc = pParser.parseString(parameterXML);
94 } catch (Poco::Exception &exc) {
95 throw Kernel::Exception::FileError(exc.displayText() + ". Unable to parse parameter XML string", "ParameterXML");
96 } catch (...) {
97 throw Kernel::Exception::FileError("Unable to parse parameter XML string", "ParameterXML");
98 }
99 } else {
100 try {
101 // First see if the file exists
102 Poco::File ipfFile(filename);
103 if (!ipfFile.exists()) {
104 Poco::Path filePath(filename);
105 filename = Poco::Path(Kernel::ConfigService::Instance().getInstrumentDirectory())
106 .makeDirectory()
107 .setFileName(filePath.getFileName())
108 .toString();
109 }
110 g_log.information() << "Parsing from XML file: " << filename << '\n';
111 pDoc = pParser.parse(filename);
112 } catch (Poco::Exception &exc) {
113 throw Kernel::Exception::FileError(exc.displayText() + ". Unable to parse File:", filename);
114 } catch (...) {
115 throw Kernel::Exception::FileError("Unable to parse File:", filename);
116 }
117 }
118
119 // Get pointer to root element
120 Element *pRootElem = pDoc->documentElement();
121 if (!pRootElem->hasChildNodes()) {
122 throw Kernel::Exception::InstrumentDefinitionError("No root element in XML Parameter file", filename);
123 }
124
125 // Set all parameters that specified in all component-link elements of
126 // pRootElem
128 loadInstr.setComponentLinks(instrument, pRootElem, &prog, localWorkspace->getWorkspaceStartDate());
129
130 // populate parameter map of workspace
131 localWorkspace->populateInstrumentParameters();
132 if (!filename.empty()) {
133 localWorkspace->instrumentParameters().addParameterFilename(filename);
134 }
135
136 prog.resetNumSteps(1, 0.0, 1.0);
137 prog.report("Done");
138}
139
140} // 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
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
Definition: FileProperty.h:53
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:105
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.
Definition: ProgressBase.h:51
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,...
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
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