plot the ground track of the international space station in python

To plot the ground track of the International Space Station (ISS) in Python, we can utilize NASA's Open APIs that provide live data on the current position of the ISS. We will also need to use some astronomy libraries and visualization tools.

  1. Install the necessary libraries:
main.py
pip install skyfield numpy matplotlib requests
47 chars
2 lines
  1. Import the necessary modules:
main.py
import numpy as np
from skyfield.api import Topos, load, EarthSatellite
import requests
import matplotlib.pyplot as plt
120 chars
5 lines
  1. Use the NASA's API to retrieve the current position of the ISS:
main.py
url = 'http://api.open-notify.org/iss-now.json'
response = requests.get(url)
data = response.json()
latitude = float(data['iss_position']['latitude'])
longitude = float(data['iss_position']['longitude'])
204 chars
6 lines
  1. Load the necessary astronomical datasets:
main.py
ts = load.timescale()
planets = load('de421.bsp')
earth = planets['earth']
75 chars
4 lines
  1. Define a function that converts the latitude and longitude to a Skyfield Topos object:
main.py
def get_topos(latitude, longitude):
    return earth + Topos(latitude_degrees=latitude, longitude_degrees=longitude)
117 chars
3 lines
  1. Define a function that calculates the ISS's ground track for a given period of time:
main.py
def get_ground_track(topos, hours):
    t = ts.now()
    end_time = t.utc_datetime() + datetime.timedelta(hours=hours)
    times = ts.utc(t.utc_datetime(), end_time, 10)
    satellite = EarthSatellite(None, None, None, ts=ts)
    satellite = satellite.from_satellite_id('ISS')
    geometry = satellite.at(times).subpoint()
    latitudes, longitudes = [], []
    for i in range(len(geometry.latitude.degrees)):
        topos_lat = topos.latitude.radians
        topos_long = topos.longitude.radians
        latitude = geometry.latitude.degrees[i]
        longitude = (geometry.longitude.degrees[i] + 180) % 360 - 180
        if abs(topos_long - longitude) < np.pi:
            latitudes.append(latitude)
            longitudes.append(longitude)
        elif topos_long > 0:
            longitudes.append(longitude + 360)
            latitudes.append(latitude)
        else:
            longitudes.append(longitude - 360)
            latitudes.append(latitude)
    return latitudes, longitudes
992 chars
24 lines
  1. Use the get_ground_track function to calculate the ISS's ground track for a specified amount of time:
main.py
hours = 3
topos = get_topos(latitude, longitude)
latitudes, longitudes = get_ground_track(topos, hours)
104 chars
4 lines
  1. Plot the ground track using matplotlib:
main.py
plt.plot(longitudes, latitudes, 'r', linewidth=2)
plt.xlabel('Longitude (degrees)')
plt.ylabel('Latitude (degrees)')
plt.title('ISS Ground Track')
plt.grid(True)
plt.show()
173 chars
7 lines

This should generate a plot of the ISS's ground track for the specified duration of time.

gistlibby LogSnag