Skip to content

Commit

Permalink
Port final DataHandling loaders to new structure. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jul 3, 2013
1 parent 9a2601f commit b3236b5
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 410 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/IFileLoader.h"
#include <fstream>

namespace Mantid
Expand Down Expand Up @@ -45,7 +44,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 LoadRKH : public API::IDataFileChecker
class DLLExport LoadRKH : public API::IFileLoader
{
public:
/// Constructor
Expand All @@ -58,10 +57,10 @@ class DLLExport LoadRKH : public API::IDataFileChecker
virtual int version() const { return (1); }
/// Algorithm's category for identification
virtual const std::string category() const { return "DataHandling\\Text;SANS"; }
/// do a quick check that this file can be loaded
virtual bool quickFileCheck(const std::string& ,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(Kernel::FileDescriptor & descriptor) const;

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/IFileLoader.h"

namespace Mantid
{
namespace DataHandling
Expand Down Expand Up @@ -44,7 +44,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 LoadSNSspec : public API::IDataFileChecker
class DLLExport LoadSNSspec : public API::IFileLoader
{
public:
LoadSNSspec();
Expand All @@ -53,10 +53,8 @@ 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,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(Kernel::FileDescriptor & 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,8 @@
//---------------------------------------------------
// Includes
//---------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/IFileLoader.h"

namespace Mantid
{
namespace DataHandling
Expand Down Expand Up @@ -42,11 +42,11 @@ namespace DataHandling
File change history is stored at: <https://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport LoadSPE : public API::IDataFileChecker
class DLLExport LoadSPE : public API::IFileLoader
{
public:
/// Constructor
LoadSPE() : API::IDataFileChecker() {}
LoadSPE() : API::IFileLoader() {}
/// Virtual destructor
virtual ~LoadSPE() {}
/// Algorithm's name
Expand All @@ -55,10 +55,9 @@ class DLLExport LoadSPE : public API::IDataFileChecker
virtual int version() const { return (1); }
/// Algorithm's category for identification
virtual const std::string category() const { return "DataHandling\\SPE;Inelastic"; }
///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(Kernel::FileDescriptor & descriptor) const;

private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IFileLoader.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/IDataFileChecker.h"

//----------------------------------------------------------------------

namespace Poco {
Expand Down Expand Up @@ -49,7 +49,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 LoadSpice2D : public API::IDataFileChecker
class DLLExport LoadSpice2D : public API::IFileLoader
{
public:
///default constructor
Expand All @@ -65,10 +65,8 @@ 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,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(Kernel::FileDescriptor & descriptor) const;

private:
/// Sets documentation strings for this algorithm
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/DataHandling/src/LoadAscii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace Mantid
{
confidence = 0;
}
else if(Kernel::FileDescriptor::isAscii(descriptor.data()))
else if(descriptor.isAscii())
{
confidence = 10; // Low so that others may try
}
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/DataHandling/src/LoadDaveGrp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int LoadDaveGrp::confidence(Kernel::FileDescriptor & descriptor) const
const std::string & extn = descriptor.extension();
if(extn.compare(".grp") != 0 && extn.compare(".sqe") != 0 && extn.compare(".txt") != 0) return 0;

if(!descriptor.isAscii(descriptor.data())) return 0;
if(!descriptor.isAscii()) return 0;


bool daveGrp(false);
Expand Down
2 changes: 1 addition & 1 deletion Code/Mantid/Framework/DataHandling/src/LoadGSS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace Mantid
*/
int LoadGSS::confidence(Kernel::FileDescriptor & descriptor) const
{
if(!descriptor.isAscii(descriptor.filename())) return 0;
if(!descriptor.isAscii()) return 0;

std::string str;
std::istream & file = descriptor.data();
Expand Down
4 changes: 2 additions & 2 deletions Code/Mantid/Framework/DataHandling/src/LoadPDFgetNFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ namespace DataHandling
return 0;
}

auto & file = descriptor.data();
if(!Kernel::FileDescriptor::isAscii(file)) return 0;
if(!descriptor.isAscii()) return 0;

auto & file = descriptor.data();
std::string str;
std::getline(file, str); //workspace title first line
while (!file.eof())
Expand Down
196 changes: 59 additions & 137 deletions Code/Mantid/Framework/DataHandling/src/LoadRKH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Loads the given file in the RKH text format, which can be a file with three colu
#include "MantidDataHandling/LoadRKH.h"
#include "MantidDataHandling/SaveRKH.h"
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidAPI/NumericAxis.h"
#include "MantidKernel/VectorHelper.h"
#include "MantidKernel/ListValidator.h"

Expand All @@ -32,10 +32,63 @@ namespace DataHandling
using namespace Mantid::API;
using namespace Mantid::Kernel;

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(LoadRKH)
//register the algorithm into loadalgorithm factory
DECLARE_LOADALGORITHM(LoadRKH)
DECLARE_FILELOADER_ALGORITHM(LoadRKH);

/**
* 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 LoadRKH::confidence(Kernel::FileDescriptor & descriptor) const
{
if(!descriptor.isAscii()) return 0;

int confidence = 0;
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep(" ");

auto &file = descriptor.data();
std::string fileline("");

// Header looks something like this where the text inside [] could be anything
// LOQ Thu 28-OCT-2004 12:23 [W 26 INST_DIRECT_BEAM]

// -- First line --
std::getline(file, fileline);
// LOQ or SANS2D
if(fileline.find("LOQ") == std::string::npos && fileline.find("SANS2D") == std::string::npos) return 0;
// Next should be date time string
static const char* MONTHS[12] = {"-JAN-", "-FEB-", "-MAR-", "-APR-", "-MAY-", "-JUN-", "-JUL-", "-AUG-", "-SEP-", "-OCT-", "-NOV-", "-DEC-"};

bool foundMonth(false);
for(size_t i = 0 ; i < 12; ++i)
{
if(fileline.find(MONTHS[i]) != std::string::npos)
{
foundMonth = true;
break;
}
}
if(!foundMonth) return 0;

// there are no constraints on the second line
std::getline(file, fileline);

// read 3rd line - should contain sequence " 0 0 0 1"
std::getline(file, fileline);
if(fileline.find(" 0 0 0 1") == std::string::npos) return 0;

// read 4th line - should contain sequence " 0 0 0 1"
std::getline(file, fileline);
if(fileline.find(" 0 0 0 0") == std::string::npos) return 0;

// read 5th line - should contain sequence "3 (F12.5,2E16.6)"
std::getline(file, fileline);
if(fileline.find("3 (F12.5,2E16.6)") == std::string::npos) return 0;

return 20; // Better than LoadAscii
}


/// Sets documentation strings for this algorithm
void LoadRKH::initDocs()
Expand Down Expand Up @@ -449,138 +502,7 @@ void LoadRKH::binCenter(const MantidVec oldBoundaries, MantidVec & toCenter) con
{
VectorHelper::convertToBinCentre(oldBoundaries, toCenter);
}
/**This method does a quick file check by checking the no.of bytes read nread params and header buffer
* @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 LoadRKH::quickFileCheck(const std::string &,size_t nread,const file_header& header)
{
for(size_t i=0; i<nread; i++)
{
if (!isascii(header.full_hdr[i]))
return false;
}
// RKH files are always ASCII, they can have extension
return true;
}

/**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("");

//get first line
getline(file, fileline);
if ( fileline.find("Workspace:") == std::string::npos )
{//seeing Workspace: is enough to carry on
if ( fileline.find(":") == std::string::npos )
{//there must be a : in the line
return 0;
}
if ( fileline.find("LOQ") == std::string::npos )
{
if ( fileline.find("SANS2D") == std::string::npos )
{
static const std::string MONTHS[12] = {"-JAN-", "-FEB-", "-MAR-", "-APR-", "-MAY-", "-JUN-", "-JUL-", "-AUG-", "-SEP-", "-OCT-", "-NOV-", "-DEC-"};
size_t i = 0;
for ( ; i < 12; ++i )
{
if ( fileline.find(MONTHS[i]) != std::string::npos )
{
//the line is acceptable
break;
}
}
if ( i == 12 )
{
//the line contains none of the strings we were looking for except one : which is not enough, reject the file
return 0;
}
}
}
}

//there are no constraints on the second line
getline(file, fileline);

//read 3rd line
getline(file, fileline);
int ncols=0;
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;
}

if( bret == 10 )
{
// Assume we are better than LoadAscii
bret += 10;
}
return bret;
}

}
}

0 comments on commit b3236b5

Please sign in to comment.