Commit c7033595ff393d2b3da52159679e57f822e41aff
1 parent
db174b47
Add GGti::overlap() method
Showing
7 changed files
with
181 additions
and
100 deletions
ChangeLog
NEWS
| 1 | 1 | New Features and Important Changes in GammaLib 1.7.0 |
| 2 | 2 | |
| 3 | -15 July 2019 | |
| 3 | +19 July 2019 | |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | 1. Introduction |
| ... | ... | @@ -22,6 +22,7 @@ The following classes have been renamed: |
| 22 | 22 | |
| 23 | 23 | The following methods have been added: |
| 24 | 24 | - GTime.datetime() - Datetime conversion in Python interface |
| 25 | +- GGti::overlap(GTime&, GTime&) - Compute overlap with time interval | |
| 25 | 26 | |
| 26 | 27 | The following methods have been removed: |
| 27 | 28 | - none |
| ... | ... | @@ -30,7 +31,7 @@ The following methods have been renamed: |
| 30 | 31 | - none |
| 31 | 32 | |
| 32 | 33 | The arguments for the following methods have been changed: |
| 33 | -- GTime::utc(const int& precision=0) - Add optional precision value | |
| 34 | +- GTime::utc(int&) - Add optional precision value | |
| 34 | 35 | |
| 35 | 36 | The return value of the following methods has been changed: |
| 36 | 37 | - none |
| ... | ... | @@ -73,6 +74,9 @@ None |
| 73 | 74 | |
| 74 | 75 | 10. Observation module |
| 75 | 76 | ---------------------- |
| 77 | +Add GGti::overlap() method to compute the temporal overlap in seconds with | |
| 78 | +a specified time interval. | |
| 79 | + | |
| 76 | 80 | Add GTime.datetime() method to the Python interface to convert times from and |
| 77 | 81 | to datetime.datetime objects. Add an optional precision argument to the GTime.utc() |
| 78 | 82 | to set the number of digits after the comma for the seconds (#2909). | ... | ... |
doc/source/admin/release_history/1.7.rst
include/GGti.hpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GGti.hpp - Good time interval class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2017 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2008-2019 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -102,6 +102,7 @@ public: |
| 102 | 102 | const GTime& tstop(const int& index) const; |
| 103 | 103 | const double& telapse(void) const; |
| 104 | 104 | const double& ontime(void) const; |
| 105 | + double overlap(const GTime& tstart, const GTime& tstop) const; | |
| 105 | 106 | void reference(const GTimeReference& ref); |
| 106 | 107 | const GTimeReference& reference(void) const; |
| 107 | 108 | bool contains(const GTime& time) const; |
| ... | ... | @@ -166,4 +167,114 @@ bool GGti::is_empty(void) const |
| 166 | 167 | return (m_num == 0); |
| 167 | 168 | } |
| 168 | 169 | |
| 170 | + | |
| 171 | +/***********************************************************************//** | |
| 172 | + * @brief Reserve space for Good Time Intervals | |
| 173 | + * | |
| 174 | + * @param[in] num Number of elements. | |
| 175 | + * | |
| 176 | + * This method does nothing (it is needed to satisfy the GContainer | |
| 177 | + * interface). | |
| 178 | + ***************************************************************************/ | |
| 179 | +inline | |
| 180 | +void GGti::reserve(const int& num) | |
| 181 | +{ | |
| 182 | + // Return | |
| 183 | + return; | |
| 184 | +} | |
| 185 | + | |
| 186 | + | |
| 187 | +/***********************************************************************//** | |
| 188 | + * @brief Returns earliest start time in Good Time Intervals | |
| 189 | + * | |
| 190 | + * @return Earliest start time in Good Time Intervals. | |
| 191 | + ***************************************************************************/ | |
| 192 | +inline | |
| 193 | +const GTime& GGti::tstart(void) const | |
| 194 | +{ | |
| 195 | + // Return | |
| 196 | + return m_tstart; | |
| 197 | +} | |
| 198 | + | |
| 199 | + | |
| 200 | +/***********************************************************************//** | |
| 201 | + * @brief Returns latest stop time in Good Time Intervals | |
| 202 | + * | |
| 203 | + * @return Latest stop time in Good Time Intervals. | |
| 204 | + ***************************************************************************/ | |
| 205 | +inline | |
| 206 | +const GTime& GGti::tstop(void) const | |
| 207 | +{ | |
| 208 | + // Return | |
| 209 | + return m_tstop; | |
| 210 | +} | |
| 211 | + | |
| 212 | + | |
| 213 | +/***********************************************************************//** | |
| 214 | + * @brief Returns elapsed time | |
| 215 | + * | |
| 216 | + * @return Elapsed time (seconds). | |
| 217 | + * | |
| 218 | + * Returns the elapsed time in seconds. The elapsed time is defined as the | |
| 219 | + * time difference between the latest stop time and the earliest start time | |
| 220 | + * in the Good Time Intervals. | |
| 221 | + ***************************************************************************/ | |
| 222 | +inline | |
| 223 | +const double& GGti::telapse(void) const | |
| 224 | +{ | |
| 225 | + // Return | |
| 226 | + return m_telapse; | |
| 227 | +} | |
| 228 | + | |
| 229 | + | |
| 230 | +/***********************************************************************//** | |
| 231 | + * @brief Returns ontime | |
| 232 | + * | |
| 233 | + * @return Ontime (seconds). | |
| 234 | + * | |
| 235 | + * Returns the ontime in seconds. The ontime is defined as the sum of all | |
| 236 | + * Good Time Intervals. | |
| 237 | + ***************************************************************************/ | |
| 238 | +inline | |
| 239 | +const double& GGti::ontime(void) const | |
| 240 | +{ | |
| 241 | + // Return | |
| 242 | + return m_ontime; | |
| 243 | +} | |
| 244 | + | |
| 245 | + | |
| 246 | +/***********************************************************************//** | |
| 247 | + * @brief Set time reference for Good Time Intervals | |
| 248 | + * | |
| 249 | + * @param[in] ref Time reference. | |
| 250 | + * | |
| 251 | + * Sets the time reference of the Good Time Intervals. This defines the | |
| 252 | + * reference time which will be writted into the FITS file upon saving of | |
| 253 | + * the Good Time Intervals. | |
| 254 | + ***************************************************************************/ | |
| 255 | +inline | |
| 256 | +void GGti::reference(const GTimeReference& ref) | |
| 257 | +{ | |
| 258 | + // Set time reference | |
| 259 | + m_reference = ref; | |
| 260 | + | |
| 261 | + // Return | |
| 262 | + return; | |
| 263 | +} | |
| 264 | + | |
| 265 | + | |
| 266 | +/***********************************************************************//** | |
| 267 | + * @brief Return time reference for Good Time Intervals | |
| 268 | + * | |
| 269 | + * @return Time reference. | |
| 270 | + * | |
| 271 | + * Returns the time reference of the Good Time Intervals. | |
| 272 | + ***************************************************************************/ | |
| 273 | +inline | |
| 274 | +const GTimeReference& GGti::reference(void) const | |
| 275 | +{ | |
| 276 | + // Return time reference | |
| 277 | + return m_reference; | |
| 278 | +} | |
| 279 | + | |
| 169 | 280 | #endif /* GGTI_HPP */ | ... | ... |
pyext/GGti.i
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GGti.i - Good time interval class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2018 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2010-2019 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -75,6 +75,7 @@ public: |
| 75 | 75 | const GTime& tstop(const int& index) const; |
| 76 | 76 | const double& telapse(void) const; |
| 77 | 77 | const double& ontime(void) const; |
| 78 | + double overlap(const GTime& tstart, const GTime& tstop) const; | |
| 78 | 79 | void reference(const GTimeReference& ref); |
| 79 | 80 | const GTimeReference& reference(void) const; |
| 80 | 81 | bool contains(const GTime& time) const; | ... | ... |
src/obs/GGti.cpp
| 1 | 1 | /*************************************************************************** |
| 2 | 2 | * GGti.cpp - Good time interval class * |
| 3 | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2017 by Juergen Knoedlseder * | |
| 4 | + * copyright (C) 2008-2019 by Juergen Knoedlseder * | |
| 5 | 5 | * ----------------------------------------------------------------------- * |
| 6 | 6 | * * |
| 7 | 7 | * This program is free software: you can redistribute it and/or modify * |
| ... | ... | @@ -499,21 +499,6 @@ void GGti::remove(const int& index) |
| 499 | 499 | |
| 500 | 500 | |
| 501 | 501 | /***********************************************************************//** |
| 502 | - * @brief Reserve space for Good Time Intervals | |
| 503 | - * | |
| 504 | - * @param[in] num Number of elements. | |
| 505 | - * | |
| 506 | - * This method does nothing (it is needed to satisfy the GContainer | |
| 507 | - * interface). | |
| 508 | - ***************************************************************************/ | |
| 509 | -void GGti::reserve(const int& num) | |
| 510 | -{ | |
| 511 | - // Return | |
| 512 | - return; | |
| 513 | -} | |
| 514 | - | |
| 515 | - | |
| 516 | -/***********************************************************************//** | |
| 517 | 502 | * @brief Append Good Time Intervals |
| 518 | 503 | * |
| 519 | 504 | * @param[in] gti Good Time Intervals. |
| ... | ... | @@ -867,30 +852,6 @@ void GGti::write(GXmlElement& xml) const |
| 867 | 852 | |
| 868 | 853 | |
| 869 | 854 | /***********************************************************************//** |
| 870 | - * @brief Returns earliest start time in Good Time Intervals | |
| 871 | - * | |
| 872 | - * @return Earliest start time in Good Time Intervals. | |
| 873 | - ***************************************************************************/ | |
| 874 | -const GTime& GGti::tstart(void) const | |
| 875 | -{ | |
| 876 | - // Return | |
| 877 | - return m_tstart; | |
| 878 | -} | |
| 879 | - | |
| 880 | - | |
| 881 | -/***********************************************************************//** | |
| 882 | - * @brief Returns latest stop time in Good Time Intervals | |
| 883 | - * | |
| 884 | - * @return Latest stop time in Good Time Intervals. | |
| 885 | - ***************************************************************************/ | |
| 886 | -const GTime& GGti::tstop(void) const | |
| 887 | -{ | |
| 888 | - // Return | |
| 889 | - return m_tstop; | |
| 890 | -} | |
| 891 | - | |
| 892 | - | |
| 893 | -/***********************************************************************//** | |
| 894 | 855 | * @brief Returns start time for a given Good Time Interval |
| 895 | 856 | * |
| 896 | 857 | * @param[in] index Good Time Interval index (0,...,size()-1). |
| ... | ... | @@ -935,71 +896,48 @@ const GTime& GGti::tstop(const int& index) const |
| 935 | 896 | |
| 936 | 897 | |
| 937 | 898 | /***********************************************************************//** |
| 938 | - * @brief Returns elapsed time | |
| 939 | - * | |
| 940 | - * @return Elapsed time [seconds]. | |
| 899 | + * @brief Computes overlap of time interval with GTIs | |
| 941 | 900 | * |
| 942 | - * Returns the elapsed time in seconds. The elapsed time is defined as the | |
| 943 | - * time difference between the latest stop time and the earliest start time | |
| 944 | - * in the Good Time Intervals. | |
| 945 | - ***************************************************************************/ | |
| 946 | -const double& GGti::telapse(void) const | |
| 947 | -{ | |
| 948 | - // Return | |
| 949 | - return m_telapse; | |
| 950 | -} | |
| 951 | - | |
| 952 | - | |
| 953 | -/***********************************************************************//** | |
| 954 | - * @brief Returns ontime | |
| 955 | - * | |
| 956 | - * @return Ontime [seconds]. | |
| 901 | + * @param[in] tstart Start time of interval. | |
| 902 | + * @param[in] tstop Stop time of interval. | |
| 903 | + * @return Overlap (seconds). | |
| 957 | 904 | * |
| 958 | - * Returns the ontime in seconds. The ontime is defined as the sum of all | |
| 959 | - * Good Time Intervals. | |
| 905 | + * Returns the overlap of time interval with GTIs in seconds. | |
| 960 | 906 | ***************************************************************************/ |
| 961 | -const double& GGti::ontime(void) const | |
| 907 | +double GGti::overlap(const GTime& tstart, const GTime& tstop) const | |
| 962 | 908 | { |
| 963 | - // Return | |
| 964 | - return m_ontime; | |
| 965 | -} | |
| 909 | + // Initialise overlap | |
| 910 | + double overlap = 0.0; | |
| 966 | 911 | |
| 912 | + // Compute the overlap | |
| 913 | + for (int i = 0; i < m_num; ++i) { | |
| 914 | + if (m_start[i] < tstart) { | |
| 915 | + if (m_stop[i] > tstart) { | |
| 916 | + if (m_stop[i] > tstop) { | |
| 917 | + overlap += tstop - tstart; | |
| 918 | + } | |
| 919 | + else { | |
| 920 | + overlap += m_stop[i] - tstart; | |
| 921 | + } | |
| 922 | + } | |
| 923 | + } | |
| 924 | + else if (m_stop[i] > tstop) { | |
| 925 | + if (m_start[i] < tstop) { | |
| 926 | + overlap += tstop - m_start[i]; | |
| 927 | + } | |
| 928 | + } | |
| 929 | + else { | |
| 930 | + overlap += m_stop[i] - m_start[i]; | |
| 931 | + } | |
| 932 | + } | |
| 967 | 933 | |
| 968 | -/***********************************************************************//** | |
| 969 | - * @brief Set time reference for Good Time Intervals | |
| 970 | - * | |
| 971 | - * @param[in] ref Time reference. | |
| 972 | - * | |
| 973 | - * Sets the time reference of the Good Time Intervals. This defines the | |
| 974 | - * reference time which will be writted into the FITS file upon saving of | |
| 975 | - * the Good Time Intervals. | |
| 976 | - ***************************************************************************/ | |
| 977 | -void GGti::reference(const GTimeReference& ref) | |
| 978 | -{ | |
| 979 | - // Set time reference | |
| 980 | - m_reference = ref; | |
| 981 | - | |
| 982 | 934 | // Return |
| 983 | - return; | |
| 984 | -} | |
| 985 | - | |
| 986 | - | |
| 987 | -/***********************************************************************//** | |
| 988 | - * @brief Return time reference for Good Time Intervals | |
| 989 | - * | |
| 990 | - * @return Time reference. | |
| 991 | - * | |
| 992 | - * Returns the time reference of the Good Time Intervals. | |
| 993 | - ***************************************************************************/ | |
| 994 | -const GTimeReference& GGti::reference(void) const | |
| 995 | -{ | |
| 996 | - // Return time reference | |
| 997 | - return m_reference; | |
| 935 | + return overlap; | |
| 998 | 936 | } |
| 999 | 937 | |
| 1000 | 938 | |
| 1001 | 939 | /***********************************************************************//** |
| 1002 | - * @brief Checks whether Good Time Intervals contain time | |
| 940 | + * @brief Checks whether Good Time Intervals contains time | |
| 1003 | 941 | * |
| 1004 | 942 | * @param[in] time Time to be checked. |
| 1005 | 943 | * | ... | ... |
test/test_GObservation.cpp
| ... | ... | @@ -630,8 +630,10 @@ void TestGObservation::test_gti(void) |
| 630 | 630 | test3.save("test_gti.fits[GOOD TIME INTERVALS]", true); |
| 631 | 631 | GGti load2("test_gti.fits[GOOD TIME INTERVALS]"); |
| 632 | 632 | test_value(load2.size(), 1, "GGti should have 1 interval."); |
| 633 | - test_value(load2.tstart().convert(GTimeReference(51544.5, "s")), 0.0, 1.0e-7, "Start time should be 0."); | |
| 634 | - test_value(load2.tstop().convert(GTimeReference(51544.5, "s")), 100.0, 1.0e-7, "Stop time should be 100."); | |
| 633 | + test_value(load2.tstart().convert(GTimeReference(51544.5, "s")), 0.0, 1.0e-7, | |
| 634 | + "Start time should be 0."); | |
| 635 | + test_value(load2.tstop().convert(GTimeReference(51544.5, "s")), 100.0, 1.0e-7, | |
| 636 | + "Stop time should be 100."); | |
| 635 | 637 | |
| 636 | 638 | // Check contains() method |
| 637 | 639 | GGti test6; |
| ... | ... | @@ -650,6 +652,29 @@ void TestGObservation::test_gti(void) |
| 650 | 652 | test_assert(!test6.contains(GTime(8.0)), "Check GGti::contains(): 8 is not contained"); |
| 651 | 653 | test_assert(!test6.contains(GTime(4.0)), "Check GGti::contains(): 4 is not contained"); |
| 652 | 654 | |
| 655 | + // Check overlap() method | |
| 656 | + GGti test7; | |
| 657 | + test7.append(GTime(5.0), GTime(6.0)); | |
| 658 | + test7.append(GTime(9.0), GTime(10.0)); | |
| 659 | + test_value(test7.overlap(GTime(0.0), GTime(5.0)), 0.0, 1.0e-7, | |
| 660 | + "Check GGti::overlap(): interval before GTIs"); | |
| 661 | + test_value(test7.overlap(GTime(10.0), GTime(15.0)), 0.0, 1.0e-7, | |
| 662 | + "Check GGti::overlap(): interval after GTIs"); | |
| 663 | + test_value(test7.overlap(GTime(4.0), GTime(5.5)), 0.5, 1.0e-7, | |
| 664 | + "Check GGti::overlap(): interval ends in GTI"); | |
| 665 | + test_value(test7.overlap(GTime(9.5), GTime(11.0)), 0.5, 1.0e-7, | |
| 666 | + "Check GGti::overlap(): interval starts in GTI"); | |
| 667 | + test_value(test7.overlap(GTime(4.0), GTime(7.0)), 1.0, 1.0e-7, | |
| 668 | + "Check GGti::overlap(): interval starts before and ends after GTI"); | |
| 669 | + test_value(test7.overlap(GTime(5.1), GTime(5.9)), 0.8, 1.0e-7, | |
| 670 | + "Check GGti::overlap(): interval within GTI"); | |
| 671 | + test_value(test7.overlap(GTime(5.0), GTime(10.0)), 2.0, 1.0e-7, | |
| 672 | + "Check GGti::overlap(): interval comprises exactly GTIs"); | |
| 673 | + test_value(test7.overlap(GTime(4.0), GTime(11.0)), 2.0, 1.0e-7, | |
| 674 | + "Check GGti::overlap(): interval comprises GTIs"); | |
| 675 | + test_value(test7.overlap(GTime(5.5), GTime(9.5)), 1.0, 1.0e-7, | |
| 676 | + "Check GGti::overlap(): interval partly overlaps GTIs"); | |
| 677 | + | |
| 653 | 678 | // Return |
| 654 | 679 | return; |
| 655 | 680 | } | ... | ... |