Mantid
Loading...
Searching...
No Matches
MergeLogs.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"
13
14using namespace Mantid::Kernel;
15using namespace Mantid::API;
16
17namespace Mantid::Algorithms {
18
19DECLARE_ALGORITHM(MergeLogs)
20
21void MergeLogs::init() {
22 declareProperty(std::make_unique<WorkspaceProperty<MatrixWorkspace>>("Workspace", "Anonymous", Direction::InOut),
23 "Workspace to have logs merged");
24 declareProperty("LogName1", "", std::make_shared<MandatoryValidator<std::string>>(),
25 "The name of the first log to be merged.");
26 declareProperty("LogName2", "", std::make_shared<MandatoryValidator<std::string>>(),
27 "The name of the second log to be merged.");
28 declareProperty("MergedLogName", "", std::make_shared<MandatoryValidator<std::string>>(),
29 "The name of the new log as the result "
30 "of log 1 being merged with log 2.");
31 declareProperty("ResetLogValue", false, "Reset both logs' values to unity for each one.");
32 declareProperty("LogValue1", 0.0, "Unity value of log 1.");
33 declareProperty("LogValue2", 1.0, "Unity value of log 2.");
34}
35
40std::string MergeLogs::validateTSP(std::string const &propertyName) {
41 std::string const logName = this->getProperty(propertyName);
42 MatrixWorkspace_const_sptr ws = this->getProperty("Workspace");
43 if (!this->getPointerToProperty(propertyName)->isDefault() && ws->run().hasProperty(logName)) {
44 try {
45 ws->run().getTimeSeriesProperty<double>(logName);
46 } catch (std::invalid_argument &) {
47 return "Must be a TimeSeriesProperty";
48 }
49 } else
50 return "TimeSeriesLog must exist.";
51 return "";
52}
53
57std::map<std::string, std::string> MergeLogs::validateInputs(void) {
58 std::map<std::string, std::string> issues;
59 std::string const mlogname = this->getProperty("MergedLogName");
60 std::string const logName1 = this->getProperty("LogName1");
61 std::string const logName2 = this->getProperty("LogName2");
62 MatrixWorkspace_const_sptr ws = this->getProperty("Workspace");
63 if ((mlogname == logName1) || (mlogname == logName2) || ws->run().hasProperty(mlogname))
64 issues["MergedLogName"] = "TimeSeriesLog name must be unique.";
65 else {
66 std::string const &issueLog1 = this->validateTSP("LogName1");
67 if (!issueLog1.empty())
68 issues["LogName1"] = issueLog1;
69 std::string const &issueLog2 = this->validateTSP("LogName2");
70 if (!issueLog2.empty())
71 issues["LogName2"] = issueLog2;
72 }
73 return issues;
74}
75
77 MatrixWorkspace_sptr ws = this->getProperty("Workspace");
78 const std::string log1name = this->getProperty("LogName1");
79 const std::string log2name = this->getProperty("LogName2");
80 const std::string mlogname = this->getProperty("MergedLogName");
81 const bool resetlogvalue = this->getProperty("ResetLogValue");
82 TimeSeriesProperty<double> *log1(ws->run().getTimeSeriesProperty<double>(log1name));
83 TimeSeriesProperty<double> *log2(ws->run().getTimeSeriesProperty<double>(log2name));
84 std::unique_ptr<TimeSeriesProperty<double>> mlog1(log1->clone());
85 std::unique_ptr<TimeSeriesProperty<double>> mlog2(log2->clone());
86 mlog1->setName(mlogname);
87 mlog2->setName(mlogname);
88 if (resetlogvalue) {
89 const double logvalue1 = this->getProperty("LogValue1");
90 const double logvalue2 = this->getProperty("LogValue2");
91 const std::vector<double> logvector1(mlog1->size(), logvalue1);
92 const std::vector<double> logvector2(mlog2->size(), logvalue2);
93 mlog1->replaceValues(log1->timesAsVector(), logvector1);
94 mlog2->replaceValues(log2->timesAsVector(), logvector2);
95 }
96 mlog1->merge(mlog2.get());
97 ws->mutableRun().addProperty(std::move(mlog1));
98 setProperty("Workspace", ws);
99}
100
101} // namespace Mantid::Algorithms
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
Kernel::Property * getPointerToProperty(const std::string &name) const override
Get a property by name.
Definition: Algorithm.cpp:2033
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
bool isDefault(const std::string &name) const
Definition: Algorithm.cpp:2084
A property class for workspaces.
std::map< std::string, std::string > validateInputs() override
Cross-check properties with each other.
Definition: MergeLogs.cpp:57
std::string validateTSP(std::string const &propertyName)
Helper to validate the TimeSeriesProperty.
Definition: MergeLogs.cpp:40
void exec() override
Virtual method - must be overridden by concrete algorithm.
Definition: MergeLogs.cpp:76
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
Validator to check that a property is not left empty.
A specialised Property class for holding a series of time-value pairs.
TimeSeriesProperty< TYPE > * clone() const override
"Virtual" copy constructor
std::vector< Types::Core::DateAndTime > timesAsVector() const override
Return the time series's times as a vector<DateAndTime>
std::shared_ptr< const MatrixWorkspace > MatrixWorkspace_const_sptr
shared pointer to the matrix workspace base class (const version)
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ InOut
Both an input & output workspace.
Definition: Property.h:55