;+
; NAME:
;     BCS_CREATE_CAT
;
; PURPOSE:
;     Create BCS catalogue of maximum counts and associated GOES events
;
; CALLING SEQUENCE:
;     IDL> bcs_create_cat, bda_file_spec, catalogue_file [,/keep]
;
; INPUTS:
;          bda_file_spec      - the specification of the BDA files to be used
;                               can use wild cards (standard search path used)
;                               eg. 'BDA9302*.*'  will catalogue Feb 93
;
; OPTIONAL INPUT PARAMETERS: 
;          None
;
; KEYWORD PARAMETERS:
;          Keep                -  if present, the temporary BCS and GOES lists
;                                 will not be deleted.
;
; OUTPUTS:
;          catalogue_file      -  name of catalogue file to be created
;
; OPTIONAL OUTPUT PARAMETERS:
;          None
;
; PROCEDURE:
;          Packages the cataloguing routines to do the whole process
;
; MODIFICATION HISTORY:
;          Written:   CDP April 93
;
;-

pro bcs_create_cat, infil, outfile, keep=keep

if n_params() lt 2 then begin
   bell
   print,'Use:  IDL> bcs_create_cat, bda_file_spec, out_file'
   print,' '
   return
endif
;
;  find the data files
;
infil = strupcase(infil)
if (strpos(strupcase(infil),'BDA') lt 0) then infil =  'BDA' + infil
ff  = file_list(data_paths(),infil)

;
;  check if any such files exist in temporary, decompression directory, and
;  delete them if they do as they will cause a user prompt about whether to 
;  overwrite or not.
;
break_file,ff,disk,dirs,fils,ext,ver
nx = where(dirs eq '[YOHKOH.BCS]')
if nx(0) ge 0 then begin
   for i=0,n_elements(nx)-1 do begin
      com = 'Delete ' + ff(nx(i))
      spawn,com
   endfor
endif
;
;  now trim orginal list to cut out any decompressed files
;
nx = where(dirs ne '[YOHKOH.BCS]')
if nx(0) ge 0 then ff = ff(nx)
;
;
;  if there are no files then return
;
if ff(0) eq '' then begin
   bell
   print,'No files found with that specification.'
   print,' '
   return
endif
;
;  start and end time in user format
;
break_file,ff(0),disk,dir,fil,ext,ver
t1 = fmt_tim(fid2ex(strmid(fil,3,6)+strmid(ext,0,5)))
break_file,ff(n_elements(ff)-1),disk,dir,fil,ext,ver
;
;  add 2 hours to start of last file to cover its time span
;
ex2int,fid2ex(strmid(fil,3,6)+strmid(ext,0,5)),m,d
m = m + 7200000L
if m gt 86400000L then begin
   d = d + 1
   m = m - 86400000L
endif
int2ex,m,d,t
t2 = fmt_tim(t)
print,'start and stop times are: ',t1,t2


;
; produce BCS BDA file summary
;
bcs_cat,infil,'temp.bcs'

;
;  create the GOES listing
;
pr_gev,t1,t2,outfile='temp.goes'

;
;  combine the two into output file
;
bcs_add_goes,'temp.bcs','temp.goes',outfile

;
;  tidy up if required
;
if not keyword_set(keep)then begin
   com = 'Delete temp.bcs;*'
   spawn,com
   com = 'delete temp.goes;*'
   spawn,com
endif

end
