20#include <Poco/AutoPtr.h>
21#include <Poco/DOM/DOMParser.h>
22#include <Poco/DOM/Document.h>
23#include <Poco/DOM/NodeList.h>
24#include <Poco/SAX/InputSource.h>
26using Poco::XML::Document;
27using Poco::XML::DOMParser;
28using Poco::XML::Element;
30using Poco::XML::NodeList;
37int getGeometryID(
const std::string &selection) {
39 if (selection ==
"Cylinder") {
41 }
else if (selection ==
"Flat plate") {
43 }
else if (selection ==
"Disc") {
59bool setLogFromElementIfNameIs(std::string
const &searchName, Element *elem,
Run &run, std::string
const &logName) {
63 const std::string termName = elem->getAttribute(
"name");
64 if (termName == searchName) {
65 std::string file = elem->innerText();
85 const std::string &extn = descriptor.extension();
89 std::istream &is = descriptor.data();
93 Poco::XML::InputSource src(is);
96 Poco::AutoPtr<Document> pDoc;
98 pDoc = pParser.parse(&src);
103 Element *pRootElem = pDoc->documentElement();
105 if (pRootElem->tagName() ==
"SASroot") {
117 "The name of the CanSAS1D file to load");
119 "The name to use for the output workspace");
131 Poco::AutoPtr<Document> pDoc;
133 pDoc = pParser.parse(fileName);
138 Element *pRootElem = pDoc->documentElement();
139 if (!pRootElem->hasChildNodes()) {
144 Poco::AutoPtr<NodeList> entryList = pRootElem->getElementsByTagName(
"SASentry");
145 size_t numEntries = entryList->length();
149 switch (numEntries) {
155 WS =
loadEntry(entryList->item(0), runName);
156 WS->mutableRun().addProperty(
"Filename", fileName);
160 auto group = std::make_shared<WorkspaceGroup>();
161 for (
unsigned int i = 0; i < numEntries; ++i) {
164 newWork->mutableRun().addProperty(
"Filename", fileName);
179 auto *workspaceElem =
dynamic_cast<Element *
>(workspaceData);
180 check(workspaceElem,
"<SASentry>");
181 runName = workspaceElem->getAttribute(
"name");
183 Poco::AutoPtr<NodeList> runs = workspaceElem->getElementsByTagName(
"Run");
184 if (runs->length() != 1) {
186 "runs, or no runs, are not currently "
190 Element *sasDataElem = workspaceElem->getChildElement(
"SASdata");
191 check(sasDataElem,
"<SASdata>");
193 Poco::AutoPtr<NodeList> idataElemList = sasDataElem->getElementsByTagName(
"Idata");
194 size_t nBins = idataElemList->length();
200 Element *titleElem = workspaceElem->getChildElement(
"Title");
201 check(titleElem,
"<Title>");
202 dataWS->setTitle(titleElem->innerText());
203 dataWS->setDistribution(
true);
204 dataWS->setYUnit(
"");
207 auto &
X = dataWS->mutableX(0);
208 auto &
Y = dataWS->mutableY(0);
209 auto &E = dataWS->mutableE(0);
211 dataWS->setPointStandardDeviations(0, nBins);
212 auto &Dx = dataWS->mutableDx(0);
215 bool isCommon =
true;
219 Node *idataElem = idataElemList->item(
index);
220 auto *elem =
dynamic_cast<Element *
>(idataElem);
224 Element *qElem = elem->getChildElement(
"Q");
226 nodeVal = qElem->innerText();
227 std::stringstream
x(nodeVal);
233 Element *dqElem = elem->getChildElement(
"Qdev");
235 nodeVal = dqElem->innerText();
236 std::stringstream dx(nodeVal);
242 Element *iElem = elem->getChildElement(
"I");
244 const std::string unit = iElem->getAttribute(
"unit");
247 else if (unit != yUnit)
249 nodeVal = iElem->innerText();
250 std::stringstream
y(nodeVal);
257 Element *idevElem = elem->getChildElement(
"Idev");
259 check(qElem,
"Idev");
260 nodeVal = idevElem->innerText();
261 std::stringstream e(nodeVal);
265 E[vecindex] = std::sqrt(
d);
272 Element *instrElem = workspaceElem->getChildElement(
"SASinstrument");
273 check(instrElem,
"SASinstrument");
274 std::string instname;
275 Element *nameElem = instrElem->getChildElement(
"name");
276 check(nameElem,
"name");
277 instname = nameElem->innerText();
284 dataWS->getAxis(0)->setUnit(
"MomentumTransfer");
286 dataWS->setYUnitLabel(yUnit);
314 std::string propName = newWorkName +
"_run";
320 container->addWorkspace(newWork);
333 loadInst->setPropertyValue(
"InstrumentName", inst_name);
337 }
catch (std::invalid_argument &) {
339 }
catch (std::runtime_error &) {
340 g_log.
information(
"Unable to successfully run LoadInstrument Child Algorithm");
349 API::Run &run = wSpace->mutableRun();
350 Element *runText = sasEntry->getChildElement(
"Run");
351 check(runText,
"Run");
354 Element *process = sasEntry->getChildElement(
"SASprocess");
356 Poco::AutoPtr<NodeList> terms = process->getElementsByTagName(
"term");
357 auto setUserFile =
false;
358 auto setBatchFile =
false;
359 for (
unsigned int i = 0; i < terms->length() && (!setUserFile || !setBatchFile); ++i) {
360 Node *term = terms->item(i);
361 auto *elem =
dynamic_cast<Element *
>(term);
362 if (!setUserFile && setLogFromElementIfNameIs(
"user_file", elem, run,
"UserFile"))
364 else if (!setBatchFile && setLogFromElementIfNameIs(
"batch_file", elem, run,
"BatchFile"))
372 auto &sample = wSpace->mutableSample();
375 auto sasSampleElement = sasEntry->getChildElement(
"SASsample");
376 check(sasSampleElement,
"<SASsample>");
377 auto thicknessElement = sasSampleElement->getChildElement(
"thickness");
378 if (thicknessElement) {
379 double thickness = std::stod(thicknessElement->innerText());
380 sample.setThickness(thickness);
383 auto sasInstrumentElement = sasEntry->getChildElement(
"SASinstrument");
384 check(sasInstrumentElement,
"<SASinstrument>");
385 auto sasCollimationElement = sasInstrumentElement->getChildElement(
"SAScollimation");
386 check(sasCollimationElement,
"<SAScollimation>");
390 bool isInValidOldFormat =
true;
392 auto name = sasCollimationElement->getChildElement(
"name");
395 isInValidOldFormat =
false;
398 if (isInValidOldFormat) {
400 auto geometryElement = sasCollimationElement->getChildElement(
"name");
401 if (geometryElement) {
402 auto geometry = geometryElement->innerText();
403 auto geometryID = getGeometryID(geometry);
404 sample.setGeometryFlag(geometryID);
408 auto widthElement = sasCollimationElement->getChildElement(
"X");
410 double width = std::stod(widthElement->innerText());
411 sample.setWidth(width);
415 auto heightElement = sasCollimationElement->getChildElement(
"Y");
417 double height = std::stod(heightElement->innerText());
423 auto aperture = sasCollimationElement->getChildElement(
"aperture");
426 auto geometry = aperture->getAttribute(
"name");
427 if (!geometry.empty()) {
428 auto geometryID = getGeometryID(Poco::XML::fromXMLString(geometry));
429 sample.setGeometryFlag(geometryID);
433 auto size = aperture->getChildElement(
"size");
436 auto widthElement = size->getChildElement(
"x");
438 double width = std::stod(widthElement->innerText());
439 sample.setWidth(width);
443 auto heightElement = size->getChildElement(
"y");
445 double height = std::stod(heightElement->innerText());
std::map< DeltaEMode::Type, std::string > index
#define DECLARE_FILELOADER_ALGORITHM(classname)
DECLARE_FILELOADER_ALGORITHM should be used in place of the standard DECLARE_ALGORITHM macro when wri...
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.
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
void addLogData(Kernel::Property *p)
Add a log entry.
This class stores information regarding an experimental run as a series of log entries.
A property class for workspaces.
This algorithm loads 1 CanSAS1d xml file into a workspace.
void exec() override
Overwrites Algorithm method.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
void runLoadInstrument(const std::string &inst_name, const API::MatrixWorkspace_sptr &localWorkspace)
Run LoadInstrument Child Algorithm.
void init() override
Overwrites Algorithm method.
void check(const Poco::XML::Element *const toCheck, const std::string &name) const
Checks if the pointer to the loaded data is not null or throws if it is.
void appendDataToOutput(const API::MatrixWorkspace_sptr &newWork, const std::string &newWorkName, const API::WorkspaceGroup_sptr &container)
Appends the new data workspace creating a workspace group if there was existing data.
void createLogs(const Poco::XML::Element *const sasEntry, const API::MatrixWorkspace_sptr &wSpace) const
Loads data into the run log.
virtual API::MatrixWorkspace_sptr loadEntry(Poco::XML::Node *const workspaceData, std::string &runName)
Loads an individual SASentry element into a new workspace.
void createSampleInformation(const Poco::XML::Element *const sasEntry, const Mantid::API::MatrixWorkspace_sptr &wSpace) const
Loads the information about hhe sample.
Records the filename and the description of failure.
Exception for when an item is not found in a collection.
Marks code as not implemented yet.
Defines a wrapper around an open file.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void information(const std::string &msg)
Logs at information level.
OptionalBool : Tri-state bool.
The concrete, templated class for properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Kernel::Logger g_log("ExperimentInfo")
static logger object
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ Output
An output workspace.