Skip to content

Commit

Permalink
Final NeXus loader converted. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jun 29, 2013
1 parent e8d298b commit 04e3257
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 67 deletions.
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"

namespace Mantid
{
Expand Down Expand Up @@ -38,7 +37,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 LoadQKK : public API::IDataFileChecker
class DLLExport LoadQKK : public API::IHDFFileLoader
{
public:
/// (Empty) Constructor
Expand All @@ -52,10 +51,8 @@ class DLLExport LoadQKK : public API::IDataFileChecker
/// 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,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
73 changes: 13 additions & 60 deletions Code/Mantid/Framework/DataHandling/src/LoadQKK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//---------------------------------------------------
#include "MantidDataHandling/LoadQKK.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/WorkspaceValidators.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidGeometry/Objects/ShapeFactory.h"
Expand All @@ -32,10 +32,19 @@ namespace Mantid
{

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(LoadQKK)
DECLARE_HDF_FILELOADER_ALGORITHM(LoadQKK);

//register the algorithm into loadalgorithm factory
DECLARE_LOADALGORITHM(LoadQKK)
/**
* 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 LoadQKK::confidence(const Kernel::HDFDescriptor & descriptor) const
{
const auto & firstEntryName = descriptor.firstEntryNameType().first;
if(descriptor.pathExists("/" + firstEntryName + "/data/hmm_xy")) return 80;
else return 0;
}

/// Sets documentation strings for this algorithm
void LoadQKK::initDocs()
Expand Down Expand Up @@ -198,61 +207,5 @@ namespace Mantid

}

/**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 :: 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 LoadQKK::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")||!extn.compare("nx.hdf"))?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 including its path
* @return an integer value how much this algorithm can load the file
*/
int LoadQKK::fileCheck(const std::string& filePath)
{
NeXus::NXRoot root(filePath);

try
{
// Check if there exists a data set with name hmm_xy
NeXus::NXEntry entry = root.openFirstEntry();
if (entry.containsGroup("data"))
{
NeXus::NXData data = entry.openNXData("data");
if (data.getDataSetInfo("hmm_xy").stat != NX_ERROR)
{
return 80; // Give it an 80% chance that the file is readable
}
}
}
catch(...)
{
return 0;
}
return 0;
}

}//namespace
}//namespace
9 changes: 9 additions & 0 deletions Code/Mantid/Framework/DataHandling/test/LoadQKKTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ class LoadQKKTest : public CxxTest::TestSuite
{
}

void test_File_Check_Confidence()
{
Mantid::DataHandling::LoadQKK loader;
loader.initialize();
loader.setPropertyValue("Filename", "QKK0029775.nx.hdf"); // find the full path

TS_ASSERT_EQUALS(80, loader.confidence(loader.getPropertyValue("Filename")));
}

void testInit()
{
Mantid::DataHandling::LoadQKK load;
Expand Down

0 comments on commit 04e3257

Please sign in to comment.