Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Julian Day
#1
Here is another Python piece of code derived from Jean Meuus' book "Astronomical Algorithms"
to compute the Julian Day (JD) for a given Gregorian date and time. This formula uses the
astronomical definition of JD in that it includes the year '0', whereas the historical
definition does not. If you enter a decimal day, you will not receive any more prompts, otherwise
you will be prompted for the hours (in military time), minutes and seconds UT. Comments and improvements
are appreciated, but NOT to the function "def gtoj"

from calendar import monthrange


###################################################################################################
# Convert a Gregorian Date to a Julian Day using Jean Meeus formula 7.1
###################################################################################################

def g2j(year, month, day):
    if month == 1 or month == 2:
        year -= 1
        month += 12
    if month > 2:
        year = year
        month = month
    if year >= 1582:
        a = int(year / 100)
        b = 2 - a + int(a / 4)
    else:
        b = 0

    jd = int(365.25 * (year + 4716)) + int(30.6001 * (month + 1)) + day + b - 1524.5
    return jd

###################################################################################################
# Convert day to decimal day
###################################################################################################


def day2decday(d_day, d_hour, d_min, d_sec):
    if d_hour >= 0:
        d_day = d_day + d_hour/24 + d_min/(24 * 60) + d_sec/(24 * 3600)
    return d_day

my_year = 0
my_month = 0
my_day = 0.0

while True:
    try:
        my_year = int(input('Please enter the 4 digit year (-4712 to 2100): '))
        if -4712 <= my_year <= 2100:    # Historical Julian date starts -4713 (no year 0 (zero)
            break
        else:
            print('\n WARNING: Year out of range, must be a whole number between -4712 and 2100 ')
    except ValueError:
        print('\n ERROR: Year must be numeric and a whole number between -4712 and 2100 ')

while True:
    try:
        my_month = int(input('Please enter the digit month (1 - 12): '))
        if 1 <= my_month <= 12:
            break
        else:
            print('\n WARNING: Month out of range, must be a whole number between 1 and 12 ')
    except ValueError:
        print('\n ERROR: Month must be numeric and a whole number between 1 and 12 ')

days_in_month = monthrange(my_year, my_month)

while True:
    try:
        my_day = float(input('Please enter the day: '))
        if 1.0 <= my_day <= days_in_month[1]:
            break
        else:
            print('\n WARNING: Day out of range, must be a number between 1 and {} '
                  .format(days_in_month[1]))
    except ValueError:
        print('\n ERROR: Day out of range, must be a number between 1 and {} '
              .format(days_in_month[1]))

if my_day > int(my_day):
    my_day = my_day
    greg2jul = g2j(my_year, my_month, my_day)
else:
    print('\nEnter Greenwich UTC time')
    my_hour = int(input('Enter the hour (24 hour clock): '))
    my_min = int(input('Enter the number minutes: '))
    my_sec = int(input('Enter the number of seconds: '))

    dec_day = day2decday(my_day, my_hour, my_min, my_sec)
    greg2jul = g2j(my_year, my_month, dec_day)

print('\nJulian Day: ', str(greg2jul))

# 1957 October 4.81 should = 2436116.31
# 333 January 27 at 1200 hrs UT = 1842713.0
#
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition


Messages In This Thread
Julian Day - by sparkz_alot - Oct-06-2016, 03:39 PM
RE: Julian Day - by j.crater - Oct-06-2016, 06:15 PM
RE: Julian Day - by sparkz_alot - Oct-06-2016, 08:34 PM
RE: Julian Day - by j.crater - Oct-06-2016, 08:38 PM
RE: Julian Day - by Skaperen - Oct-07-2016, 04:17 AM
RE: Julian Day - by sparkz_alot - Oct-07-2016, 02:25 PM
RE: Julian Day - by Skaperen - Oct-08-2016, 01:05 AM
RE: Julian Day - by sparkz_alot - Oct-08-2016, 01:41 AM
RE: Julian Day - by Skaperen - Oct-08-2016, 04:11 AM
RE: Julian Day - by sparkz_alot - Oct-08-2016, 02:09 PM
RE: Julian Day - by Skaperen - Oct-09-2016, 02:27 AM
RE: Julian Day - by sparkz_alot - Oct-09-2016, 02:37 PM
RE: Julian Day - by Skaperen - Oct-10-2016, 02:27 AM
RE: Julian Day - by sparkz_alot - Oct-12-2016, 04:42 PM
RE: Julian Day - by Skaperen - Oct-13-2016, 03:32 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020