	FUNCTION find_nearest_fid, tfid, fids
;+							6-May-94
;	NAME:    find_nearest_fid
;	Purpose: Using the input fid "tfid" find and return the index 
;		 to the nearest fid from the list of fileids fids.
;	Calling Sequence:
;		 ifid = nearest_fid(testfid, fids)
;	Input:
;		 testfid	fileid in the format of yymmdd.hhmm
;		 fids		list of file ids
;	Return:
;		the index of the nearest file id.
;
;	History: 
;		14-Aug-04  rdb written, based on nearest_fid without Yohkoh stuff
;-

hit = where(fids eq tfid, nhits)
if (nhits ge 1) then begin
  ret = hit(0)		;pass the 1st hit back
endif else begin
  low = where(fids lt tfid, nlow)
  if (nlow gt 1) then low = low(nlow-1)	;nearest low

  high= where(fids gt tfid, nhigh)
  if (nhigh gt 1) then high = high(0)	;nearest high

  if high eq 0 and low eq -1 then low=0
  if low eq n_elements(fids)-1 and high eq -1 then high = low
          

  deltasec = int2secarr(fid2ex([fids(low(0)),fids(high(0))]),fid2ex(tfid))
  nearesti = where(abs(deltasec) eq min(abs(deltasec)))
  if (nearesti(0)) then begin
    ret = high			;got index=1 or high val.
  endif else ret = low		;got index=0 or low val.
endelse

return, ret
end	
