Skip to content

Commit

Permalink
More NeXus loaders converted. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jun 29, 2013
1 parent ff828f6 commit e61a010
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#define MANTID_DATAHANDLING_LOADNXSPE_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/IHDFFileLoader.h"

namespace Mantid
{
Expand Down Expand Up @@ -39,7 +38,7 @@ namespace DataHandling
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport LoadNXSPE : public API::IDataFileChecker
class DLLExport LoadNXSPE : public API::IHDFFileLoader
{
public:
LoadNXSPE();
Expand All @@ -51,11 +50,9 @@ namespace DataHandling
virtual int version() const { return 1;};
/// Algorithm's category for identification
virtual const std::string category() const { return "DataHandling\\Nexus;DataHandling\\SPE;Inelastic";}
/// 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);

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

private:
/// Sets documentation strings for this algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
//---------------------------------------------------
// Includes
//---------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/IHDFFileLoader.h"
#include "MantidNexus/NexusClasses.h"

namespace Mantid {
Expand Down Expand Up @@ -41,26 +40,25 @@ namespace DataHandling {
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport LoadSINQ: public API::IDataFileChecker {
class DLLExport LoadSINQ: public API::IHDFFileLoader
{
public:
LoadSINQ();
virtual ~LoadSINQ();

virtual const std::string name() const;
virtual int version() const;
virtual const std::string category() const;
///checks the file can be loaded by reading 1st 100 bytes and looking at the file extension.
bool quickFileCheck(const std::string& filePath, size_t nread,
const file_header& header);
/// 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);

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

private:
virtual void initDocs();
void init();
void exec();
void setInstrumentName(NeXus::NXEntry& entry);
std::string getInstrumentName(NeXus::NXEntry& entry);
std::string getInstrumentName(NeXus::NXEntry& entry, std::string &nexusInstrumentName) const;
void initWorkSpace(NeXus::NXEntry&);
void loadDataIntoTheWorkSpace(NeXus::NXEntry&);
/// Calculate error for y
Expand All @@ -72,8 +70,8 @@ class DLLExport LoadSINQ: public API::IDataFileChecker {
void runLoadInstrument();

std::vector<std::string> supportedInstruments;
std::string m_nexusInstrumentEntryName;
std::string m_instrumentName;
std::string m_nexusInstrumentEntryName;
API::MatrixWorkspace_sptr m_localWorkspace;
size_t m_numberOfTubes; // number of tubes - X
size_t m_numberOfPixelsPerTube; //number of pixels per tube - Y
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/DeprecatedAlgorithm.h"
#include "MantidDataHandling/LoadTOFRawNexus.h"
#include <climits>
Expand All @@ -17,8 +16,8 @@

namespace Mantid
{
namespace DataHandling
{
namespace DataHandling
{
/** @class LoadSNSNexus LoadSNSNexus.h NeXus/LoadSNSNexus.h
Loads a NeXus file that conforms to the TOFRaw instrument definition format and stores it in a 2D workspace.
LoadTOFRawNeXus is an algorithm and as such inherits from the Algorithm class, via DataHandlingCommand, and overrides
Expand All @@ -43,25 +42,27 @@ namespace DataHandling
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
*/
class DLLExport LoadSNSNexus : public LoadTOFRawNexus, public API::DeprecatedAlgorithm
{
public:
/// Default constructor
LoadSNSNexus();
/// Destructor
~LoadSNSNexus() {}
/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const;
/// Default constructor
LoadSNSNexus();
/// Destructor
~LoadSNSNexus() {}
/// Algorithm's name for identification overriding a virtual method
virtual const std::string name() const;

/// Algorithm's version for identification overriding a virtual method
virtual int version() const;
/// category
virtual const std::string category() const { return "Deprecated"; }

/// Algorithm's version for identification overriding a virtual method
virtual int version() const;
/// category
virtual const std::string category() const { return "Deprecated"; }
int fileCheck(const std::string& filePath);
/// Returns a confidence value that this algorithm can load a file (Required by base class)
virtual int confidence(const Kernel::HDFDescriptor &) const { return 0; }
};

} // namespace DataHandling
} // namespace DataHandling
} // namespace Mantid

#endif /*MANTID_DATAHANDLING_LOADSNSNEXUS_H_*/
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IHDFFileLoader.h"
#include "MantidAPI/Sample.h"
#include "MantidAPI/SpectraDetectorTypes.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidNexus/NexusClasses.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidAPI/SpectraDetectorTypes.h"
#include "MantidAPI/IDataFileChecker.h"
#include "MantidKernel/MultiThreaded.h"

namespace Mantid
Expand Down Expand Up @@ -40,7 +39,7 @@ namespace DataHandling
File change history is stored at: <https://github.com/mantidproject/mantid>
*/
class DLLExport LoadTOFRawNexus : public API::IDataFileChecker
class DLLExport LoadTOFRawNexus : public API::IHDFFileLoader
{
public:
/// Default Constructor
Expand All @@ -64,10 +63,8 @@ class DLLExport LoadTOFRawNexus : public API::IDataFileChecker

static std::string getEntryName(const std::string & filename);

/// 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
virtual int confidence(const Kernel::HDFDescriptor & descriptor) const;

void countPixels(const std::string &nexusfilename, const std::string & entry_name,
std::vector<std::string> & bankNames);
Expand Down
37 changes: 7 additions & 30 deletions Code/Mantid/Framework/DataHandling/src/LoadNXSPE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Algorithm to load an NXSPE file into a workspace2D. It will create a new instrum
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidAPI/RegisterFileLoader.h"

#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
Expand All @@ -29,10 +29,7 @@ namespace Mantid
namespace DataHandling
{

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(LoadNXSPE)
//register the algorithm into loadalgorithm factory
DECLARE_LOADALGORITHM(LoadNXSPE)
DECLARE_HDF_FILELOADER_ALGORITHM(LoadNXSPE);

using namespace Mantid::Kernel;
using namespace Mantid::API;
Expand Down Expand Up @@ -62,38 +59,18 @@ namespace DataHandling
this->setWikiDescription("Algorithm to load an NXSPE file into a workspace2D. It will create a new instrument, that can be overwritten later by the LoadInstrument algorithm.");
}

//----------------------------------------------------------------------------------------------
/**
* 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 LoadNXSPE::quickFileCheck(const std::string& filePath,size_t nread, const file_header& header)
{
std::string ext = this->extension(filePath);
// If the extension is nxspe then give it a go
if( ext.compare("nxspe") == 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
* 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 LoadNXSPE::fileCheck(const std::string& filePath)
int LoadNXSPE::confidence(const Kernel::HDFDescriptor & descriptor) const
{
int confidence(0);
typedef std::map<std::string,std::string> string_map_t;
try
{
::NeXus::File file = ::NeXus::File(filePath);
::NeXus::File file = ::NeXus::File(descriptor.filename());
string_map_t entries = file.getEntries();
for(string_map_t::const_iterator it = entries.begin(); it != entries.end(); ++it)
{
Expand Down
55 changes: 16 additions & 39 deletions Code/Mantid/Framework/DataHandling/src/LoadSINQ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#include "MantidDataHandling/LoadSINQ.h"
#include "MantidAPI/FileProperty.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidAPI/Progress.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/UnitFactory.h"

#include <limits>
#include <algorithm>
Expand All @@ -26,10 +26,7 @@ using namespace Kernel;
using namespace API;
using namespace NeXus;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(LoadSINQ)
//register the algorithm into loadalgorithm factory
DECLARE_LOADALGORITHM(LoadSINQ)
DECLARE_HDF_FILELOADER_ALGORITHM(LoadSINQ);

//----------------------------------------------------------------------------------------------
/** Constructor
Expand Down Expand Up @@ -70,38 +67,18 @@ void LoadSINQ::initDocs() {
this->setOptionalMessage("Loads PSI nexus file.");
}

bool LoadSINQ::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
* 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 LoadSINQ::fileCheck(const std::string& filePath) {
// Create the root Nexus class
NXRoot root(filePath);
int LoadSINQ::confidence(const Kernel::HDFDescriptor & descriptor) const
{
// Create the root Nexus class
NXRoot root(descriptor.filename());
NXEntry entry = root.openFirstEntry();
std::string instrumentName = getInstrumentName(entry);
std::string nexusInstrumentName;
std::string instrumentName = getInstrumentName(entry,nexusInstrumentName);
if (std::find(supportedInstruments.begin(), supportedInstruments.end(),
instrumentName) != supportedInstruments.end()) {
// FOUND
Expand Down Expand Up @@ -148,7 +125,7 @@ void LoadSINQ::exec() {

void LoadSINQ::setInstrumentName(NeXus::NXEntry& entry) {

m_instrumentName = getInstrumentName(entry);
m_instrumentName = getInstrumentName(entry,m_nexusInstrumentEntryName);
if (m_instrumentName == "") {
std::string message(
"Cannot read the instrument name from the Nexus file!");
Expand All @@ -158,7 +135,7 @@ void LoadSINQ::setInstrumentName(NeXus::NXEntry& entry) {

}

std::string LoadSINQ::getInstrumentName(NeXus::NXEntry& entry) {
std::string LoadSINQ::getInstrumentName(NeXus::NXEntry& entry, std::string &nexusInstrumentName) const {

// format: /entry0/?????/name

Expand All @@ -167,8 +144,8 @@ std::string LoadSINQ::getInstrumentName(NeXus::NXEntry& entry) {
std::vector<NXClassInfo> v = entry.groups();
for (auto it = v.begin(); it < v.end(); it++) {
if (it->nxclass == "NXinstrument") {
m_nexusInstrumentEntryName = it->nxname;
std::string insNamePath = m_nexusInstrumentEntryName + "/name";
nexusInstrumentName = it->nxname;
std::string insNamePath = nexusInstrumentName + "/name";
if ( !entry.isValid(insNamePath) )
throw std::runtime_error("Error reading the instrument name: " + insNamePath + " is not a valid path!");
instrumentName = entry.getString(insNamePath);
Expand Down
15 changes: 1 addition & 14 deletions Code/Mantid/Framework/DataHandling/src/LoadSNSNexus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/LoadSNSNexus.h"
#include "MantidAPI/LoadAlgorithmFactory.h" // For the DECLARE_LOADALGORITHM macro

namespace Mantid
{
namespace DataHandling
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadSNSNexus)
DECLARE_LOADALGORITHM(LoadSNSNexus)
DECLARE_ALGORITHM(LoadSNSNexus);

LoadSNSNexus::LoadSNSNexus()
{
Expand All @@ -27,16 +25,5 @@ int LoadSNSNexus::version() const {return 1;}

const std::string LoadSNSNexus::name() const {return "LoadSNSNexus";}

/**
* 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)
{
UNUSED_ARG(filePath);
return 0;
}

} // namespace DataHandling
} // namespace Mantid

0 comments on commit e61a010

Please sign in to comment.