From ac31bf86e9e1bc7e3c8b871721543468d40c7810 Mon Sep 17 00:00:00 2001 From: Martyn Gigg <martyn.gigg@stfc.ac.uk> Date: Tue, 1 Feb 2011 09:35:52 +0000 Subject: [PATCH] Update to the Load algorithm to allow running solely to discover the concrete loader type. Also updated workspace property to respect the optional flag in other cirumstances. Re #2329 --- .../API/inc/MantidAPI/WorkspaceProperty.h | 3 +- .../inc/MantidDataHandling/Load.h | 12 +-- .../Framework/DataHandling/src/Load.cpp | 73 ++++++++++--------- .../Framework/DataHandling/test/LoadTest.h | 44 +++++++---- 4 files changed, 78 insertions(+), 54 deletions(-) diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceProperty.h b/Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceProperty.h index 1b5612fcf39..7e8a5723d2f 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceProperty.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/WorkspaceProperty.h @@ -192,6 +192,7 @@ namespace Mantid } else { + if( m_optional ) return error; //Return a user level error error = "Enter a name for the Output workspace"; //the debug message has more detail to put it in context @@ -292,7 +293,7 @@ namespace Mantid virtual bool store() { bool result = false; - + if ( ! this->operator()() && m_optional ) return result; if ( this->direction() ) // Output or InOut { // Check that workspace exists diff --git a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/Load.h b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/Load.h index bc7a39fdd96..77a8895c8f6 100644 --- a/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/Load.h +++ b/Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/Load.h @@ -53,21 +53,23 @@ namespace Mantid virtual const std::string name() const { return "Load"; } /// Algorithm's version for identification overriding a virtual method virtual int version() const { return 1; } + /// Category virtual const std::string category() const { return "DataHandling"; } - + private: ///init void init(); /// execute void exec(); - /// This method returns shared pointer to load algorithm which got the highest preference after file check. - API::IAlgorithm_sptr getLoadAlgorithmfromFile(const std::string& filePath); + /// This method returns shared pointer to load algorithm which got + /// the highest preference after file check. + API::IAlgorithm_sptr getFileLoader(const std::string& filePath); /// Set the output workspace(s) void setOutputWorkspace(API::IAlgorithm_sptr&); /// intiliases the load algorithm with highest preference and sets this as a child algorithm - void initialiseLoadSubAlgorithm(API::IAlgorithm_sptr alg, const double startProgress, const double endProgress, - const bool enableLogging, const int& version); + void initialiseLoadSubAlgorithm(API::IAlgorithm_sptr alg, const double startProgress, + const double endProgress, const bool enableLogging, const int& version); private: /// union used for identifying teh file type unsigned char* m_header_buffer; diff --git a/Code/Mantid/Framework/DataHandling/src/Load.cpp b/Code/Mantid/Framework/DataHandling/src/Load.cpp index d57a7fd3e52..f323279a31f 100644 --- a/Code/Mantid/Framework/DataHandling/src/Load.cpp +++ b/Code/Mantid/Framework/DataHandling/src/Load.cpp @@ -43,40 +43,40 @@ namespace Mantid declareProperty(new FileProperty("Filename", "", FileProperty::Load, exts), "The name of the file to read, including its full or relative\n" "path. (N.B. case sensitive if running on Linux)."); - declareProperty(new WorkspaceProperty<Workspace>("OutputWorkspace", - "",Direction::Output), "The name of the workspace that will be created, filled with the\n" + declareProperty(new WorkspaceProperty<Workspace>("OutputWorkspace", "",Direction::Output, true), + "The name of the workspace that will be created, filled with the\n" "read-in data and stored in the Analysis Data Service."); - - BoundedValidator<int> *mustBePositive = new BoundedValidator<int>(); - mustBePositive->setLower(1); - declareProperty("SpectrumMin", 1, mustBePositive); - declareProperty("SpectrumMax", EMPTY_INT(), mustBePositive->clone()); - declareProperty(new ArrayProperty<int>("SpectrumList")); - declareProperty("EntryNumber", 0, - "Load a particular entry, if supported by the file format (default: Load all entries)"); + declareProperty("FindLoader", false, "If true the algorithm will only be run as\n" + "far as is necessary to discover the concrete Load algorithm to use"); + declareProperty("LoaderName", std::string(""), "A string containing the name of the concrete loader used", + Direction::Output); } - /** checks this property exists in the list of algorithm properties. - */ - struct hasProperty - { - /** constructor which takes 1 arguement. - *@param name :: name of the property - */ - hasProperty(const std::string name):m_name(name){} - /**This method comapres teh property name - *@param prop :: shared pointer to property - *@return true if the property exists in the list of properties. - */ - bool operator()(Mantid::Kernel::Property* prop) - { - std::string name=prop->name(); - return (!name.compare(m_name)); - } - /// name of teh property - std::string m_name; - - }; + namespace + { + /** + * Checks this property exists in the list of algorithm properties. + */ + struct hasProperty + { + /** constructor which takes 1 arguement. + *@param name :: name of the property + */ + hasProperty(const std::string name):m_name(name){} + + /**This method comapres teh property name + *@param prop :: shared pointer to property + *@return true if the property exists in the list of properties. + */ + bool operator()(Mantid::Kernel::Property* prop) + { + std::string name=prop->name(); + return (!name.compare(m_name)); + } + /// name of teh property + std::string m_name; + }; + } /** * Executes the algorithm. @@ -88,11 +88,14 @@ namespace Mantid std::string ext = fileName.substr(i+1); std::transform(ext.begin(),ext.end(),ext.begin(),tolower); //get the shared pointer to the specialised load algorithm to execute - API::IAlgorithm_sptr alg= getLoadAlgorithmfromFile(fileName); + API::IAlgorithm_sptr alg= getFileLoader(fileName); if(!alg) { throw std::runtime_error("Cannot load file " + fileName); } + bool findOnly = getProperty("FindLoader"); + if( findOnly ) return; + g_log.information()<<"The sub load algorithm created to execute is "<<alg->name()<<" and it version is "<<alg->version()<<std::endl; double startProgress=0,endProgress=1; // set the load algorithm as a child algorithm @@ -129,7 +132,7 @@ namespace Mantid *@param filePath :: path of the file *@return filePath - path of the file */ - API::IAlgorithm_sptr Load::getLoadAlgorithmfromFile(const std::string& filePath) + API::IAlgorithm_sptr Load::getFileLoader(const std::string& filePath) { unsigned char* header_buffer = header_buffer_union.c; int nread; @@ -182,6 +185,10 @@ namespace Mantid load=alg_itr->second; } } + if( load ) + { + setPropertyValue("LoaderName", load->name()); + } return load; } diff --git a/Code/Mantid/Framework/DataHandling/test/LoadTest.h b/Code/Mantid/Framework/DataHandling/test/LoadTest.h index 6c1eca9cd8d..7321af7d837 100644 --- a/Code/Mantid/Framework/DataHandling/test/LoadTest.h +++ b/Code/Mantid/Framework/DataHandling/test/LoadTest.h @@ -15,6 +15,28 @@ using namespace Mantid::DataHandling; class LoadTest : public CxxTest::TestSuite { public: + + void testFindLoader() + { + Load loader; + loader.initialize(); + loader.setPropertyValue("Filename","IRS38633.raw"); + const std::string outputName("LoadTest_IRS38633raw"); + loader.setPropertyValue("OutputWorkspace", outputName); + loader.setPropertyValue("FindLoader", "1"); + loader.setRethrows(true); + TS_ASSERT_THROWS_NOTHING(loader.execute()); + + // Did it find the right loader + TS_ASSERT_EQUALS(loader.getPropertyValue("LoaderName"), "LoadRaw"); + AnalysisDataServiceImpl& dataStore = AnalysisDataService::Instance(); + TS_ASSERT_EQUALS(dataStore.doesExist(outputName), false); + if( dataStore.doesExist(outputName) ) + { + AnalysisDataService::Instance().remove(outputName); + } + } + void testRaw() { Load loader; @@ -59,8 +81,9 @@ public: AnalysisDataService::Instance().remove("LoadTest_Output_6"); } //disabled because hdf4 can't be opened on windows 64-bit, I think - void t1estNexus() + void testNexus() { +#ifndef _WIN64 Load loader; loader.initialize(); loader.setPropertyValue("Filename","emu00006473.nxs"); @@ -69,10 +92,12 @@ public: MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("LoadTest_Output")); TS_ASSERT(ws); AnalysisDataService::Instance().remove("LoadTest_Output"); + #endif } - void t1estNexusGroup() + void testNexusGroup() { +#ifndef _WIN64 Load loader; loader.initialize(); loader.setPropertyValue("Filename","MUSR00015189.nxs"); @@ -85,8 +110,9 @@ public: AnalysisDataService::Instance().remove("LoadTest_Output"); AnalysisDataService::Instance().remove("LoadTest_Output_1"); AnalysisDataService::Instance().remove("LoadTest_Output_2"); +#endif } - void t1estISISNexus() + void testISISNexus() { Load loader; loader.initialize(); @@ -98,18 +124,6 @@ public: AnalysisDataService::Instance().remove("LoadTest_Output"); } - void testEntryNumber() - { - Load loader; - loader.initialize(); - loader.setPropertyValue("Filename","TEST00000008.nxs"); - loader.setPropertyValue("OutputWorkspace","LoadTest_entry2"); - loader.setPropertyValue("EntryNumber","2"); - TS_ASSERT_THROWS_NOTHING(loader.execute()); - Workspace2D_sptr wsg = boost::dynamic_pointer_cast<Workspace2D>(AnalysisDataService::Instance().retrieve("LoadTest_entry2")); - TS_ASSERT(wsg); - AnalysisDataService::Instance().remove("LoadTest_entry2"); - } void testUnknownExt() { Load loader; -- GitLab