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"
12#include "MantidNexus/NexusFile.h"
13#include <vector>
14
15namespace Mantid::DataHandling {
16
17namespace {
18Mantid::Kernel::Logger g_log("StartAndEndTimeFromNexusFileExtractor");
19
20enum class NexusType { Muon, Processed, ISIS, TofRaw };
21enum class TimeType { StartTime, EndTime };
22
23Mantid::Types::Core::DateAndTime getValue(const std::string &filename, const std::string &absAddress) {
24 Nexus::File nxfile(filename, NXaccess::READ);
25 nxfile.openAddress(absAddress);
26 const auto valueStr = nxfile.getStrData();
27 g_log.debug(valueStr + " from " + absAddress + " in " + filename);
28 return Mantid::Types::Core::DateAndTime(valueStr);
29}
30
34NexusType whichNexusType(const std::string &filename) {
35 std::vector<std::string> entryName;
36 std::vector<std::string> definition;
37 auto count = LoadNexus::getNexusEntryTypes(filename, entryName, definition);
38
39 // Issues with the file
40 if (count <= -1) {
41 g_log.error("Error reading file " + filename);
42 throw Mantid::Kernel::Exception::FileError("Unable to read data in File:", filename);
43 } else if (count == 0) {
44 g_log.error("Error no entries found in " + filename);
45 throw Mantid::Kernel::Exception::FileError("Error no entries found in ", filename);
46 }
47
48 NexusType nexusType;
49 if (definition[0] == LoadNexus::muonTD || definition[0] == LoadNexus::pulsedTD) {
50 nexusType = NexusType::Muon;
51 } else if (entryName[0] == "mantid_workspace_1") {
52 nexusType = NexusType::Processed;
53
54 } else if (entryName[0] == "raw_data_1") {
55 nexusType = NexusType::ISIS;
56 } else {
57 Nexus::File nxfile(filename, NXaccess::READ);
58 const auto entries = nxfile.getEntries();
59 const auto firstEntryName = entries.begin()->first;
60 try {
61 nxfile.openAddress("/" + firstEntryName + "/instrument/SNSdetector_calibration_id");
62 } catch (...) {
63 g_log.error("File " + filename + " is a currently unsupported type of NeXus file");
64 throw Mantid::Kernel::Exception::FileError("Unable to read File:", filename);
65 }
66 nexusType = NexusType::TofRaw;
67 }
68
69 return nexusType;
70}
71
75std::string getDataFieldAddress(const NexusType nexusType, const TimeType type) {
76 std::string datafieldaddress;
77 switch (nexusType) {
78 case NexusType::Muon:
79 if (type == TimeType::StartTime) {
80 datafieldaddress = "/run/start_time";
81 } else {
82 datafieldaddress = "/run/stop_time";
83 }
84 break;
85 case NexusType::ISIS:
86 if (type == TimeType::StartTime) {
87 datafieldaddress = "/raw_data_1/start_time";
88 } else {
89 datafieldaddress = "/raw_data_1/end_time";
90 }
91 break;
92 case NexusType::Processed:
93 if (type == TimeType::StartTime) {
94 datafieldaddress = "/mantid_workspace_1/logs/run_start/value";
95 } else {
96 datafieldaddress = "/mantid_workspace_1/logs/run_end/value";
97 }
98 break;
99 case NexusType::TofRaw:
100 if (type == TimeType::StartTime) {
101 datafieldaddress = "/entry/start_time";
102 } else {
103 datafieldaddress = "/entry/end_time";
104 }
105 break;
106 default:
107 throw std::runtime_error("Unkown Nexus format. Not able to extract a date and time.");
108 };
109
110 return datafieldaddress;
111}
112
116Mantid::Types::Core::DateAndTime extractDateAndTime(TimeType type, const std::string &filename) {
117 // convert relative to absolute path
118 auto fullFileName = Mantid::API::FileFinder::Instance().getFullPath(filename);
119 // Figure out the type of the Nexus file. We need to handle them individually since they store the datetime
120 // differently
121 auto nexusType = whichNexusType(fullFileName);
122 // convert the type information into a address within the nexus file
123 const auto dataAddress = getDataFieldAddress(nexusType, type);
124 // return the result
125 return getValue(fullFileName, dataAddress);
126}
127} // namespace
128
135Mantid::Types::Core::DateAndTime extractStartTime(const std::string &filename) {
136 return extractDateAndTime(TimeType::StartTime, filename);
137}
138
145Mantid::Types::Core::DateAndTime extractEndTime(const std::string &filename) {
146 return extractDateAndTime(TimeType::EndTime, filename);
147}
148
149} // namespace Mantid::DataHandling
int count
counter
Definition Matrix.cpp:37
static 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.
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:51
void debug(const std::string &msg)
Logs at debug level.
Definition Logger.cpp:145
void error(const std::string &msg)
Logs at error level.
Definition Logger.cpp:108
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.
Mantid::Types::Core::DateAndTime DLLExport extractStartTime(const std::string &filename)
Extracts the start and the end time from a Nexus file.