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.
 
 
 
 

150 lines
5.6 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. import datetime
  18. from skyfield import almanac
  19. from skyfield.timelib import Time
  20. from .data import Object, Position, AsterEphemerides
  21. from .core import get_skf_objects, get_timescale, ASTERS, MONTHS
  22. RISEN_ANGLE = -0.8333
  23. class EphemeridesComputer:
  24. def __init__(self, position: Position):
  25. position.observation_planet = get_skf_objects()['earth']
  26. self.position = position
  27. def get_sun(self, start_time, end_time) -> dict:
  28. times, is_risen = almanac.find_discrete(start_time,
  29. end_time,
  30. almanac.sunrise_sunset(get_skf_objects(), self.position))
  31. sunrise = times[0] if is_risen[0] else times[1]
  32. sunset = times[1] if not is_risen[1] else times[0]
  33. return {'rise': sunrise, 'set': sunset}
  34. @staticmethod
  35. def get_moon(year, month, day) -> dict:
  36. time1 = get_timescale().utc(year, month, day - 10)
  37. time2 = get_timescale().utc(year, month, day)
  38. _, moon_phase = almanac.find_discrete(time1, time2, almanac.moon_phases(get_skf_objects()))
  39. return {'phase': moon_phase[-1]}
  40. @staticmethod
  41. def get_asters_ephemerides_for_aster(aster, date: datetime.date, position: Position) -> Object:
  42. skyfield_aster = get_skf_objects()[aster.skyfield_name]
  43. def get_angle(time: Time) -> float:
  44. return position.get_planet_topos().at(time).observe(skyfield_aster).apparent().altaz()[0].degrees
  45. def is_risen(time: Time) -> bool:
  46. return get_angle(time) > RISEN_ANGLE
  47. get_angle.rough_period = 1.0
  48. is_risen.rough_period = 0.5
  49. start_time = get_timescale().utc(date.year, date.month, date.day)
  50. end_time = get_timescale().utc(date.year, date.month, date.day, 23, 59, 59)
  51. rise_times, arr = almanac.find_discrete(start_time, end_time, is_risen)
  52. try:
  53. culmination_time, _ = almanac._find_maxima(start_time, end_time, get_angle, epsilon=1./3600/24)
  54. except ValueError:
  55. culmination_time = None
  56. if len(rise_times) == 2:
  57. rise_time = rise_times[0 if arr[0] else 1]
  58. set_time = rise_times[0 if not arr[1] else 0]
  59. else:
  60. rise_time = rise_times[0] if arr[0] else None
  61. set_time = rise_times[0] if not arr[0] else None
  62. culmination_time = culmination_time[0] if culmination_time is not None else None
  63. aster.ephemerides = AsterEphemerides(rise_time, culmination_time, set_time)
  64. return aster
  65. @staticmethod
  66. def is_leap_year(year: int) -> bool:
  67. return (year % 4 == 0 and year % 100 > 0) or (year % 400 == 0)
  68. def compute_ephemerides_for_day(self, year: int, month: int, day: int) -> dict:
  69. return {'moon_phase': self.get_moon(year, month, day),
  70. 'planets': [self.get_asters_ephemerides_for_aster(aster, datetime.date(year, month, day), self.position)
  71. for aster in ASTERS]}
  72. def compute_ephemerides_for_month(self, year: int, month: int) -> [dict]:
  73. if month == 2:
  74. max_day = 29 if self.is_leap_year(year) else 28
  75. elif month < 8:
  76. max_day = 30 if month % 2 == 0 else 31
  77. else:
  78. max_day = 31 if month % 2 == 0 else 30
  79. ephemerides = []
  80. for day in range(1, max_day + 1):
  81. ephemerides.append(self.compute_ephemerides_for_day(year, month, day))
  82. return ephemerides
  83. def compute_ephemerides_for_year(self, year: int) -> [dict]:
  84. ephemerides = {'seasons': self.get_seasons(year)}
  85. for month in range(0, 12):
  86. ephemerides[MONTHS[month]] = self.compute_ephemerides_for_month(year, month + 1)
  87. return ephemerides
  88. @staticmethod
  89. def get_seasons(year: int) -> dict:
  90. start_time = get_timescale().utc(year, 1, 1)
  91. end_time = get_timescale().utc(year, 12, 31)
  92. times, almanac_seasons = almanac.find_discrete(start_time, end_time, almanac.seasons(get_skf_objects()))
  93. seasons = {}
  94. for time, almanac_season in zip(times, almanac_seasons):
  95. if almanac_season == 0:
  96. season = 'MARCH'
  97. elif almanac_season == 1:
  98. season = 'JUNE'
  99. elif almanac_season == 2:
  100. season = 'SEPTEMBER'
  101. elif almanac_season == 3:
  102. season = 'DECEMBER'
  103. else:
  104. raise AssertionError
  105. seasons[season] = time.utc_iso()
  106. return seasons
  107. def compute_ephemerides(self, year: int, month: int, day: int):
  108. if day is not None:
  109. return self.compute_ephemerides_for_day(year, month, day)
  110. if month is not None:
  111. return self.compute_ephemerides_for_month(year, month)
  112. return self.compute_ephemerides_for_year(year)