21#include "MantidNexusGeometry/NexusGeometryParser.h"
23#include <boost/algorithm/string.hpp>
29using namespace Kernel;
31using namespace Geometry;
48 "The name of the workspace to load the instrument definition "
49 "into. Any existing instrument will be replaced.");
52 "The filename (including its full or relative path) of an instrument "
53 "definition file. The file extension must either be .xml or .XML when "
54 "specifying an instrument definition file. Files can also be .hdf5 or "
55 ".nxs for usage with NeXus Geometry files. Note Filename or "
56 "InstrumentName must be specified but not both.");
58 "Will be filled with a list of the detector ids of any "
59 "monitors loaded in to the workspace.");
61 "Name of instrument. Can be used instead of Filename to specify an"
62 "instrument definition.");
63 declareProperty(
"InstrumentXML",
"",
"The full XML instrument definition as a string.");
66 "If set to True then a 1:1 map between the spectrum numbers and "
67 "detector/monitor IDs is set up such that the detector/monitor IDs in "
68 "the IDF are ordered from smallest to largest number and then assigned "
69 "in that order to the spectra in the workspace. For example if the IDF "
70 "has defined detectors/monitors with IDs 1, 5, 10 and the workspace "
71 "contains 3 spectra with numbers 1, 2, 3 (and workspace indices 0, 1, 2) "
72 "then spectrum number 1 is associated with detector ID 1, spectrum "
73 "number 2 with detector ID 5 and spectrum number 3 with detector ID 10."
74 "If the number of spectra and detectors do not match then the operation "
75 "is performed until the maximum number of either is reached. For example "
76 "if there are 12 spectra and 50 detectors then the first 12 detectors "
77 "are assigned to the 12 spectra in the workspace."
78 "If set to False then the spectrum numbers and detector IDs of the "
79 "workspace are not modified."
80 "This property must be set to either True or False.");
93 std::shared_ptr<API::MatrixWorkspace> ws =
getProperty(
"Workspace");
105 if (instname.empty())
106 throw std::runtime_error(
"The InstrumentName property must be set when "
107 "using the InstrumentXML property.");
110 if (filename.empty())
123 if (filename.empty()) {
126 if (instname.empty()) {
127 g_log.
error(
"Either the InstrumentName or Filename property of "
128 "LoadInstrument most be specified");
130 "must be specified to load an instrument",
137 if (filename.empty()) {
142 const std::string::size_type stripPath = filename.find_last_of(
"\\/");
143 std::string instrumentFile = filename.substr(stripPath + 1, filename.size());
146 auto definitionRange = boost::ifind_first(instrumentFile,
"_Def");
147 if (definitionRange) {
148 instname = instrumentFile.substr(0, std::distance(instrumentFile.begin(), definitionRange.begin()));
150 g_log.
warning(
"The instrument definition filename does not contain "
151 "_Definition. Your instrument name will be set to: " +
153 instname = instrumentFile;
169 std::string instrumentNameMangled;
182 instrumentNameMangled = NexusGeometry::NexusGeometryParser::getMangledName(filename, instname);
184 throw std::runtime_error(
"Unknown instrument LoaderType");
188 std::lock_guard<std::recursive_mutex> lock(
m_mutex);
199 instrument = parser.
parseXML(&prog);
206 instrument->parseTreeAndCacheBeamline();
209 NexusGeometry::NexusGeometryParser::createInstrument(filename, NexusGeometry::makeLogger(&
m_log));
210 instrument = std::const_pointer_cast<Instrument>(ins);
215 ws->setInstrument(instrument);
218 ws->populateInstrumentParameters();
228 setProperty(
"MonitorList", (ws->getInstrument())->getMonitors());
234 ws->rebuildSpectraMapping();
240 const std::string &filename) {
241 g_log.
debug(
"Loading the parameter definition...");
244 const std::string::size_type dir_end = filename.find_last_of(
"\\/");
245 std::string directoryName = filename.substr(0, dir_end + 1);
248 if (!fullPathParamIDF.empty()) {
250 g_log.
debug() <<
"Parameter file: " << fullPathParamIDF <<
'\n';
256 loadParamAlg->setProperty(
"Filename", fullPathParamIDF);
257 loadParamAlg->setProperty(
"Workspace", ws);
258 loadParamAlg->execute();
259 g_log.
debug(
"Parameters loaded successfully.");
260 }
catch (std::invalid_argument &e) {
261 g_log.
information(
"LoadParameterFile: No parameter file found for this instrument");
263 }
catch (std::runtime_error &e) {
264 g_log.
information(
"Unable to successfully run LoadParameterFile Child Algorithm");
#define DECLARE_ALGORITHM(classname)
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.
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.
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
Kernel::Logger m_log
Logger for this algorithm.
@ OptionalLoad
to specify a file to read but the file doesn't have to exist
static std::string getParameterPath(const std::string &instName, const std::string &dirHint="")
Search instrument directories for Parameter file, return full path name if found, else "".
static std::string getInstrumentFilename(const std::string &instrumentName, const std::string &date="")
Get the IDF using the instrument name and date.
Helper class for reporting progress from algorithms.
A property class for workspaces.
static std::recursive_mutex m_mutex
Mutex to avoid simultaneous access.
void init() override
Initialisation method.
void runLoadParameterFile(const std::shared_ptr< API::MatrixWorkspace > &ws, const std::string &filename)
Run the Child Algorithm LoadParameters.
void exec() override
Executes the algorithm.
Creates an instrument data from a XML instrument description file.
std::shared_ptr< Instrument > parseXML(Kernel::ProgressBase *progressReporter)
Parse XML contents.
std::string getMangledName()
Handle used in the singleton constructor for instrument file should append the value file sha-1 check...
Support for a property that holds an array of values.
Records the filename and the description of failure.
Exception for when an item is not found in a collection.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
void error(const std::string &msg)
Logs at error level.
void warning(const std::string &msg)
Logs at warning level.
void information(const std::string &msg)
Logs at information level.
Validator to check that a property is not left empty.
OptionalBool : Tri-state bool.
The concrete, templated class for properties.
Base class for properties.
virtual bool isDefault() const =0
Overriden function that returns if property has the same value that it was initialised with,...
virtual std::string value() const =0
Returns the value of the property as a string.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
bool isIDF(const std::string &filename)
Determine if the Geometry file type is IDF.
const std::vector< std::string > validExtensions()
List allowed file extensions for geometry.
bool isNexus(const std::string &filename)
Determine if the Geometry file type is Nexus.
std::shared_ptr< const Instrument > Instrument_const_sptr
Shared pointer to an const instrument object.
std::shared_ptr< Instrument > Instrument_sptr
Shared pointer to an instrument object.
MANTID_KERNEL_DLL std::string loadFile(const std::string &filename)
Loads the entire contents of a text file into a string.
@ InOut
Both an input & output workspace.
@ Output
An output workspace.