Newer
Older
Ronald Fowler
committed
#ifndef SAVENEXUSPROCESSEDTEST_H_
#define SAVENEXUSPROCESSEDTEST_H_
#include <fstream>
#include <cxxtest/TestSuite.h>
// These includes seem to make the difference between initialization of the
// workspace names (workspace2D/1D etc), instrument classes and not for this test case.
#include "MantidDataObjects/WorkspaceSingleValue.h"
#include "MantidDataHandling/LoadInstrument.h"
//
#include "MantidDataHandling/LoadRaw.h"
#include "MantidAPI/FrameworkManager.h"
#include "MantidAPI/WorkspaceFactory.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidAPI/AnalysisDataService.h"
#include "MantidNexus/SaveNexusProcessed.h"
#include "MantidNexus/LoadMuonNexus.h"
#include "MantidNexus/LoadNeXus.h"
#include "MantidKernel/UnitFactory.h"
Gigg, Martyn Anthony
committed
#include "Poco/File.h"
Ronald Fowler
committed
using namespace Mantid::API;
using namespace Mantid::Kernel;
using namespace Mantid::NeXus;
using namespace Mantid::DataObjects;
class SaveNexusProcessedTest : public CxxTest::TestSuite
{
public:
SaveNexusProcessedTest()
{
// clearfiles - make true for SVN as dont want to leave on build server.
// Unless the file "KEEP_NXS_FILES" exists, then clear up nxs files
Gigg, Martyn Anthony
committed
Poco::File file("KEEP_NXS_FILES");
clearfiles = !file.exists();
//clearfiles=true;
Ronald Fowler
committed
// create dummy 2D-workspace
std::vector<double> lVecX; for(double d=0.0; d<0.95; d=d+0.1) lVecX.push_back(d);
std::vector<double> lVecY; for(double d=0.0; d<0.95; d=d+0.1) lVecY.push_back(d);
std::vector<double> lVecE; for(double d=0.0; d<0.95; d=d+0.1) lVecE.push_back(d);
Workspace2D_sptr localWorkspace2D = boost::dynamic_pointer_cast<Workspace2D>
Ronald Fowler
committed
(WorkspaceFactory::Instance().create("Workspace2D",1,10,10));
localWorkspace2D->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
localWorkspace2D->setX(0,lVecX);
localWorkspace2D->setData(0,lVecY, lVecE);
AnalysisDataService::Instance().add("testSpace", localWorkspace2D);
}
void testInit()
{
TS_ASSERT_THROWS_NOTHING(algToBeTested.initialize());
TS_ASSERT( algToBeTested.isInitialized() );
}
void testExec()
{
if ( !algToBeTested.isInitialized() ) algToBeTested.initialize();
// Should fail because mandatory parameter has not been set
TS_ASSERT_THROWS(algToBeTested.execute(),std::runtime_error);
// Now set it...
// specify name of file to save workspace to
algToBeTested.setPropertyValue("InputWorkspace", "testSpace");
outputFile = "testOfSaveNexusProcessed.nxs";
//entryName = "test";
Ronald Fowler
committed
dataName = "spectra";
title = "A simple workspace saved in Processed Nexus format";
TS_ASSERT_THROWS_NOTHING(algToBeTested.setPropertyValue("FileName", outputFile));
//algToBeTested.setPropertyValue("EntryName", entryName);
Ronald Fowler
committed
algToBeTested.setPropertyValue("Title", title);
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
Ronald Fowler
committed
std::string result;
TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("FileName") )
TS_ASSERT( ! result.compare(outputFile));
//TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("EntryName") )
//TS_ASSERT( ! result.compare(entryName));
Ronald Fowler
committed
// changed so that 1D workspaces are no longer written.
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
Ronald Fowler
committed
}
void testExecOnMuon()
{
Ronald Fowler
committed
LoadNexus nxLoad;
Ronald Fowler
committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
std::string outputSpace,inputFile;
nxLoad.initialize();
// Now set required filename and output workspace name
inputFile = "../../../../Test/Nexus/emu00006473.nxs";
nxLoad.setPropertyValue("FileName", inputFile);
outputSpace="outer";
nxLoad.setPropertyValue("OutputWorkspace", outputSpace);
//
// Test execute to read file and populate workspace
//
TS_ASSERT_THROWS_NOTHING(nxLoad.execute());
TS_ASSERT( nxLoad.isExecuted() );
//
// get workspace
//
Workspace_sptr output;
TS_ASSERT_THROWS_NOTHING(output = AnalysisDataService::Instance().retrieve(outputSpace));
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
// this would make all X's separate
// output2D->dataX(22)[3]=0.55;
//
if ( !algToBeTested.isInitialized() ) algToBeTested.initialize();
algToBeTested.setPropertyValue("InputWorkspace", outputSpace);
// specify name of file to save workspace to
outputFile = "testOfSaveNexusProcessed2.nxs";
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
//entryName = "entry4";
Ronald Fowler
committed
dataName = "spectra";
title = "A save of a 2D workspace from Muon file";
algToBeTested.setPropertyValue("FileName", outputFile);
//algToBeTested.setPropertyValue("EntryName", entryName);
Ronald Fowler
committed
algToBeTested.setPropertyValue("Title", title);
std::string result;
TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("Filename") );
TS_ASSERT( ! result.compare(outputFile));
//TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("EntryName") );
//TS_ASSERT( ! result.compare(entryName));
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
// try writing data again
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
if(clearfiles) Poco::File(outputFile).remove();
Ronald Fowler
committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(outputSpace));
}
void testExecOnLoadraw()
{
std::string inputFile = "../../../../Test/Data/HET15869.RAW";
TS_ASSERT_THROWS_NOTHING( loader.initialize());
TS_ASSERT( loader.isInitialized() );
loader.setPropertyValue("Filename", inputFile);
outputSpace = "outer4";
loader.setPropertyValue("OutputWorkspace", outputSpace);
TS_ASSERT_THROWS_NOTHING(loader.execute());
TS_ASSERT( loader.isExecuted() );
//
// get workspace
//
Workspace_sptr output;
TS_ASSERT_THROWS_NOTHING(output = AnalysisDataService::Instance().retrieve(outputSpace));
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
//
if ( !algToBeTested.isInitialized() ) algToBeTested.initialize();
algToBeTested.setPropertyValue("InputWorkspace", outputSpace);
// specify name of file to save workspace to
outputFile = "testSaveFromLoadraw.nxs";
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
//entryName = "entry4";
Ronald Fowler
committed
dataName = "spectra";
title = "A save of a workspace from Loadraw file";
algToBeTested.setPropertyValue("FileName", outputFile);
//algToBeTested.setPropertyValue("EntryName", entryName);
Ronald Fowler
committed
algToBeTested.setPropertyValue("Title", title);
std::string result;
TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("Filename") );
TS_ASSERT( ! result.compare(outputFile));
//TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("EntryName") );
//TS_ASSERT( ! result.compare(entryName));
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
if(clearfiles) Poco::File(outputFile).remove();
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(outputSpace));
}
void testExecOnMuonXml()
{
Ronald Fowler
committed
LoadNexus nxLoad;
Ronald Fowler
committed
std::string outputSpace,inputFile;
nxLoad.initialize();
// Now set required filename and output workspace name
inputFile = "../../../../Test/Nexus/emu00006473.nxs";
nxLoad.setPropertyValue("FileName", inputFile);
outputSpace="outer";
nxLoad.setPropertyValue("OutputWorkspace", outputSpace);
//
// Test execute to read file and populate workspace
//
TS_ASSERT_THROWS_NOTHING(nxLoad.execute());
TS_ASSERT( nxLoad.isExecuted() );
//
// get workspace
//
Workspace_sptr output;
TS_ASSERT_THROWS_NOTHING(output = AnalysisDataService::Instance().retrieve(outputSpace));
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
if ( !algToBeTested.isInitialized() ) algToBeTested.initialize();
algToBeTested.setPropertyValue("InputWorkspace", outputSpace);
// specify name of file to save workspace to
outputFile = "testOfSaveNexusProcessed2.xml";
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
//entryName = "entry4";
Ronald Fowler
committed
dataName = "spectra";
title = "A save of a 2D workspace from Muon file";
algToBeTested.setPropertyValue("FileName", outputFile);
//algToBeTested.setPropertyValue("EntryName", entryName);
Ronald Fowler
committed
algToBeTested.setPropertyValue("Title", title);
std::string result;
TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("Filename") );
TS_ASSERT( ! result.compare(outputFile));
//TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("EntryName") );
//TS_ASSERT( ! result.compare(entryName));
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
// try writing data again
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
Gigg, Martyn Anthony
committed
if(clearfiles) Poco::File(outputFile).remove();
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(outputSpace));
}
private:
SaveNexusProcessed algToBeTested;
std::string outputFile;
std::string entryName;
std::string dataName;
std::string title;
Workspace2D myworkspace;
Mantid::DataHandling::LoadRaw loader;
std::string inputFile;
std::string outputSpace;
Ronald Fowler
committed
};
#endif /*SAVENEXUSPROCESSEDTEST_H_*/