I'm trying to write an azimuth/elevation calculator as an exercise to explore the astropy libraries. I'm quite new to Python and have run into trouble with a basic while loop within my programme:
LATLONGALT = eci2lla(x,y,z,dt)
lon_sub = LATLONGALT[0]/u.rad
lat_sub = LATLONGALT[1]/u.rad
#print (LATLONGALT[2]) #Geometric altitude (m)
gamma = (math.acos(math.cos(math.radians(lat_es)) * \
math.cos(lat_sub) * \
math.cos(lon_sub -
(math.radians(lon_es)) + \
math.sin(math.radians(lat_es)) * \
math.sin(lat_sub))))
# Gamma is the central angle in radians
rs = LATLONGALT[2]/u.m + re
dr = re/rs
d = rs*(math.sqrt(1+(dr)**2 - 2*(dr)*math.cos(gamma)))
while rs**2 > (d**2 + re**2):
print('Visible')
EL = math.acos((rs/d)*math.sin(gamma))
if rs**2 < (d**2 + re**2):
print('Not Visible')
break
This is contained in a larger while loop that I have running to calculate parameters from sgp4. When I run this in shell, I get nothing. I don't understand why neither 'Visible' or 'Not visible' is showing up.
Full code:
from sgp4.earth_gravity import wgs84 #import most recent gravity model used in satellite tracking
from sgp4.io import twoline2rv
import datetime
from datetime import datetime
from astropy.coordinates import GCRS, ITRS, EarthLocation, CartesianRepresentation
from astropy import units as u
from astropy.time import Time
import time
import math
def eci2lla(x,y,z,dt):
#Convert Earth-Centered Inertial (ECI) cartesian coordinates to latitude, longitude, and altitude, using astropy.
#Inputs :
#x = ECI X-coordinate (m)
#y = ECI Y-coordinate (m)
#z = ECI Z-coordinate (m)
#dt = UTC time (datetime object)
#Outputs :
#lon = longitude (radians)
#lat = geodetic latitude (radians)
#alt = height above WGS84 ellipsoid (m)
# convert datetime object to astropy time object
tt=Time(dt,format='datetime')
# Read the coordinates in the Geocentric Celestial Reference System
gcrs = GCRS(CartesianRepresentation(x=x*u.meter, y=y*u.meter, z=z*u.meter), obstime=tt)
# Convert it to an Earth-fixed frame
itrs = gcrs.transform_to(ITRS(obstime=tt))
el = EarthLocation.from_geocentric(itrs.x, itrs.y, itrs.z)
# conversion to geodetic
lon, lat, alt = el.to_geodetic()
return lon, lat, alt
print ('Type track to initialize tracking. Press CTRL+C to cease tracking.')
run = input('')
try:
while run == 'track':
t = datetime.now().strftime("%H%M%S%f") # Current time formatted into HHMMSS.MSMS """
v = list(t) # current date and time with individual number as list elements"""
a = int(v[0] + v[1]) # current hour in integer form
b = int(v[2] +v[3]) # current minute in integer form
c = int(v[4] + v[5]) # current second in integer form
d = int(v[6] + v[7]) # current millisecond in integer form
p = datetime.now().strftime("%Y%m%d") # Current date formatted into YYYY:MM:DD
q = list(p) # Current formatted date in list form
f = int(q[0] + q[1] + q[2] + q[3]) #Current year as integer
g = int(q[4] + q[5]) #Current month as integer
h = int(q[6] + q[7]) #Current day as integer
lon_es = -1.124632 # University of Leicester Longitude in radians
lat_es = 52.621139 # University of Leicester Latitude
utc_time = datetime(f, g, h, a, b, c, d) #UTC time in real time
dt = utc_time # Renaming for convenience
re = 6.3781*10**6 #Earth radius in m
line1 = ('1 39090U 13009E 18024.54492438 ' # TLE line 1
'.00000001 00000-0 15855-4 0 9997')
line2 = ('2 39090 098.5332 232.6769 0009391 ' #TLE line 2
'010.6657 349.4725 14.35007731257206')
satellite = twoline2rv(line1, line2, wgs84) #returns a Satellite object whose attributes carry the data loaded from the TLE entry.
position, velocity = satellite.propagate(
f, g, h, a, b, c)
#print (satellite.error) #nonzero on error
#print (satellite.error_message)
#print('Satellite position (x,y,z) [km]:', position) # x, y , z position of satellite (km)
#print ('Satellite velocity (x,y,z) [km/s]:', velocity) # x, y, z velocity of satellite (km/s)
x = position[0]*1000
y = position[1]*1000
z = position[2]*1000
R = math.sqrt(x**2 + y**2 + z**2) #Distance from the centre of the Earth
#print(R)
LATLONGALT = eci2lla(x,y,z,dt)
lon_sub = LATLONGALT[0]/u.rad
lat_sub = LATLONGALT[1]/u.rad
#print (LATLONGALT[2]) #Geometric altitude (m)
gamma = (math.acos(math.cos(math.radians(lat_es))*math.cos(lat_sub)*math.cos(lon_sub - (math.radians(lon_es)) + math.sin(math.radians(lat_es))*math.sin(lat_sub))))
#Gamma is the central angle in radians
rs = LATLONGALT[2]/u.m + re
dr = re/rs
d = rs*(math.sqrt(1+(dr)**2 - 2*(dr)*math.cos(gamma)))
while rs**2 > (d**2 + re**2):
print('Visible')
EL = math.acos((rs/d)*math.sin(gamma))
if rs**2 < (d**2 + re**2):
print('Not Visible')
break
time.sleep(0.5) # Pause so interpreter isn't saturated with data
except KeyboardInterrupt: #Waits for CTRL+C to end tracking
pass
I want to calculate the elevation and display it when the satellite is visible, and stop the calculation when it is not.