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 

26 

27import astropy.time 

28 

29 

30# These constants can be used by client code. 

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

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

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

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

35stored in the database. 

36""" 

37 

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

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

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

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

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

43consideration. 

44""" 

45 

46# number of nanosecons in a day 

47_NSEC_PER_DAY = 1_000_000_000 * 24 * 3600 

48 

49_LOG = logging.getLogger(__name__) 

50 

51 

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

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

54 

55 Input time is converted to TAI scale before conversion to 

56 nanoseconds. 

57 

58 Parameters 

59 ---------- 

60 astropy_time : `astropy.time.Time` 

61 Time to be converted. 

62 

63 Returns 

64 ------- 

65 time_nsec : `int` 

66 Nanoseconds since epoch. 

67 

68 Note 

69 ---- 

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

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

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

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

74 """ 

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

76 # scale, transform it to TAI before doing anyhting 

77 value = astropy_time.tai 

78 # anything before epoch or after MAX_TIME is truncated 

79 if value < EPOCH: 

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

81 astropy_time, EPOCH) 

82 value = EPOCH 

83 elif value > MAX_TIME: 

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

85 value, MAX_TIME) 

86 value = MAX_TIME 

87 

88 delta = value - EPOCH 

89 # Special care needed to preserve nanosecond precision. 

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

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

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

93 return value 

94 

95 

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

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

98 

99 Parameters 

100 ---------- 

101 time_nsec : `int` 

102 Nanoseconds since epoch. 

103 

104 Returns 

105 ------- 

106 astropy_time : `astropy.time.Time` 

107 Time to be converted. 

108 

109 Note 

110 ---- 

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

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

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

114 time that is outside of that range. 

115 """ 

116 jd1, jd2 = divmod(time_nsec, _NSEC_PER_DAY) 

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

118 value = EPOCH + delta 

119 return value 

120 

121 

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

123 time2: astropy.time.Time, 

124 precision_nsec: float = 1.0) -> bool: 

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

126 

127 Parameters 

128 ---------- 

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

130 Times to compare. 

131 precision_nsec : `float`, optional 

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

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

134 to/from integer nanoseconds. 

135 """ 

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

137 # bring them both to TAI scale 

138 time1 = time1.tai 

139 time2 = time2.tai 

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

141 delta *= _NSEC_PER_DAY 

142 return abs(delta) < precision_nsec