Mantid
Loading...
Searching...
No Matches
ChangeLogTime.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 +
8#include "MantidAPI/Run.h"
11#include <sstream>
12
13namespace Mantid::Algorithms {
14
15// Register the algorithm into the AlgorithmFactory
16DECLARE_ALGORITHM(ChangeLogTime)
17
18using std::string;
19using std::stringstream;
20using std::vector;
21using namespace Mantid::API;
22using namespace Mantid::DataObjects;
23using namespace Mantid::Kernel;
24using Types::Core::DateAndTime;
25
27const string ChangeLogTime::name() const { return "ChangeLogTime"; }
28
30int ChangeLogTime::version() const { return 1; }
31
33const std::string ChangeLogTime::category() const { return "DataHandling\\Logs"; }
34
37 declareProperty(std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
38 "A workspace");
39 declareProperty(std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
40 "The name to use for the output workspace");
41 this->declareProperty("LogName", "", "Name of the log to add the offset to");
42 this->declareProperty(std::make_unique<PropertyWithValue<double>>("TimeOffset", Direction::Input),
43 "Number of seconds (a float) to add to the time of each log value. "
44 "Required.");
45}
46
49 // check that a log was specified
50 string logname = this->getProperty("LogName");
51 if (logname.empty()) {
52 throw std::runtime_error("Failed to supply a LogName");
53 }
54 // everything will need an offset
55 double offset = this->getProperty("TimeOffset");
56
57 // make sure the log is in the input workspace
58 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
59 auto *oldlog = dynamic_cast<Kernel::TimeSeriesProperty<double> *>(inputWS->run().getLogData(logname));
60 if (!oldlog) {
61 stringstream msg;
62 msg << "InputWorkspace \'" << this->getPropertyValue("InputWorkspace") << "\' does not have LogName \'" << logname
63 << "\'";
64 throw std::runtime_error(msg.str());
65 }
66
67 // Create the new log
68 auto newlog = new TimeSeriesProperty<double>(logname);
69 newlog->setUnits(oldlog->units());
70 int size = oldlog->realSize();
71 vector<double> values = oldlog->valuesAsVector();
72 vector<DateAndTime> times = oldlog->timesAsVector();
73 for (int i = 0; i < size; i++) {
74 newlog->addValue(times[i] + offset, values[i]);
75 }
76
77 // Just overwrite if the change is in place
78 MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
79 if (outputWS != inputWS) {
80 auto duplicate = createChildAlgorithm("CloneWorkspace");
81 duplicate->initialize();
82 duplicate->setProperty<Workspace_sptr>("InputWorkspace", std::dynamic_pointer_cast<Workspace>(inputWS));
83 duplicate->execute();
84 Workspace_sptr temp = duplicate->getProperty("OutputWorkspace");
85 outputWS = std::dynamic_pointer_cast<MatrixWorkspace>(temp);
86
87 setProperty("OutputWorkspace", outputWS);
88 }
89
90 outputWS->mutableRun().addProperty(newlog, true);
91}
92
93} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
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
virtual std::shared_ptr< Algorithm > createChildAlgorithm(const std::string &name, const double startProgress=-1., const double endProgress=-1., const bool enableLogging=true, const int &version=-1)
Create a Child Algorithm.
Definition: Algorithm.cpp:842
A property class for workspaces.
void exec() override
Run the algorithm.
int version() const override
Algorithm's version for identification.
const std::string name() const override
Algorithm's name for identification.
void init() override
Initialise the properties.
const std::string category() const override
Algorithm's category for identification.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
The concrete, templated class for properties.
A specialised Property class for holding a series of time-value pairs.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
Definition: Workspace_fwd.h:20
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ Input
An input workspace.
Definition: Property.h:53
@ Output
An output workspace.
Definition: Property.h:54