39bool isSingleFile(
const std::vector<std::vector<std::string>> &fileNames) {
40 return fileNames.size() == 1 && fileNames[0u].size() == 1;
52std::string generateWsNameFromFileNames(
const std::vector<std::string> &filenames) {
55 for (
auto &filename : filenames) {
59 std::filesystem::path path(filename);
60 wsName += path.stem().string();
70std::vector<std::string> flattenVecOfVec(std::vector<std::vector<std::string>> vecOfVec) {
71 std::vector<std::string> flattenedVec;
73 std::vector<std::vector<std::string>>::const_iterator it = vecOfVec.begin();
75 for (; it != vecOfVec.end(); ++it) {
76 flattenedVec.insert(flattenedVec.end(), it->begin(), it->end());
90using namespace Kernel;
109 std::string NAME(
name);
110 std::transform(
name.begin(),
name.end(), NAME.begin(), toupper);
111 if (NAME ==
"FILENAME") {
114 std::vector<std::string> fileNames = flattenVecOfVec(
getProperty(
"Filename"));
116 if (fileNames.size() == 1) {
127 else if (fileNames.size() > 1) {
134 const std::string loaderName = loader->name();
135 int firstFileLoaderVersion = loader->version();
140 auto ifl = std::dynamic_pointer_cast<IFileLoader<Kernel::FileDescriptor>>(loader);
141 auto iflNexus = std::dynamic_pointer_cast<IFileLoader<Nexus::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() || firstFileLoaderVersion != loader->version())
159 throw std::runtime_error(
"Cannot load multiple files when more "
160 "than one Loader is needed.");
184 winningLoader = API::FileLoaderRegistry::Instance().chooseLoader(filePath);
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 const &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 &propName = 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 const *loadProp = loaderProps[i];
262 auto propClone = std::unique_ptr<Property>(loadProp->
clone());
263 propClone->clearSettings();
276 const FacilityInfo &defaultFacility = Mantid::Kernel::ConfigService::Instance().getFacility();
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");
328 auto ifl = std::dynamic_pointer_cast<IFileLoader<Kernel::FileDescriptor>>(
m_loader);
329 auto iflNexus = std::dynamic_pointer_cast<IFileLoader<Nexus::NexusDescriptor>>(
m_loader);
331 if (isSingleFile(fileNames) || (ifl && ifl->loadMutipleAsOne()) || (iflNexus && iflNexus->loadMutipleAsOne())) {
362 const std::vector<Kernel::Property *> &loader_props =
m_loader->getProperties();
365 std::vector<Kernel::Property *>::const_iterator itr;
366 for (itr = loader_props.begin(); itr != loader_props.end(); ++itr) {
367 const std::string propName = (*itr)->name();
387 const std::vector<std::vector<std::string>> allFilenames =
getProperty(
"Filename");
388 std::string outputWsName =
getProperty(
"OutputWorkspace");
390 std::vector<std::string> wsNames(allFilenames.size());
391 std::transform(allFilenames.begin(), allFilenames.end(), wsNames.begin(), generateWsNameFromFileNames);
393 auto wsName = wsNames.cbegin();
394 assert(allFilenames.size() == wsNames.size());
396 std::vector<API::Workspace_sptr> loadedWsList;
397 loadedWsList.reserve(allFilenames.size());
402 for (
auto filenames = allFilenames.cbegin(); filenames != allFilenames.cend(); ++filenames, ++wsName) {
403 auto filename = filenames->cbegin();
407 for (; filename != filenames->cend(); ++filename) {
409 sumWS =
plusWs(sumWS, tempWs);
414 std::vector<std::string> childWsNames =
group->getNames();
415 auto childWsName = childWsNames.begin();
417 for (; childWsName != childWsNames.end(); ++childWsName, ++
count) {
420 API::AnalysisDataService::Instance().addOrReplace(childName, childWs);
426 loadedWsList.emplace_back(sumWS);
430 if (loadedWsList.size() == 1) {
432 AnalysisDataService::Instance().rename(loadedWsList[0]->
getName(), outputWsName);
440 std::vector<std::string> childWsNames =
group->getNames();
442 for (
auto &childWsName : childWsNames) {
443 if (childWsName == outputWsName) {
448 API::AnalysisDataService::Instance().addOrReplace(childName, child);
453 childWsNames =
group->getNames();
455 for (
auto &childWsName : childWsNames) {
466 Algorithm_sptr alg = AlgorithmManager::Instance().createUnmanaged(
"DeleteWorkspace");
469 alg->setProperty(
"Workspace", tempWs);
483 const bool logging)
const {
486 auto loader = API::AlgorithmManager::Instance().createUnmanaged(loaderName, loaderVersion);
487 loader->initialize();
489 throw std::runtime_error(
"Cannot create loader for \"" +
getPropertyValue(
"Filename") +
"\"");
491 setUpLoader(loader, startProgress, endProgress, logging);
503 const bool logging)
const {
505 loader->setChild(
true);
506 loader->setLogging(logging);
509 const std::vector<Property *> &props = loader->getProperties();
510 for (
auto prop : props) {
514 if (prop->value().empty())
515 prop->setValue(
"LoadChildWorkspace");
518 if (startProgress >= 0. && endProgress > startProgress && endProgress <= 1.) {
531 while (loader->propertyCount() > 0) {
532 auto prop = loader->takeProperty(0);
535 if (prop->name() !=
"OutputWorkspace")
552 return loader->getProperty(propName);
553 }
catch (std::runtime_error &) {
560 }
catch (std::runtime_error &) {
567 }
catch (std::runtime_error &) {
574 }
catch (std::runtime_error &) {
581 }
catch (std::runtime_error &) {
588 }
catch (std::runtime_error &) {
595 }
catch (std::runtime_error &) {
602 }
catch (std::runtime_error &) {
605 g_log.
debug() <<
"Workspace property " << propName
606 <<
" did not return to MatrixWorkspace, EventWorkspace, "
607 "IMDEventWorkspace, IMDWorkspace\n";
632 const std::vector<Kernel::Property *> &props =
getProperties();
635 for (
auto prop : props) {
636 const std::string &propName = prop->name();
639 if (propName ==
"Filename") {
640 loadAlg->setPropertyValue(
"Filename", fileName);
641 }
else if (propName ==
"OutputWorkspace") {
642 loadAlg->setPropertyValue(
"OutputWorkspace", wsName);
649 loadAlg->executeAsChildAlg();
653 AnalysisDataService::Instance().addOrReplace(wsName, ws);
670 if (group1 && group2) {
674 std::vector<std::string> group1ChildWsNames = group1->getNames();
675 std::vector<std::string> group2ChildWsNames = group2->getNames();
677 if (group1ChildWsNames.size() != group2ChildWsNames.size())
678 throw std::runtime_error(
"Unable to add group workspaces with different "
679 "number of child workspaces.");
681 auto group1ChildWsName = group1ChildWsNames.begin();
682 auto group2ChildWsName = group2ChildWsNames.begin();
684 for (; group1ChildWsName != group1ChildWsNames.end(); ++group1ChildWsName, ++group2ChildWsName) {
685 Workspace_sptr group1ChildWs = group1->getItem(*group1ChildWsName);
686 Workspace_sptr group2ChildWs = group2->getItem(*group2ChildWsName);
689 plusAlg->setProperty<
Workspace_sptr>(
"LHSWorkspace", group1ChildWs);
690 plusAlg->setProperty<
Workspace_sptr>(
"RHSWorkspace", group2ChildWs);
691 plusAlg->setProperty<
Workspace_sptr>(
"OutputWorkspace", group1ChildWs);
692 plusAlg->executeAsChildAlg();
694 }
else if (!group1 && !group2) {
699 plusAlg->executeAsChildAlg();
701 throw std::runtime_error(
"Unable to add a group workspace to a non-group workspace");
715 auto group = std::make_shared<WorkspaceGroup>();
717 for (
const auto &ws : wsList) {
721 std::vector<std::string> childrenNames = isGroup->getNames();
723 for (
auto childName = childrenNames.begin(); childName != childrenNames.end(); ++childName, ++
count) {
725 isGroup->remove(*childName);
728 group->addWorkspace(childWs);
732 AnalysisDataService::Instance().remove(isGroup->getName());
734 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.