Mantid
Loading...
Searching...
No Matches
LoadGeometry.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 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 +
10#include "MantidNexusGeometry/NexusGeometryDefinitions.h"
11
12namespace Mantid::DataHandling {
13
15bool LoadGeometry::isIDF(const std::string &filename) {
16 if (!filename.empty()) {
17 Mantid::Kernel::FileDescriptor descriptor(filename);
18 return descriptor.isXML();
19 }
20 return false;
21}
22
24bool LoadGeometry::isNexus(const std::string &filename) {
25 if (filename.empty() || Mantid::Kernel::FileDescriptor(filename).isAscii(filename)) {
26 return false;
27 }
28
29 if (H5::H5File::isHdf5(filename)) {
30 Mantid::Nexus::NexusDescriptor descriptor(filename);
31 return isNexus(descriptor.getAllEntries());
32 }
33
34 return false;
35}
36
37bool LoadGeometry::isNexus(const std::map<std::string, std::set<std::string>> &allEntries) {
38 // Checks if the nexus filename contains a valid Mantid geometry
39 if ((allEntries.count("NXcylindrical_geometry") != 1 && allEntries.count("NXoff_geometry") != 1 &&
40 allEntries.count("NXtransformations") != 1) ||
41 allEntries.count(Mantid::NexusGeometry::NX_SOURCE) != 1 ||
42 allEntries.count(Mantid::NexusGeometry::NX_SAMPLE) != 1) {
43 return false;
44 }
45 std::set<std::string> data_entries = allEntries.at("SDS");
46
47 if (allEntries.contains(Mantid::NexusGeometry::NX_DETECTOR)) {
48 std::set<std::string> det_entries = allEntries.at(Mantid::NexusGeometry::NX_DETECTOR);
49 for (auto it = det_entries.begin(); it != det_entries.end(); ++it) {
50 std::string id_name = *it + "/" + Mantid::NexusGeometry::DETECTOR_IDS;
51 if (data_entries.find(id_name) == data_entries.end()) {
52 return false;
53 }
54 }
55 }
56 if (allEntries.contains(Mantid::NexusGeometry::NX_MONITOR)) {
57 std::set<std::string> mon_entries = allEntries.at(Mantid::NexusGeometry::NX_MONITOR);
58 for (auto it = mon_entries.begin(); it != mon_entries.end(); ++it) {
59 std::string id_name = *it + "/" + Mantid::NexusGeometry::DETECTOR_ID;
60 if (data_entries.find(id_name) == data_entries.end()) {
61 return false;
62 }
63 }
64 }
65 return true;
66}
67
69const std::vector<std::string> LoadGeometry::validExtensions() { return {".xml", ".nxs", ".hdf5"}; }
70
71} // namespace Mantid::DataHandling
Defines a wrapper around an open file.
static bool isAscii(const std::string &filename, const size_t nbytes=256)
Returns true if the file is considered ascii.
bool isXML() const
Returns true if the descriptor is looking at an XML file.
const std::map< std::string, std::set< std::string > > & getAllEntries() const noexcept
Returns a const reference of the internal map holding all entries in the Nexus HDF5 file.
bool isIDF(const std::string &filename)
Determine if the Geometry file type is IDF.
const std::vector< std::string > validExtensions()
List allowed file extensions for geometry.
bool isNexus(const std::string &filename)
Determine if the Geometry file type is Nexus.