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.
 
 

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