Where is the Earth?

Last edited on 2023-09-20 Tagged under  #space   #python   #programming 

Where is the Earth exactly located with respect to the Sun? Let's use Python to find out!

Code and notes are adapted from the YouTube series Space Science with Python, and its fourth tutorial: The Earth.

Note on my setup: Series creator is using VS Code and a Docker container for a self-contained development environment. However, in my setup, I install everything needed inside pyenv on my Linux laptop, and use a text editor to write code.

Let's go!

SPICE is an observation geometry system for space science missions developed by NASA, comprising a library and toolkit. Though SPICE itself is not written in Python, I can use SPICE with the Pythonic wrapper spiceypy to start computing Earth's location and velocity.

1. Setup

Install spiceypy ...

$ pip install spiceypy

Create directories for this project ...

$ mkdir -p spice/kernels/lsk
$ mkdir -p spice/kernels/spk/planets

Create an empty program file named whereis_earth.py.

2. Import

Open whereis_earth.py in the editor.

Start by importing the spiceypy wrapper, plus some Python built-in modules ...

import math
from datetime import datetime
import spiceypy

3. SPICE kernels

SPICE stores data for its repository of space objects in various kernels.

The generic_kernels directory holds data stores not tied to a specific mission. Required kernels for this script are found in the lsk and spk subdirectories:

  • lsk holds the kernel to handle leapseconds for ephemeris time; used in almost any SPICE-based computation
  • spk kernels are for planets, natural satellites, a few asteroids and comets, and Deep Space Network (DSN) ground stations

From generic_kernels, download kernels lsk/naif0012.tls and spk/planets/de432s.bsp.

Load the kernels for use in the script ...

spiceypy.furnsh('spice/kernels/lsk/naif0012.tls')
spiceypy.furnsh('spice/kernels/spk/planets/de432s.bsp')

4. Ephemeris Time

Get today's date, convert date object to a string, and replace the time with midnight ...

date_today = datetime.today().strftime('%Y-%m-%dT00:00:00')
print(f"Today's date (midnight): {date_today}")

SPICE doesn't work with UTC time; convert to Ephemeris Time ...

et_date_today = spiceypy.utc2et(date_today)
print(f"Ephemeris Time (midnight): {et_date_today}")

5. State Vector

Compute the state vector of the Earth with regard to the Sun:

  • Target targ= is the Earth barycenter, and its NAIF Integer ID code is 399
  • ECLIPJ2000 is the ecliptic plane
  • Observer is the Sun, and its NAIF ID is 10
earth_state_wrt_sun, earth_sun_light_time = spiceypy.spkgeo(
    targ=399, et=et_date_today, ref='ECLIPJ2000', obs=10
)

The state vector is 6 dimensional: x,y,z in km and the corresponding velocities in km/s (returns a list) ...

print(
    "\nState vector of Earth with regards to the Sun for today (midnight): "
    f"\n{earth_state_wrt_sun}"
)

6. Distance

Distance should be around 1 astronomical unit (AU).

First, we compute the distance in km ...

earth_sun_distance = math.sqrt(
    earth_state_wrt_sun[0]**2.0
    + earth_state_wrt_sun[1]**2.0
    + earth_state_wrt_sun[2]**2.0
)

Convert the distance into AU ...

earth_sun_distance_au = spiceypy.convrt(earth_sun_distance, 'km', 'AU')

Display current distance ...

print("\nCurrent distance between the Earth and the Sun in:")
print(f"* km: {earth_sun_distance_km}")
print(f"* AU: {earth_sun_distance_au}")

7. Run

$ whereis_earth.py 
Today's date (midnight): 2023-09-20T00:00:00
Ephemeris Time (midnight): 748440069.1824044

State vector of Earth with regards to the Sun today (midnight): 
[ 1.49991007e+08 -9.26512036e+06 -3.04056190e+02  1.34198990e+00
  2.96266871e+01 -6.64667552e-04]

Current distance between the Earth and the Sun in:
* km: 150276893.4649018
* AU: 1.0045389874095627

Good stuff! I look forward to exploring the rest of the series.

8. Resources

Onward!

Thanks for reading! Read other posts?

» Next: #32. Travelling Telescope

« Previous: #31. The High Frontier