Mantid
Loading...
Searching...
No Matches
AddTimeSeriesLog.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"
14
15namespace Mantid::Algorithms {
16using namespace API;
17using namespace Kernel;
18
19namespace {
30template <typename T>
31void createOrUpdateValue(API::Run &run, const std::string &name, const std::string &time, const T value) {
32 TimeSeriesProperty<T> *timeSeries(nullptr);
33 if (run.hasProperty(name)) {
34 timeSeries = dynamic_cast<TimeSeriesProperty<T> *>(run.getLogData(name));
35 if (!timeSeries)
36 throw std::invalid_argument("Log '" + name + "' already exists but the values are a different type.");
37 } else {
38 timeSeries = new TimeSeriesProperty<T>(name);
39 run.addProperty(timeSeries);
40 }
41 timeSeries->addValue(time, value);
42}
43} // namespace
44
45// Register the algorithm into the AlgorithmFactory
46DECLARE_ALGORITHM(AddTimeSeriesLog)
47
48//----------------------------------------------------------------------------------------------
50const std::string AddTimeSeriesLog::name() const { return "AddTimeSeriesLog"; }
51
53int AddTimeSeriesLog::version() const { return 1; }
54
56const std::string AddTimeSeriesLog::category() const { return "DataHandling\\Logs"; }
57
58//----------------------------------------------------------------------------------------------
59
60//----------------------------------------------------------------------------------------------
66 "In/out workspace that will store the new log information");
67
68 declareProperty("Name", "", std::make_shared<MandatoryValidator<std::string>>(),
69 "A string name for either a new time series log to be created "
70 "or an existing name to update",
72 declareProperty("Time", "", std::make_shared<DateTimeValidator>(),
73 "An ISO formatted date/time string specifying the timestamp for "
74 "the given log value, e.g 2010-09-14T04:20:12",
76 auto nonEmtpyDbl = std::make_shared<MandatoryValidator<double>>();
77 declareProperty("Value", EMPTY_DBL(), nonEmtpyDbl, "The value for the log at the given time", Direction::Input);
78
79 auto optionsValidator = std::make_shared<ListValidator<std::string>>();
80 optionsValidator->addAllowedValue("double");
81 optionsValidator->addAllowedValue("int");
82 declareProperty("Type", "double", optionsValidator,
83 "An optional type for the given value. A double value with a "
84 "Type=int will have "
85 "the fractional part chopped off.",
87 declareProperty("DeleteExisting", false, "If true and the named log exists then the whole log is removed first.",
89}
90
91//----------------------------------------------------------------------------------------------
96 MatrixWorkspace_sptr logWS = getProperty("Workspace");
97 std::string name = getProperty("Name");
98
99 const bool deleteExisting = getProperty("DeleteExisting");
100 auto &run = logWS->mutableRun();
101 if (deleteExisting && run.hasProperty(name))
102 removeExisting(logWS, name);
103
104 createOrUpdate(run, name);
105}
106
111void AddTimeSeriesLog::removeExisting(API::MatrixWorkspace_sptr &logWS, const std::string &name) {
112 auto deleter = createChildAlgorithm("DeleteLog", -1, -1, false);
113 deleter->setProperty("Workspace", logWS);
114 deleter->setProperty("Name", name);
115 deleter->executeAsChildAlg();
116}
117
122void AddTimeSeriesLog::createOrUpdate(API::Run &run, const std::string &name) {
123 std::string time = getProperty("Time");
124 double valueAsDouble = getProperty("Value");
125 std::string type = getProperty("Type");
126 bool asInt = (type == "int");
127
128 if (asInt) {
129 createOrUpdateValue<int>(run, name, time, static_cast<int>(valueAsDouble));
130 } else {
131 createOrUpdateValue<double>(run, name, time, valueAsDouble);
132 }
133}
134
135} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
double value
The value of the point.
Definition: FitMW.cpp:51
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
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
This class stores information regarding an experimental run as a series of log entries.
Definition: Run.h:38
A property class for workspaces.
void exec() override
Execute the algorithm.
int version() const override
Algorithm's version for identification.
void init() override
Initialize the algorithm's properties.
const std::string category() const override
Algorithm's category for identification.
void createOrUpdate(API::Run &run, const std::string &name)
Create or update the named log entry.
void removeExisting(API::MatrixWorkspace_sptr &logWS, const std::string &name)
Remove an existing log of the given name.
const std::string name() const override
Algorithm's name for identification.
Validator to check that a property is not left empty.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
constexpr double EMPTY_DBL() noexcept
Returns what we consider an "empty" double within a property.
Definition: EmptyValues.h:43
STL namespace.
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Input
An input workspace.
Definition: Property.h:53