Where is the Earth?

Last updated on 2022-01-02 Tagged under  #python   #programming   #space 

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 (Debian 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 space-science-with-python/kernels/{lsk,spk}
$ cd space-science-with-python

Create file earth_comp.py. Begin with importing the wrapper, plus some Python built-in modules ...

import math
import spiceypy
from datetime import datetime

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:

Download lsk/naif0012.tls and spk/de432s.bsp, then load files in the script ...

spiceypy.furnsh('kernels/lsk/naif0012.tls')
spiceypy.furnsh('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
)

... then 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}\n"
    + f"{current} AU: {earth_sun_distance_au}"
)

Run ...

$ ./whereis_earth.py 
Today's date (midnight): 2022-01-02T00:00:00
Ephemeris Time (midnight): 694353669.1839501
State vector of Earth w.r.t. the Sun for today (midnight):
 [-2.87014567e+07  1.44279350e+08 -6.68995422e+03 -2.97137958e+01
 -5.92516791e+00  1.44427451e-03]
Current distance between the Earth and the Sun in km: 147106439.11229855
Current distance between the Earth and the Sun in AU: 0.9833458090601837

Good stuff!

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

Thanks for reading! Read other posts?

» Later: Dot bashrc

« Earlier: Buster to Bullseye