Coverage for tests/test_time_utils.py: 15%

75 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-04-04 02:06 -0700

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <https://www.gnu.org/licenses/>. 

21 

22"""Unit tests for methods in butler.time module. 

23""" 

24 

25import unittest 

26import warnings 

27 

28# As of astropy 4.2, the erfa interface is shipped independently and 

29# ErfaWarning is no longer an AstropyWarning 

30try: 

31 import erfa 

32except ImportError: 

33 erfa = None 

34 

35import astropy.utils.exceptions 

36from astropy.time import Time, TimeDelta 

37from lsst.daf.butler.core.time_utils import TimeConverter 

38 

39 

40class TimeTestCase(unittest.TestCase): 

41 """A test case for time module""" 

42 

43 def test_time_before_epoch(self): 

44 """Tests for before-the-epoch time.""" 

45 time = Time("1950-01-01T00:00:00", format="isot", scale="tai") 

46 value = TimeConverter().astropy_to_nsec(time) 

47 self.assertEqual(value, 0) 

48 

49 value = TimeConverter().nsec_to_astropy(value) 

50 self.assertEqual(value, TimeConverter().epoch) 

51 

52 def test_max_time(self): 

53 """Tests for after-the-end-of-astronomy time.""" 

54 # there are rounding issues, need more complex comparison 

55 time = Time("2101-01-01T00:00:00", format="isot", scale="tai") 

56 value = TimeConverter().astropy_to_nsec(time) 

57 

58 value_max = TimeConverter().astropy_to_nsec(TimeConverter().max_time) 

59 self.assertEqual(value, value_max) 

60 

61 # Astropy will give "dubious year" for UTC five years in the future 

62 # so hide these expected warnings from the test output 

63 with warnings.catch_warnings(): 

64 warnings.simplefilter("ignore", category=astropy.utils.exceptions.AstropyWarning) 

65 if erfa is not None: 

66 warnings.simplefilter("ignore", category=erfa.ErfaWarning) 

67 time = Time("2101-01-01T00:00:00", format="isot", scale="utc") 

68 

69 # unittest can't test for no warnings so we run the test and 

70 # trigger our own warning and count all the warnings 

71 with self.assertWarns(Warning) as cm: 

72 TimeConverter().astropy_to_nsec(time) 

73 warnings.warn("deliberate") 

74 self.assertEqual(str(cm.warning), "deliberate") 

75 

76 def test_round_trip(self): 

77 """Test precision of round-trip conversion.""" 

78 # do tests at random points between epoch and max. time 

79 times = [ 

80 "1970-01-01T12:00:00.123", 

81 "1999-12-31T23:59:57.123456", 

82 "2000-01-01T12:00:00.123456", 

83 "2030-01-01T12:00:00.123456789", 

84 "2075-08-17T00:03:45", 

85 "2099-12-31T23:00:50", 

86 ] 

87 for time in times: 

88 atime = Time(time, format="isot", scale="tai") 

89 for sec in range(7): 

90 # loop over few seconds to add to each time 

91 for i in range(100): 

92 # loop over additional fractions of seconds 

93 delta = sec + 0.3e-9 * i 

94 in_time = atime + TimeDelta(delta, format="sec") 

95 # do round-trip conversion to nsec and back 

96 value = TimeConverter().astropy_to_nsec(in_time) 

97 value = TimeConverter().nsec_to_astropy(value) 

98 delta2 = value - in_time 

99 delta2_sec = delta2.to_value("sec") 

100 # absolute precision should be better than half 

101 # nanosecond, but there are rounding errors too 

102 self.assertLess(abs(delta2_sec), 0.51e-9) 

103 

104 def test_times_equal(self): 

105 """Test for times_equal method""" 

106 # time == time should always work 

107 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai") 

108 self.assertTrue(TimeConverter().times_equal(time1, time1)) 

109 

110 # one nsec difference 

111 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai") 

112 time2 = Time("2000-01-01T00:00:00.123456788", format="isot", scale="tai") 

113 self.assertTrue(TimeConverter().times_equal(time1, time2, 2.0)) 

114 self.assertTrue(TimeConverter().times_equal(time2, time1, 2.0)) 

115 self.assertFalse(TimeConverter().times_equal(time1, time2, 0.5)) 

116 self.assertFalse(TimeConverter().times_equal(time2, time1, 0.5)) 

117 

118 # one nsec difference, times in UTC 

119 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="utc") 

120 time2 = Time("2000-01-01T00:00:00.123456788", format="isot", scale="utc") 

121 self.assertTrue(TimeConverter().times_equal(time1, time2, 2.0)) 

122 self.assertTrue(TimeConverter().times_equal(time2, time1, 2.0)) 

123 self.assertFalse(TimeConverter().times_equal(time1, time2, 0.5)) 

124 self.assertFalse(TimeConverter().times_equal(time2, time1, 0.5)) 

125 

126 # 1/2 nsec difference 

127 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai") 

128 time2 = time1 + TimeDelta(0.5e-9, format="sec") 

129 self.assertTrue(TimeConverter().times_equal(time1, time2)) 

130 self.assertTrue(TimeConverter().times_equal(time2, time1)) 

131 self.assertFalse(TimeConverter().times_equal(time1, time2, 0.25)) 

132 self.assertFalse(TimeConverter().times_equal(time2, time1, 0.25)) 

133 

134 # 1/2 microsec difference 

135 time1 = Time("2000-01-01T00:00:00.123456789", format="isot", scale="tai") 

136 time2 = time1 + TimeDelta(0.5e-6, format="sec") 

137 self.assertTrue(TimeConverter().times_equal(time1, time2, 501)) 

138 self.assertTrue(TimeConverter().times_equal(time2, time1, 501)) 

139 self.assertFalse(TimeConverter().times_equal(time1, time2, 499)) 

140 self.assertFalse(TimeConverter().times_equal(time2, time1, 499)) 

141 

142 # UTC vs TAI 

143 time1 = Time("2013-06-17 13:34:45.775000", scale="tai", format="iso") 

144 time2 = Time("2013-06-17T13:34:10.775", scale="utc", format="isot") 

145 self.assertTrue(TimeConverter().times_equal(time1, time2)) 

146 self.assertEqual(TimeConverter().astropy_to_nsec(time1), TimeConverter().astropy_to_nsec(time2)) 

147 

148 

149if __name__ == "__main__": 

150 unittest.main()