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.
 
 

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