Mantid
Loading...
Searching...
No Matches
AddNote.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 +
12#include "boost/date_time/local_time/posix_time_zone.hpp"
13
14namespace Mantid::Algorithms {
15
16using namespace API;
17using namespace Kernel;
18using Types::Core::DateAndTime;
19
20namespace {
29void createOrUpdateValue(API::Run &run, const std::string &name, const std::string &time, const std::string &value) {
30 TimeSeriesProperty<std::string> *timeSeries(nullptr);
31 if (run.hasProperty(name)) {
32 timeSeries = dynamic_cast<TimeSeriesProperty<std::string> *>(run.getLogData(name));
33 if (!timeSeries)
34 throw std::invalid_argument("Log '" + name + "' already exists but the values are a different type.");
35 } else {
36 timeSeries = new TimeSeriesProperty<std::string>(name);
37 run.addProperty(timeSeries);
38 }
39 timeSeries->addValue(time, value);
40}
41} // namespace
42
43// Register the algorithm into the AlgorithmFactory
44DECLARE_ALGORITHM(AddNote)
45
46//----------------------------------------------------------------------------------------------
47AddNote::AddNote() { useAlgorithm("Comment", 1); }
48
49//----------------------------------------------------------------------------------------------
50
52const std::string AddNote::name() const { return "AddNote"; }
53
55int AddNote::version() const { return 1; }
56
58const std::string AddNote::category() const { return "DataHandling\\Logs"; }
59
61const std::string AddNote::summary() const { return "Adds a timestamped note to a workspace."; }
62
63//----------------------------------------------------------------------------------------------
68 "An InOut workspace that will store the new log information");
69
70 declareProperty("Name", "", std::make_shared<MandatoryValidator<std::string>>(),
71 "A String name for either a new time series log to be "
72 "created or an existing name to update",
74
75 auto dtv = std::make_shared<DateTimeValidator>();
76 dtv->allowEmpty(true);
77
78 declareProperty("Time", "", dtv,
79 "An ISO formatted date/time string specifying the timestamp for "
80 "the given log value, for example 2010-09-14T04:20:12 \n"
81 "If left blank, this will default to the current Date and Time",
83
84 declareProperty("Value", "", std::make_shared<MandatoryValidator<std::string>>(),
85 "A String value for the series log at the given time", Direction::Input);
86
87 declareProperty("DeleteExisting", false, "If true and the named log exists then the whole log is removed first.",
89}
90//----------------------------------------------------------------------------------------------
94 MatrixWorkspace_sptr logWS = getProperty("Workspace");
95 std::string name = getProperty("Name");
96 const bool deleteExisting = getProperty("DeleteExisting");
97 auto &run = logWS->mutableRun();
98 if (deleteExisting && run.hasProperty(name)) {
99 removeExisting(logWS, name);
100 }
101 createOrUpdate(run, name);
102}
103
109void AddNote::removeExisting(API::MatrixWorkspace_sptr &logWS, const std::string &name) {
110 auto deleter = createChildAlgorithm("DeleteLog", -1, -1, false);
111 deleter->setProperty("Workspace", logWS);
112 deleter->setProperty("Name", name);
113 deleter->executeAsChildAlg();
114}
115
121void AddNote::createOrUpdate(API::Run &run, const std::string &name) {
122 std::string time = getProperty("Time");
123 if (time.empty()) {
124 namespace pt = boost::posix_time;
125 auto dateTimeObj = DateAndTime(pt::second_clock::local_time());
126 time = dateTimeObj.toISO8601String();
127 }
128 std::string value = getProperty("Value");
129
130 createOrUpdateValue(run, name, time, value);
131}
132
133} // 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.
An Algorithm that adds a timestamped note to a workspace.
Definition: AddNote.h:23
const std::string summary() const override
Algorithm's summary for use in the GUI and help.
Definition: AddNote.cpp:61
void init() override
Initialize the algorithm's properties.
Definition: AddNote.cpp:66
int version() const override
Algorithm's version for identification.
Definition: AddNote.cpp:55
void exec() override
Executes the algorithm.
Definition: AddNote.cpp:93
void removeExisting(API::MatrixWorkspace_sptr &, const std::string &)
Remove an existing log of the given name.
Definition: AddNote.cpp:109
void createOrUpdate(API::Run &, const std::string &)
Create or update the named log entry.
Definition: AddNote.cpp:121
const std::string name() const override
Algorithms name for identification.
Definition: AddNote.cpp:52
const std::string category() const override
Algorithm's category for identification.
Definition: AddNote.cpp:58
Validator to check that a property is not left empty.
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ InOut
Both an input & output workspace.
Definition: Property.h:55
@ Input
An input workspace.
Definition: Property.h:53