14#include "MantidNexus/NexusFile.h"
16#include <Poco/DOM/DOMParser.h>
17#include <Poco/DOM/Document.h>
18#include <Poco/DOM/Element.h>
19#include <Poco/DOM/NodeIterator.h>
20#include <Poco/DOM/NodeList.h>
22using Poco::XML::Document;
23using Poco::XML::DOMParser;
24using Poco::XML::Element;
25using Poco::XML::NodeList;
31using namespace Kernel;
33using Types::Core::DateAndTime;
43 "The name of the workspace in which to attach the imported instrument");
45 const std::vector<std::string> exts{
".nxs",
".nxs.h5"};
48 "The name (including its full or relative path) of the Nexus file to attempt to load the instrument from.");
51 "InstrumentParentPath", std::string(
""),
52 "Path name within the Nexus tree of the folder containing the instrument folder. "
53 "For example it is 'raw_data_1' for an ISIS raw Nexus file and 'mantid_workspace_1' for a processed nexus file. "
54 "Only a one level path is curently supported",
57 "Full path name of Parameter Correction file. "
58 "This should only be used in a situation,where the default full file path is inconvenient.",
75 std::string instrumentParentAddress =
getPropertyValue(
"InstrumentParentPath");
78 Nexus::File nxfile(filename);
80 nxfile.openAddress(instrumentParentAddress);
83 localWorkspace->loadInstrumentInfoNexus(filename, &nxfile);
86 std::string parameterCorrectionFile =
getPropertyValue(
"ParameterCorrectionFilePath");
87 if (parameterCorrectionFile.empty()) {
90 g_log.
debug() <<
"Parameter correction file: " << parameterCorrectionFile <<
"\n";
93 std::string correctionParameterFile;
95 if (!parameterCorrectionFile.empty()) {
99 g_log.
notice() <<
"Using parameter correction file: " << parameterCorrectionFile <<
".\n";
101 correctionParameterFile, append);
106 if (correctionParameterFile.empty() || append) {
109 g_log.
notice() <<
"Parameters to be replaced are cleared.\n";
110 localWorkspace->getInstrument()->getParameterMap()->clear();
114 if (!correctionParameterFile.empty()) {
115 std::filesystem::path corrFilePath(parameterCorrectionFile);
116 g_log.
debug() <<
"Correction file path: " << corrFilePath.string() <<
"\n";
117 std::filesystem::path corrDirPath = corrFilePath.parent_path();
118 g_log.
debug() <<
"Correction directory path: " << corrDirPath.string() <<
"\n";
119 std::filesystem::path corrParamFile(corrDirPath / correctionParameterFile);
121 g_log.
notice() <<
"Using correction parameter file: " << corrParamFile.string() <<
" to append parameters.\n";
123 g_log.
notice() <<
"Using correction parameter file: " << corrParamFile.string() <<
" to replace parameters.\n";
127 g_log.
notice() <<
"No correction parameter file applies to the date for "
128 "correction file.\n";
139 std::vector<std::string> directoryNames = ConfigService::Instance().getInstrumentDirectories();
140 for (
auto &directoryName : directoryNames) {
143 std::filesystem::path iPath =
144 std::filesystem::path(directoryName) /
"embedded_instrument_corrections";
146 if (std::filesystem::exists(iPath) && std::filesystem::is_directory(iPath)) {
147 std::filesystem::path ipFile = iPath / (instName +
"_Parameter_Corrections.xml");
148 if (std::filesystem::exists(ipFile) && std::filesystem::is_regular_file(ipFile)) {
149 return ipFile.string();
170 std::string ¶meter_file,
bool &append) {
178 g_log.
notice() <<
"No date is supplied for parameter correction file " << correction_file
179 <<
". Correction file is ignored.\n";
188 Poco::AutoPtr<Document> pDoc;
190 pDoc = pParser.parseString(xmlText);
191 }
catch (Poco::Exception &exc) {
198 Element *pRootElem = pDoc->documentElement();
199 if (!pRootElem->hasChildNodes()) {
200 g_log.
error(
"Parameter correction file: " + correction_file +
"contains no XML root element.");
206 g_log.
notice() <<
"Date for correction file " << date <<
"\n";
207 DateAndTime externalDate(date);
210 Poco::AutoPtr<NodeList> correctionNodeList = pRootElem->getElementsByTagName(
"correction");
211 for (
unsigned long i = 0; i < correctionNodeList->length(); ++i) {
213 auto *corr =
dynamic_cast<Element *
>(correctionNodeList->item(i));
215 DateAndTime start(corr->getAttribute(
"valid-from"));
216 DateAndTime end(corr->getAttribute(
"valid-to"));
217 if (start <= externalDate && externalDate <= end) {
218 parameter_file = corr->getAttribute(
"file");
219 append = (corr->getAttribute(
"append") ==
"true");
223 g_log.
error(
"Parameter correction file: " + correction_file +
"contains an invalid correction element.");
240 std::string parameterString;
243 nxfile->openGroup(
"instrument",
"NXinstrument");
244 localWorkspace->loadInstrumentParametersNexus(nxfile, parameterString);
245 nxfile->closeGroup();
249 localWorkspace->populateInstrumentParameters();
251 if (parameterString.empty()) {
254 g_log.
notice() <<
"Found Instrument parameter map entry in Nexus file, "
255 "which is loaded.\n\n";
257 localWorkspace->readParameterMap(parameterString);
263 g_log.
debug(
"Loading the parameter definition...");
266 const std::string instName = ws->getInstrument()->getName();
267 const std::string runDate = ws->getWorkspaceStartDate();
269 if (!fullPathParamIDF.empty()) {
270 g_log.
debug() <<
"Parameter file: " << fullPathParamIDF <<
'\n';
273 g_log.
notice() <<
"No Instrument parameter map entry found.\n\n";
286 loadParamAlg->setProperty(
"Filename", fullPathName);
287 loadParamAlg->setProperty(
"Workspace", localWorkspace);
288 loadParamAlg->execute();
289 g_log.
notice() <<
"Instrument parameter file: " << fullPathName <<
" has been loaded.\n\n";
291 }
catch (std::invalid_argument &e) {
292 g_log.
information(
"LoadParameterFile: No parameter file found for this instrument");
294 }
catch (std::runtime_error &e) {
295 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.
@ Load
allowed here which will be passed to the algorithm
static std::string getParameterFilename(const std::string &instrumentName, const std::string &date="", const std::string &dirHint="")
Get the Parameter file using the instrument name and date, optionally searching dirHint first before ...
A property class for workspaces.
std::string getParameterCorrectionFile(const std::string &instName)
Get the parameter correction file, if it exists else return "".
void exec() override
Overwrites Algorithm method.
void readParameterCorrectionFile(const std::string &correction_file, const std::string &date, std::string ¶meter_file, bool &append)
Read parameter correction file, return applicabel parameter file and whether to append.
void LoadParameters(Nexus::File *nxfile, const API::MatrixWorkspace_sptr &localWorkspace)
Load the parameters from Nexus file if possible, else from parameter file, into workspace.
bool runLoadParameterFile(const std::string &fullPathName, const API::MatrixWorkspace_sptr &localWorkspace)
Load Parameter File specified by full pathname into given workspace, return success.
void init() override
Overwrites Algorithm method. Does nothing at present.
LoadIDFFromNexus()
Default constructor.
void loadParameterFile(const std::shared_ptr< API::MatrixWorkspace > &ws)
Search for and then load parameter file.
Records the filename and the description of failure.
Exception for errors associated with the instrument definition.
void debug(const std::string &msg)
Logs at debug level.
void notice(const std::string &msg)
Logs at notice level.
void error(const std::string &msg)
Logs at error level.
void information(const std::string &msg)
Logs at information level.
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
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.
@ Input
An input workspace.