np.polyfit公司如何为y inpu将列表放入2D数组

2024-09-25 00:26:36 发布

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

我为卫星轨道生成了纬度、经度和高度数据。现在,我想对我的数据做一个多项式拟合,以便插值。numpy.polyfit()只对每个列包含一个数据集的y坐标使用2D数组。在

现在经度和高度在两个单独的列表中,我需要将它们组合成一个2D数组。我尝试过np.matrix([lon] [alt]),但我得到一个错误:

TypeError: list indices must be integers, not list

输入:

Lat: [-22.0, -50.5, -5.8, 45.2, 32.7]

Lon: [-66.6, 21.3, 90.1, 147.4, -115.7]

Alt: [368752.8, 371184.4, 357834.7, 375131.8, 375643.8]

期望输出:

^{pr2}$

完整代码:

'''
Satellite Orbit Propagation Function
'''

def orbitPropandcoordTrans(propNum, orbitPineapple_J2000time, _ecc, _inc, _raan, _arg_pe, _meananom, meanMotion):

        '''
        Create original orbit and run for 100 propagations (in total one whole orbit)
        in order to get xyz and time for each propagation step.
        The end goal is to plot the lat, lon, & alt data to see if it matches ISS groundtrace.
        '''
        import orbital
        from orbital import earth, KeplerianElements, plot
        import matplotlib.pyplot as plt
        import numpy as np
        from astropy import time
        from astropy.time import TimeDelta, Time
        from astropy import units as u
        from astropy import coordinates as coord

    'Calculate Avg. Period from Mean Motion'
    _avgPeriod = 86400 / meanMotion
    print('_avgPeriod', _avgPeriod)

    'Generate Orbit'
    orbitPineapple = KeplerianElements.with_period(_avgPeriod, body=earth, e=_ecc, i=(np.deg2rad(_inc)), raan=(np.deg2rad(_raan)), arg_pe=(np.deg2rad(_arg_pe)), M0=(np.deg2rad(_meananom))) #ref_epoch=   
    plot(orbitPineapple)
    plt.show()

    'Propagate Orbit and retrieve xyz'
    myOrbitX = []         #X Coordinate for propagated orbit step
    myOrbitY = []         #Y Coordinate for propagated orbit step
    myOrbitZ = []         #Z Coordinate for propagated orbit step
    myOrbitTime = []      #Time for each propagated orbit step
    #propNum = 100        #Number of propagations and Mean Anomaly size (one orbit 2pi/propNum)

    for i in range(propNum):
        orbitPineapple.propagate_anomaly_by(M=(2.0*np.pi/propNum)) #Propagate the orbit by the Mean Anomaly
        myOrbitX.append(orbitPineapple.r.x)                        #x vals
        myOrbitY.append(orbitPineapple.r.y)                        #y vals
        myOrbitZ.append(orbitPineapple.r.z)                        #z vals
        myOrbitTime.append(orbitPineapple_J2000time)               #J2000 time vals
        #myOrbitJ2000Time.append(orbitPineapple.t)
        #plot(orbitPineapple)


    'Getting the correct J2000 Time'
    times = [orbitPineapple_J2000time] * propNum
    #print('times',times)
    #print('')

    myOrbitJ2000Time = [] #J2000 times
    for i in range(propNum):
        myOrbitJ2000Time.append(times[i] + i) 



    '''Because the myOrbitTime is only the time between each step to be the sum of itself plus
    all the previous times. And then I need to convert that time from seconds after J2000 to UTC.'''
    myT = [] #UTC time list

    for i in range(propNum):
        myT.append((Time(2000, format='jyear') + TimeDelta(myOrbitTime[i]*u.s)).iso) #Convert time from J2000 to UTC
    #print('UTC Time List Length:', len(myT))
    #print('UTC Times:', myT)

    '''Now I have xyz and time for each propagation step and need to convert the coordinates from
    ECI to Lat, Lon, & Alt'''
    #now = []     #UTC time at each propagation step
    xyz =[]      #Xyz coordinates from OrbitalPy initial orbit propagation
    cartrep = [] #Cartesian Representation
    gcrs = []    #Geocentric Celestial Reference System/Geocentric Equatorial Inertial, the default coord system of OrbitalPy
    itrs =[]     #International Terrestrial Reference System coordinates
    lat = []     #Longitude of the location, for the default ellipsoid
    lon = []     #Longitude of the location, for the default ellipsoid
    alt = []     #Height of the location, for the default ellipsoid


    for i in range(propNum):
        xyz = (myOrbitX[i], myOrbitY[i], myOrbitZ[i])                   #Xyz coord for each prop. step
        #now = time.Time(myT[i])                                         #UTC time at each propagation step
        cartrep = coord.CartesianRepresentation(*xyz, unit=u.m)         #Add units of [m] to xyz
        gcrs = coord.GCRS(cartrep, obstime=time.Time(myT[i]))           #Let AstroPy know xyz is in GCRS
        itrs = gcrs.transform_to(coord.ITRS(obstime=time.Time(myT[i]))) #Convert GCRS to ITRS
        loc = coord.EarthLocation(*itrs.cartesian.xyz)                  #Get lat/lon/height from ITRS
        lat.append(loc.lat.value)                                       #Create latitude list
        lon.append(loc.lon.value)                                       #Create longitude list
        alt.append(loc.height.value)           

    print('Lat:')
    print(lat)
    print('Lon:')
    print(lon)
    print('Alt:')
    print(alt)


    lonAlt_2D_array = np.matrix([lon] [alt])

    fit = np.polyfit(lat, lonAlt_2D_array, 2)

orbitPropandcoordTrans(520027712.00,0.000939,51.5777,172.5018,323.1066,173.4358,15.68522506)


Tags: thetofromimportfortimestepnp