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.

events.py 3.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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.almanac import find_discrete
  20. from .data import Event, Planet
  21. from .core import get_timescale, get_skf_objects, ASTERS, 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))
  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))
  67. return events
  68. def search_events(date: date_type) -> [Event]:
  69. start_time = get_timescale().utc(date.year, date.month, date.day)
  70. end_time = get_timescale().utc(date.year, date.month, date.day + 1)
  71. return sorted(flatten_list([
  72. _search_oppositions(start_time, end_time),
  73. _search_conjunction(start_time, end_time)
  74. ]), key=lambda event: event.start_time.utc_datetime())