diff --git a/skyfield/sgp4lib.py b/skyfield/sgp4lib.py index 8957ab5b..2f152cf0 100644 --- a/skyfield/sgp4lib.py +++ b/skyfield/sgp4lib.py @@ -103,7 +103,9 @@ def __init__(self, line1, line2, name=None, ts=None): else: year = two_digit_year + 1900 - self.epoch = ts.utc(year, 1, satrec.epochdays) + whole_days = int(satrec.epochdays) + seconds = (satrec.epochdays - whole_days) * DAY_S + self.epoch = ts.utc(year, 1, whole_days, 0, 0, seconds) self._setup(satrec) @@ -131,8 +133,8 @@ def from_satrec(cls, satrec, ts): # be unified to always use epochyr and epochdays. whole, fraction = divmod(satrec.jdsatepoch, 1.0) year, month, day = compute_calendar_date(whole) - day += 0.5 # convert integer Julian day into Julian date float - self.epoch = ts.utc(year, month, day + fraction + satrec.jdsatepochF) + seconds = (0.5 + fraction + satrec.jdsatepochF) * DAY_S + self.epoch = ts.utc(year, month, day, 0, 0, seconds) self._setup(satrec) return self diff --git a/skyfield/tests/test_earth_satellites.py b/skyfield/tests/test_earth_satellites.py index c23c2d8a..2b1478c1 100644 --- a/skyfield/tests/test_earth_satellites.py +++ b/skyfield/tests/test_earth_satellites.py @@ -207,3 +207,20 @@ def _at(self, t): True, True, True, False, True, True, True, False, # Last two fake sats can see each other. ] + +def test_tle_epoch_matches_sgp4_library(): + from numpy import asarray + from sgp4.api import Satrec + from skyfield.constants import DAY_S + line1 = '1 55910U 23035C 23076.01808579 -.00000195 00000-0 00000+0 0 9994' + line2 = '2 55910 44.0037 312.8770 0004210 247.2472 260.1626 14.90311335 06' + ts = api.load.timescale(builtin=True) + satrec = Satrec.twoline2rv(line1, line2) + sat = EarthSatellite(line1, line2, ts=ts) + fraction = sat.epoch.tai_fraction - sat.epoch._leap_seconds() / DAY_S + offset_s = ((sat.epoch.whole - satrec.jdsatepoch) + + (fraction - satrec.jdsatepochF)) * DAY_S + assert abs(offset_s) < 1e-9 + _, r_library, _ = satrec.sgp4(satrec.jdsatepoch, satrec.jdsatepochF) + r_epoch, _, _ = sat._position_and_velocity_TEME_km(sat.epoch) + assert abs(asarray(r_epoch) - r_library).max() < 1e-12