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.
 
 

372 lines
12 KiB

  1. #!/usr/bin/env python3
  2. # Kosmorrolib - The Library To Compute Your Ephemerides
  3. # Copyright (C) 2021 Jérôme Deuchnord <jerome@deuchnord.fr>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. from abc import ABC, abstractmethod
  18. from typing import Union
  19. from datetime import datetime, timezone
  20. from math import asin
  21. from skyfield.api import Topos, Time, Angle
  22. from skyfield.vectorlib import VectorSum as SkfPlanet
  23. from .core import get_skf_objects, get_timescale, deprecated
  24. from .enum import MoonPhaseType, EventType, ObjectIdentifier, ObjectType
  25. class Serializable(ABC):
  26. @abstractmethod
  27. def serialize(self) -> dict:
  28. pass
  29. class MoonPhase(Serializable):
  30. def __init__(
  31. self,
  32. phase_type: MoonPhaseType,
  33. time: datetime = None,
  34. next_phase_date: datetime = None,
  35. ):
  36. self.phase_type = phase_type
  37. self.time = time
  38. self.next_phase_date = next_phase_date
  39. def __repr__(self):
  40. return "<MoonPhase phase_type=%s time=%s next_phase_date=%s>" % (
  41. self.phase_type,
  42. self.time,
  43. self.next_phase_date,
  44. )
  45. def get_next_phase(self):
  46. """Helper to get the Moon phase that follows the one described by the object.
  47. If the current Moon phase is New Moon or Waxing crescent, the next one will be First Quarter:
  48. >>> moon_phase = MoonPhase(MoonPhaseType.NEW_MOON)
  49. >>> moon_phase.get_next_phase()
  50. <MoonPhaseType.FIRST_QUARTER: 2>
  51. >>> moon_phase = MoonPhase(MoonPhaseType.NEW_MOON)
  52. >>> moon_phase.get_next_phase()
  53. <MoonPhaseType.FIRST_QUARTER: 2>
  54. If the current Moon phase is First Quarter or Waxing gibbous, the next one will be Full Moon:
  55. >>> moon_phase = MoonPhase(MoonPhaseType.FIRST_QUARTER)
  56. >>> moon_phase.get_next_phase()
  57. <MoonPhaseType.FULL_MOON: 4>
  58. >>> moon_phase = MoonPhase(MoonPhaseType.WAXING_GIBBOUS)
  59. >>> moon_phase.get_next_phase()
  60. <MoonPhaseType.FULL_MOON: 4>
  61. If the current Moon phase is Full Moon or Waning gibbous, the next one will be Last Quarter:
  62. >>> moon_phase = MoonPhase(MoonPhaseType.FULL_MOON)
  63. >>> moon_phase.get_next_phase()
  64. <MoonPhaseType.LAST_QUARTER: 6>
  65. >>> moon_phase = MoonPhase(MoonPhaseType.WANING_GIBBOUS)
  66. >>> moon_phase.get_next_phase()
  67. <MoonPhaseType.LAST_QUARTER: 6>
  68. If the current Moon phase is Last Quarter Moon or Waning crescent, the next one will be New Moon:
  69. >>> moon_phase = MoonPhase(MoonPhaseType.LAST_QUARTER)
  70. >>> moon_phase.get_next_phase()
  71. <MoonPhaseType.NEW_MOON: 0>
  72. >>> moon_phase = MoonPhase(MoonPhaseType.WANING_CRESCENT)
  73. >>> moon_phase.get_next_phase()
  74. <MoonPhaseType.NEW_MOON: 0>
  75. """
  76. if self.phase_type in [MoonPhaseType.NEW_MOON, MoonPhaseType.WAXING_CRESCENT]:
  77. return MoonPhaseType.FIRST_QUARTER
  78. if self.phase_type in [
  79. MoonPhaseType.FIRST_QUARTER,
  80. MoonPhaseType.WAXING_GIBBOUS,
  81. ]:
  82. return MoonPhaseType.FULL_MOON
  83. if self.phase_type in [MoonPhaseType.FULL_MOON, MoonPhaseType.WANING_GIBBOUS]:
  84. return MoonPhaseType.LAST_QUARTER
  85. return MoonPhaseType.NEW_MOON
  86. def serialize(self) -> dict:
  87. return {
  88. "phase": self.phase_type.name,
  89. "time": self.time.isoformat() if self.time is not None else None,
  90. "next": {
  91. "phase": self.get_next_phase().name,
  92. "time": self.next_phase_date.isoformat(),
  93. },
  94. }
  95. class Object(Serializable):
  96. """
  97. An astronomical object.
  98. """
  99. def __init__(
  100. self,
  101. identifier: ObjectIdentifier,
  102. skyfield_object: SkfPlanet,
  103. radius: float = None,
  104. ):
  105. """
  106. Initialize an astronomical object
  107. :param ObjectIdentifier identifier: the official name of the object (may be internationalized)
  108. :param str skyfield_object: the object from Skyfield library
  109. :param float radius: the radius (in km) of the object
  110. """
  111. self.identifier = identifier
  112. self.skyfield_object = skyfield_object
  113. self.radius = radius
  114. def __repr__(self):
  115. return "<Object type=%s name=%s />" % (
  116. self.get_type().name,
  117. self.identifier.name,
  118. )
  119. @abstractmethod
  120. def get_type(self) -> ObjectType:
  121. pass
  122. def get_apparent_radius(self, for_date: Union[Time, datetime]) -> Angle:
  123. """Calculate the apparent radius, in degrees, of the object from the given place at a given time.
  124. **Warning:** this is an internal function, not intended for use by end-developers.
  125. For an easier usage, this method accepts datetime and Skyfield's Time objects:
  126. >>> sun = ASTERS[0]
  127. >>> sun.get_apparent_radius(datetime(2021, 6, 9, tzinfo=timezone.utc))
  128. <Angle 00deg 31' 31.6">
  129. >>> sun.get_apparent_radius(get_timescale().utc(2021, 6, 9))
  130. <Angle 00deg 31' 31.6">
  131. Source of the algorithm: https://rhodesmill.org/skyfield/examples.html#what-is-the-angular-diameter-of-a-planet-given-its-radius
  132. :param for_date: the date for which the apparent radius has to be returned
  133. :return: an object representing a Skyfield angle
  134. """
  135. if isinstance(for_date, datetime):
  136. for_date = get_timescale().from_datetime(for_date)
  137. ra, dec, distance = (
  138. EARTH.skyfield_object.at(for_date)
  139. .observe(self.skyfield_object)
  140. .apparent()
  141. .radec()
  142. )
  143. return Angle(radians=asin(self.radius / distance.km) * 2.0)
  144. def serialize(self) -> dict:
  145. """Serialize the given object
  146. >>> planet = Planet(ObjectIdentifier.MARS, "MARS")
  147. >>> planet.serialize()
  148. {'identifier': 'MARS', 'type': 'PLANET', 'radius': None}
  149. """
  150. return {
  151. "identifier": self.identifier.name,
  152. "type": self.get_type().name,
  153. "radius": self.radius,
  154. }
  155. class Star(Object):
  156. def get_type(self) -> ObjectType:
  157. return ObjectType.STAR
  158. class Planet(Object):
  159. def get_type(self) -> ObjectType:
  160. return ObjectType.PLANET
  161. class DwarfPlanet(Planet):
  162. def get_type(self) -> ObjectType:
  163. return ObjectType.DWARF_PLANET
  164. class Satellite(Object):
  165. def get_type(self) -> ObjectType:
  166. return ObjectType.SATELLITE
  167. class Event(Serializable):
  168. def __init__(
  169. self,
  170. event_type: EventType,
  171. objects: [Object],
  172. start_time: datetime,
  173. end_time: Union[datetime, None] = None,
  174. details: {str: any} = None,
  175. ):
  176. self.event_type = event_type
  177. self.objects = objects
  178. self.start_time = start_time
  179. self.end_time = end_time
  180. self.details = details
  181. def __repr__(self):
  182. return "<Event type=%s objects=%s start=%s end=%s details=%s />" % (
  183. self.event_type.name,
  184. self.objects,
  185. self.start_time,
  186. self.end_time,
  187. self.details,
  188. )
  189. def __eq__(self, other: object) -> bool:
  190. return (
  191. isinstance(other, Event)
  192. and self.event_type.name == other.event_type.name
  193. and self.objects == other.objects
  194. and self.start_time == other.start_time
  195. and self.end_time == other.end_time
  196. and self.details == other.details
  197. )
  198. @deprecated(
  199. "kosmorrolib.Event.get_description method is deprecated since version 1.1 "
  200. "and will be removed in version 2.0. "
  201. "It should be reimplemented it in your own code."
  202. )
  203. def get_description(self, show_details: bool = True) -> str:
  204. """Return a textual description for the given details
  205. *Deprecated* since version 1.1
  206. """
  207. description = f"Event of type {str(self.event_type)}"
  208. if show_details and len(self.details) > 0:
  209. details = ""
  210. for key in self.details:
  211. if details != "":
  212. details += ", "
  213. details += f"{key}: {self.details[key]}"
  214. description += f" ({details})"
  215. return description
  216. def serialize(self) -> dict:
  217. return {
  218. "objects": [object.serialize() for object in self.objects],
  219. "EventType": self.event_type.name,
  220. "starts_at": self.start_time.isoformat(),
  221. "ends_at": self.end_time.isoformat() if self.end_time is not None else None,
  222. "details": self.details,
  223. }
  224. class AsterEphemerides(Serializable):
  225. def __init__(
  226. self,
  227. rise_time: Union[datetime, None],
  228. culmination_time: Union[datetime, None],
  229. set_time: Union[datetime, None],
  230. aster: Object,
  231. ):
  232. self.rise_time = rise_time
  233. self.culmination_time = culmination_time
  234. self.set_time = set_time
  235. self.object = aster
  236. def __repr__(self):
  237. return (
  238. "<AsterEphemerides rise_time=%s culmination_time=%s set_time=%s aster=%s>"
  239. % (self.rise_time, self.culmination_time, self.set_time, self.object)
  240. )
  241. def serialize(self) -> dict:
  242. return {
  243. "object": self.object.serialize(),
  244. "rise_time": self.rise_time.isoformat()
  245. if self.rise_time is not None
  246. else None,
  247. "culmination_time": self.culmination_time.isoformat()
  248. if self.culmination_time is not None
  249. else None,
  250. "set_time": self.set_time.isoformat()
  251. if self.set_time is not None
  252. else None,
  253. }
  254. EARTH = Planet(ObjectIdentifier.EARTH, get_skf_objects()["EARTH"])
  255. ASTERS = [
  256. Star(ObjectIdentifier.SUN, get_skf_objects()["SUN"], radius=696342),
  257. Satellite(ObjectIdentifier.MOON, get_skf_objects()["MOON"], radius=1737.4),
  258. Planet(ObjectIdentifier.MERCURY, get_skf_objects()["MERCURY"], radius=2439.7),
  259. Planet(ObjectIdentifier.VENUS, get_skf_objects()["VENUS"], radius=6051.8),
  260. Planet(ObjectIdentifier.MARS, get_skf_objects()["MARS"], radius=3396.2),
  261. Planet(
  262. ObjectIdentifier.JUPITER, get_skf_objects()["JUPITER BARYCENTER"], radius=71492
  263. ),
  264. Planet(
  265. ObjectIdentifier.SATURN, get_skf_objects()["SATURN BARYCENTER"], radius=60268
  266. ),
  267. Planet(
  268. ObjectIdentifier.URANUS, get_skf_objects()["URANUS BARYCENTER"], radius=25559
  269. ),
  270. Planet(
  271. ObjectIdentifier.NEPTUNE, get_skf_objects()["NEPTUNE BARYCENTER"], radius=24764
  272. ),
  273. Planet(ObjectIdentifier.PLUTO, get_skf_objects()["PLUTO BARYCENTER"], radius=1185),
  274. ]
  275. def get_aster(identifier: ObjectIdentifier) -> Object:
  276. """Return the aster with the given identifier
  277. >>> get_aster(ObjectIdentifier.SATURN)
  278. <Object type=PLANET name=SATURN />
  279. You can also use it to get the `EARTH` object, even though it has its own constant:
  280. <Object type=PLANET name=EARTH />
  281. """
  282. if identifier == ObjectIdentifier.EARTH:
  283. return EARTH
  284. for aster in ASTERS:
  285. if aster.identifier == identifier:
  286. return aster
  287. class Position:
  288. def __init__(self, latitude: float, longitude: float):
  289. self.latitude = latitude
  290. self.longitude = longitude
  291. self._topos = None
  292. def get_planet_topos(self) -> Topos:
  293. if self._topos is None:
  294. self._topos = EARTH.skyfield_object + Topos(
  295. latitude_degrees=self.latitude, longitude_degrees=self.longitude
  296. )
  297. return self._topos