Newer
Older
Anders Markvardsen
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
Anders Markvardsen
committed
#include "MantidDataHandling/LoadParameterFile.h"
Anders Markvardsen
committed
#include "MantidDataHandling/LoadInstrument.h"
Gigg, Martyn Anthony
committed
#include "MantidGeometry/Instrument/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/File.h>
Anders Markvardsen
committed
#include <sstream>
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;
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]]). ");
this->setOptionalMessage("Loads instrument parameters into a workspace. where these parameters are associated component names as defined in Instrument Definition File (IDF).");
}
Anders Markvardsen
committed
using namespace Kernel;
using namespace API;
Gigg, Martyn Anthony
committed
using Geometry::Instrument;
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 sub-algorithm the workspace name is not used - hence the "Anonymous" to satisfy the validator
declareProperty(
new WorkspaceProperty<MatrixWorkspace>("Workspace","Anonymous",Direction::InOut),
"The name of the workspace to load the instrument parameters into" );
Peterson, Peter
committed
declareProperty(new FileProperty("Filename","", FileProperty::Load, ".xml"),
Anders Markvardsen
committed
"The filename (including its full or relative path) of an parameter\n"
"definition file");
}
/** 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");
// Get the input workspace
const MatrixWorkspace_sptr localWorkspace = getProperty("Workspace");
Gigg, Martyn Anthony
committed
boost::shared_ptr<Instrument> instrument = localWorkspace->getBaseInstrument();
Anders Markvardsen
committed
// Set up the DOM parser and parse xml file
DOMParser pParser;
Document* pDoc;
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
Element* pRootElem = pDoc->documentElement();
if ( !pRootElem->hasChildNodes() )
{
g_log.error("XML file: " + filename + "contains no root element.");
throw Kernel::Exception::InstrumentDefinitionError("No root element in XML instrument file", filename);
}
//
LoadInstrument loadInstr;
loadInstr.setComponentLinks(instrument, pRootElem);
// populate parameter map of workspace
localWorkspace->populateInstrumentParameters();
pDoc->release();
Anders Markvardsen
committed
}
} // namespace DataHandling
} // namespace Mantid