;+
; NAME:
;	BCS_CAT
; PURPOSE:
;	To give information on the maximum count rate etc. in BDA files.
;
; CALLING SEQUENCE:  
;	BCS_CAT,data_file_name [,catalogue_file]
;
; INPUT:
;       DATA_FILE_NAME  Specifies the file(s) to be inspected. Do not include
;                       directories (standard data search paths used). Wild
;                       cards can be included.
;
;       CATALOGUE_FILE  if specified, gives the output file for the catalogue
;                       information.  Default = 'BCS_CAT.DAT'
;
;
; OUTPUT:
;	                Information is stored in the output file.
;               
;
; USE:    bcs_cat,'bda9110*.*'               catalogue the files of Oct 91
;                                            in the default output file
;                                            bcs_cat.dat
;
;         bcs_cat,'bda91*.*','all_91.dat'    catalogue 1991 data files and
;                                            output to file all_91.dat
;
;
; HISTORY
;	CDP     February  1993    Written
;       CDP     Update to fit into scheme of bcs_create_cat   Apr 93
;
;-

pro bcs_cat,raw_ff,outfile

;
;  set up list of files
;
ff  = file_list(data_paths(),raw_ff)
cat_list = strarr(n_elements(ff))
catter = 0
mcnts = fltarr(4)
;
;  if there are files then...
;
if ff(0) ne '' then begin

;
;  because of the way the data are stored on the disk, the
;  list in ff need not be in time order, so oblige
;  (can't get arrays to work in strmid??)
;
   fn =  strarr(n_elements(ff))
   for i=0,n_elements(ff)-1 do fn(i) = strmid(ff(i),strpos(ff(i),'BDA9')+3,11)
   ff = ff(sort(fn))
;
;  set up output file
;
   if n_elements(outfile) lt 1 then outfile = 'bcs_cat.dat'
   openw,lun,outfile,/get_lun
   printf,lun,'                                       UT        '+$
              '           Maximum counts'
   printf,lun,' File name   Week      Day      Start      End'+$
              '       FeXXVI FeXXV  CaXIX   SXV'
   printf,lun,' '

   for i=0,n_elements(ff)-1 do begin
;
;  check for compressed file
;
      qc = strpos(strupcase(ff(i)),'Z')
      if qc ge 0 then begin
         compressed = 1
         break_file,ff(i),dsk,dir,fil,ext,ver
         print,'Dealing with compressed file ',ff(i)
         ff(i) = fil+ext
         print,'Unpacking - ',' ',ff(i)
;
;  note that bcs_unpack requires the _Z identifier, but the VMS unpack 
;  program will replace it in the YDX directory, this time without the _Z, so 
;  readjust the filename afterwards.
;
         bcs_unpack,ff(i)
         ff(i) = file_list(data_paths(),fil+strmid(ext,0,5))
      endif else begin
         compressed = 0
      endelse

;
;  read them
;
      rd_roadmap,ff(i),roadmap
      if n_elements(roadmap) gt 2 then begin
;
;  avoid /zero untidiness
;
         nz = where(roadmap.dgi ne 0)
         if n_elements(nz) gt 1 then begin
;
;  pick up statistics for each channel
;
            for kk=0,3 do begin
;
;  to counts/sec
;
               cnts = (roadmap(nz).total_cnts(kk)*10.)/$
                          (roadmap(nz).dgi*0.125)

;
;  get sensible estimate of max (copied from bcs_24hr_plot)
;
               ymax = max(cnts)
               qy = where(cnts ge 0.0)
               for j=0,9 do begin
                  qymax = where(cnts gt 0.5*ymax)
                  if n_elements(qymax) le 2 and $
                      n_elements(qymax) lt n_elements(qy) then ymax = 0.5*ymax
               endfor
               ymax = max(cnts(where(cnts le 2.*ymax)))
               mcnts(kk) = ymax
            endfor
;
;  find start and stop times and week id
;
            tst = fmt_tim(roadmap(0))
            tend = fmt_tim(roadmap(n_elements(roadmap)-1))
            tim2orbit,tst,wid=week
;
;  duff roadmap trap
;
         endif else begin
            ymax = 0
            mcnts(*) = 0.   
            tst = ' ' & tend = ' ' & week = ' '
         endelse
      endif else begin
         ymax = 0
         mcnts(*) = 0.
         tst = ' ' & tend = ' '    & week = ' '
      endelse
      break_file,ff(i),dsk,dir,file,ext
      if total(mcnts) eq 0 then begin
         printf,lun,strmid(file,3,6)+ext,$
         '                ------------',format='(2a)'
      endif else begin
         printf,lun,strmid(file,3,6)+ext,'  ',week,'  ',tst,'  ',$
                    strmid(tend,11,8),'  ',fix(mcnts),$
                    format='(8a,4i7)'
      endelse
      print,strmid(file,3,6)+ext,'  ',week,'  ',tst,'  ',$
                 strmid(tend,11,8),'  ',fix(mcnts),$
                 format='(8a,4i7)'
   endfor
;
;  close output file
;
   free_lun,lun

endif else begin
  print,'No data files found for that specification > ',raw_ff
endelse




end

