Mantid
Loading...
Searching...
No Matches
ShiftLogTime.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 "MantidKernel/System.h"
12
13#include <sstream>
14
15using namespace Mantid::Kernel;
16using namespace Mantid::API;
17using Mantid::Types::Core::DateAndTime;
18using std::string;
19using std::stringstream;
20using std::vector;
21
22namespace Mantid::Algorithms {
23
24// Register the algorithm into the AlgorithmFactory
25DECLARE_ALGORITHM(ShiftLogTime)
26
27
28const string ShiftLogTime::name() const { return "ShiftLogTime"; }
29
31int ShiftLogTime::version() const { return 1; }
32
34const string ShiftLogTime::category() const { return "DataHandling\\Logs"; }
35
39 declareProperty(std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("InputWorkspace", "", Direction::Input),
40 "A workspace with units of TOF");
41 declareProperty(std::make_unique<WorkspaceProperty<API::MatrixWorkspace>>("OutputWorkspace", "", Direction::Output),
42 "The name to use for the output workspace");
43 this->declareProperty("LogName", "", "Name of the log to add the offset to");
44 this->declareProperty(std::make_unique<PropertyWithValue<int>>("IndexShift", Direction::Input),
45 "Number of (integer) values to move the log values. Required.");
46}
47
51 // check that a log was specified
52 string logname = this->getProperty("LogName");
53 if (logname.empty()) {
54 throw std::runtime_error("Failed to supply a LogName");
55 }
56 // everything will need an offset
57 int indexshift = this->getProperty("IndexShift");
58
59 // make sure the log is in the input workspace
60 MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
61 auto *oldlog = dynamic_cast<Kernel::TimeSeriesProperty<double> *>(inputWS->run().getLogData(logname));
62 if (!oldlog) {
63 stringstream msg;
64 msg << "InputWorkspace \'" << this->getPropertyValue("InputWorkspace") << "\' does not have LogName \'" << logname
65 << "\'";
66 throw std::runtime_error(msg.str());
67 }
68
69 // get the data out of the old log
70 int size = oldlog->realSize();
71 if (abs(indexshift) > size) {
72 stringstream msg;
73 msg << "IndexShift (=" << indexshift << ") is larger than the log length (=" << size << ")";
74 throw std::runtime_error(msg.str());
75 }
76 vector<double> values = oldlog->valuesAsVector();
77 vector<DateAndTime> times = oldlog->timesAsVector();
78
79 // 'fix' the indices
80 if (indexshift == 0) {
81 // nothing to do
82 } else if (indexshift > 0) {
83 values.erase(values.end() - indexshift, values.end());
84 times.erase(times.begin(), times.begin() + indexshift);
85 } else // indexshift < 0
86 {
87 // indexshift<0, so -indexshift>0
88 values.erase(values.begin(), values.begin() - indexshift);
89 times.erase(times.end() + indexshift, times.end());
90 }
91
92 // Create the new log
93 auto newlog = new TimeSeriesProperty<double>(logname);
94 newlog->setUnits(oldlog->units());
95 newlog->create(times, values);
96
97 // Just overwrite if the change is in place
98 MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
99 if (outputWS != inputWS) {
100 auto duplicate = createChildAlgorithm("CloneWorkspace");
101 duplicate->initialize();
102 duplicate->setProperty<Workspace_sptr>("InputWorkspace", std::dynamic_pointer_cast<Workspace>(inputWS));
103 duplicate->execute();
104 Workspace_sptr temp = duplicate->getProperty("OutputWorkspace");
105 outputWS = std::dynamic_pointer_cast<MatrixWorkspace>(temp);
106
107 setProperty("OutputWorkspace", outputWS);
108 }
109
110 outputWS->mutableRun().addProperty(newlog, true);
111}
112
113} // 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.
ShiftLogTime : TODO: DESCRIPTION.
Definition: ShiftLogTime.h:19
void exec() override
Run the algorithm.
void init() override
Sets documentation strings for this algorithm.
const std::string category() const override
Algorithm's category for identification.
int version() const override
Algorithm's version 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