Skip to content

Instantly share code, notes, and snippets.

@wo0dyn
Forked from brunobord/earth_mars.py
Last active February 19, 2021 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wo0dyn/eb653342f0bc8ac1ed651e7c45e3082b to your computer and use it in GitHub Desktop.
Save wo0dyn/eb653342f0bc8ac1ed651e7c45e3082b to your computer and use it in GitHub Desktop.
What's the distance (in light-seconds and light-minutes) from Earth to Mars?
"""
Compute and display the Earth-Mars distance in light-seconds, light-minutes
and kilometers.
The easiest way is to install both ``skyfield`` & ``skyfield-data`` to compute
this:
$ pip install skyfield skyfield-data
You can also install `skyfield` only, but it'll force you to download the
`de421.bsp` file (at relatively slow speed, as far as I tested it, maybe it's
because the install of ``skyfield-data`` is compressed in the .whl file).
Fortunately, this file download is made only once, since the file is in your
current directory.
"""
try:
from skyfield_data import get_skyfield_data_path
except ImportError:
from skyfield.api import load
else:
from skyfield.api import Loader
load = Loader(get_skyfield_data_path())
finally:
time = load.timescale().now()
planets = load('de421.bsp')
v = planets['mars'].at(time) - planets['earth'].at(time)
km = v.distance().km
ls = v.distance().light_seconds()
lmin = ls / 60.
print(f'Mars is {ls} light-seconds away ({lmin} min -- {km} km)')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment