循环问题 - Python 3.6.4

时间:2018-02-02 00:48:35

标签: python python-3.x astronomy astropy

我试图将方位角/仰角计算器作为练习来探索星座图库。我对Python很陌生,并且在我的程序中遇到了基本的while循环问题:

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

这包含在我运行的较大while循环中,用于计算sgp4中的参数。当我在shell中运行它时,我什么也得不到。我不明白为什么“Visible&#39;或者&#39;不可见&#39;正在出现。

完整代码:

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

我想计算高程并在卫星可见时显示,并在不可见时停止计算。

1 个答案:

答案 0 :(得分:0)

当您第一次点击循环时,您的说明会建议rs**2 < (d**2 + re**2)。在这种情况下,程序会跳过整个循环。由于两个print语句都在循环内,因此既没有输出。

我无法告诉您如何解决这个问题,因为您还没有描述逻辑或您想要的输出。既然你的代码没有运行,我就不能这样做了。可靠地重现问题并显示修复。