Skip to content

Commit

Permalink
More loaders migrated. Refs #7263
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Jun 30, 2013
1 parent 7170674 commit 7a1bd4e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataHandling/LoadRawHelper.h"
#include "MantidAPI/IDataFileChecker.h"
#include "MantidDataObjects/Workspace2D.h"
#include <climits>

//----------------------------------------------------------------------
Expand Down Expand Up @@ -61,12 +59,6 @@ namespace Mantid
/// Algorithm's category for identification overriding a virtual method
virtual const std::string category() const { return "DataHandling\\Raw"; }

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


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,11 +4,10 @@
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidAPI/Algorithm.h"
#include "MantidAPI/IFileLoader.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidDataHandling/ISISRunLogs.h"
#include "MantidAPI/Run.h"
#include "MantidAPI/IDataFileChecker.h"
#include <climits>

//----------------------------------------------------------------------
Expand Down Expand Up @@ -54,7 +53,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 LoadRawHelper: public API::IDataFileChecker
class DLLExport LoadRawHelper: public API::IFileLoader
{
public:
/// Default constructor
Expand All @@ -70,10 +69,8 @@ namespace Mantid
/// Read in run parameters Public so that LoadRaw2 can use it
void loadRunParameters(API::MatrixWorkspace_sptr localWorkspace, ISISRAW * const = NULL) 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 if this file can be loaded return a value between 1 and 100
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;

protected:
/// Overwrites Algorithm method.
Expand Down Expand Up @@ -186,8 +183,6 @@ namespace Mantid

/// Overwrites Algorithm method
void exec();
/// Check if the buffer looks like a RAW file header
bool isRawFileHeader(const int nread, const unsigned char* buffer) const;
/// convert month label to int string
std::string convertMonthLabelToIntStr(std::string month) const;

Expand Down
27 changes: 2 additions & 25 deletions Code/Mantid/Framework/DataHandling/src/LoadRaw3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ LoadRaw version 1 and 2 are no longer available in Mantid. Version 3 has been v
#include "MantidGeometry/Instrument/XMLlogfile.h"
#include "MantidAPI/MemoryManager.h"
#include "MantidAPI/WorkspaceGroup.h"
#include "MantidAPI/RegisterFileLoader.h"
#include "MantidAPI/SpectraAxis.h"
#include "MantidKernel/UnitFactory.h"
#include "MantidKernel/ConfigService.h"
Expand All @@ -38,7 +39,6 @@ LoadRaw version 1 and 2 are no longer available in Mantid. Version 3 has been v
#include "MantidAPI/FileProperty.h"
#include "LoadRaw/isisraw2.h"
#include "MantidDataHandling/LoadLog.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidAPI/SpectrumDetectorMapping.h"

#include <boost/shared_ptr.hpp>
Expand All @@ -50,9 +50,7 @@ namespace Mantid
{
namespace DataHandling
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadRaw3)
DECLARE_LOADALGORITHM(LoadRaw3)
DECLARE_FILELOADER_ALGORITHM(LoadRaw3);

/// Sets documentation strings for this algorithm
void LoadRaw3::initDocs()
Expand Down Expand Up @@ -725,26 +723,5 @@ namespace Mantid
}
}

/**This method does a quick file check by checking the no.of bytes read nread params and header buffer
* @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 LoadRaw3::quickFileCheck(const std::string& filePath,size_t nread,const file_header& header)
{
return LoadRawHelper::quickFileCheck(filePath,nread,header);

}
/**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 LoadRaw3::fileCheck(const std::string& filePath)
{
return LoadRawHelper::fileCheck(filePath);
}


} // namespace DataHandling
} // namespace Mantid
80 changes: 15 additions & 65 deletions Code/Mantid/Framework/DataHandling/src/LoadRawHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "MantidKernel/ListValidator.h"
#include "LoadRaw/isisraw2.h"
#include "MantidDataHandling/LoadLog.h"
#include "MantidAPI/LoadAlgorithmFactory.h"
#include "MantidDataHandling/LoadAscii.h"

#include <boost/date_time/gregorian/gregorian.hpp>
Expand Down Expand Up @@ -1043,75 +1042,26 @@ namespace Mantid

}


/**
* Check if the buffer looks like a RAW file header by looking at
* at the "address of RUN and INST section" attribute - if there, it's an ISIS raw file.
* @param nread The number of bytes in the buffer
* @param buffer A buffer of nread bytes of the file
* @returns True if this looks like an ISIS raw file
*/
bool LoadRawHelper::isRawFileHeader(const int nread, const unsigned char* buffer) const
{
if( nread > 88 && (buffer[84] == 32) && (buffer[88] == 126) )
{
return true;
}
else return false;
}


/**This method does a quick file check by checking the no.of bytes read nread params and header buffer
* @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 LoadRawHelper::quickFileCheck(const std::string& filePath,size_t nread,const file_header& header)
{
std::string extn=extension(filePath);
bool braw = (!extn.compare("raw")||!extn.compare("add")||(extn.length() > 0 && extn[0]=='s')) ? true : false;
if( isRawFileHeader(static_cast<int>(nread), header.full_hdr) || braw )
{
return true;
}
else
{
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 LoadRawHelper::fileCheck(const std::string& filePath)
int LoadRawHelper::confidence(Kernel::FileDescriptor & descriptor) const
{
/* Open the file and read in the first bufferSize bytes - these will
* be used to determine the type of the file
*/
int bret=0;
FILE* fp = fopen(filePath.c_str(), "rb");
if (fp == NULL)
{
return bret;
}
file_header header;
int nread(static_cast<int>(fread(
&header,sizeof(unsigned char), IDataFileChecker::g_hdr_bytes, fp)));
header.full_hdr[IDataFileChecker::g_hdr_bytes] = '\0';

if (fclose(fp) != 0)
{
}

if( isRawFileHeader(nread, header.full_hdr) )
auto & stream = descriptor.data();
// 85th character is a space & 89th character is a ~
stream.seekg(84, std::ios::beg);
int c = stream.get();
int confidence(0);
if(c == 32)
{
bret=80;
stream.seekg(3, std::ios::cur);
int c = stream.get();
if(c == 126) confidence = 80;
}
return bret;
return confidence;
}



} // namespace DataHandling
} // namespace Mantid
31 changes: 20 additions & 11 deletions Code/Mantid/Framework/DataHandling/src/UpdateInstrumentFromFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ would tell the algorithm to interpret the columns as:
#include "MantidAPI/MatrixWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidGeometry/Instrument/ComponentHelper.h"
#include "MantidKernel/HDFDescriptor.h"
#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
#include "LoadRaw/isisraw2.h"
Expand Down Expand Up @@ -132,27 +133,35 @@ namespace Mantid
const bool moveMonitors = getProperty("MoveMonitors");
m_ignoreMonitors = (!moveMonitors);

// Check the file type
LoadRawHelper isisRAW;
LoadAscii ascii;
LoadISISNexus2 isisNexus;
if( isisRAW.fileCheck(filename) > 0 )
// Check file type
if(HDFDescriptor::isHDF(filename))
{
updateFromRaw(filename);
LoadISISNexus2 isisNexus;
if(isisNexus.confidence(filename) > 0)
{
updateFromNeXus(filename);
return;
}
}
else if(isisNexus.confidence(filename) > 0)

if(FileDescriptor::isAscii(filename))
{
updateFromNeXus(filename);
updateFromAscii(filename);
return;
}
else if(Kernel::FileDescriptor::isAscii(filename))

LoadRawHelper isisRAW;
auto *descriptor = new Kernel::FileDescriptor(filename);
if( isisRAW.confidence(*descriptor) > 0 )
{
updateFromAscii(filename);
delete descriptor;
updateFromRaw(filename);
}
else
{
delete descriptor;
throw std::invalid_argument("File \"" + filename + "\" is not a valid input file.");
}

}

/**
Expand Down
11 changes: 11 additions & 0 deletions Code/Mantid/Framework/DataHandling/test/LoadRaw3Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ class LoadRaw3Test : public CxxTest::TestSuite
inputFile = "HET15869.raw";
}

void testConfidence()
{
using Mantid::Kernel::FileDescriptor;
LoadRaw3 loader;
loader.initialize();
loader.setPropertyValue("Filename", inputFile);

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

void testInit()
{
TS_ASSERT_THROWS_NOTHING( loader.initialize());
Expand Down

0 comments on commit 7a1bd4e

Please sign in to comment.