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.
 
 
 
 

156 regels
6.2 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 datetime import date as date_type
  18. from skyfield.errors import EphemerisRangeError
  19. from skyfield.timelib import Time
  20. from skyfield.searchlib import find_discrete, find_maxima
  21. from numpy import pi
  22. from .data import Event, Star, Planet, ASTERS
  23. from .dateutil import translate_to_timezone
  24. from .exceptions import OutOfRangeDateError
  25. from .core import get_timescale, get_skf_objects, flatten_list
  26. def _search_conjunction(start_time: Time, end_time: Time, timezone: int) -> [Event]:
  27. earth = get_skf_objects()['earth']
  28. aster1 = None
  29. aster2 = None
  30. def is_in_conjunction(time: Time):
  31. earth_pos = earth.at(time)
  32. _, aster1_lon, _ = earth_pos.observe(aster1.get_skyfield_object()).apparent().ecliptic_latlon()
  33. _, aster2_lon, _ = earth_pos.observe(aster2.get_skyfield_object()).apparent().ecliptic_latlon()
  34. return ((aster1_lon.radians - aster2_lon.radians) / pi % 2.0).astype('int8') == 0
  35. is_in_conjunction.rough_period = 60.0
  36. computed = []
  37. conjunctions = []
  38. for aster1 in ASTERS:
  39. # Ignore the Sun
  40. if isinstance(aster1, Star):
  41. continue
  42. for aster2 in ASTERS:
  43. if isinstance(aster2, Star) or aster2 == aster1 or aster2 in computed:
  44. continue
  45. times, is_conjs = find_discrete(start_time, end_time, is_in_conjunction)
  46. for i, time in enumerate(times):
  47. if is_conjs[i]:
  48. aster1_pos = (aster1.get_skyfield_object() - earth).at(time)
  49. aster2_pos = (aster2.get_skyfield_object() - earth).at(time)
  50. distance = aster1_pos.separation_from(aster2_pos).degrees
  51. if distance - aster2.get_apparent_radius(time, earth) < aster1.get_apparent_radius(time, earth):
  52. occulting_aster = [aster1,
  53. aster2] if aster1_pos.distance().km < aster2_pos.distance().km else [aster2,
  54. aster1]
  55. conjunctions.append(Event('OCCULTATION', occulting_aster,
  56. translate_to_timezone(time.utc_datetime(), timezone)))
  57. else:
  58. conjunctions.append(Event('CONJUNCTION', [aster1, aster2],
  59. translate_to_timezone(time.utc_datetime(), timezone)))
  60. computed.append(aster1)
  61. return conjunctions
  62. def _search_oppositions(start_time: Time, end_time: Time, timezone: int) -> [Event]:
  63. earth = get_skf_objects()['earth']
  64. sun = get_skf_objects()['sun']
  65. aster = None
  66. def is_oppositing(time: Time) -> [bool]:
  67. earth_pos = earth.at(time)
  68. sun_pos = earth_pos.observe(sun).apparent() # Never do this without eyes protection!
  69. aster_pos = earth_pos.observe(get_skf_objects()[aster.skyfield_name]).apparent()
  70. _, lon1, _ = sun_pos.ecliptic_latlon()
  71. _, lon2, _ = aster_pos.ecliptic_latlon()
  72. return (lon1.degrees - lon2.degrees) > 180
  73. is_oppositing.rough_period = 1.0
  74. events = []
  75. for aster in ASTERS:
  76. if not isinstance(aster, Planet) or aster.skyfield_name in ['MERCURY', 'VENUS']:
  77. continue
  78. times, _ = find_discrete(start_time, end_time, is_oppositing)
  79. for time in times:
  80. events.append(Event('OPPOSITION', [aster], translate_to_timezone(time.utc_datetime(), timezone)))
  81. return events
  82. def _search_maximal_elongations(start_time: Time, end_time: Time, timezone: int) -> [Event]:
  83. earth = get_skf_objects()['earth']
  84. sun = get_skf_objects()['sun']
  85. aster = None
  86. def get_elongation(time: Time):
  87. sun_pos = (sun - earth).at(time)
  88. aster_pos = (aster.get_skyfield_object() - earth).at(time)
  89. separation = sun_pos.separation_from(aster_pos)
  90. return separation.degrees
  91. get_elongation.rough_period = 1.0
  92. events = []
  93. for aster in ASTERS:
  94. if aster.skyfield_name not in ['MERCURY', 'VENUS']:
  95. continue
  96. times, elongations = find_maxima(start_time, end_time, f=get_elongation, epsilon=1./24/3600, num=12)
  97. for i, time in enumerate(times):
  98. elongation = elongations[i]
  99. events.append(Event('MAXIMAL_ELONGATION', [aster], translate_to_timezone(time.utc_datetime(), timezone),
  100. details='{:.3n}°'.format(elongation)))
  101. return events
  102. def search_events(date: date_type, timezone: int = 0) -> [Event]:
  103. start_time = get_timescale().utc(date.year, date.month, date.day, -timezone)
  104. end_time = get_timescale().utc(date.year, date.month, date.day + 1, -timezone)
  105. try:
  106. return sorted(flatten_list([
  107. _search_oppositions(start_time, end_time, timezone),
  108. _search_conjunction(start_time, end_time, timezone),
  109. _search_maximal_elongations(start_time, end_time, timezone)
  110. ]), key=lambda event: event.start_time)
  111. except EphemerisRangeError as error:
  112. start_date = translate_to_timezone(error.start_time.utc_datetime(), timezone)
  113. end_date = translate_to_timezone(error.end_time.utc_datetime(), timezone)
  114. start_date = date_type(start_date.year, start_date.month, start_date.day)
  115. end_date = date_type(end_date.year, end_date.month, end_date.day)
  116. raise OutOfRangeDateError(start_date, end_date)