Skip to content
Snippets Groups Projects
Commit 7b226ee8 authored by Peterson, Peter's avatar Peterson, Peter
Browse files

Add registration of NeXusHDF5 file loaders

parent c4e9150a
No related branches found
No related tags found
No related merge requests found
...@@ -10,15 +10,16 @@ ...@@ -10,15 +10,16 @@
#include "MantidAPI/IFileLoader.h" #include "MantidAPI/IFileLoader.h"
#include "MantidKernel/FileDescriptor.h" #include "MantidKernel/FileDescriptor.h"
#include "MantidKernel/NexusDescriptor.h" #include "MantidKernel/NexusDescriptor.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include "MantidKernel/SingletonHolder.h" #include "MantidKernel/SingletonHolder.h"
#ifndef Q_MOC_RUN #ifndef Q_MOC_RUN
#include <type_traits> #include <type_traits>
#endif #endif
#include <array>
#include <map> #include <map>
#include <string> #include <string>
#include <vector>
namespace Mantid { namespace Mantid {
namespace Kernel { namespace Kernel {
...@@ -41,7 +42,7 @@ DECLARE_ALGORITHM macro ...@@ -41,7 +42,7 @@ DECLARE_ALGORITHM macro
class MANTID_API_DLL FileLoaderRegistryImpl { class MANTID_API_DLL FileLoaderRegistryImpl {
public: public:
/// Defines types of possible file /// Defines types of possible file
enum LoaderFormat { Nexus, Generic }; enum LoaderFormat { Nexus, Generic, NexusHDF5 };
public: public:
/// @returns the number of entries in the registry /// @returns the number of entries in the registry
...@@ -101,6 +102,16 @@ private: ...@@ -101,6 +102,16 @@ private:
"API::IFileLoader<Kernel::NexusDescriptor>"); "API::IFileLoader<Kernel::NexusDescriptor>");
} }
break; break;
case NexusHDF5:
if (!std::is_base_of<IFileLoader<Kernel::NexusHDF5Descriptor>, T>::value) {
throw std::runtime_error(
std::string("FileLoaderRegistryImpl::subscribe - Class '") +
typeid(T).name() +
"' registered as NexusHDF5 loader but it does not "
"inherit from "
"API::IFileLoader<Kernel::NexusHDF5Descriptor>");
}
break;
case Generic: case Generic:
if (!std::is_base_of<IFileLoader<Kernel::FileDescriptor>, T>::value) { if (!std::is_base_of<IFileLoader<Kernel::FileDescriptor>, T>::value) {
throw std::runtime_error( throw std::runtime_error(
...@@ -122,8 +133,8 @@ private: ...@@ -122,8 +133,8 @@ private:
std::multimap<std::string, int> &typedLoaders); std::multimap<std::string, int> &typedLoaders);
/// The list of names. The index pointed to by LoaderFormat defines a set for /// The list of names. The index pointed to by LoaderFormat defines a set for
/// that format /// that format. The length is equal to the length of the LoaderFormat enum
std::vector<std::multimap<std::string, int>> m_names; std::array<std::multimap<std::string, int>, 3> m_names;
/// Total number of names registered /// Total number of names registered
size_t m_totalSize; size_t m_totalSize;
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
// SPDX - License - Identifier: GPL - 3.0 + // SPDX - License - Identifier: GPL - 3.0 +
#include "MantidAPI/FileLoaderRegistry.h" #include "MantidAPI/FileLoaderRegistry.h"
#include "MantidAPI/IFileLoader.h" #include "MantidAPI/IFileLoader.h"
#include "MantidKernel/NexusHDF5Descriptor.h"
#include <Poco/File.h> #include <Poco/File.h>
...@@ -113,14 +112,23 @@ FileLoaderRegistryImpl::chooseLoader(const std::string &filename) const { ...@@ -113,14 +112,23 @@ FileLoaderRegistryImpl::chooseLoader(const std::string &filename) const {
m_log.debug() m_log.debug()
<< filename << filename
<< " looks like a Nexus file. Checking registered Nexus loaders\n"; << " looks like a Nexus file. Checking registered Nexus loaders\n";
try {
bestLoader = searchForLoader<NexusHDF5Descriptor, // a large subset of NeXus files are actually HDF5 based
IFileLoader<NexusHDF5Descriptor>>( if (NexusHDF5Descriptor::isReadable(filename)) {
filename, m_names[Nexus], m_log); try {
} catch (const std::invalid_argument &) { 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 = bestLoader =
searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>( searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>(
filename, m_names[Nexus], m_log); filename, m_names[Nexus], m_log);
} }
} else { } else {
m_log.debug() << "Checking registered non-HDF loaders\n"; m_log.debug() << "Checking registered non-HDF loaders\n";
...@@ -147,15 +155,14 @@ bool FileLoaderRegistryImpl::canLoad(const std::string &algorithmName, ...@@ -147,15 +155,14 @@ bool FileLoaderRegistryImpl::canLoad(const std::string &algorithmName,
const std::string &filename) const { const std::string &filename) const {
using Kernel::FileDescriptor; using Kernel::FileDescriptor;
using Kernel::NexusDescriptor; using Kernel::NexusDescriptor;
using Kernel::NexusHDF5Descriptor;
// Check if it is in one of our lists // Check if it is in one of our lists
bool nexus(false), nonHDF(false); const bool nexus = (m_names[Nexus].find(algorithmName) != m_names[Nexus].end());
if (m_names[Nexus].find(algorithmName) != m_names[Nexus].end()) const bool nexusHDF5 = (m_names[NexusHDF5].find(algorithmName) != m_names[NexusHDF5].end());
nexus = true; const bool nonHDF = (m_names[Generic].find(algorithmName) != m_names[Generic].end());
else if (m_names[Generic].find(algorithmName) != m_names[Generic].end())
nonHDF = true;
if (!nexus && !nonHDF) if (!(nexus || nexusHDF5 || nonHDF))
throw std::invalid_argument( throw std::invalid_argument(
"FileLoaderRegistryImpl::canLoad - Algorithm '" + algorithmName + "FileLoaderRegistryImpl::canLoad - Algorithm '" + algorithmName +
"' is not registered as a loader."); "' is not registered as a loader.");
...@@ -165,11 +172,21 @@ bool FileLoaderRegistryImpl::canLoad(const std::string &algorithmName, ...@@ -165,11 +172,21 @@ bool FileLoaderRegistryImpl::canLoad(const std::string &algorithmName,
if (nexus) { if (nexus) {
if (NexusDescriptor::isReadable(filename)) { if (NexusDescriptor::isReadable(filename)) {
loader = searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>( loader = searchForLoader<NexusDescriptor, IFileLoader<NexusDescriptor>>(
filename, names, m_log); filename, names, m_log);
} }
} else { } else if (nexusHDF5) {
if (NexusHDF5Descriptor::isReadable(filename)) {
try {
loader = searchForLoader<NexusHDF5Descriptor,
IFileLoader<NexusHDF5Descriptor>>(
filename, names, m_log);
} 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>>( loader = searchForLoader<FileDescriptor, IFileLoader<FileDescriptor>>(
filename, names, m_log); filename, names, m_log);
} }
return static_cast<bool>(loader); return static_cast<bool>(loader);
} }
...@@ -179,10 +196,11 @@ bool FileLoaderRegistryImpl::canLoad(const std::string &algorithmName, ...@@ -179,10 +196,11 @@ bool FileLoaderRegistryImpl::canLoad(const std::string &algorithmName,
//---------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------
/** /**
* Creates an empty registry * Creates an empty registry
*
* m_names is initialized in the header
*/ */
FileLoaderRegistryImpl::FileLoaderRegistryImpl() FileLoaderRegistryImpl::FileLoaderRegistryImpl()
: m_names(2, std::multimap<std::string, int>()), m_totalSize(0), : m_totalSize(0), m_log("FileLoaderRegistry") {}
m_log("FileLoaderRegistry") {}
FileLoaderRegistryImpl::~FileLoaderRegistryImpl() = default; FileLoaderRegistryImpl::~FileLoaderRegistryImpl() = default;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment