38bool isSingleFile(
const std::vector<std::vector<std::string>> &fileNames) {
39 return fileNames.size() == 1 && fileNames[0u].size() == 1;
51std::string generateWsNameFromFileNames(
const std::vector<std::string> &filenames) {
54 for (
auto &filename : filenames) {
58 std::filesystem::path path(filename);
59 wsName += path.stem().string();
69std::vector<std::string> flattenVecOfVec(std::vector<std::vector<std::string>> vecOfVec) {
70 std::vector<std::string> flattenedVec;
72 std::vector<std::vector<std::string>>::const_iterator it = vecOfVec.begin();
74 for (; it != vecOfVec.end(); ++it) {
75 flattenedVec.insert(flattenedVec.end(), it->begin(), it->end());
89using namespace Kernel;
108 std::string NAME(
name);
109 std::transform(
name.begin(),
name.end(), NAME.begin(), toupper);
110 if (NAME ==
"FILENAME") {
113 std::vector<std::string> fileNames = flattenVecOfVec(
getProperty(
"Filename"));
115 if (fileNames.size() == 1) {
126 else if (fileNames.size() > 1) {
133 const std::string loaderName = loader->name();
134 int firstFileLoaderVersion = loader->version();
139 auto ifl = std::dynamic_pointer_cast<IFileLoader<Kernel::FileDescriptor>>(loader);
140 auto iflNexus = std::dynamic_pointer_cast<IFileLoader<Nexus::NexusDescriptor>>(loader);
142 for (
size_t i = 1; i < fileNames.size(); ++i) {
145 if ((ifl && ifl->loadMutipleAsOne()) || (iflNexus && iflNexus->loadMutipleAsOne())) {
157 if (loaderName != loader->name() || firstFileLoaderVersion != loader->version())
158 throw std::runtime_error(
"Cannot load multiple files when more "
159 "than one Loader is needed.");
183 winningLoader = API::FileLoaderRegistry::Instance().chooseLoader(filePath);
188 throw std::runtime_error(
"Cannot find an algorithm that is able to load \"" + filePath +
190 "Check that the file is a supported type.");
192 winningLoader->initialize();
198 setProperty(
"LoaderVersion", winningLoader->version());
199 return winningLoader;
205 const auto &props = loader->getProperties();
206 for (
auto const &prop : props) {
225 std::stringstream msg;
226 msg <<
"Cannot find FileProperty on \"" << loader->name() <<
"\" v" << loader->version() <<
" algorithm.";
227 throw std::runtime_error(msg.str());
240 const std::vector<Property *> existingProps = this->
getProperties();
241 for (
auto existingProp : existingProps) {
242 const std::string &propName = existingProp->name();
249 const std::vector<Property *> &loaderProps = loader->getProperties();
250 size_t numProps(loaderProps.size());
251 for (
size_t i = 0; i < numProps; ++i) {
252 Property const *loadProp = loaderProps[i];
256 auto propClone = std::unique_ptr<Property>(loadProp->
clone());
257 propClone->clearSettings();
270 const FacilityInfo &defaultFacility = Mantid::Kernel::ConfigService::Instance().getFacility();
271 std::vector<std::string> exts = defaultFacility.
extensions();
273 exts.emplace_back(
".xml");
274 exts.emplace_back(
".dat");
275 exts.emplace_back(
".txt");
276 exts.emplace_back(
".csv");
277 exts.emplace_back(
".spe");
278 exts.emplace_back(
".grp");
279 exts.emplace_back(
".nxspe");
280 exts.emplace_back(
".h5");
281 exts.emplace_back(
".hd5");
282 exts.emplace_back(
".sqw");
283 exts.emplace_back(
".fits");
284 exts.emplace_back(
".bin");
285 exts.emplace_back(
".edb");
287 declareProperty(std::make_unique<MultipleFileProperty>(
"Filename", exts),
288 "The name of the file(s) to read, including the full or relative "
289 "path. (N.B. case sensitive if running on Linux). Multiple runs "
290 "can be loaded and added together, e.g. INST10,11+12,13.ext");
292 "The name of the workspace that will be created, filled with the "
293 "read-in data and stored in the Analysis Data Service. Some algorithms "
294 "can created additional OutputWorkspace properties on the fly, e.g. "
295 "multi-period data.");
298 "When an algorithm has been found that will load the given "
299 "file, its name is set here.",
302 "When an algorithm has been found that "
303 "will load the given file, its version "
307 const std::vector<Property *> &props = this->
getProperties();
317 std::vector<std::vector<std::string>> fileNames =
getProperty(
"Filename");
322 auto ifl = std::dynamic_pointer_cast<IFileLoader<Kernel::FileDescriptor>>(
m_loader);
323 auto iflNexus = std::dynamic_pointer_cast<IFileLoader<Nexus::NexusDescriptor>>(
m_loader);
325 if (isSingleFile(fileNames) || (ifl && ifl->loadMutipleAsOne()) || (iflNexus && iflNexus->loadMutipleAsOne())) {
356 const std::vector<Kernel::Property *> &loader_props =
m_loader->getProperties();
359 std::vector<Kernel::Property *>::const_iterator itr;
360 for (itr = loader_props.begin(); itr != loader_props.end(); ++itr) {
361 const std::string propName = (*itr)->name();
381 const std::vector<std::vector<std::string>> allFilenames =
getProperty(
"Filename");
382 std::string outputWsName =
getProperty(
"OutputWorkspace");
384 std::vector<std::string> wsNames(allFilenames.size());
385 std::transform(allFilenames.begin(), allFilenames.end(), wsNames.begin(), generateWsNameFromFileNames);
387 auto wsName = wsNames.cbegin();
388 assert(allFilenames.size() == wsNames.size());
390 std::vector<API::Workspace_sptr> loadedWsList;
391 loadedWsList.reserve(allFilenames.size());
396 for (
auto filenames = allFilenames.cbegin(); filenames != allFilenames.cend(); ++filenames, ++wsName) {
397 auto filename = filenames->cbegin();
401 for (; filename != filenames->cend(); ++filename) {
403 sumWS =
plusWs(sumWS, tempWs);
408 std::vector<std::string> childWsNames =
group->getNames();
409 auto childWsName = childWsNames.begin();
411 for (; childWsName != childWsNames.end(); ++childWsName, ++
count) {
414 API::AnalysisDataService::Instance().addOrReplace(childName, childWs);
420 loadedWsList.emplace_back(sumWS);
424 if (loadedWsList.size() == 1) {
426 AnalysisDataService::Instance().rename(loadedWsList[0]->
getName(), outputWsName);
434 std::vector<std::string> childWsNames =
group->getNames();
436 for (
auto &childWsName : childWsNames) {
437 if (childWsName == outputWsName) {
442 API::AnalysisDataService::Instance().addOrReplace(childName, child);
447 childWsNames =
group->getNames();
449 for (
auto &childWsName : childWsNames) {
460 Algorithm_sptr alg = AlgorithmManager::Instance().createUnmanaged(
"DeleteWorkspace");
463 alg->setProperty(
"Workspace", tempWs);
477 const bool logging)
const {
480 auto loader = API::AlgorithmManager::Instance().createUnmanaged(loaderName, loaderVersion);
481 loader->initialize();
483 throw std::runtime_error(
"Cannot create loader for \"" +
getPropertyValue(
"Filename") +
"\"");
485 setUpLoader(loader, startProgress, endProgress, logging);
497 const bool logging)
const {
499 loader->setChild(
true);
500 loader->setLogging(logging);
503 const std::vector<Property *> &props = loader->getProperties();
504 for (
auto prop : props) {
508 if (prop->value().empty())
509 prop->setValue(
"LoadChildWorkspace");
512 if (startProgress >= 0. && endProgress > startProgress && endProgress <= 1.) {
525 while (loader->propertyCount() > 0) {
526 auto prop = loader->takeProperty(0);
529 if (prop->name() !=
"OutputWorkspace")
546 return loader->getProperty(propName);
547 }
catch (std::runtime_error &) {
554 }
catch (std::runtime_error &) {
561 }
catch (std::runtime_error &) {
568 }
catch (std::runtime_error &) {
575 }
catch (std::runtime_error &) {
582 }
catch (std::runtime_error &) {
589 }
catch (std::runtime_error &) {
596 }
catch (std::runtime_error &) {
599 g_log.
debug() <<
"Workspace property " << propName
600 <<
" did not return to MatrixWorkspace, EventWorkspace, "
601 "IMDEventWorkspace, IMDWorkspace\n";
626 const std::vector<Kernel::Property *> &props =
getProperties();
629 for (
auto prop : props) {
630 const std::string &propName = prop->name();
633 if (propName ==
"Filename") {
634 loadAlg->setPropertyValue(
"Filename", fileName);
635 }
else if (propName ==
"OutputWorkspace") {
636 loadAlg->setPropertyValue(
"OutputWorkspace", wsName);
643 loadAlg->executeAsChildAlg();
647 AnalysisDataService::Instance().addOrReplace(wsName, ws);
664 if (group1 && group2) {
668 std::vector<std::string> group1ChildWsNames = group1->getNames();
669 std::vector<std::string> group2ChildWsNames = group2->getNames();
671 if (group1ChildWsNames.size() != group2ChildWsNames.size())
672 throw std::runtime_error(
"Unable to add group workspaces with different "
673 "number of child workspaces.");
675 auto group1ChildWsName = group1ChildWsNames.begin();
676 auto group2ChildWsName = group2ChildWsNames.begin();
678 for (; group1ChildWsName != group1ChildWsNames.end(); ++group1ChildWsName, ++group2ChildWsName) {
679 Workspace_sptr group1ChildWs = group1->getItem(*group1ChildWsName);
680 Workspace_sptr group2ChildWs = group2->getItem(*group2ChildWsName);
683 plusAlg->setProperty<
Workspace_sptr>(
"LHSWorkspace", group1ChildWs);
684 plusAlg->setProperty<
Workspace_sptr>(
"RHSWorkspace", group2ChildWs);
685 plusAlg->setProperty<
Workspace_sptr>(
"OutputWorkspace", group1ChildWs);
686 plusAlg->executeAsChildAlg();
688 }
else if (!group1 && !group2) {
693 plusAlg->executeAsChildAlg();
695 throw std::runtime_error(
"Unable to add a group workspace to a non-group workspace");
709 auto group = std::make_shared<WorkspaceGroup>();
711 for (
const auto &ws : wsList) {
715 std::vector<std::string> childrenNames = isGroup->getNames();
717 for (
auto childName = childrenNames.begin(); childName != childrenNames.end(); ++childName, ++
count) {
719 isGroup->remove(*childName);
722 group->addWorkspace(childWs);
726 AnalysisDataService::Instance().remove(isGroup->getName());
728 group->addWorkspace(ws);
#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.
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.
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.
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.