问题
I am trying to get the cartesian position and velocity vectors for each propagation step in my orbit. I am using OrbitalPy http://pythonhosted.org/OrbitalPy/ to generate the orbit with classical Keplerian elements.
According to the documentation I should be able to get the state vectors (both position and velocity) from class orbital.utilities.StateVector
, but I get a Type Error: new() takes exactly 3 arguments (2 given)
Here is the code:
from scipy.constants import kilo
import orbital
from orbital import earth, KeplerianElements, Maneuver, plot, utilities
from orbital.utilities import Position, Velocity
import matplotlib.pyplot as plt
import numpy as np
#Orbit Setup
orbitPineapple = KeplerianElements.with_period(96 * 60, body=earth, i=(np.deg2rad(51.6)))
plot(orbitPineapple)
plt.show()
orbitPineapple
Out[23]: KeplerianElements(a=6945033.343911132,
e=0,
i=0.90058989402907408,
raan=0,
arg_pe=0,
M0=0.0,
body=orbital.bodies.earth,
ref_epoch=<Time object: scale='utc' format='jyear_str' value=J2000.000>)
prop1 = orbital.maneuver.PropagateAnomalyTo(M=1.00)
orbitX = orbitPineapple.apply_maneuver(prop1)
plot(orbitPineapple, title='Go Pineapple!')
plt.show()
orbital.utilities.StateVector(orbitPineapple)
TypeError Traceback (most recent call last)
<ipython-input-53-91fb5303082b> in <module>()
4 #print(orbital.utilities.StateVector.velocity(orbitPineapple))
5
----> 6 orbital.utilities.StateVector(orbitPineapple)
7 #orbital.utilities.StateVector.position(orbitPineapple())
8
TypeError: __new__() takes exactly 3 arguments (2 given)
回答1:
I don't use this package but the error is simple enough to diagnose. From the docs you can see that orbital.utilities.StateVector
takes two arguments; one for "position" and one for "velocity". When you do orbital.utilities.StateVector(orbitPineapple)
you only supply one argument (orbitPineapple
) whose value will be taken as representing "position". You need to supply velocity too.
As for the error ...takes exactly 3 arguments (2 given)
, python overestimates the number of required/passed arguments for class methods because it takes the self
parameter into account when it is counting them up. For example:
class Testing(object):
def __init__(self):
self.a = 2
def do_something(self, b):
self.a += b
obj = Testing()
obj.do_something(2, 3) # Clearly passing only 2 arguments
Gives:
TypeError: do_something() takes exactly 2 arguments (3 given)
So you can read the error as "takes 2 arguments but you only gave 1"
回答2:
It turns out that the issue is with OrbitalPy. One can only get state vectors when using the original orbit name.
In this case orbitPineapple.r
would return position (x,y,z) and orbitPineapple.v
would return (Vx,Vy,Vy).
The position and velocity vectors are update after each maneuver is applied just use the exact same line with the original orbit name print(orbitPineapple.r, orbitPineapple.v)
.
Additionally, a super useful feature that would have saved me hours, is you can just type a variable or function and name.
and hit the tab key and all the options are displayed.
来源:https://stackoverflow.com/questions/46350116/python-orbitalpy-traceback-error-cartesian-state-vectors-position-and-velocity-f