;+
; PROJECT:
;	SDAC
; NAME: 
;       TIME_F_NAME
;
; PURPOSE:
;	This procedure locates BATSE files from their times.
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;
;
; CALLS:
;	TIME2FILE
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	Version 2, richard.schwartz@gsfc.nasa.gov 7-jun-1998.
;		cleaned up code to make it more robust and close open units not needed.
;-
pro time_f_name, asctime, dd_open, filename, error=error
;
;****************************************************************************

error = 0
 
;
filetype = ['BDB', 'FDB', 'CONT', 'DISCSP']
ftype = filetype(dd_open.type)

if ftype ne 'BDB' then begin

   ; File type is FDB, CONT, or DISCSP. Generate a name to search for files 
   ; based on the requested start time .

   batse_dir = 'BATSE_' + ftype

  
  file_arr = loc_file(path=batse_dir, $
	strlowcase(time2file(asctime,/year2digit,/date_only)+'*.'+ftype + '*') ,count=num_file)
  if num_file eq 0 then begin
    print,'No ' + ftype + ' files are available for this date.'
    goto, error_exit
  endif
;
; Narrow list of files for that day down to files that contain requested time.
; If requested time doesn't contain a time of day, then list all files
; for that day.
;
  savefiles = strarr(num_file)
  ndx = 0
  
  flare_secs = anytim(asctime,/sec)
  just_day = anytim(asctime,/sec,/date)
  for i=0,num_file-1 do begin
    if flare_secs eq just_day then goto,saveit
    fs_open, file=file_arr(i), dd_type=dd_open.type, dd_open=dd_test, $
       error=error, verbose=0
;    print,i,file_arr(i),dd_test.header.st_time,dd_test.header.en_time
    if error then goto,getout

    if (flare_secs+.1) ge dd_test.header.st_time and $
       (flare_secs-.1) le dd_test.header.en_time then begin
       saveit:
       savefiles(ndx) = file_arr(i)
       ndx = ndx + 1
    endif else free_lun,dd_test.lun
  endfor
;
  case ndx of
  0: begin
     print,'No ', filetype(dd_open.type-1) + $
        ' files are available for this time.'
     goto, error_exit
      end
    
  1: filename = savefiles(0)

  else: begin
     message = ['The following files contain the specified time or flare:', $
                'File name indicates start time of file (yymmdd_hhmm) and ', $
                'flare number xxx (.fdb_xxx) contained in file.', $
                '   ']
     prompt = 'Enter number of file to select, or press return to cancel.'
   
     choices = strarr(ndx)
     for i=0,ndx-1 do begin
        brack = strpos(savefiles(i),']') + 1
        choices(i) = strmid(savefiles(i), brack, 50)
     endfor
  
     user_choose, message, prompt, choices, selection
     if selection eq -1 then goto, error_exit

     filename = savefiles(selection)
     end
  endcase

endif else begin  ; file is BDB - only one per day
  filename = (time2file(asctime,/year2digit,/date_only)+'.bdb')(0)
endelse

goto, getout

error_exit:
error = 1

getout:

end
