Newer
Older
#include <fstream>
#include <Poco/Path.h>
#include <Poco/File.h>
#include <Poco/DOM/DOMParser.h>
#include <Poco/DOM/Document.h>
#include <Poco/DOM/Element.h>
#include <Poco/DOM/NodeIterator.h>
#include <Poco/DOM/NodeFilter.h>
#include <Poco/DOM/NodeList.h>
#include <Poco/DOM/AutoPtr.h>
#include <Poco/SAX/InputSource.h>
#include "MantidAPI/IEventWorkspace.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/WorkspaceOpOverloads.h"
#include "MantidDataHandling/LoadPreNexus.h"
#include "MantidKernel/System.h"
#include "MantidKernel/VisibleWhenProperty.h"
#include "MantidKernel/BoundedValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidDataHandling/LoadEventNexus.h"
#include "MantidDataHandling/LoadTOFRawNexus.h"
using namespace Mantid::Kernel;
using namespace Mantid::API;
using std::vector;
namespace Mantid {
namespace DataHandling {
DECLARE_FILELOADER_ALGORITHM(LoadPreNexus)
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
static const string RUNINFO_PARAM("Filename");
static const string MAP_PARAM("MappingFilename");
//----------------------------------------------------------------------------------------------
/// Constructor
LoadPreNexus::LoadPreNexus() {}
//----------------------------------------------------------------------------------------------
/// Destructor
LoadPreNexus::~LoadPreNexus() {}
//----------------------------------------------------------------------------------------------
/// @copydoc Mantid::API::IAlgorithm::name()
const std::string LoadPreNexus::name() const { return "LoadPreNexus"; }
/// @copydoc Mantid::API::IAlgorithm::version()
int LoadPreNexus::version() const { return 1; }
/// @copydoc Mantid::API::IAlgorithm::category()
const std::string LoadPreNexus::category() const {
return "DataHandling\\PreNexus;Workflow\\DataHandling";
}
/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not
* be used
*/
int LoadPreNexus::confidence(Kernel::FileDescriptor &descriptor) const {
const std::string &filename = descriptor.filename();
if (filename.compare(filename.size() - 12, 12, "_runinfo.xml") == 0)
return 80;
else
return 0;
}
//----------------------------------------------------------------------------------------------
/// @copydoc Mantid::API::Algorithm::init()
void LoadPreNexus::init() {
// runfile to read in
declareProperty(
new FileProperty(RUNINFO_PARAM, "", FileProperty::Load, "_runinfo.xml"),
"The name of the runinfo file to read, including its full or relative "
"path.");
// copied (by hand) from LoadEventPreNexus2
declareProperty(
new FileProperty(MAP_PARAM, "", FileProperty::OptionalLoad, ".dat"),
"File containing the pixel mapping (DAS pixels to pixel IDs) file "
"(typically INSTRUMENT_TS_YYYY_MM_DD.dat). The filename will be found "
"automatically if not specified.");
auto mustBePositive = boost::make_shared<BoundedValidator<int>>();
mustBePositive->setLower(1);
declareProperty("ChunkNumber", EMPTY_INT(), mustBePositive,
"If loading the file by sections ('chunks'), this is the "
"section number of this execution of the algorithm.");
declareProperty("TotalChunks", EMPTY_INT(), mustBePositive,
"If loading the file by sections ('chunks'), this is the "
"total number of sections.");
// TotalChunks is only meaningful if ChunkNumber is set
// Would be nice to be able to restrict ChunkNumber to be <= TotalChunks at
// validation
setPropertySettings("TotalChunks",
new VisibleWhenProperty("ChunkNumber", IS_NOT_DEFAULT));
std::vector<std::string> propOptions;
propOptions.emplace_back("Auto");
propOptions.emplace_back("Serial");
propOptions.emplace_back("Parallel");
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
declareProperty("UseParallelProcessing", "Auto",
boost::make_shared<StringListValidator>(propOptions),
"Use multiple cores for loading the data?\n"
" Auto: Use serial loading for small data sets, parallel "
"for large data sets.\n"
" Serial: Use a single core.\n"
" Parallel: Use all available cores.");
declareProperty(
new PropertyWithValue<bool>("LoadMonitors", true, Direction::Input),
"Load the monitors from the file.");
declareProperty(
new WorkspaceProperty<>("OutputWorkspace", "", Direction::Output),
"An output workspace.");
}
//----------------------------------------------------------------------------------------------
/// @copydoc Mantid::API::Algorithm::exec()
void LoadPreNexus::exec() {
string runinfo = this->getPropertyValue(RUNINFO_PARAM);
string mapfile = this->getPropertyValue(MAP_PARAM);
int chunkTotal = this->getProperty("TotalChunks");
int chunkNumber = this->getProperty("ChunkNumber");
if (isEmpty(chunkTotal) || isEmpty(chunkNumber)) {
chunkNumber = EMPTY_INT();
chunkTotal = EMPTY_INT();
} else {
if (chunkNumber > chunkTotal)
throw std::out_of_range("ChunkNumber cannot be larger than TotalChunks");
string useParallel = this->getProperty("UseParallelProcessing");
string wsname = this->getProperty("OutputWorkspace");
bool loadmonitors = this->getProperty("LoadMonitors");
// determine the event file names
Progress prog(this, 0., .1, 1);
vector<string> eventFilenames;
string dataDir;
this->parseRuninfo(runinfo, dataDir, eventFilenames);
prog.doReport("parsed runinfo file");
// do math for the progress bar
size_t numFiles = eventFilenames.size() + 1; // extra 1 is nexus logs
if (loadmonitors)
numFiles++;
double prog_start = .1;
double prog_delta = (1. - prog_start) / static_cast<double>(numFiles);
// load event files
string temp_wsname;
for (size_t i = 0; i < eventFilenames.size(); i++) {
if (i == 0)
temp_wsname = wsname;
temp_wsname = "__" + wsname + "_temp__";
IAlgorithm_sptr alg = this->createChildAlgorithm(
"LoadEventPreNexus", prog_start, prog_start + prog_delta);
alg->setProperty("EventFilename", dataDir + eventFilenames[i]);
alg->setProperty("MappingFilename", mapfile);
alg->setProperty("ChunkNumber", chunkNumber);
alg->setProperty("TotalChunks", chunkTotal);
alg->setProperty("UseParallelProcessing", useParallel);
alg->setPropertyValue("OutputWorkspace", temp_wsname);
alg->executeAsChildAlg();
prog_start += prog_delta;
if (i == 0) {
m_outputWorkspace = alg->getProperty("OutputWorkspace");
} else {
IEventWorkspace_sptr tempws = alg->getProperty("OutputWorkspace");
// clean up properties before adding data
Run &run = tempws->mutableRun();
if (run.hasProperty("gd_prtn_chrg"))
run.removeProperty("gd_prtn_chrg");
if (run.hasProperty("proton_charge"))
run.removeProperty("proton_charge");
m_outputWorkspace += tempws;
// load the logs
this->runLoadNexusLogs(runinfo, dataDir, prog_start, prog_start + prog_delta);
prog_start += prog_delta;
// publish output workspace
this->setProperty("OutputWorkspace", m_outputWorkspace);
// load the monitor
if (loadmonitors) {
this->runLoadMonitors(prog_start, 1.);
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
}
/**
* Parse the runinfo file to find the names of the neutron event files.
*
* @param runinfo Runinfo file with full path.
* @param dataDir Directory where the runinfo file lives.
* @param eventFilenames vector of all possible event files. This is filled by
*the algorithm.
*/
void LoadPreNexus::parseRuninfo(const string &runinfo, string &dataDir,
vector<string> &eventFilenames) {
eventFilenames.clear();
// Create a Poco Path object for runinfo filename
Poco::Path runinfoPath(runinfo, Poco::Path::PATH_GUESS);
// Now lets get the directory
Poco::Path dirPath(runinfoPath.parent());
dataDir = dirPath.absolute().toString();
g_log.debug() << "Data directory \"" << dataDir << "\"\n";
std::ifstream in(runinfo.c_str());
Poco::XML::InputSource src(in);
Poco::XML::DOMParser parser;
Poco::AutoPtr<Poco::XML::Document> pDoc = parser.parse(&src);
Poco::XML::NodeIterator it(pDoc, Poco::XML::NodeFilter::SHOW_ELEMENT);
Poco::XML::Node *pNode = it.nextNode(); // root node
while (pNode) {
if (pNode->nodeName() == "RunInfo") // standard name for this type
pNode = pNode->firstChild();
while (pNode) {
if (pNode->nodeName() == "FileList") {
pNode = pNode->firstChild();
while (pNode) {
if (pNode->nodeName() == "DataList") {
pNode = pNode->firstChild();
while (pNode) {
if (pNode->nodeName() == "scattering") {
Poco::XML::Element *element =
static_cast<Poco::XML::Element *>(pNode);
eventFilenames.push_back(element->getAttribute("name"));
}
pNode = pNode->nextSibling();
}
} else // search for DataList
pNode = pNode->nextSibling();
} else // search for FileList
pNode = pNode->nextSibling();
} else // search for RunInfo
pNode = pNode->nextSibling();
// report the results to the log
if (eventFilenames.size() == 1) {
g_log.debug() << "Found 1 event file: \"" << eventFilenames[0] << "\"\n";
} else {
g_log.debug() << "Found " << eventFilenames.size() << " event files:";
for (size_t i = 0; i < eventFilenames.size(); i++) {
g_log.debug() << "\"" << eventFilenames[i] << "\" ";
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
}
/**
* Load logs from a nexus file onto the workspace.
*
* @param runinfo Runinfo file with full path.
* @param dataDir Directory where the runinfo file lives.
* @param prog_start Starting position for the progress bar.
* @param prog_stop Ending position for the progress bar.
*/
void LoadPreNexus::runLoadNexusLogs(const string &runinfo,
const string &dataDir,
const double prog_start,
const double prog_stop) {
// determine the name of the file "inst_run"
string shortName = runinfo.substr(runinfo.find_last_of("/\\") + 1);
shortName = shortName.substr(0, shortName.find("_runinfo.xml"));
g_log.debug() << "SHORTNAME = \"" << shortName << "\"\n";
// put together a list of possible locations
vector<string> possibilities;
possibilities.push_back(dataDir + shortName +
"_event.nxs"); // next to runinfo
possibilities.push_back(dataDir + shortName + "_histo.nxs");
possibilities.push_back(dataDir + shortName + ".nxs");
possibilities.push_back(dataDir + "../NeXus/" + shortName +
"_event.nxs"); // in NeXus directory
possibilities.push_back(dataDir + "../NeXus/" + shortName + "_histo.nxs");
possibilities.push_back(dataDir + "../NeXus/" + shortName + ".nxs");
// run the algorithm
bool loadedLogs = false;
for (size_t i = 0; i < possibilities.size(); i++) {
if (Poco::File(possibilities[i]).exists()) {
g_log.information() << "Loading logs from \"" << possibilities[i]
<< "\"\n";
IAlgorithm_sptr alg =
this->createChildAlgorithm("LoadNexusLogs", prog_start, prog_stop);
alg->setProperty("Workspace", m_outputWorkspace);
alg->setProperty("Filename", possibilities[i]);
alg->setProperty("OverwriteLogs", false);
alg->executeAsChildAlg();
loadedLogs = true;
// Reload instrument so SNAP can use log values
std::string entry_name = LoadTOFRawNexus::getEntryName(possibilities[i]);
LoadEventNexus::runLoadInstrument(possibilities[i], m_outputWorkspace,
entry_name, this);
break;
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
if (!loadedLogs)
g_log.notice() << "Did not find a nexus file to load logs from\n";
}
/**
* Load the monitor files.
*
* @param prog_start Starting position for the progress bar.
* @param prog_stop Ending position for the progress bar.
*/
void LoadPreNexus::runLoadMonitors(const double prog_start,
const double prog_stop) {
std::string mon_wsname = this->getProperty("OutputWorkspace");
mon_wsname.append("_monitors");
try {
IAlgorithm_sptr alg = this->createChildAlgorithm("LoadPreNexusMonitors",
prog_start, prog_stop);
alg->setPropertyValue("RunInfoFilename", this->getProperty(RUNINFO_PARAM));
alg->setPropertyValue("OutputWorkspace", mon_wsname);
alg->executeAsChildAlg();
MatrixWorkspace_sptr mons = alg->getProperty("OutputWorkspace");
this->declareProperty(new WorkspaceProperty<>("MonitorWorkspace",
mon_wsname,
Direction::Output),
"Monitors from the Event NeXus file");
this->setProperty("MonitorWorkspace", mons);
// Add an internal pointer to monitor workspace in the 'main' workspace
m_outputWorkspace->setMonitorWorkspace(mons);
} catch (std::exception &e) {
g_log.warning() << "Failed to load monitors: " << e.what() << "\n";
}
}
} // namespace Mantid
} // namespace DataHandling