PRO Date2DOY, idate, DOY, yr ; ------------------------------------------------------------ ;+ 18-Sep-91 ; NAME: ; Date2DOY ; PURPOSE: ; Convert yymmdd into DOY (day of the year). Input can ; be either string or integer. The year is an Optional ; return parameter. ; CALLING SEQUENCE: ; Date2DOY, idate, DOY [, yr] ; INPUT: ; idate input format for the date: yymmdd. ; Data-type can be either string or integer. ; OUTPUT: ; DOY integer with the day of the year. ; Output/Optional: ; yr year of the returned DOY. ; Note: If input data-type is string the returned values are ; string-type and if input-type is longword the returned ; parameters (DOY and yr) are integers. ; HISTORY: ; written by GAL 18-Sep-91 ;- ; ----------------------------------------------------------------- ; ON_ERROR, 2 ;force a return to caller on error ; Check data type of input set ascII flag and convert to yy,mm,dd: info = SIZE(idate) IF (info(0) eq 0) THEN BEGIN scalar = 1 ;scalar flag set ENDIF ELSE BEGIN scalar = 0 ;vector input ENDELSE IF (info(info(0) + 1) eq 7) THEN BEGIN ascII = 1 ;ascII input flag set yy = FIX(STRMID(idate,0,2)) ;extract year mm = FIX(STRMID(idate,2,2)) ;extract month dd = FIX(STRMID(idate,4,2)) ;extract day ENDIF ELSE BEGIN ;should be a longWord ascII = 0 ;non-ascII input sdate = STRTRIM(STRING(idate),2) ;convert to string yy = FIX(STRMID(sdate,0,2)) ;extract year mm = FIX(STRMID(sdate,2,2)) ;extract month dd = FIX(STRMID(sdate,4,2)) ;extract day ENDELSE ; Check for leap year and compute DOY: ; J F M A M J J A S O N D imonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] IF (scalar) THEN BEGIN ;scalar input IF ((yy MOD 4) eq 0) THEN BEGIN ;leap year imonth(2) = 29 ;set feb ENDIF DOY = FIX( TOTAL(imonth(0:mm-1)) ) + dd ENDIF ELSE BEGIN DOY = dd ;set correct len on vector leapYrs = WHERE( (yy MOD 4) eq 0) ;index of leap years nonLeap = WHERE( (yy MOD 4) ne 0) ;index of non-leap years IF (nonLeap(0) ne -1) THEN BEGIN FOR i=0, N_elements(nonLeap)-1 DO BEGIN DOY(nonLeap(i)) = FIX( TOTAL(imonth(0:mm(nonLeap(i))-1)) ) + $ dd(nonLeap(i)) ENDFOR ENDIF IF (leapYrs(0) ne -1) THEN BEGIN imonth(2) = 29 ;set feb FOR i =0, N_elements(leapYrs)-1 DO BEGIN DOY(leapYrs(i)) = FIX( TOTAL(imonth(0:mm(leapYrs(i))-1)) ) + $ dd(leapYrs(i)) ENDFOR ENDIF ENDELSE IF (N_PARAMS() EQ 3) THEN BEGIN ;pass year back to caller IF (ascII) THEN BEGIN DOY = STRTRIM( STRING(DOY), 2) ;convert to string yr = STRTRIM( STRING(yy), 2) ;convert to string ENDIF ELSE BEGIN yr = yy ENDELSE ENDIF ELSE BEGIN ;pass DOY only IF (ascII) THEN BEGIN DOY = STRTRIM( STRING(DOY), 2) ;convert to string ENDIF ENDELSE END