# Function wrapper for built-in 'jdn' function. This function checks the
#   parameters passed to the two versions of the 'jdn' function:
#
#   jdn
#
#   or
#
#   jdn()
#
#   or
#
#   jdn(Julian_Day_Number)
#
#   or
#
#   jdn(year,month,day)
#
#   The built-in variable 'Gregorian' is utilized by the 'jdn' function to determine whether
#   the Julian or Gregorian calender is used for computation. The user must set this
#   variable. The default value is TRUE, i.e., use the Gregorian calender. This is suitable
#   for current dates or dates after the transition from the Julian to Gregorian calender.
#   However the user must remember the effective dates to set this variable. This function
#   examines the paramters passed and sets the variable "Gregorian" automatically.
#
#   Julian Day Numbers (JDN) are used by
#   astronomers as a date/time measure independent of calendars and
#   convenient for computing the elapsed time between dates.  The JDN
#   for any date/time is the number of days (including fractional
#   days) elapsed since noon, 1 Jan 4713 BC.  Julian Day Numbers were
#   originated by Joseph Scaliger in 1582 and named after his father
#   Julius, not after Julius Caesar.  They are not related to the
#   Julian calendar.
#
#   For dates from 1 Jan 4713 BC thru 12 Dec Feb 32766 AD, jdn(year,month,day)
#   will give the JDN for noon on that date.  jdn(JDN) will compute
#   the year, month, and day from the JDN.  Years BC are given (and
#   returned) as negative numbers.  Note that there is no year 0 AD or 0 BC;
#   the day before Jan 1, 1 AD is Dec 31, 1 BC.  Note also that 1 BC,
#   5 BC, etc. are leap years.
#
#   Pope Gregory XIII decreed that the Julian calendar would end on
#   Oct 4, 1582 AD and that the next day would be Oct 15, 1582 in the
#   Gregorian Calendar, making Oct 5, 1582 in the Julian calender the same day
#   as Oct 15, 1582 in the Gregorian calender. The only other change is that centesimal
#   years (years ending in 00) would no longer be leap years
#   unless divisible by 400.  Britain and its possessions and
#   colonies continued to use the Julian calendar up until Sep 2, 1752,
#   when the next day became Sep 14, 1752 in the Gregorian Calendar.
#
#
function auto_jdn(...)
{
    local Transition_Year = 1582;           # Papal decree
    local Transition_Month = 10;            # Papal Decree
    local Transition_Day = 5;               # Papal Decree
    local Next_Transition_Day = 15;         # Papal Decree

#    local Transition_Year = 1752;           # UK & colonies & US
#    local Transition_Month = 9;             # UK & colonies & US
#    local Transition_Day = 3;               # UK & colonies & US
#    local Next_Transition_Day = 14;         # UK & colonies & US

    local max_Julian_JDN = 2299161;         # Papal Decree
#    local max_Julian_JDN = 2361211;         # UK & colonies & US

    local Gregorian_hold = Gregorian;
    local pdate;
    local ljdn;

    switch ( vargc ) {
        case 0:
            Gregorian = TRUE;
            ljdn = jdn();
            break;
        case 1:
            Gregorian = (vargv[1] >= max_Julian_JDN);   # set calender to use
            ljdn = jdn(vargv[1]);
            break;
        case 3:
            if ( vargv[1] == Transition_Year ) {
                local max_Julian_date = (((Transition_Year * 100) + Transition_Month) * 100) + Transition_Day;
                local min_Gregorian_date = (((Transition_Year * 100) + Transition_Month) * 100) + Next_Transition_Day;

                pdate = (((vargv[1] * 100) + vargv[2]) * 100) + vargv[3];
                # check illegal date range
                if ( (pdate > max_Julian_date) && (pdate < min_Gregorian_date) ) {
                    print "Incorrect date to 'auto_jdn' function";
                } ## endif

                Gregorian = (pdate > max_Julian_date);  # set calender to use
              } else if ( vargv[1] < Transition_Year ) {
                Gregorian = FALSE;
              } else {
                Gregorian = TRUE;
            } ## endif

            ljdn = jdn(vargv[1],vargv[2],vargv[3]);
            break;
        default:
            print "Error: Incorrect number of arguments to 'auto_jdn' function";
            print "       Correct forms are:";
            print "       auto_jdn or auto_jdn()";
            print "       auto_jdn(JDN)";
            print "       auto_jdn(year,month,day)";
            exit;
            break;
    } ## endswitch

    Gregorian = Gregorian_hold;

    return ljdn;
}