您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

132 行
4.5 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 .data import Event, Planet, ASTERS
  21. from .core import get_timescale, get_skf_objects, flatten_list
  22. def _search_conjunction(start_time: Time, end_time: Time) -> [Event]:
  23. earth = get_skf_objects()['earth']
  24. aster1 = None
  25. aster2 = None
  26. def is_in_conjunction(time: Time):
  27. earth_pos = earth.at(time)
  28. aster1_pos = earth_pos.observe(get_skf_objects()[aster1.skyfield_name]).apparent()
  29. aster2_pos = earth_pos.observe(get_skf_objects()[aster2.skyfield_name]).apparent()
  30. aster_1_right_ascension, _, _ = aster1_pos.radec()
  31. aster_2_right_ascension, _, _ = aster2_pos.radec()
  32. return aster_1_right_ascension.hours - aster_2_right_ascension.hours < 0
  33. is_in_conjunction.rough_period = 1.0
  34. computed = []
  35. conjunctions = []
  36. for aster1 in ASTERS:
  37. # Ignore the Sun
  38. if not isinstance(aster1, Planet):
  39. continue
  40. for aster2 in ASTERS:
  41. if not isinstance(aster2, Planet) or aster2 == aster1 or aster2 in computed:
  42. continue
  43. times, _ = find_discrete(start_time, end_time, is_in_conjunction)
  44. for time in times:
  45. conjunctions.append(Event('CONJUNCTION', [aster1, aster2], time.utc_datetime()))
  46. computed.append(aster1)
  47. return conjunctions
  48. def _search_oppositions(start_time: Time, end_time: Time) -> [Event]:
  49. earth = get_skf_objects()['earth']
  50. sun = get_skf_objects()['sun']
  51. aster = None
  52. def is_oppositing(time: Time) -> [bool]:
  53. earth_pos = earth.at(time)
  54. sun_pos = earth_pos.observe(sun).apparent() # Never do this without eyes protection!
  55. aster_pos = earth_pos.observe(get_skf_objects()[aster.skyfield_name]).apparent()
  56. _, lon1, _ = sun_pos.ecliptic_latlon()
  57. _, lon2, _ = aster_pos.ecliptic_latlon()
  58. return (lon1.degrees - lon2.degrees) > 180
  59. is_oppositing.rough_period = 1.0
  60. events = []
  61. for aster in ASTERS:
  62. if not isinstance(aster, Planet) or aster.name in ['Mercury', 'Venus']:
  63. continue
  64. times, _ = find_discrete(start_time, end_time, is_oppositing)
  65. for time in times:
  66. events.append(Event('OPPOSITION', [aster], time.utc_datetime()))
  67. return events
  68. def _search_maximal_elongations(start_time: Time, end_time: Time) -> [Event]:
  69. earth = get_skf_objects()['earth']
  70. sun = get_skf_objects()['sun']
  71. aster = None
  72. def get_elongation(time: Time):
  73. sun_pos = (sun - earth).at(time)
  74. aster_pos = (aster.get_skyfield_object() - earth).at(time)
  75. separation = sun_pos.separation_from(aster_pos)
  76. return separation.degrees
  77. get_elongation.rough_period = 1.0
  78. events = []
  79. for aster in ASTERS:
  80. if aster.skyfield_name not in ['MERCURY', 'VENUS']:
  81. continue
  82. times, elongations = find_maxima(start_time, end_time, f=get_elongation, epsilon=1./24/3600, num=12)
  83. for i, time in enumerate(times):
  84. elongation = elongations[i]
  85. events.append(Event('MAXIMAL_ELONGATION', [aster], time.utc_datetime(),
  86. details='{:.3n}°'.format(elongation)))
  87. return events
  88. def search_events(date: date_type) -> [Event]:
  89. start_time = get_timescale().utc(date.year, date.month, date.day)
  90. end_time = get_timescale().utc(date.year, date.month, date.day + 1)
  91. return sorted(flatten_list([
  92. _search_oppositions(start_time, end_time),
  93. _search_conjunction(start_time, end_time),
  94. _search_maximal_elongations(start_time, end_time)
  95. ]), key=lambda event: event.start_time)