【问题标题】:np.polyfit how to get lists into a 2D array for y inputnp.polyfit 如何将列表放入 y 输入的二维数组中
【发布时间】:2018-03-28 07:33:31
【问题描述】:

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

现在经度和高度在两个单独的列表中,我需要将它们组合成一个二维数组。我试过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]

期望的输出:

lonAlt_2D_array = ([-66.6, 21.3, 90.1, 147.4, -115.7] [368752.8, 371184.4, 357834.7, 375131.8, 375643.8])

fit = np.polyfit(lat, lonAlt_2D_array, 2)    #not sure if 2 is correct degree

完整代码:

'''
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(5, 20027712.00, 0.000939, 51.5777, 172.5018, 323.1066, 173.4358, 15.68522506)

【问题讨论】:

  • np.array([Lat, Lon])
  • 这确实可以将列表转换为数组。但是,我现在收到 np.polyfit() TypeError: expected x and y to have same length 和 len(lonAlt_2D_array) = 2 和 shape = (2,5) 的错误。长度需要为 5 才能匹配 x(纬度)。

标签: python arrays python-2.7 numpy multidimensional-array


【解决方案1】:

原来lonAltMatrix = [list(a) for a in zip(lon, alt)] 可以创建一个 5x2 矩阵。

对于 np.polyfit(),y 坐标的行数必须与 x 坐标的长度匹配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多