当我尝试从我创建的类orbitPropandcoordTrans
运行我的函数interpolation
和Prop
时,我收到错误:
TypeError:必须使用调用unbound方法orbitPropandcoordTrans() prop instance作为第一个参数(改为使用int实例)
这是我创建课程的第一次尝试,我不太确定self
在哪里,以及哪些变量需要self.
。
在创建课程之前,当它们是两个独立的函数时,一切运行正常。
完整代码:
'''
Class with functions to:
-Create a sample orbit
-Propagate the orbit
-Convert the times from J2000 to UTC
-Convert the cartesian coordinates to latitude, longitude, and altitude
-Map a groundtrace
-Interpolate a 0-4 deg longitude window
-Save data
The orbit generation and propagation are done with OrbitalPy.
'''
import orbital
from orbital import earth, KeplerianElements, plot
import mpl_toolkits
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
from astropy.table import Table
from astropy.io import ascii
class Prop:
def orbitPropandcoordTrans(self, initial_alt, propNum, orbit_initial_J2000_time, ecc, inc, raan, arg_perig, meanAnom, meanMotion): #orbitPropandcoordTrans(Number of Propagations [], J2000 Times for original orbit [s], Eccentricity [], Inclination [deg], Right Ascension of the Ascending Node [deg], Argument of Perigee [deg], Mean Anomaly [deg], Mean Motion [revolutions per day]):
'''
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.
'''
'Calculate Avg. Period from Mean Motion'
avgPeriod = 86400 / meanMotion
'Generate Orbit'
# Period version
#orbit = KeplerianElements.with_period(avgPeriod, body=earth, e=ecc, i=(np.deg2rad(inc)), raan=(np.deg2rad(raan)), arg_pe=(np.deg2rad(arg_perig)), M0=(np.deg2rad(meanAnom))) #ref_epoch=
# Alt version
orbit = KeplerianElements.with_altitude(initial_alt, body=earth, e=ecc, i=(np.deg2rad(inc)), raan=(np.deg2rad(raan)), arg_pe=(np.deg2rad(arg_perig)), M0=(np.deg2rad(meanAnom))) #ref_epoch=
'Propagate Orbit and retrieve xyz'
x = np.empty(propNum, dtype=float) #X Coordinate for propagated orbit step
y = np.empty(propNum, dtype=float) #Y Coordinate for propagated orbit step
z = np.empty(propNum, dtype=float) #Z Coordinate for propagated orbit step
prop_time_J2000 = np.empty(propNum, dtype=float) #Time for each propagated orbit step
#propNum = 100 #Number of propagations and Mean Anomaly size (one orbit 2pi/propNum)
for i in range(propNum):
orbit.propagate_anomaly_by(M=(2.0*np.pi/propNum)) #Propagate the orbit by the Mean Anomaly
x[i] = orbit.r.x #x vals
y[i] = orbit.r.y #y vals
z[i] = orbit.r.z #z vals
prop_time_J2000[i] = orbit_initial_J2000_time #J2000 time vals
'Getting the correct J2000 Time'
array_initial_time = [orbit_initial_J2000_time ] * propNum #setting each element in times equal to the original J2000 time
orbit_J2000_time = np.empty(propNum, dtype=float) #J2000 times
for i in range(propNum):
orbit_J2000_time[i] = array_initial_time[i] + (avgPeriod/propNum)*i
'''Because the prop_time_J2000 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.'''
prop_time_utc = [] #UTC times for each propagation list
for i in range(propNum):
prop_time_utc.append((Time(2000, format='jyear') + TimeDelta(prop_time_J2000[i]*u.s)).iso) #Convert time from J2000 to UTC
'''Now I have xyz and time for each propagation step and need to convert the coordinates from
ECI to Lat, Lon, & Alt'''
xyz = np.empty(propNum, dtype=float) #Xyz coordinates from OrbitalPy initial orbit propagation
cartrep = np.empty(propNum, dtype=float) #Cartesian Representation
gcrs = np.empty(propNum, dtype=float) #Geocentric Celestial Reference System/Geocentric Equatorial Inertial, the default coord system of OrbitalPy
itrs = np.empty(propNum, dtype=float) #International Terrestrial Reference System coordinates
lat = np.empty(propNum, dtype=float) #Longitude of the location, for the default ellipsoid
lon = np.empty(propNum, dtype=float) #Longitude of the location, for the default ellipsoid
alt = np.empty(propNum, dtype=float) #Height of the location, for the default ellipsoid
for i in range(propNum):
xyz = (x[i], y[i], z[i]) #Xyz coord for each prop. step
cartrep = coord.CartesianRepresentation(*xyz, unit=u.m) #Add units of [m] to xyz
gcrs = coord.GCRS(cartrep, obstime=time.Time(prop_time_utc[i])) #Let AstroPy know xyz is in GCRS
itrs = gcrs.transform_to(coord.ITRS(obstime=time.Time(prop_time_utc[i]))) #Convert GCRS to ITRS
loc = coord.EarthLocation(*itrs.cartesian.xyz) #Get lat/lon/height from ITRS
lat[i] = loc.lat.value #Create latitude array
lon[i] = loc.lon.value #Create longitude array
alt[i] = loc.height.value
lat = np.array(lat)
lon = np.array(lon)
alt = np.array(alt)
orbit_J2000_time = np.array(orbit_J2000_time)
#return orbit_J2000_time, lat, lon, alt, avgPeriod, x, y, z, propNum, orbit_initial_J2000_time , ecc, inc, raan, arg_perig, meanAnom, meanMotion
return orbit_J2000_time, prop_time_utc, lat, lon, alt, x, y, z, propNum, orbit_initial_J2000_time, ecc, inc, raan, arg_perig, meanAnom, meanMotion, avgPeriod
def interpolation(self, orbit_J2000_time, lat, lon, alt):
'''
INTERPOLATION
'''
'Get lat, lon, alt, and time vals in a 10 degree window'
points_in_window = np.where(np.abs(lon-2)<15) #finds the times for the values of longitude that are in a 10 deg window centered around the 0-3 deg window
time_in_10deg_window = orbit_J2000_time[points_in_window] #change from 2D array to 1D array
#print 'time_in_10deg_window', time_in_10deg_window
lon_in_10deg_window = lon[points_in_window] #find the longitude vals in the 10 deg window
lat_in_10deg_window = lat[points_in_window] #find the latitude vals in the 10 deg window
alt_in_10deg_window = alt[points_in_window] #find the altitude vals in the 10 deg window
#print ''
#print 'time_in_10deg_window', time_in_10deg_window
#print ''
##prop_time_utc =
'Find fit coefficients for 10 deg window'
polynom_deg = 3 #polynomial degree of fit
fit_lon = np.polyfit(time_in_10deg_window, lon_in_10deg_window, polynom_deg) #get fit coefficients for longitude in the 10 deg window
fit_lat = np.polyfit(time_in_10deg_window, lat_in_10deg_window, polynom_deg) #get fit coefficients for latitude in the 10 deg window
fit_alt = np.polyfit(time_in_10deg_window, alt_in_10deg_window, polynom_deg) #get fit coefficients for altitude in the 10 deg window
#print 'fit_lon', fit_lon
'Find start and stop times of 0-4 deg window'
fit_time = np.polyfit(lon_in_10deg_window, time_in_10deg_window, polynom_deg) #get fit coefficients for TIME in the 10 deg window
#print 'fit_time', fit_time
#print ''
time_start = np.polyval(fit_time, 0) #start time for 0-4 deg window
time_stop = np.polyval(fit_time, 4) #stop time for 0-4 deg window
'Generate Spacecraft time in 0-4 deg window '
#spacecraft time in 0-4 deg window
time_04_window = np.arange(time_start, time_stop, 1) #generate 1 second cadence times within the 0-4 deg window
#print 'time_04_window', time_04_window
#print ''
'Interpolate for 0-4 deg window'
lon_04_window = np.polyval(fit_lon, time_04_window) #interpolated longitude vals for 0-4 deg window
lat_04_window = np.polyval(fit_lat, time_04_window) #interpolated latitude vals for 0-4 deg window
alt_04_window = np.polyval(fit_alt, time_04_window) #interpolated altitude vals for 0-4 deg window
return time_04_window, lon_04_window, lat_04_window, alt_04_window
'Run Functions'
orbit_J2000_time, prop_time_utc, lat, lon, alt, x, y, z, propNum, orbit_initial_J2000_time, ecc, inc, raan, arg_perig, meanAnom, meanMotion, avgPeriod = Prop.orbitPropandcoordTrans((400*1000), 100, 20027712.00, 0, 0, 172.5018, 323.1066, 173.4358, 15.68522506)
time_04_window, lon_04_window, lat_04_window, alt_04_window = Prop.interpolation(orbit_J2000_time, lat, lon, alt)