Mantid
Loading...
Searching...
No Matches
LoadMuonLog.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 +
9#include "MantidAPI/Run.h"
10#include "MantidAPI/Sample.h"
13
14#include <algorithm>
15#include <ctime>
16
17namespace {
18void toLower(std::string &name) {
19 std::transform(name.begin(), name.end(), name.begin(), [](unsigned char c) { return std::tolower(c); });
20}
21} // namespace
22
23namespace Mantid::DataHandling {
24
25// Register the algorithm into the algorithm factory
26DECLARE_ALGORITHM(LoadMuonLog)
27
28using namespace Kernel;
29using API::FileProperty;
30using API::MatrixWorkspace;
32using API::Progress;
33using API::WorkspaceProperty;
35
37LoadMuonLog::LoadMuonLog() = default;
38
41 // When used as a Child Algorithm the workspace name is not used - hence the
42 // "Anonymous" to satisfy the validator
43 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("Workspace", "Anonymous", Direction::InOut),
44 "The name of the workspace to which the log data will be added.");
45 declareProperty(std::make_unique<FileProperty>("Filename", "", FileProperty::Load),
46 "The filename (including its full or relative path) of the "
47 "Muon Nexus file.");
48}
49
57 // Retrieve the filename from the properties and perform some initial checks
58 // on the filename
59
60 m_filename = getPropertyValue("Filename");
61 MuonNexusReader nxload;
62 nxload.readLogData(m_filename);
63
64 // Get the input workspace and retrieve sample from workspace.
65 // the log data will be loaded into the Sample container of the workspace
66 // Also set the sample name at this point, as part of the sample related log
67 // data.
68
69 MatrixWorkspace_sptr localWorkspace = getProperty("Workspace");
70 localWorkspace->mutableSample().setName(nxload.getSampleName());
71
72 std::set<std::string> logNames;
73 auto logs = localWorkspace->mutableRun().getLogData();
74 // need to remove case
75 for (auto log : logs) {
76 std::string name = log->name();
77 toLower(name);
78 logNames.insert(name);
79 }
80
81 // Attempt to load the content of each NXlog section into the Sample object
82 // Assumes that MuonNexusReader has read all log data
83 // Two cases of double or string data allowed
84 Progress prog(this, 0.0, 1.0, nxload.numberOfLogs());
85 for (int i = 0; i < nxload.numberOfLogs(); i++) {
86 addLogValueFromIndex(nxload, i, localWorkspace, logNames);
87 prog.report();
88 }
89 // operation was a success and ended normally
90}
91
93 API::MatrixWorkspace_sptr &localWorkspace, std::set<std::string> &logNames) {
94 std::string newLogName = nxload.getLogName(index);
95 // want to keep the original case for the logs
96 std::string logNameLower = nxload.getLogName(index);
97 toLower(logNameLower);
98 // check if log name already exists
99 if (logNames.find(logNameLower) != logNames.end()) {
100 g_log.warning("The log " + newLogName +
101 " is already in the nexus file. The sample log names are "
102 "case insensitive.");
103 return;
104 }
105 logNames.insert(newLogName);
106 auto l_PropertyDouble = std::make_unique<TimeSeriesProperty<double>>(newLogName);
107 auto l_PropertyString = std::make_unique<TimeSeriesProperty<std::string>>(newLogName);
108
109 // Read log file into Property which is then stored in Sample object
110 if (!nxload.logTypeNumeric(index)) {
111 std::string logValue;
112 std::time_t logTime;
113 for (int j = 0; j < nxload.getLogLength(index); j++) {
114 nxload.getLogStringValues(index, j, logTime, logValue);
115 l_PropertyString->addValue(logTime, logValue);
116 l_PropertyString->setUnits(nxload.logUnits(index));
117 }
118 } else {
119 double logValue;
120 std::time_t logTime;
121 for (int j = 0; j < nxload.getLogLength(index); j++) {
122 nxload.getLogValues(index, j, logTime, logValue);
123 l_PropertyDouble->addValue(logTime, logValue);
124 l_PropertyDouble->setUnits(nxload.logUnits(index));
125 }
126 }
127
128 // store Property in Sample object and delete unused object
129 if (nxload.logTypeNumeric(index)) {
130 localWorkspace->mutableRun().addLogData(std::move(l_PropertyDouble));
131 } else {
132 localWorkspace->mutableRun().addLogData(std::move(l_PropertyString));
133 }
134}
135
140std::string LoadMuonLog::stringToLower(std::string strToConvert) {
141 std::transform(strToConvert.begin(), strToConvert.end(), strToConvert.begin(), ::tolower);
142 return strToConvert; // return the converted string
143}
144
150bool LoadMuonLog::isDateTimeString(const std::string &str) {
151 if (str.size() >= 19)
152 if (str.compare(4, 1, "-") == 0 && str.compare(7, 1, "-") == 0 && str.compare(13, 1, ":") == 0 &&
153 str.compare(16, 1, ":") == 0 && str.compare(10, 1, "T") == 0)
154 return true;
155
156 return false;
157}
158
159} // namespace Mantid::DataHandling
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
std::map< DeltaEMode::Type, std::string > index
Definition: DeltaEMode.cpp:19
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
std::string getPropertyValue(const std::string &name) const override
Get the value of a property as a string.
Definition: Algorithm.cpp:2026
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
@ Load
allowed here which will be passed to the algorithm
Definition: FileProperty.h:52
Helper class for reporting progress from algorithms.
Definition: Progress.h:25
A property class for workspaces.
std::string m_filename
The name and path of an input file.
Definition: LoadMuonLog.h:69
void init() override
Overwrites Algorithm method.
Definition: LoadMuonLog.cpp:40
LoadMuonLog()
Default constructor.
const std::string name() const override
Algorithm's name for identification overriding a virtual method.
Definition: LoadMuonLog.h:48
bool isDateTimeString(const std::string &str)
check if first 19 characters of a string is data-time string according to yyyy-mm-ddThh:mm:ss
void exec() override
Overwrites Algorithm method.
Definition: LoadMuonLog.cpp:56
void addLogValueFromIndex(MuonNexusReader &nxload, const int &index, API::MatrixWorkspace_sptr &localWorkspace, std::set< std::string > &logNames)
Adds a log to the workspace.
Definition: LoadMuonLog.cpp:92
std::string stringToLower(std::string strToConvert)
convert string to lower case
void warning(const std::string &msg)
Logs at warning level.
Definition: Logger.cpp:86
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
Definition: ProgressBase.h:51
MuunNexusReader opens a Nexus file and reads certain fields expected for a ISIS Muon data file (old f...
int numberOfLogs() const
Number of NXlog sections read from file.
std::string logUnits(const int i) const
void readLogData(const std::string &filename)
read log data
std::string getSampleName() const
int getLogLength(const int i) const
Lenght of i'th log.
bool logTypeNumeric(const int i) const
true if i'th log is of numeric type
void getLogValues(const int &logNumber, const int &logSequence, std::time_t &logTime, double &value)
get logSequence pair of logNumber log
std::string getLogName(const int i) const
Name of i'th log.
void getLogStringValues(const int &logNumber, const int &logSequence, std::time_t &logTime, std::string &value)
get logSequence pair of logNumber string log
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
std::shared_ptr< Workspace2D > Workspace2D_sptr
shared pointer to Mantid::DataObjects::Workspace2D
MANTID_KERNEL_DLL std::string toLower(const std::string &input)
Converts string to all lowercase.
Definition: Strings.cpp:116
@ InOut
Both an input & output workspace.
Definition: Property.h:55