Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

141 lignes
4.9 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 abc import ABC, abstractmethod
  18. import datetime
  19. import json
  20. from tabulate import tabulate
  21. from skyfield.timelib import Time
  22. from numpy import int64
  23. from .data import Object, AsterEphemerides, MoonPhase, Event
  24. class Dumper(ABC):
  25. def __init__(self, ephemeris: dict, events: [Event], date: datetime.date = datetime.date.today()):
  26. self.ephemeris = ephemeris
  27. self.events = events
  28. self.date = date
  29. @abstractmethod
  30. def to_string(self):
  31. pass
  32. class JsonDumper(Dumper):
  33. def to_string(self):
  34. self.ephemeris['events'] = self.events
  35. self.ephemeris['ephemerides'] = self.ephemeris.pop('details')
  36. return json.dumps(self.ephemeris,
  37. default=self._json_default,
  38. indent=4)
  39. @staticmethod
  40. def _json_default(obj):
  41. # Fixes the "TypeError: Object of type int64 is not JSON serializable"
  42. # See https://stackoverflow.com/a/50577730
  43. if isinstance(obj, int64):
  44. return int(obj)
  45. if isinstance(obj, Time):
  46. return obj.utc_iso()
  47. if isinstance(obj, Object):
  48. obj = obj.__dict__
  49. obj.pop('skyfield_name')
  50. obj['object'] = obj.pop('name')
  51. obj['details'] = obj.pop('ephemerides')
  52. return obj
  53. if isinstance(obj, AsterEphemerides):
  54. return obj.__dict__
  55. if isinstance(obj, MoonPhase):
  56. moon_phase = obj.__dict__
  57. moon_phase['phase'] = moon_phase.pop('identifier')
  58. moon_phase['date'] = moon_phase.pop('time')
  59. return moon_phase
  60. if isinstance(obj, Event):
  61. event = obj.__dict__
  62. event['object'] = event['object'].name
  63. return event
  64. raise TypeError('Object of type "%s" could not be integrated in the JSON' % str(type(obj)))
  65. class TextDumper(Dumper):
  66. def to_string(self):
  67. text = self.date.strftime('%A %B %d, %Y')
  68. if len(self.ephemeris['details']) > 0:
  69. text = '\n\n'.join([text,
  70. self.get_asters(self.ephemeris['details'])
  71. ])
  72. text = '\n\n'.join([text,
  73. self.get_moon(self.ephemeris['moon_phase'])
  74. ])
  75. if len(self.events) > 0:
  76. text = '\n\n'.join([text,
  77. 'Expected events:',
  78. self.get_events(self.events)
  79. ])
  80. text = '\n\n'.join([text, 'Note: All the hours are given in UTC.'])
  81. return text
  82. @staticmethod
  83. def get_asters(asters: [Object]) -> str:
  84. data = []
  85. for aster in asters:
  86. name = aster.name
  87. if aster.ephemerides.rise_time is not None:
  88. planet_rise = aster.ephemerides.rise_time.utc_strftime('%H:%M')
  89. else:
  90. planet_rise = '-'
  91. if aster.ephemerides.culmination_time is not None:
  92. planet_culmination = aster.ephemerides.culmination_time.utc_strftime('%H:%M')
  93. else:
  94. planet_culmination = '-'
  95. if aster.ephemerides.set_time is not None:
  96. planet_set = aster.ephemerides.set_time.utc_strftime('%H:%M')
  97. else:
  98. planet_set = '-'
  99. data.append([name, planet_rise, planet_culmination, planet_set])
  100. return tabulate(data, headers=['Object', 'Rise time', 'Culmination time', 'Set time'], tablefmt='simple',
  101. stralign='center', colalign=('left',))
  102. @staticmethod
  103. def get_events(events: [Event]) -> str:
  104. data = []
  105. for event in events:
  106. data.append([event.start_time.utc_strftime('%H:%M'), event.get_description()])
  107. return tabulate(data, tablefmt='plain', stralign='left')
  108. @staticmethod
  109. def get_moon(moon_phase: MoonPhase) -> str:
  110. return 'Moon phase: %s\n' \
  111. '%s on %s' % (moon_phase.get_phase(),
  112. moon_phase.get_next_phase(),
  113. moon_phase.next_phase_date.utc_strftime('%a %b %-d, %Y %H:%M'))