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.

data.py 7.1 KiB

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