Newer
Older
Janik Zikovsky
committed
/*WIKI*
This algorithm allows instrument parameters to be specified in a separate file from the [[InstrumentDefinitionFile|IDF]]. The required format for this file is identical to that used for defining parameters through <component-link>s in an IDF. Below is an example of how to define a parameter named 'test' to be associated with a component named 'bank_90degnew' defined in the IDF of the HRPD instrument:
<div style="border:1pt dashed black; background:#f9f9f9;padding: 1em 0;">
<source lang="xml">
<?xml version="1.0" encoding="UTF-8" ?>
<parameter-file instrument="HRPD" valid-from="YYYY-MM-DD HH:MM:SS">
Janik Zikovsky
committed
<component-link name="bank_90degnew" >
<parameter name="test"> <value val="50.0" /> </parameter>
</component-link>
</parameter-file>
</source></div>
*WIKI*/
Anders Markvardsen
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
Anders Markvardsen
committed
#include "MantidDataHandling/LoadParameterFile.h"
Anders Markvardsen
committed
#include "MantidDataHandling/LoadInstrument.h"
Russell Taylor
committed
#include "MantidGeometry/Instrument.h"
Anders Markvardsen
committed
#include "MantidAPI/InstrumentDataService.h"
Gigg, Martyn Anthony
committed
#include "MantidGeometry/Instrument/XMLlogfile.h"
Anders Markvardsen
committed
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidGeometry/Instrument/Component.h"
Gigg, Martyn Anthony
committed
#include "MantidAPI/Progress.h"
#include "MantidAPI/FileProperty.h"
Anders Markvardsen
committed
#include "MantidKernel/ArrayProperty.h"
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/Element.h>
#include <Poco/DOM/NodeList.h>
#include <Poco/DOM/NodeIterator.h>
#include <Poco/DOM/NodeFilter.h>
#include <Poco/DOM/AutoPtr.h>
Anders Markvardsen
committed
#include <sstream>
#include "MantidGeometry/Instrument/InstrumentDefinitionParser.h"
Anders Markvardsen
committed
using Poco::XML::DOMParser;
using Poco::XML::Document;
using Poco::XML::Element;
using Poco::XML::Node;
using Poco::XML::NodeList;
using Poco::XML::NodeIterator;
using Poco::XML::NodeFilter;
using Mantid::Geometry::InstrumentDefinitionParser;
Anders Markvardsen
committed
namespace Mantid
{
namespace DataHandling
{
Anders Markvardsen
committed
DECLARE_ALGORITHM(LoadParameterFile)
Anders Markvardsen
committed
Janik Zikovsky
committed
/// Sets documentation strings for this algorithm
void LoadParameterFile::initDocs()
{
this->setWikiSummary("Loads instrument parameters into a [[workspace]]. where these parameters are associated component names as defined in Instrument Definition File ([[InstrumentDefinitionFile|IDF]]) or a string consisting of the contents of such..");
this->setOptionalMessage("Loads instrument parameters into a workspace. where these parameters are associated component names as defined in Instrument Definition File (IDF) or a string consisting of the contents of such.");
Janik Zikovsky
committed
}
Anders Markvardsen
committed
using namespace Kernel;
using namespace API;
Gigg, Martyn Anthony
committed
using Geometry::Instrument;
Russell Taylor
committed
using Geometry::Instrument_sptr;
Anders Markvardsen
committed
/// Empty default constructor
Anders Markvardsen
committed
LoadParameterFile::LoadParameterFile() : Algorithm()
Anders Markvardsen
committed
{}
/// Initialisation method.
Anders Markvardsen
committed
void LoadParameterFile::init()
Anders Markvardsen
committed
{
// When used as a Child Algorithm the workspace name is not used - hence the "Anonymous" to satisfy the validator
Anders Markvardsen
committed
declareProperty(
new WorkspaceProperty<MatrixWorkspace>("Workspace","Anonymous",Direction::InOut),
"The name of the workspace to load the instrument parameters into." );
declareProperty(new FileProperty("Filename","", FileProperty::OptionalLoad, ".xml"),
"The filename (including its full or relative path) of a parameter definition file. The file extension must either be .xml or .XML.");
declareProperty("ParameterXML","","The parameter definition XML as a string.");
Anders Markvardsen
committed
}
/** Executes the algorithm. Reading in the file and creating and populating
* the output workspace
*
* @throw FileError Thrown if unable to parse XML file
* @throw InstrumentDefinitionError Thrown if issues with the content of XML instrument file
*/
Anders Markvardsen
committed
void LoadParameterFile::exec()
Anders Markvardsen
committed
{
// Retrieve the filename from the properties
std::string filename = getPropertyValue("Filename");
// Retrieve the parameter XML string from the properties
std::string parameterXML = getPropertyValue("ParameterXML");
Anders Markvardsen
committed
// Get the input workspace
const MatrixWorkspace_sptr localWorkspace = getProperty("Workspace");
execManually(false, filename, parameterXML, localWorkspace);
Janik Zikovsky
committed
}
void LoadParameterFile::execManually(bool useString, std::string filename, std::string parameterXML, Mantid::API::ExperimentInfo_sptr localWorkspace)
Janik Zikovsky
committed
{
Russell Taylor
committed
// TODO: Refactor to remove the need for the const cast
Russell Taylor
committed
Instrument_sptr instrument = boost::const_pointer_cast<Instrument>(localWorkspace->getInstrument()->baseInstrument());
Anders Markvardsen
committed
// Set up the DOM parser and parse xml file
DOMParser pParser;
Anders Markvardsen
committed
try
{
pDoc = pParser.parse(filename);
}
Anders Markvardsen
committed
catch(Poco::Exception& exc)
{
throw Kernel::Exception::FileError(exc.displayText() + ". Unable to parse File:", filename);
}
Anders Markvardsen
committed
catch(...)
{
throw Kernel::Exception::FileError("Unable to parse File:" , filename);
}
Anders Markvardsen
committed
Anders Markvardsen
committed
// Get pointer to root element
AutoPtr<Element> pRootElem = pDoc->documentElement();
Anders Markvardsen
committed
if ( !pRootElem->hasChildNodes() )
{
Janik Zikovsky
committed
throw Kernel::Exception::InstrumentDefinitionError("No root element in XML Parameter file", filename);
Anders Markvardsen
committed
}
//
InstrumentDefinitionParser loadInstr;
Anders Markvardsen
committed
loadInstr.setComponentLinks(instrument, pRootElem);
// populate parameter map of workspace
localWorkspace->populateInstrumentParameters();
}
} // namespace DataHandling
} // namespace Mantid