Skip to content

Commit

Permalink
Merge pull request #38786 from rosswhitfield/NexusDescriptor_NexusHDF…
Browse files Browse the repository at this point in the history
…5Descriptor_main

Move all HDF5 loading algorithms to NexusHDF5Descriptor
  • Loading branch information
peterfpeterson authored Feb 5, 2025
2 parents 718346f + cc0c046 commit cb4ff86
Show file tree
Hide file tree
Showing 58 changed files with 444 additions and 491 deletions.
51 changes: 23 additions & 28 deletions Framework/API/src/FileLoaderRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ template <> struct DescriptorSetter<Kernel::NexusHDF5Descriptor> {
* was found
*/
template <typename DescriptorType, typename FileLoaderType>
const IAlgorithm_sptr searchForLoader(const std::string &filename, const std::multimap<std::string, int> &names,
Kernel::Logger &logger) {
std::pair<IAlgorithm_sptr, int> searchForLoader(const std::string &filename,
const std::multimap<std::string, int> &names, Kernel::Logger &logger) {
const auto &factory = AlgorithmFactory::Instance();
IAlgorithm_sptr bestLoader;
int maxConfidence(0);
Expand Down Expand Up @@ -81,7 +81,7 @@ const IAlgorithm_sptr searchForLoader(const std::string &filename, const std::mu
if (nxsLoader)
setdescriptor.apply(nxsLoader, descriptor);

return bestLoader;
return {bestLoader, maxConfidence};
}
} // namespace

Expand Down Expand Up @@ -117,27 +117,22 @@ const std::shared_ptr<IAlgorithm> FileLoaderRegistryImpl::chooseLoader(const std
m_log.debug() << "Trying to find loader for '" << filename << "'\n";

IAlgorithm_sptr bestLoader;
if (NexusDescriptor::isReadable(filename)) {
m_log.debug() << filename << " looks like a Nexus file. Checking registered Nexus loaders\n";

// a large subset of NeXus files are actually HDF5 based
if (NexusHDF5Descriptor::isReadable(filename)) {
try {
bestLoader =
searchForLoader<NexusHDF5Descriptor, IFileLoader<NexusHDF5Descriptor>>(filename, m_names[NexusHDF5], m_log);
} catch (const std::invalid_argument &e) {
m_log.debug() << "Error in looking for HDF5 based NeXus files: " << e.what() << '\n';
}
}

// try generic nexus loaders
if (!bestLoader) {
bestLoader = searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>(filename, m_names[Nexus], m_log);
}
} else {
m_log.debug() << "Checking registered non-HDF loaders\n";
bestLoader = searchForLoader<FileDescriptor, IFileLoader<FileDescriptor>>(filename, m_names[Generic], m_log);
}
auto HDFversion = NexusHDF5Descriptor::getHDFVersion(filename);
if (HDFversion == NexusHDF5Descriptor::Version5) {
std::pair<IAlgorithm_sptr, int> HDF5result =
searchForLoader<NexusHDF5Descriptor, IFileLoader<NexusHDF5Descriptor>>(filename, m_names[NexusHDF5], m_log);

// must also try NexusDescriptor algorithms because LoadMuonNexus can load both HDF4 and HDF5 files
std::pair<IAlgorithm_sptr, int> HDF4result =
searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>(filename, m_names[Nexus], m_log);
if (HDF5result.second > HDF4result.second)
bestLoader = HDF5result.first;
else
bestLoader = HDF4result.first;
} else if (HDFversion == NexusHDF5Descriptor::Version4)
bestLoader = searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>(filename, m_names[Nexus], m_log).first;
else
bestLoader = searchForLoader<FileDescriptor, IFileLoader<FileDescriptor>>(filename, m_names[Generic], m_log).first;

if (!bestLoader) {
throw Kernel::Exception::NotFoundError(filename, "Unable to find loader");
Expand Down Expand Up @@ -170,19 +165,19 @@ bool FileLoaderRegistryImpl::canLoad(const std::string &algorithmName, const std
std::multimap<std::string, int> names{{algorithmName, -1}};
IAlgorithm_sptr loader;
if (nexus) {
if (NexusDescriptor::isReadable(filename)) {
loader = searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>(filename, names, m_log);
if (NexusHDF5Descriptor::isReadable(filename, NexusHDF5Descriptor::Version4)) {
loader = searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>(filename, names, m_log).first;
}
} else if (nexusHDF5) {
if (NexusHDF5Descriptor::isReadable(filename)) {
try {
loader = searchForLoader<NexusHDF5Descriptor, IFileLoader<NexusHDF5Descriptor>>(filename, names, m_log);
loader = searchForLoader<NexusHDF5Descriptor, IFileLoader<NexusHDF5Descriptor>>(filename, names, m_log).first;
} catch (const std::invalid_argument &e) {
m_log.debug() << "Error in looking for HDF5 based NeXus files: " << e.what() << '\n';
}
}
} else if (nonHDF) {
loader = searchForLoader<FileDescriptor, IFileLoader<FileDescriptor>>(filename, names, m_log);
loader = searchForLoader<FileDescriptor, IFileLoader<FileDescriptor>>(filename, names, m_log).first;
}
return static_cast<bool>(loader);
}
Expand Down
8 changes: 4 additions & 4 deletions Framework/DataHandling/inc/MantidDataHandling/LoadEMU.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidGeometry/Instrument.h"
#include "MantidKernel/FileDescriptor.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"

namespace Mantid {
Expand Down Expand Up @@ -94,7 +94,7 @@ template <typename FD> class LoadEMU : public API::IFileLoader<FD> {
// the instantiation and linking did not behave consistently across platforms.

extern template class LoadEMU<Kernel::FileDescriptor>;
extern template class LoadEMU<Kernel::NexusDescriptor>;
extern template class LoadEMU<Kernel::NexusHDF5Descriptor>;

/** LoadEMUTar : Loads a merged ANSTO EMU Hdf and event file into a workspace.
Expand Down Expand Up @@ -154,14 +154,14 @@ Optional Properties:
</UL>
*/
class MANTID_DATAHANDLING_DLL LoadEMUHdf : public LoadEMU<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadEMUHdf : public LoadEMU<Kernel::NexusHDF5Descriptor> {
public:
int version() const override;
const std::vector<std::string> seeAlso() const override;
const std::string category() const override;
const std::string name() const override;
const std::string summary() const override;
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;

private:
void exec() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ bool LoadEventNexus::runLoadInstrument(const std::string &nexusfilename, T local
std::string instFilename;

const bool isNexus = (descriptor == nullptr) ? LoadGeometry::isNexus(nexusfilename)
: LoadGeometry::isNexus(nexusfilename, descriptor->getAllEntries());
: LoadGeometry::isNexus(descriptor->getAllEntries());

// Check if the geometry can be loaded directly from the Nexus file
if (isNexus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ bool isIDF(const std::string &filename);
bool isNexus(const std::string &filename);
/// Determine if the Geometry file type is Nexus
/// version that reuses the metadata container
bool isNexus(const std::string &filename, const std::map<std::string, std::set<std::string>> &allEntries);
bool isNexus(const std::map<std::string, std::set<std::string>> &allEntries);
/// List allowed file extensions for geometry
const std::vector<std::string> validExtensions();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidKernel/V3D.h"
#include "MantidNexus/NexusClasses.h"

Expand All @@ -22,14 +22,14 @@ namespace DataHandling {
@date 15/05/17
*/
class MANTID_DATAHANDLING_DLL LoadILLDiffraction : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadILLDiffraction : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
const std::string name() const override;
int version() const override;
const std::vector<std::string> seeAlso() const override { return {"LoadNexus"}; }
const std::string category() const override;
const std::string summary() const override;
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;
LoadILLDiffraction();

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"

namespace Mantid {
Expand All @@ -17,11 +17,11 @@ namespace DataHandling {
/**
Loads an ILL IN16B nexus file into a Mantid workspace.
*/
class MANTID_DATAHANDLING_DLL LoadILLIndirect2 : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadILLIndirect2 : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
LoadILLIndirect2();
/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;

/// Algorithm's version for identification. @see Algorithm::version
int version() const override { return 2; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"

#include <H5Cpp.h>
Expand All @@ -18,14 +18,14 @@ namespace DataHandling {

/** LoadILLLagrange : Loads nexus files from ILL instrument LAGRANGE.
*/
class MANTID_DATAHANDLING_DLL LoadILLLagrange : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadILLLagrange : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
const std::string name() const override;
int version() const override;
const std::vector<std::string> seeAlso() const override { return {"LagrangeILLReduction"}; }
const std::string category() const override;
const std::string summary() const override;
int confidence(Kernel::NexusDescriptor &) const override;
int confidence(Kernel::NexusHDF5Descriptor &) const override;
LoadILLLagrange();

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "MantidAPI/WorkspaceGroup_fwd.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidKernel/DateAndTime.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"

#include <utility>
Expand All @@ -23,14 +23,14 @@ namespace DataHandling {
@date 15/05/20
*/
class MANTID_DATAHANDLING_DLL LoadILLPolarizedDiffraction : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadILLPolarizedDiffraction : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
const std::string name() const override;
int version() const override;
const std::vector<std::string> seeAlso() const override { return {"LoadNexus"}; }
const std::string category() const override;
const std::string summary() const override;
int confidence(Kernel::NexusDescriptor &) const override;
int confidence(Kernel::NexusHDF5Descriptor &) const override;
LoadILLPolarizedDiffraction();

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "MantidAPI/IFileLoader.h"
#include "MantidAPI/MatrixWorkspace_fwd.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"
#include "MantidTypes/Core/DateAndTime.h"

Expand All @@ -18,11 +18,11 @@ namespace DataHandling {

/*! LoadILLReflectometry : Loads an ILL reflectometry Nexus data file.
*/
class MANTID_DATAHANDLING_DLL LoadILLReflectometry : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadILLReflectometry : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
LoadILLReflectometry() = default;
/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;
/// Algorithm's name for identification. @see Algorithm::name
const std::string name() const override { return "LoadILLReflectometry"; }
/// Algorithm's version for identification. @see Algorithm::version
Expand Down
6 changes: 3 additions & 3 deletions Framework/DataHandling/inc/MantidDataHandling/LoadILLSALSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidDataObjects/Workspace2D.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"

#include <H5Cpp.h>

Expand All @@ -22,7 +22,7 @@ namespace DataHandling {
/**
Loads an ILL SALSA NeXus file into a Mantid workspace.
*/
class MANTID_DATAHANDLING_DLL LoadILLSALSA : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadILLSALSA : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
/// Algorithm's name
const std::string name() const override { return "LoadILLSALSA"; }
Expand All @@ -34,7 +34,7 @@ class MANTID_DATAHANDLING_DLL LoadILLSALSA : public API::IFileLoader<Kernel::Nex
/// Algorithm's category for identification
const std::string category() const override { return "DataHandling\\Nexus;ILL\\Strain"; }
/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;

private:
// Number of pixel on the detector in the vertical dimension
Expand Down
6 changes: 3 additions & 3 deletions Framework/DataHandling/inc/MantidDataHandling/LoadILLSANS.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"

#include <H5Cpp.h>
Expand All @@ -19,7 +19,7 @@ namespace DataHandling {
/** LoadILLSANS; supports D11, D22 and D33 (TOF/monochromatic)
*/

class MANTID_DATAHANDLING_DLL LoadILLSANS : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadILLSANS : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
LoadILLSANS();
const std::string name() const override;
Expand All @@ -28,7 +28,7 @@ class MANTID_DATAHANDLING_DLL LoadILLSANS : public API::IFileLoader<Kernel::Nexu
const std::vector<std::string> seeAlso() const override { return {"LoadNexus"}; }
const std::string category() const override;
/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;

private:
enum MultichannelType { TOF, KINETIC, SCAN };
Expand Down
6 changes: 3 additions & 3 deletions Framework/DataHandling/inc/MantidDataHandling/LoadILLTOF2.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidGeometry/IDTypes.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"

namespace Mantid {
namespace DataHandling {
/**
Loads an ILL IN4/5/6/Panther NeXus file into a Mantid workspace.
*/
class MANTID_DATAHANDLING_DLL LoadILLTOF2 : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadILLTOF2 : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
/// Constructor
LoadILLTOF2();
Expand All @@ -33,7 +33,7 @@ class MANTID_DATAHANDLING_DLL LoadILLTOF2 : public API::IFileLoader<Kernel::Nexu
const std::string category() const override { return "DataHandling\\Nexus;ILL\\Direct"; }

/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;

private:
// Initialisation code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "MantidDataHandling/DataBlockComposite.h"
#include "MantidDataObjects/Workspace2D_fwd.h"
#include "MantidHistogramData/HistogramX.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"
#include "MantidNexusCpp/NeXusFile.hpp"

Expand Down Expand Up @@ -56,7 +56,7 @@ multi-period file)
@author Roman Tolchenov, Tessella plc
*/
class MANTID_DATAHANDLING_DLL LoadISISNexus2 : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadISISNexus2 : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
/// Default constructor
LoadISISNexus2();
Expand All @@ -71,7 +71,7 @@ class MANTID_DATAHANDLING_DLL LoadISISNexus2 : public API::IFileLoader<Kernel::N
const std::string summary() const override { return "Loads a file in ISIS NeXus format."; }

/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;

/// Spectra block descriptor
struct SpectraBlock {
Expand Down
6 changes: 3 additions & 3 deletions Framework/DataHandling/inc/MantidDataHandling/LoadMLZ.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
//---------------------------------------------------
#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidNexus/NexusClasses.h"

namespace Mantid {
namespace DataHandling {
/**
LoadMLZ : Loads MLZ nexus or hdf file into a Mantid workspace.
*/
class MANTID_DATAHANDLING_DLL LoadMLZ : public API::IFileLoader<Kernel::NexusDescriptor> {
class MANTID_DATAHANDLING_DLL LoadMLZ : public API::IFileLoader<Kernel::NexusHDF5Descriptor> {
public:
LoadMLZ();

Expand All @@ -31,7 +31,7 @@ class MANTID_DATAHANDLING_DLL LoadMLZ : public API::IFileLoader<Kernel::NexusDes
const std::string summary() const override { return "Loads a nexus file from MLZ facility."; }

/// Returns a confidence value that this algorithm can load a file
int confidence(Kernel::NexusDescriptor &descriptor) const override;
int confidence(Kernel::NexusHDF5Descriptor &descriptor) const override;

private:
void init() override;
Expand Down
Loading

0 comments on commit cb4ff86

Please sign in to comment.