simulate a satellite orbit using sgp4 in python

To simulate a satellite orbit using SGP4 in Python, you can use the sgp4 module. This module implements the SGP4 algorithm, which is a mathematical model used to predict the position and velocity of satellites in Earth orbit.

Here is an example code that shows how to use sgp4 to propagate the orbit of a satellite:

main.py
from sgp4.earth_gravity import wgs72
from sgp4.io import twoline2rv
from datetime import datetime, timedelta

# TLE data for the satellite 
line1 = '1 25544U 98067A   19168.58969394  .00001382  00000-0  29673-4 0  9998'
line2 = '2 25544  51.6444 152.9324 0007151  44.1849 345.9938 15.51571890165539'

# parse the TLE data
satellite = twoline2rv(line1, line2, wgs72)

# set the start time
dt = datetime(2019, 6, 17, 12, 0, 0)

# propagate the orbit for 10 minutes
for i in range(60):
    # get the position and velocity of the satellite
    position, velocity = satellite.propagate(dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second)
    
    # print the position and velocity
    print("Position (km): ", position)
    print("Velocity (km/s): ", velocity)
    
    # update the time
    dt += timedelta(seconds=10)
821 chars
26 lines

In this code, we first import the necessary libraries: wgs72 from sgp4.earth_gravity, twoline2rv from sgp4.io, and datetime and timedelta from the Python standard library.

Next, we define the TLE data for the satellite and parse it using twoline2rv to create a Satellite object.

We then set the start time for the simulation, and propagate the orbit for 10 minutes (60 time steps of 10 seconds each). For each time step, we call the propagate method of the Satellite object to get the position and velocity of the satellite at that time step, and print the results.

Finally, we update the time using timedelta so that the simulation progresses forward in time.

Note that the position and velocity are given in Earth-centered inertial (ECI) coordinates, which are a Cartesian coordinate system with the origin at the center of the Earth and the x-axis pointing towards the vernal equinox. To convert to Earth-centered Earth-fixed (ECEF) coordinates, you can use the eci_to_ecef function from the sgp4.propagation module.

gistlibby LogSnag