A library that computes the ephemerides.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

239 lines
6.8 KiB

  1. #!/usr/bin/env python3
  2. from abc import ABC, abstractmethod
  3. from typing import Union
  4. from datetime import datetime
  5. from numpy import pi, arcsin
  6. from skyfield.api import Topos, Time
  7. from skyfield.vectorlib import VectorSum as SkfPlanet
  8. from .core import get_skf_objects
  9. from .enum import MoonPhaseType, EventType
  10. class Serializable(ABC):
  11. @abstractmethod
  12. def serialize(self) -> dict:
  13. pass
  14. class MoonPhase(Serializable):
  15. def __init__(
  16. self,
  17. phase_type: MoonPhaseType,
  18. time: datetime = None,
  19. next_phase_date: datetime = None,
  20. ):
  21. self.phase_type = phase_type
  22. self.time = time
  23. self.next_phase_date = next_phase_date
  24. def get_next_phase(self):
  25. if self.phase_type in [MoonPhaseType.NEW_MOON, MoonPhaseType.WAXING_CRESCENT]:
  26. return MoonPhaseType.FIRST_QUARTER
  27. if self.phase_type in [
  28. MoonPhaseType.FIRST_QUARTER,
  29. MoonPhaseType.WAXING_GIBBOUS,
  30. ]:
  31. return MoonPhaseType.FULL_MOON
  32. if self.phase_type in [MoonPhaseType.FULL_MOON, MoonPhaseType.WANING_GIBBOUS]:
  33. return MoonPhaseType.LAST_QUARTER
  34. return MoonPhaseType.NEW_MOON
  35. def serialize(self) -> dict:
  36. return {
  37. "phase": self.phase_type.name,
  38. "time": self.time.isoformat() if self.time is not None else None,
  39. "next": {
  40. "phase": self.get_next_phase().name,
  41. "time": self.next_phase_date.isoformat(),
  42. },
  43. }
  44. class Object(Serializable):
  45. """
  46. An astronomical object.
  47. """
  48. def __init__(self, name: str, skyfield_name: str, radius: float = None):
  49. """
  50. Initialize an astronomical object
  51. :param str name: the official name of the object (may be internationalized)
  52. :param str skyfield_name: the internal name of the object in Skyfield library
  53. :param float radius: the radius (in km) of the object
  54. :param AsterEphemerides ephemerides: the ephemerides associated to the object
  55. """
  56. self.name = name
  57. self.skyfield_name = skyfield_name
  58. self.radius = radius
  59. def __repr__(self):
  60. return "<Object type=%s name=%s />" % (self.get_type(), self.name)
  61. def get_skyfield_object(self) -> SkfPlanet:
  62. return get_skf_objects()[self.skyfield_name]
  63. @abstractmethod
  64. def get_type(self) -> str:
  65. pass
  66. def get_apparent_radius(self, time: Time, from_place) -> float:
  67. """
  68. Calculate the apparent radius, in degrees, of the object from the given place at a given time.
  69. :param time:
  70. :param from_place:
  71. :return:
  72. """
  73. if self.radius is None:
  74. raise ValueError("Missing radius for %s object" % self.name)
  75. return (
  76. 360
  77. / pi
  78. * arcsin(
  79. self.radius
  80. / from_place.at(time).observe(self.get_skyfield_object()).distance().km
  81. )
  82. )
  83. def serialize(self) -> dict:
  84. return {
  85. "name": self.name,
  86. "type": self.get_type(),
  87. "radius": self.radius,
  88. }
  89. class Star(Object):
  90. def get_type(self) -> str:
  91. return "star"
  92. class Planet(Object):
  93. def get_type(self) -> str:
  94. return "planet"
  95. class DwarfPlanet(Planet):
  96. def get_type(self) -> str:
  97. return "dwarf_planet"
  98. class Satellite(Object):
  99. def get_type(self) -> str:
  100. return "satellite"
  101. class Event(Serializable):
  102. def __init__(
  103. self,
  104. event_type: EventType,
  105. objects: [Object],
  106. start_time: datetime,
  107. end_time: Union[datetime, None] = None,
  108. details: str = None,
  109. ):
  110. self.event_type = event_type
  111. self.objects = objects
  112. self.start_time = start_time
  113. self.end_time = end_time
  114. self.details = details
  115. def __repr__(self):
  116. return "<Event type=%s objects=%s start=%s end=%s details=%s />" % (
  117. self.event_type.name,
  118. self.objects,
  119. self.start_time,
  120. self.end_time,
  121. self.details,
  122. )
  123. def get_description(self, show_details: bool = True) -> str:
  124. description = self.event_type.value % self._get_objects_name()
  125. if show_details and self.details is not None:
  126. description += " ({:s})".format(self.details)
  127. return description
  128. def _get_objects_name(self):
  129. if len(self.objects) == 1:
  130. return self.objects[0].name
  131. return tuple(object.name for object in self.objects)
  132. def serialize(self) -> dict:
  133. return {
  134. "objects": [object.serialize() for object in self.objects],
  135. "EventType": self.event_type.name,
  136. "starts_at": self.start_time.isoformat(),
  137. "ends_at": self.end_time.isoformat() if self.end_time is not None else None,
  138. "details": self.details,
  139. }
  140. class AsterEphemerides(Serializable):
  141. def __init__(
  142. self,
  143. rise_time: Union[datetime, None],
  144. culmination_time: Union[datetime, None],
  145. set_time: Union[datetime, None],
  146. aster: Object,
  147. ):
  148. self.rise_time = rise_time
  149. self.culmination_time = culmination_time
  150. self.set_time = set_time
  151. self.object = aster
  152. def serialize(self) -> dict:
  153. return {
  154. "object": self.object.serialize(),
  155. "rise_time": self.rise_time.isoformat()
  156. if self.rise_time is not None
  157. else None,
  158. "culmination_time": self.culmination_time.isoformat()
  159. if self.culmination_time is not None
  160. else None,
  161. "set_time": self.set_time.isoformat()
  162. if self.set_time is not None
  163. else None,
  164. }
  165. EARTH = Planet("Earth", "EARTH")
  166. ASTERS = [
  167. Star("Sun", "SUN", radius=696342),
  168. Satellite("Moon", "MOON", radius=1737.4),
  169. Planet("Mercury", "MERCURY", radius=2439.7),
  170. Planet("Venus", "VENUS", radius=6051.8),
  171. Planet("Mars", "MARS", radius=3396.2),
  172. Planet("Jupiter", "JUPITER BARYCENTER", radius=71492),
  173. Planet("Saturn", "SATURN BARYCENTER", radius=60268),
  174. Planet("Uranus", "URANUS BARYCENTER", radius=25559),
  175. Planet("Neptune", "NEPTUNE BARYCENTER", radius=24764),
  176. Planet("Pluto", "PLUTO BARYCENTER", radius=1185),
  177. ]
  178. class Position:
  179. def __init__(self, latitude: float, longitude: float, aster: Object = EARTH):
  180. self.latitude = latitude
  181. self.longitude = longitude
  182. self.aster = aster
  183. self._topos = None
  184. def get_planet_topos(self) -> Topos:
  185. if self.aster is None:
  186. raise TypeError("Observation planet must be set.")
  187. if self._topos is None:
  188. self._topos = self.aster.get_skyfield_object() + Topos(
  189. latitude_degrees=self.latitude, longitude_degrees=self.longitude
  190. )
  191. return self._topos