subr fits_time_decode, h
 ;scans a fits header and looks for a time
 ;4/22/99 version, still using a single string or byte array, will convert
 ;to strarr with the new fits readers
 nl = num_elem(h)
 if isstrarr(h) eq 0 then {
   hb = bmap(h)
   nl = nl/80
   redim, hb, 80, nl
 }
 time_flag = 0
 zeroifnotdefined, $data_time_tai, $data_xcen, $data_ycen, $data_xscale, $data_yscale, -
  $data_dist, $bscale, $bzero, $data_r_sun, $data_x0, $data_y0, $data_p_angle, $data_fdmg_flag, $data_dpc
 zeroifnotdefined, $instrument_type
 zero, $data_time_tai, $data_xcen, $data_ycen, $data_xscale, $data_yscale, -
  $data_dist, $bscale, $bzero, $data_r_sun, $data_x0, $data_y0, $data_p_angle, $data_fdmg_flag, $data_dpc
 zero, $instrument_type, $instrument_wave
 ;add $bscale and $bzero as regulars and $data_r_sun (2/26/2002), add $bunit (a string) 3/5/2010
 $bunit = ''
 ;also $data_x0 and $data_y0 (should be doing this in C)
 ;first check for TIME_TAI, always preferred
 for i=1,nl-1 do {
   if isstrarr(h) then s=h(i) else s=smap(hb(*, i))
   s=upcase(s)
   s2 = strskp(s, '=')
   s3 = s(0:7)
   if s3 eq 'DPC     ' then {
    ;might be a full disk mg, if so we set the $data_fdmg_flag
    s2 = strr(s2,'''','')
    $data_dpc = strtol(s2,0)
    dpc = $data_dpc and 0x0f000000
    if dpc eq 0x08000000 or dpc eq 0x09000000 then $data_fdmg_flag = 1
 	  } else
   if s3 eq 'INSTRUME' then {
     ;6/21/2010 - check for some common cases
     if strpos(s2,'AIA') ge 0 then $instrument_type = 6
     if strpos(s2,'HMI') ge 0 then $instrument_type = 5
     if strpos(s2,'SOT') ge 0 then $instrument_type = 3
     if strpos(s2,'TRACE') ge 0 then $instrument_type = 1
 	  } else
   if s3 eq 'WAVELNTH' then {
    $instrument_wave = float(s2)
 	  } else
   if s3 eq 'BSCALE  ' then {
    $bscale = float(s2)
 	  } else
   if s3 eq 'BZERO   ' then {
    $bzero = float(s2)
 	  } else
   if s3 eq 'R_SUN   ' then {
    $data_r_sun = float(s2)
 	  } else
   if s3 eq 'X0      ' then {
    $data_x0 = float(s2)
 	  } else
   if s3 eq 'Y0      ' then {
    $data_y0 = float(s2)
 	  } else
   if s3 eq 'P_ANGLE ' then {
    $data_p_angle = float(s2)
 	  } else
   if s3 eq 'TIME_TAI' then {
    $data_time_tai = double(s2)
    time_flag = 1		;indicates no more searching for time
	  } else
   ;also check for position
   if s3 eq 'XCEN    ' then {
    $data_xcen = float(s2)
 	  } else
   if s3 eq 'YCEN    ' then {
    $data_ycen = float(s2)
 	  } else
   if s3 eq 'XSCALE  ' then {
    $data_xscale = float(s2)
 	  } else
   if s3 eq 'YSCALE  ' then {
    $data_yscale = float(s2)
	 } else
   if s3 eq 'CDELT1  ' then {
    $data_xscale = float(s2)
	 } else
   if s3 eq 'CDELT2  ' then {
    $data_yscale = float(s2)
	 } else
    if s3 eq 'OBS_DIST' then {        ; added, LS 17apr00 (SOHO distance from Sun)
     $data_dist = float(s2)
 	  } else
    if s3 eq 'BUNIT   ' then {        ; added, 3/5/2010 BUNIT for MDI MG's
     $bunit = s2
 	  }
 }
 ;sometimes we get nan's for the scales (they actually have the NAN string in the
 ;key), zap any
 zeronans, $data_xscale 
 zeronans, $data_yscale 
 if time_flag then return
 ;try a bit harder for the time if we haven't succeeded yet
 date_flag = 0
 tzc = 0
 ;first try for DATE_OBS which has UTC all on one line (date and time)
 for i=1,nl-1 do {
   if isstrarr(h) then s=h(i) else s=smap(hb(*, i))
   s=upcase(s)
   s2 = strskp(s, '=')
   s3 = s(0:7)
   if s3 eq 'DATE_OBS' then {
     $data_time_tai = tai_from_date_obs(s2)
       ;;ty,'got time from DATE_OBS-OBS'
     return	;ok to leave now, don't need to check for others
   }
   ;for SDO we want (I think) a DATE-OBS that is different from earlier defs so
   ;11/2/2010 - switch to T_OBS (mid-exposure) since file names use it
   ;the DATE-OBS is the start time
   if s3 eq 'T_OBS   ' then {
     $data_time_tai = tai_from_date_obs(s2)
     return	;ok to leave now, don't need to check for others
   }
   ;have to be careful, it only has the date in earlier data and comes before DATE_OBS
   ;also try for individual DATEOBS (no underscore) and TIMEOBS
   ;try to accommandate some known cases
   if s3 eq 'DATEOBS ' then {
    decode_date_str, s2, year, dy
    date_flag = 1
   } else
   if s3 eq 'TIMEOBS ' then {
    decode_time_str, s2, ih, im, sec
    time_flag = 1
    } else
   if s3 eq 'TIMEZONE' then {
    ;try to accommondate a time zone
    s2 = upcase(s2)
    if strpos(s2, 'PACIFIC') ge 0 then tzc = 8
    if strpos(s2, 'EAST') ge 0 then tzc = 5
    if strpos(s2, 'MOUNT') ge 0 then tzc = 7
    if strpos(s2, 'MID') ge 0 then tzc = 6
    if strpos(s2, 'DAYLIGHT') ge 0 then tzc = tzc -1
    }
 }
 ;insist on both a date and a time if we didn't get a DATE_OBS
 ;;ty,'year,dy,ih,im,sec,tzc =', year,dy,ih,im,sec,tzc
 if time_flag and date_flag then $data_time_tai = tai_from_date(year,dy,ih+tzc,im,sec)
 endsubr
 ;======================================================= 
