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.
 
 
 
 

106 lignes
4.2 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. import argparse
  18. import sys
  19. from datetime import date
  20. from kosmorrolib import dumper
  21. from kosmorrolib import core
  22. from kosmorrolib.ephemerides import EphemeridesComputer, Position
  23. def main():
  24. output_formats = get_dumpers()
  25. args = get_args(list(output_formats.keys()))
  26. if args.special_action is not None:
  27. return 0 if args.special_action() else 1
  28. year = args.year
  29. month = args.month
  30. day = args.day
  31. if day is not None and month is None:
  32. month = date.today().month
  33. ephemeris = EphemeridesComputer(Position(args.latitude, args.longitude, altitude=args.altitude))
  34. ephemerides = ephemeris.compute_ephemerides(year, month, day)
  35. dump = output_formats[args.format](ephemerides)
  36. print(dump.to_string())
  37. return 0
  38. def get_dumpers() -> {str: dumper.Dumper}:
  39. return {
  40. 'text': dumper.TextDumper,
  41. 'json': dumper.JsonDumper
  42. }
  43. def clear_cache() -> bool:
  44. confirm = input("Do you really want to clear Kosmorro's cache? [yN] ").upper()
  45. if confirm == 'Y':
  46. try:
  47. core.clear_cache()
  48. except FileNotFoundError:
  49. pass
  50. elif confirm not in ('N', ''):
  51. print('Answer did not match expected options, cache not cleared.')
  52. return False
  53. return True
  54. def get_args(output_formats: [str]):
  55. today = date.today()
  56. parser = argparse.ArgumentParser(description='Compute the ephemerides for a given date, at a given position'
  57. ' on Earth.',
  58. epilog='By default, the ephemerides will be computed for today (%s) for an'
  59. ' observer positioned at coordinates (0,0), with an altitude of 0.'
  60. % today.strftime('%a %b %d, %Y'))
  61. parser.add_argument('--clear-cache', dest='special_action', action='store_const', const=clear_cache, default=None,
  62. help='Delete all the files Kosmorro stored in the cache.')
  63. parser.add_argument('--format', '-f', type=str, default=output_formats[0], choices=output_formats,
  64. help='The format under which the information have to be output')
  65. parser.add_argument('--latitude', '-lat', type=float, default=0.,
  66. help="The observer's latitude on Earth")
  67. parser.add_argument('--longitude', '-lon', type=float, default=0.,
  68. help="The observer's longitude on Earth")
  69. parser.add_argument('--altitude', '-alt', type=float, default=0.,
  70. help="The observer's altitude on Earth")
  71. parser.add_argument('--day', '-d', type=int, default=today.day,
  72. help='A number between 1 and 28, 29, 30 or 31 (depending on the month). The day you want to '
  73. ' compute the ephemerides for. Defaults to %d (the current day).' % today.day)
  74. parser.add_argument('--month', '-m', type=int, default=today.month,
  75. help='A number between 1 and 12. The month you want to compute the ephemerides for. Defaults to'
  76. ' %d (the current month).' % today.month)
  77. parser.add_argument('--year', '-y', type=int, default=today.year,
  78. help='The year you want to compute the ephemerides for.'
  79. ' Defaults to %d (the current year).' % today.year)
  80. return parser.parse_args()
  81. if __name__ == '__main__':
  82. sys.exit(main())