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.
 
 
 
 

98 lines
3.0 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 shutil import rmtree
  18. from pathlib import Path
  19. from typing import Union
  20. from skyfield.api import Loader
  21. from skyfield.timelib import Time
  22. from skyfield.nutationlib import iau2000b
  23. from .data import Star, Planet, Satellite, MOON_PHASES, MoonPhase
  24. CACHE_FOLDER = str(Path.home()) + '/.kosmorro-cache'
  25. MONTHS = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
  26. ASTERS = [Star('Sun', 'SUN'),
  27. Satellite('Moon', 'MOON'),
  28. Planet('Mercury', 'MERCURY'),
  29. Planet('Venus', 'VENUS'),
  30. Planet('Mars', 'MARS'),
  31. Planet('Jupiter', 'JUPITER BARYCENTER'),
  32. Planet('Saturn', 'SATURN BARYCENTER'),
  33. Planet('Uranus', 'URANUS BARYCENTER'),
  34. Planet('Neptune', 'NEPTUNE BARYCENTER'),
  35. Planet('Pluto', 'PLUTO BARYCENTER')]
  36. def get_loader():
  37. return Loader(CACHE_FOLDER)
  38. def get_timescale():
  39. return get_loader().timescale()
  40. def get_skf_objects():
  41. return get_loader()('de421.bsp')
  42. def get_iau2000b(time: Time):
  43. return iau2000b(time.tt)
  44. def clear_cache():
  45. rmtree(CACHE_FOLDER)
  46. def skyfield_to_moon_phase(times: [Time], vals: [int], now: Time) -> Union[MoonPhase, None]:
  47. tomorrow = get_timescale().utc(now.utc_datetime().year, now.utc_datetime().month, now.utc_datetime().day + 1)
  48. phases = list(MOON_PHASES.keys())
  49. current_phase = None
  50. current_phase_time = None
  51. next_phase_time = None
  52. i = 0
  53. if len(times) == 0:
  54. return None
  55. for i, time in enumerate(times):
  56. if now.utc_iso() <= time.utc_iso():
  57. if vals[i] in [0, 2, 4, 6]:
  58. if time.utc_datetime() < tomorrow.utc_datetime():
  59. current_phase_time = time
  60. current_phase = phases[vals[i]]
  61. else:
  62. i -= 1
  63. current_phase_time = None
  64. current_phase = phases[vals[i]]
  65. else:
  66. current_phase = phases[vals[i]]
  67. break
  68. for j in range(i + 1, len(times)):
  69. if vals[j] in [0, 2, 4, 6]:
  70. next_phase_time = times[j]
  71. break
  72. return MoonPhase(current_phase, current_phase_time, next_phase_time)