40bool isSingleFile(
const std::vector<std::vector<std::string>> &fileNames) {
41 return fileNames.size() == 1 && fileNames[0u].size() == 1;
53std::string generateWsNameFromFileNames(
const std::vector<std::string> &filenames) {
56 for (
auto &filename : filenames) {
60 Poco::Path path(filename);
61 wsName += path.getBaseName();
71std::vector<std::string> flattenVecOfVec(std::vector<std::vector<std::string>> vecOfVec) {
72 std::vector<std::string> flattenedVec;
74 std::vector<std::vector<std::string>>::const_iterator it = vecOfVec.begin();
76 for (; it != vecOfVec.end(); ++it) {
77 flattenedVec.insert(flattenedVec.end(), it->begin(), it->end());
91using namespace Kernel;
110 std::string NAME(
name);
111 std::transform(
name.begin(),
name.end(), NAME.begin(), toupper);
112 if (NAME ==
"FILENAME") {
115 std::vector<std::string> fileNames = flattenVecOfVec(
getProperty(
"Filename"));
117 if (fileNames.size() == 1) {
127 else if (fileNames.size() > 1) {
134 const std::string loaderName = loader->name();
135 int version = loader->version();
140 auto ifl = std::dynamic_pointer_cast<IFileLoader<Kernel::FileDescriptor>>(loader);
141 auto iflNexus = std::dynamic_pointer_cast<IFileLoader<Kernel::NexusDescriptor>>(loader);
143 for (
size_t i = 1; i < fileNames.size(); ++i) {
146 if ((ifl && ifl->loadMutipleAsOne()) || (iflNexus && iflNexus->loadMutipleAsOne())) {
158 if (loaderName != loader->name() ||
version != loader->version())
159 throw std::runtime_error(
"Cannot load multiple files when more "
160 "than one Loader is needed.");
189 throw std::runtime_error(
"Cannot find an algorithm that is able to load \"" + filePath +
191 "Check that the file is a supported type.");
193 winningLoader->initialize();
199 setProperty(
"LoaderVersion", winningLoader->version());
200 return winningLoader;
204 const auto nxsLoader = std::dynamic_pointer_cast<NexusFileLoader>(loader);
210 const auto &props = loader->getProperties();
211 for (
auto prop : props) {
231 std::stringstream msg;
232 msg <<
"Cannot find FileProperty on \"" << loader->name() <<
"\" v" << loader->version() <<
" algorithm.";
233 throw std::runtime_error(msg.str());
246 const std::vector<Property *> existingProps = this->
getProperties();
247 for (
auto existingProp : existingProps) {
248 const std::string &
name = existingProp->name();
255 const std::vector<Property *> &loaderProps = loader->getProperties();
256 size_t numProps(loaderProps.size());
257 for (
size_t i = 0; i < numProps; ++i) {
258 Property *loadProp = loaderProps[i];
262 auto propClone = std::unique_ptr<Property>(loadProp->
clone());
263 propClone->clearSettings();
277 std::vector<std::string> exts = defaultFacility.
extensions();
279 exts.emplace_back(
".xml");
280 exts.emplace_back(
".dat");
281 exts.emplace_back(
".txt");
282 exts.emplace_back(
".csv");
283 exts.emplace_back(
".spe");
284 exts.emplace_back(
".grp");
285 exts.emplace_back(
".nxspe");
286 exts.emplace_back(
".h5");
287 exts.emplace_back(
".hd5");
288 exts.emplace_back(
".sqw");
289 exts.emplace_back(
".fits");
290 exts.emplace_back(
".bin");
291 exts.emplace_back(
".edb");
293 declareProperty(std::make_unique<MultipleFileProperty>(
"Filename", exts),
294 "The name of the file(s) to read, including the full or relative "
295 "path. (N.B. case sensitive if running on Linux). Multiple runs "
296 "can be loaded and added together, e.g. INST10,11+12,13.ext");
298 "The name of the workspace that will be created, filled with the "
299 "read-in data and stored in the Analysis Data Service. Some algorithms "
300 "can created additional OutputWorkspace properties on the fly, e.g. "
301 "multi-period data.");
304 "When an algorithm has been found that will load the given "
305 "file, its name is set here.",
308 "When an algorithm has been found that "
309 "will load the given file, its version "
313 const std::vector<Property *> &props = this->
getProperties();
323 std::vector<std::vector<std::string>> fileNames =
getProperty(
"Filename");
327 auto ifl = std::dynamic_pointer_cast<IFileLoader<Kernel::FileDescriptor>>(loader);
328 auto iflNexus = std::dynamic_pointer_cast<IFileLoader<Kernel::NexusDescriptor>>(loader);
330 if (isSingleFile(fileNames) || (ifl && ifl->loadMutipleAsOne()) || (iflNexus && iflNexus->loadMutipleAsOne())) {
345 if (loaderName.empty()) {
354 const std::vector<Kernel::Property *> &loader_props =
m_loader->getProperties();
357 std::vector<Kernel::Property *>::const_iterator itr;
358 for (itr = loader_props.begin(); itr != loader_props.end(); ++itr) {
359 const std::string propName = (*itr)->name();
379 const std::vector<std::vector<std::string>> allFilenames =
getProperty(
"Filename");
380 std::string outputWsName =
getProperty(
"OutputWorkspace");
382 std::vector<std::string> wsNames(allFilenames.size());
383 std::transform(allFilenames.begin(), allFilenames.end(), wsNames.begin(), generateWsNameFromFileNames);
385 auto wsName = wsNames.cbegin();
386 assert(allFilenames.size() == wsNames.size());
388 std::vector<API::Workspace_sptr> loadedWsList;
389 loadedWsList.reserve(allFilenames.size());
394 for (
auto filenames = allFilenames.cbegin(); filenames != allFilenames.cend(); ++filenames, ++wsName) {
395 auto filename = filenames->cbegin();
399 for (; filename != filenames->cend(); ++filename) {
401 sumWS =
plusWs(sumWS, tempWs);
406 std::vector<std::string> childWsNames = group->getNames();
407 auto childWsName = childWsNames.begin();
409 for (; childWsName != childWsNames.end(); ++childWsName, ++
count) {
418 loadedWsList.emplace_back(sumWS);
422 if (loadedWsList.size() == 1) {
432 std::vector<std::string> childWsNames = group->getNames();
434 for (
auto &childWsName : childWsNames) {
435 if (childWsName == outputWsName) {
445 childWsNames = group->getNames();
447 for (
auto &childWsName : childWsNames) {
461 alg->setProperty(
"Workspace", tempWs);
475 const bool logging)
const {
479 loader->initialize();
481 throw std::runtime_error(
"Cannot create loader for \"" +
getPropertyValue(
"Filename") +
"\"");
483 setUpLoader(loader, startProgress, endProgress, logging);
495 const bool logging)
const {
497 loader->setChild(
true);
498 loader->setLogging(logging);
501 const std::vector<Property *> &props = loader->getProperties();
502 for (
auto prop : props) {
506 if (prop->value().empty())
507 prop->setValue(
"LoadChildWorkspace");
510 if (startProgress >= 0. && endProgress > startProgress && endProgress <= 1.) {
523 while (loader->propertyCount() > 0) {
524 auto prop = loader->takeProperty(0);
527 if (prop->name() !=
"OutputWorkspace")
544 return loader->getProperty(propName);
545 }
catch (std::runtime_error &) {
552 }
catch (std::runtime_error &) {
559 }
catch (std::runtime_error &) {
566 }
catch (std::runtime_error &) {
573 }
catch (std::runtime_error &) {
580 }
catch (std::runtime_error &) {
587 }
catch (std::runtime_error &) {
594 }
catch (std::runtime_error &) {
597 g_log.
debug() <<
"Workspace property " << propName
598 <<
" did not return to MatrixWorkspace, EventWorkspace, "
599 "IMDEventWorkspace, IMDWorkspace\n";
624 const std::vector<Kernel::Property *> &props =
getProperties();
627 for (
auto prop : props) {
628 const std::string &propName = prop->name();
631 if (propName ==
"Filename") {
632 loadAlg->setPropertyValue(
"Filename", fileName);
633 }
else if (propName ==
"OutputWorkspace") {
634 loadAlg->setPropertyValue(
"OutputWorkspace", wsName);
641 loadAlg->executeAsChildAlg();
662 if (group1 && group2) {
666 std::vector<std::string> group1ChildWsNames = group1->getNames();
667 std::vector<std::string> group2ChildWsNames = group2->getNames();
669 if (group1ChildWsNames.size() != group2ChildWsNames.size())
670 throw std::runtime_error(
"Unable to add group workspaces with different "
671 "number of child workspaces.");
673 auto group1ChildWsName = group1ChildWsNames.begin();
674 auto group2ChildWsName = group2ChildWsNames.begin();
676 for (; group1ChildWsName != group1ChildWsNames.end(); ++group1ChildWsName, ++group2ChildWsName) {
677 Workspace_sptr group1ChildWs = group1->getItem(*group1ChildWsName);
678 Workspace_sptr group2ChildWs = group2->getItem(*group2ChildWsName);
681 plusAlg->setProperty<
Workspace_sptr>(
"LHSWorkspace", group1ChildWs);
682 plusAlg->setProperty<
Workspace_sptr>(
"RHSWorkspace", group2ChildWs);
683 plusAlg->setProperty<
Workspace_sptr>(
"OutputWorkspace", group1ChildWs);
684 plusAlg->executeAsChildAlg();
686 }
else if (!group1 && !group2) {
691 plusAlg->executeAsChildAlg();
693 throw std::runtime_error(
"Unable to add a group workspace to a non-group workspace");
707 auto group = std::make_shared<WorkspaceGroup>();
709 for (
const auto &ws : wsList) {
713 std::vector<std::string> childrenNames = isGroup->getNames();
715 for (
auto childName = childrenNames.begin(); childName != childrenNames.end(); ++childName, ++
count) {
717 isGroup->remove(*childName);
720 group->addWorkspace(childWs);
726 group->addWorkspace(ws);
733Parallel::ExecutionMode
735 static_cast<void>(storageModes);
739 return Parallel::ExecutionMode::Distributed;
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
std::string getName(const IMDDimension &self)
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
void setChildEndProgress(const double endProgress) const override
setting the child end progress
bool existsProperty(const std::string &name) const override
Checks whether the named property is already in the list of managed property.
void removeProperty(const std::string &name, const bool delproperty=true) override
Removes the property from management.
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
size_t propertyCount() const override
Count the number of properties under management.
void setChildStartProgress(const double startProgress) const override
setting the child start progress
void declareOrReplaceProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add or replace property in the list of managed properties.
void setPropertyValue(const std::string &name, const std::string &value) override
Set the value of a property by string N.B.
const std::vector< Kernel::Property * > & getProperties() const override
Get the list of managed properties.
const Poco::AbstractObserver & progressObserver() const
Return a reference to the algorithm's object that is reporting progress.
A specialized class for dealing with file properties.
An interface that is implemented by WorkspaceProperty.
A property to allow a user to specify multiple files to load.
A property class for workspaces.
API::WorkspaceGroup_sptr groupWsList(const std::vector< API::Workspace_sptr > &wsList)
Manually group workspaces.
API::Workspace_sptr plusWs(API::Workspace_sptr ws1, const API::Workspace_sptr &ws2)
Plus two workspaces together, "in place".
void cancel() override
Overrides the cancel() method to call m_loader->cancel()
void setOutputProperties(const API::IAlgorithm_sptr &loader)
Set the output properties.
void setPropertyValue(const std::string &name, const std::string &value) override
Override setPropertyValue.
API::Workspace_sptr getOutputWorkspace(const std::string &propName, const API::IAlgorithm_sptr &loader) const
Retrieve a pointer to the output workspace from the Child Algorithm.
Parallel::ExecutionMode getParallelExecutionMode(const std::map< std::string, Parallel::StorageMode > &storageModes) const override
Get correct execution mode based on input storage modes for an MPI run.
void exec() override
Execute.
API::IAlgorithm_sptr m_loader
The actual loader.
static std::recursive_mutex m_mutex
Mutex for temporary fix for #5963.
API::IAlgorithm_sptr createLoader(const double startProgress=-1.0, const double endProgress=-1.0, const bool logging=true) const
Create the concrete instance use for the actual loading.
void init() override
Initialize the static base properties.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
std::string m_filenamePropName
The name of the property that will be passed the property from our Filename.
void loadSingleFile()
Called when there is only one file to load.
void findFilenameProperty(const API::IAlgorithm_sptr &loader)
void loadMultipleFiles()
Called when there are multiple files to load.
API::IAlgorithm_sptr getFileLoader(const std::string &filePath)
This method returns shared pointer to a load algorithm which got the highest preference after file ch...
void setUpLoader(const API::IAlgorithm_sptr &loader, const double startProgress=-1.0, const double endProgress=-1.0, const bool logging=true) const
Set the loader option for use as a Child Algorithm.
int version() const override
Algorithm's version for identification overriding a virtual method.
std::unordered_set< std::string > m_baseProps
The base properties.
API::Workspace_sptr loadFileToWs(const std::string &fileName, const std::string &wsName)
Load a file to a given workspace name.
void declareLoaderProperties(const API::IAlgorithm_sptr &loader)
Declare any additional input properties from the concrete loader.
Exception for when an item is already in a collection.
Exception for when an item is not found in a collection.
A class that holds information about a facility.
const std::vector< std::string > extensions() const
Returns a list of file extensions.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void debug(const std::string &msg)
Logs at debug level.
void information(const std::string &msg)
Logs at information level.
Base class for properties.
const std::string & documentation() const
Get the property's documentation string.
virtual Property * clone() const =0
'Virtual copy constructor'
const std::string & name() const
Get the property's name.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::shared_ptr< IMDEventWorkspace > IMDEventWorkspace_sptr
Shared pointer to Mantid::API::IMDEventWorkspace.
std::shared_ptr< IAlgorithm > IAlgorithm_sptr
shared pointer to Mantid::API::IAlgorithm
std::shared_ptr< WorkspaceGroup > WorkspaceGroup_sptr
shared pointer to Mantid::API::WorkspaceGroup
std::shared_ptr< ITableWorkspace > ITableWorkspace_sptr
shared pointer to Mantid::API::ITableWorkspace
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< IMDWorkspace > IMDWorkspace_sptr
Shared pointer to the IMDWorkspace base class.
std::shared_ptr< IEventWorkspace > IEventWorkspace_sptr
shared pointer to Mantid::API::IEventWorkspace
std::shared_ptr< Algorithm > Algorithm_sptr
Typedef for a shared pointer to an Algorithm.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::string to_string(const wide_integer< Bits, Signed > &n)
@ Output
An output workspace.