;+
; NAME:
;	PROCESS_BCS_CALS
; PURPOSE:
;	Uses source log to process all BCS cals in log
; CATEGORY:
; CALLING SEQUENCE:
;	.run process_bcs_cals
; INPUTS:
;	Source of input file (local or on $DIR_BCS_CALS) requested.
;	User must specify if stims are to be processed (def=source cal)
; OPTIONAL INPUT PARAMETERS:
;	Can create log and plot files
;	  set PLOT = 1 for plot (set_plot to PS for PostScript files)
;	  set LOG = 1  for logging of the results to default files
;	Debug mode assumed by default (just checks existance of files!)
;	  set DEBUG = 0 for normal running
; KEYWORD PARAMETERS:
; OUTPUTS:
; OPTIONAL OUTPUT PARAMETERS:
;	Creates log and plot files if requested
; COMMON BLOCKS:
; SIDE EFFECTS:
; RESTRICTIONS:
; PROCEDURE:
; MODIFICATION HISTORY:
;	RDB  19-May-94	Written from seperate routines
;	RDB  18-Aug-94  Some improvment to documentation
;	RDB  09-Feb-95	Allowed use of general input file
;-

if n_elements(log) eq 0 then print,'** No Logging **'
if n_elements(plot) eq 0 then print,'** No Plotting **'
if n_elements(debug) eq 0 then debug=1
if debug eq 1 then print,'** Assuming debug mode, set debug=0 for normal **'
print,' '

yesnox,'* Use local log file as input [def=$DIR_BCS_LOGS]',local
log_dir = '$DIR_BCS_LOGS'
if local then log_dir = curdir()

yesnox,'* Process stim data [def is source cals]',stims
search  = 'SOURCE'
if stims then search = 'STIM'

use_def = 1
cal_logx = ''
if local then yesnox,'* Use default names',use_def
cal_log = 'source_log.dat'
if stims then cal_log = 'stim_log.dat'
if not use_def then begin
  read,'* Enter Input file name: ',cal_log
  cal_logx = cal_log+'_'+strlowcase(search)
endif
set_logenv,'CAL_LOG',cal_logx
  

slog = concat_dir(log_dir,cal_log)
print,' '
print,'Using Cal Input File: ',slog
print,'Using Output File:    ',get_logenv('CAL_LOG')
yesnox,'* Continue',ans
if not ans then stop

openr,lunq,slog,/get_lun
srq=''


;	loop through the files in the input log, processing each and 
;	logging the results if required

while not eof(lunq) do begin

  readf,lunq,srq
  print,' '
  print,srq
  ss1=str2arr(strcompress(srq),delim=' ')

;	skip past record in input file of the wrong type...
  if strpos(ss1(0),search) lt 0 then begin
    print,'** Skipping: ',ss1(0),'  ',ss1(1)
    goto, next_file
  endif

  infil = ss1(1)
  if stims then begin
    durn = long(ss1(6))
    print,infil,durn
;    if durn lt 30 or durn gt 700 then goto,next_file
  endif

;	shortcut search at dogwood...
 if strpos(get_logenv('HOME'),'dogwood') gt 0 then begin
  ss = '/yd*/*/'
  qq = concat_dir(ss,infil)
  qqj = findfile(qq,count=count)
  if count eq 0 then goto, next_file

 endif else begin
  ss = [curdir(),data_paths()]
  qq = concat_dir(ss,infil)

  qqj = ''
  for j=0,n_elements(qq)-1 do qqj=[qqj,findfile(qq(j))]

  rqj = where(strlen(qqj) gt 0)
  if rqj(0) eq -1 then goto,next_file

  qqj = qqj(rqj)
 endelse

  print,qqj
  ff = qqj(0)
  print,' '
  print,'File found: ',ff

  if debug then goto,next_file		;debug mode

  rd_bda_dp,ff,dp_sync
  rd_qs,ff,qs

  if stims then begin
    rd_bda,ff,-1,index,data
    anal_stims,infil,index,data,dp_sync,qs,log=log,plot=plot

  endif else begin
    anal_bcs_pha,infil,dp_sync,qs,log=log,plot=plot

  endelse

next_file:
endwhile

free_lun,lunq

end
