shbhuk / barycorrpy

Python version of Barycorr
GNU General Public License v3.0
37 stars 6 forks source link

Backwards time conversion (BJDTDB to JDUTC) #54

Open tronsgaard opened 2 years ago

tronsgaard commented 2 years ago

It would be really neat if this code could also do the backwards BJD conversion, like https://astroutils.astronomy.osu.edu/time/bjd2utc.html

shbhuk commented 2 years ago

Hi @tronsgaard, I agree, this would be a nice feature to have. I'm currently a bit short on time, but can try to get to this in the (hopefully) not-to-distant future!

Shubham

tronsgaard commented 2 years ago

Thanks! And no worries! In the meantime I figured out how to do this in astropy with pretty decent accuracy (took me a while to get rid of the 69s offset between UTC and TDB):

from astropy.time import Time
from astropy.coordinates import SkyCoord, EarthLocation

location = EarthLocation(lat='28.754d', lon='-217.88814d', height='2370.0m')
target = SkyCoord('11 30 14.52 +07 35 18.26', unit='hour,deg')

bjd_tdb = Time([2459500.0], format='jd', scale='tdb')

# From BJD_TDB to JD_TDB
jd_tdb = bjd_tdb - bjd_tdb.light_travel_time(target, kind='barycentric', location=location, ephemeris='jpl')

# From JD_TDB to JD_UTC (leap seconds + 32.18 s difference)
jd_utc = jd_tdb.utc 
print(jd_utc.jd)

# Convert back with barycorrpy to check accuracy
from barycorrpy.utc_tdb import JDUTC_to_BJDTDB
res,_,_ = JDUTC_to_BJDTDB(jd_utc.jd, ra=target.ra.deg, dec=target.dec.deg,
                          longi=location.lon.deg, lat=location.lat.deg, alt=location.height.value)
print(res)
print('Difference in seconds:', (res-bjd_tdb.jd)*86400)

Prints:

[2459500.00418141]
[2459500.00000025]
Difference in seconds: [0.02120286]

The location was required by the light_travel_time() function, but I believe it only makes a small difference.

shbhuk commented 2 years ago

That's awesome. I think that's essentially what I'll need to do, but perhaps iterate a couple times since the JPL ephemeris call requires the JDTT time standard, and not UTC.

Just a note, when calling JDUTC_to_BJDTDB, you should also specify the location of the observatory for maximum accuracy (perhaps that might help reduce the difference between the two?)

tronsgaard commented 2 years ago

Good point! I edited the example above. Indeed, it reduced the difference from 0.05 to 0.02 seconds.