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