Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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# (http://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 <http://www.gnu.org/licenses/>. 

21from __future__ import annotations 

22 

23__all__ = ("astropy_to_nsec", "nsec_to_astropy", "times_equal") 

24 

25import logging 

26import warnings 

27 

28import astropy.time 

29import astropy.utils.exceptions 

30 

31# These constants can be used by client code. 

32# EPOCH is used to construct times as read from database, its precision is 

33# used by all those timestamps, set it to 1 microsecond. 

34EPOCH = astropy.time.Time("1970-01-01 00:00:00", format="iso", scale="tai", precision=6) 

35"""Epoch for calculating time delta, this is the minimum time that can be 

36stored in the database. 

37""" 

38 

39MAX_TIME = astropy.time.Time("2100-01-01 00:00:00", format="iso", scale="tai") 

40"""Maximum time value that we can store. Assuming 64-bit integer field we 

41can actually store higher values but we intentionally limit it to arbitrary 

42but reasonably high value. Note that this value will be stored in registry 

43database for eternity, so it should not be changed without proper 

44consideration. 

45""" 

46 

47# number of nanosecons in a day 

48_NSEC_PER_DAY = 1_000_000_000 * 24 * 3600 

49 

50_LOG = logging.getLogger(__name__) 

51 

52 

53def astropy_to_nsec(astropy_time: astropy.time.Time) -> int: 

54 """Convert astropy time to nanoseconds since epoch. 

55 

56 Input time is converted to TAI scale before conversion to 

57 nanoseconds. 

58 

59 Parameters 

60 ---------- 

61 astropy_time : `astropy.time.Time` 

62 Time to be converted. 

63 

64 Returns 

65 ------- 

66 time_nsec : `int` 

67 Nanoseconds since epoch. 

68 

69 Note 

70 ---- 

71 Only the limited range of input times is supported by this method as it 

72 is defined useful in the context of Butler and Registry. If input time is 

73 earlier than epoch time then this method returns 0. If input time comes 

74 after the max. time then it returns number corresponding to max. time. 

75 """ 

76 # sometimes comparison produces warnings if input value is in UTC 

77 # scale, transform it to TAI before doing anything but also trap 

78 # warnings in case we are dealing with simulated data from the future 

79 with warnings.catch_warnings(): 

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

81 value = astropy_time.tai 

82 # anything before epoch or after MAX_TIME is truncated 

83 if value < EPOCH: 

84 _LOG.warning("'%s' is earlier than epoch time '%s', epoch time will be used instead", 

85 astropy_time, EPOCH) 

86 value = EPOCH 

87 elif value > MAX_TIME: 

88 _LOG.warning("'%s' is later than max. time '%s', max. time time will be used instead", 

89 value, MAX_TIME) 

90 value = MAX_TIME 

91 

92 delta = value - EPOCH 

93 # Special care needed to preserve nanosecond precision. 

94 # Usually jd1 has no fractional part but just in case. 

95 jd1, extra_jd2 = divmod(delta.jd1, 1) 

96 value = int(jd1) * _NSEC_PER_DAY + int(round((delta.jd2 + extra_jd2)*_NSEC_PER_DAY)) 

97 return value 

98 

99 

100def nsec_to_astropy(time_nsec: int) -> astropy.time.Time: 

101 """Convert nanoseconds since epoch to astropy time. 

102 

103 Parameters 

104 ---------- 

105 time_nsec : `int` 

106 Nanoseconds since epoch. 

107 

108 Returns 

109 ------- 

110 astropy_time : `astropy.time.Time` 

111 Time to be converted. 

112 

113 Note 

114 ---- 

115 Usually the input time for this method is the number returned from 

116 `astropy_to_nsec` which has a limited range. This method does not check 

117 that the number falls in the supported range and can produce output 

118 time that is outside of that range. 

119 """ 

120 jd1, jd2 = divmod(time_nsec, _NSEC_PER_DAY) 

121 delta = astropy.time.TimeDelta(float(jd1), float(jd2)/_NSEC_PER_DAY, format="jd", scale="tai") 

122 value = EPOCH + delta 

123 return value 

124 

125 

126def times_equal(time1: astropy.time.Time, 

127 time2: astropy.time.Time, 

128 precision_nsec: float = 1.0) -> bool: 

129 """Check that times are equal within specified precision. 

130 

131 Parameters 

132 ---------- 

133 time1, time2 : `astropy.time.Time` 

134 Times to compare. 

135 precision_nsec : `float`, optional 

136 Precision to use for comparison in nanoseconds, default is one 

137 nanosecond which is larger that round-trip error for conversion 

138 to/from integer nanoseconds. 

139 """ 

140 # To compare we need them in common scale, for simplicity just 

141 # bring them both to TAI scale 

142 # Hide any warnings from this conversion since they are not relevant 

143 # to the equality 

144 with warnings.catch_warnings(): 

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

146 time1 = time1.tai 

147 time2 = time2.tai 

148 delta = (time2.jd1 - time1.jd1) + (time2.jd2 - time1.jd2) 

149 delta *= _NSEC_PER_DAY 

150 return abs(delta) < precision_nsec