Skip to content

Commit

Permalink
Merge pull request #646 from mantidproject/11362_SaveMD_MDHisto_struc…
Browse files Browse the repository at this point in the history
…ture_change

Change SaveMD structure for MDHisto
  • Loading branch information
AndreiSavici committed Apr 24, 2015
2 parents d210c00 + ef93235 commit 1396a3e
Show file tree
Hide file tree
Showing 8 changed files with 698 additions and 15 deletions.
3 changes: 3 additions & 0 deletions Code/Mantid/Framework/MDAlgorithms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ set ( SRC_FILES
src/ReflectometryTransformQxQz.cpp
src/SaveIsawQvector.cpp
src/SaveMD.cpp
src/SaveMD2.cpp
src/SaveZODS.cpp
src/SetMDUsingMask.cpp
src/SliceMD.cpp
Expand Down Expand Up @@ -220,6 +221,7 @@ set ( INC_FILES
inc/MantidMDAlgorithms/ReflectometryTransformQxQz.h
inc/MantidMDAlgorithms/SaveIsawQvector.h
inc/MantidMDAlgorithms/SaveMD.h
inc/MantidMDAlgorithms/SaveMD2.h
inc/MantidMDAlgorithms/SaveZODS.h
inc/MantidMDAlgorithms/SetMDUsingMask.h
inc/MantidMDAlgorithms/SliceMD.h
Expand Down Expand Up @@ -320,6 +322,7 @@ set ( TEST_FILES
ResolutionConvolvedCrossSectionTest.h
SaveIsawQvectorTest.h
SaveMDTest.h
SaveMD2Test.h
SaveZODSTest.h
SetMDUsingMaskTest.h
SimulateResolutionConvolvedModelTest.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class DLLExport LoadMD : public API::IFileLoader<Kernel::NexusDescriptor> {

void loadDimensions();

void loadDimensions2();

void loadCoordinateSystem();

/// Load all the affine matricies
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef MANTID_MDALGORITHMS_SAVEMD2_H_
#define MANTID_MDALGORITHMS_SAVEMD2_H_

#include "MantidKernel/System.h"
#include "MantidAPI/Algorithm.h"
#include "MantidDataObjects/MDEventWorkspace.h"

namespace Mantid {

namespace MDAlgorithms {

/** Save a MDEventWorkspace to a .nxs file.
Copyright &copy; 2015 ISIS Rutherford Appleton Laboratory, NScD Oak Ridge National Laboratory & European Spallation Source
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://github.com/mantidproject/mantid>
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
class DLLExport SaveMD2 : public API::Algorithm {
public:
SaveMD2();
~SaveMD2();

/// Algorithm's name for identification
virtual const std::string name() const { return "SaveMD"; };
/// Summary of algorithms purpose
virtual const std::string summary() const {
return "Save a MDEventWorkspace or MDHistoWorkspace to a .nxs file.";
}

/// Algorithm's version for identification
virtual int version() const { return 2; };
/// Algorithm's category for identification
virtual const std::string category() const { return "MDAlgorithms"; }

private:
/// Initialise the properties
void init();
/// Run the algorithm
void exec();

/// Save the MDHistoWorkspace.
void doSaveHisto(Mantid::DataObjects::MDHistoWorkspace_sptr ws);

/// Save a generic matrix
template <typename T>
void saveMatrix(::NeXus::File *const file, std::string name,
Kernel::Matrix<T> &m, ::NeXus::NXnumtype type,
std::string tag = "");
};

} // namespace DataObjects
} // namespace Mantid

#endif /* MANTID_MDALGORITHMS_SAVEMD2_H_ */
76 changes: 62 additions & 14 deletions Code/Mantid/Framework/MDAlgorithms/src/LoadMD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,27 @@ void LoadMD::exec() {
// Open the entry
m_file->openGroup(entryName, "NXentry");

// How many dimensions?
std::vector<int32_t> vecDims;
m_file->readData("dimensions", vecDims);
if (vecDims.empty())
throw std::runtime_error("LoadMD:: Error loading number of dimensions.");
m_numDims = vecDims[0];
if (m_numDims <= 0)
throw std::runtime_error("LoadMD:: number of dimensions <= 0.");

// Now load all the dimension xml
this->loadDimensions();
// Check is SaveMD version 2 was used
int SaveMDVersion = 0;
if (m_file->hasAttr("SaveMDVersion"))
m_file->getAttr("SaveMDVersion", SaveMDVersion);

if (SaveMDVersion == 2)
this->loadDimensions2();
else {
// How many dimensions?
std::vector<int32_t> vecDims;
m_file->readData("dimensions", vecDims);
if (vecDims.empty())
throw std::runtime_error("LoadMD:: Error loading number of dimensions.");
m_numDims = vecDims[0];
if (m_numDims <= 0)
throw std::runtime_error("LoadMD:: number of dimensions <= 0.");

// Now load all the dimension xml
this->loadDimensions();
}

// Coordinate system
this->loadCoordinateSystem();

Expand Down Expand Up @@ -211,12 +221,19 @@ void LoadMD::loadSlab(std::string name, void *data, MDHistoWorkspace_sptr ws,
if (m_file->getInfo().type != dataType)
throw std::runtime_error("Unexpected data type for '" + name +
"' data set.'");
if (m_file->getInfo().dims[0] != static_cast<int>(ws->getNPoints()))

int nPoints = 1;
size_t numDims = m_file->getInfo().dims.size();
std::vector<int> size(numDims);
for (size_t d = 0; d < numDims; d++) {
nPoints *= static_cast<int>(m_file->getInfo().dims[d]);
size[d] = static_cast<int>(m_file->getInfo().dims[d]);
}
if (nPoints != static_cast<int>(ws->getNPoints()))
throw std::runtime_error(
"Inconsistency between the number of points in '" + name +
"' and the number of bins defined by the dimensions.");
std::vector<int> start(1, 0);
std::vector<int> size(1, static_cast<int>(ws->getNPoints()));
std::vector<int> start(numDims, 0);
try {
m_file->getSlab(data, start, size);
} catch (...) {
Expand Down Expand Up @@ -273,6 +290,37 @@ void LoadMD::loadDimensions() {
}
}

//----------------------------------------------------------------------------------------------
/** Load all the dimensions into this->m_dims
* The dimensions are stored as an nxData array */
void LoadMD::loadDimensions2() {
m_dims.clear();

std::string axes;

m_file->openData("signal");
m_file->getAttr("axes", axes);
m_file->closeData();

std::vector<std::string> splitAxes;
boost::split(splitAxes, axes, boost::is_any_of(":"));
// Create each dimension from axes data
// We loop axes backwards because Mantid
for (size_t d = splitAxes.size(); d > 0; d--) {
std::string long_name;
std::string units;
std::vector<double> axis;
m_file->openData(splitAxes[d - 1]);
m_file->getAttr("long_name", long_name);
m_file->getAttr("units", units);
m_file->getData(axis);
m_file->closeData();
m_dims.push_back(boost::make_shared<MDHistoDimension>(
long_name, splitAxes[d - 1], units, static_cast<coord_t>(axis.front()),
static_cast<coord_t>(axis.back()), axis.size() - 1));
}
}

/** Load the coordinate system **/
void LoadMD::loadCoordinateSystem() {
// Current version stores the coordinate system
Expand Down

0 comments on commit 1396a3e

Please sign in to comment.