Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hdf4 support from NexusCpp #38954

Merged
merged 16 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Framework/API/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ set(TEST_FILES
FileBackedExperimentInfoTest.h
FileFinderTest.h
FilePropertyTest.h
FileLoaderRegistryTest.h
FrameworkManagerTest.h
FuncMinimizerFactoryTest.h
FunctionAttributeTest.h
Expand Down
69 changes: 69 additions & 0 deletions Framework/API/test/FileLoaderRegistryTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include "MantidAPI/FileFinder.h"
#include "MantidAPI/FileLoaderRegistry.h"
#include <cxxtest/TestSuite.h>

#include <filesystem>

using namespace Mantid::Kernel;
using namespace Mantid::API;

class FileLoaderRegistryTest : public CxxTest::TestSuite {
public:
// This pair of boilerplate methods prevent the suite being created statically
// This means the constructor (& destructor) isn't called when running other tests
static FileLoaderRegistryTest *createSuite() { return new FileLoaderRegistryTest(); }
static void destroySuite(FileLoaderRegistryTest *suite) { delete suite; }

FileLoaderRegistryTest() {}

~FileLoaderRegistryTest() override {}

void runCheck(const std::string &filename, const std::string &alg_expected, const int version_expected) {
std::string path = FileFinder::Instance().getFullPath(filename);

TSM_ASSERT("File not found: " + filename, !path.empty());

// get the right algorithm
try {
const auto loader = FileLoaderRegistry::Instance().chooseLoader(path);
if (loader) {
TSM_ASSERT_EQUALS(filename, loader->name(), alg_expected);
TSM_ASSERT_EQUALS(filename, loader->version(), version_expected);
} else {
TS_FAIL("Failed to find a loader for " + filename);
}
} catch (const std::runtime_error &e) {
TS_FAIL(e.what()); // no exception should be thrown
}
}

void testMuon() {
runCheck("MUSR00022725.nxs", "LoadMuonNexus", 1);
runCheck("emu00006473.nxs", "LoadMuonNexus", 1);
}

void testILL() {
// IN5 tests
const std::string path("ILL/IN5/");
runCheck(path + "095893.nxs", "LoadILLTOF", 2); // hdf4
runCheck(path + "104007.nxs", "LoadILLTOF", 3); // hdf5
}

void testSNS() {
runCheck("TOPAZ_3007.peaks.nxs", "LoadNexusProcessed", 2);
runCheck("PG3_733.nxs", "LoadNexusProcessed", 2);

runCheck("REF_L_183110.nxs.h5", "LoadEventNexus", 1);
runCheck("CNCS_7860_event.nxs", "LoadEventNexus", 1);
}

private:
};
5 changes: 4 additions & 1 deletion Framework/DataHandling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ set(SRC_FILES
src/LoadILLSALSA.cpp
src/LoadILLSANS.cpp
src/LoadILLTOF2.cpp
src/LoadILLTOF3.cpp
src/LoadISISNexus2.cpp
src/LoadISISNexusHelper.cpp
src/LoadISISPolarizationEfficiencies.cpp
Expand Down Expand Up @@ -293,6 +294,7 @@ set(INC_FILES
inc/MantidDataHandling/LoadILLSALSA.h
inc/MantidDataHandling/LoadILLSANS.h
inc/MantidDataHandling/LoadILLTOF2.h
inc/MantidDataHandling/LoadILLTOF3.h
inc/MantidDataHandling/LoadISISNexus2.h
inc/MantidDataHandling/LoadISISNexusHelper.h
inc/MantidDataHandling/LoadISISPolarizationEfficiencies.h
Expand Down Expand Up @@ -506,6 +508,7 @@ set(TEST_FILES
LoadILLSALSATest.h
LoadILLSANSTest.h
LoadILLTOF2Test.h
LoadILLTOF3Test.h
LoadILLTest.h
LoadISISNexusTest.h
LoadISISPolarizationEfficienciesTest.h
Expand Down Expand Up @@ -685,7 +688,7 @@ target_link_libraries(
Mantid::Indexing
Mantid::Parallel
Mantid::Muon
PRIVATE Mantid::Json Boost::filesystem Mantid::NexusGeometry
PRIVATE Mantid::Json Boost::filesystem Mantid::NexusGeometry Mantid::LegacyNexus
)

# Lib3mf is technically a public dependency as it is in our public headers. We are assuming here that it won't be used.
Expand Down
26 changes: 15 additions & 11 deletions Framework/DataHandling/inc/MantidDataHandling/LoadILLTOF2.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidGeometry/IDTypes.h"
#include "MantidKernel/NexusDescriptor.h"
#include "MantidNexus/NexusClasses_fwd.h"
#include "MantidKernel/LegacyNexusDescriptor.h"

namespace Mantid {
// forward declares
namespace LegacyNexus {
class NXEntry;
}
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::LegacyNexusDescriptor> {
public:
/// Constructor
LoadILLTOF2();
Expand All @@ -33,7 +36,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::LegacyNexusDescriptor &descriptor) const override;

private:
// Initialisation code
Expand All @@ -46,16 +49,17 @@ class MANTID_DATAHANDLING_DLL LoadILLTOF2 : public API::IFileLoader<Kernel::Nexu
void addFacility();
void addPulseInterval();

void fillStaticWorkspace(const NeXus::NXEntry &entry, const std::vector<std::string> &monitorList, bool convertToTOF);
void fillScanWorkspace(const NeXus::NXEntry &entry, const std::vector<std::string> &monitorList);
void fillStaticWorkspace(const LegacyNexus::NXEntry &entry, const std::vector<std::string> &monitorList,
bool convertToTOF);
void fillScanWorkspace(const LegacyNexus::NXEntry &entry, const std::vector<std::string> &monitorList);

std::vector<std::string> getMonitorInfo(const NeXus::NXEntry &firstEntry);
void initWorkspace(const NeXus::NXEntry &entry);
std::vector<std::string> getMonitorInfo(const LegacyNexus::NXEntry &firstEntry);
void initWorkspace(const LegacyNexus::NXEntry &entry);

void loadInstrumentDetails(const NeXus::NXEntry &);
void loadTimeDetails(const NeXus::NXEntry &entry);
void loadInstrumentDetails(const LegacyNexus::NXEntry &);
void loadTimeDetails(const LegacyNexus::NXEntry &entry);

std::vector<double> prepareAxis(const NeXus::NXEntry &entry, bool convertToTOF);
std::vector<double> prepareAxis(const LegacyNexus::NXEntry &entry, bool convertToTOF);

API::MatrixWorkspace_sptr m_localWorkspace;

Expand Down
81 changes: 81 additions & 0 deletions Framework/DataHandling/inc/MantidDataHandling/LoadILLTOF3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright &copy; 2025 ISIS Rutherford Appleton Laboratory UKRI,
// NScD Oak Ridge National Laboratory, European Spallation Source,
// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
// SPDX - License - Identifier: GPL - 3.0 +
#pragma once

#include "MantidAPI/IFileLoader.h"
#include "MantidDataHandling/DllConfig.h"
#include "MantidGeometry/IDTypes.h"
#include "MantidKernel/NexusDescriptor.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 LoadILLTOF3 : public API::IFileLoader<Kernel::NexusDescriptor> {
public:
/// Constructor
LoadILLTOF3();
/// Algorithm's name
const std::string name() const override { return "LoadILLTOF"; }
/// Summary of algorithms purpose
const std::string summary() const override { return "Loads an ILL TOF NeXus file."; }

/// Algorithm's version
int version() const override { return 3; }
const std::vector<std::string> seeAlso() const override { return {"LoadNexus"}; }
/// Algorithm's category for identification
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;

private:
// Initialisation code
void init() override;
// Execution code
void exec() override;

void addAllNexusFieldsAsProperties(const std::string &filename);
void addEnergyToRun();
void addFacility();
void addPulseInterval();

void fillStaticWorkspace(const NeXus::NXEntry &entry, const std::vector<std::string> &monitorList, bool convertToTOF);
void fillScanWorkspace(const NeXus::NXEntry &entry, const std::vector<std::string> &monitorList);

std::vector<std::string> getMonitorInfo(const NeXus::NXEntry &firstEntry);
void initWorkspace(const NeXus::NXEntry &entry);

void loadInstrumentDetails(const NeXus::NXEntry &);
void loadTimeDetails(const NeXus::NXEntry &entry);

std::vector<double> prepareAxis(const NeXus::NXEntry &entry, bool convertToTOF);

API::MatrixWorkspace_sptr m_localWorkspace;

std::string m_instrumentName; ///< Name of the instrument
std::string m_instrumentPath; ///< Name of the instrument path

// Variables describing the data in the detector
size_t m_numberOfTubes; // number of tubes - X
size_t m_numberOfPixelsPerTube; // number of pixels per tube - Y
size_t m_numberOfChannels; // time channels - Z
size_t m_numberOfHistograms; // number of histograms (individual detectors)
size_t m_numberOfMonitors; // number of monitors

// Values parsed from the nexus file
double m_wavelength;
double m_channelWidth;
double m_timeOfFlightDelay;
std::string m_monitorName;
bool m_isScan; // whether the loaded data is a scan measurement
};

} // namespace DataHandling
} // namespace Mantid
6 changes: 4 additions & 2 deletions Framework/DataHandling/src/LoadHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ using namespace API;
* Usually of the form: entry0/\<NXinstrument class\>/name
*/
std::string LoadHelper::findInstrumentNexusPath(const Mantid::NeXus::NXEntry &firstEntry) {
std::string result("");
std::vector<Mantid::NeXus::NXClassInfo> v = firstEntry.groups();
const auto it = std::find_if(v.cbegin(), v.cend(), [](const auto &group) { return group.nxclass == "NXinstrument"; });
if (it != v.cend())
return it->nxname;
return "";
result = it->nxname;

return result;
}

std::string LoadHelper::getStringFromNexusPath(const Mantid::NeXus::NXEntry &firstEntry, const std::string &nexusPath) {
Expand Down
Loading