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.
 
 
 
 

225 lines
7.7 KiB

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