Large diffs are not rendered by default.

@@ -0,0 +1,89 @@
#ifndef MANTID_CUSTOMINTERFACES_EVENT_NEXUS_FILE_MEMENTO
#define MANTID_CUSTOMINTERFACES_EVENT_NEXUS_FILE_MEMENTO

#include "MantidQtCustomInterfaces/WorkspaceMemento.h"

namespace MantidQt
{
namespace CustomInterfaces
{
/** @class EventNexusFileMemento
A workspace memento refering to a Event nexus File on disk
Copyright © 2011-12 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport EventNexusFileMemento : public WorkspaceMemento
{
public:
/**
@param fileName : path + name of the file holding the workspace
*/
EventNexusFileMemento(std::string fileName);
/**
Getter for the id of the workspace
@return the id of the workspace
*/
virtual std::string getId() const;
/**
Getter for the type of location where the workspace is stored
@ return the location type
*/
virtual std::string locationType() const;
/**
Check that the workspace has not been deleted since instantiating this memento
@return true if still in specified location
*/
virtual bool checkStillThere() const;
/**
Getter for the workspace itself
@returns the workspace
@throw if workspace has been moved since instantiation.
*/
virtual Mantid::API::Workspace_sptr fetchIt() const;
///Clean-up operations
virtual void cleanUp();
/// Destructor
virtual ~EventNexusFileMemento();

/*
Location type associated with this type.
@return string describing location
*/
static std::string locType()
{
return "On Disk";
}

//Apply actions wrapped up in this memento.
virtual Mantid::API::Workspace_sptr applyActions();

private:
/// Helper method to delete a workspace out of memory after loading.
void dumpIt(const std::string& name);
/// Path + name of file containing workspace to use.
std::string m_fileName;
// Id of the workspace in the ADS.
std::string m_adsID;
};

}
}
#endif
@@ -1,5 +1,5 @@
#ifndef MANTID_CUSTOMINTERFACES_WORKSPACE_ON_DISK_H_
#define MANTID_CUSTOMINTERFACES_WORKSPACE_ON_DISK_H_
#ifndef MANTID_CUSTOMINTERFACES_RAW_FILE_ON_DISK_H_
#define MANTID_CUSTOMINTERFACES_RAW_FILE_ON_DISK_H_

#include "MantidQtCustomInterfaces/WorkspaceMemento.h"

@@ -10,7 +10,7 @@ namespace MantidQt
/** @class RawFileMemento
A workspace memento refering to a Raw File on disk
Copyright &copy; 2007-8 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
Copyright &copy; 2011-12 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
@@ -6,6 +6,7 @@
#include "MantidQtCustomInterfaces/WorkspaceMemento.h"
#include "MantidQtCustomInterfaces/WorkspaceInADS.h"
#include "MantidQtCustomInterfaces/RawFileMemento.h"
#include "MantidQtCustomInterfaces/EventNexusFileMemento.h"
#include "MantidQtCustomInterfaces/WorkspaceMemento.h"

#include "MantidAPI/WorkspaceFactory.h"
@@ -39,6 +40,7 @@
#include <QMessageBox>
#include <QFileDialog>
#include <QRadioButton>
#include <QCheckBox>
#include <strstream>
#include <boost/regex.hpp>
#include <boost/algorithm/string.hpp>
@@ -94,14 +96,16 @@ void CreateMDWorkspace::initLayout()
m_uiForm.setupUi(this);
connect(m_uiForm.btn_create, SIGNAL(clicked()), this, SLOT(createMDWorkspaceClicked()));
connect(m_uiForm.btn_add_workspace, SIGNAL(clicked()), this, SLOT(addWorkspaceClicked()));
connect(m_uiForm.btn_add_file, SIGNAL(clicked()), this, SLOT(addFileClicked()));
connect(m_uiForm.btn_add_raw_file, SIGNAL(clicked()), this, SLOT(addRawFileClicked()));
connect(m_uiForm.btn_add_event_nexus_file, SIGNAL(clicked()), this, SLOT(addEventNexusFileClicked()));
connect(m_uiForm.btn_remove_workspace, SIGNAL(clicked()), this, SLOT(removeSelectedClicked()));
connect(m_uiForm.btn_set_ub_matrix, SIGNAL(clicked()), this, SLOT(setUBMatrixClicked()));
connect(m_uiForm.btn_find_ub_matrix, SIGNAL(clicked()), this, SLOT(findUBMatrixClicked()));
connect(m_uiForm.btn_create, SIGNAL(clicked()), this, SLOT(createMDWorkspaceClicked()));
connect(m_uiForm.btn_set_goniometer, SIGNAL(clicked()), this, SLOT(setGoniometerClicked()));
//Set MVC Model
m_uiForm.tableView->setModel(m_model);
this->setWindowTitle("Create MD Workspaces");
}

/*
@@ -278,7 +282,7 @@ void CreateMDWorkspace::addUniqueMemento(WorkspaceMemento_sptr candidate)
/*
Add a raw file from the ADS
*/
void CreateMDWorkspace::addFileClicked()
void CreateMDWorkspace::addRawFileClicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
@@ -299,6 +303,30 @@ void CreateMDWorkspace::addFileClicked()
}
}

/*
Add an Event nexus file from the ADS
*/
void CreateMDWorkspace::addEventNexusFileClicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
"/home",
tr("Event Nexus files (*.nxs)"));

std::string name = fileName.toStdString();
if(!name.empty())
{
try
{
WorkspaceMemento_sptr candidate(new EventNexusFileMemento(name));
addUniqueMemento(candidate);
}
catch(std::invalid_argument& arg)
{
this->runConfirmation(arg.what());
}
}
}

/**
Handler for setting the goniometer.
*/
@@ -386,6 +414,25 @@ int CreateMDWorkspace::runConfirmation(const std::string& message)
void CreateMDWorkspace::createMDWorkspaceClicked()
{
//Launch dialog

//1) Run a top-level dialog similar to ConvertToMDEvents. Extract all required arguments.

//2) Run ConvertToMDEvents on each workspace.
for(int i = 0; i < m_data.size(); i++)
{
//Workspace_sptr ws = m_data[i]->applyActions();
//QString command = ""
// "";
}


//3) Run Merge algorithm (if required)
if(m_uiForm.ck_merge->isChecked())
{
//QString command = ""
// "";
}

}


@@ -0,0 +1,146 @@
#include "MantidQtCustomInterfaces/EventNexusFileMemento.h"
#include "MantidKernel/Matrix.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/IEventWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"
#include <iostream>
#include <fstream>
#include <boost/regex.hpp>

using namespace Mantid::API;

namespace MantidQt
{
namespace CustomInterfaces
{
/**
Constructor
@param fileName : path + name of the file to load
*/
EventNexusFileMemento::EventNexusFileMemento(std::string fileName) : m_fileName(fileName)
{
boost::regex pattern("(NXS)$", boost::regex_constants::icase);

if(!boost::regex_search(fileName, pattern))
{
std::string msg = "EventNexusFileMemento:: Unknown File extension on: " + fileName;
throw std::invalid_argument(msg);
}

if(!checkStillThere())
{
throw std::runtime_error("EventNexusFileMemento:: File doesn't exist");
}

std::vector<std::string> strs;
boost::split(strs, m_fileName, boost::is_any_of("/,\\"));
m_adsID = strs.back();
m_adsID = m_adsID.substr(0, m_adsID.find('.'));

//Generate an initial report.
IEventWorkspace_sptr ws = boost::dynamic_pointer_cast<IEventWorkspace>(fetchIt());
if(ws->mutableSample().hasOrientedLattice())
{
std::vector<double> ub = ws->mutableSample().getOrientedLattice().getUB().get_vector();
this->setUB(ub[0], ub[1], ub[2], ub[3], ub[4], ub[5], ub[6], ub[7], ub[8]);
}
cleanUp();
}

/**
Getter for the id of the workspace
@return the id of the workspace
*/
std::string EventNexusFileMemento::getId() const
{
return m_adsID;
}

/**
Getter for the type of location where the workspace is stored
@ return the location type
*/
std::string EventNexusFileMemento::locationType() const
{
return locType();
}

/**
Check that the workspace has not been deleted since instantiating this memento
@return true if still in specified location
*/
bool EventNexusFileMemento::checkStillThere() const
{
std::ifstream ifile;
ifile.open(m_fileName.c_str(), std::ifstream::in);
return !ifile.fail();
}

/**
Getter for the workspace itself
@returns the matrix workspace
@throw if workspace has been moved since instantiation.
*/
Mantid::API::Workspace_sptr EventNexusFileMemento::fetchIt() const
{
checkStillThere();

IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("LoadEventNexus");
alg->initialize();
alg->setRethrows(true);
alg->setProperty("Filename", m_fileName);
alg->setPropertyValue("OutputWorkspace", m_adsID);
alg->execute();

Workspace_sptr ws = AnalysisDataService::Instance().retrieve(m_adsID);

Mantid::API::WorkspaceGroup_sptr gws = boost::dynamic_pointer_cast<WorkspaceGroup>(ws);
if(gws != NULL)
{
throw std::invalid_argument("This raw file corresponds to a WorkspaceGroup. Cannot process groups like this. Import via MantidPlot instead.");
}
return ws;
}

/**
Dump the workspace out of memory:
@name : name of the workspace to clean-out.
*/
void EventNexusFileMemento::dumpIt(const std::string& name)
{
if(AnalysisDataService::Instance().doesExist(name))
{
AnalysisDataService::Instance().remove(name);
}
}

/// Destructor
EventNexusFileMemento::~EventNexusFileMemento()
{
}

/// Clean up.
void EventNexusFileMemento::cleanUp()
{
dumpIt(m_adsID);
}

/*
Apply actions. Load workspace and apply all actions to it.
*/
Mantid::API::Workspace_sptr EventNexusFileMemento::applyActions()
{
Mantid::API::Workspace_sptr ws = fetchIt();

Mantid::API::IAlgorithm_sptr alg = Mantid::API::AlgorithmManager::Instance().create("SetUB");
alg->initialize();
alg->setRethrows(true);
alg->setPropertyValue("Workspace", this->m_adsID);
alg->setProperty("UB", m_ub);
alg->execute();

return AnalysisDataService::Instance().retrieve(m_adsID);
}

}
}
@@ -0,0 +1,75 @@
#ifndef CUSTOM_INTERFACES_WORKSPACE_EVENT_NEXUS_FILE_MEMENTO_TEST_H_
#define CUSTOM_INTERFACES_WORKSPACE_EVENT_NEXUS_FILE_MEMENTO_TEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidQtCustomInterfaces/EventNexusFileMemento.h"
#include "MantidAPI/FileFinder.h"
#include "MantidAPI/IEventWorkspace.h"
#include "MantidGeometry/Crystal/OrientedLattice.h"

using namespace MantidQt::CustomInterfaces;

using namespace Mantid::API;

class EventNexusFileMementoTest : public CxxTest::TestSuite
{
private:

static std::string getSuitableFileNamePath()
{
return Mantid::API::FileFinder::Instance().getFullPath("CNCS_7860_event.nxs");
}

public:

void testConstructorThrowsWithWrongExtension()
{
std::string badFile = "CNCS_7860_event.rrr"; //Fictional extension
TSM_ASSERT_THROWS("Unknown extension, should throw.", new EventNexusFileMemento(badFile), std::invalid_argument);
}

void testFileExists()
{
EventNexusFileMemento memento(getSuitableFileNamePath());
TSM_ASSERT("File should be present", memento.checkStillThere());
}

void testConstructThrowsWhenFileDoesntExist()
{
TSM_ASSERT_THROWS("Unknown file, should throw.", new EventNexusFileMemento("MadeUp.nxs"), std::runtime_error);
}

void testFetchItSucceedsWhenFileExists()
{
EventNexusFileMemento memento(getSuitableFileNamePath());
TSM_ASSERT("File should be present", memento.checkStillThere());
IEventWorkspace_sptr result = boost::dynamic_pointer_cast<IEventWorkspace>(memento.fetchIt());
TSM_ASSERT("Should have fetched the workspace", result);
}

void testNoExistingUB()
{
EventNexusFileMemento memento(getSuitableFileNamePath());
TS_ASSERT_EQUALS(WorkspaceMemento::NoOrientedLattice, memento.generateStatus());
}

void testApplyActions()
{
EventNexusFileMemento memento(getSuitableFileNamePath());
memento.setUB(0,0,2,0,4,0,-8,0,0);
IEventWorkspace_sptr ws = boost::dynamic_pointer_cast<IEventWorkspace>(memento.applyActions());
std::vector<double> ub = ws->sample().getOrientedLattice().getUB().get_vector();
TS_ASSERT_EQUALS(0, ub[0]);
TS_ASSERT_EQUALS(0, ub[1]);
TS_ASSERT_EQUALS(2, ub[2]);
TS_ASSERT_EQUALS(0, ub[3]);
TS_ASSERT_EQUALS(4, ub[4]);
TS_ASSERT_EQUALS(0, ub[5]);
TS_ASSERT_EQUALS(-8, ub[6]);
TS_ASSERT_EQUALS(0, ub[7]);
TS_ASSERT_EQUALS(0, ub[8]);
}

};

#endif
@@ -1,5 +1,5 @@
#ifndef CUSTOM_INTERFACES_WORKSPACE_ON_DISK_TEST_H_
#define CUSTOM_INTERFACES_WORKSPACE_ON_DISK_TEST_H_
#ifndef CUSTOM_INTERFACES_WORKSPACE_RAW_FILE_MEMENTO_TEST_H_
#define CUSTOM_INTERFACES_WORKSPACE_RAW_FILE_MEMENTO_TEST_H_

#include <cxxtest/TestSuite.h>
#include "MantidQtCustomInterfaces/RawFileMemento.h"