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.
 
 
 
 

162 lines
5.1 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 skyfield.api import Topos
  20. from skyfield.timelib import Time
  21. MOON_PHASES = {
  22. 'NEW_MOON': 'New Moon',
  23. 'WAXING_CRESCENT': 'Waxing crescent',
  24. 'FIRST_QUARTER': 'First Quarter',
  25. 'WAXING_GIBBOUS': 'Waxing gibbous',
  26. 'FULL_MOON': 'Full Moon',
  27. 'WANING_GIBBOUS': 'Waning gibbous',
  28. 'LAST_QUARTER': 'Last Quarter',
  29. 'WANING_CRESCENT': 'Waning crescent'
  30. }
  31. EVENTS = {
  32. 'OPPOSITION': {'message': '%s is in opposition'},
  33. 'CONJUNCTION': {'message': '%s and %s are in conjunction'}
  34. }
  35. class MoonPhase:
  36. def __init__(self, identifier: str, time: Union[Time, None], next_phase_date: Union[Time, None]):
  37. if identifier not in MOON_PHASES.keys():
  38. raise ValueError('identifier parameter must be one of %s (got %s)' % (', '.join(MOON_PHASES.keys()),
  39. identifier))
  40. self.identifier = identifier
  41. self.time = time
  42. self.next_phase_date = next_phase_date
  43. def get_phase(self):
  44. return MOON_PHASES[self.identifier]
  45. def get_next_phase(self):
  46. if self.identifier == 'NEW_MOON' or self.identifier == 'WAXING_CRESCENT':
  47. next_identifier = 'FIRST_QUARTER'
  48. elif self.identifier == 'FIRST_QUARTER' or self.identifier == 'WAXING_GIBBOUS':
  49. next_identifier = 'FULL_MOON'
  50. elif self.identifier == 'FULL_MOON' or self.identifier == 'WANING_GIBBOUS':
  51. next_identifier = 'LAST_QUARTER'
  52. else:
  53. next_identifier = 'NEW_MOON'
  54. return MOON_PHASES[next_identifier]
  55. class Position:
  56. def __init__(self, latitude: float, longitude: float):
  57. self.latitude = latitude
  58. self.longitude = longitude
  59. self.observation_planet = None
  60. self._topos = None
  61. def get_planet_topos(self) -> Topos:
  62. if self.observation_planet is None:
  63. raise TypeError('Observation planet must be set.')
  64. if self._topos is None:
  65. self._topos = self.observation_planet + Topos(latitude_degrees=self.latitude,
  66. longitude_degrees=self.longitude)
  67. return self._topos
  68. class AsterEphemerides:
  69. def __init__(self,
  70. rise_time: Union[Time, None],
  71. culmination_time: Union[Time, None],
  72. set_time: Union[Time, None]):
  73. self.rise_time = rise_time
  74. self.culmination_time = culmination_time
  75. self.set_time = set_time
  76. class Object(ABC):
  77. """
  78. An astronomical object.
  79. """
  80. def __init__(self,
  81. name: str,
  82. skyfield_name: str,
  83. ephemerides: AsterEphemerides or None = None):
  84. """
  85. Initialize an astronomical object
  86. :param str name: the official name of the object (may be internationalized)
  87. :param str skyfield_name: the internal name of the object in Skyfield library
  88. :param AsterEphemerides ephemerides: the ephemerides associated to the object
  89. """
  90. self.name = name
  91. self.skyfield_name = skyfield_name
  92. self.ephemerides = ephemerides
  93. @abstractmethod
  94. def get_type(self) -> str:
  95. pass
  96. class Star(Object):
  97. def get_type(self) -> str:
  98. return 'star'
  99. class Planet(Object):
  100. def get_type(self) -> str:
  101. return 'planet'
  102. class DwarfPlanet(Planet):
  103. def get_type(self) -> str:
  104. return 'dwarf_planet'
  105. class Satellite(Object):
  106. def get_type(self) -> str:
  107. return 'satellite'
  108. class Event:
  109. def __init__(self, event_type: str, objects: [Object], start_time: Time, end_time: Union[Time, None] = None):
  110. if event_type not in EVENTS.keys():
  111. raise ValueError('event_type parameter must be one of the following: %s (got %s)' % (
  112. ', '.join(EVENTS.keys()),
  113. event_type)
  114. )
  115. self.event_type = event_type
  116. self.objects = objects
  117. self.start_time = start_time
  118. self.end_time = end_time
  119. def get_description(self) -> str:
  120. return EVENTS[self.event_type]['message'] % self._get_objects_name()
  121. def _get_objects_name(self):
  122. if len(self.objects) == 1:
  123. return self.objects[0].name
  124. return tuple(object.name for object in self.objects)