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.
 
 
 
 

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