Skip to content

Commit

Permalink
include type checks in propagate_many
Browse files Browse the repository at this point in the history
(Addresses some of the issues in #8)
  • Loading branch information
Benjamin Winkel committed Nov 3, 2020
1 parent 0b626db commit 132e406
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
23 changes: 21 additions & 2 deletions cysgp4/cysgp4.pyx
Expand Up @@ -1835,8 +1835,14 @@ def _propagate_many_cysgp4(

int _sat_frame = 0 # 0: 'zxy', 1: 'xyz'

assert on_error in ['raise', 'coerce_to_nan']
assert sat_frame in ['zxy', 'xyz']
if on_error not in ['raise', 'coerce_to_nan']:
raise ValueError(
'Argument "on_error" must be one of "raise" or "coerce_to_nan"'
)
if sat_frame not in ['zxy', 'xyz']:
raise ValueError(
'Argument "sat_frame" must be one of "zxy" or "xyz"'
)

if sat_frame == 'xyz':
_sat_frame = 1
Expand Down Expand Up @@ -1919,6 +1925,19 @@ def _propagate_many_cysgp4(
# unfortunately, it is not possible in nogil loop to access
# the cdef'ed class members; therefore, we have to maintain
# arrays of pointers to the sgp4 and observer objects
if not isinstance(tle[i], PyTle):
print(type(tle[i]))
raise TypeError(
'Argument "tles" must be of type "PyTle" '
'(or list/array of "PyTle")'
)
if not isinstance(obs[i], PyObserver):
print(type(obs[i]))
raise TypeError(
'Argument "observers" must be of type "PyObserver" '
'(or list/array of "PyObserver")'
)

_tle_ptr_array[i] = (<PyTle> tle[i]).thisptr
_obs_ptr_array[i] = &(<PyObserver> obs[i])._cobj

Expand Down
51 changes: 51 additions & 0 deletions cysgp4/tests/test_cysgp4.py
Expand Up @@ -830,6 +830,57 @@ def test_propagate_many_suppress_error():
assert_allclose(res['eci_pos'], np.array([np.nan] * 3))


def test_propagate_many_argument_errors():

mjds = 56458.123
obs = PyObserver(6.88375, 50.525, 0.366)
tles = PyTle(*TLE_ISS)

propagate_many(mjds, tles, obs)

with pytest.raises(ValueError) as excinfo:
propagate_many(mjds, tles, obs, on_error='bla')

print(str(excinfo.value))
assert str(excinfo.value) == '"on_error" most be one of ["raise", "coerce_to_nan"]'

with pytest.raises(ValueError) as excinfo:
propagate_many(mjds, tles, obs, sat_frame='bla')

print(str(excinfo.value))
assert str(excinfo.value) == '"sat_frame" most be one of ["zxy", "xyz"]'

with pytest.raises(TypeError) as excinfo:
propagate_many(mjds, 1, obs)

print(str(excinfo.value))
assert str(excinfo.value) == 'Argument "tles" must be of type "PyTle" (or list/array of "PyTle")'

with pytest.raises(TypeError) as excinfo:
propagate_many(mjds, [tles, 1], obs)

print(str(excinfo.value))
assert str(excinfo.value) == 'Argument "tles" must be of type "PyTle" (or list/array of "PyTle")'

with pytest.raises(TypeError) as excinfo:
propagate_many(mjds, tles, 1)

print(str(excinfo.value))
assert str(excinfo.value) == 'Argument "observers" must be of type "PyObserver" (or list/array of "PyObserver")'

with pytest.raises(TypeError) as excinfo:
propagate_many(mjds, tles, [obs, 1])

print(str(excinfo.value))
assert str(excinfo.value) == 'Argument "observers" must be of type "PyObserver" (or list/array of "PyObserver")'

with pytest.raises(TypeError) as excinfo:
propagate_many('a', tles, obs)

print(str(excinfo.value))
assert "could not be cast" in str(excinfo.value)


def _propagate_prepare():

# url = 'http://celestrak.com/NORAD/elements/science.txt'
Expand Down

0 comments on commit 132e406

Please sign in to comment.