| @@ -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 |
| @@ -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 |