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.
 
 
 
 

89 lines
2.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 os
  18. import re
  19. from shutil import rmtree
  20. from pathlib import Path
  21. from skyfield.api import Loader
  22. from skyfield.timelib import Time
  23. from skyfield.nutationlib import iau2000b
  24. CACHE_FOLDER = str(Path.home()) + '/.kosmorro-cache'
  25. class Environment:
  26. def __init__(self):
  27. self._vars = {}
  28. def __set__(self, key, value):
  29. self._vars[key] = value
  30. def __getattr__(self, key):
  31. return self._vars[key] if key in self._vars else None
  32. def __str__(self):
  33. return self._vars.__str__()
  34. def __len__(self):
  35. return len(self._vars)
  36. def get_env() -> Environment:
  37. environment = Environment()
  38. for var in os.environ:
  39. if not re.search('^KOSMORRO_', var):
  40. continue
  41. [_, env] = var.split('_', 1)
  42. environment.__set__(env.lower(), os.getenv(var))
  43. return environment
  44. def get_loader():
  45. return Loader(CACHE_FOLDER)
  46. def get_timescale():
  47. return get_loader().timescale()
  48. def get_skf_objects():
  49. return get_loader()('de421.bsp')
  50. def get_iau2000b(time: Time):
  51. return iau2000b(time.tt)
  52. def clear_cache():
  53. rmtree(CACHE_FOLDER)
  54. def flatten_list(the_list: list):
  55. new_list = []
  56. for item in the_list:
  57. if isinstance(item, list):
  58. for item2 in flatten_list(item):
  59. new_list.append(item2)
  60. continue
  61. new_list.append(item)
  62. return new_list