Newer
Older
#include "MantidVatesAPI/MDHWNexusLoadingPresenter.h"
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "MantidVatesAPI/MDLoadingView.h"
#include "MantidVatesAPI/ProgressAction.h"
#include "MantidVatesAPI/vtkDataSetFactory.h"
#include "MantidGeometry/MDGeometry/MDGeometryXMLBuilder.h"
#include <nexus/NeXusFile.hpp>
#include <nexus/NeXusException.hpp>
#include "MantidAPI/AlgorithmManager.h"
#include <vtkUnstructuredGrid.h>
namespace Mantid
{
namespace VATES
{
/**
* Constructor
* @param view : MVP view
* @param filename : name of file to load
* @throw invalid_argument if file name is empty
* @throw invalid_arument if view is null
* @throw logic_error if cannot use the reader-presenter for this filetype.
*/
MDHWNexusLoadingPresenter::MDHWNexusLoadingPresenter(MDLoadingView* view, const std::string filename) : MDHWLoadingPresenter(view), m_filename(filename), m_wsTypeName("")
{
if(this->m_filename.empty())
{
throw std::invalid_argument("File name is an empty string.");
}
if(NULL == this->m_view)
{
throw std::invalid_argument("View is NULL.");
}
}
/**
* Indicates whether this presenter is capable of handling the type of file that is attempted to be loaded.
* @return false if the file cannot be read.
*/
bool MDHWNexusLoadingPresenter::canReadFile() const
{
// Quick check based on extension.
if(!canLoadFileBasedOnExtension(m_filename, ".nxs"))
{
return 0;
}
::NeXus::File * file = NULL;
file = new ::NeXus::File(this->m_filename);
// MDHistoWorkspace file has a different name for the entry
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
try
{
file->openGroup("MDHistoWorkspace", "NXentry");
file->close();
return 1;
}
catch(::NeXus::Exception &)
{
// If the entry name does not match, then it can't read the file.
file->close();
return 0;
}
return 0;
}
/**
* Executes the underlying algorithm to create the MVP model.
* @param factory : visualisation factory to use.
* @param loadingProgressUpdate : Handler for GUI updates while algorithm progresses.
* @param drawingProgressUpdate : Handler for GUI updates while vtkDataSetFactory::create occurs.
*/
vtkDataSet* MDHWNexusLoadingPresenter::execute(vtkDataSetFactory* factory, ProgressAction& loadingProgressUpdate, ProgressAction& drawingProgressUpdate)
{
using namespace Mantid::API;
using namespace Mantid::Geometry;
if(this->shouldLoad())
{
Poco::NObserver<ProgressAction, Mantid::API::Algorithm::ProgressNotification> observer(loadingProgressUpdate, &ProgressAction::handler);
AnalysisDataService::Instance().remove("MD_HISTO_WS_ID");
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("LoadMD");
alg->initialize();
alg->setPropertyValue("Filename", this->m_filename);
alg->setPropertyValue("OutputWorkspace", "MD_HISTO_WS_ID");
alg->setProperty("FileBackEnd", !this->m_view->getLoadInMemory()); //Load from file by default.
alg->addObserver(observer);
alg->execute();
alg->removeObserver(observer);
}
Workspace_sptr result = AnalysisDataService::Instance().retrieve("MD_HISTO_WS_ID");
Mantid::API::IMDHistoWorkspace_sptr histoWs = boost::dynamic_pointer_cast<Mantid::API::IMDHistoWorkspace>(result);
//factory->setRecursionDepth(this->m_view->getRecursionDepth());
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Create visualisation in one-shot.
vtkDataSet* visualDataSet = factory->oneStepCreate(histoWs, drawingProgressUpdate);
// extractMetaData needs to be re-run here because the first execution
// of this from ::executeLoadMetadata will not have ensured that all
// dimensions have proper range extents set.
this->extractMetadata(histoWs);
this->appendMetadata(visualDataSet, histoWs->getName());
return visualDataSet;
}
/**
* Executes any meta-data loading required.
*/
void MDHWNexusLoadingPresenter::executeLoadMetadata()
{
using namespace Mantid::API;
AnalysisDataService::Instance().remove("MD_HISTO_WS_ID");
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("LoadMD");
alg->initialize();
alg->setPropertyValue("Filename", this->m_filename);
alg->setPropertyValue("OutputWorkspace", "MD_HISTO_WS_ID");
alg->setProperty("MetadataOnly", true); //Don't load the events.
alg->setProperty("FileBackEnd", false); //Only require metadata, so do it in memory.
alg->execute();
Workspace_sptr result = AnalysisDataService::Instance().retrieve("MD_HISTO_WS_ID");
IMDHistoWorkspace_sptr histoWs = boost::dynamic_pointer_cast<Mantid::API::IMDHistoWorkspace>(result);
m_wsTypeName = histoWs->id();
// Call base-class extraction method.
this->extractMetadata(histoWs);
AnalysisDataService::Instance().remove("MD_HISTO_WS_ID");
}
/**
* Destructor
* @return
*/
MDHWNexusLoadingPresenter::~MDHWNexusLoadingPresenter()
{
delete m_view;
}
/**
* Getter for the workspace type name.
* @return Workspace Type Name
*/
std::string MDHWNexusLoadingPresenter::getWorkspaceTypeName()
{
return m_wsTypeName;
}
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
180
std::vector<int> MDHWNexusLoadingPresenter::getExtents()
{
// Hack which only works in 3D. Needs to be updated for 4 dimensions!
// Hack to ensure MD_HISTO_WS_ID is available. Fix so we're not constantly reloading!
using namespace Mantid::API;
AnalysisDataService::Instance().remove("MD_HISTO_WS_ID");
IAlgorithm_sptr alg = AlgorithmManager::Instance().create("LoadMD");
alg->initialize();
alg->setPropertyValue("Filename", this->m_filename);
alg->setPropertyValue("OutputWorkspace", "MD_HISTO_WS_ID");
alg->setProperty("FileBackEnd", !this->m_view->getLoadInMemory()); //Load from file by default.
alg->execute();
Workspace_sptr result = AnalysisDataService::Instance().retrieve("MD_HISTO_WS_ID");
Mantid::API::IMDHistoWorkspace_sptr histoWs = boost::dynamic_pointer_cast<Mantid::API::IMDHistoWorkspace>(result);
std::vector<int> extents(6, 0);
extents[1] = histoWs->getDimension(0)->getNBins();
extents[3] = histoWs->getDimension(1)->getNBins();
extents[5] = histoWs->getDimension(2)->getNBins();
//AnalysisDataService::Instance().remove("MD_HISTO_WS_ID");
return extents;
}