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.
 
 
 
 

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