C+ C NAME: C Time2YMD C PURPOSE: C Converts (Yr,Month,Day) to (Yr,Doy) and v.v. C CATEGORY: C gen/for/lib C CALLING SEQUENCE: subroutine Time2YMD(id,t,yr,mon,day) C INPUTS: C id integer =0 time to date (yr,month,day) C =1 date (yr,month,day) to time C t(2) integer id=0: times relative to current time origin C yr integer id=1: year; the year xxxBC should be entered as -xxx+1. C mon integer id=1: month (1,..,12) C day(2) integer id=1: day of month (day(2) is # fract-day units C OUTPUTS: C yr integer id=0: year; the year xxxBC should be entered as -xxx+1. C mon integer id=0: month (1,..,12) C day(2) integer day of month (day(2) is fraction of day C in units set by Time2SetOrigin2000. C C id = 1: C C t(2) integer times relative to current time origin C CALLS: C Time2YDoy C RESTRICTIONS: C >>> For ID=0 (date --> doy) the input for the month may be given in the form C of a char-string cMon or as an integer iMon. cMon takes precedence C over iMon: first cMon is checked for valid input; if cMon is not valid C iMon is used (if iMon is invalid too, the program is aborted). C >>> ADVICE: ALWAYS use cMon for input. C Upon output the values of cMon and iMon are always consistent. C The returned value for cMon is always in uppercase. C PROCEDURE: C Dates before 5 october 1582 are interpreted as Julian dates; after 15 C october as Gregorian dates. C >>> 5 october 1582 (Julian) = 15 october 1582 (Gregorian). C MODIFICATION HISTORY: C 1989-1990, Paul Hick (MPAE,UCSD/CASS; pphick@ucsd.edu) C- integer id integer t(2) integer yr integer mon integer day(2) integer days(12) /31,28,31,30,31,30,31,31,30,31,30,31/ if (id .eq. 0) then ! Time ---> Y,M,D call Time2YDoy(0,t,yr,day) iyr = yr Leap = 2 if (iyr/4*4 .eq. iyr .and. (iyr .lt. 1582 .or. iyr/100*100 .ne. iyr & .or. iyr/400*400 .eq. iyr)) Leap = 1 ! It's a leap year days(2) = 30-Leap ! Days in february idoy = day(1) if (idoy .lt. 1 .or. idoy .gt. 367-Leap) stop 'Time2YMD: invalid iDoy' if (iyr .eq. 1582) then if (idoy .gt. 355) stop 'Time2YMD: invalid iDoy for 1582' days(10) = 21 ! Subtract 10 day gap for october end if iday = idoy imon = 1 ! Suppose it's in january do while ((iday-1)/days(imon) .ge. 1) iday = iday-days(imon) ! Month iMon already past imon = imon+1 ! Try next month end do if (iyr .eq. 1582 .and. imon .eq. 10 .and. iday .ge. 5) iday = iday+10 mon = imon day(1) = iday else iyr = yr Leap = 2 if (iyr/4*4 .eq. iyr .and. (iyr .lt. 1582 .or. iyr/100*100 .ne. iyr & .or. iyr/400*400 .eq. iyr)) Leap = 1 ! It's a leap year days(2) = 30-Leap ! Days in february imon = mon iday = day(1) if (imon .lt. 1 .or. imon .gt. 12) stop 'Time2YMD: invalid month' if (iday .lt. 1 .or. iday .gt. days(imon)) stop 'Time2YMD: invalid day of month' i = imon-1 ! Number of months already past idoy = i*31-min(i/2,3)-max(0,(i-7)/2)+iday ! Day of year number if (i .ge. 2) idoy = idoy-Leap ! Correction for February if (iyr .eq. 1582 .and. idoy .gt. 278) then ! 278=15 oct (Greg.) if (idoy .lt. 288) stop 'Time2YMD: date in gap between calendars' idoy = idoy-10 ! Subtract 10 day gap end if t(1) = idoy t(2) = day(2) call Time2YDoy(1,t,yr,t) end if return end