Raise a more specific error when the date is not covered (#36)

This commit is contained in:
Jérôme Deuchnord 2020-01-04 21:29:35 +01:00 committed by Brandon Rhodes
parent fa1f6f2b19
commit 2ad36b41d2
2 changed files with 28 additions and 2 deletions

20
jplephem/exceptions.py Normal file
View File

@ -0,0 +1,20 @@
"""A set of special exceptions that can be thrown by the jplephem library"""
class OutOfRangeTimestampError(ValueError):
"""
This exception is thrown if any input times are out of the range of
times jplephem can compute ephemeris for.
It has for properties:
- `message` is a string explaining what happened,
- `min_timestamp` and `max_timestamp` are floats giving the minimum and
maximum supported times,
- `out_of_range_times` is an array of booleans where `True` means the
corresponding date in the input array is out of range and `False` means
it is correct.
"""
def __init__(self, message, min_timestamp, max_timestamp, out_of_range_times):
self.message = message
self.min_timestamp = min_timestamp
self.max_timestamp = max_timestamp
self.out_of_range_times = out_of_range_times

View File

@ -6,6 +6,7 @@ http://naif.jpl.nasa.gov/pub/naif/toolkit_docs/FORTRAN/req/spk.html
from numpy import array, empty, empty_like, interp, rollaxis
from .daf import DAF
from .descriptorlib import reify
from .exceptions import OutOfRangeTimestampError
from .names import target_names
T0 = 2451545.0
@ -227,8 +228,13 @@ class Segment(BaseSegment):
if (index < 0).any() or (index > n).any():
final_epoch = init + intlen * n
raise ValueError('segment only covers dates %.1f through %.1f'
% (init, final_epoch))
raise OutOfRangeTimestampError(
'segment only covers dates %.1f through %.1f' % (init,
final_epoch),
min_timestamp=init,
max_timestamp=final_epoch,
out_of_range_times=[True if i < 0 or i > n else False
for i in index])
omegas = (index == n)
index[omegas] -= 1