Newer
Older
#include "MantidAlgorithms/AddTimeSeriesLog.h"
#include "MantidKernel/DateTimeValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/TimeSeriesProperty.h"
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
namespace Mantid {
namespace Algorithms {
using namespace API;
using namespace Kernel;
namespace {
/**
* Creates/updates the named log on the given run. The template type
* specifies either the type of TimeSeriesProperty that is expected to
* exist or the type that will be created
* @param run A reference to the run object that stores the logs
* @param name The name of the log that is to be either created or updated
* @param time A time string in ISO format, passed to the DateAndTime
* constructor
* @param value The value at the given time
*/
template <typename T>
void createOrUpdateValue(API::Run &run, const std::string &name,
const std::string &time, const T value) {
TimeSeriesProperty<T> *timeSeries(NULL);
if (run.hasProperty(name)) {
timeSeries = dynamic_cast<TimeSeriesProperty<T> *>(run.getLogData(name));
if (!timeSeries)
throw std::invalid_argument(
"Log '" + name +
"' already exists but the values are a different type.");
} else {
timeSeries = new TimeSeriesProperty<T>(name);
run.addProperty(timeSeries);
}
timeSeries->addValue(time, value);
}
}
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(AddTimeSeriesLog)
//----------------------------------------------------------------------------------------------
/// Algorithm's name for identification. @see Algorithm::name
const std::string AddTimeSeriesLog::name() const { return "AddTimeSeriesLog"; }
/// Algorithm's version for identification. @see Algorithm::version
int AddTimeSeriesLog::version() const { return 1; }
/// Algorithm's category for identification. @see Algorithm::category
const std::string AddTimeSeriesLog::category() const {
return "DataHandling\\Logs";
}
//----------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------
/**
* Initialize the algorithm's properties.
*/
void AddTimeSeriesLog::init() {
declareProperty(
new WorkspaceProperty<MatrixWorkspace>("Workspace", "", Direction::InOut),
"In/out workspace that will store the new log information");
declareProperty(
"Name", "", boost::make_shared<MandatoryValidator<std::string>>(),
"A string name for either a new time series log to be created "
"or an existing name to update",
Direction::Input);
declareProperty(
"Time", "", boost::make_shared<DateTimeValidator>(),
"An ISO formatted date/time string specifying the timestamp for "
"the given log value, e.g 2010-09-14T04:20:12",
Direction::Input);
auto nonEmtpyDbl = boost::make_shared<MandatoryValidator<double>>();
declareProperty("Value", EMPTY_DBL(), nonEmtpyDbl,
"The value for the log at the given time", Direction::Input);
auto optionsValidator = boost::make_shared<ListValidator<std::string>>();
optionsValidator->addAllowedValue("double");
optionsValidator->addAllowedValue("int");
declareProperty("Type", "double", optionsValidator,
"An optional type for the given value. A double value with a "
"Type=int will have "
"the fractional part chopped off.",
Direction::Input);
declareProperty(
"DeleteExisting", false,
"If true and the named log exists then the whole log is removed first.",
Direction::Input);
}
//----------------------------------------------------------------------------------------------
/**
* Execute the algorithm.
*/
void AddTimeSeriesLog::exec() {
MatrixWorkspace_sptr logWS = getProperty("Workspace");
std::string name = getProperty("Name");
const bool deleteExisting = getProperty("DeleteExisting");
auto &run = logWS->mutableRun();
if (deleteExisting && run.hasProperty(name))
removeExisting(logWS, name);
createOrUpdate(run, name);
}
/**
* @param logWS The workspace containing the log
* @param name The name of the log to delete
*/
void AddTimeSeriesLog::removeExisting(API::MatrixWorkspace_sptr &logWS,
const std::string &name) {
auto deleter = createChildAlgorithm("DeleteLog", -1, -1, false);
deleter->setProperty("Workspace", logWS);
deleter->setProperty("Name", name);
deleter->executeAsChildAlg();
}
/**
* @param run The run object that either has the log or will store it
* @param name The name of the log to create/update
*/
void AddTimeSeriesLog::createOrUpdate(API::Run &run, const std::string &name) {
std::string time = getProperty("Time");
double valueAsDouble = getProperty("Value");
std::string type = getProperty("Type");
bool asInt = (type == "int");
if (asInt) {
createOrUpdateValue<int>(run, name, time, static_cast<int>(valueAsDouble));
} else {
createOrUpdateValue<double>(run, name, time, valueAsDouble);
}
}
} // namespace Algorithms
} // namespace Mantid