25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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