;+
; NAME:
;       BCS_FREE_DATA
; PURPOSE:
;       To give information on the maximum count rate in BDA files, to list
;       those whose maximum count rate is below a user-supplied limit and to
;       delete them if requested.
;
; CALLING SEQUENCE:  
;       BCS_FREE_DATA,'file_name',limit[,chan=n,/delete,/compress,/no_bell]
;
; INPUT:
;       FILENAME        Specifies the file(s) to be inspected. Wild cards
;                       can be included, as can directories.
;       LIMIT           The count rate (c/s) to use as the threshold
;
; OPTIONAL INPUTS:
;       CHAN            if present and non-zero, specifies channel to 
;                       be used [Def=3; CaXIX]
;       COMPRESS        keyword if present will cause files with maximum
;                       count rates below threshold to be COMPRESSED.
;       DELETE          keyword if present will cause files with maximum
;                       count rates below threshold to be DELETED.
;       NO_BELL         switches offbell when confirmation required
;       LOG             log file to be used; if only switch specified, 
;                       defaults to filename 'free_data.log' 
;                       (Note: routine exits after search)
;
;
; OUTPUT:
;                       None                
;
; RESTRICTIONS:
;       The DELETE and COMPRESS keywords cannot be used together
;
; USE:
;   bcs_free_data,'bda9110*.*',50                    (just check CaXIX with
;                                                     limit of 50 c/s)
;
;   bcs_free_data,'yd2:[bda]bda91*.*',100,/compress  (check Ca XIX rate with
;                                                     limit of 100 c/s and
;                                                     compress all below limit)
;
;   bcs_free_data,'bda91*.*',100,chan=4,/delete      (check SXV rate with
;                                                     limit of 100 c/s and
;                                                      delete all below limit)
;
; HISTORY
;       CDP     Aug-92  Written
;       RDB  12-Nov-92  Added COMPRESS keyword
;       CDP     Jan-93  added NO_BELL keyword
;       CDP     Mar-93  Test for compressed file, look for Z only in file name
;                       not _Z to make OS independent.
;       RDB   2-Sep-93  Allowed directory specification in filename
;       RDB  20-Jan-94  Added logging option
;-

pro bcs_free_data,raw_ff,killim,chan=chan,$
        delete=delete,compress=compress,no_bell=no_bell,log=log

if keyword_set(delete) and keyword_set(compress) then begin
   bell
   print,'*** Keywords DELETE and COMPRESS cannot be used together ***'
   return
endif

;               default to compress
if not keyword_set(delete) and not keyword_set(compress) then compress=1

;
;  use channel requested or default to CaXIX
;
channam = ['FeXXVI','FeXXV','CaXIX','SXV']

if keyword_set(chan) then begin
   chan = chan - 1
endif else begin
   chan = 2
endelse
print,'Comparing ',channam(chan),' counts against limit of ',killim,' c/s',$
       format='(3a,i5,a)'

;       if logging, setup log file
if keyword_set(log) then begin
  log_name = 'free_data.log'
  sx = size(log)
  if sx(sx(0)+1) eq 7 then log_name=log         ;string supplied
  openw,lunk,log_name,/get_lun
  printf,lunk,'Files less than ',killim,' c/s  using search string: ',raw_ff
  print,' '
  print,'** Logging to file: ',log_name
  print,' '
endif

;
;  set up list of files
;
break_file,raw_ff,ddsk,ddir,dfil,dext
dpath = ddsk + ddir
raw_file = dfil+dext
if strlen(dpath) eq 0 then $
  ff  = file_list(data_paths(),raw_file) else $
  ff  = file_list(dpath,raw_file)

print,' '
print,'No of files located =',n_elements(ff)
print,' '
kill_list = strarr(n_elements(ff))
killer = 0
;
;  if there are files then...
;
if ff(0) ne '' then begin

  for i=0,n_elements(ff)-1 do begin

    qcompress = strpos(strupcase(ff(i)),'Z')
    if qcompress lt 0 then begin              ;file not compressed
;
;  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
;
;  to counts/sec
;
            cnts = (roadmap(nz).total_cnts(2)*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)))
;
;  duff roadmap trap
;
         endif else begin
            ymax = 0
         endelse
      endif else begin
         ymax = 0
      endelse
      print,'File  ',ff(i),'  maximum count rate = ',fix(ymax),format='(3a,i5)'
;
;  if insufficient counts then store file name
;
      if ymax lt killim then begin
         kill_list(killer) = ff(i)
         killer = killer + 1
         if keyword_set(log) then printf,lunk,ff(i),fix(ymax),format='(a,i7)'
      endif

    endif else begin                     ;file is compressed
      print,'*** File  ',ff(i),'  is already compressed ***'
    endelse

  endfor

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

;       if only logging, close file and exit
if keyword_set(log) then begin
  free_lun,lunk
  return
endif

;
;  inform of files found and give idea of their size and space saving
;

if killer gt 0 then begin
  method = 'COMPRESSION'
  if keyword_set(delete) then method = 'DELETION'
  print,' '
  print,'Following blocks would be saved by ',method,' for the'
  print,channam(chan),' channel with a threshold of: ',killim,' c/s.', $
        format = '(a,a,i4,a)'
  tot_big = 0
  get_lun,lun
  for i=0,killer-1 do begin
    openr,lun,kill_list(i)
    st = fstat(lun)
    big = st.size/512
    if keyword_set(compress) then big=big/2
    close,lun
    tot_big = tot_big + big
    print,'File #',i,' ',kill_list(i),'  ',big,' blocks.',$
                      format='(a,i4,a,a,a,i4,a)'
  endfor
  free_lun,lun
  print,' '
  under = ['=','=','=','=','=','=','=']
  bl = fix(alog10(tot_big))+1
  for j=0,6-bl do under(j) = ' '
  print,'Which would release a grand total of ',tot_big,' blocks.',$
        format='(a,i7,a)'
  print,'                                     ',under,format='(8a)'

;
; second chance to stop DELETION happening
;
  if keyword_set(delete) then begin
     ans = ' '
     if not keyword_set(no_bell) then bell
     read,'** DELETION WAS REQUESTED.**  OK to go ahead? (Y/N) > ',ans
     yesno,ans
     if ans eq 1 then begin
        print,' '
        get_lun,lun
        for i=0,killer-1 do begin
           openr,lun,kill_list(i),/delete
           close,lun
           print,'File ',kill_list(i),' DELETED.'
        endfor
        free_lun,lun
      endif
  endif

;
; second chance to stop COMPESSION happening
;
  if keyword_set(compress) then begin
     if not keyword_set(no_bell) then bell
     ans = ' '
     read,'** COMPRESSION WAS REQUESTED.**  OK to go ahead? (Y/N) > ',ans
     yesno,ans
     if ans eq 1 then begin
        print,' '
        for i=0,killer-1 do begin
           break_file,kill_list(i),dsk,dir,fil,ext
           cmd = 'compress '+dsk+dir+fil+ext
           print,cmd
           spawn,cmd
        endfor
     endif
  endif

endif else begin
  print,$
       'No files have a ',channam(chan),' count rate of ',killim,$
       ' c/s. or less.',format='(3a,i4,a)'
endelse
end
