While循环发布python3.6.4

2024-10-02 10:27:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着写一个方位角/仰角计算器作为探索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中运行这个时,我什么也得不到。我不明白为什么“可见”和“不可见”都没有出现。你知道吗

完整代码:

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

我想计算卫星可见时的高程并显示它,不可见时停止计算。你知道吗


Tags: toinfromimportredatetimetimemath
1条回答
网友
1楼 · 发布于 2024-10-02 10:27:32

您的描述表明rs**2 < (d**2 + re**2)当您第一次进入循环时。在这种情况下,程序跳过整个循环。因为这两个print语句都在循环中,所以您不会得到任何输出。你知道吗

我无法告诉您如何修复此问题,因为您尚未描述逻辑或所需的输出。由于给定的代码无法运行,我无法可靠地再现问题并显示修复。你知道吗

相关问题 更多 >

    热门问题