Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions skyfield/relativity.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,16 @@ def _compute_deflection(position, pe, rmass):

# Compute scalar factors.

fac1 = 2.0 * GS / (C * C * emag * AU_M * rmass)
fac2 = 1.0 + qdote
# Both divisors can be zero. `emag` is zero when the deflector sits
# exactly at the observer, and `fac2` is zero when the target lies
# exactly on the line from the observer through the deflector. In both
# cases the numerator is zero as well, so the deflection is zero, but
# `flag` cannot suppress the quotient afterwards: multiplying a NaN by
# False gives NaN, not zero. Left alone, that NaN travels on through
# apparent() into positions and times, and a NaN time then raises
# OverflowError when the caller converts it to a datetime.
fac1 = 2.0 * GS / (C * C * where(emag, emag, 1.0) * AU_M * rmass)
fac2 = where(flag, 1.0 + qdote, 1.0)

return flag * fac1 * (pdotq * ehat - edotp * qhat) / fac2 * pmag

Expand Down
32 changes: 31 additions & 1 deletion skyfield/tests/test_deflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

"""
from contextlib import contextmanager
from numpy import arange, diff
from warnings import catch_warnings, simplefilter
from numpy import arange, array, diff
from skyfield import relativity
from skyfield.api import load, wgs84
from skyfield.framelib import ecliptic_frame as ef
Expand Down Expand Up @@ -116,3 +117,32 @@ def test_earth_deflection_cutoff():
assert hprime[2] < 1.3e-8 # moment when nadir angle crosses 0.8
assert hprime[3] > 1.8e-8
assert hprime[4] > 1.8e-8

def test_deflection_along_the_line_of_sight_is_zero():
# When the target lies exactly on the line from the observer through a
# deflector, the divisor `1 + qdote` is zero -- and so is the numerator,
# because the deflection really is zero in that geometry. But the
# `flag` that detects the case cannot suppress the quotient afterwards:
# multiplying a NaN by False gives NaN, not zero. The NaN then travelled
# through apparent() into positions and times, and converting a NaN time
# to a datetime raised OverflowError instead of reporting a rise or set.
# See https://github.com/skyfielders/python-skyfield/issues/1086

pe = array([1.0, 0.0, 0.0]) # deflector, relative to the observer
positions = [
array([-2.0, 0.0, 0.0]), # target exactly behind the deflector
array([-1.0, 0.0, 0.0]),
array([-2.0, 1e-8, 0.0]), # just off the line, still inside the
array([-2.0, 1e-12, 0.0]), # arcsecond cone that `flag` matches
]
with catch_warnings():
simplefilter('error') # a divide-by-zero would fail the test
for position in positions:
d = relativity._compute_deflection(position, pe, 1047.3486)
assert (d == 0.0).all(), d

# A deflector sitting exactly at the observer makes `emag` zero, which
# is the other divisor in the same expression.
d = relativity._compute_deflection(
array([-9.5, 0.3, 0.1]), array([0.0, 0.0, 0.0]), 332946.050895)
assert (d == 0.0).all(), d