Newer
Older
Anders Markvardsen
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
Anders Markvardsen
committed
#include "MantidDataHandling/LoadParameterFile.h"
Anders Markvardsen
committed
#include "MantidDataHandling/LoadInstrument.h"
#include "MantidAPI/Instrument.h"
#include "MantidAPI/InstrumentDataService.h"
#include "MantidAPI/XMLlogfile.h"
#include "MantidAPI/Progress.h"
Gigg, Martyn Anthony
committed
#include "MantidAPI/FileProperty.h"
Anders Markvardsen
committed
#include "MantidGeometry/Instrument/Detector.h"
#include "MantidGeometry/Instrument/ParametrizedComponent.h"
#include "MantidGeometry/Instrument/Component.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"
#include "MantidKernel/ArrayProperty.h"
#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
using namespace Kernel;
using namespace API;
/// 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
{
// Retrieve the filename from the properties
std::string filename = getPropertyValue("Filename");
// Get the input workspace
const MatrixWorkspace_sptr localWorkspace = getProperty("Workspace");
// Remove the path from the filename for use with the InstrumentDataService
//const int stripPath = m_filename.find_last_of("\\/");
//std::string instrumentFile = m_filename.substr(stripPath+1,m_filename.size());
boost::shared_ptr<API::Instrument> instrument = localWorkspace->getBaseInstrument();
// Set up the DOM parser and parse xml file
DOMParser pParser;
Document* pDoc;
try
{
pDoc = pParser.parse(filename);
}
catch(...)
{
g_log.error("Unable to parse file " + filename);
throw Kernel::Exception::FileError("Unable to parse File:" , filename);
}
// 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();
}
} // namespace DataHandling
} // namespace Mantid