C+
C NAME:
C	Time2YDoy
C PURPOSE:
C	Convert time to year and day of year and v.v.
C CATEGORY:
C	gen/for/lib
C CALLING SEQUENCE:
	subroutine Time2YDoy(id,t,yr,doy)
C INPUTS:
C	id		integer		=0  time to year/doy
C					=1  year/doy to time
C	t(2)		integer		id=0: times relative to current time origin
C	yr		integer		id=1: year
C	doy(2)		integer		id=1: day of year (doy(2) is # fract-day units)
C OUTPUTS:
C	yr		integer		id=0: year
C	doy(2)		integer		id=0: day of year (doy(2) is # fract-day units)
C	t(2)		integer		id=1: times relative to current time origin
C CALLS:
C	Time2TwoThousand, Time2Standardize
C PROCEDURE:
C	Dates before 5 october 1582 are interpreted as Julian dates;
C	after 15 october as Gregorian dates.
C	5 october 1582 (Julian) = 15 october 1582 (Gregorian).
C
C	RJD	'relative' Julian day = (Julian day - 2451544.5)      
C	J2000.0 = 2000 January 1.5d TDB = JD 2451545.0
C	JD 2451544.5 = 2000 January 1.0d (i.e. 1 January, midnight)
C	Check:	JD 2398220.0 =  1854 Jan.  1.5d
C		JD 2299161.0 =  1582 Oct. 15.5d (Gregorian calender)
C			     =  1582 Oct.  5.5d (Julian calender)
C				(Doy = 278.5)
C		JD       0.0 = -4712 Jan.  1.5d
C
C	JEpoch 1900 = JD 2415020.0 = Jan 0.5 1900
C	JEpoch 2000 = JD 2451545.0 = Jan 1.5 2000
C
C	BEpoch = 1900+(JD -2415020.31352)/365.242198781
C	       = 1900+(RJD+  36524.18648)/365.242198781
C	Check:  BEpoch 1950.0 = JD 2433282.423 = Jan 0.923 1950
C		BEpoch 1982.0 = JD 2444970.174 = Jan 0.674 1982
C
C MODIFICATION HISTORY:
C	OCT-2003, Paul Hick (MPAE,UCSD/CASS; pphick@ucsd.edu)
C-
	    integer	id
	    integer	t(2)
	    integer	yr
	    integer	doy(2)

	parameter	(I365  =  365)
	parameter	(I1582 = -418)	!1582-2000

	if (id .eq. 0) then				! Time to Yr, Doy

	    call Time2TwoThousand(0,t,doy)		! Shift origin to 2000 Jan 1, 0 UT

	    Laps = 0
	    idoy = doy(1)

	    do while(abs(idoy) .gt. 365)

		Laps = Laps+idoy/366		! Underestimate number of full years in iDoy
		i = Laps
		if (Laps .gt. 0) i = Laps-1
		j = max(I1582,i)
		Leap = i/4-j/100+j/400
		if (Laps .gt. 0) Leap = Leap+1	! # leap years

		idoy = doy(1)-Laps*I365-Leap
		if (Laps .le. I1582) idoy = idoy-10

	    end do

	    !-----
	    ! Leave do while loop with -365<=iDoy<=365. Doy <= 0 happens for dates earlier
	    ! than 2000 Jan. 1.0; Doy >=0 for dates later than 2000 Jan 1.0.

	    yr = 2000+Laps
	    if (idoy .lt. 0) then
		yr = yr-1
		if (yr .eq. 1582) then
		    idoy = idoy+355		! 355: # days in 1582
		    if (idoy .lt. 0) yr = yr-1
		end if
	    end if

	    Leap = 0				! Suppose it's not a leap year
	    if (yr/4*4 .eq. yr .and. (yr .lt. 1582 .or. !It's a leap year
     &		 yr/100*100 .ne. yr .or. yr/400*400 .eq. yr)) Leap = 1
	    if (idoy .lt. 0) idoy = 365+Leap+idoy	! Now iDoy>=0

	    if (idoy .eq. 365+Leap) then
		idoy = 0
		yr = yr+1
	    end if

	    doy(1) = idoy+1

	else					! Yr, Doy ---> Time

	    call Time2Standardize(doy,doy)

	    Laps = yr-2000			! Whole years from 2000, 1 Jan 1, 0 UT to idem of iYr
	    i = Laps
	    if (Laps .gt. 0) i = Laps-1
	    j = max(I1582,i)
	    Leap = i/4-j/100+j/400
	    if (Laps .gt. 0) Leap = Leap+1	! # leap years

	    t(1) = I365*Laps+Leap+doy(1)-1	! Rel. to 2000 Jan. 1, 0 UT
	    if (yr .le. 1582) t(1) = t(1)+10

	    t(2) = doy(2)

	    call Time2TwoThousand(1,t,t)	! Shift to current time origin

	end if

	return
	end
