A library that computes the ephemerides.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

data.py 7.7 KiB

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