Skip to content

Instantly share code, notes, and snippets.

@virtuallynathan
Last active March 17, 2023 20:53
Show Gist options
  • Save virtuallynathan/a38731d0b65dd02b079f62b5263e9a48 to your computer and use it in GitHub Desktop.
Save virtuallynathan/a38731d0b65dd02b079f62b5263e9a48 to your computer and use it in GitHub Desktop.
import re
import requests
from skyfield.api import Topos, Loader, wgs84
from skyfield.timelib import Time
from skyfield.sgp4lib import EarthSatellite
# Load the TLE data from Celestrak
def get_tle_data(url):
response = requests.get(url)
response.raise_for_status()
return response.text.splitlines()
satellites_url = 'https://www.celestrak.com/NORAD/elements/stations.txt'
starlink_url = 'https://www.celestrak.com/NORAD/elements/starlink.txt'
satellites_tle = get_tle_data(satellites_url)
starlink_tle = get_tle_data(starlink_url)
load = Loader('.')
ts = load.timescale()
def load_tle_from_lines(lines):
satellites = {}
for i in range(0, len(lines) - 2, 3):
name, line1, line2 = lines[i:i + 3]
satellite = EarthSatellite(line1, line2, name, ts)
satellites[satellite.model.satnum] = satellite
return satellites
satellites = load_tle_from_lines(satellites_tle)
starlinks = load_tle_from_lines(starlink_tle)
# Find all Starlink satellites that match the pattern STARLINK-300XX
starlink_pattern = re.compile(r'STARLINK-300\d\d')
matched_starlinks = [sat for sat in starlinks.values() if starlink_pattern.match(sat.name)]
# Set the starting date and time for checking conjunctions
from datetime import datetime, timedelta
# Calculate the distance between two satellites in kilometers
def distance_between(sat1, sat2, t):
sat1_position = sat1.at(t).position.km
sat2_position = sat2.at(t).position.km
return sum((p1 - p2) ** 2 for p1, p2 in zip(sat1_position, sat2_position)) ** 0.5
# Define the threshold for a conjunction (in kilometers)
conjunction_threshold_km = 5.0
# Generate a list of times to check for conjunctions (every minute)
start_time = datetime(2023, 2, 28, 0, 0)
end_time = datetime(2023, 3, 17, 0, 0)
time_range = [start_time + timedelta(minutes=i) for i in range(((end_time - start_time).days * 24 * 60) + 1)]
# Find conjunctions
for check_time in time_range:
t_check = ts.utc(check_time.year, check_time.month, check_time.day, check_time.hour, check_time.minute)
for starlink in matched_starlinks:
for sat in satellites.values():
distance_km = distance_between(sat, starlink, t_check)
if distance_km < conjunction_threshold_km:
print(f'Conjunction between {sat.name} and {starlink.name}:')
print(f' Distance: {distance_km:.2f} km')
print(f' Time (UTC): {t_check.utc_strftime()}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment