I was making a satellite position detection project in python. But this code is throwing this error:
Traceback (most recent call last):
File "C:\Users\soumy\OneDrive\Desktop\PySatelliteFinder\main.py", line 50, in <module>
calculate_and_display_position(name.upper()) # Convert to uppercase for TLE lookup
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\soumy\OneDrive\Desktop\PySatelliteFinder\main.py", line 15, in calculate_and_display_position
satellite = ts[name]
~~^^^^^^
TypeError: list indices must be integers or slices, not str
My Code is This:
import turtle
from datetime import datetime
from skyfield.api import Topos, load
# Function to calculate and display satellite position
def calculate_and_display_position(name):
# Clear previous drawing
screen.clear()
# Load TLE data
ts = load.tle_file('stations.txt')
try:
satellite = ts[name]
except KeyError:
screen.write(f"Satellite '{name}' not found.", align="center", font=("Arial", 16, "normal"))
return
# Get current UTC time
current_time = datetime.utcnow()
# Calculate satellite position
topos = satellite.at(ts.utc(current_time.year, current_time.month, current_time.day,
current_time.hour, current_time.minute, current_time.second))
alt, az, _ = topos.observe(observer_location).apparent().altaz()
# Display satellite name, position, and local time
screen.write(f"Satellite: {name}\n"
f"Altitude: {alt.degrees:.2f} degrees\n"
f"Azimuth: {az.degrees:.2f} degrees\n"
f"Local Time: {current_time}", align="left", font=("Arial", 16, "normal"))
# Create a Turtle screen
screen = turtle.Screen()
screen.bgpic("world_map.gif") # You'll need to provide a world map image
# Create an observer location
observer_location = Topos(latitude_degrees=23.247921, longitude_degrees=87.871365)
# Initialize the Turtle
satellite_turtle = turtle.Turtle()
satellite_turtle.penup()
satellite_turtle.goto(0, 200)
satellite_turtle.hideturtle()
# Input box for satellite name
name = screen.textinput("Enter Satellite Name", "Enter the satellite name:")
if name:
calculate_and_display_position(name.upper()) # Convert to uppercase for TLE lookup
# Listen for clicks to change satellite
screen.onclick(lambda x, y: calculate_and_display_position(screen.textinput("Enter Satellite Name", "Enter the satellite name:").upper()))
# Close the window when clicked
screen.exitonclick()
Now if i click the ok button, then its happening: The Moment When The Code Is Run, It Throws A Dialog Asking For The Name
I try separating the ts = load.tle_file('stations.txt') outside of calculate_and_display_position(name) and inside calculate_and_display_position(name), i write global ts but still showing the same thing.
I was exepecting a output where when entered the latest satellite name, the module will fetch the coordinates and make a point in the map and shows the position with the name in red.
Comparing your code to the examples on Skyfield’s Earth Satellites page, it looks like you are missing the line that would convert the list of satellites into a dictionary indexed by satellite name:
Adapting to your own code, maybe: