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.
 
 
 
 

55 lines
2.2 KiB

  1. # Kosmorro - Compute The Next Ephemeris
  2. # Copyright (C) 2019 Jérôme Deuchnord <jerome@deuchnord.fr>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from abc import ABC, abstractmethod
  17. from tabulate import tabulate
  18. from skyfield import almanac
  19. class Dumper(ABC):
  20. def __init__(self, ephemeris):
  21. self.ephemeris = ephemeris
  22. @abstractmethod
  23. def to_string(self):
  24. pass
  25. class TextDumper(Dumper):
  26. def to_string(self):
  27. return '\n\n'.join([self.get_planets(self.ephemeris['planets'], self.ephemeris['sun']),
  28. self.get_moon(self.ephemeris['moon'])])
  29. @staticmethod
  30. def get_planets(planets, sun):
  31. data = [['SUN', sun['rise'].utc_strftime('%H:%M'), '-', sun['set'].utc_strftime('%H:%M')]]
  32. for planet in planets:
  33. name = planet
  34. planet_data = planets[planet]
  35. planet_rise = planet_data['rise'].utc_strftime('%H:%M') if planet_data['rise'] is not None else ' -'
  36. planet_maximum = planet_data['maximum'].utc_strftime('%H:%M') if planet_data['maximum'] is not None\
  37. else ' -'
  38. planet_set = planet_data['set'].utc_strftime('%H:%M') if planet_data['set'] is not None else ' -'
  39. data.append([name, planet_rise, planet_maximum, planet_set])
  40. return tabulate(data, headers=['Planet', 'Rise time', 'Maximum time', 'Set time'], tablefmt='simple',
  41. stralign='center', colalign=('left',))
  42. @staticmethod
  43. def get_moon(moon):
  44. return 'Moon phase: %s' % almanac.MOON_PHASES[moon['phase']]