-
Notifications
You must be signed in to change notification settings - Fork 122
/
AddTimeSeriesLog.cpp
141 lines (122 loc) · 5.47 KB
/
AddTimeSeriesLog.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*WIKI*
Creates/updates a time-series log entry on a chosen workspace. The given timestamp & value are appended to the
named log entry. If the named entry does not exist then a new log is created. A time stamp must be given in
ISO8601 format, e.g. 2010-09-14T04:20:12.
By default, the given value is interpreted as a double and a double series is either created or expected. However,
if the "Type" is set to "int" then the value is interpreted as an integer and an integer is either created
or expected.
*WIKI*/
/*WIKI_USAGE*
'''Python'''
import datetime as dt
# Add an entry for the current time
log_name = "temperature"
log_value = 21.5
AddTimeSeriesLog(inOutWS, Name=log_name, Time=dt.datetime.utcnow().isoformat(), Value=log_value)
*WIKI_USAGE*/
#include "MantidAlgorithms/AddTimeSeriesLog.h"
#include "MantidKernel/DateTimeValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/TimeSeriesProperty.h"
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 createOrUpdate(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"; }
//----------------------------------------------------------------------------------------------
/// Sets documentation strings for this algorithm
void AddTimeSeriesLog::initDocs()
{
this->setWikiSummary("Creates/updates a time-series log");
this->setOptionalMessage("Creates/updates a time-series log");
}
//----------------------------------------------------------------------------------------------
/**
* 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);
}
//----------------------------------------------------------------------------------------------
/**
* Execute the algorithm.
*/
void AddTimeSeriesLog::exec()
{
MatrixWorkspace_sptr logWS = getProperty("Workspace");
auto &run = logWS->mutableRun();
std::string name = getProperty("Name");
std::string time = getProperty("Time");
double valueAsDouble = getProperty("Value");
std::string type = getProperty("Type");
bool asInt = (type == "int");
if(asInt)
{
createOrUpdate<int>(run, name, time, static_cast<int>(valueAsDouble));
}
else
{
createOrUpdate<double>(run, name, time, valueAsDouble);
}
}
} // namespace Algorithms
} // namespace Mantid