Newer
Older
// LoadNeXus
// @author Freddie Akeroyd, STFC ISIS Faility
Ronald Fowler
committed
// @author Ronald Fowler, e_Science - updated to be wrapper to either LoadMuonNeuxs or LoadIsisNexus
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include "MantidDataObjects/Workspace2D.h"
Ronald Fowler
committed
#include "MantidKernel/ArrayProperty.h"
#include <cmath>
#include <boost/shared_ptr.hpp>
namespace Mantid
{
{
// Register the algorithm into the algorithm factory
DECLARE_ALGORITHM(LoadNeXus)
using namespace Kernel;
using namespace API;
using namespace DataObjects;
Logger& LoadNeXus::g_log = Logger::get("LoadNeXus");
/// Empty default constructor
Ronald Fowler
committed
LoadNeXus::LoadNeXus() :
Algorithm(), m_filename()
{
}
/** Initialisation method.
Russell Taylor
committed
*
*/
void LoadNeXus::init()
{
Ronald Fowler
committed
// Declare required input parameters for all sub algorithms
Ronald Fowler
committed
std::vector<std::string> exts;
exts.push_back("NXS");
exts.push_back("nxs");
declareProperty("Filename","",new FileValidator(exts));
declareProperty(new WorkspaceProperty<Workspace2D>("OutputWorkspace","",Direction::Output));
Ronald Fowler
committed
// Declare optional input parameters
BoundedValidator<int> *mustBePositive = new BoundedValidator<int>();
mustBePositive->setLower(0);
declareProperty("spectrum_min",0, mustBePositive);
declareProperty("spectrum_max",0, mustBePositive->clone());
declareProperty(new ArrayProperty<int>("spectrum_list"));
Russell Taylor
committed
/** Executes the algorithm. Reading in the file and creating and populating
* the output workspace
Russell Taylor
committed
*
* @throw runtime_error Thrown if algorithm cannot execute
*/
void LoadNeXus::exec()
{
Ronald Fowler
committed
// Retrieve the filename and output workspace name from the properties
m_filename = getPropertyValue("Filename");
Ronald Fowler
committed
m_workspace = getPropertyValue("OutputWorkspace");
Russell Taylor
committed
Ronald Fowler
committed
// Test the given filename to see if it contains the field "analysis" with value "muonTD"
// within the first NXentry.
// If so, assume it is a Muon Nexus file (version 1) and pass to the LoadMuonNexus algorithm
// Otherwise try LoadIsisNexus.
std::string dataName="analysis", muonTd="muonTD";
std::string value;
NeXusUtils *nexusFile= new NeXusUtils();
Ronald Fowler
committed
std::vector<std::string> entryName,definition;
int count=nexusFile->getNexusEntryTypes(m_filename,entryName,definition);
if( definition[0]==muonTd )
Ronald Fowler
committed
{
runLoadMuonNexus();
}
Ronald Fowler
committed
else if( entryName[0]=="mantid_workspace_1" )
Ronald Fowler
committed
{
Ronald Fowler
committed
runLoadNexusProcessed();
Ronald Fowler
committed
}
else
{
g_log.error("Unable to open file " + m_filename);
throw Exception::FileError("Unable to open File:" , m_filename);
}
return;
}
Ronald Fowler
committed
void LoadNeXus::runLoadMuonNexus()
{
Algorithm_sptr loadMuonNexus = createSubAlgorithm("LoadMuonNexus");
// Pass through the same input filename
loadMuonNexus->setPropertyValue("Filename",m_filename);
// Set the workspace property
Ronald Fowler
committed
std::string outputWorkspace="OutputWorkspace";
loadMuonNexus->setPropertyValue(outputWorkspace,m_workspace);
Ronald Fowler
committed
//
Property *specList = getProperty("spectrum_list");
if( !(specList->isDefault()) )
loadMuonNexus->setPropertyValue("spectrum_list",getPropertyValue("spectrum_list"));
//
Property *specMax = getProperty("spectrum_max");
if( !(specMax->isDefault()) )
{
loadMuonNexus->setPropertyValue("spectrum_max",getPropertyValue("spectrum_max"));
loadMuonNexus->setPropertyValue("spectrum_min",getPropertyValue("spectrum_min"));
}
Ronald Fowler
committed
// Now execute the sub-algorithm. Catch and log any error, but don't stop.
try
{
loadMuonNexus->execute();
}
catch (std::runtime_error& err)
{
g_log.error("Unable to successfully run LoadMuonNexus sub-algorithm");
}
if ( ! loadMuonNexus->isExecuted() ) g_log.error("Unable to successfully run LoadMuonNexus sub-algorithm");
Ronald Fowler
committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Get pointer to the workspace created
m_localWorkspace=loadMuonNexus->getProperty(outputWorkspace);
setProperty<Workspace2D_sptr>(outputWorkspace,m_localWorkspace);
//
// copy pointers to any new output workspaces created by alg LoadMuonNexus to alg LoadNexus
// Loop through names of form "OutputWorkspace<n>" where <n> is integer from 2 upwards
// until name not found
//
int period=0;
bool noError=true;
while(noError)
{
std::stringstream suffix;
period++;
suffix << (period+1);
std::string opWS = outputWorkspace + suffix.str();
std::string WSName = m_workspace + "_" + suffix.str();
try
{
m_localWorkspace=loadMuonNexus->getProperty(opWS);
declareProperty(new WorkspaceProperty<DataObjects::Workspace2D>(opWS,WSName,Direction::Output));
setProperty<Workspace2D_sptr>(opWS,m_localWorkspace);
}
catch (Exception::NotFoundError)
{
noError=false;
}
}
Ronald Fowler
committed
}
Russell Taylor
committed
Ronald Fowler
committed
void LoadNeXus::runLoadNexusProcessed()
Ronald Fowler
committed
{
Ronald Fowler
committed
} // namespace Mantid