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, and its fourth tutorial: The Earth.
Note on setup: Series creator is using VS Code and a Docker container for a self-contained development environment. However, in my setup, I directly install everything needed 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.
Install spiceypy
...
$ pip install spiceypy
Create directories for this project ...
$ mkdir -p spice/kernels/{lsk,spk}
Create file whereis_earth.py
.
Begin with importing the wrapper, plus some Python built-in modules ...
import math
from datetime import datetime
import spiceypy
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 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 timespk
kernels are for planets, natural satellites, a few asteroids and comets, and Deep Space Network (DSN) ground stations
Download lsk/naif0012.tls
and spk/de432s.bsp
, then load files in the script ...
spiceypy.furnsh('spice/kernels/lsk/naif0012.tls')
spiceypy.furnsh('spice/kernels/spk/de432s.bsp')
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}")
Compute the state vector of the Earth with regard to the Sun. Target targ=
is the Earth, and its NAIF Integer ID code is 399
; ECLIPJ2000
is the ecliptic plane ...
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(
'State vector of Earth w.r.t. the Sun for today (midnight):\n',
earth_state_wrt_sun
)
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 ...
current = 'Current distance between the Earth and the Sun in'
print(f"{current} km: {earth_sun_distance}")
print(f"{current} AU: {earth_sun_distance_au}")
Run ...
$ ./whereis_earth.py
Today's date (midnight): 2022-10-07T00:00:00
Ephemeris Time (midnight): 718372869.1823435
State vector of Earth with regards to the Sun for today (midnight):
[ 1.45468145e+08 3.46604808e+07 -2.19008424e+03 -7.39332201e+00
2.88534422e+01 -1.87089484e-03]
Current distance between the Earth and the Sun in km: 149540396.39450407
Current distance between the Earth and the Sun in AU: 0.9996158085743531
I look forward to exploring the rest of the series. It's extremely relevant to my focus on learning to program, and combining code with space-focused activities!
Source: whereis_earth.py
» Next: Track my weight using Python
« Previous: Nostromo boot noise