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.
 
 
 
 

107 lines
3.8 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
  24. class Dumper(ABC):
  25. def __init__(self, ephemeris: dict, date: datetime.date = datetime.date.today()):
  26. self.ephemeris = ephemeris
  27. self.date = date
  28. @abstractmethod
  29. def to_string(self):
  30. pass
  31. class JsonDumper(Dumper):
  32. def to_string(self):
  33. return json.dumps(self.ephemeris,
  34. default=self._json_default,
  35. indent=4)
  36. @staticmethod
  37. def _json_default(obj):
  38. # Fixes the "TypeError: Object of type int64 is not JSON serializable"
  39. # See https://stackoverflow.com/a/50577730
  40. if isinstance(obj, int64):
  41. return int(obj)
  42. if isinstance(obj, Time):
  43. return obj.utc_iso()
  44. if isinstance(obj, Object):
  45. obj = obj.__dict__
  46. obj.pop('skyfield_name')
  47. return obj
  48. if isinstance(obj, AsterEphemerides):
  49. return obj.__dict__
  50. if isinstance(obj, MoonPhase):
  51. moon_phase = obj.__dict__
  52. moon_phase['phase'] = moon_phase.pop('identifier')
  53. moon_phase['date'] = moon_phase.pop('time')
  54. return moon_phase
  55. raise TypeError('Object of type "%s" could not be integrated in the JSON' % str(type(obj)))
  56. class TextDumper(Dumper):
  57. def to_string(self):
  58. return '\n\n'.join(['Ephemerides of %s' % self.date.strftime('%A %B %d, %Y'),
  59. self.get_asters(self.ephemeris['details']),
  60. self.get_moon(self.ephemeris['moon_phase']),
  61. 'Note: All the hours are given in UTC.'])
  62. @staticmethod
  63. def get_asters(asters: [Object]) -> str:
  64. data = []
  65. for aster in asters:
  66. name = aster.name
  67. if aster.ephemerides.rise_time is not None:
  68. planet_rise = aster.ephemerides.rise_time.utc_strftime('%H:%M')
  69. else:
  70. planet_rise = '-'
  71. if aster.ephemerides.culmination_time is not None:
  72. planet_culmination = aster.ephemerides.culmination_time.utc_strftime('%H:%M')
  73. else:
  74. planet_culmination = '-'
  75. if aster.ephemerides.set_time is not None:
  76. planet_set = aster.ephemerides.set_time.utc_strftime('%H:%M')
  77. else:
  78. planet_set = '-'
  79. data.append([name, planet_rise, planet_culmination, planet_set])
  80. return tabulate(data, headers=['Planet', 'Rise time', 'Culmination time', 'Set time'], tablefmt='simple',
  81. stralign='center', colalign=('left',))
  82. @staticmethod
  83. def get_moon(moon_phase: MoonPhase) -> str:
  84. return 'Moon phase: %s\n' \
  85. '%s on %s' % (moon_phase.get_phase(),
  86. moon_phase.get_next_phase(),
  87. moon_phase.next_phase_date.utc_strftime('%a %b %-d, %Y %H:%M'))