Epoch.py
6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"""Epoch module."""
import operator
import math
import datetime as DT
from matplotlib import cbook
from matplotlib.dates import date2num
class Epoch:
# Frame conversion offsets in seconds
# t(TO) = t(FROM) + allowed[ FROM ][ TO ]
allowed = {
"ET": {
"UTC": +64.1839,
},
"UTC": {
"ET": -64.1839,
},
}
def __init__(self, frame, sec=None, jd=None, daynum=None, dt=None):
"""
Create a new Epoch object.
Build an epoch 1 of 2 ways:
Using seconds past a Julian date:
# Epoch('ET', sec=1e8, jd=2451545)
or using a matplotlib day number
# Epoch('ET', daynum=730119.5)
= ERROR CONDITIONS
- If the input units are not in the allowed list, an error is thrown.
= INPUT VARIABLES
- frame The frame of the epoch. Must be 'ET' or 'UTC'
- sec The number of seconds past the input JD.
- jd The Julian date of the epoch.
- daynum The matplotlib day number of the epoch.
- dt A python datetime instance.
"""
if ((sec is None and jd is not None) or
(sec is not None and jd is None) or
(daynum is not None and
(sec is not None or jd is not None)) or
(daynum is None and dt is None and
(sec is None or jd is None)) or
(daynum is not None and dt is not None) or
(dt is not None and (sec is not None or jd is not None)) or
((dt is not None) and not isinstance(dt, DT.datetime))):
raise ValueError(
"Invalid inputs. Must enter sec and jd together, "
"daynum by itself, or dt (must be a python datetime).\n"
"Sec = %s\n"
"JD = %s\n"
"dnum= %s\n"
"dt = %s" % (sec, jd, daynum, dt))
cbook._check_in_list(self.allowed, frame=frame)
self._frame = frame
if dt is not None:
daynum = date2num(dt)
if daynum is not None:
# 1-JAN-0001 in JD = 1721425.5
jd = float(daynum) + 1721425.5
self._jd = math.floor(jd)
self._seconds = (jd - self._jd) * 86400.0
else:
self._seconds = float(sec)
self._jd = float(jd)
# Resolve seconds down to [ 0, 86400)
deltaDays = math.floor(self._seconds / 86400)
self._jd += deltaDays
self._seconds -= deltaDays * 86400.0
def convert(self, frame):
if self._frame == frame:
return self
offset = self.allowed[self._frame][frame]
return Epoch(frame, self._seconds + offset, self._jd)
def frame(self):
return self._frame
def julianDate(self, frame):
t = self
if frame != self._frame:
t = self.convert(frame)
return t._jd + t._seconds / 86400.0
def secondsPast(self, frame, jd):
t = self
if frame != self._frame:
t = self.convert(frame)
delta = t._jd - jd
return t._seconds + delta * 86400
def __eq__(self, rhs):
return self._cmp(rhs, operator.eq)
def __ne__(self, rhs):
return self._cmp(rhs, operator.ne)
def __lt__(self, rhs):
return self._cmp(rhs, operator.lt)
def __le__(self, rhs):
return self._cmp(rhs, operator.le)
def __gt__(self, rhs):
return self._cmp(rhs, operator.gt)
def __ge__(self, rhs):
return self._cmp(rhs, operator.ge)
def _cmp(self, rhs, op):
"""
Compare two Epoch's.
= INPUT VARIABLES
- rhs The Epoch to compare against.
- op The function to do the comparison
= RETURN VALUE
- Returns op(self, rhs)
"""
t = self
if self._frame != rhs._frame:
t = self.convert(rhs._frame)
if t._jd != rhs._jd:
return op(t._jd, rhs._jd)
return op(t._seconds, rhs._seconds)
def __add__(self, rhs):
"""
Add a duration to an Epoch.
= INPUT VARIABLES
- rhs The Epoch to subtract.
= RETURN VALUE
- Returns the difference of ourselves and the input Epoch.
"""
t = self
if self._frame != rhs.frame():
t = self.convert(rhs._frame)
sec = t._seconds + rhs.seconds()
return Epoch(t._frame, sec, t._jd)
def __sub__(self, rhs):
"""
Subtract two Epoch's or a Duration from an Epoch.
Valid:
Duration = Epoch - Epoch
Epoch = Epoch - Duration
= INPUT VARIABLES
- rhs The Epoch to subtract.
= RETURN VALUE
- Returns either the duration between to Epoch's or the a new
Epoch that is the result of subtracting a duration from an epoch.
"""
# Delay-load due to circular dependencies.
import matplotlib.testing.jpl_units as U
# Handle Epoch - Duration
if isinstance(rhs, U.Duration):
return self + -rhs
t = self
if self._frame != rhs._frame:
t = self.convert(rhs._frame)
days = t._jd - rhs._jd
sec = t._seconds - rhs._seconds
return U.Duration(rhs._frame, days*86400 + sec)
def __str__(self):
"""Print the Epoch."""
return "%22.15e %s" % (self.julianDate(self._frame), self._frame)
def __repr__(self):
"""Print the Epoch."""
return str(self)
@staticmethod
def range(start, stop, step):
"""
Generate a range of Epoch objects.
Similar to the Python range() method. Returns the range [
start, stop) at the requested step. Each element will be a
Epoch object.
= INPUT VARIABLES
- start The starting value of the range.
- stop The stop value of the range.
- step Step to use.
= RETURN VALUE
- Returns a list containing the requested Epoch values.
"""
elems = []
i = 0
while True:
d = start + i * step
if d >= stop:
break
elems.append(d)
i += 1
return elems