Epoch.py 6.0 KB

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