-
LamarMoore authoredLamarMoore authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
To find the state of this project's repository at the time of any of these versions, check out the tags.
ChangeLogTime.cpp 3.36 KiB
#include "MantidAlgorithms/ChangeLogTime.h"
#include "MantidAPI/Run.h"
#include "MantidDataObjects/EventWorkspace.h"
#include "MantidKernel/TimeSeriesProperty.h"
#include <sstream>
using Mantid::Types::DateAndTime;
namespace Mantid {
namespace Algorithms {
// Register the algorithm into the AlgorithmFactory
DECLARE_ALGORITHM(ChangeLogTime)
using std::string;
using std::stringstream;
using std::vector;
using namespace Mantid::API;
using namespace Mantid::DataObjects;
using namespace Mantid::Kernel;
/// Algorithm's name for identification
const string ChangeLogTime::name() const { return "ChangeLogTime"; }
/// Algorithm's version for identification
int ChangeLogTime::version() const { return 1; }
/// Algorithm's category for identification
const std::string ChangeLogTime::category() const {
return "DataHandling\\Logs";
}
/// Declares the parameters for running the algorithm.
void ChangeLogTime::init() {
declareProperty(make_unique<WorkspaceProperty<API::MatrixWorkspace>>(
"InputWorkspace", "", Direction::Input),
"A workspace");
declareProperty(make_unique<WorkspaceProperty<API::MatrixWorkspace>>(
"OutputWorkspace", "", Direction::Output),
"The name to use for the output workspace");
this->declareProperty("LogName", "", "Name of the log to add the offset to");
this->declareProperty(
make_unique<PropertyWithValue<double>>("TimeOffset", Direction::Input),
"Number of seconds (a float) to add to the time of each log value. "
"Required.");
}
/// Do the actual work of modifying the log in the workspace.
void ChangeLogTime::exec() {
// check that a log was specified
string logname = this->getProperty("LogName");
if (logname.empty()) {
throw std::runtime_error("Failed to supply a LogName");
}
// everything will need an offset
double offset = this->getProperty("TimeOffset");
// make sure the log is in the input workspace
MatrixWorkspace_sptr inputWS = getProperty("InputWorkspace");
Kernel::TimeSeriesProperty<double> *oldlog =
dynamic_cast<Kernel::TimeSeriesProperty<double> *>(
inputWS->run().getLogData(logname));
if (!oldlog) {
stringstream msg;
msg << "InputWorkspace \'" << this->getPropertyValue("InputWorkspace")
<< "\' does not have LogName \'" << logname << "\'";
throw std::runtime_error(msg.str());
}
// Create the new log
auto newlog = new TimeSeriesProperty<double>(logname);
newlog->setUnits(oldlog->units());
int size = oldlog->realSize();
vector<double> values = oldlog->valuesAsVector();
vector<DateAndTime> times = oldlog->timesAsVector();
for (int i = 0; i < size; i++) {
newlog->addValue(times[i] + offset, values[i]);
}
// Just overwrite if the change is in place
MatrixWorkspace_sptr outputWS = getProperty("OutputWorkspace");
if (outputWS != inputWS) {
IAlgorithm_sptr duplicate = createChildAlgorithm("CloneWorkspace");
duplicate->initialize();
duplicate->setProperty<Workspace_sptr>(
"InputWorkspace", boost::dynamic_pointer_cast<Workspace>(inputWS));
duplicate->execute();
Workspace_sptr temp = duplicate->getProperty("OutputWorkspace");
outputWS = boost::dynamic_pointer_cast<MatrixWorkspace>(temp);
setProperty("OutputWorkspace", outputWS);
}
outputWS->mutableRun().addProperty(newlog, true);
}
} // namespace Algorithms
} // namespace Mantid