Mantid
Loading...
Searching...
No Matches
SaveNexusESS.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
9#include "MantidNexusGeometry/NexusGeometrySave.h"
10#include <H5Cpp.h>
11
12namespace Mantid::DataHandling {
16
17// Register the algorithm into the AlgorithmFactory
18DECLARE_ALGORITHM(SaveNexusESS)
19
20//----------------------------------------------------------------------------------------------
21
22
23const std::string SaveNexusESS::name() const { return "SaveNexusESS"; }
24
26int SaveNexusESS::version() const { return 1; }
27
29const std::string SaveNexusESS::category() const { return "DataHandling\\Nexus"; }
30
32const std::string SaveNexusESS::summary() const {
33 return "Saves intermediate, also known as 'processed' nexus file with data "
34 "and geometry";
35}
36
42
43 // TODO: Due to the mixture of Nexus and HDF5 operations, and the original design of `SaveNexusESS`: this isn't as
44 // efficient as it could be.
45
46 const std::string filename = getProperty("Filename");
47 NexusGeometry::LogAdapter<Kernel::Logger> adapter(&g_log);
48
50
51 // Now loop over the workspace entries again and fill in their NXinstrument groups.
52 // (Also see comments at: `SaveNexusProcessed::processGroups`.)
53 const auto &workspaces = m_unrolledInputWorkspaces[0];
54 if (!workspaces.empty()) {
55 for (size_t entry = 0; entry < workspaces.size(); entry++) {
56 const Workspace_sptr ws = workspaces[entry];
57 auto matrixWs = std::dynamic_pointer_cast<API::MatrixWorkspace>(ws);
58 if (!matrixWs)
59 throw std::runtime_error("SaveNexusESS::processGroups: workspace is not a MatrixWorkspace");
60
61 saveNexusGeometry(*matrixWs, filename, entry + 1);
62 g_log.information() << "Adding instrument to workspace at group index " << entry << "\n";
63 }
64 }
65
66 return true;
67}
68
69void SaveNexusESS::saveNexusGeometry(const Mantid::API::MatrixWorkspace &ws, const std::string &filename,
70 std::optional<size_t> entryNumber) {
71
72 try {
73 NexusGeometry::LogAdapter<Kernel::Logger> adapter(&g_log);
74 NexusGeometry::NexusGeometrySave::saveInstrument(ws, filename, "mantid_workspace_", entryNumber, adapter, true);
75 } catch (std::exception &e) {
76 g_log.error(std::string(e.what()) + ":\n Nexus Geometry may be absent or incomplete "
77 "from the processed Nexus file");
78 } catch (H5::Exception &ex) {
79 g_log.error(ex.getDetailMsg() + ":\n Nexus Geometry may be absent or incomplete "
80 "from the processed Nexus file");
81 }
82}
83
85 /*A hard No on this one. Mantids's current NXDetector, NXMonitor ... types do
86 * not have information needed for loading and just cause down-stream
87 * problems. Best not to save them in the first place.*/
88 return false;
89}
90
91//----------------------------------------------------------------------------------------------
95 // Re-use the same properties as those of the base class.
97}
98
99//----------------------------------------------------------------------------------------------
103
104 API::Workspace_sptr ws = getProperty("InputWorkspace");
105 const std::string filename = getProperty("Filename");
106 auto matrixWs = std::dynamic_pointer_cast<API::MatrixWorkspace>(ws);
107 if (!matrixWs)
108 throw std::runtime_error("SaveNexusESS expects a MatrixWorkspace as input");
109
110 // First: call the base-class `exec` method.
112
113 // Next: append the NeXus geometry
114 saveNexusGeometry(*matrixWs, filename);
115
116 return;
117}
118} // namespace Mantid::DataHandling
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:538
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:422
std::vector< WorkspaceVector > m_unrolledInputWorkspaces
One vector of workspaces for each input workspace property.
Definition Algorithm.h:429
Base MatrixWorkspace Abstract Class.
A property class for workspaces.
SaveNexusESS : Save algorithm to save a NeXus organised hdf5 file containing data and geometry from r...
int version() const override
Algorithm's version for identification.
virtual bool saveLegacyInstrument() override
const std::string category() const override
Algorithm's category for identification.
void exec() override
Execute the algorithm.
void init() override
Initialize the algorithm's properties.
bool processGroups() override
SaveNexusESS::processGroups.
void saveNexusGeometry(const Mantid::API::MatrixWorkspace &ws, const std::string &filename, std::optional< size_t > entryNumber=std::optional< size_t >())
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
void init() override
Overwrites Algorithm method.
bool processGroups() override
Override process groups.
void exec() override
Overwrites Algorithm method.
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
void information(const std::string &msg)
Logs at information level.
Definition Logger.cpp:136
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
STL namespace.
Describes the direction (within an algorithm) of a Property.
Definition Property.h:50