Skip to content

Commit

Permalink
First lot of updates for new loader check style. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jun 29, 2013
1 parent 4293cc3 commit b4ba323
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/IHDFFileLoader.h"
#include "MantidDataObjects/EventWorkspace.h"
#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
Expand Down Expand Up @@ -68,7 +68,7 @@ namespace Mantid
File change history is stored at: <https://github.com/mantidproject/mantid>
*/
class DLLExport LoadEventNexus : public API::IDataFileChecker
class DLLExport LoadEventNexus : public API::IHDFFileLoader
{
public:
/// Sets documentation strings for this algorithm
Expand All @@ -80,10 +80,14 @@ namespace Mantid
virtual int version() const { return 1;};
virtual const std::string category() const { return "DataHandling\\Nexus";}

/// 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);
// /// 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);

/// Returns a confidence value that this algorithm can load a file
int confidence(const Kernel::HDFDescriptor & descriptor) const;


/** Sets whether the pixel counts will be pre-counted.
* @param value :: true if you want to precount. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/IHDFFileLoader.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataHandling/ISISRunLogs.h"
#include "MantidNexus/NexusClasses.h"
Expand Down Expand Up @@ -67,7 +67,7 @@ namespace Mantid
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport LoadISISNexus2 : public API::IDataFileChecker
class DLLExport LoadISISNexus2 : public API::IHDFFileLoader
{
public:
/// Default constructor
Expand All @@ -81,11 +81,9 @@ namespace Mantid
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "DataHandling\\Nexus"; }

/// Returns a confidence value that this algorithm can load a file
virtual int confidence(const Kernel::HDFDescriptor & descriptor) const;

/// do a quick check that this file can be loaded
virtual 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
virtual int fileCheck(const std::string& filePath);
/// Spectra block descriptor
struct SpectraBlock
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/IHDFFileLoader.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidDataObjects/Workspace2D.h"
#include <hdf5.h>
Expand Down Expand Up @@ -53,11 +53,11 @@ namespace Mantid
};
*/

class DLLExport LoadSassena : public API::IDataFileChecker
class DLLExport LoadSassena : public API::IHDFFileLoader
{
public:
/// Constructor
LoadSassena(): IDataFileChecker(), m_filename("") {};
LoadSassena(): API::IHDFFileLoader(), m_filename("") {};
/// Virtual Destructor
virtual ~LoadSassena() {}
/// Algorithm's name
Expand All @@ -66,23 +66,9 @@ namespace Mantid
virtual int version() const { return 1; }
/// Algorithm's category for identification
virtual const std::string category() const { return "DataHandling\\Sassena"; }
/**
* Do a quick check that this file can be loaded
*
* @param filePath the location of and the file to check
* @param nread number of bytes to read
* @param header the first 100 bytes of the file as a union
* @return true if the file can be loaded, otherwise false
*/
virtual 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
*
* @param filePath the location of and the file to check
* @return a confidence level indicator between 0 and 100
*/
int fileCheck(const std::string& filePath);

/// Returns a confidence value that this algorithm can load a file
virtual int confidence(const Kernel::HDFDescriptor & descriptor) const;

protected:
/// Add a workspace to the group and register in the analysis data service
Expand Down
122 changes: 68 additions & 54 deletions Code/Mantid/Framework/DataHandling/src/LoadEventNexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ Veto pulses can be filtered out in a separate step using [[FilterByLogValue]]:
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/MemoryManager.h"
#include "MantidAPI/LoadAlgorithmFactory.h" // For the DECLARE_LOADALGORITHM macro
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/SpectrumDetectorMapping.h"


using std::endl;
using std::map;
using std::string;
Expand All @@ -56,8 +57,7 @@ namespace Mantid
namespace DataHandling
{

DECLARE_ALGORITHM(LoadEventNexus)
DECLARE_LOADALGORITHM(LoadEventNexus)
DECLARE_HDF_FILELOADER_ALGORITHM(LoadEventNexus);

using namespace Kernel;
using namespace Geometry;
Expand Down Expand Up @@ -896,7 +896,7 @@ class LoadBankFromDiskTask : public Task
//===============================================================================================

/// Empty default constructor
LoadEventNexus::LoadEventNexus() : IDataFileChecker(),
LoadEventNexus::LoadEventNexus() : IHDFFileLoader(),
event_id_is_spec(false), m_allBanksPulseTimes(NULL)
{
}
Expand All @@ -909,62 +909,76 @@ LoadEventNexus::~LoadEventNexus()
delete m_allBanksPulseTimes;
}

/**
* 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 LoadEventNexus::quickFileCheck(const std::string& filePath,size_t nread, const file_header& header)
{
std::string ext = this->extension(filePath);
g_log.debug() << "LoadEventNexus::quickFileCheck() - File extension is: " << ext << std::endl;

// If the extension is nxs then give it a go
if( ext.compare("nxs") == 0 ) return true;
// If the extension is h5 then give it a go
if( ext.compare("h5") == 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 LoadEventNexus::fileCheck(const std::string& filePath)
///**
// * 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 LoadEventNexus::quickFileCheck(const std::string& filePath,size_t nread, const file_header& header)
//{
// std::string ext = this->extension(filePath);
// g_log.debug() << "LoadEventNexus::quickFileCheck() - File extension is: " << ext << std::endl;
//
// // If the extension is nxs then give it a go
// if( ext.compare("nxs") == 0 ) return true;
// // If the extension is h5 then give it a go
// if( ext.compare("h5") == 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 LoadEventNexus::fileCheck(const std::string& filePath)
//{
// int confidence(0);
// typedef std::map<std::string,std::string> string_map_t;
// try
// {
// ::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 == "entry") || (it->first == "raw_data_1")) && (it->second == "NXentry") )
// {
// file.openGroup(it->first, it->second);
// string_map_t entries2 = file.getEntries();
// for(string_map_t::const_iterator it2 = entries2.begin(); it2 != entries2.end(); ++it2)
// {
// if (it2->second == "NXevent_data")
// {
// confidence = 80;
// }
// }
// file.closeGroup();
// }
// }
// }
// catch(::NeXus::Exception&)
// {
// }
// return confidence;
//}

/// Returns a confidence value that this algorithm can load a file
int LoadEventNexus::confidence(const Kernel::HDFDescriptor & descriptor) const
{
int confidence(0);
typedef std::map<std::string,std::string> string_map_t;
try
if(descriptor.classTypeExists("NXevent_data"))
{
::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(descriptor.pathOfTypeExists("/entry", "NXentry") || descriptor.pathOfTypeExists("/raw_data_1", "NXentry"))
{
if ( ((it->first == "entry") || (it->first == "raw_data_1")) && (it->second == "NXentry") )
{
file.openGroup(it->first, it->second);
string_map_t entries2 = file.getEntries();
for(string_map_t::const_iterator it2 = entries2.begin(); it2 != entries2.end(); ++it2)
{
if (it2->second == "NXevent_data")
{
confidence = 80;
}
}
file.closeGroup();
}
confidence = 80;
}
}
catch(::NeXus::Exception&)
{
}
return confidence;
}

Expand Down
66 changes: 13 additions & 53 deletions Code/Mantid/Framework/DataHandling/src/LoadISISNexus2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Loads a Nexus file created from an ISIS instrument.
#include "MantidKernel/UnitFactory.h"

#include "MantidAPI/FileProperty.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/SpectrumDetectorMapping.h"

#include "MantidGeometry/Instrument/Detector.h"
Expand All @@ -43,9 +43,7 @@ namespace Mantid
{
namespace DataHandling
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadISISNexus2)
DECLARE_LOADALGORITHM(LoadISISNexus2)
DECLARE_HDF_FILELOADER_ALGORITHM(LoadISISNexus2);

using namespace Kernel;
using namespace API;
Expand All @@ -61,6 +59,17 @@ namespace Mantid
m_spec(), m_monitors(), m_logCreator(), m_progress()
{}

/**
* Return the confidence with with this algorithm can load the file
* @param descriptor A descriptor for the file
* @returns An integer specifying the confidence level. 0 indicates it will not be used
*/
int LoadISISNexus2::confidence(const Kernel::HDFDescriptor & descriptor) const
{
if(descriptor.pathOfTypeExists("/raw_data_1","NXentry")) return 80;
return 0;
}

/// Initialisation method.
void LoadISISNexus2::init()
{
Expand Down Expand Up @@ -854,54 +863,5 @@ 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 :: 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 LoadISISNexus2::quickFileCheck(const std::string& filePath, size_t nread,const file_header& header)
{
std::string extn=extension(filePath);
bool bnexs(false);
(!extn.compare("nxs")||!extn.compare("nx5"))?bnexs=true:bnexs=false;
/*
* HDF files have magic cookie in the first 4 bytes
*/
if ( ((nread >= sizeof(unsigned)) && (ntohl(header.four_bytes) == g_hdf_cookie)) || bnexs )
{
//hdf
return true;
}
else if ( (nread >= sizeof(g_hdf5_signature)) &&
(!memcmp(header.full_hdr, g_hdf5_signature, sizeof(g_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)
{
using namespace ::NeXus;

int confidence(0);
try
{
::NeXus::File file = ::NeXus::File(filePath);
// Open the base group called 'entry'
file.openGroup("raw_data_1", "NXentry");
// If all this succeeded then we'll assume this is an ISIS NeXus file
confidence = 80;
}
catch(::NeXus::Exception&)
{
}
return confidence;
}
} // namespace DataHandling
} // namespace Mantid
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace DataHandling
{

DECLARE_ALGORITHM(LoadSNSEventNexus)
DECLARE_LOADALGORITHM(LoadSNSEventNexus)
//DECLARE_LOADALGORITHM(LoadSNSEventNexus)

LoadSNSEventNexus::LoadSNSEventNexus()
{
Expand Down

0 comments on commit b4ba323

Please sign in to comment.