Skip to content

Commit

Permalink
Merge pull request #42 from sot/fix-precision-convert-secs-to-jd
Browse files Browse the repository at this point in the history
Fix a precision issue in convert_time_format
  • Loading branch information
taldcroft authored Nov 20, 2023
2 parents 7511e59 + f5208d1 commit 9a1f951
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
15 changes: 9 additions & 6 deletions cxotime/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import erfa
import numpy as np
from astropy.time.core import day_frac
from astropy.time.formats import TIME_FORMATS, TimeYearDayTime, _parse_times

from .cxotime import CxoTime, TimeGreta, TimeMaude
Expand Down Expand Up @@ -264,12 +265,14 @@ def convert_jd1_jd2_to_date(jd1, jd2):


def convert_secs_to_jd1_jd2(secs):
jd2 = 0.0
try:
jd1 = secs / 86400.0 + 2450814.5
except TypeError:
# For a list input
jd1 = np.array(secs, dtype=np.float64) / 86400.0 + 2450814.5
if not isinstance(secs, (float, np.ndarray)):
secs = np.asarray(secs, dtype=float)

day, frac = day_frac(secs, 0.0, divisor=86400.0)

# CxoTime("1998:001:00:00:00.000").jd1,2
jd1 = 2450814.0 + day
jd2 = 0.5 + frac

# In these ERFA calls ignore the return value since we know jd1, jd2 are OK.
# Checking the return value via np.any is quite slow.
Expand Down
6 changes: 1 addition & 5 deletions cxotime/tests/test_cxotime.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,14 +416,10 @@ def test_convert_functions(fmt_val, val_type, fmt_out):
exp = getattr(CxoTime(val, format=fmt_in), fmt_out)
out = convert_time_format(val, fmt_out, fmt_in=fmt_in)

exp_kind = np.asarray(exp).dtype.kind
val_kind = np.asarray(val).dtype.kind

assert type(exp) is type(out)
if exp_kind == "f":
assert np.allclose(exp, out, rtol=1e-12)
else:
assert np.all(exp == out)
assert np.all(exp == out)

if (
fmt_in in cxotime.convert.CONVERT_FORMATS
Expand Down

0 comments on commit 9a1f951

Please sign in to comment.