From 2a73b57aa7c0423c2f3537df549ac26308d48292 Mon Sep 17 00:00:00 2001 From: Sofia Antony <sofia.antony@stfc.ac.uk> Date: Fri, 10 Dec 2010 10:59:08 +0000 Subject: [PATCH] re #1538 - Refactored generic load algorithm.Implemented quick file check and file check in all load algorithms. --- .../inc/MantidDataHandling/Load.h | 40 ++- .../inc/MantidDataHandling/LoadAscii.h | 9 +- .../inc/MantidDataHandling/LoadCanSAS1D.h | 8 +- .../inc/MantidDataHandling/LoadGSS.h | 11 +- .../inc/MantidDataHandling/LoadRKH.h | 10 +- .../inc/MantidDataHandling/LoadRaw3.h | 7 + .../inc/MantidDataHandling/LoadRawHelper.h | 13 +- .../inc/MantidDataHandling/LoadSNSspec.h | 9 +- .../inc/MantidDataHandling/LoadSPE.h | 11 +- .../inc/MantidDataHandling/LoadSpice2D.h | 8 +- Code/Mantid/DataHandling/src/Load.cpp | 300 ++++++++++-------- Code/Mantid/DataHandling/src/LoadAscii.cpp | 116 ++++++- Code/Mantid/DataHandling/src/LoadCanSAS1D.cpp | 56 ++++ Code/Mantid/DataHandling/src/LoadGSS.cpp | 57 ++++ Code/Mantid/DataHandling/src/LoadRKH.cpp | 157 ++++++++- Code/Mantid/DataHandling/src/LoadRaw3.cpp | 22 +- .../Mantid/DataHandling/src/LoadRawHelper.cpp | 37 ++- Code/Mantid/DataHandling/src/LoadSNSspec.cpp | 97 +++++- Code/Mantid/DataHandling/src/LoadSPE.cpp | 66 +++- Code/Mantid/DataHandling/src/LoadSpec.cpp | 9 +- Code/Mantid/DataHandling/src/LoadSpice2D.cpp | 54 ++++ Code/Mantid/DataHandling/test/LoadTest.h | 45 +++ Code/Mantid/Nexus/Nexus.vcxproj | 4 +- .../Nexus/inc/MantidNexus/LoadISISNexus2.h | 9 +- .../Nexus/inc/MantidNexus/LoadMuonNexus.h | 8 +- .../Nexus/inc/MantidNexus/LoadMuonNexus2.h | 5 + .../inc/MantidNexus/LoadNexusProcessed.h | 8 +- .../Nexus/inc/MantidNexus/LoadSNSNexus.h | 10 +- Code/Mantid/Nexus/src/LoadISISNexus2.cpp | 64 +++- Code/Mantid/Nexus/src/LoadMuonNexus.cpp | 60 +++- Code/Mantid/Nexus/src/LoadMuonNexus2.cpp | 66 +++- Code/Mantid/Nexus/src/LoadNexusProcessed.cpp | 63 +++- Code/Mantid/Nexus/src/LoadSNSNexus.cpp | 59 +++- 33 files changed, 1300 insertions(+), 198 deletions(-) diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/Load.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/Load.h index 2c02044f2ff..23d6f5c257a 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/Load.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/Load.h @@ -5,7 +5,7 @@ // Includes //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" - +#include "MantidAPI/IDataFileChecker.h" namespace Mantid { namespace DataHandling @@ -37,35 +37,43 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ + + static const int bufferSize=100; class DLLExport Load : public API::Algorithm { public: + /// Default constructor Load(){} + /// Destructor ~Load() {} + /// Algorithm's name for identification overriding a virtual method virtual const std::string name() const { return "Load"; } + /// Algorithm's version for identification overriding a virtual method virtual int version() const { return 1; } virtual const std::string category() const { return "DataHandling"; } - + private: + ///init void init(); + /// execute void exec(); - /// Run LoadRaw - void runLoadRaw(API::IAlgorithm_sptr&); - /// Run LoadNexus - void runLoadNexus(API::IAlgorithm_sptr&); - /// Run LoadAscii - void runLoadAscii(API::IAlgorithm_sptr&); - /// Run LoadSPE - void runLoadSPE(API::IAlgorithm_sptr&); - /// Run LoadSpice2D - void runLoadSpice2D(API::IAlgorithm_sptr&); + /// This method returns shared pointer to load algorithm which got the highest preference after file check. + API::IAlgorithm_sptr getLoadAlgorithmfromFile(const std::string& filePath); /// Set the output workspace(s) void setOutputWorkspace(API::IAlgorithm_sptr&); - /// Set the output workspace - void setOutputMatrixWorkspace(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); + private: + /// union used for identifying teh file type + unsigned char* m_header_buffer; + union { + unsigned u; + unsigned long ul; + unsigned char c[bufferSize+1]; + } header_buffer_union; + }; } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadAscii.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadAscii.h index c0c04df8191..d6750503ce3 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadAscii.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadAscii.h @@ -5,6 +5,7 @@ // Includes //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" +#include "MantidAPI/IDataFileChecker.h" namespace Mantid { @@ -44,7 +45,7 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadAscii : public API::Algorithm + class DLLExport LoadAscii :public API::IDataFileChecker { public: LoadAscii(); @@ -53,6 +54,12 @@ namespace Mantid virtual int version() const { return 1; } virtual const std::string category() const { return "DataHandling"; } + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); + + private: void init(); void exec(); diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadCanSAS1D.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadCanSAS1D.h index 92bbb779501..824fdfe87a0 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadCanSAS1D.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadCanSAS1D.h @@ -6,6 +6,7 @@ //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" #include "MantidDataObjects/Workspace2D.h" +#include "MantidAPI/IDataFileChecker.h" #include "Poco/DOM/Element.h" #include "Poco/DOM/Node.h" //---------------------------------------------------------------------- @@ -53,7 +54,7 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadCanSAS1D : public API::Algorithm + class DLLExport LoadCanSAS1D : public API::IDataFileChecker { public: ///default constructor @@ -67,6 +68,11 @@ namespace Mantid /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling"; } + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); + private: /// If a workspace group is created this is set from empty to the root name of the members, the name of the workspace group members up to and including the _ std::string m_groupMembersBase; diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadGSS.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadGSS.h index 2ff42b59cc5..df605f6fb05 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadGSS.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadGSS.h @@ -5,7 +5,7 @@ // Includes //--------------------------------------------------- #include "MantidAPI/Algorithm.h" - +#include "MantidAPI/IDataFileChecker.h" namespace Mantid { namespace DataHandling @@ -36,11 +36,11 @@ namespace DataHandling File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> Code Documentation is available at: <http://doxygen.mantidproject.org> */ -class DLLExport LoadGSS : public Mantid::API::Algorithm +class DLLExport LoadGSS : public API::IDataFileChecker { public: /// (Empty) Constructor - LoadGSS() : Mantid::API::Algorithm(){} + LoadGSS() {} /// Virtual destructor virtual ~LoadGSS() {} /// Algorithm's name @@ -50,6 +50,11 @@ public: /// Algorithm's category for identification virtual const std::string category() const { return "Diffraction"; } + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); + private: /// Initialisation code void init(); diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRKH.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRKH.h index 14a32f8e29b..d08e1eb06cd 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRKH.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRKH.h @@ -5,6 +5,7 @@ // Includes //--------------------------------------------------- #include "MantidAPI/Algorithm.h" +#include "MantidAPI/IDataFileChecker.h" #include <istream> namespace Mantid @@ -44,11 +45,11 @@ namespace DataHandling File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> Code Documentation is available at: <http://doxygen.mantidproject.org> */ -class DLLExport LoadRKH : public Mantid::API::Algorithm +class DLLExport LoadRKH : public API::IDataFileChecker { public: /// Constructor - LoadRKH() : Mantid::API::Algorithm(), m_unitKeys(), m_RKHKeys() {} + LoadRKH() :m_unitKeys(), m_RKHKeys() {} /// Virtual destructor virtual ~LoadRKH() {} /// Algorithm's name @@ -57,7 +58,10 @@ public: virtual int version() const { return (1); } /// Algorithm's category for identification virtual const std::string category() const { return "DataHandling"; } - + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); private: // Initialisation code void init(); diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRaw3.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRaw3.h index 52ddd9da869..9876bd32443 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRaw3.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRaw3.h @@ -7,6 +7,7 @@ #include "MantidAPI/Algorithm.h" #include "MantidDataObjects/Workspace2D.h" #include "MantidDataHandling/LoadRawHelper.h" +#include "MantidAPI/IDataFileChecker.h" #include <climits> //---------------------------------------------------------------------- @@ -76,6 +77,12 @@ namespace Mantid virtual int version() const { return 3; } /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling"; } + + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); + private: /// Overwrites Algorithm method. diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRawHelper.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRawHelper.h index 5212b0ac061..d7e2e7c3f81 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRawHelper.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadRawHelper.h @@ -7,6 +7,7 @@ #include "MantidAPI/Algorithm.h" #include "MantidDataObjects/Workspace2D.h" #include "MantidAPI/Run.h" +#include "MantidAPI/IDataFileChecker.h" #include <climits> //---------------------------------------------------------------------- @@ -47,7 +48,7 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadRawHelper: public API::Algorithm + class DLLExport LoadRawHelper: public API::IDataFileChecker { public: /// Default constructor @@ -62,6 +63,12 @@ namespace Mantid FILE* openRawFile(const std::string & fileName); /// Read in run parameters Public so that LoadRaw2 can use it void loadRunParameters(API::MatrixWorkspace_sptr localWorkspace, ISISRAW * const = NULL) const; + + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and if this file can be loaded return a value between 1 and 100 + virtual int fileCheck(const std::string& filePath); + protected: /// Overwrites Algorithm method. void init(); @@ -154,7 +161,9 @@ namespace Mantid int& normalwsSpecs, int& monitorwsSpecs); /// load the specra void loadSpectra(FILE* file,const int& period, const int& m_total_specs, - DataObjects::Workspace2D_sptr ws_sptr,std::vector<boost::shared_ptr<MantidVec> >); + DataObjects::Workspace2D_sptr ws_sptr,std::vector<boost::shared_ptr<MantidVec> >); + + /// Has the spectrum_list property been set? bool m_list; /// Have the spectrum_min/max properties been set? diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSNSspec.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSNSspec.h index f4ffdd0732f..df192dc4097 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSNSspec.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSNSspec.h @@ -5,7 +5,7 @@ // Includes //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" - +#include "MantidAPI/IDataFileChecker.h" namespace Mantid { namespace DataHandling @@ -44,7 +44,7 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadSNSspec : public API::Algorithm + class DLLExport LoadSNSspec : public API::IDataFileChecker { public: LoadSNSspec(); @@ -53,6 +53,11 @@ namespace Mantid virtual int version() const { return 1; } virtual const std::string category() const { return "DataHandling"; } + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); + private: void init(); void exec(); diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSPE.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSPE.h index 9436f585a2c..ec125e0dc3b 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSPE.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSPE.h @@ -5,7 +5,7 @@ // Includes //--------------------------------------------------- #include "MantidAPI/Algorithm.h" - +#include "MantidAPI/IDataFileChecker.h" namespace Mantid { namespace DataHandling @@ -42,11 +42,11 @@ namespace DataHandling File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> Code Documentation is available at: <http://doxygen.mantidproject.org> */ -class DLLExport LoadSPE : public API::Algorithm +class DLLExport LoadSPE : public API::IDataFileChecker { public: /// Constructor - LoadSPE() : API::Algorithm() {} + LoadSPE() : API::IDataFileChecker() {} /// Virtual destructor virtual ~LoadSPE() {} /// Algorithm's name @@ -55,7 +55,10 @@ public: virtual int version() const { return (1); } /// Algorithm's category for identification virtual const std::string category() const { return "DataHandling"; } - + ///checks the file can be loaded by reading 1st 100 bytes and looking at the file extension. + bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and if this file can be loaded return a value between 1 and 100 + int fileCheck(const std::string& filePath); private: // Initialisation code void init(); diff --git a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSpice2D.h b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSpice2D.h index 679e7405918..c82eb242ce8 100644 --- a/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSpice2D.h +++ b/Code/Mantid/DataHandling/inc/MantidDataHandling/LoadSpice2D.h @@ -6,6 +6,7 @@ //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" #include "MantidDataObjects/Workspace2D.h" +#include "MantidAPI/IDataFileChecker.h" //---------------------------------------------------------------------- namespace Poco { @@ -48,7 +49,7 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadSpice2D : public API::Algorithm + class DLLExport LoadSpice2D : public API::IDataFileChecker { public: ///default constructor @@ -64,6 +65,11 @@ namespace Mantid /// Number of monitors static const int nMonitors = 2; + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); + private: /// Overwrites Algorithm method. void init(); diff --git a/Code/Mantid/DataHandling/src/Load.cpp b/Code/Mantid/DataHandling/src/Load.cpp index 43293fb494e..dc3aef2fc3a 100644 --- a/Code/Mantid/DataHandling/src/Load.cpp +++ b/Code/Mantid/DataHandling/src/Load.cpp @@ -5,9 +5,9 @@ #include "MantidAPI/FileProperty.h" #include "MantidKernel/ArrayProperty.h" #include "MantidDataObjects/Workspace2D.h" - -#include <algorithm> -#include <sstream> +#include "MantidAPI/IDataFileChecker.h" +#include "MantidAPI/LoadAlgorithmFactory.h" +#include<algorithm> namespace Mantid { @@ -22,6 +22,7 @@ namespace Mantid /// Initialisation method. void Load::init() { + std::vector<std::string> exts; exts.push_back(".raw"); exts.push_back(".s*"); @@ -37,7 +38,7 @@ namespace Mantid exts.push_back(".csv"); exts.push_back(".spe"); - + 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)."); @@ -51,8 +52,20 @@ namespace Mantid declareProperty("SpectrumMax", EMPTY_INT(), mustBePositive->clone()); declareProperty(new ArrayProperty<int>("SpectrumList")); } + + struct hasProperty + { + hasProperty(const std::string name):m_name(name){} + bool operator()(Mantid::Kernel::Property* prop) + { + std::string name=prop->name(); + return (!name.compare(m_name)); + } + std::string m_name; + + }; - /** + /** * Executes the algorithm. */ void Load::exec() @@ -61,155 +74,178 @@ namespace Mantid std::string::size_type i = fileName.find_last_of('.'); std::string ext = fileName.substr(i+1); std::transform(ext.begin(),ext.end(),ext.begin(),tolower); - - API::IAlgorithm_sptr load; - if (ext == "raw" || ext == "add") - { - runLoadRaw(load); - } - else if (ext == "nxs" || ext == "nx5") + //get the shared pointer to the specialised load algorithm to execute + API::IAlgorithm_sptr alg= getLoadAlgorithmfromFile(fileName); + if(!alg) { - runLoadNexus(load); - } - else if (ext == "dat" || ext == "txt" || ext == "csv") - { - runLoadAscii(load); - } - else if (ext == "spe") - { - runLoadSPE(load); - } - else if (ext[0] == 's') - { - runLoadRaw(load); - } - else if (ext[0] == 'n') - { - runLoadNexus(load); + throw std::runtime_error("Cannot load file " + fileName); } - else if (ext == "xml") - { - try - { - runLoadNexus(load); - } - catch(...) - { - runLoadSpice2D(load); + g_log.debug()<<"The sub algorithm name is "<<alg->name()<<std::endl; + double startProgress=0,endProgress=1; + // set the load algorithm as a child algorithm + initialiseLoadSubAlgorithm(alg,startProgress,endProgress,true,-1); + + //get the list of properties for this algorithm + std::vector<Kernel::Property*> props=getProperties(); + ///get the list properties for the sub load algorithm + std::vector<Kernel::Property*>loader_props=alg->getProperties(); + + //loop through the properties of this algorithm + std::vector<Kernel::Property*>::iterator itr; + for (itr=props.begin();itr!=props.end();++itr) + { + std::vector<Mantid::Kernel::Property*>::iterator prop; + //if the load sub algorithm has the same property then set it. + prop=std::find_if(loader_props.begin(),loader_props.end(),hasProperty((*itr)->name())); + if(prop!=loader_props.end()) + { + alg->setPropertyValue((*prop)->name(),getPropertyValue((*prop)->name())); } + } - else - { - throw std::runtime_error("Cannot load file " + fileName); - } - - } - - /** - * Run LoadRaw - * @param load Shared pointer to the subalgorithm - */ - void Load::runLoadRaw(API::IAlgorithm_sptr& load) - { - load = createSubAlgorithm("LoadRaw",0,1); - load->initialize(); - load->setPropertyValue("Filename",getPropertyValue("Filename")); - load->setPropertyValue("OutputWorkspace",getPropertyValue("OutputWorkspace")); - load->setPropertyValue("SpectrumMin",getPropertyValue("SpectrumMin")); - load->setPropertyValue("SpectrumMax",getPropertyValue("SpectrumMax")); - load->setPropertyValue("SpectrumList",getPropertyValue("SpectrumList")); - load->execute(); - setOutputWorkspace(load); + //execute the load sub algorithm + alg->execute(); + //se the workspace + setOutputWorkspace(alg); + } - /** - * Run LoadNexus - * @param load Shared pointer to the subalgorithm - */ - void Load::runLoadNexus(API::IAlgorithm_sptr& load) - { - load = createSubAlgorithm("LoadNexus",0,1); - load->initialize(); - load->setPropertyValue("Filename",getPropertyValue("Filename")); - load->setPropertyValue("OutputWorkspace",getPropertyValue("OutputWorkspace")); - load->setPropertyValue("SpectrumMin",getPropertyValue("SpectrumMin")); - load->setPropertyValue("SpectrumMax",getPropertyValue("SpectrumMax")); - load->setPropertyValue("SpectrumList",getPropertyValue("SpectrumList")); - load->execute(); - setOutputWorkspace(load); - } - /** - * Run LoadAscii - * @param load Shared pointer to the subalgorithm - */ - void Load::runLoadAscii(API::IAlgorithm_sptr& load) + /** get a shared pointer to the load algorithm with highest preference for loading + *@param filePath path of the file + *@return filePath - path of the file + */ + API::IAlgorithm_sptr Load::getLoadAlgorithmfromFile(const std::string& filePath) + { + unsigned char* header_buffer = header_buffer_union.c; + int nread; + /* Open the file and read in the first bufferSize bytes - these will + * be used to determine the type of the file + */ + FILE* fp = fopen(filePath.c_str(), "rb"); + if (fp == NULL) + { + throw Kernel::Exception::FileError("Unable to open the file:", filePath); + } + nread = fread((char*)header_buffer,sizeof(char), bufferSize,fp); + header_buffer[bufferSize] = '\0'; + if (nread == -1) + { + fclose(fp); + } + + if (fclose(fp) != 0) + { + } + + int val=0; + API::IAlgorithm_sptr load; + // now get the name of algorithms registered in the loadAlgorithmfactory + std::vector<std::string> algnames=API::LoadAlgorithmFactory::Instance().getKeys(); + std::map<std::string, boost::shared_ptr<DataHandling::IDataFileChecker> >loadalgs; + /// create load algorithms and store it in a map + std::vector<std::string>::const_iterator citr; + for (citr=algnames.begin();citr!=algnames.end();++citr) + { + loadalgs[*citr]= API::LoadAlgorithmFactory::Instance().create(*citr); + } + + std::map<std::string, boost::shared_ptr<DataHandling::IDataFileChecker> >::const_iterator alg_itr; + //loop thorugh the map and do a quick check for each load algorithm by opening the file and look at it's first 100 bytes and extensions + //if quick check succeeds then do a filecheck again by opening the file and read the structure of the file + for (alg_itr=loadalgs.begin();alg_itr!=loadalgs.end();++alg_itr) + { + bool bcheck=alg_itr->second->quickFileCheck(filePath,nread,header_buffer); + if(!bcheck) + { + continue; + } + int ret=alg_itr->second->fileCheck(filePath); + if(ret>val) + { + // algorithm with highest preference will cached. + val=ret; + load=alg_itr->second; + } + } + return load; + } + + /** This method set the algorithm as a child algorithm. + * @param alg The shared pointer to a algorithm + * @param startProgress The percentage progress value of the overall algorithm where this child algorithm starts + * @param endProgress The percentage progress value of the overall algorithm where this child algorithm ends + * @param enableLogging Set to false to disable logging from the child algorithm + * @param version The version of the child algorithm to create. By default gives the latest version. + */ + void Load::initialiseLoadSubAlgorithm(API::IAlgorithm_sptr alg, const double startProgress, const double endProgress, + const bool enableLogging, const int& version) + { - load = createSubAlgorithm("LoadAscii",0,1); - load->initialize(); - load->setPropertyValue("Filename",getPropertyValue("Filename")); - load->setPropertyValue("OutputWorkspace",getPropertyValue("OutputWorkspace")); - load->execute(); - setOutputMatrixWorkspace(load); - } + //set as a child + alg->initialize(); + alg->setChild(true); + alg->setLogging(enableLogging); + + // If output workspaces are nameless, give them a temporary name to satisfy validator + const std::vector< Property*> &props = alg->getProperties(); + for (unsigned int i = 0; i < props.size(); ++i) + { + if (props[i]->direction() == 1 && dynamic_cast<IWorkspaceProperty*>(props[i]) ) + { + if ( props[i]->value().empty() ) props[i]->setValue("ChildAlgOutput"); + } + } - /** - * Run LoadSPE - * @param load Shared pointer to the subalgorithm - */ - void Load::runLoadSPE(API::IAlgorithm_sptr& load) - { - load = createSubAlgorithm("LoadSPE",0,1); - load->initialize(); - load->setPropertyValue("Filename",getPropertyValue("Filename")); - load->setPropertyValue("OutputWorkspace",getPropertyValue("OutputWorkspace")); - load->execute(); - setOutputMatrixWorkspace(load); - } + if (startProgress >= 0 && endProgress > startProgress && endProgress <= 1.) + { + alg->addObserver(m_progressObserver); + alg->setChildStartProgress(startProgress); + alg->setChildEndProgress(endProgress); + } - void Load::runLoadSpice2D(API::IAlgorithm_sptr& load) - { - load = createSubAlgorithm("LoadSpice2D",0,1); - load->initialize(); - load->setPropertyValue("Filename",getPropertyValue("Filename")); - load->setPropertyValue("OutputWorkspace",getPropertyValue("OutputWorkspace")); - load->execute(); - setOutputWorkspace(load); } - + /** * Set the output workspace(s) if the load's return workspace * has type API::Workspace + *@param shared pointer to load algorithm */ void Load::setOutputWorkspace(API::IAlgorithm_sptr& load) { - Workspace_sptr ws = load->getProperty("OutputWorkspace"); - setProperty("OutputWorkspace",ws); - WorkspaceGroup_sptr wsg = boost::dynamic_pointer_cast<WorkspaceGroup>(ws); - if (wsg) + try { - std::vector<std::string> names = wsg->getNames(); - for(size_t i = 0; i < names.size(); ++i) + Workspace_sptr ws = load->getProperty("OutputWorkspace"); + WorkspaceGroup_sptr wsg = boost::dynamic_pointer_cast<WorkspaceGroup>(ws); + if (wsg) { - std::ostringstream propName; - propName << "OutputWorkspace_" << (i+1); - DataObjects::Workspace2D_sptr ws1 = load->getProperty(propName.str()); - std::string wsName = load->getPropertyValue(propName.str()); - declareProperty(new WorkspaceProperty<>(propName.str(),wsName,Direction::Output)); - setProperty(propName.str(),boost::dynamic_pointer_cast<MatrixWorkspace>(ws1)); + setProperty("OutputWorkspace",ws); + std::vector<std::string> names = wsg->getNames(); + for(size_t i = 0; i < names.size(); ++i) + { + std::ostringstream propName; + propName << "OutputWorkspace_" << (i+1); + DataObjects::Workspace2D_sptr memberwsws1 = load->getProperty(propName.str()); + + std::string memberwsName = load->getPropertyValue(propName.str()); + declareProperty(new WorkspaceProperty<>(propName.str(),memberwsName,Direction::Output)); + setProperty(propName.str(),boost::dynamic_pointer_cast<MatrixWorkspace>(memberwsws1)); + } + } + else + { + setProperty("OutputWorkspace",ws); } } + catch(std::runtime_error&) + { + MatrixWorkspace_sptr mws=load->getProperty("OutputWorkspace"); + setProperty("OutputWorkspace",boost::dynamic_pointer_cast<Workspace>(mws)); + } + + } - /** - * Set the output workspace(s) if the load's return workspace - * has type API::MatrixWorkspace - */ - void Load::setOutputMatrixWorkspace(API::IAlgorithm_sptr& load) - { - MatrixWorkspace_sptr ws = load->getProperty("OutputWorkspace"); - setProperty("OutputWorkspace",boost::dynamic_pointer_cast<Workspace>(ws)); - } - + } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/DataHandling/src/LoadAscii.cpp b/Code/Mantid/DataHandling/src/LoadAscii.cpp index c7d10e8b684..72092280c80 100644 --- a/Code/Mantid/DataHandling/src/LoadAscii.cpp +++ b/Code/Mantid/DataHandling/src/LoadAscii.cpp @@ -5,7 +5,7 @@ #include "MantidDataObjects/Workspace2D.h" #include "MantidKernel/UnitFactory.h" #include "MantidAPI/FileProperty.h" - +#include "MantidAPI/LoadAlgorithmFactory.h" #include <fstream> #include <boost/tokenizer.hpp> @@ -15,12 +15,15 @@ namespace Mantid { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(LoadAscii) + + //register the algorithm into loadalgorithm factory + DECLARE_LOADALGORITHM(LoadAscii) using namespace Kernel; using namespace API; /// Empty constructor - LoadAscii::LoadAscii() : Algorithm() {} + LoadAscii::LoadAscii() {} /// Initialisation method. void LoadAscii::init() @@ -163,5 +166,114 @@ namespace Mantid setProperty("OutputWorkspace",localWorkspace); } +/**This method does a quick file check by checking the no.of bytes read nread params and header buffer + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadAscii::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bascii(false); + (!extn.compare("dat")||!extn.compare("csv")|| extn.compare("txt"))?bascii=true:bascii=false; + + bool is_ascii (true); + for(int i=0; i<nread; i++) + { + if (!isascii(header_buffer[i])) + is_ascii =false; + } + return(is_ascii|| bascii?true:false); + } + +/**checks the file by opening it and reading few lines + * @param filePath name of the file including its path + * @return an integer value how much this algorithm can load the file + */ + int LoadAscii::fileCheck(const std::string& filePath) + { + std::ifstream file(filePath.c_str()); + if (!file) + { + g_log.error("Unable to open file: " + filePath); + throw Exception::FileError("Unable to open file: " , filePath); + } + //set up the separators + /*std::map<std::string,const char*>::const_iterator it; + std::string separators; + for(it=m_separatormap.begin();it!=m_separatormap.end();++it) + { + separators+=it->second; + } +*/ + std::string separators(","); + int ncols=0; + typedef boost::tokenizer<boost::char_separator<char> > tokenizer; + boost::char_separator<char> seps(separators.c_str()); + std::string line; + int bret=0; + while(!file.eof()) + { + getline(file,line) ; + if (line.empty()) + { + continue; + } + + if(line.at(0) == '#') + { + try + { + if(line.at(1)=='L') + { + return 0; + } + } + catch(std::out_of_range&) + { + continue; + } + } + else + { + //break at a non empty line + break; + } + } + + boost::tokenizer<boost::char_separator<char> > values(line, seps); + for (tokenizer::iterator it = values.begin(); it != values.end(); ++it) + { + ++ncols; + } + + //if the line has odd number of coulmns with mantid supported separators + // this is considered as ascci file + if (ncols % 2 == 1 && ncols>2) + { + bret+=40; + } + bool bloadAscii(true); + //if the data is of double type this file can be loaded by loadascci + double data; + for (tokenizer::iterator it = values.begin(); it != values.end(); ++it) + { + std::istringstream is(*it); + is>>data; + if(is.fail()) + { + bloadAscii=false; + break; + } + + } + if(bloadAscii) + { + bret+=40; + } + return bret; + } + } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/DataHandling/src/LoadCanSAS1D.cpp b/Code/Mantid/DataHandling/src/LoadCanSAS1D.cpp index 4892932a616..5ece66a202f 100644 --- a/Code/Mantid/DataHandling/src/LoadCanSAS1D.cpp +++ b/Code/Mantid/DataHandling/src/LoadCanSAS1D.cpp @@ -8,6 +8,7 @@ #include "MantidKernel/UnitFactory.h" #include "MantidKernel/ConfigService.h" #include "MantidAPI/AlgorithmFactory.h" +#include "MantidAPI/LoadAlgorithmFactory.h" #include "Poco/Path.h" #include "Poco/DOM/DOMParser.h" @@ -37,6 +38,9 @@ namespace DataHandling // Register the algorithm into the AlgorithmFactory DECLARE_ALGORITHM(LoadCanSAS1D) +//register the algorithm into loadalgorithm factory +DECLARE_LOADALGORITHM(LoadCanSAS1D) + /// constructor LoadCanSAS1D::LoadCanSAS1D() : m_groupNumber(0) {} @@ -285,5 +289,57 @@ void LoadCanSAS1D::createLogs(const Poco::XML::Element * const sasEntry, API::Ma } } + +/**This method does a quick file check by checking the no.of bytes read nread params and header buffer + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ +bool LoadCanSAS1D::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) +{ + std::string extn=extension(filePath); + bool bspice2d(false); + (!extn.compare("xml"))?bspice2d=true:bspice2d=false; + + const char* xml_header="<?xml version="; + if ( ((unsigned)nread >= strlen(xml_header)) && + !strncmp((char*)header_buffer, xml_header, strlen(xml_header)) ) + { + } + return(bspice2d?true:false); +} + +/**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ +int LoadCanSAS1D::fileCheck(const std::string& filePath) +{ + // Set up the DOM parser and parse xml file + DOMParser pParser; + Document* pDoc; + try + { + pDoc = pParser.parse(filePath); + } catch (...) + { + throw Kernel::Exception::FileError("Unable to parse File:", filePath); + } + // Get pointer to root element + Element* pRootElem = pDoc->documentElement(); + if(pRootElem) + { + if(!pRootElem->tagName().compare("SASroot")) + { + return 80; + } + } + return 0; + +} + + + } } diff --git a/Code/Mantid/DataHandling/src/LoadGSS.cpp b/Code/Mantid/DataHandling/src/LoadGSS.cpp index 866902160a7..0cd88055132 100644 --- a/Code/Mantid/DataHandling/src/LoadGSS.cpp +++ b/Code/Mantid/DataHandling/src/LoadGSS.cpp @@ -7,6 +7,7 @@ #include "MantidKernel/UnitFactory.h" #include <boost/math/special_functions/fpclassify.hpp> +#include "MantidAPI/LoadAlgorithmFactory.h" #include "Poco/File.h" #include <iostream> #include <fstream> @@ -19,6 +20,8 @@ using namespace Mantid::Kernel; // Register the algorithm into the AlgorithmFactory DECLARE_ALGORITHM(LoadGSS) +//register the algorithm into loadalgorithm factory +DECLARE_LOADALGORITHM(LoadGSS) //--------------------------------------------------- // Private member functions //--------------------------------------------------- @@ -180,3 +183,57 @@ void LoadGSS::exec() setProperty("OutputWorkspace", outputWorkspace); return; } + + +/**This method does a quick file type check by checking the first 100 bytes of the file + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadGSS::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bascii(false); + (!extn.compare("text"))?bascii=true:bascii=false; + + bool is_ascii (true); + for(int i=0; i<nread; i++) + { + if (!isascii(header_buffer[i])) + is_ascii =false; + } + return(is_ascii|| bascii?true:false); + } + +/**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ + int LoadGSS::fileCheck(const std::string& filePath) + { + std::ifstream file(filePath.c_str()); + if (!file) + { + g_log.error("Unable to open file: " + filePath); + throw Exception::FileError("Unable to open file: " , filePath); + } + std::string str; + getline(file,str);//workspace ttile first line + while (!file.eof()) + { + getline(file,str); + if(str.empty()||str[0]=='#') + { + continue; + } + if(!str.substr(0,4).compare("BANK")&& (str.find("RALF")!=std::string::npos)&& (str.find("FXYE")!=std::string::npos)) + { + return 80; + } + return 0; + } + return 0; + + } + diff --git a/Code/Mantid/DataHandling/src/LoadRKH.cpp b/Code/Mantid/DataHandling/src/LoadRKH.cpp index 32bdd6e45e8..fb34e64e03f 100644 --- a/Code/Mantid/DataHandling/src/LoadRKH.cpp +++ b/Code/Mantid/DataHandling/src/LoadRKH.cpp @@ -5,14 +5,19 @@ #include "MantidAPI/FileProperty.h" #include "MantidKernel/UnitFactory.h" #include "MantidDataObjects/Workspace1D.h" +#include "MantidAPI/LoadAlgorithmFactory.h" +#include "boost/date_time/gregorian/gregorian.hpp" +#include "boost/date_time/date_parsing.hpp" #include <fstream> using namespace Mantid::DataHandling; +using namespace Mantid::Kernel; // Register the algorithm into the AlgorithmFactory DECLARE_ALGORITHM(LoadRKH) - +//register the algorithm into loadalgorithm factory +DECLARE_LOADALGORITHM(LoadRKH) //--------------------------------------------------- // Private member functions //--------------------------------------------------- @@ -184,3 +189,153 @@ void LoadRKH::skipLines(std::istream & strm, int nlines) getline(strm, buried); } } + +/**This method does a quick file check by checking the no.of bytes read nread params and header buffer + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ +bool LoadRKH::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) +{ + std::string extn=extension(filePath); + bool bascii(false); + (!extn.compare("txt")|| extn.compare("Q"))?bascii=true:bascii=false; + + bool is_ascii (true); + for(int i=0; i<nread; i++) + { + if (!isascii(header_buffer[i])) + is_ascii =false; + } + return(is_ascii|| bascii?true:false); +} + +/**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ +int LoadRKH::fileCheck(const std::string& filePath) +{ + int bret=0; + std::ifstream file(filePath.c_str()); + if (!file) + { + g_log.error("Unable to open file: " + filePath); + throw Exception::FileError("Unable to open file: " , filePath); + } + + typedef boost::tokenizer<boost::char_separator<char> > tokenizer; + boost::char_separator<char> sep(" "); + + std::string fileline(""); + std::string ws,da,dt,time; + int ncols=0; + //get first line + getline(file, fileline); + std::istringstream is(fileline); + //get diff words from first line + is>>ws>>da>>dt>>time; + //get the day,year and month part from first line + std::string::size_type pos=dt.find_last_of("-"); + if(pos==std::string::npos) + { + return 0; + } + std::string year(dt.substr(pos+1,dt.length()-pos)); + + std::string::size_type pos1=dt.find_last_of("-",pos-1); + if(pos1==std::string::npos) + { + return 0; + } + std::string month(dt.substr(pos1+1,pos-pos1)); + + std::string day(dt.substr(0,pos1)); + std::string rkhdate=year+"-"; + rkhdate+=month; + rkhdate+="-"; + rkhdate+=day; +//if the first line contains date this could be rkh file + try + { + boost::gregorian::date d(boost::gregorian::from_string(rkhdate)); + boost::gregorian::date::ymd_type ymd = d.year_month_day(); + if(ymd.month>=1 && ymd.month<13) + { + bret+=10; + } + if(ymd.day>=1 && ymd.month<32) + { + bret+=10; + } + } + catch(std::exception&) + { + return 0; + } + + + //read second line + getline(file, fileline); + if(fileline.find("(")!=std::string::npos && fileline.find(")")!=std::string::npos) + { + bret+=10; + } + //read 3rd line + getline(file, fileline); + if(fileline.find("(")!=std::string::npos && fileline.find(")")!=std::string::npos) + { + bret+=10; + if(fileline.find(" 0 0 0 1")!=std::string::npos) + { + bret+=10; + } + } + else + { + tokenizer tok(fileline,sep); + for (tokenizer::iterator beg=tok.begin(); beg!=tok.end(); ++beg) + { + ++ncols; + } + if(ncols==7) + { + bret+=10; + } + } + //4th line + getline(file, fileline); + if(fileline.find(" 0 0 0 0")!=std::string::npos) + { + bret+=10; + } + else if(fileline.find(" 0 ")!=std::string::npos) + { + bret+=10; + } + //5th line + getline(file, fileline); + if(fileline.find("3 (F12.5,2E16.6)")!=std::string::npos) + { + bret+=10; + } + else if (fileline.find(" 1\n")!=std::string::npos) + { + bret+=10; + } + ncols=0; + //6th line data line + getline(file, fileline); + tokenizer tok1(fileline, sep); + for (tokenizer::iterator beg=tok1.begin(); beg!=tok1.end(); ++beg) + { + ++ncols; + } + if(ncols==3) + { + bret+=20; + } + + return bret; +} diff --git a/Code/Mantid/DataHandling/src/LoadRaw3.cpp b/Code/Mantid/DataHandling/src/LoadRaw3.cpp index c3733b301d7..acf548b2439 100644 --- a/Code/Mantid/DataHandling/src/LoadRaw3.cpp +++ b/Code/Mantid/DataHandling/src/LoadRaw3.cpp @@ -15,6 +15,7 @@ #include "MantidAPI/FileProperty.h" #include "LoadRaw/isisraw2.h" #include "MantidDataHandling/LoadLog.h" +#include "MantidAPI/LoadAlgorithmFactory.h" #include <boost/shared_ptr.hpp> #include "Poco/Path.h" @@ -27,6 +28,7 @@ namespace DataHandling { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(LoadRaw3) +DECLARE_LOADALGORITHM(LoadRaw3) using namespace Kernel; using namespace API; @@ -662,7 +664,25 @@ void LoadRaw3::separateOrexcludeMonitors(DataObjects::Workspace2D_sptr localWork } } - +/**This method does a quick file check by checking the no.of bytes read nread params and header buffer + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ +bool LoadRaw3::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) +{ + return(LoadRawHelper::quickFileCheck(filePath,nread,header_buffer)?true:false); + +} +/**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ +int LoadRaw3::fileCheck(const std::string& filePath) +{ + return LoadRawHelper::fileCheck(filePath); +} } // namespace DataHandling diff --git a/Code/Mantid/DataHandling/src/LoadRawHelper.cpp b/Code/Mantid/DataHandling/src/LoadRawHelper.cpp index cc3dec0ac27..70044802bc3 100644 --- a/Code/Mantid/DataHandling/src/LoadRawHelper.cpp +++ b/Code/Mantid/DataHandling/src/LoadRawHelper.cpp @@ -18,6 +18,7 @@ #include "MantidDataHandling/LoadInstrumentHelper.h" #include "boost/date_time/gregorian/gregorian.hpp" +#include "MantidAPI/LoadAlgorithmFactory.h" #include <boost/shared_ptr.hpp> #include "Poco/Path.h" #include "Poco/DateTimeParser.h" @@ -32,12 +33,12 @@ namespace Mantid using namespace Kernel; using namespace API; - /// Constructor + /// Constructor LoadRawHelper::LoadRawHelper() : - Algorithm(),isisRaw(new ISISRAW2), + isisRaw(new ISISRAW2), m_list(false),m_spec_list(),m_spec_min(0), m_spec_max(EMPTY_INT()),m_specTimeRegimes(),m_bmspeclist(false) - { + { } LoadRawHelper::~LoadRawHelper() @@ -1014,6 +1015,36 @@ namespace Mantid } } + + /// +bool LoadRawHelper::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) +{ + std::string extn=extension(filePath); + bool braw(false); + (!extn.compare("raw")||!extn.compare("add")||!extn.compare("s"))?braw=true:braw=false; + /* + * look at the "address of RUN and INST section" attribute - if there, must be an ISIS raw file + */ + if ( ((nread > 88) && (header_buffer[84] == 32) && (header_buffer[88] == 126))|| braw ) + { + return true; + } + else + { + return false; + } + +} +/// +int LoadRawHelper::fileCheck(const std::string& filePath) +{ + //here the assumption is generic load algorithm will + //call filecheck if quickFileCheck passed. + // once quick checked passed return 80 for loadraw + return 80; +} + + } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/DataHandling/src/LoadSNSspec.cpp b/Code/Mantid/DataHandling/src/LoadSNSspec.cpp index 44f86f4e474..ce774d988ce 100644 --- a/Code/Mantid/DataHandling/src/LoadSNSspec.cpp +++ b/Code/Mantid/DataHandling/src/LoadSNSspec.cpp @@ -5,7 +5,7 @@ #include "MantidDataObjects/Workspace2D.h" #include "MantidKernel/UnitFactory.h" #include "MantidAPI/FileProperty.h" - +#include "MantidAPI/LoadAlgorithmFactory.h" #include <fstream> #include <cstring> #include <boost/tokenizer.hpp> @@ -16,12 +16,14 @@ namespace Mantid { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(LoadSNSspec) + //register the algorithm into loadalgorithm factory + DECLARE_LOADALGORITHM(LoadSNSspec) using namespace Kernel; using namespace API; /// Empty constructor - LoadSNSspec::LoadSNSspec() : Algorithm() {} + LoadSNSspec::LoadSNSspec() {} /// Initialisation method. void LoadSNSspec::init() @@ -171,6 +173,95 @@ namespace Mantid } } - + /**This method does a quick file check by checking the no.of bytes read nread params and header buffer + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadSNSspec::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bascii(false); + (!extn.compare("txt"))?bascii=true:bascii=false; + + bool is_ascii (true); + for(int i=0; i<nread; i++) + { + if (!isascii(header_buffer[i])) + is_ascii =false; + } + return(is_ascii|| bascii?true:false); + } + +/**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ + int LoadSNSspec::fileCheck(const std::string& filePath) + { + std::ifstream file(filePath.c_str()); + if (!file) + { + g_log.error("Unable to open file: " + filePath); + throw Exception::FileError("Unable to open file: " , filePath); + } + + int bret=0; + int ncols=0; + std::string str; + typedef boost::tokenizer<boost::char_separator<char> > tokenizer; + boost::char_separator<char> sep(" "); + + while(!file.eof()) + { + //read line + std::getline(file,str); + if(str.empty()) + { + continue; + } + try + { + //if it's comment line + if (str.at(0)=='#' ) + { + if(str.at(1) =='L') + { + tokenizer tok(str, sep); + for (tokenizer::iterator beg=tok.begin(); beg!=tok.end(); ++beg) + { + ++ncols; + } + //if the file contains a comment line starting with "#L" followed + //by three columns this could be load spec file + if(ncols>3) + { + bret+=40; + } + } + } + else //first non comment line is data line + { + ncols=0; + tokenizer tok(str, sep); + for (tokenizer::iterator beg=tok.begin(); beg!=tok.end(); ++beg) + { + ++ncols; + } + if(ncols==3) + { + bret+=40; + } + break; + } + } + catch(std::out_of_range& ) + { + } + } + + return bret; + } } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/DataHandling/src/LoadSPE.cpp b/Code/Mantid/DataHandling/src/LoadSPE.cpp index 35a30e4429c..ca469d1d15a 100644 --- a/Code/Mantid/DataHandling/src/LoadSPE.cpp +++ b/Code/Mantid/DataHandling/src/LoadSPE.cpp @@ -7,9 +7,10 @@ #include "MantidKernel/UnitFactory.h" #include "MantidAPI/NumericAxis.h" #include "MantidDataObjects/Histogram1D.h" +#include "MantidAPI/LoadAlgorithmFactory.h" #include <cstdio> #include <limits> - +#include <fstream> /// @cond // Don't document this very long winded way of getting "degrees" to print on the axis. namespace @@ -38,6 +39,9 @@ using namespace API; // Register the algorithm into the AlgorithmFactory DECLARE_ALGORITHM(LoadSPE) +//register the algorithm into loadalgorithm factory +DECLARE_LOADALGORITHM(LoadSPE) + //--------------------------------------------------- // Private member functions //--------------------------------------------------- @@ -224,5 +228,65 @@ void LoadSPE::reportFormatError(const std::string& what) throw Exception::FileError("Unexpected formatting in file: " , m_filename); } +/**This method does a quick file check by checking the no.of bytes read nread params and header buffer + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ +bool LoadSPE::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) +{ + std::string extn=extension(filePath); + bool bspe(false); + (!extn.compare("spe"))?bspe=true:bspe=false; + bool is_ascii (true); + for(int i=0; i<nread; i++) + { + if (!isascii(header_buffer[i])) + is_ascii =false; + } + return(is_ascii|| bspe?true:false); +} + +/**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ +int LoadSPE::fileCheck(const std::string& filePath) +{ + std::ifstream file(filePath.c_str()); + if (!file) + { + g_log.error("Unable to open file: " + filePath); + throw Exception::FileError("Unable to open file: " , filePath); + } + int bret=0; + std::string fileline; + //read first line + getline(file,fileline); + boost::char_separator<char> sep(" "); + int ncols=0; + typedef boost::tokenizer<boost::char_separator<char> > tokenizer; + tokenizer tok(fileline,sep); + for (tokenizer::iterator beg=tok.begin();beg!=tok.end();++beg) + { + ++ncols; + } + if(ncols==2) + { + bret+=40; + } + // Next line should be comment line: "### Phi Grid" or "### Q Grid" + std::string commentline; + getline(file,commentline); + if(commentline.find("Phi Grid")!=std::string::npos|| commentline.find("Q Grid")!=std::string::npos ) + { + bret+=40; + } + + return bret; +} + + } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/DataHandling/src/LoadSpec.cpp b/Code/Mantid/DataHandling/src/LoadSpec.cpp index 51652ce1835..0cd037608b9 100644 --- a/Code/Mantid/DataHandling/src/LoadSpec.cpp +++ b/Code/Mantid/DataHandling/src/LoadSpec.cpp @@ -5,7 +5,6 @@ #include "MantidDataObjects/Workspace2D.h" #include "MantidKernel/UnitFactory.h" #include "MantidAPI/FileProperty.h" - #include <fstream> #include <cstring> #include <boost/tokenizer.hpp> @@ -16,17 +15,18 @@ namespace Mantid { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(LoadSpec) + using namespace Kernel; using namespace API; - + /// Empty constructor - LoadSpec::LoadSpec() : Algorithm() {} + LoadSpec::LoadSpec() {} /// Initialisation method. void LoadSpec::init() { - std::vector<std::string> exts; + std::vector<std::string> exts; exts.push_back(".dat"); exts.push_back(".txt"); @@ -172,5 +172,6 @@ namespace Mantid } + } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/DataHandling/src/LoadSpice2D.cpp b/Code/Mantid/DataHandling/src/LoadSpice2D.cpp index 06df19a847e..b2ee7ea39dc 100644 --- a/Code/Mantid/DataHandling/src/LoadSpice2D.cpp +++ b/Code/Mantid/DataHandling/src/LoadSpice2D.cpp @@ -17,6 +17,7 @@ #include "Poco/DOM/Node.h" #include "Poco/DOM/Text.h" #include "MantidAPI/SpectraDetectorMap.h" +#include "MantidAPI/LoadAlgorithmFactory.h" #include <boost/shared_array.hpp> #include <iostream> //----------------------------------------------------------------------- @@ -87,6 +88,9 @@ namespace Mantid // Register the algorithm into the AlgorithmFactory DECLARE_ALGORITHM(LoadSpice2D) + //register the algorithm into loadalgorithm factory + DECLARE_LOADALGORITHM(LoadSpice2D) + /// Constructor LoadSpice2D::LoadSpice2D() {} @@ -398,5 +402,55 @@ namespace Mantid throw Kernel::Exception::NotFoundError(name + " element not found in Spice XML file", fileName); } } + +/**This method does a quick file check by checking the no.of bytes read nread params and header buffer + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadSpice2D::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bspice2d(false); + (!extn.compare("xml"))?bspice2d=true:bspice2d=false; + + const char* xml_header="<?xml version="; + if ( ((unsigned)nread >= strlen(xml_header)) && + !strncmp((char*)header_buffer, xml_header, strlen(xml_header)) ) + { + } + return(bspice2d?true:false); + } + +/**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ + int LoadSpice2D::fileCheck(const std::string& filePath) + { + // Set up the DOM parser and parse xml file + DOMParser pParser; + Document* pDoc; + try + { + pDoc = pParser.parse(filePath); + } catch (...) + { + throw Kernel::Exception::FileError("Unable to parse File:", filePath); + } + // Get pointer to root element + Element* pRootElem = pDoc->documentElement(); + if(pRootElem) + { + if(!pRootElem->tagName().compare("SPICErack")) + { + return 80; + } + } + + return 0; + + } } } diff --git a/Code/Mantid/DataHandling/test/LoadTest.h b/Code/Mantid/DataHandling/test/LoadTest.h index 6947ff33e1b..13e900702a0 100644 --- a/Code/Mantid/DataHandling/test/LoadTest.h +++ b/Code/Mantid/DataHandling/test/LoadTest.h @@ -86,6 +86,17 @@ public: AnalysisDataService::Instance().remove("LoadTest_Output_1"); AnalysisDataService::Instance().remove("LoadTest_Output_2"); } + void t1estISISNexus() + { + Load loader; + loader.initialize(); + loader.setPropertyValue("Filename",m_baseDir+"AutoTestData/LOQ49886.nxs"); + loader.setPropertyValue("OutputWorkspace","LoadTest_Output"); + TS_ASSERT_THROWS_NOTHING(loader.execute()); + MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("LoadTest_Output")); + TS_ASSERT(ws); + AnalysisDataService::Instance().remove("LoadTest_Output"); + } void testUnknownExt() { @@ -132,7 +143,41 @@ public: TS_ASSERT(ws); AnalysisDataService::Instance().remove("LoadTest_Output"); } + void testSNSSpec() + { + Load loader; + loader.initialize(); + loader.setPropertyValue("Filename",m_baseDir+"AutoTestData/LoadSNSspec.txt"); + loader.setPropertyValue("OutputWorkspace","LoadTest_Output"); + TS_ASSERT_THROWS_NOTHING(loader.execute()); + MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("LoadTest_Output")); + TS_ASSERT(ws); + AnalysisDataService::Instance().remove("LoadTest_Output"); + } + + void testGSS() + { + Load loader; + loader.initialize(); + loader.setPropertyValue("Filename",m_baseDir+"AutoTestData/gss.txt"); + loader.setPropertyValue("OutputWorkspace","LoadTest_Output"); + TS_ASSERT_THROWS_NOTHING(loader.execute()); + MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("LoadTest_Output")); + TS_ASSERT(ws); + AnalysisDataService::Instance().remove("LoadTest_Output"); + } + void testRKH() + { + Load loader; + loader.initialize(); + loader.setPropertyValue("Filename",m_baseDir+"AutoTestData/DIRECT.041"); + loader.setPropertyValue("OutputWorkspace","LoadTest_Output"); + TS_ASSERT_THROWS_NOTHING(loader.execute()); + MatrixWorkspace_sptr ws = boost::dynamic_pointer_cast<MatrixWorkspace>(AnalysisDataService::Instance().retrieve("LoadTest_Output")); + TS_ASSERT(ws); + AnalysisDataService::Instance().remove("LoadTest_Output"); + } private: std::string m_baseDir; }; diff --git a/Code/Mantid/Nexus/Nexus.vcxproj b/Code/Mantid/Nexus/Nexus.vcxproj index fdcfc19744b..f7288be50f1 100644 --- a/Code/Mantid/Nexus/Nexus.vcxproj +++ b/Code/Mantid/Nexus/Nexus.vcxproj @@ -89,7 +89,7 @@ <DisableSpecificWarnings>4275;%(DisableSpecificWarnings)</DisableSpecificWarnings> </ClCompile> <Link> - <AdditionalDependencies>$(SolutionName)kernel.lib;$(SolutionName)DataObjects.lib;libnexus-0.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalDependencies>$(SolutionName)kernel.lib;$(SolutionName)DataObjects.lib;libnexus-0.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies> <OutputFile>$(OutDir)$(SolutionName)$(ProjectName).dll</OutputFile> <AdditionalLibraryDirectories>../debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <GenerateDebugInformation>true</GenerateDebugInformation> @@ -126,7 +126,7 @@ <DisableSpecificWarnings>4275;%(DisableSpecificWarnings)</DisableSpecificWarnings> </ClCompile> <Link> - <AdditionalDependencies>$(SolutionName)kernel.lib;$(SolutionName)DataObjects.lib;libnexus-0.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalDependencies>$(SolutionName)kernel.lib;$(SolutionName)DataObjects.lib;libnexus-0.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies> <OutputFile>$(OutDir)$(SolutionName)$(ProjectName).dll</OutputFile> <AdditionalLibraryDirectories>../release;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <OptimizeReferences>true</OptimizeReferences> diff --git a/Code/Mantid/Nexus/inc/MantidNexus/LoadISISNexus2.h b/Code/Mantid/Nexus/inc/MantidNexus/LoadISISNexus2.h index 245dfb42fb0..10840468361 100644 --- a/Code/Mantid/Nexus/inc/MantidNexus/LoadISISNexus2.h +++ b/Code/Mantid/Nexus/inc/MantidNexus/LoadISISNexus2.h @@ -7,8 +7,10 @@ #include "MantidAPI/Algorithm.h" #include "MantidDataObjects/Workspace2D.h" #include "MantidNexus/NexusClasses.h" +#include "MantidAPI/IDataFileChecker.h" #include <climits> + //---------------------------------------------------------------------- // Forward declaration //---------------------------------------------------------------------- @@ -59,7 +61,7 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadISISNexus2 : public API::Algorithm + class DLLExport LoadISISNexus2 : public API::IDataFileChecker { public: /// Default constructor @@ -73,6 +75,11 @@ namespace Mantid /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling"; } + + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); private: /// Overwrites Algorithm method. void init(); diff --git a/Code/Mantid/Nexus/inc/MantidNexus/LoadMuonNexus.h b/Code/Mantid/Nexus/inc/MantidNexus/LoadMuonNexus.h index b2481239f1e..f585ecf8245 100644 --- a/Code/Mantid/Nexus/inc/MantidNexus/LoadMuonNexus.h +++ b/Code/Mantid/Nexus/inc/MantidNexus/LoadMuonNexus.h @@ -6,7 +6,7 @@ //---------------------------------------------------------------------- #include "MantidAPI/Algorithm.h" #include "MantidDataObjects/Workspace2D.h" - +#include "MantidAPI/IDataFileChecker.h" //---------------------------------------------------------------------- // Forward declaration //---------------------------------------------------------------------- @@ -59,7 +59,7 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadMuonNexus : public API::Algorithm + class DLLExport LoadMuonNexus : public API::IDataFileChecker { public: /// Default constructor @@ -73,6 +73,10 @@ namespace Mantid /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling"; } + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); protected: /// Overwrites Algorithm method void exec(); diff --git a/Code/Mantid/Nexus/inc/MantidNexus/LoadMuonNexus2.h b/Code/Mantid/Nexus/inc/MantidNexus/LoadMuonNexus2.h index 0230cb06c5d..c1e11d75250 100644 --- a/Code/Mantid/Nexus/inc/MantidNexus/LoadMuonNexus2.h +++ b/Code/Mantid/Nexus/inc/MantidNexus/LoadMuonNexus2.h @@ -5,6 +5,7 @@ // Includes //---------------------------------------------------------------------- #include "MantidNexus/LoadMuonNexus.h" +#include "MantidAPI/IDataFileChecker.h" namespace Mantid { @@ -72,6 +73,10 @@ namespace Mantid /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling"; } + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); private: /// Overwrites Algorithm method void exec(); diff --git a/Code/Mantid/Nexus/inc/MantidNexus/LoadNexusProcessed.h b/Code/Mantid/Nexus/inc/MantidNexus/LoadNexusProcessed.h index 4cb6f529902..1b598494c04 100644 --- a/Code/Mantid/Nexus/inc/MantidNexus/LoadNexusProcessed.h +++ b/Code/Mantid/Nexus/inc/MantidNexus/LoadNexusProcessed.h @@ -8,6 +8,7 @@ #include "MantidDataObjects/Workspace2D.h" #include "MantidNexus/NexusFileIO.h" #include "MantidNexus/NexusClasses.h" +#include "MantidAPI/IDataFileChecker.h" namespace Mantid { @@ -47,7 +48,7 @@ namespace Mantid File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadNexusProcessed : public API::Algorithm + class DLLExport LoadNexusProcessed : public API::IDataFileChecker { public: @@ -62,6 +63,11 @@ namespace Mantid /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling";} + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); + private: /// specifies the order that algorithm data is listed in workspaces' histories enum AlgorithmHist diff --git a/Code/Mantid/Nexus/inc/MantidNexus/LoadSNSNexus.h b/Code/Mantid/Nexus/inc/MantidNexus/LoadSNSNexus.h index 03351c87402..7f8e99bca26 100644 --- a/Code/Mantid/Nexus/inc/MantidNexus/LoadSNSNexus.h +++ b/Code/Mantid/Nexus/inc/MantidNexus/LoadSNSNexus.h @@ -7,11 +7,13 @@ #include "MantidAPI/Algorithm.h" #include "MantidDataObjects/Workspace2D.h" #include "MantidNexus/NexusClasses.h" - +#include "MantidAPI/IDataFileChecker.h" #include <napi.h> #include <climits> #include <boost/shared_array.hpp> + + //---------------------------------------------------------------------- // Forward declaration //---------------------------------------------------------------------- @@ -66,7 +68,7 @@ namespace NeXus File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. Code Documentation is available at: <http://doxygen.mantidproject.org> */ - class DLLExport LoadSNSNexus : public API::Algorithm + class DLLExport LoadSNSNexus : public API::IDataFileChecker { public: /// Default constructor @@ -79,6 +81,10 @@ namespace NeXus virtual int version() const { return 1; } /// Algorithm's category for identification overriding a virtual method virtual const std::string category() const { return "DataHandling"; } + /// do a quick check that this file can be loaded + virtual bool quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + virtual int fileCheck(const std::string& filePath); private: /// Overwrites Algorithm method. diff --git a/Code/Mantid/Nexus/src/LoadISISNexus2.cpp b/Code/Mantid/Nexus/src/LoadISISNexus2.cpp index 62a048122e2..fadeb93d75f 100644 --- a/Code/Mantid/Nexus/src/LoadISISNexus2.cpp +++ b/Code/Mantid/Nexus/src/LoadISISNexus2.cpp @@ -11,7 +11,8 @@ #include "MantidKernel/LogParser.h" #include "MantidGeometry/Instrument/XMLlogfile.h" #include "MantidGeometry/Instrument/Detector.h" - +#include "MantidNexus/NexusFileIO.h" +#include "MantidAPI/LoadAlgorithmFactory.h" #include <Poco/Path.h> #include <Poco/DateTimeFormatter.h> #include <Poco/DateTimeParser.h> @@ -24,19 +25,25 @@ #include <functional> #include <algorithm> +#ifdef _WIN32 +# include <winsock.h> +#else +# include <netinet/in.h> +#endif namespace Mantid { namespace NeXus { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(LoadISISNexus2) - + DECLARE_LOADALGORITHM(LoadISISNexus2) + using namespace Kernel; using namespace API; /// Empty default constructor LoadISISNexus2::LoadISISNexus2() : - Algorithm(), m_filename(), m_instrument_name(), m_samplename(), m_numberOfSpectra(0), m_numberOfSpectraInFile(0), + m_filename(), m_instrument_name(), m_samplename(), m_numberOfSpectra(0), m_numberOfSpectraInFile(0), m_numberOfPeriods(0), m_numberOfPeriodsInFile(0), m_numberOfChannels(0), m_numberOfChannelsInFile(0), m_have_detector(false), m_spec_min(0), m_spec_max(EMPTY_INT()), m_spec_list(), m_entrynumber(0), m_range_supplied(true), m_tof_data(), m_proton_charge(0.), @@ -750,5 +757,56 @@ namespace Mantid return sqrt(in); } + /**This method does a quick file type check by looking at the first 100 bytes of the file + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadISISNexus2::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bnexs(false); + (!extn.compare("nxs")||!extn.compare(".nx5"))?bnexs=true:bnexs=false; + /* + * HDF files have magic cookie 0x0e031301 in the first 4 bytes + */ + if ( (nread >= sizeof(unsigned)) && (ntohl(header_buffer_union.u) == 0x0e031301)||bnexs ) + { + //hdf + return true; + } + else if ( (nread >= sizeof(hdf5_signature)) && (!memcmp(header_buffer, hdf5_signature, sizeof(hdf5_signature))) ) + { + //hdf5 + return true; + } + return false; + } + /**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ + int LoadISISNexus2::fileCheck(const std::string& filePath) + { + std::vector<std::string> entryName,definition; + int count= getNexusEntryTypes(filePath,entryName,definition); + if(count<=-1) + { + g_log.error("Error reading file " + filePath); + throw Exception::FileError("Unable to read data in File:" , filePath); + } + else if(count==0) + { + g_log.error("Error no entries found in " + filePath); + throw Exception::FileError("Error no entries found in " , filePath); + } + int ret=0; + if( entryName[0]=="raw_data_1" ) + { + ret=80; + } + return ret; + } } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/Nexus/src/LoadMuonNexus.cpp b/Code/Mantid/Nexus/src/LoadMuonNexus.cpp index 082b219420b..5be372e7fb1 100644 --- a/Code/Mantid/Nexus/src/LoadMuonNexus.cpp +++ b/Code/Mantid/Nexus/src/LoadMuonNexus.cpp @@ -18,7 +18,13 @@ #include <boost/shared_ptr.hpp> #include "MantidNexus/MuonNexusReader.h" #include "MantidNexus/NexusClasses.h" +#include "MantidNexus/NexusFileIO.h" +#ifdef _WIN32 +# include <winsock.h> +#else +# include <netinet/in.h> +#endif namespace Mantid { namespace NeXus @@ -32,8 +38,7 @@ namespace Mantid /// Empty default constructor LoadMuonNexus::LoadMuonNexus() : - Algorithm(), - m_filename(), m_entrynumber(0), m_numberOfSpectra(0), m_numberOfPeriods(0), m_list(false), + m_filename(), m_entrynumber(0), m_numberOfSpectra(0), m_numberOfPeriods(0), m_list(false), m_interval(false), m_spec_list(), m_spec_min(0), m_spec_max(EMPTY_INT()) {} @@ -599,5 +604,56 @@ namespace Mantid } + /**This method does a quick file type check by looking at the first 100 bytes of the file + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadMuonNexus::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bnexs(false); + (!extn.compare("nxs")||!extn.compare(".nx5"))?bnexs=true:bnexs=false; + /* + * HDF files have magic cookie 0x0e031301 in the first 4 bytes + */ + if ( (nread >= sizeof(unsigned)) && (ntohl(header_buffer_union.u) == 0x0e031301)||bnexs ) + { + //hdf + return true; + } + else if ( (nread >= sizeof(hdf5_signature)) && (!memcmp(header_buffer, hdf5_signature, sizeof(hdf5_signature))) ) + { + //hdf5 + return true; + } + return false; + + } + /**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ + int LoadMuonNexus::fileCheck(const std::string& filePath) + { + int ret=0; + std::vector<std::string> entryName,definition; + int count= getNexusEntryTypes(filePath,entryName,definition); + if(count<=-1) + { + ret =0; + } + else if(count==0) + { + ret=0; + } + if( definition[0]=="muonTD") + { + ret=80; + } + return ret; + } + } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/Nexus/src/LoadMuonNexus2.cpp b/Code/Mantid/Nexus/src/LoadMuonNexus2.cpp index 6bb93054015..c2ecc493241 100644 --- a/Code/Mantid/Nexus/src/LoadMuonNexus2.cpp +++ b/Code/Mantid/Nexus/src/LoadMuonNexus2.cpp @@ -12,14 +12,20 @@ #include "MantidAPI/Progress.h" #include "MantidAPI/SpectraDetectorMap.h" #include "MantidNexus/NexusClasses.h" - +#include "MantidNexus/NexusFileIO.h" #include "Poco/Path.h" +#include "MantidAPI/LoadAlgorithmFactory.h" #include <boost/lexical_cast.hpp> #include <boost/shared_ptr.hpp> #include <cmath> #include <numeric> +#ifdef _WIN32 +# include <winsock.h> +#else +# include <netinet/in.h> +#endif namespace Mantid { @@ -27,7 +33,7 @@ namespace Mantid { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(LoadMuonNexus2) - + DECLARE_LOADALGORITHM(LoadMuonNexus2) using namespace Kernel; using namespace API; using Geometry::IInstrument; @@ -299,5 +305,61 @@ namespace Mantid ws->populateInstrumentParameters(); } + /**This method does a quick file type check by looking at the first 100 bytes of the file + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadMuonNexus2::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bnexs(false); + (!extn.compare("nxs")||!extn.compare(".nx5"))?bnexs=true:bnexs=false; + /* + * HDF files have magic cookie 0x0e031301 in the first 4 bytes + */ + if ( (nread >= sizeof(unsigned)) && (ntohl(header_buffer_union.u) == 0x0e031301)||bnexs ) + { + //hdf + return true; + } + else if ( (nread >= sizeof(hdf5_signature)) && (!memcmp(header_buffer, hdf5_signature, sizeof(hdf5_signature))) ) + { + //hdf5 + return true; + } + return false; + + } + /**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ + int LoadMuonNexus2::fileCheck(const std::string& filePath) + { + int ret=0; + std::vector<std::string> entryName,definition; + int count= getNexusEntryTypes(filePath,entryName,definition); + if(count<=-1) + { + ret =0; + } + else if(count==0) + { + ret=0; + } + + if( definition[0]=="muonTD") + { + ret=50; + } + else if (definition[0]=="pulsedTD") + { + ret=80; + } + return ret; + } + } // namespace DataHandling } // namespace Mantid diff --git a/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp b/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp index 15f8da66078..aa716d84dbf 100644 --- a/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp +++ b/Code/Mantid/Nexus/src/LoadNexusProcessed.cpp @@ -17,9 +17,16 @@ #include "Poco/Path.h" #include "Poco/DateTimeParser.h" #include "Poco/StringTokenizer.h" +#include "MantidAPI/LoadAlgorithmFactory.h" #include <cmath> #include <boost/shared_ptr.hpp> +#ifdef _WIN32 +# include <winsock.h> +#else +# include <netinet/in.h> +#endif + namespace Mantid { namespace NeXus @@ -27,13 +34,14 @@ namespace NeXus // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(LoadNexusProcessed) +DECLARE_LOADALGORITHM(LoadNexusProcessed) using namespace Kernel; using namespace API; using Geometry::IInstrument_sptr; /// Default constructor -LoadNexusProcessed::LoadNexusProcessed() : Algorithm(), m_shared_bins(false), m_xbins(), +LoadNexusProcessed::LoadNexusProcessed() : m_shared_bins(false), m_xbins(), m_axis1vals(), m_list(false), m_interval(false), m_spec_list(), m_spec_min(0), m_spec_max(Mantid::EMPTY_INT()) { @@ -1212,7 +1220,58 @@ int LoadNexusProcessed::calculateWorkspacesize(const int numberofspectra) return total_specs; } - + /**This method does a quick file type check by looking at the first 100 bytes of the file + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadNexusProcessed::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bnexs(false); + (!extn.compare("nxs")||!extn.compare(".nx5"))?bnexs=true:bnexs=false; + /* + * HDF files have magic cookie 0x0e031301 in the first 4 bytes + */ + if ( (nread >= sizeof(unsigned)) && (ntohl(header_buffer_union.u) == 0x0e031301)||bnexs ) + { + //hdf + return true; + } + else if ( (nread >= sizeof(hdf5_signature)) && (!memcmp(header_buffer, hdf5_signature, sizeof(hdf5_signature))) ) + { + //hdf5 + return true; + } + return false; + + } + /**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ + int LoadNexusProcessed::fileCheck(const std::string& filePath) + { + std::vector<std::string> entryName,definition; + int count= getNexusEntryTypes(filePath,entryName,definition); + if(count<=-1) + { + g_log.error("Error reading file " + filePath); + throw Exception::FileError("Unable to read data in File:" , filePath); + } + else if(count==0) + { + g_log.error("Error no entries found in " + filePath); + throw Exception::FileError("Error no entries found in " , filePath); + } + int ret=0; + if( entryName[0]=="mantid_workspace_1" ) + { + ret=80; + } + return ret; + } } // namespace NeXus } // namespace Mantid diff --git a/Code/Mantid/Nexus/src/LoadSNSNexus.cpp b/Code/Mantid/Nexus/src/LoadSNSNexus.cpp index 5a6c8ce5784..419129e4698 100644 --- a/Code/Mantid/Nexus/src/LoadSNSNexus.cpp +++ b/Code/Mantid/Nexus/src/LoadSNSNexus.cpp @@ -14,19 +14,25 @@ #include "MantidKernel/LogParser.h" #include "MantidAPI/WorkspaceGroup.h" #include "MantidKernel/PhysicalConstants.h" - +#include "MantidAPI/LoadAlgorithmFactory.h" #include <cmath> #include <iostream> #include <sstream> #include <fstream> #include <limits> +#ifdef _WIN32 +# include <winsock.h> +#else +# include <netinet/in.h> +#endif namespace Mantid { namespace NeXus { // Register the algorithm into the algorithm factory DECLARE_ALGORITHM(LoadSNSNexus) +DECLARE_LOADALGORITHM(LoadSNSNexus) using namespace Kernel; using namespace API; @@ -304,5 +310,56 @@ double LoadSNSNexus::dblSqrt(double in) { return sqrt(in); } + + +/**This method does a quick file type check by looking at the first 100 bytes of the file + * @param filePath- path of the file including name. + * @param nread - no.of bytes read + * @param header_buffer - buffer containing the 1st 100 bytes of the file + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadSNSNexus::quickFileCheck(const std::string& filePath,int nread,unsigned char* header_buffer) + { + std::string extn=extension(filePath); + bool bnexs(false); + (!extn.compare("nxs")||!extn.compare(".nx5"))?bnexs=true:bnexs=false; + /* + * HDF files have magic cookie 0x0e031301 in the first 4 bytes + */ + if ( (nread >= sizeof(unsigned)) && (ntohl(header_buffer_union.u) == 0x0e031301)||bnexs ) + { + //hdf + return true; + } + else if ( (nread >= sizeof(hdf5_signature)) && (!memcmp(header_buffer, hdf5_signature, sizeof(hdf5_signature))) ) + { + //hdf5 + return true; + } + return false; + + } + /**checks the file by opening it and reading few lines + * @param filePath name of the file inluding its path + * @return an integer value how much this algorithm can load the file + */ + int LoadSNSNexus::fileCheck(const std::string& filePath) + { + NXRoot root(filePath); + NXEntry entry = root.openEntry(root.groups().front().nxname); + int bret=0; + try + { + NXChar nxc = entry.openNXChar("instrument/SNSdetector_calibration_id"); + bret= 80; + } + catch(...) + { + bret=0; + } + return bret; + } + + } // namespace DataHandling } // namespace Mantid -- GitLab