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) {
144 if ((ifl && ifl->loadMutipleAsOne()) || (iflNexus && iflNexus->loadMutipleAsOne())) {
149 if (loaderName != loader->name() || firstFileLoaderVersion != loader->version())
150 throw std::runtime_error(
"Cannot load multiple files when more "
151 "than one Loader is needed.");
175 winningLoader = API::FileLoaderRegistry::Instance().chooseLoader(filePath);
180 throw std::runtime_error(
"Cannot find an algorithm that is able to load \"" + filePath +
182 "Check that the file is a supported type.");
184 winningLoader->initialize();
190 setProperty(
"LoaderVersion", winningLoader->version());
191 return winningLoader;
197 const auto &props = loader->getProperties();
198 for (
auto const &prop : props) {
217 std::stringstream msg;
218 msg <<
"Cannot find FileProperty on \"" << loader->name() <<
"\" v" << loader->version() <<
" algorithm.";
219 throw std::runtime_error(msg.str());
232 const std::vector<Property *> existingProps = this->
getProperties();
233 for (
auto existingProp : existingProps) {
234 const std::string &propName = existingProp->name();
241 const std::vector<Property *> &loaderProps = loader->getProperties();
242 size_t numProps(loaderProps.size());
243 for (
size_t i = 0; i < numProps; ++i) {
244 Property const *loadProp = loaderProps[i];
248 auto propClone = std::unique_ptr<Property>(loadProp->
clone());
249 propClone->clearSettings();
262 const FacilityInfo &defaultFacility = Mantid::Kernel::ConfigService::Instance().getFacility();
263 std::vector<std::string> exts = defaultFacility.
extensions();
265 exts.emplace_back(
".xml");
266 exts.emplace_back(
".dat");
267 exts.emplace_back(
".txt");
268 exts.emplace_back(
".csv");
269 exts.emplace_back(
".spe");
270 exts.emplace_back(
".grp");
271 exts.emplace_back(
".nxspe");
272 exts.emplace_back(
".h5");
273 exts.emplace_back(
".hd5");
274 exts.emplace_back(
".sqw");
275 exts.emplace_back(
".fits");
276 exts.emplace_back(
".bin");
277 exts.emplace_back(
".edb");
279 declareProperty(std::make_unique<MultipleFileProperty>(
"Filename", exts),
280 "The name of the file(s) to read, including the full or relative "
281 "path. (N.B. case sensitive if running on Linux). Multiple runs "
282 "can be loaded and added together, e.g. INST10,11+12,13.ext");
284 "The name of the workspace that will be created, filled with the "
285 "read-in data and stored in the Analysis Data Service. Some algorithms "
286 "can created additional OutputWorkspace properties on the fly, e.g. "
287 "multi-period data.");
290 "When an algorithm has been found that will load the given "
291 "file, its name is set here.",
294 "When an algorithm has been found that "
295 "will load the given file, its version "
299 const std::vector<Property *> &props = this->
getProperties();
309 std::vector<std::vector<std::string>> fileNames =
getProperty(
"Filename");
314 auto ifl = std::dynamic_pointer_cast<IFileLoader<Kernel::FileDescriptor>>(
m_loader);
315 auto iflNexus = std::dynamic_pointer_cast<IFileLoader<Nexus::NexusDescriptor>>(
m_loader);
317 if (isSingleFile(fileNames) || (ifl && ifl->loadMutipleAsOne()) || (iflNexus && iflNexus->loadMutipleAsOne())) {
348 const std::vector<Kernel::Property *> &loader_props =
m_loader->getProperties();
351 std::vector<Kernel::Property *>::const_iterator itr;
352 for (itr = loader_props.begin(); itr != loader_props.end(); ++itr) {
353 const std::string propName = (*itr)->name();
373 const std::vector<std::vector<std::string>> allFilenames =
getProperty(
"Filename");
374 std::string outputWsName =
getProperty(
"OutputWorkspace");
376 std::vector<std::string> wsNames(allFilenames.size());
377 std::transform(allFilenames.begin(), allFilenames.end(), wsNames.begin(), generateWsNameFromFileNames);
379 auto wsName = wsNames.cbegin();
380 assert(allFilenames.size() == wsNames.size());
382 std::vector<API::Workspace_sptr> loadedWsList;
383 loadedWsList.reserve(allFilenames.size());
388 for (
auto filenames = allFilenames.cbegin(); filenames != allFilenames.cend(); ++filenames, ++wsName) {
389 auto filename = filenames->cbegin();
393 for (; filename != filenames->cend(); ++filename) {
395 sumWS =
plusWs(sumWS, tempWs);
400 std::vector<std::string> childWsNames =
group->getNames();
401 auto childWsName = childWsNames.begin();
403 for (; childWsName != childWsNames.end(); ++childWsName, ++
count) {
406 API::AnalysisDataService::Instance().addOrReplace(childName, childWs);
412 loadedWsList.emplace_back(sumWS);
416 if (loadedWsList.size() == 1) {
418 AnalysisDataService::Instance().rename(loadedWsList[0]->
getName(), outputWsName);
426 std::vector<std::string> childWsNames =
group->getNames();
428 for (
auto &childWsName : childWsNames) {
429 if (childWsName == outputWsName) {
434 API::AnalysisDataService::Instance().addOrReplace(childName, child);
439 childWsNames =
group->getNames();
441 for (
auto &childWsName : childWsNames) {
452 Algorithm_sptr alg = AlgorithmManager::Instance().createUnmanaged(
"DeleteWorkspace");
455 alg->setProperty(
"Workspace", tempWs);
469 const bool logging)
const {
472 auto loader = API::AlgorithmManager::Instance().createUnmanaged(loaderName, loaderVersion);
473 loader->initialize();
475 throw std::runtime_error(
"Cannot create loader for \"" +
getPropertyValue(
"Filename") +
"\"");
477 setUpLoader(loader, startProgress, endProgress, logging);
489 const bool logging)
const {
491 loader->setChild(
true);
492 loader->setLogging(logging);
495 const std::vector<Property *> &props = loader->getProperties();
496 for (
auto prop : props) {
500 if (prop->value().empty())
501 prop->setValue(
"LoadChildWorkspace");
504 if (startProgress >= 0. && endProgress > startProgress && endProgress <= 1.) {
517 while (loader->propertyCount() > 0) {
518 auto prop = loader->takeProperty(0);
521 if (prop->name() !=
"OutputWorkspace")
538 return loader->getProperty(propName);
539 }
catch (std::runtime_error &) {
546 }
catch (std::runtime_error &) {
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 &) {
591 g_log.
debug() <<
"Workspace property " << propName
592 <<
" did not return to MatrixWorkspace, EventWorkspace, "
593 "IMDEventWorkspace, IMDWorkspace\n";
618 const std::vector<Kernel::Property *> &props =
getProperties();
621 for (
auto prop : props) {
622 const std::string &propName = prop->name();
625 if (propName ==
"Filename") {
626 loadAlg->setPropertyValue(
"Filename", fileName);
627 }
else if (propName ==
"OutputWorkspace") {
628 loadAlg->setPropertyValue(
"OutputWorkspace", wsName);
635 loadAlg->executeAsChildAlg();
639 AnalysisDataService::Instance().addOrReplace(wsName, ws);
656 if (group1 && group2) {
660 std::vector<std::string> group1ChildWsNames = group1->getNames();
661 std::vector<std::string> group2ChildWsNames = group2->getNames();
663 if (group1ChildWsNames.size() != group2ChildWsNames.size())
664 throw std::runtime_error(
"Unable to add group workspaces with different "
665 "number of child workspaces.");
667 auto group1ChildWsName = group1ChildWsNames.begin();
668 auto group2ChildWsName = group2ChildWsNames.begin();
670 for (; group1ChildWsName != group1ChildWsNames.end(); ++group1ChildWsName, ++group2ChildWsName) {
671 Workspace_sptr group1ChildWs = group1->getItem(*group1ChildWsName);
672 Workspace_sptr group2ChildWs = group2->getItem(*group2ChildWsName);
675 plusAlg->setProperty<
Workspace_sptr>(
"LHSWorkspace", group1ChildWs);
676 plusAlg->setProperty<
Workspace_sptr>(
"RHSWorkspace", group2ChildWs);
677 plusAlg->setProperty<
Workspace_sptr>(
"OutputWorkspace", group1ChildWs);
678 plusAlg->executeAsChildAlg();
680 }
else if (!group1 && !group2) {
685 plusAlg->executeAsChildAlg();
687 throw std::runtime_error(
"Unable to add a group workspace to a non-group workspace");
701 auto group = std::make_shared<WorkspaceGroup>();
703 for (
const auto &ws : wsList) {
707 std::vector<std::string> childrenNames = isGroup->getNames();
709 for (
auto childName = childrenNames.begin(); childName != childrenNames.end(); ++childName, ++
count) {
711 isGroup->remove(*childName);
714 group->addWorkspace(childWs);
718 AnalysisDataService::Instance().remove(isGroup->getName());
720 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.