選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 

105 行
4.3 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.searchlib import find_discrete, find_maxima
  19. from skyfield.timelib import Time
  20. from skyfield.constants import tau
  21. from .data import Position, AsterEphemerides, MoonPhase, Object, ASTERS, skyfield_to_moon_phase
  22. from .dateutil import translate_to_timezone
  23. from .core import get_skf_objects, get_timescale, get_iau2000b
  24. RISEN_ANGLE = -0.8333
  25. def get_moon_phase(compute_date: datetime.date) -> MoonPhase:
  26. earth = get_skf_objects()['earth']
  27. moon = get_skf_objects()['moon']
  28. sun = get_skf_objects()['sun']
  29. def moon_phase_at(time: Time):
  30. time._nutation_angles = get_iau2000b(time)
  31. current_earth = earth.at(time)
  32. _, mlon, _ = current_earth.observe(moon).apparent().ecliptic_latlon('date')
  33. _, slon, _ = current_earth.observe(sun).apparent().ecliptic_latlon('date')
  34. return (((mlon.radians - slon.radians) // (tau / 8)) % 8).astype(int)
  35. moon_phase_at.rough_period = 7.0 # one lunar phase per week
  36. today = get_timescale().utc(compute_date.year, compute_date.month, compute_date.day)
  37. time1 = get_timescale().utc(compute_date.year, compute_date.month, compute_date.day - 10)
  38. time2 = get_timescale().utc(compute_date.year, compute_date.month, compute_date.day + 10)
  39. times, phase = find_discrete(time1, time2, moon_phase_at)
  40. return skyfield_to_moon_phase(times, phase, today)
  41. def get_ephemerides(date: datetime.date, position: Position, timezone: int = 0) -> [AsterEphemerides]:
  42. ephemerides = []
  43. def get_angle(for_aster: Object):
  44. def fun(time: Time) -> float:
  45. return position.get_planet_topos().at(time).observe(for_aster.get_skyfield_object()).apparent().altaz()[0]\
  46. .degrees
  47. fun.rough_period = 1.0
  48. return fun
  49. def is_risen(for_aster: Object):
  50. def fun(time: Time) -> bool:
  51. return get_angle(for_aster)(time) > RISEN_ANGLE
  52. fun.rough_period = 0.5
  53. return fun
  54. start_time = get_timescale().utc(date.year, date.month, date.day, -timezone)
  55. end_time = get_timescale().utc(date.year, date.month, date.day, 23 - timezone, 59, 59)
  56. for aster in ASTERS:
  57. rise_times, arr = find_discrete(start_time, end_time, is_risen(aster))
  58. try:
  59. culmination_time, _ = find_maxima(start_time, end_time, f=get_angle(aster), epsilon=1./3600/24, num=12)
  60. culmination_time = culmination_time[0] if len(culmination_time) > 0 else None
  61. except ValueError:
  62. culmination_time = None
  63. if len(rise_times) == 2:
  64. rise_time = rise_times[0 if arr[0] else 1]
  65. set_time = rise_times[1 if not arr[1] else 0]
  66. else:
  67. rise_time = rise_times[0] if arr[0] else None
  68. set_time = rise_times[0] if not arr[0] else None
  69. # Convert the Time instances to Python datetime objects
  70. if rise_time is not None:
  71. rise_time = translate_to_timezone(rise_time.utc_datetime().replace(microsecond=0),
  72. to_tz=timezone)
  73. if culmination_time is not None:
  74. culmination_time = translate_to_timezone(culmination_time.utc_datetime().replace(microsecond=0),
  75. to_tz=timezone)
  76. if set_time is not None:
  77. set_time = translate_to_timezone(set_time.utc_datetime().replace(microsecond=0),
  78. to_tz=timezone)
  79. ephemerides.append(AsterEphemerides(rise_time, culmination_time, set_time, aster=aster))
  80. return ephemerides