Newer
Older
Anders Markvardsen
committed
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataHandling/LoadEmptyInstrument.h"
#include "MantidDataObjects/Workspace2D.h"
Russell Taylor
committed
#include "MantidAPI/SpectraDetectorMap.h"
Anders Markvardsen
committed
#include "MantidKernel/ConfigService.h"
Gigg, Martyn Anthony
committed
#include "MantidKernel/FileProperty.h"
Anders Markvardsen
committed
Gigg, Martyn Anthony
committed
#include "Poco/Path.h"
Anders Markvardsen
committed
#include <cmath>
namespace Mantid
{
namespace DataHandling
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadEmptyInstrument)
using namespace Kernel;
using namespace API;
/// Empty default constructor
LoadEmptyInstrument::LoadEmptyInstrument() : Algorithm()
{}
Anders Markvardsen
committed
/// Initialisation method.
void LoadEmptyInstrument::init()
{
declareProperty(new FileProperty("Filename","", FileProperty::Load, std::vector<std::string>(1, "xml")),
Gigg, Martyn Anthony
committed
"The filename (including its full or relative path) of an ISIS instrument\n"
"defintion file");
Steve Williams
committed
declareProperty(
new WorkspaceProperty<DataObjects::Workspace2D>("OutputWorkspace","",Direction::Output),
"The name of the workspace in which to store the imported instrument" );
Anders Markvardsen
committed
BoundedValidator<double> *mustBePositive = new BoundedValidator<double>();
mustBePositive->setLower(0.0);
declareProperty("DetectorValue",1.0, mustBePositive,
Steve Williams
committed
"This value affects the colour of the detectors in the instrument\n"
"display window (default 1)" );
declareProperty("MonitorValue",2.0, mustBePositive->clone(),
Steve Williams
committed
"This value affects the colour of the monitors in the instrument\n"
"display window (default 2)");
Anders Markvardsen
committed
}
/** Executes the algorithm. Reading in the file and creating and populating
* the output workspace
*
* @throw Exception::FileError If the RAW file cannot be found/opened
* @throw std::invalid_argument If the optional properties are set to invalid values
*/
void LoadEmptyInstrument::exec()
{
// Get other properties
const double detector_value = getProperty("DetectorValue");
const double monitor_value = getProperty("MonitorValue");
Anders Markvardsen
committed
// load the instrument into this workspace
IInstrument_sptr instrument = this->runLoadInstrument();
Anders Markvardsen
committed
// Get detectors stored in instrument and create dummy c-arrays for the purpose
// of calling method of SpectraDetectorMap
const std::map<int, Geometry::IDetector_sptr> detCache = instrument->getDetectors();
const int number_spectra = static_cast<int>(detCache.size());
// Now create the outputworkspace and copy over the instrument object
DataObjects::Workspace2D_sptr localWorkspace =
boost::dynamic_pointer_cast<DataObjects::Workspace2D>(WorkspaceFactory::Instance().create("Workspace2D",number_spectra,2,1));
localWorkspace->setInstrument(instrument);
Anders Markvardsen
committed
int *spec = new int[number_spectra];
int *udet = new int[number_spectra];
std::map<int, Geometry::IDetector_sptr>::const_iterator it;
Anders Markvardsen
committed
int counter = 0;
for ( it = detCache.begin(); it != detCache.end(); ++it )
{
counter++;
spec[counter-1] = counter; // have no feeling of how best to number these spectra
// and sure whether the way it is done here is the best way...
udet[counter-1] = it->first;
}
Russell Taylor
committed
localWorkspace->mutableSpectraMap().populate(spec,udet,number_spectra);
Anders Markvardsen
committed
counter = 0;
DataObjects::Histogram1D::RCtype x,v,v_monitor;
x.access().resize(2); x.access()[0]=1.0; x.access()[1]=2.0;
v.access().resize(1); v.access()[0]=detector_value;
v_monitor.access().resize(1); v_monitor.access()[0]=monitor_value;
for ( it = detCache.begin(); it != detCache.end(); ++it )
Anders Markvardsen
committed
{
if ( (it->second)->isMonitor() )
localWorkspace->setData(counter, v_monitor, v_monitor);
localWorkspace->setData(counter, v, v);
Anders Markvardsen
committed
localWorkspace->setX(counter, x);
localWorkspace->getAxis(1)->spectraNo(counter)= counter+1; // Not entirely sure if this 100% ok
Anders Markvardsen
committed
++counter;
}
Anders Markvardsen
committed
// populate the workspace parameter map the best you can
localWorkspace->populateInstrumentParameters();
Anders Markvardsen
committed
setProperty("OutputWorkspace",localWorkspace);
// Clean up
delete[] spec;
delete[] udet;
}
/// Run the sub-algorithm LoadInstrument (or LoadInstrumentFromRaw)
API::IInstrument_sptr LoadEmptyInstrument::runLoadInstrument()
Anders Markvardsen
committed
{
const std::string filename = getPropertyValue("Filename");
Anders Markvardsen
committed
// Determine the search directory for XML instrument definition files (IDFs)
std::string directoryName = Kernel::ConfigService::Instance().getString("instrumentDefinition.directory");
Gigg, Martyn Anthony
committed
if ( directoryName.empty() )
{
// This is the assumed deployment directory for IDFs, where we need to be relative to the
// directory of the executable, not the current working directory.
directoryName = Poco::Path(Mantid::Kernel::ConfigService::Instance().getBaseDir()).resolve("../Instrument").toString();
Gigg, Martyn Anthony
committed
}
const std::string::size_type stripPath = filename.find_last_of("\\/");
std::string fullPathIDF;
if (stripPath != std::string::npos)
{
fullPathIDF = filename; // since if path already provided don't modify m_filename
}
else
{
//std::string instrumentID = m_filename.substr(stripPath+1);
fullPathIDF = directoryName + "/" + filename;
IAlgorithm_sptr loadInst = createSubAlgorithm("LoadInstrument",0,1);
loadInst->setPropertyValue("Filename", fullPathIDF);
MatrixWorkspace_sptr ws = WorkspaceFactory::Instance().create("WorkspaceSingleValue",1,1,1);
loadInst->setProperty<MatrixWorkspace_sptr>("Workspace",ws);
Anders Markvardsen
committed
// Now execute the sub-algorithm. Catch and log any error, but don't stop.
try
{
loadInst->execute();
}
Anders Markvardsen
committed
{
g_log.error("Unable to successfully run LoadInstrument sub-algorithm");
}
return ws->getInstrument();
Anders Markvardsen
committed
}
} // namespace DataHandling
} // namespace Mantid