pro dc_getfile, stime, etime, cinstr, cdir, files_found, error, $
    span_files=span_files, err_msg=err_msg
;+
; NAME:
;	dc_getfile
; PUROSE:
;	Called by DCFREAD to find the correct DC file to read based on
;	user-selected times.  Finds all of the DC files on the specified
;	directory (HXRBS$DATA if none specified) containing data for
;	specified instrument.  Saves start and end times for each file.
;	Finds any files that overlap with the start/end times specified
;	(or if just one time was specified, the files that contain that time).
;       If more than one file is found, or no file is found but several files
;	are close, the list of possible files is displayed and the user is
;	prompted for his selection.
; INPUTS:
;	STIME - User-specified start time in r*8 MILLIseconds since 79/1/1,0
;	ETIME - User-specified end time in r*8 MILLIseconds since 79/1/1,0
;	CINSTR - Instrument. Type of DC file to read. (HXR, GOE, V13, etc)
;	CDIR - Directory to search for files
;       SPAN_FILES - (keyword parameter) 1 means assume DC files are continuous
;       in time and if hit eof before reaching requested end time, open
;       the next sequential file and continue accumulating data. 0 otherwise.
; OUTPUTS:
;	FILES_FOUND - Name of file(s) containing times requested by user.
;       Includes directory specification.
;	ERROR - 0/1 indicates no error/error
;       ERR_MSG - ASCII string containing error message if error = 1
;
; MODIFICATION HISTORY:
;	Written by Eric Carzon and K. Tolbert August 1992
;       Mod. 3/36/93 by KT. For GOES data, call find_goes_data which will
;         determine if the disk with the requested data is available, and
;         return the file names and start and end times.
;       Mod 7/2/93 by KT to add err_msg keyword parameter.
;-
;---------------------------------------------------------------------------
error = 0
; If start time > end time, error, so getout
if(stime ne 0.d0 and etime ne 0.d0 and stime ge etime) then begin
   err_msg  = 'Error - Start time is >= end time'
   print, err_msg
   goto, error_exit
endif
if strmid(strupcase(cinstr),0,3) eq 'GOE' then begin
   ;print,atime(stime/1000)
   find_goes_data, stime, cdir, files, fstart, fend,error=error,err_msg=err_msg
   if error gt 0 then goto, error_exit
   f_count = n_elements(files)
   fstart = fstart * 1000.d0 & fend = fend * 1000.d0  ; convert to msec
endif else begin
   print,'File search directory is, ', cdir
   ; See if directory is a logical (have to get rid of colon if there is one
   ; before calling trnlog) so that we can print what the logical points to.
   ;ldir = cdir
   ;icolon = strpos (ldir, ':')
   ;if icolon ne -1 then ldir = strmid(ldir,0,icolon)
   ;logical = trnlog(ldir, log_trans)
   ;if logical then ldir = log_trans
   ;if ldir ne cdir then print, '  (which points to ',ldir, ')'
   ; Find all files on cdir starting with D and ending with cinstr.
   files = findfile (cdir + 'D*_*.' + cinstr, count=f_count)
   if (f_count eq 0) then begin
      err_msg = 'No files found.'
      print, err_msg
      goto, error_exit  ; no files found
   endif
   ; Open files that were found and save start and end times of files in
   ; milliseconds relative to 79/1/1,0
   fend = dblarr(f_count)
   fstart = dblarr(f_count)
   for i = 0,f_count-1 do begin
      on_ioerror, next_file
      openr, lun, files(i), /get_lun
      h = assoc(lun, intarr(2048))
      head = h(0)
      fstart(i) = ((head(886)-1) * 86400.d0 + long(head,1774)/1000.d0) * 1000.d0
      fend(i) = ((head(892)-1) * 86400.d0 + long(head,1786)/1000.d0) * 1000.d0
      on_ioerror, close_ok
      close, lun
      close_ok:
      on_ioerror, null
      free_lun, lun
   next_file:
   endfor
endelse
; Stop if etime is < first start time or if stime is > last end time
if ((etime ne 0.d0) and (etime lt min(fstart))) or $
   ((stime ne 0.d0) and (stime gt max(fend))) then begin
   err_msg = 'No files found containing requested time interval.'
   print, err_msg
   goto, error_exit
endif
; Sort files by start time
sorted = sort(fstart)
fstart = fstart(sorted)
fend = fend(sorted)
files = files(sorted)
;
stime1 = stime       ; set new start/end time variables to switch between
etime1 = etime       ;  stime/etime given and only stime or etime given
if (stime eq 0.d0) then stime1 = etime    ;stime1 = etime if only etime given
if (etime eq 0.d0) then etime1 = stime    ;etime1 = stime if only stime given
; Use where function to find union of (stime lt fend) and (etime gt fstart)
fget = where((stime1 lt fend and etime1 gt fstart),fget_count)
case fget_count of
  0: begin    ; No files overlap requested times
     err_msg = 'No files found containing requested time interval.'
     case 1 of
        stime1 lt fstart(0): begin
           print,'The earliest file is: '
           print, files(0),' (',strmid(atime((fstart(0)/1000.d0)),0,14),$
                  ' - ',strmid(atime((fend(0)/1000.d0)),0,14),')'
           goto,error_exit
        end
        etime1 gt fend(f_count-1): begin
           print,'The latest file is:  '
           print, files(f_count-1),$
                  ' (',strmid(atime((fstart(f_count-1)/1000.d0)),0,14),$
                  ' - ',strmid(atime((fend(f_count-1)/1000.d0)),0,14),')'
           goto,error_exit
        end
        else: begin
           ; If passed above two tests, then selected time(s) must be
           ; between two files.  Find two closest files and prompt for choice
           s = where(stime1 lt fend, s_count)    ; s(0) is upper bound
           e = where(etime1 gt fstart, e_count)  ; e(e_count-1) is lower bound
           if (s_count eq 0 or e_count eq 0) then begin
              err_msg = 'No files found containing requested time interval.'
              goto, error_exit
           endif
           fget_count = (s(0) - e(e_count-1)) + 1
           fget = e(e_count-1) + indgen(fget_count)
           stime = 0.d0
           etime = 0.d0
           print,'The time you have requested is not within any of the files ',$
                 'in your search directory.'
           print,'The closest files are:'
           goto,prompt_for_file
        end
     endcase
     end
  1: begin    ; if only one file matches then select it
     files_found = files(fget(0))
     goto,got_file
     end
  else: begin  ; More than one file overlaps times. list files and prompt
     ; for choice unless span_files keyword was set.  In that case, return
     ; all files (hopefully only two) that contain time range.
     if keyword_set(span_files) then begin
        files_found = files(fget)
        goto, got_file
     endif else begin
        err_msg = 'No files found containing requested time interval.'
        print, 'Time you have requested is contained in more than one file:'
        prompt_for_file:
        for j = 0,fget_count-1 do begin
           k = fget(j)
           print, j+1, ') ', files(k), ' ', $
              strmid(atime((fstart(k)/1000.d0)),0,14),$
              ' - ', strmid(atime((fend(k)/1000.d0)),0,14)
        endfor
        get_ifile:
        if strmid(strupcase(cinstr),0,3) eq 'GOE' then goto, error_exit
        on_ioerror, r_err
        ifile = 0
        read, 'Enter 0 for Quit, or number of the file you want.', ifile
        if ifile eq 0 then goto,error_exit
        if ifile lt 1 or ifile gt fget_count then begin
           r_err:
           print, 'Invalid choice.  Value must be between 1 and ',fget_count
           goto,get_ifile
        endif
        files_found = files(fget(ifile-1))
        on_ioerror, null
     endelse
  end
endcase
;
got_file:
print,'Selected file(s)  ',files_found(0)
n = n_elements(files_found)
if n gt 1 then for i=1,n-1 do print,'                  ',files_found(i)
if(stime eq 0.d0 and etime ne 0.d0) then etime = 0.d0
if(stime ne 0.d0 and etime eq 0.d0) then stime = 0.d0
goto, getout
;
error_exit:
error = 1
;
getout:
return&end