Skip to content

Instantly share code, notes, and snippets.

@brunobord
Last active February 19, 2021 10:49
Show Gist options
  • Save brunobord/39be649898cfb34d57f94f483e0956cc to your computer and use it in GitHub Desktop.
Save brunobord/39be649898cfb34d57f94f483e0956cc 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:
# With skyfield-data
from skyfield_data import get_skyfield_data_path
except ImportError:
# Without skyfield-data
from skyfield.api import load
else:
from skyfield.api import Loader
load = Loader(get_skyfield_data_path())
finally:
t = load.timescale().now()
planets = load('de421.bsp')
v = planets['mars'].at(t) - planets['earth'].at(t)
km = v.distance().km
ls = v.distance().light_seconds()
lmin = ls / 60.
print(f'Mars is {ls} light-seconds away ({lmin} min -- {km} km)')
@brunobord
Copy link
Author

According to the table visible here, the Mars-Earth distance will be of 55.957 millions of km (0.374041 au) on August 15th, 2050.

There are several ways to compute the time the light will take to travel, but with Skyfield, it's as easy as this:

>>> from skyfield.units import Distance
>>> Distance(km=55.957 * 1000000).light_seconds()
186.65246075002995

So, Mars will be at about 3 mn (and 6s) from the Earth, for waves that travel at the speed of light.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment