Where is the Earth?
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, specifically its fourth tutorial: The Earth.
NOTE
The video tutorial uses VS Code and a Docker container for a self-contained development environment. However, in my setup, I install everything required inside pyenv on my Linux system, 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:
$ touch whereis_earth.py
2. Import
Open whereis_earth.py
in the editor.
Start by importing the spiceypy
wrapper and some Python built-in modules:
#!/usr/bin/env python3
from datetime import datetime
import math
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 computationspk
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
to their respective local directories.
Load the kernels for use in the script:
spiceypy.furnsh('spice/kernels/lsk/naif0012.tls')
spiceypy.furnsh('spice/kernels/spk/planets/de432s.bsp')
4. 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, so convert UTC 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 is399
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): 2025-02-04T00:00:00
Ephemeris Time (midnight): 791899269.1848668
State vector of Earth with regards to the Sun today (midnight):
[-1.04407049e+08 1.04151799e+08 -5.50740876e+03 -2.15176507e+01
-2.12117050e+01 2.96611194e-04]
Current distance between the Earth and the Sun in:
* km: 147473486.17872483
* AU: 0.9857993671550986
Good stuff! I look forward to exploring the rest of the series.
8. Resources
- Source: whereis_earth.py
- Space Science with Python - Part 4: The Earth
- Manage multiple Python versions in Linux using pyenv
- SPICE Toolkit
- SPICE generic kernels
- NAIF ID codes
- SpiceyPy
You can like, share, or comment on this post on the Fediverse 💬
» Next: FOSS IN SPACE #2: Space Python
« Previous: Manage multiple Python versions in Linux using pyenv