From 4b82d7c54d295357690f5e50d642629b7f956c98 Mon Sep 17 00:00:00 2001 From: Jürgen Knödlseder Date: Thu, 25 Feb 2016 15:49:17 +0100 Subject: [PATCH] Revise GTime operators. --- ChangeLog | 3 ++- NEWS | 24 +++++++++++++++++++++++- include/GTime.hpp | 165 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------- inst/com/src/GCOMEventCube.cpp | 2 +- inst/cta/src/GCTAEventCube.cpp | 22 ++++++++++++---------- inst/lat/src/GLATEventCube.cpp | 20 +++++++++++--------- pyext/GTime.i | 38 +++++++++++--------------------------- src/obs/GTime.cpp | 7 ++++--- test/test_GObservation.cpp | 27 ++++++++++++++++----------- test/test_GObservation.py | 47 +++++++++++++++++++++-------------------------- 10 files changed, 165 insertions(+), 190 deletions(-) diff --git a/ChangeLog b/ChangeLog index 419dc66..1240a46 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,8 +1,9 @@ -2016-02-24 Juergen Knoedlseder +2016-02-25 Juergen Knoedlseder * Version 1.1.0 released ========================= + Revise GTime operators (#1714) Add GSkyMap::publish() method to publish map on VO Hub (#1710) Add GVOClient::publish() method to publish a FITS HDU (#1710) Handle FITS extensions in GSkyMap load() and save() methods (#1695) diff --git a/NEWS b/NEWS index aff4137..3a813ba 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 1.1.0 Author(s) : Juergen Knoedlseder -24 February 2016 +25 February 2016 1. Introduction @@ -26,12 +26,28 @@ The following methods have been added to the public interface: - GCTAResponseTable::table(std::string) - GCTAResponseTable::has_axis(std::string) - GCTAResponseTable::has_table(std::string) +- friend GTime operator+(const GTime& time, const double& seconds) +- friend GTime operator+(const double& seconds, const GTime& time) +- friend GTime operator-(const GTime& time, const double& seconds) +- friend double operator-(const GTime& a, const GTime& b) +- GTime::operator+=(const double& seconds) +- GTime::operator-=(const double& seconds) The following methods have been removed: - GFilename::size() - gammalib::file_exists() - gammalib::file_exists_gzip() - gammalib::is_fits() +- GTime::operator+=(const GTime& time) +- GTime::operator-=(const GTime& time) +- GTime::operator*=(const double& scale) +- GTime::operator/=(const double& scale) +- friend GTime operator+(const GTime &a, const GTime &b) +- friend GTime operator-(const GTime &a, const GTime &b) +- friend GTime operator* (const GTime &time, const double &s) +- friend GTime operator*(const double &s, const GTime &time) +- friend GTime operator*(const GTime &time, const double &s) +- friend GTime operator/(const GTime &time, const double &s) The following methods have been renamed: - GCTAResponseTable::tables(), was GCTAResponseTable::size() @@ -143,6 +159,12 @@ that no argument is needed when calling the method. This reflects the fact that the deadtime correction is often computed as an average value over the observation, and does in fact not depend on time (#1670). +The GTime operators have been revised as time is implemented as an +absolute quantity, while addition and subtraction of GTime objects are +relative operations. The GTime arguments of the operators have thus be +replaced by double precision values that specify relative time in seconds. +The multiplication and division operators have been removed (#1714). + 11. Optimization module ----------------------- diff --git a/include/GTime.hpp b/include/GTime.hpp index f07c167..57df8c7 100644 --- a/include/GTime.hpp +++ b/include/GTime.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GTime.hpp - Time class * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2014 by Juergen Knoedlseder * + * copyright (C) 2010-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -30,7 +30,9 @@ /* __ Includes ___________________________________________________________ */ #include #include "GBase.hpp" -#include "GTimeReference.hpp" + +/* __ Forward declarations _______________________________________________ */ +class GTimeReference; /***********************************************************************//** @@ -52,17 +54,16 @@ class GTime : public GBase { // Operator friends - friend GTime operator+(const GTime &a, const GTime &b); - friend GTime operator-(const GTime &a, const GTime &b); - friend GTime operator*(const double &s, const GTime &time); - friend GTime operator* (const GTime &time, const double &s); - friend GTime operator/(const GTime &time, const double &s); - friend bool operator==(const GTime &a, const GTime &b); - friend bool operator!=(const GTime &a, const GTime &b); - friend bool operator<(const GTime &a, const GTime &b); - friend bool operator<=(const GTime &a, const GTime &b); - friend bool operator>(const GTime &a, const GTime &b); - friend bool operator>=(const GTime &a, const GTime &b); + friend GTime operator+(const GTime& time, const double& seconds); + friend GTime operator+(const double& seconds, const GTime& time); + friend GTime operator-(const GTime& time, const double& seconds); + friend double operator-(const GTime& a, const GTime& b); + friend bool operator==(const GTime& a, const GTime& b); + friend bool operator!=(const GTime& a, const GTime& b); + friend bool operator<(const GTime& a, const GTime& b); + friend bool operator<=(const GTime& a, const GTime& b); + friend bool operator>(const GTime& a, const GTime& b); + friend bool operator>=(const GTime& a, const GTime& b); public: // Constructors and destructors @@ -74,10 +75,8 @@ public: // Operators GTime& operator=(const GTime& time); - GTime& operator+=(const GTime& time); - GTime& operator-=(const GTime& time); - GTime& operator*=(const double& scale); - GTime& operator/=(const double& scale); + GTime& operator+=(const double& seconds); + GTime& operator-=(const double& seconds); // Methods void clear(void); @@ -179,143 +178,109 @@ int GTime::days_in_year(const int& year) const /***********************************************************************//** - * @brief Time unary addition operator + * @brief Add seconds to time * - * @param[in] time Time. - * @return Sum of times. - ***************************************************************************/ -inline -GTime& GTime::operator+=(const GTime& time) -{ - m_time += time.m_time; - return *this; -} - - -/***********************************************************************//** - * @brief Time unary subtraction operator + * @param[in] seconds Seconds. + * @return Time. * - * @param[in] time Time. - * @return Difference of times. + * Adds @p seconds to the time. ***************************************************************************/ inline -GTime& GTime::operator-=(const GTime& time) +GTime& GTime::operator+=(const double& seconds) { - m_time -= time.m_time; + m_time += seconds; return *this; } /***********************************************************************//** - * @brief Time unary multiplication operator + * @brief Subtract seconds from time * - * @param[in] scale Scale. - * @return Time multiplied by scale. - ***************************************************************************/ -inline -GTime& GTime::operator*=(const double& scale) -{ - m_time *= scale; - return *this; -} - - -/***********************************************************************//** - * @brief Time unary division operator + * @param[in] seconds Seconds. + * @return Time. * - * @param[in] scale Scale. - * @return Time divided by scale. + * Subtracts @p seconds from the time. ***************************************************************************/ inline -GTime& GTime::operator/=(const double& scale) +GTime& GTime::operator-=(const double& seconds) { - m_time /= scale; + m_time -= seconds; return *this; } /***********************************************************************//** - * @brief Time addition operator friend + * @brief Add seconds to time * - * @param[in] a First time. - * @param[in] b Second time. - * @return Added time. - ***************************************************************************/ -inline -GTime operator+(const GTime& a, const GTime& b) -{ - GTime result; - result.m_time = a.m_time + b.m_time; - return result; -} - - -/***********************************************************************//** - * @brief Time subtraction operator friend + * @param[in] time Time. + * @param[in] seconds Seconds. + * @return Time. * - * @param[in] a First time. - * @param[in] b Second time. - * @return Subtracted time. + * Adds @p seconds to the @p time. ***************************************************************************/ inline -GTime operator-(const GTime& a, const GTime& b) +GTime operator+(const GTime& time, const double& seconds) { GTime result; - result.m_time = a.m_time - b.m_time; + result.m_time = time.m_time + seconds; return result; } /***********************************************************************//** - * @brief Time multiplication operator friend + * @brief Add seconds to time * - * @param[in] s Multiplier. + * @param[in] seconds Seconds. * @param[in] time Time. - * @return Multiplied time. + * @return Time. + * + * Adds @p seconds to the @p time. ***************************************************************************/ inline -GTime operator*(const double& s, const GTime& time) +GTime operator+(const double& seconds, const GTime& time) { GTime result; - result.m_time = s * time.m_time; + result.m_time = time.m_time + seconds; return result; } /***********************************************************************//** - * @brief Time multiplication operator friend + * @brief Subtract seconds from time * * @param[in] time Time. - * @param[in] s Multiplier. - * @return Multiplied time. + * @param[in] seconds Seconds. + * @return Time. + * + * Subtracts @p seconds from the @p time. ***************************************************************************/ inline -GTime operator*(const GTime& time, const double& s) +GTime operator-(const GTime& time, const double& seconds) { GTime result; - result.m_time = s * time.m_time; + result.m_time = time.m_time - seconds; return result; } /***********************************************************************//** - * @brief Time division operator friend + * @brief Subtract times * - * @param[in] time Time. - * @param[in] s Divider. - * @return Divided time. + * @param[in] a First time. + * @param[in] b Second time. + * @return Time difference in seconds. + * + * Subtracts time @p b from time @p a and returns the difference in seconds. ***************************************************************************/ inline -GTime operator/(const GTime& time, const double& s) +double operator-(const GTime& a, const GTime& b) { - GTime result; - result.m_time = time.m_time / s; - return result; + return (a.m_time - b.m_time); } - + /***********************************************************************//** - * @brief Time equality operator friend + * @brief Check if times are equal * * @param[in] a First time. * @param[in] b Second time. @@ -329,7 +294,7 @@ bool operator==(const GTime &a, const GTime &b) /***********************************************************************//** - * @brief Time non-equality operator friend + * @brief Check if times are not equal * * @param[in] a First time. * @param[in] b Second time. @@ -343,7 +308,7 @@ bool operator!=(const GTime &a, const GTime &b) /***********************************************************************//** - * @brief Time smaller than operator friend + * @brief Check if time is smaller than other time * * @param[in] a First time. * @param[in] b Second time. @@ -357,7 +322,7 @@ bool operator<(const GTime &a, const GTime &b) /***********************************************************************//** - * @brief Time smaller than or equal operator friend + * @brief Check if time is smaller than or equal to other time * * @param[in] a First time. * @param[in] b Second time. @@ -371,7 +336,7 @@ bool operator<=(const GTime &a, const GTime &b) /***********************************************************************//** - * @brief Time larger than operator friend + * @brief Check if time is larger than other time * * @param[in] a First time. * @param[in] b Second time. @@ -385,7 +350,7 @@ bool operator>(const GTime &a, const GTime &b) /***********************************************************************//** - * @brief Time larger than or equal operator friend + * @brief Check if time is larger than or equal to other time * * @param[in] a First time. * @param[in] b Second time. @@ -397,6 +362,4 @@ bool operator>=(const GTime &a, const GTime &b) return (a.m_time >= b.m_time); } - - #endif /* GTIME_HPP */ diff --git a/inst/com/src/GCOMEventCube.cpp b/inst/com/src/GCOMEventCube.cpp index a82c60c..67ab684 100644 --- a/inst/com/src/GCOMEventCube.cpp +++ b/inst/com/src/GCOMEventCube.cpp @@ -799,7 +799,7 @@ void GCOMEventCube::set_times(void) } // Compute mean time - m_time = 0.5 * (m_gti.tstart() + m_gti.tstop()); + m_time = m_gti.tstart() + 0.5 * (m_gti.tstop() - m_gti.tstart()); // Set ontime m_ontime = m_gti.ontime(); diff --git a/inst/cta/src/GCTAEventCube.cpp b/inst/cta/src/GCTAEventCube.cpp index 94f22f2..d61ec05 100644 --- a/inst/cta/src/GCTAEventCube.cpp +++ b/inst/cta/src/GCTAEventCube.cpp @@ -38,7 +38,7 @@ #define G_ENERGY "GCTAEventCube::energy(int&)" #define G_SET_DIRECTIONS "GCTAEventCube::set_directions()" #define G_SET_ENERGIES "GCTAEventCube::set_energies()" -#define G_SET_TIME "GCTAEventCube::set_time()" +#define G_SET_TIMES "GCTAEventCube::set_times()" #define G_SET_BIN "GCTAEventCube::set_bin(int&)" /* __ Macros _____________________________________________________________ */ @@ -764,14 +764,14 @@ void GCTAEventCube::set_energies(void) /***********************************************************************//** - * @brief Set mean event time and ontime of event cube. + * @brief Set mean event time and ontime of event cube * - * @exception GCTAException::no_gti - * No Good Time Intervals found in event cube. + * @exception GException::invalid_value + * No Good Time Intervals found. * - * This method computes the mean event time and the ontime of the event - * cube. The mean event time is the average between the start and the stop - * time. The ontime is the sum of all Good Time Intervals. + * Computes the mean time of the event cube by taking the mean between start + * and stop time. Computes also the ontime by summing up of all good time + * intervals. * * @todo Could add a more sophisticated mean event time computation that * weights by the length of the GTIs, yet so far we do not really use @@ -781,12 +781,14 @@ void GCTAEventCube::set_times(void) { // Throw an error if GTI is empty if (m_gti.size() < 1) { - throw GCTAException::no_gti(G_SET_TIME, "Every CTA event cube needs" - " associated GTIs to allow the computation of the ontime."); + std::string msg = "No Good Time Intervals have been found in event " + "cube. Every CTA event cube needs a definition " + "of the Good Time Intervals."; + throw GException::invalid_value(G_SET_TIMES, msg); } // Compute mean time - m_time = 0.5 * (m_gti.tstart() + m_gti.tstop()); + m_time = m_gti.tstart() + 0.5 * (m_gti.tstop() - m_gti.tstart()); // Set ontime m_ontime = m_gti.ontime(); diff --git a/inst/lat/src/GLATEventCube.cpp b/inst/lat/src/GLATEventCube.cpp index 3f35131..4d3d522 100644 --- a/inst/lat/src/GLATEventCube.cpp +++ b/inst/lat/src/GLATEventCube.cpp @@ -927,14 +927,14 @@ void GLATEventCube::set_energies(void) /***********************************************************************//** - * @brief Set mean event time and ontime of event cube. + * @brief Set mean event time and ontime of event cube * - * @exception GLATException::no_gti - * No Good Time Intervals found in event cube. + * @exception GException::invalid_value + * No Good Time Intervals found. * - * This method computes the mean event time and the ontime of the event - * cube. The mean event time is the average between the start and the stop - * time. The ontime is the sum of all Good Time Intervals. + * Computes the mean time of the event cube by taking the mean between start + * and stop time. Computes also the ontime by summing up of all good time + * intervals. * * @todo Could add a more sophisticated mean event time computation that * weights by the length of the GTIs, yet so far we do not really use @@ -944,12 +944,14 @@ void GLATEventCube::set_times(void) { // Throw an error if GTI is empty if (m_gti.size() < 1) { - throw GLATException::no_gti(G_SET_TIMES, "Every LAT event cube needs" - " associated GTIs to allow the computation of the ontime."); + std::string msg = "No Good Time Intervals have been found in event " + "cube. Every LAT event cube needs a definition " + "of the Good Time Intervals."; + throw GException::invalid_value(G_SET_TIMES, msg); } // Compute mean time - m_time = 0.5 * (gti().tstart() + gti().tstop()); + m_time = m_gti.tstart() + 0.5 * (m_gti.tstop() - m_gti.tstart()); // Set ontime m_ontime = m_gti.ontime(); diff --git a/pyext/GTime.i b/pyext/GTime.i index 5e80c92..f7abf4e 100644 --- a/pyext/GTime.i +++ b/pyext/GTime.i @@ -1,7 +1,7 @@ /*************************************************************************** * GTime.i - Time class * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2014 by Juergen Knoedlseder * + * copyright (C) 2010-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -45,9 +45,8 @@ public: virtual ~GTime(void); // Operators - GTime& operator+=(const GTime& time); - GTime& operator-=(const GTime& time); - GTime& operator*=(const double& scale); + GTime& operator+=(const double& seconds); + GTime& operator-=(const double& seconds); // Methods void clear(void); @@ -73,32 +72,17 @@ public: * @brief GTime class extension ***************************************************************************/ %extend GTime { - GTime __add__(const GTime& time) const { - return ((*self) + time); + GTime __add__(const double& seconds) const { + return ((*self) + seconds); } - GTime __sub__(const GTime& time) const { - return ((*self) - time); - } - GTime __mul__(const double& factor) const { - return ((*self) * factor); + GTime __radd__(const double& seconds) const { + return (seconds + (*self)); } - // Python 2.x - GTime __div__(const double& factor) const { - return ((*self) / factor); + GTime __sub__(const double& seconds) const { + return ((*self) - seconds); } - // Python 3.x - GTime __truediv__(const double& factor) const { - return ((*self) / factor); - } - // Python 2.x operator/= - GTime __idiv__(const double& scale) { - self->operator/=(scale); - return (*self); - } - // Python 3.x operator/= - GTime __itruediv__(const double& scale) { - self->operator/=(scale); - return (*self); + double __sub__(const GTime& time) const { + return ((*self) - time); } bool __eq__(const GTime& time) const { return ((*self) == time); diff --git a/src/obs/GTime.cpp b/src/obs/GTime.cpp index f7ee0c9..462870c 100644 --- a/src/obs/GTime.cpp +++ b/src/obs/GTime.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GTime.cpp - Time class * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2015 by Juergen Knoedlseder * + * copyright (C) 2010-2016 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -29,9 +29,10 @@ #include #endif #include -#include "GTime.hpp" #include "GTools.hpp" #include "GException.hpp" +#include "GTime.hpp" +#include "GTimeReference.hpp" /* __ Constants __________________________________________________________ */ const double mjd_ref = 55197.000766018518519; //!< MJD of time=0 @@ -170,7 +171,7 @@ GTime::~GTime(void) * @param[in] time Time. * @return Time. ***************************************************************************/ -GTime& GTime::operator= (const GTime& time) +GTime& GTime::operator=(const GTime& time) { // Execute only if object is not identical if (this != &time) { diff --git a/test/test_GObservation.cpp b/test/test_GObservation.cpp index fee1db3..5b0fbad 100644 --- a/test/test_GObservation.cpp +++ b/test/test_GObservation.cpp @@ -751,17 +751,22 @@ void TestGObservation::test_time(void) // Test operators GTime a(13.72); GTime b(6.28); - test_value((a+b).secs(), 20.00); - test_value((a-b).secs(), 7.44); - test_value((a*3.3).secs(), 45.276); - test_value((3.3*a).secs(), 45.276); - test_value((a/13.72).secs(), 1.0); - test_assert(a == a, "Equality operator corrupt."); - test_assert(a != b, "Non-equality operator corrupt."); - test_assert(a > b, "Greater than operator corrupt."); - test_assert(a >= b, "Greater than or equal operator corrupt."); - test_assert(b < a, "Less than operator corrupt."); - test_assert(b <= a, "Less than or equal operator corrupt."); + test_value((a+6.28).secs(), 20.00, 1.0e-6, "Seconds right addition operator"); + test_value((6.28+a).secs(), 20.00, 1.0e-6, "Seconds left addition operator"); + test_value((a-6.28).secs(), 7.44, 1.0e-6, "Seconds substraction operator"); + test_value((a-b), 7.44, 1.0e-6, "Time subtraction operator"); + GTime c = a; + c += 6.28, + test_value(c.secs(), 20.00, 1.0e-6, "Seconds unary addition operator"); + GTime d = a; + d -= 6.28, + test_value(d.secs(), 7.44, 1.0e-6, "Seconds unary subtraction operator"); + test_assert(a == a, "Equality operator"); + test_assert(a != b, "Non-equality operator"); + test_assert(a > b, "Greater than operator"); + test_assert(a >= b, "Greater than or equal operator"); + test_assert(b < a, "Less than operator"); + test_assert(b <= a, "Less than or equal operator"); // Return return; diff --git a/test/test_GObservation.py b/test/test_GObservation.py index b3f3e65..cee90c4 100755 --- a/test/test_GObservation.py +++ b/test/test_GObservation.py @@ -1,7 +1,7 @@ # ========================================================================== # This module performs unit tests for the GammaLib observation module. # -# Copyright (C) 2012-2015 Juergen Knoedlseder +# Copyright (C) 2012-2016 Juergen Knoedlseder # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -119,40 +119,35 @@ class Test(gammalib.GPythonTestSuite): Test GTime class. """ # Setup times - time = gammalib.GTime(3.0) - time_b = time.copy() + time_a = gammalib.GTime(9.0) + time_b = gammalib.GTime(3.0) + secs_b = 2.0 # Unary addition operator - time += time_b - self.test_value(time.secs(), 6.0); + time = time_a.copy() + time += secs_b + self.test_value(time.secs(), 11.0); # Unary subtraction operator - time -= time_b - self.test_value(time.secs(), 3.0); + time = time_a.copy() + time -= secs_b + self.test_value(time.secs(), 7.0); - # Unary multiplication operator - time *= 2.0 - self.test_value(time.secs(), 6.0); + # Binary left addition operator + time = time_a + secs_b + self.test_value(time.secs(), 11.0); - # Unary division operator - time /= 2.0 - self.test_value(time.secs(), 3.0); - - # Binary addition operator - time = time + time_b - self.test_value(time.secs(), 6.0); + # Binary right addition operator + time = secs_b + time_a + self.test_value(time.secs(), 11.0); # Binary subtraction operator - time = time - time_b - self.test_value(time.secs(), 3.0); + time = time_a - secs_b + self.test_value(time.secs(), 7.0); - # Scalar multiplication operator - time = time * 2.0 - self.test_value(time.secs(), 6.0); - - # Scalar division operator - time = time / 2.0 - self.test_value(time.secs(), 3.0); + # Binary time subtraction operator + time = time_a - time_b + self.test_value(time, 6.0); # Return return -- libgit2 0.21.4