Python: Julian date

As an astronomer a fast an easy way to calculate the Julian date of an observation is always handy. As I’m currently doing a bunch of python demos and adding some new libraries to some of my tools I though I’d share this one. Oh and if the first sentence confused you: The Julain date or JD is the the interval of time in days and fractions of a day since January 1, 4713 BC Greenwich noon. Now this is how you calculate it:

def julian_date(YY,MM,DD,HR,Min,Sec,UTcor):
return 367*YY – (7*(YY+((MM+9)/12))/4) + (275*MM/9)+ DD + 1721013.5 + UTcor/24 – 0.5*sign((100*YY)+MM-190002.5) + 0.5 + HR/24.0 + Min/(60.0*24.0) + Sec/(3600.0*24.0)

So all you need to do is supply the year (YY), month (MM), day (DD), hour (HR), minute (Min), Second (Sec) and the time difference from UT (UTcor) and their is your JD. So for example:

YY = 2011 #year
MM = 9 # month
DD = 7 #day
UTcor = 0 #ut offset
HR = 12 #hour
Min = 53 #minute
Sec = 0
LONG = 0 #degrees, -ve if West +ve if East
latitude = 0 #The latitude of the telescope; a scalar
JD = julian_date(YY,MM,DD,HR,Min,Sec,UTcor)

You can also grab the function from: jd.py.
I’m probably going to put a php version up on the webpage for people to use too…

Advertisements

3 responses to “Python: Julian date”

  1. Martin says :

    Why do all the computations yourself and not let python do it ?

    Especially that your solution seems to consider year 1800 as a leap year, which it is not…

    For example with the datetime objects:

    from datetime import datetime

    def jdays(utc_time):
    dt = utc_time - datetime(2000, 1, 1, 12, 0)
    return (dt.days + (dt.seconds + dt.microseconds / (1000000.0)) / (24 * 3600.0) + 2451545)

    t = datetime(2011, 9, 7, 12, 53, 0)

    print jdays(t)

    • samuel says :

      The idea was to avoid having to use any other functions – as at that point it time I was going through a period of avoiding other modules. I like your solution. I’m not sure about the leap year issue, but when I get a moment I’ll have a look.

    • aj says :

      You forget that Julian dates have no leap year exceptions like Gregorian dates do. So 1800 was a leap year in Julian terms.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: