Skip to content

Commit

Permalink
Move the remaining loaders to the new scheme. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jul 3, 2013
1 parent db8d95c commit 2a330f5
Show file tree
Hide file tree
Showing 10 changed files with 236 additions and 264 deletions.
11 changes: 4 additions & 7 deletions Code/Mantid/Framework/Crystal/inc/MantidCrystal/LoadIsawPeaks.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
#define MANTID_CRYSTAL_LOADISAWPEAKS_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IFileLoader.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidAPI/IDataFileChecker.h"

namespace Mantid
{
Expand All @@ -17,7 +16,7 @@ namespace Crystal
* @author Janik Zikovsky, SNS
* @date 2011-03-07 15:22:11.897153
*/
class DLLExport LoadIsawPeaks : public API::IDataFileChecker
class DLLExport LoadIsawPeaks : public API::IFileLoader
{
public:
LoadIsawPeaks();
Expand All @@ -30,10 +29,8 @@ namespace Crystal
/// Algorithm's category for identification
virtual const std::string category() const { return "Crystal;DataHandling\\Isaw";}

/// 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(Kernel::FileDescriptor & descriptor) const;

private:
/// Sets documentation strings for this algorithm
Expand Down
129 changes: 62 additions & 67 deletions Code/Mantid/Framework/Crystal/src/LoadIsawPeaks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ NOTE: The instrument used is determined by reading the 'Instrument:' and 'Date:'
*WIKI*/
#include "MantidAPI/FileProperty.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidCrystal/LoadIsawPeaks.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
Expand All @@ -29,7 +30,7 @@ NOTE: The instrument used is determined by reading the 'Instrument:' and 'Date:'
#include <stdlib.h>
#include <string>
#include "MantidKernel/Unit.h"
#include "MantidAPI/LoadAlgorithmFactory.h"


using Mantid::Kernel::Strings::readToEndOfLine;
using Mantid::Kernel::Strings::getWord;
Expand All @@ -40,9 +41,7 @@ namespace Mantid
namespace Crystal
{

// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(LoadIsawPeaks)
DECLARE_LOADALGORITHM(LoadIsawPeaks)
DECLARE_FILELOADER_ALGORITHM(LoadIsawPeaks);

using namespace Mantid::Kernel;
using namespace Mantid::API;
Expand All @@ -64,6 +63,65 @@ namespace Crystal
}


/**
* 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 LoadIsawPeaks::confidence(Kernel::FileDescriptor & descriptor) const
{
const std::string & extn = descriptor.extension();
// If the extension is peaks or integrate then give it a go
if(extn.compare(".peaks") != 0 && extn.compare(".integrate") != 0 ) return 0;

int confidence(0);
try
{
auto &in = descriptor.data();
// Read the header, load the instrument
std::string tag;
std::string r = getWord(in, false);

if(r.length() < 1)
throw std::logic_error( std::string( "No first line of Peaks file" ) );

if(r.compare("Version:") != 0)
throw std::logic_error(
std::string( "No Version: on first line of Peaks file" ) );

std::string C_version = getWord( in , false );
if( C_version.length() < 1 )
throw std::logic_error( std::string( "No Version for Peaks file" ) );

getWord(in , false); //tag
// cppcheck-suppress unreadVariable
std::string C_Facility = getWord(in, false );

getWord(in , false ); //tag
std::string C_Instrument = getWord(in , false);

if( C_Instrument.length() < 1 )
throw std::logic_error(
std::string( "No Instrument for Peaks file" ) );

// Date: use the current date/time if not found
Kernel::DateAndTime C_experimentDate;
std::string date;
tag = getWord(in, false );
if(tag.empty())
date = Kernel::DateAndTime::getCurrentTime().toISO8601String();
else if(tag == "Date:")
date = getWord(in, false );
readToEndOfLine( in, true );
confidence = 95;
}
catch (std::exception & )
{
}
return confidence;
}


//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void LoadIsawPeaks::initDocs()
Expand All @@ -73,69 +131,6 @@ namespace Crystal
}
//----------------------------------------------------------------------------------------------

/// @copydoc Mantid::API::IDataFileChecker::quickFileCheck
bool LoadIsawPeaks::quickFileCheck(const std::string& filePath,size_t nread,const file_header& header)
{
UNUSED_ARG(nread);
UNUSED_ARG(header);

std::string ext = this->extension(filePath);
// If the extension is peaks or integrate then give it a go
if( ext.compare("peaks") == 0 ) return true;
else if( ext.compare("integrate") == 0 ) return true;
else return false;
}

/// @copydoc Mantid::API::IDataFileChecker::fileCheck
int LoadIsawPeaks::fileCheck(const std::string& filePath)
{
int confidence(0);
try
{
// Open the file
std::ifstream in( filePath.c_str() );
// Read the header, load the instrument
std::string tag;
std::string r = getWord( in , false );

if( r.length() < 1 )
throw std::logic_error( std::string( "No first line of Peaks file" ) );

if( r.compare( std::string( "Version:" ) ) != 0 )
throw std::logic_error(
std::string( "No Version: on first line of Peaks file" ) );

std::string C_version = getWord( in , false );
if( C_version.length() < 1 )
throw std::logic_error( std::string( "No Version for Peaks file" ) );

getWord( in , false ); //tag
// cppcheck-suppress unreadVariable
std::string C_Facility = getWord( in , false );

getWord( in , false ); //tag
std::string C_Instrument = getWord( in , false );

if( C_Instrument.length() < 1 )
throw std::logic_error(
std::string( "No Instrument for Peaks file" ) );

// Date: use the current date/time if not found
Kernel::DateAndTime C_experimentDate;
std::string date;
tag = getWord( in , false );
if(tag.empty())
date = Kernel::DateAndTime::getCurrentTime().toISO8601String();
else if(tag == "Date:")
date = getWord( in , false );
readToEndOfLine( in , true );
confidence = 95;
}
catch (std::exception & )
{
}
return confidence;
}

//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
Expand Down
11 changes: 10 additions & 1 deletion Code/Mantid/Framework/Crystal/test/LoadIsawPeaksTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@ class LoadIsawPeaksTest : public CxxTest::TestSuite
{
public:


void test_Confidence()
{
LoadIsawPeaks alg;
alg.initialize();
alg.setPropertyValue("Filename", "TOPAZ_1241.integrate");

Mantid::Kernel::FileDescriptor descriptor(alg.getPropertyValue("Filename"));
TS_ASSERT_EQUALS(95, alg.confidence(descriptor));
}

void test_Init()
{
LoadIsawPeaks alg;
Expand Down
36 changes: 17 additions & 19 deletions Code/Mantid/Framework/DataHandling/inc/MantidDataHandling/Load.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/IDataFileChecker.h"
#include "MantidAPI/Algorithm.h"
#include <Poco/Mutex.h>

#include <list>
Expand All @@ -13,7 +13,7 @@ namespace Mantid
{
namespace DataHandling
{

/**
Loads a workspace from a data file. The algorithm tries to determine the actual type
of the file (raw, nxs, ...) and use the specialized loading algorith to load it.
Expand All @@ -40,8 +40,8 @@ namespace Mantid
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport Load : public API::IDataFileChecker
*/
class DLLExport Load : public API::Algorithm
{
public:
/// Default constructor
Expand All @@ -60,16 +60,12 @@ namespace Mantid
private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Quick file always returns false here
virtual bool quickFileCheck(const std::string& filePath,size_t nread,const file_header& header);
/// file check by looking at the structure of the data file
virtual int fileCheck(const std::string& filePath);
/// This method returns shared pointer to a load algorithm which got
/// the highest preference after file check.
API::IDataFileChecker_sptr getFileLoader(const std::string& filePath);
API::IAlgorithm_sptr getFileLoader(const std::string& filePath);
/// Declare any additional input properties from the concrete loader
void declareLoaderProperties(const API::IDataFileChecker_sptr loader);
void declareLoaderProperties(const API::IAlgorithm_sptr & loader);

/// Initialize the static base properties
void init();
/// Execute
Expand All @@ -83,16 +79,16 @@ namespace Mantid
/// Overrides the cancel() method to call m_loader->cancel()
void cancel();
/// Create the concrete instance use for the actual loading.
API::IDataFileChecker_sptr createLoader(const std::string & name, const double startProgress = -1.0,
const double endProgress=-1.0, const bool logging = true) const;
API::IAlgorithm_sptr createLoader(const std::string & name, const double startProgress = -1.0,
const double endProgress=-1.0, const bool logging = true) const;
/// Set the loader option for use as a Child Algorithm.
void setUpLoader(API::IDataFileChecker_sptr loader, const double startProgress = -1.0,
const double endProgress=-1.0, const bool logging = true) const;
void setUpLoader(API::IAlgorithm_sptr & loader, const double startProgress = -1.0,
const double endProgress=-1.0, const bool logging = true) const;
/// Set the output workspace(s)
void setOutputWorkspace(const API::IDataFileChecker_sptr & loader);
void setOutputWorkspace(const API::IAlgorithm_sptr & loader);
/// Retrieve a pointer to the output workspace from the Child Algorithm
API::Workspace_sptr getOutputWorkspace(const std::string & propName,
const API::IDataFileChecker_sptr & loader) const;
const API::IAlgorithm_sptr & loader) const;

/// Load a file to a given workspace name.
API::Workspace_sptr loadFileToWs(const std::string & fileName, const std::string & wsName);
Expand All @@ -105,10 +101,12 @@ namespace Mantid
/// The base properties
std::set<std::string> m_baseProps;
/// The actual loader
API::IDataFileChecker_sptr m_loader;
API::IAlgorithm_sptr m_loader;
/// The name of the property that will be passed the property from our Filename
std::string m_filenamePropName;
/// Mutex for temporary fix for #5963
static Poco::Mutex m_mutex;
};
};

} // namespace DataHandling
} // namespace Mantid
Expand Down

0 comments on commit 2a330f5

Please sign in to comment.