21#include <boost/lexical_cast.hpp>
33using Types::Core::DateAndTime;
42 auto isTimeSeries =
false;
54 "An input workspace.");
55 declareProperty<double>(
"RelativeTimeOffset",
m_defaultTimeShift,
"A relative time offset in seconds.");
58 "An absolute time offset as an ISO8601 string "
59 "(YYYY-MM-DDTHH:MM::SS, eg 2013-10-25T13:58:03).");
62 "An output workspace.");
71 const double progressStartCreateOutputWs = 0.0;
72 const double progressStopCreateOutputWs = 0.3;
80 const double progressStartShiftTimeLogs = progressStopCreateOutputWs;
82 double progressStopShiftTimeLogs =
83 std::dynamic_pointer_cast<Mantid::API::IEventWorkspace>(out_ws) ? progressStartShiftTimeLogs + 0.1 : 1.0;
85 const double progressStartShiftNeutrons = progressStopShiftTimeLogs;
86 const double progressStopShiftNeutrons = 1.0;
90 shiftTimeOfLogs(out_ws, timeShift, progressStartShiftTimeLogs, progressStopShiftTimeLogs);
93 shiftTimeOfNeutrons(out_ws, timeShift, progressStartShiftNeutrons, progressStopShiftNeutrons);
106 double stopProgress) {
109 if (input != output) {
111 duplicate->initialize();
112 duplicate->setProperty<
API::Workspace_sptr>(
"InputWorkspace", std::dynamic_pointer_cast<API::Workspace>(input));
113 duplicate->execute();
115 output = std::dynamic_pointer_cast<API::MatrixWorkspace>(temp);
129 std::string timeOffset =
getProperty(
"AbsoluteTimeOffset");
131 DateAndTime desiredTime(timeOffset);
133 timeShift = DateAndTime::secondsFromDuration(desiredTime - originalTime);
148 double startProgress,
double stopProgress) {
152 auto logs = ws->mutableRun().getLogData();
153 Progress prog(
this, startProgress, stopProgress, logs.size());
154 for (
auto &log : logs) {
155 if (isTimeSeries(log)) {
175 auto newlog = timeSeries->cloneWithTimeShift(timeShift);
176 ws->mutableRun().addProperty(newlog,
true);
189 DateAndTime dateTime(
value);
190 DateAndTime shiftedTime = dateTime + timeShift;
191 logEntry->
setValue(shiftedTime.toISO8601String());
203 double startProgress,
double stopProgress) {
204 if (
auto eventWs = std::dynamic_pointer_cast<Mantid::API::IEventWorkspace>(ws)) {
208 alg->setProperty(
"InputWorkspace", eventWs);
209 alg->setProperty(
"OutputWorkspace", eventWs);
210 alg->setProperty(
"TimeOffset", timeShift);
221 auto run = ws->run();
225 goodFrame = run.getTimeSeriesProperty<
double>(
"proton_charge");
226 }
catch (
const std::invalid_argument &) {
227 throw std::invalid_argument(
"ChangeTimeZero: The log needs a proton_charge "
228 "time series to determine the zero time.");
231 DateAndTime startTime;
232 if (goodFrame->
size() > 0) {
244 std::map<std::string, std::string> invalidProperties;
247 double relativeTimeOffset =
getProperty(
"RelativeTimeOffset");
248 std::string absoluteTimeOffset =
getProperty(
"AbsoluteTimeOffset");
255 if (isRelative && absoluteTimeInput) {
256 invalidProperties.emplace(
"RelativeTimeOffset",
"You can either specify a relative time shift or "
257 "an absolute time shift.");
258 invalidProperties.emplace(
"AbsoluteTimeOffset",
"You can either specify a relative time shift or "
259 "an absolute time shift.");
261 return invalidProperties;
262 }
else if (!isRelative && !isAbsolute) {
263 invalidProperties.emplace(
"RelativeTimeOffset",
"TimeOffset must either be a numeric "
264 "value or a ISO8601 (YYYY-MM-DDTHH:MM::SS) date-time stamp.");
265 invalidProperties.emplace(
"AbsoluteTimeOffset",
"TimeOffset must either be a numeric "
266 "value or a ISO8601 (YYYY-MM-DDTHH:MM::SS) date-time stamp.");
274 auto run = ws->run();
276 run.getTimeSeriesProperty<
double>(
"proton_charge");
278 invalidProperties.emplace(
"InputWorkspace",
"A TimeOffset with an absolute time requires the "
279 "input workspace to have a proton_charge property in "
285 return invalidProperties;
293 auto isDouble =
false;
295 boost::lexical_cast<double>(val);
297 }
catch (boost::bad_lexical_cast
const &) {
308 auto isDateTime =
false;
312 isDateTime = validator.isValid(val).empty();
#define DECLARE_ALGORITHM(classname)
double value
The value of the point.
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
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.
Helper class for reporting progress from algorithms.
A property class for workspaces.
double getTimeShift(const API::MatrixWorkspace_sptr &ws) const
Get the time shift.
bool checkForDouble(const std::string &val) const
Can the string be transformed to double.
Mantid::API::MatrixWorkspace_sptr createOutputWS(const Mantid::API::MatrixWorkspace_sptr &input, double startProgress, double stopProgress)
Create the output workspace.
const double m_defaultTimeShift
void init() override
Initialise the properties.
void shiftTimeOfLogForStringProperty(Mantid::Kernel::PropertyWithValue< std::string > *logEntry, double timeShift) const
Time shift the log of a string property.
void shiftTimeInLogForTimeSeries(const Mantid::API::MatrixWorkspace_sptr &ws, Mantid::Kernel::Property *prop, double timeShift) const
Time shift the log of a double series property.
void shiftTimeOfLogs(const Mantid::API::MatrixWorkspace_sptr &ws, double timeShift, double startProgress, double stopProgress)
Shift the time of the logs.
bool isRelativeTimeShift(double offset) const
Checks if a relative offset has been set.
Mantid::Types::Core::DateAndTime getStartTimeFromWorkspace(const Mantid::API::MatrixWorkspace_sptr &ws) const
Get the date and time of the first good frame of a workspace.
void exec() override
Run the algorithm.
const std::string name() const override
Algorithm's name for identification.
bool isAbsoluteTimeShift(const std::string &offset) const
Checks if an absolute offset has been set.
bool checkForDateTime(const std::string &val) const
Can the string be transformed to a DateTime.
void shiftTimeOfNeutrons(const Mantid::API::MatrixWorkspace_sptr &ws, double timeShift, double startProgress, double stopProgress)
Shift the time of the neutrons.
std::map< std::string, std::string > validateInputs() override
Check the inputs.
const std::string m_defaultAbsoluteTimeShift
Checks that a string contains a timestamp in ISO 8601 format (YYYY-MM-DDTHH:MM:SS....
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
A non-templated interface to a TimeSeriesProperty.
void report()
Increments the loop counter by 1, then sends the progress notification on behalf of its algorithm.
The concrete, templated class for properties.
std::string setValue(const std::string &value) override
Set the value of the property via a string.
std::string value() const override
Returns the value of the property as a string.
Base class for properties.
A specialised Property class for holding a series of time-value pairs.
int size() const override
Returns the number of values at UNIQUE time intervals in the time series.
Types::Core::DateAndTime firstTime() const
Returns the first time regardless of filter.
std::shared_ptr< Workspace > Workspace_sptr
shared pointer to Mantid::API::Workspace
std::shared_ptr< MatrixWorkspace > MatrixWorkspace_sptr
shared pointer to the matrix workspace base class
@ Input
An input workspace.
@ Output
An output workspace.