我創建了一個函數來生成和傳播衛星軌道。現在,我想將所有數據保存在.dat文件中。我不確定需要多少循環,或者完全可以如何操作。如何在.dat文件中寫入數據Python新行多個列表衛星軌道
我希望每個傳播步驟的時間,緯度,經度和高度都在一行上。
代碼數據:
myOrbitJ2000Time = [1085.0, 2170.0, 3255.0, 4340.1, 5425.1]
lat = [48.5, 26.5, -28.8, -48.1, 0.1]
lon = [-144.1, -50.4, -1.6, 91.5, 152.8]
alt = [264779.5, 262446.1, 319661.8, 355717.3, 306129.0]
在.dat文件所需的輸出:
J2000時間,緯度,經度,Alt鍵
1085.0, 48.6, -144.2, 264779.5
2170.0, 26.5, -50.4, 262446.1
3255.0, -28.7, -1.6, 319661.8
4340.1, -48.0, 91.5, 355717.2
5425.1, 0.1, 152.8, 06129.0
全碼:
個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
#def orbitPropandcoordTrans(orbitPineapple_J2000time, _ecc, _inc, _raan, _arg_pe, _meananom, meanMotion):
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)
######
#propNum = int(_avgPeriod)
'Generate Orbit'
#orbitPineapple = KeplerianElements.with_period(5560, body=earth, e=0.0004525, i=(np.deg2rad(51.6414)), raan=(np.deg2rad(247.1662)))
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()
#print('')
#print('')
'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
myOrbitJ2000Time = [] #J2000 times
#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)
#print('X:', 'Length:', len(myOrbitX))
#print(myOrbitX)
#print('Y:', 'Length:',len(myOrbitY))
#print(myOrbitY)
#print('Z:', 'Length:', len(myOrbitZ))
#print(myOrbitZ)
#print('J2000 Time:', 'Length:',len(myOrbitTime))
#print(myOrbitTime)
'''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('Current Time:', now)
#print('')
#print('UTC Time:')
#print(myT)
print('myOrbitJ2000Time', myOrbitJ2000Time)
print('')
print('Lat:')
print('')
print(lat)
print('')
print('Lon:')
print(lon)
print('')
print('Alt:')
print(alt)
orbitPropandcoordTrans(5,-34963095,0.0073662,51.5946,154.8079,103.6176,257.3038,15.92610159)
作爲一個便箋,我將這樣做一個更大的數據集,其中每個列表將有〜6000個值而不是5個,因此正在尋找最有效的方式。 – Rose
要清楚,你問的是如何將數據列寫成逗號分隔值?你是否熟悉Numpy和/或Astropy的Table類? – Iguananaut