Skip to content

Instantly share code, notes, and snippets.

@StuartLittlefair
Created October 17, 2019 10:07
Show Gist options
  • Save StuartLittlefair/4ab7bb8cf21862e250be8cb25f72bb7a to your computer and use it in GitHub Desktop.
Save StuartLittlefair/4ab7bb8cf21862e250be8cb25f72bb7a to your computer and use it in GitHub Desktop.
Quick snippet to use astropy to convert from heliocentric times to barycentric and vice-versa
from astropy.coordinates import SkyCoord, EarthLocation
from astropy import units as u
from astropy.time import Time
def helio_to_bary(coords, hjd, obs_name):
helio = Time(hjd, scale='utc', format='jd')
obs = EarthLocation.of_site(obs_name)
star = SkyCoord(coords, unit=(u.hour, u.deg))
ltt = helio.light_travel_time(star, 'heliocentric', location=obs)
guess = helio - ltt
# if we assume guess is correct - how far is heliocentric time away from true value?
delta = (guess + guess.light_travel_time(star, 'heliocentric', obs)).jd - helio.jd
# apply this correction
guess -= delta * u.d
ltt = guess.light_travel_time(star, 'barycentric', obs)
return guess.tdb + ltt
def bary_to_helio(coords, bjd, obs_name):
bary = Time(bjd, scale='tdb', format='jd')
obs = EarthLocation.of_site(obs_name)
star = SkyCoord(coords, unit=(u.hour, u.deg))
ltt = bary.light_travel_time(star, 'barycentric', location=obs)
guess = bary - ltt
delta = (guess + guess.light_travel_time(star, 'barycentric', obs)).jd - bary.jd
guess -= delta * u.d
ltt = guess.light_travel_time(star, 'heliocentric', obs)
return guess.utc + ltt
@StuartLittlefair
Copy link
Author

Hi Ian,

What you've done is slightly incorrect. There are two issues; one is that the observer-provided t0 is on the TDB timescale, and you should initialise as such:

t = Time(dt, scale='tdb',  location=(str(INT_Lon) + 'd', str(INT_Lat) + 'd'))

The second is that the call to light_travel_time will not quite get the correct time, as it is meant to be used the other way around. This is what the second iteration in my functions is meant to correct for; guess the light_travel_time, use the guess to correct to UTC, and see if the corrected UTC converts to the given TDB. A version of your code using these fixes would look like:

def convert_tdb_to_utc(dt, targ):
    """
    Convert TDB to UTC
    'targ' is a SkyCoord object of the target star
    """
    t = Time(dt, scale='tdb',  location=(str(INT_Lon) + 'd', str(INT_Lat) + 'd'))
    td = t.light_travel_time(targ)
    guess = t - td
    # if guess is correct, what's the difference between the inferred BJD and the given BJD?
    delta = (guess + guess.light_travel_time(targ)).jd  - t.jd
    # apply this correction
    guess -= delta * u.d
    # return on UTC scale as date time
    return guess.utc.datetime

However, the correction involves a second call to light_travel_time and only provides a minor change in precision - around 1/100th of a second. So a cruder but faster version (x2) would just be

def convert_tdb_to_utc(dt, targ):
    """
    Convert TDB to UTC
    'targ' is a SkyCoord object of the target star
    """
    t = Time(dt, scale='tdb',  location=(str(INT_Lon) + 'd', str(INT_Lat) + 'd'))
    td = t.light_travel_time(targ)
    correct = t - td
    return correct.utc.datetime

@iwellaway
Copy link

iwellaway commented Feb 7, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment