Skip to content

Commit

Permalink
Speed up nsec_to_astropy by avoiding TimeDelta
Browse files Browse the repository at this point in the history
Constructing the TimeDelta is slow. Calculating the JD components
and explicitly adding them to the epoch JD values is much faster
than constructing a TimeDelta and adding that to a Time.
  • Loading branch information
timj committed Oct 2, 2024
1 parent 229753a commit 4b9e99a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions python/lsst/daf/butler/time_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,14 @@ def nsec_to_astropy(self, time_nsec: int) -> astropy.time.Time:
that the number falls in the supported range and can produce output
time that is outside of that range.
"""
jd1, jd2 = divmod(time_nsec, self._NSEC_PER_DAY)
delta = astropy.time.TimeDelta(float(jd1), float(jd2) / self._NSEC_PER_DAY, format="jd", scale="tai")
value = self.epoch + delta
return value
njd1, njd2 = divmod(time_nsec, self._NSEC_PER_DAY)
# We can use ._time.jd1 if we know that the reference epoch is also
# JD/TAI. Tom Aldcroft on Astropy Slack noted that this private
# attribute is effectively public and is much faster than
# using `time.jd1`.
jd1 = float(njd1) + self.epoch._time.jd1
jd2 = (float(njd2) / self._NSEC_PER_DAY) + self.epoch._time.jd2
return astropy.time.Time(jd1, jd2, format="jd", scale="tai", precision=6)

def times_equal(
self, time1: astropy.time.Time, time2: astropy.time.Time, precision_nsec: float = 1.0
Expand Down

0 comments on commit 4b9e99a

Please sign in to comment.