<p>There is no simple function that is accurate. Both Earth and Mars are orbiting in ellipses (Kepler's first law). They are travelling at varying speeds (Kepler's second). To find the current distance between them you have to calculate their positions in three dimensions. </p>

<p>In other words you have to do orbital kinematics. <a href="https://astronomy.stackexchange.com/questions/8012/how-do-i-calculate-the-positions-of-objects-in-orbit">How do I calculate the positions of objects in orbit?</a></p>

<p>If accuracy is not important. You can approximate the orbits with circles in the same plane. The Earths position is then
$$(\cos(2\pi t + \alpha), \sin(2\pi t +\alpha))$$ 
and Mars is 
$$(1.5\cos(2\pi t/1.882 +\beta), 1.5\sin(2\pi t/1.882 + \beta))$$</p>

<p>The distance between them is calculated by pythagoras</p>

<p>The distances are in AU and the time is in years (converting to lightseconds is then easy). $\alpha$ and $\beta$ are constants chosen to fit the positions of the planets at time 0. The values 1.5 and 1.882 are chosen because Mars's orbit is about 1.5 times bigger than Earth, and a Mars year is 1.882 times longer than Earth. </p>

<p>The circular approximation however is poor for Mars, and this ignores the fact that Mars isn't in exactly the same orbital plane as the earth, so the values will only be rather approximate. </p>

CC BY-SA 3.0
2016-08-16T17:22:11.697Z
Thanks! Two question, when is (what is the date of) $t = 0$ and what it the unit of $t$? What is the maximal error percentage? Thanks in advance.
Kevin
CC BY-SA 3.0
2016-08-15T21:50:45.470Z
2016-08-15T21:50:45.470Z
2017-04-13T12:59:06.910Z
James K
<p><strong>note:</strong> This is an "extended comment" type answer, addressing two potential solutions that may not be exactly what you need, but are close.</p>

<p>A thing which gives you positions of things in the solar system given a specified time is called an <a href="https://en.wikipedia.org/wiki/Ephemeris" rel="nofollow noreferrer">Ephemeris</a>.</p>

<p>Originally they were tables of predictions (past and future) based on calculations. By the time of Newton, they became quite good, since Newton developed a method to iteratively solve Kepler's equation (e.g. <em>Newton's Method</em>). See the question <a href="https://hsm.stackexchange.com/q/3575/3869">How did Newton and Kepler (actually) do it?</a> and the answers there for a little more background.</p>

<ol>
<li><p>The Python package <a href="http://rhodesmill.org/pyephem/" rel="nofollow noreferrer">PyEphem</a> has been around and well supported, and is the pythonic reincarnation of <a href="http://www.clearskyinstitute.com/xephem/" rel="nofollow noreferrer">XEphem</a>. I haven't used it, but I believe it keeps enough information about orbital parameters at certain epochs to generate an ephemeris, including some gravitational perturbations. In other words, it's much more than planets moving on fixed elliptical orbits around a fixed sun. So I believe it runs without internet connection.</p></li>
<li><p>I never used it because I was recommended to look at <a href="http://rhodesmill.org/skyfield/" rel="nofollow noreferrer">Skyfield</a> and it's exactly what I needed. It downloads a standard JPL ephemeris that you choose, and then just uses it from your hard drive after that. However, in order to deal with leap seconds and other <em>time related</em> effects, it <em>occasionally</em> needs to check the internet for leap second information updates, since these are arbitrary.</p></li>
</ol>

<p>I don't know if Skyfield has a mode to avoid that. Actually that's a good question. If you work with a timescale that doesn't have leap seconds, I am not sure if it will run in its current version.</p>

<p>Both Skyfield and PyEphem Python packages have been written and are maintained by @BrandonRhodes.</p>

<p>If you can allow an occasional connection to the internet (say every month or few months) then Skyfield is <em>extremely easy and pythonic</em> to use. For example here is a script I used <a href="https://space.stackexchange.com/a/16986/12102">in this answer</a>. If you want to convert time formats from system time, you can search my questions in stackoverflow and space exploration. If you want it for your local computers system time now, you just use <code>t = ts.now()</code> directly instead of utc.</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

from skyfield.api import load

data   = load('de421.bsp')
ts     = load.timescale()
t      = ts.utc(2016, 7, 5, 9, 50, 0)

jupiter, earth  = data['Jupiter barycenter'], data['Earth']
jpos, epos      = jupiter.at(t).position.km, earth.at(t).position.km
d_instantaneous = np.sqrt(((jpos - epos)**2).sum())

d_light = earth.at(t).observe(jupiter).distance().km  # where WAS Jupiter 48 minutes ago?

clight = 299792.458  # km/s

print "d_instantaneous / c = ", d_instantaneous/clight
print "d_light / c =         ", d_light/clight
</code></pre>

CC BY-SA 3.0
2016-08-16T03:19:30.817Z
@barrycarter While it's not "Python or pseudocode" mentioned in the question, that is definitely *the go-to place* for real source code!
uhoh
CC BY-SA 3.0
2016-08-16T03:22:58.280Z
note: I've asked about the long-term off-line use of Skyfield here: [https://github.com/skyfielders/python-skyfield/issues/105](https://github.com/skyfielders/python-skyfield/issues/105).
uhoh
CC BY-SA 3.0
2016-08-16T15:56:56.650Z
...and of course replace `jupiter = data['Jupiter barycenter']` with `mars = data['Mars barycenter']` if you want Mars :-)
uhoh
CC BY-SA 3.0
2016-08-16T02:34:03.720Z
2016-08-24T02:26:26.223Z
2017-04-13T12:58:55.210Z
uhoh
<p>I'm looking for a function that given a time (for example <a href="https://en.wikipedia.org/wiki/Unix_time" rel="nofollow noreferrer">Unix time</a>) gives back the current distance between Mars and Earth in light-seconds.</p>

<p>It can be a mathematical function or programming function (preferably in Python or pseudocode), but must not rely on an Internet connection.</p>

<p>This question is similar to <a href="https://space.stackexchange.com/questions/17479/current-communication-delay-to-mars">a question I asked erlier</a>, but again I need to get the distance without an Internet connection.</p>

<p>Thanks in advance!</p>

<p>Sidenote: I'm planning on learning orbital mechanics, but I need this function earlier. :)</p>

<distances>
Function for current distance between in light-seconds between Mars and Earth
Kevin
# Function for current distance between in light-seconds between Mars and Earth

(Asked by: username_0 on 2016-08-15)

I'm looking for a function that given a time (for example [Unix time](https://en.wikipedia.org/wiki/Unix_time)) gives back the current distance between Mars and Earth in light-seconds.

It can be a mathematical function or programming function (preferably in Python or pseudocode), but must not rely on an Internet connection.

This question is similar to [a question I asked erlier](https://space.stackexchange.com/questions/17479/current-communication-delay-to-mars), but again I need to get the distance without an Internet connection.

Thanks in advance!

Sidenote: I'm planning on learning orbital mechanics, but I need this function earlier. :)

**Tags:** distances


---

## 2 answers

### Answer by username_1 on 2016-08-15. Score: 4

There is no simple function that is accurate. Both Earth and Mars are orbiting in ellipses (Kepler's first law). They are travelling at varying speeds (Kepler's second). To find the current distance between them you have to calculate their positions in three dimensions.

In other words you have to do orbital kinematics. [How do I calculate the positions of objects in orbit?](https://astronomy.stackexchange.com/questions/8012/how-do-i-calculate-the-positions-of-objects-in-orbit)

If accuracy is not important. You can approximate the orbits with circles in the same plane. The Earths position is then
$$(\cos(2\pi t + \alpha), \sin(2\pi t +\alpha))$$
and Mars is
$$(1.5\cos(2\pi t/1.882 +\beta), 1.5\sin(2\pi t/1.882 + \beta))$$

The distance between them is calculated by pythagoras

The distances are in AU and the time is in years (converting to lightseconds is then easy). $\alpha$ and $\beta$ are constants chosen to fit the positions of the planets at time 0. The values 1.5 and 1.882 are chosen because Mars's orbit is about 1.5 times bigger than Earth, and a Mars year is 1.882 times longer than Earth.

The circular approximation however is poor for Mars, and this ignores the fact that Mars isn't in exactly the same orbital plane as the earth, so the values will only be rather approximate.

> **Comments:**
> **username_0**: Thanks! Two question, when is (what is the date of) $t = 0$ and what it the unit of $t$? What is the maximal error percentage? Thanks in advance.  

---

### Answer by username_2 on 2016-08-16. Score: 4

note: This is an "extended comment" type answer, addressing two potential solutions that may not be exactly what you need, but are close.
A thing which gives you positions of things in the solar system given a specified time is called an Ephemeris.
Originally they were tables of predictions (past and future) based on calculations. By the time of Newton, they became quite good, since Newton developed a method to iteratively solve Kepler's equation (e.g. Newton's Method). See the question How did Newton and Kepler (actually) do it? and the answers there for a little more background.

The Python package PyEphem has been around and well supported, and is the pythonic reincarnation of XEphem. I haven't used it, but I believe it keeps enough information about orbital parameters at certain epochs to generate an ephemeris, including some gravitational perturbations. In other words, it's much more than planets moving on fixed elliptical orbits around a fixed sun. So I believe it runs without internet connection.
I never used it because I was recommended to look at Skyfield and it's exactly what I needed. It downloads a standard JPL ephemeris that you choose, and then just uses it from your hard drive after that. However, in order to deal with leap seconds and other time related effects, it occasionally needs to check the internet for leap second information updates, since these are arbitrary.

I don't know if Skyfield has a mode to avoid that. Actually that's a good question. If you work with a timescale that doesn't have leap seconds, I am not sure if it will run in its current version.
Both Skyfield and PyEphem Python packages have been written and are maintained by @BrandonRhodes.
If you can allow an occasional connection to the internet (say every month or few months) then Skyfield is extremely easy and pythonic to use. For example here is a script I used in this answer. If you want to convert time formats from system time, you can search my questions in stackoverflow and space exploration. If you want it for your local computers system time now, you just use t = ts.now() directly instead of utc.
import numpy as np
import matplotlib.pyplot as plt

from skyfield.api import load

data   = load('de421.bsp')
ts     = load.timescale()
t      = ts.utc(2016, 7, 5, 9, 50, 0)

jupiter, earth  = data['Jupiter barycenter'], data['Earth']
jpos, epos      = jupiter.at(t).position.km, earth.at(t).position.km
d_instantaneous = np.sqrt(((jpos - epos)**2).sum())

d_light = earth.at(t).observe(jupiter).distance().km  # where WAS Jupiter 48 minutes ago?

clight = 299792.458  # km/s

print "d_instantaneous / c = ", d_instantaneous/clight
print "d_light / c =         ", d_light/clight

> **Comments:**
> **username_2**: @barrycarter While it's not "Python or pseudocode" mentioned in the question, that is definitely *the go-to place* for real source code!  
> **username_2**: note: I've asked about the long-term off-line use of Skyfield here: [https://github.com/skyfielders/python-skyfield/issues/105](https://github.com/skyfielders/python-skyfield/issues/105).  
> **username_2**: ...and of course replace `jupiter = data['Jupiter barycenter']` with `mars = data['Mars barycenter']` if you want Mars :-)  

---

