-2

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.

Prune
  • 76,765
  • 14
  • 60
  • 81
Lorcank11
  • 137
  • 8
  • 2
    Your script is terminating? If the script finishes but visible is never printed, the condition on the loop is never met. I would recommend printing out rs, d, and re so you can see what the values are. Also, from what we're seeing, I'm not seeing a point in having this while loop. It doesn't look like rs, d, or re are changing. – Brett Beatty Feb 02 '18 at 00:51
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [Minimal, complete, verifiable example](http://stackoverflow.com/help/mcve) applies here. We cannot effectively help you until you post your MCVE code and accurately describe the problem. We should be able to paste your posted code into a text file and reproduce the problem you described. Your posted code fails on the very first line with several undefined variables. – Prune Feb 02 '18 at 00:56
  • We don't need your *full* code: just an MCVE. Since the problem you have depends only on a handful of data values and your loop logic, please reduce the posting to that much. In *your* environment, print out the values of those variables just before the problem `while` loop. Then, edit your code to assign those values to the variables. Post with *only* those assignments and the `while` loop in question. – Prune Feb 02 '18 at 17:33

1 Answers1

0

Your description suggests that rs**2 < (d**2 + re**2) when you first hit the loop. In this case, the program skips the entire loop. Since both of the print statements are inside the loop, you get neither output.

I can't tell you how to fix this, because you haven't described the logic or your desired output. Since your given code doesn't run, I cant' reliably reproduce the problem and show a repair.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Thanks for your reply. That makes a lot of sense. The idea of this loop was to display in the interpreter when a satellite is visible in accordance with the logic found in page 3/6 of this document: http://faculty.kfupm.edu.sa/ee/wajih/files/EE%20418,%20Lecture%2003.pdf The idea was to then calculate the elevation of the satellite relative to the observer when it is visible, and cease calculation when it goes out of site. I will edit the original post to show the full script at the end. Apologies for the inconvenience, I am quite new to this. – Lorcank11 Feb 02 '18 at 14:45