Commit 3576e7c2 authored by Janik Zikovsky's avatar Janik Zikovsky
Browse files

Refs #2840: PeaksWorkspace rewritten to use a vector of Peak objects instead....

Refs #2840: PeaksWorkspace rewritten to use a vector of Peak objects instead. PeakIntegration should be checked (no test). Added an algorithm that creates a simple empty PeaksWorkspace, for testing and perhaps future use by Andrei. 
parent d49b085d
......@@ -90,6 +90,12 @@ public:
/// Prints
virtual void print(std::ostream& s, int index) const = 0;
/// Specialized type check
virtual bool isBool()const = 0;
/// Must return overall memory size taken by the column.
virtual long int sizeOfData()const = 0;
/// Templated method for returning a value. No type checks are done.
template<class T>
T& cell(int index)
......@@ -111,11 +117,6 @@ public:
return get_type_info() == typeid(T);
}
/// Specialized type check
virtual bool isBool()const = 0;
/// Must return overall memory size taken by the column.
virtual long int sizeOfData()const = 0;
protected:
/// Sets the new column size.
virtual void resize(int count) = 0;
......@@ -125,7 +126,7 @@ protected:
virtual void remove(int index) = 0;
/// Pointer to a data element
virtual void* void_pointer(int index) = 0;
private:
std::string m_name;///< name
std::string m_type;///< type
friend class ColumnFactoryImpl;
......
......@@ -144,10 +144,10 @@ public:
virtual int columnCount() const = 0;
/// Gets the shared pointer to a column by name.
virtual boost::shared_ptr<Column> getColumn(const std::string& name) = 0;
virtual boost::shared_ptr<Mantid::API::Column> getColumn(const std::string& name) = 0;
/// Gets the shared pointer to a column by index.
virtual boost::shared_ptr<Column> getColumn(int index) = 0;
virtual boost::shared_ptr<Mantid::API::Column> getColumn(int index) = 0;
/// Returns a vector of all column names.
virtual std::vector<std::string> getColumnNames() = 0;
......
......@@ -28,6 +28,7 @@ set ( SRC_FILES
src/CreateCalFileByNames.cpp
src/CreateDummyCalFile.cpp
src/CreatePSDBleedMask.cpp
src/CreatePeaksWorkspace.cpp
src/CreateSingleValuedWorkspace.cpp
src/CreateWorkspace.cpp
src/CropWorkspace.cpp
......@@ -174,6 +175,7 @@ set ( INC_FILES
inc/MantidAlgorithms/CreateCalFileByNames.h
inc/MantidAlgorithms/CreateDummyCalFile.h
inc/MantidAlgorithms/CreatePSDBleedMask.h
inc/MantidAlgorithms/CreatePeaksWorkspace.h
inc/MantidAlgorithms/CreateSingleValuedWorkspace.h
inc/MantidAlgorithms/CreateWorkspace.h
inc/MantidAlgorithms/CropWorkspace.h
......@@ -321,6 +323,7 @@ set ( TEST_FILES
test/CreateCalFileByNamesTest.h
test/CreateDummyCalFileTest.h
test/CreatePSDBleedMaskTest.h
test/CreatePeaksWorkspaceTest.h
test/CreateSingleValuedWorkspaceTest.h
test/CreateWorkspaceTest.h
test/CropWorkspaceTest.h
......
#ifndef MANTID_ALGORITHMS_CREATEPEAKSWORKSPACE_H_
#define MANTID_ALGORITHMS_CREATEPEAKSWORKSPACE_H_
#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
namespace Mantid
{
namespace Algorithms
{
/** Create an empty PeaksWorkspace.
*
* @author Janik Zikovsky
* @date 2011-04-26 08:49:10.540441
*/
class DLLExport CreatePeaksWorkspace : public API::Algorithm
{
public:
CreatePeaksWorkspace();
~CreatePeaksWorkspace();
/// Algorithm's name for identification
virtual const std::string name() const { return "CreatePeaksWorkspace";};
/// Algorithm's version for identification
virtual int version() const { return 1;};
/// Algorithm's category for identification
virtual const std::string category() const { return "Diffraction";}
private:
/// Sets documentation strings for this algorithm
virtual void initDocs();
/// Initialise the properties
void init();
/// Run the algorithm
void exec();
};
} // namespace Mantid
} // namespace Algorithms
#endif /* MANTID_ALGORITHMS_CREATEPEAKSWORKSPACE_H_ */
#include "MantidAlgorithms/CreatePeaksWorkspace.h"
#include "MantidKernel/System.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidAPI/MatrixWorkspace.h"
namespace Mantid
{
namespace Algorithms
{
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(CreatePeaksWorkspace)
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
//----------------------------------------------------------------------------------------------
/** Constructor
*/
CreatePeaksWorkspace::CreatePeaksWorkspace()
{
}
//----------------------------------------------------------------------------------------------
/** Destructor
*/
CreatePeaksWorkspace::~CreatePeaksWorkspace()
{
}
//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void CreatePeaksWorkspace::initDocs()
{
this->setWikiSummary("Create an empty PeaksWorkspace.");
this->setOptionalMessage("Create an empty PeaksWorkspace.");
}
//----------------------------------------------------------------------------------------------
/** Initialize the algorithm's properties.
*/
void CreatePeaksWorkspace::init()
{
declareProperty(new WorkspaceProperty<MatrixWorkspace>("InstrumentWorkspace","",Direction::Input, true), "An optional input workspace containing the default instrument for peaks in this workspace.");
declareProperty(new WorkspaceProperty<PeaksWorkspace>("OutputWorkspace","",Direction::Output), "An output workspace.");
}
//----------------------------------------------------------------------------------------------
/** Execute the algorithm.
*/
void CreatePeaksWorkspace::exec()
{
MatrixWorkspace_sptr instWS = getProperty("InstrumentWorkspace");
// TODO Auto-generated execute stub
PeaksWorkspace_sptr out(new PeaksWorkspace());
setProperty("OutputWorkspace", out);
if (instWS)
{
out->setInstrument(instWS->getInstrument());
// Create a dumb default peak
out->addPeak( Peak(out->getInstrument(), out->getInstrument()->getDetectorIDs(true)[0], 1.0) );
out->getPeaks()[0].setH( 1.23 );
out->getPeaks()[0].setK( 2.34 );
}
}
} // namespace Mantid
} // namespace Algorithms
#ifndef MANTID_ALGORITHMS_CREATEPEAKSWORKSPACETEST_H_
#define MANTID_ALGORITHMS_CREATEPEAKSWORKSPACETEST_H_
#include <cxxtest/TestSuite.h>
#include "MantidKernel/Timer.h"
#include "MantidKernel/System.h"
#include <iostream>
#include <iomanip>
#include "MantidAlgorithms/CreatePeaksWorkspace.h"
#include "MantidDataObjects/PeaksWorkspace.h"
using namespace Mantid::Algorithms;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
class CreatePeaksWorkspaceTest : public CxxTest::TestSuite
{
public:
void test_Init()
{
CreatePeaksWorkspace alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
}
void test_exec()
{
// Name of the output workspace.
std::string outWSName("CreatePeaksWorkspaceTest_OutputWS");
CreatePeaksWorkspace alg;
TS_ASSERT_THROWS_NOTHING( alg.initialize() )
TS_ASSERT( alg.isInitialized() )
TS_ASSERT_THROWS_NOTHING( alg.setPropertyValue("OutputWorkspace", outWSName) );
TS_ASSERT_THROWS_NOTHING( alg.execute(); )
TS_ASSERT( alg.isExecuted() );
// Retrieve the workspace from data service.
PeaksWorkspace_sptr ws;
TS_ASSERT_THROWS_NOTHING( ws = boost::dynamic_pointer_cast<PeaksWorkspace>(AnalysisDataService::Instance().retrieve(outWSName)) );
TS_ASSERT(ws);
if (!ws) return;
// Check the results
TS_ASSERT_EQUALS(ws->getNumberPeaks(), 0);
// Remove workspace from the data service.
AnalysisDataService::Instance().remove(outWSName);
}
void test_Something()
{
}
};
#endif /* MANTID_ALGORITHMS_CREATEPEAKSWORKSPACETEST_H_ */
......@@ -3,6 +3,7 @@
#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidDataObjects/PeaksWorkspace.h"
namespace Mantid
{
......@@ -36,6 +37,9 @@ namespace Crystal
/// Run the algorithm
void exec();
std::string readHeader( Mantid::DataObjects::PeaksWorkspace_sptr outWS, std::ifstream& in );
void appendFile( Mantid::DataObjects::PeaksWorkspace_sptr outWS, std::string filename);
};
......
#include "MantidCrystal/LoadPeaksFile.h"
#include "MantidKernel/System.h"
#include "MantidAPI/FileProperty.h"
#include "MantidCrystal/LoadPeaksFile.h"
#include "MantidDataObjects/PeaksWorkspace.h"
#include "MantidGeometry/IComponent.h"
#include "MantidGeometry/Instrument/RectangularDetector.h"
#include "MantidKernel/System.h"
#include <algorithm>
#include <boost/shared_ptr.hpp>
#include <exception>
#include <fstream>
#include <iostream>
#include <math.h>
#include <ostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
namespace Mantid
{
......@@ -14,6 +26,7 @@ namespace Crystal
using namespace Mantid::Kernel;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Geometry;
//----------------------------------------------------------------------------------------------
......@@ -46,13 +59,339 @@ namespace Crystal
{
std::vector<std::string> exts;
exts.push_back(".peaks");
// exts.push_back(".integrate");
exts.push_back(".integrate");
declareProperty(new FileProperty("Filename", "", FileProperty::Load, exts),
"Path to an ISAW-style .peaks filename.");
declareProperty(new WorkspaceProperty<PeaksWorkspace>("OutputWorkspace","",Direction::Output), "Name of the output workspace.");
}
//-----------------------------------------------------------------------------------------------
/** Get a word from a line and strips spaces */
std::string getWord( std::ifstream &in , bool consumeEOL )
{
std::string s;
char c = 0;
if( in.good() )
for( c = in.get() ; c == ' ' && in.good() ; c = in.get() )
{}
else
return std::string();
if( c == '\n' )
{
if( !consumeEOL )
in.putback( c );
return std::string();
}
s.push_back( c );
if( in.good() )
for( c = in.get() ; in.good()&&c != ' ' && c != '\n' && c != '\r' ; c = in.get() )
s.push_back( c );
if( ((c == '\n') || (c == '\r')) && !consumeEOL )
in.putback( c );
return s;
}
//-----------------------------------------------------------------------------------------------
/** Read up to the eol */
void readToEndOfLine( std::ifstream& in , bool ConsumeEOL )
{
while( in.good() && getWord( in , false ).length() > 0 )
getWord( in , false );
if( !ConsumeEOL )
return ;
getWord( in , true );
}
//-----------------------------------------------------------------------------------------------
/** Reads the header of a .peaks file
*
* @param in :: stream of the input file
* @return the first word on the next line
*/
std::string LoadPeaksFile::readHeader( PeaksWorkspace_sptr outWS, std::ifstream& in )
{
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" ) );
tag = getWord( in , false );
std::string C_Facility = getWord( in , false );
tag = getWord( in , false );
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::get_current_time().to_ISO8601_string();
else if(tag == "Date:")
date = getWord( in , false );
readToEndOfLine( in , true );
// Now we load the instrument using the name and date
MatrixWorkspace_sptr tempWS = WorkspaceFactory::Instance().create("Workspace2D", 1, 1, 1);
tempWS->mutableRun().addProperty<std::string>("run_start", date);
IAlgorithm_sptr loadInst= createSubAlgorithm("LoadInstrument");
loadInst->setPropertyValue("InstrumentName", C_Instrument);
loadInst->setProperty<MatrixWorkspace_sptr> ("Workspace", tempWS);
loadInst->executeAsSubAlg();
// Populate the instrument parameters in this workspace - this works around a bug
tempWS->populateInstrumentParameters();
outWS->setInstrument( tempWS->getInstrument() );
// Now skip all lines on L1, detector banks, etc. until we get to a block of peaks. They start with 0.
readToEndOfLine( in , true );
readToEndOfLine( in , true );
std::string s = getWord(in, false);
while (s != "0")
{
readToEndOfLine( in , true );
s = getWord(in, false);
}
return s;
}
//-----------------------------------------------------------------------------------------------
/** Read one peak in a line of an ISAW peaks file.
*
* @param outWS :: workspace to add peaks to
* @param lastSt[in,out] :: last word (the one at the start of the line)
* @param in :: input stream
* @param[out] seqNum :: the sequence number of the peak
* @param bankName :: the bank number from the ISAW file.
* @return the Peak the Peak object created
*/
Mantid::DataObjects::Peak readPeak( PeaksWorkspace_sptr outWS, std::string & lastStr, std::ifstream& in, int & seqNum, std::string bankName)
{
double h ; double k ; double l ; double col ;
double row ; double chan ; double L2 ;
double ScatAng ; double Az ; double wl ;
double D ; double IPK ; double Inti ;
double SigI ; int iReflag;
seqNum = -1;
std::string s = lastStr;
if( s.length() < 1 && in.good() )//blank line
{
readToEndOfLine( in , true );
s = getWord( in , false );;
}
if( s.length() < 1 )
throw std::runtime_error("Empty peak line encountered.");
if( s.compare( "2" ) == 0 )
{
readToEndOfLine( in , true );
for( s = getWord( in , false ) ; s.length() < 1 && in.good() ;
s = getWord( in , true ) )
{
s = getWord( in , false );
}
}
if( s.length() < 1 )
throw std::runtime_error("Empty peak line encountered.");
if( s.compare( "3" ) != 0 )
throw std::runtime_error("Empty peak line encountered.");
seqNum = atoi( getWord( in , false ).c_str() );
h = strtod( getWord( in , false ).c_str() , 0 ) ;
k = strtod( getWord( in , false ).c_str() , 0 ) ;
l = strtod( getWord( in , false ).c_str() , 0 ) ;
col = strtod( getWord( in , false ).c_str() , 0 ) ;
row = strtod( getWord( in , false ).c_str() , 0 ) ;
chan = strtod( getWord( in , false ).c_str() , 0 ) ;
L2 = strtod( getWord( in , false ).c_str() , 0 )/100 ;
ScatAng = strtod( getWord( in , false ).c_str() , 0 ) ;
Az = strtod( getWord( in , false ).c_str() , 0 ) ;
wl = strtod( getWord( in , false ).c_str() , 0 ) ;
D = strtod( getWord( in , false ).c_str() , 0 ) ;
IPK = strtod( getWord( in , false ).c_str() , 0 ) ;
Inti = strtod( getWord( in , false ).c_str() , 0 ) ;
SigI = strtod( getWord( in , false ).c_str() , 0 ) ;
iReflag = atoi( getWord( in , false ).c_str() ) ;
// Finish the line and get the first word of next line
readToEndOfLine( in , true );
lastStr = getWord( in , false );
// Find the detector ID from row/col
IInstrument_sptr inst = outWS->getInstrument();
if (!inst) throw std::runtime_error("No instrument in PeaksWorkspace!");
IComponent_sptr bank = inst->getComponentByName(bankName);
if (!bank) throw std::runtime_error("Bank named " + bankName + " not found!");
RectangularDetector_sptr rect = boost::dynamic_pointer_cast<RectangularDetector>(bank);
if (!rect) throw std::runtime_error("Bank named " + bankName + " is not a RectangularDetector!");
IDetector_sptr det = rect->getAtXY(int(col), int(row));
if (!det) throw std::runtime_error("Detector not found on " + bankName + "!");
//Create the peak object
Peak peak(outWS->getInstrument(), det->getID(), wl);
peak.setHKL(h,k,l);
peak.setIntensity(Inti);
peak.setSigmaIntensity(SigI);
peak.setBinCount(IPK);
// Return the peak
return peak;
}
//-----------------------------------------------------------------------------------------------
/** Read the header of each peak block section */
std::string readPeakBlockHeader( std::string lastStr , std::ifstream &in ,
int&run ,int& detName ,
double&chi , double&phi ,
double&omega , double&monCount )
{
std::string s = lastStr;
if( s.length() < 1 && in.good() )//blank line
{
readToEndOfLine( in , true );
s = getWord( in , false );
}
if( s.length() < 1 )
return std::string();
if( s.compare( "0" ) == 0 )
{
readToEndOfLine( in , true );
s = getWord( in , false );
while( s.length() < 1 )
{
readToEndOfLine( in , true );
s = getWord( in , false );
}
}
if( s.compare( std::string( "1" ) ) != 0 )
return s;
run = atoi( getWord( in , false ).c_str() );
detName =atoi( getWord( in , false ).c_str());
chi = strtod( getWord( in , false ).c_str() , 0 );
phi = strtod( getWord( in , false ).c_str() , 0 );
omega = strtod( getWord( in , false ).c_str() , 0 );
monCount = strtod( getWord( in , false ).c_str() , 0 );
readToEndOfLine( in , true );
return getWord( in , false );
}
//special formatted file Use clear peaks to no append
//-----------------------------------------------------------------------------------------------
/** Append the peaks from a .peaks file into the workspace
*
* @param filename :: path to the .peaks file
*/
void LoadPeaksFile::appendFile( PeaksWorkspace_sptr outWS, std::string filename)
{
// Open the file
std::ifstream in( filename.c_str() );
// Read the header, load the instrument
std::string s = readHeader( outWS, in );