Newer
Older
Ronald Fowler
committed
#ifndef SAVENEXUSPROCESSEDTEST_H_
#define SAVENEXUSPROCESSEDTEST_H_
//TODO: Remove this before checking in the test
//#define _WIN64
Ronald Fowler
committed
#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 "MantidDataHandling/LoadEventPreNeXus.h"
Ronald Fowler
committed
#include "MantidAPI/AnalysisDataService.h"
#include "MantidNexus/SaveNexusProcessed.h"
#include "MantidNexus/LoadMuonNexus.h"
#include "MantidNexus/LoadNeXus.h"
Janik Zikovsky
committed
#include "MantidNexus/LoadSNSEventNexus.h"
Ronald Fowler
committed
#include "MantidKernel/UnitFactory.h"
Sofia Antony
committed
#include "MantidDataHandling/LoadRaw3.h"
Janik Zikovsky
committed
#include "MantidTestHelpers/WorkspaceCreationHelper.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:
Janik Zikovsky
committed
SaveNexusProcessedTest()
Ronald Fowler
committed
{
// 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();
Janik Zikovsky
committed
}
Janik Zikovsky
committed
void setUp()
{
Ronald Fowler
committed
Janik Zikovsky
committed
}
void tearDown()
{
}
Ronald Fowler
committed
void testInit()
{
Janik Zikovsky
committed
SaveNexusProcessed algToBeTested;
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(algToBeTested.initialize());
TS_ASSERT( algToBeTested.isInitialized() );
}
void testExec()
{
Janik Zikovsky
committed
SaveNexusProcessed algToBeTested;
Ronald Fowler
committed
if ( !algToBeTested.isInitialized() ) algToBeTested.initialize();
// Should fail because mandatory parameter has not been set
TS_ASSERT_THROWS(algToBeTested.execute(),std::runtime_error);
Janik Zikovsky
committed
// create dummy 2D-workspace
Workspace2D_sptr localWorkspace2D = boost::dynamic_pointer_cast<Workspace2D>
Janik Zikovsky
committed
(WorkspaceFactory::Instance().create("Workspace2D",1,10,10));
Janik Zikovsky
committed
localWorkspace2D->getAxis(0)->unit() = UnitFactory::Instance().create("TOF");
double d = 0.0;
for(int i = 0; i<10; ++i,d+=0.1)
{
localWorkspace2D->dataX(0)[i] = d;
localWorkspace2D->dataY(0)[i] = d;
localWorkspace2D->dataE(0)[i] = d;
}
AnalysisDataService::Instance().addOrReplace("testSpace", localWorkspace2D);
Ronald Fowler
committed
// Now set it...
// specify name of file to save workspace to
algToBeTested.setPropertyValue("InputWorkspace", "testSpace");
Janik Zikovsky
committed
outputFile = "SaveNexusProcessedTest_testExec.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));
outputFile = algToBeTested.getPropertyValue("Filename");
//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") );
Ronald Fowler
committed
TS_ASSERT( ! result.compare(outputFile));
//TS_ASSERT_THROWS_NOTHING( result = algToBeTested.getPropertyValue("EntryName") )
//TS_ASSERT( ! result.compare(entryName));
Ronald Fowler
committed
Gigg, Martyn Anthony
committed
// changed so that 1D workspaces are no longer written.
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
if(clearfiles) Poco::File(outputFile).remove();
Ronald Fowler
committed
Janik Zikovsky
committed
AnalysisDataService::Instance().remove("testSpace");
Ronald Fowler
committed
}
Janik Zikovsky
committed
void testExecOnLoadraw()
Ronald Fowler
committed
{
Janik Zikovsky
committed
SaveNexusProcessed algToBeTested;
std::string inputFile = "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() );
Sofia Antony
committed
Ronald Fowler
committed
//
// 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
Janik Zikovsky
committed
outputFile = "SaveNexusProcessedTest_testExecOnLoadraw.nxs";
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
//entryName = "entry4";
Ronald Fowler
committed
dataName = "spectra";
Janik Zikovsky
committed
title = "A save of a workspace from Loadraw file";
algToBeTested.setPropertyValue("Filename", outputFile);
Janik Zikovsky
committed
//algToBeTested.setPropertyValue("EntryName", entryName);
Ronald Fowler
committed
algToBeTested.setPropertyValue("Title", title);
Janik Zikovsky
committed
algToBeTested.setPropertyValue("Append", "0");
Janik Zikovsky
committed
outputFile = algToBeTested.getPropertyValue("Filename");
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
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
Janik Zikovsky
committed
if(clearfiles) remove(outputFile.c_str());
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(outputSpace));
}
Janik Zikovsky
committed
void xtestExecOnMuon()
{
SaveNexusProcessed algToBeTested;
Janik Zikovsky
committed
//This test does not compile on Windows64 as is does not support HDF4 files
#ifndef _WIN64
LoadNexus nxLoad;
std::string outputSpace,inputFile;
nxLoad.initialize();
// Now set required filename and output workspace name
inputFile = "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() );
Ronald Fowler
committed
//
// get workspace
//
Workspace_sptr output;
TS_ASSERT_THROWS_NOTHING(output = AnalysisDataService::Instance().retrieve(outputSpace));
Workspace2D_sptr output2D = boost::dynamic_pointer_cast<Workspace2D>(output);
Janik Zikovsky
committed
// this would make all X's separate
// output2D->dataX(22)[3]=0.55;
Ronald Fowler
committed
//
if ( !algToBeTested.isInitialized() ) algToBeTested.initialize();
algToBeTested.setPropertyValue("InputWorkspace", outputSpace);
// specify name of file to save workspace to
Janik Zikovsky
committed
outputFile = "SaveNexusProcessedTest_testExecOnMuon.nxs";
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
//entryName = "entry4";
Ronald Fowler
committed
dataName = "spectra";
Janik Zikovsky
committed
title = "A save of a 2D workspace from Muon file";
algToBeTested.setPropertyValue("Filename", outputFile);
Janik Zikovsky
committed
outputFile = algToBeTested.getPropertyValue("Filename");
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
Janik Zikovsky
committed
//algToBeTested.setPropertyValue("EntryName", entryName);
Ronald Fowler
committed
algToBeTested.setPropertyValue("Title", title);
algToBeTested.setPropertyValue("Append", "0");
Janik Zikovsky
committed
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
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
Janik Zikovsky
committed
// try writing data again
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));
Janik Zikovsky
committed
#endif /*_WIN64*/
}
Ronald Fowler
committed
Janik Zikovsky
committed
void xtestExecOnMuonXml()
Ronald Fowler
committed
{
Janik Zikovsky
committed
SaveNexusProcessed algToBeTested;
//This test does not compile on Windows64 as is does not support HDF4 files
Janik Zikovsky
committed
// std::string s;
//std::getline(std::cin,s);
Sofia Antony
committed
Ronald Fowler
committed
LoadNexus nxLoad;
Ronald Fowler
committed
std::string outputSpace,inputFile;
nxLoad.initialize();
// Now set required filename and output workspace name
nxLoad.setPropertyValue("Filename", inputFile);
Janik Zikovsky
committed
inputFile = nxLoad.getPropertyValue("Filename"); //Get the absolute path
Ronald Fowler
committed
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 = "SaveNexusProcessedTest_testExecOnMuonXml.xml";
//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);
outputFile = algToBeTested.getPropertyValue("Filename");
Janik Zikovsky
committed
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
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
Janik Zikovsky
committed
// try writing data again
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(algToBeTested.execute());
TS_ASSERT( algToBeTested.isExecuted() );
Janik Zikovsky
committed
if(clearfiles)
{
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
}
Ronald Fowler
committed
TS_ASSERT_THROWS_NOTHING(AnalysisDataService::Instance().remove(outputSpace));
Ronald Fowler
committed
}
Janik Zikovsky
committed
Janik Zikovsky
committed
static EventWorkspace_sptr do_testExec_EventWorkspaces(std::string filename_root, EventType type,
bool makeDifferentTypes=false, bool clearfiles=false)
Janik Zikovsky
committed
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
std::vector< std::vector<int> > groups(5);
groups[0].push_back(10);
groups[0].push_back(11);
groups[0].push_back(12);
groups[1].push_back(20);
groups[2].push_back(30);
groups[2].push_back(31); // Switch the event type
groups[3].push_back(40);
groups[4].push_back(50);
EventWorkspace_sptr WS = WorkspaceCreationHelper::CreateGroupedEventWorkspace(groups, 100, 1.0);
WS->getEventList(3).clear();
// Switch the event type
if (makeDifferentTypes)
{
WS->getEventList(0).switchTo(TOF);
WS->getEventList(1).switchTo(WEIGHTED);
WS->getEventList(2).switchTo(WEIGHTED_NOTIME);
WS->getEventList(4).switchTo(WEIGHTED);
}
else
{
for (size_t wi=0; wi < WS->getNumberHistograms(); wi++)
WS->getEventList(wi).switchTo(type);
}
Janik Zikovsky
committed
Janik Zikovsky
committed
SaveNexusProcessed alg;
alg.initialize();
Janik Zikovsky
committed
Janik Zikovsky
committed
// Now set it...
alg.setProperty("InputWorkspace", boost::dynamic_pointer_cast<MatrixWorkspace>(WS));
Janik Zikovsky
committed
Janik Zikovsky
committed
// specify name of file to save workspace to
std::ostringstream mess;
mess << filename_root << static_cast<int>(type) << ".nxs";
std::string outputFile = mess.str();
std::string dataName = "spectra";
std::string title = "A simple workspace saved in Processed Nexus format";
Janik Zikovsky
committed
Janik Zikovsky
committed
alg.setPropertyValue("Filename", outputFile);
outputFile = alg.getPropertyValue("Filename");
alg.setPropertyValue("Title", title);
Janik Zikovsky
committed
Janik Zikovsky
committed
// Clear the existing file, if any
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
alg.execute();
TS_ASSERT( alg.isExecuted() );
Janik Zikovsky
committed
Janik Zikovsky
committed
TS_ASSERT( Poco::File(outputFile).exists() );
Janik Zikovsky
committed
Janik Zikovsky
committed
if(clearfiles) Poco::File(outputFile).remove();
Janik Zikovsky
committed
Janik Zikovsky
committed
return WS;
}
Janik Zikovsky
committed
Janik Zikovsky
committed
void testExec_EventWorkspace_TofEvent()
{
do_testExec_EventWorkspaces("SaveNexusProcessed_", TOF, false, clearfiles);
}
Janik Zikovsky
committed
void testExec_EventWorkspace_WeightedEvent()
{
do_testExec_EventWorkspaces("SaveNexusProcessed_", WEIGHTED, false, clearfiles);
}
Janik Zikovsky
committed
void testExec_EventWorkspace_WeightedEventNoTime()
{
do_testExec_EventWorkspaces("SaveNexusProcessed_", WEIGHTED_NOTIME, false, clearfiles);
}
Janik Zikovsky
committed
void testExec_EventWorkspace_DifferentTypes()
{
do_testExec_EventWorkspaces("SaveNexusProcessed_DifferentTypes_", WEIGHTED_NOTIME, true, clearfiles);
}
Janik Zikovsky
committed
void testExec_LoadedEventWorkspace()
{
Janik Zikovsky
committed
Janik Zikovsky
committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//----- Now we re-load with precounting and compare memory use ----
LoadSNSEventNexus ld2;
std::string outws_name = "SaveNexusProcessed_Loaded";
ld2.initialize();
ld2.setPropertyValue("Filename","CNCS_7860_event.nxs");
ld2.setPropertyValue("OutputWorkspace",outws_name);
ld2.setPropertyValue("Precount", "1");
ld2.execute();
TS_ASSERT( ld2.isExecuted() );
SaveNexusProcessed alg;
alg.initialize();
alg.setPropertyValue("InputWorkspace", outws_name);
outputFile = "SaveNexusProcessed_Loaded.nxs";
dataName = "spectra";
title = "A simple workspace saved in Processed Nexus format";
alg.setPropertyValue("Filename", outputFile);
outputFile = alg.getPropertyValue("Filename");
alg.setPropertyValue("Title", title);
// Clear the existing file, if any
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
alg.execute();
TS_ASSERT( alg.isExecuted() );
TS_ASSERT( Poco::File(outputFile).exists() );
if (clearfiles)
if( Poco::File(outputFile).exists() ) Poco::File(outputFile).remove();
}
Janik Zikovsky
committed
Ronald Fowler
committed
private:
std::string outputFile;
std::string entryName;
std::string dataName;
std::string title;
Workspace2D myworkspace;
Sofia Antony
committed
Mantid::DataHandling::LoadRaw3 loader;
Ronald Fowler
committed
std::string inputFile;
std::string outputSpace;
Ronald Fowler
committed
};
#endif /*SAVENEXUSPROCESSEDTEST_H_*/