Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

133 rindas
4.6 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 = 'Ephemerides of %s' % self.date.strftime('%A %B %d, %Y')
  65. text = '\n\n'.join([text,
  66. self.get_asters(self.ephemeris['details']),
  67. self.get_moon(self.ephemeris['moon_phase'])
  68. ])
  69. if len(self.events) > 0:
  70. text = '\n\n'.join([text,
  71. 'Expected events:',
  72. self.get_events(self.events)
  73. ])
  74. text = '\n\n'.join([text, 'Note: All the hours are given in UTC.'])
  75. return text
  76. @staticmethod
  77. def get_asters(asters: [Object]) -> str:
  78. data = []
  79. for aster in asters:
  80. name = aster.name
  81. if aster.ephemerides.rise_time is not None:
  82. planet_rise = aster.ephemerides.rise_time.utc_strftime('%H:%M')
  83. else:
  84. planet_rise = '-'
  85. if aster.ephemerides.culmination_time is not None:
  86. planet_culmination = aster.ephemerides.culmination_time.utc_strftime('%H:%M')
  87. else:
  88. planet_culmination = '-'
  89. if aster.ephemerides.set_time is not None:
  90. planet_set = aster.ephemerides.set_time.utc_strftime('%H:%M')
  91. else:
  92. planet_set = '-'
  93. data.append([name, planet_rise, planet_culmination, planet_set])
  94. return tabulate(data, headers=['Object', 'Rise time', 'Culmination time', 'Set time'], tablefmt='simple',
  95. stralign='center', colalign=('left',))
  96. @staticmethod
  97. def get_events(events: [Event]) -> str:
  98. data = []
  99. for event in events:
  100. data.append([event.start_time.utc_strftime('%H:%M'), event.get_description()])
  101. return tabulate(data, tablefmt='plain', stralign='left')
  102. @staticmethod
  103. def get_moon(moon_phase: MoonPhase) -> str:
  104. return 'Moon phase: %s\n' \
  105. '%s on %s' % (moon_phase.get_phase(),
  106. moon_phase.get_next_phase(),
  107. moon_phase.next_phase_date.utc_strftime('%a %b %-d, %Y %H:%M'))