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.
 
 
 
 

155 lines
4.9 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. }
  34. class MoonPhase:
  35. def __init__(self, identifier: str, time: Union[Time, None], next_phase_date: Union[Time, None]):
  36. if identifier not in MOON_PHASES.keys():
  37. raise ValueError('identifier parameter must be one of %s (got %s)' % (', '.join(MOON_PHASES.keys()),
  38. identifier))
  39. self.identifier = identifier
  40. self.time = time
  41. self.next_phase_date = next_phase_date
  42. def get_phase(self):
  43. return MOON_PHASES[self.identifier]
  44. def get_next_phase(self):
  45. if self.identifier == 'NEW_MOON' or self.identifier == 'WAXING_CRESCENT':
  46. next_identifier = 'FIRST_QUARTER'
  47. elif self.identifier == 'FIRST_QUARTER' or self.identifier == 'WAXING_GIBBOUS':
  48. next_identifier = 'FULL_MOON'
  49. elif self.identifier == 'FULL_MOON' or self.identifier == 'WANING_GIBBOUS':
  50. next_identifier = 'LAST_QUARTER'
  51. else:
  52. next_identifier = 'NEW_MOON'
  53. return MOON_PHASES[next_identifier]
  54. class Position:
  55. def __init__(self, latitude: float, longitude: float):
  56. self.latitude = latitude
  57. self.longitude = longitude
  58. self.observation_planet = None
  59. self._topos = None
  60. def get_planet_topos(self) -> Topos:
  61. if self.observation_planet is None:
  62. raise TypeError('Observation planet must be set.')
  63. if self._topos is None:
  64. self._topos = self.observation_planet + Topos(latitude_degrees=self.latitude,
  65. longitude_degrees=self.longitude)
  66. return self._topos
  67. class AsterEphemerides:
  68. def __init__(self,
  69. rise_time: Union[Time, None],
  70. culmination_time: Union[Time, None],
  71. set_time: Union[Time, None]):
  72. self.rise_time = rise_time
  73. self.culmination_time = culmination_time
  74. self.set_time = set_time
  75. class Object(ABC):
  76. """
  77. An astronomical object.
  78. """
  79. def __init__(self,
  80. name: str,
  81. skyfield_name: str,
  82. ephemerides: AsterEphemerides or None = None):
  83. """
  84. Initialize an astronomical object
  85. :param str name: the official name of the object (may be internationalized)
  86. :param str skyfield_name: the internal name of the object in Skyfield library
  87. :param AsterEphemerides ephemerides: the ephemerides associated to the object
  88. """
  89. self.name = name
  90. self.skyfield_name = skyfield_name
  91. self.ephemerides = ephemerides
  92. @abstractmethod
  93. def get_type(self) -> str:
  94. pass
  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:
  108. def __init__(self, event_type: str, aster: [Object], start_time: Time, end_time: Union[Time, None] = None):
  109. if event_type not in EVENTS.keys():
  110. raise ValueError('event_type parameter must be one of the following: %s (got %s)' % (
  111. ', '.join(EVENTS.keys()),
  112. event_type)
  113. )
  114. self.event_type = event_type
  115. self.object = aster
  116. self.start_time = start_time
  117. self.end_time = end_time
  118. def get_description(self) -> str:
  119. return EVENTS[self.event_type]['message'] % self.object.name