diff --git a/Code/Mantid/Framework/DataHandling/src/Load.cpp b/Code/Mantid/Framework/DataHandling/src/Load.cpp index 5f5b9a685284dff845015081c16503de968f330e..66635364100beb3ffe047426237094afc0bccb30 100644 --- a/Code/Mantid/Framework/DataHandling/src/Load.cpp +++ b/Code/Mantid/Framework/DataHandling/src/Load.cpp @@ -12,6 +12,7 @@ #include <cctype> #include <algorithm> +#include "MantidAPI/IMDEventWorkspace.h" namespace Mantid { @@ -124,17 +125,27 @@ namespace Mantid ++citr ) { IDataFileChecker_sptr loader = API::LoadAlgorithmFactory::Instance().create(*citr); - if( loader->quickFileCheck(filePath, nread, header) ) + try { - int pref = loader->fileCheck(filePath); - // Can't just pick the first as there might be one later in the list with a higher - // preference - if( pref > highestPref ) + if( loader->quickFileCheck(filePath, nread, header) ) { - highestPref = pref; - winningLoader = loader; + int pref = loader->fileCheck(filePath); + // Can't just pick the first as there might be one later in the list with a higher + // preference + if( pref > highestPref ) + { + highestPref = pref; + winningLoader = loader; + } } } + catch (std::exception & e) + { + g_log.debug() << "Error running file check of " << loader->name() << std::endl; + g_log.debug() << e.what() << std::endl; + + } + } if( !winningLoader ) @@ -144,6 +155,7 @@ namespace Mantid throw std::runtime_error("Cannot find an algorithm that is able to load \"" + filePath + "\".\n" "Check that the file is a supported type."); } + g_log.debug() << "Winning loader is " << winningLoader->name() << std::endl; setPropertyValue("LoaderName", winningLoader->name()); winningLoader->initialize(); setUpLoader(winningLoader); @@ -361,8 +373,8 @@ namespace Mantid return loader->getProperty(propName); } catch(std::runtime_error&) - { - } + { } + // Try a MatrixWorkspace try { @@ -370,8 +382,8 @@ namespace Mantid return childWS; } catch(std::runtime_error&) - { - } + { } + // EventWorkspace try { @@ -379,8 +391,27 @@ namespace Mantid return childWS; } catch(std::runtime_error&) + { } + + // IMDEventWorkspace + try { + IMDEventWorkspace_sptr childWS = loader->getProperty(propName); + return childWS; } + catch(std::runtime_error&) + { } + + // Just workspace? + try + { + Workspace_sptr childWS = loader->getProperty(propName); + return childWS; + } + catch(std::runtime_error&) + { } + + g_log.debug() << "Workspace property " << propName << " did not return to MatrixWorkspace, EventWorkspace, or IMDEventWorkspace." << std::endl; return Workspace_sptr(); } diff --git a/Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp b/Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp index db0495a8168bfbdaadb5d2a5a0f7d64591c07be0..34884c194b7b6abe1aa04a37ea660f8f8cc2c748 100644 --- a/Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp +++ b/Code/Mantid/Framework/DataHandling/src/LoadNexusProcessed.cpp @@ -8,20 +8,20 @@ #include "MantidAPI/SpectraDetectorMap.h" #include "MantidAPI/TextAxis.h" #include "MantidAPI/WorkspaceGroup.h" +#include "MantidDataHandling/LoadNexusProcessed.h" #include "MantidDataObjects/EventWorkspace.h" #include "MantidKernel/ArrayProperty.h" #include "MantidKernel/ConfigService.h" #include "MantidKernel/DateAndTime.h" #include "MantidKernel/UnitFactory.h" -#include "MantidDataHandling/LoadNexusProcessed.h" #include "MantidNexus/NexusClasses.h" #include "MantidNexus/NexusFileIO.h" +#include "MantidNexusCPP/NeXusFile.hpp" #include <boost/shared_ptr.hpp> #include <cmath> #include <Poco/DateTimeParser.h> #include <Poco/Path.h> #include <Poco/StringTokenizer.h> -#include "MantidNexusCPP/NeXusFile.hpp" namespace Mantid { diff --git a/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/LoadMD.h b/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/LoadMD.h index db0418401a53123119296f18205b9e0f071ac20e..377c8e7acbd20f99ab08d7add1cce6f28c75c8f9 100644 --- a/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/LoadMD.h +++ b/Code/Mantid/Framework/MDEvents/inc/MantidMDEvents/LoadMD.h @@ -10,6 +10,7 @@ Load a .nxs file into a MDEventWorkspace. #include "MantidKernel/System.h" #include "MantidMDEvents/MDEventWorkspace.h" #include "MantidNexusCPP/NeXusFile.hpp" +#include "MantidAPI/IDataFileChecker.h" namespace Mantid { @@ -41,7 +42,7 @@ namespace MDEvents 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 LoadMD : public API::Algorithm + class DLLExport LoadMD : public API::IDataFileChecker { public: LoadMD(); @@ -53,7 +54,12 @@ namespace MDEvents virtual int version() const { return 1;}; /// Algorithm's category for identification virtual const std::string category() const { return "MDEvents";} - + + /// do a quick check that this file can be loaded + bool quickFileCheck(const std::string& filePath,size_t nread,const file_header& header); + /// check the structure of the file and return a value between 0 and 100 of how much this file can be loaded + int fileCheck(const std::string& filePath); + private: /// Sets documentation strings for this algorithm virtual void initDocs(); diff --git a/Code/Mantid/Framework/MDEvents/src/LoadMD.cpp b/Code/Mantid/Framework/MDEvents/src/LoadMD.cpp index 86ba208bc7d39bdfea398219741b0b1f5aa5e955..2f6d8fc655c2d29f5155196c020b709278099f9b 100644 --- a/Code/Mantid/Framework/MDEvents/src/LoadMD.cpp +++ b/Code/Mantid/Framework/MDEvents/src/LoadMD.cpp @@ -1,6 +1,7 @@ #include "MantidAPI/ExperimentInfo.h" #include "MantidAPI/FileProperty.h" #include "MantidAPI/IMDEventWorkspace.h" +#include "MantidAPI/LoadAlgorithmFactory.h" #include "MantidGeometry/MDGeometry/IMDDimension.h" #include "MantidGeometry/MDGeometry/IMDDimensionFactory.h" #include "MantidGeometry/MDGeometry/MDDimensionExtents.h" @@ -11,6 +12,7 @@ #include "MantidKernel/System.h" #include "MantidMDEvents/LoadMD.h" #include "MantidMDEvents/MDEventFactory.h" +#include "MantidNexusCPP/NeXusException.hpp" #include <boost/algorithm/string.hpp> #include <vector> @@ -24,7 +26,8 @@ namespace Mantid { // Register the algorithm into the AlgorithmFactory - DECLARE_ALGORITHM(LoadMD) + DECLARE_ALGORITHM(LoadMD); + DECLARE_LOADALGORITHM(LoadMD); //---------------------------------------------------------------------------------------------- @@ -80,6 +83,58 @@ namespace Mantid } + + //------------------------------------------------------------------------------------------------- + /** Do 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 :: The first 100 bytes of the file as a union + * @return true if the given file is of type which can be loaded by this algorithm + */ + bool LoadMD::quickFileCheck(const std::string& filePath,size_t nread, const file_header& header) + { + std::string ext = this->extension(filePath); + // If the extension is nxs then give it a go + if( ext.compare("nxs") == 0 ) return true; + + // If not then let's see if it is a HDF file by checking for the magic cookie + if ( nread >= sizeof(int32_t) && (ntohl(header.four_bytes) == g_hdf_cookie) ) 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 LoadMD::fileCheck(const std::string& filePath) + { + int confidence(0); + typedef std::map<std::string,std::string> string_map_t; + try + { + string_map_t::const_iterator it; + ::NeXus::File file = ::NeXus::File(filePath); + string_map_t entries = file.getEntries(); + for(string_map_t::const_iterator it = entries.begin(); it != entries.end(); ++it) + { + if ( (it->first == "MDEventWorkspace") && (it->second == "NXentry") ) + confidence = 95; + + } + file.close(); + } + catch(::NeXus::Exception&) + { + } + std::cout << "LoadMD::fileCheck() says " << confidence << std::endl; + return confidence; + } + + + + + /** Load the ExperimentInfo blocks, if any, in the NXS file * * @param ws :: MDEventWorkspace to load