Mantid
Loading...
Searching...
No Matches
StartAndEndTimeFromNexusFileExtractor.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 +
11#include "MantidKernel/Logger.h"
14
15#include <vector>
16
17namespace {
18Mantid::Kernel::Logger g_log("StartAndEndTimeFromNexusFileExtractor");
19}
20
21namespace Mantid::DataHandling {
22
24enum class TimeType : unsigned char { StartTime, EndTime };
25
26Mantid::Types::Core::DateAndTime handleMuonNexusFile(TimeType type, const std::string &filename) {
27 Mantid::NeXus::NXRoot root(filename);
28 if (type == TimeType::StartTime) {
29 return Mantid::Types::Core::DateAndTime(root.getString("run/start_time"));
30 } else {
31 return Mantid::Types::Core::DateAndTime(root.getString("run/stop_time"));
32 }
33}
34
35Mantid::Types::Core::DateAndTime handleProcessedNexusFile(TimeType type, const std::string &filename) {
36 Mantid::NeXus::NXRoot root(filename);
37 if (type == TimeType::StartTime) {
38 return Mantid::Types::Core::DateAndTime(root.getString("mantid_workspace_1/logs/run_start/value"));
39 } else {
40 return Mantid::Types::Core::DateAndTime(root.getString("mantid_workspace_1/logs/run_end/value"));
41 }
42}
43
44Mantid::Types::Core::DateAndTime handleISISNexusFile(TimeType type, const std::string &filename) {
45 Mantid::NeXus::NXRoot root(filename);
46 if (type == TimeType::StartTime) {
47 return Mantid::Types::Core::DateAndTime(root.getString("raw_data_1/start_time"));
48 } else {
49 return Mantid::Types::Core::DateAndTime(root.getString("raw_data_1/end_time"));
50 }
51}
52
53Mantid::Types::Core::DateAndTime handleTofRawNexusFile(TimeType type, const std::string &filename) {
54 Mantid::NeXus::NXRoot root(filename);
55 if (type == TimeType::StartTime) {
56 return Mantid::Types::Core::DateAndTime(root.getString("entry/start_time"));
57 } else {
58 return Mantid::Types::Core::DateAndTime(root.getString("entry/end_time"));
59 }
60}
61
62NexusType whichNexusType(const std::string &filename) {
63 std::vector<std::string> entryName;
64 std::vector<std::string> definition;
65 auto count = Mantid::NeXus::getNexusEntryTypes(filename, entryName, definition);
66
67 // Issues with the file
68 if (count <= -1) {
69 g_log.error("Error reading file " + filename);
70 throw Mantid::Kernel::Exception::FileError("Unable to read data in File:", filename);
71 } else if (count == 0) {
72 g_log.error("Error no entries found in " + filename);
73 throw Mantid::Kernel::Exception::FileError("Error no entries found in ", filename);
74 }
75
76 NexusType nexusType;
77 if (definition[0] == LoadNexus::muonTD || definition[0] == LoadNexus::pulsedTD) {
78 nexusType = NexusType::Muon;
79 } else if (entryName[0] == "mantid_workspace_1") {
80 nexusType = NexusType::Processed;
81
82 } else if (entryName[0] == "raw_data_1") {
83 nexusType = NexusType::ISIS;
84 } else {
85 Mantid::NeXus::NXRoot root(filename);
86 Mantid::NeXus::NXEntry entry = root.openEntry(root.groups().front().nxname);
87 try {
88 Mantid::NeXus::NXChar nxc = entry.openNXChar("instrument/SNSdetector_calibration_id");
89 } catch (...) {
90 g_log.error("File " + filename + " is a currently unsupported type of NeXus file");
91 throw Mantid::Kernel::Exception::FileError("Unable to read File:", filename);
92 }
93 nexusType = NexusType::TofRaw;
94 }
95
96 return nexusType;
97}
98
99Mantid::Types::Core::DateAndTime extractDateAndTime(TimeType type, const std::string &filename) {
100 auto fullFileName = Mantid::API::FileFinder::Instance().getFullPath(filename);
101 // Figure out the type of the Nexus file. We need to handle them individually
102 // since they store the datetime differently
103 auto nexusType = whichNexusType(fullFileName);
104 Mantid::Types::Core::DateAndTime dateAndTime;
105
106 switch (nexusType) {
107 case NexusType::Muon:
108 dateAndTime = handleMuonNexusFile(type, fullFileName);
109 break;
110 case NexusType::ISIS:
111 dateAndTime = handleISISNexusFile(type, fullFileName);
112 break;
114 dateAndTime = handleProcessedNexusFile(type, fullFileName);
115 break;
117 dateAndTime = handleTofRawNexusFile(type, fullFileName);
118 break;
119 default:
120 throw std::runtime_error("Unkown Nexus format. Not able to extract a date and time.");
121 };
122
123 return dateAndTime;
124}
125
132Mantid::Types::Core::DateAndTime extractStartTime(const std::string &filename) {
133 return extractDateAndTime(TimeType::StartTime, filename);
134}
135
142Mantid::Types::Core::DateAndTime extractEndTime(const std::string &filename) {
143 return extractDateAndTime(TimeType::EndTime, filename);
144}
145
146} // namespace Mantid::DataHandling
int count
counter
Definition: Matrix.cpp:37
static const std::string muonTD
Definition: LoadNexus.h:66
static const std::string pulsedTD
Definition: LoadNexus.h:67
Records the filename and the description of failure.
Definition: Exception.h:98
The Logger class is in charge of the publishing messages from the framework through various channels.
Definition: Logger.h:52
void error(const std::string &msg)
Logs at error level.
Definition: Logger.cpp:77
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
std::vector< NXClassInfo > & groups() const
Returns a list of all classes (or groups) in this NXClass.
Definition: NexusClasses.h:589
NXChar openNXChar(const std::string &name) const
Creates and opens a char dataset.
Definition: NexusClasses.h:561
std::string getString(const std::string &name) const
Returns a string.
Templated class implementation of NXDataSet.
Definition: NexusClasses.h:203
Implements NXentry Nexus class.
Definition: NexusClasses.h:898
Implements NXroot Nexus class.
Definition: NexusClasses.h:922
NXEntry openEntry(const std::string &name)
Opens an entry – a topmost Nexus class.
Definition: NexusClasses.h:939
Kernel::Logger g_log("ExperimentInfo")
static logger object
Mantid::Types::Core::DateAndTime DLLExport extractEndTime(const std::string &filename)
Gets the start time from the nexus file.
NexusType whichNexusType(const std::string &filename)
Mantid::Types::Core::DateAndTime handleProcessedNexusFile(TimeType type, const std::string &filename)
Mantid::Types::Core::DateAndTime handleISISNexusFile(TimeType type, const std::string &filename)
Mantid::Types::Core::DateAndTime extractDateAndTime(TimeType type, const std::string &filename)
Mantid::Types::Core::DateAndTime handleMuonNexusFile(TimeType type, const std::string &filename)
Mantid::Types::Core::DateAndTime DLLExport extractStartTime(const std::string &filename)
Extracts the start and the end time from a Nexus file.
Mantid::Types::Core::DateAndTime handleTofRawNexusFile(TimeType type, const std::string &filename)
MANTID_NEXUS_DLL int getNexusEntryTypes(const std::string &fileName, std::vector< std::string > &entryName, std::vector< std::string > &definition)
Get all the Nexus entry types for a file.