;+
; NAME:
;       GET_BSA_ANS
; PURPOSE:
;       get BSA processing options
; CALLING SEQUENCE:
;       bsa_ans=get_bsa_ans(wid=wid)
; OUTPUTS:
;       BSA_ANS      - structure with options selected
; OPTIONAL INPUT KEYWORDS:
;       WID          - use widget option
;       CHANS,ACCUM, THRESH - channels, accumulations (secs), thresholds (counts)
; HISTORY:
;       1-Apr-93, written DMZ (ARC)
;      12-Apr-94, added additional keywords
;      16-Jun-94, added more error checking of inputs
;-

 function get_bsa_ans,wid=wid,chans=chans,accum=accum,thresh=thresh

 on_error,1

;-- use widget option?

 if keyword_set(wid) then begin
  wbsa,bsa_ans,err_flag=err
  if not err then begin
   if n_elements(thresh) eq 0 then begin
    chans=bsa_ans.chans
    chk=where(chans gt 0,nchans)
    message,'# of channels selected = '+string(nchans),/cont
    thresh=''
    read,'* enter count level THRESHOLD per channel [def= 0; no threshold]:', thresh
    if thresh eq '' then thresh=replicate(0,nchans) else thresh=str2arr(thresh)
    nthresh=n_elements(thresh)
    if nthresh eq 1 then begin 
     thresh=replicate(thresh,nchans)
     nthresh=nchans
    endif
    if nthresh ne nchans then message,'# of thresholds must equal # of channels'
    bsa_ans.thresh(chans(chk)-1)=thresh
   endif
   return,bsa_ans
  endif
 endif
 message,'using non-widget mode',/contin

;
;-- Channels
;

 if n_elements(chans) eq 0 then begin
  valid_chans=[1,2,3,4]
  repeat begin
   chans=''
   read,'* enter CHANNELS to process, separated by commas [def=2,3,4]:',chans
   if chans eq '' then chans=[2,3,4] else chans=str2arr(chans)
   match,chans,valid_chans,a,b
  endrep until (b(0) ne -1)
  chans=valid_chans(b)
 endif
 nchans=n_elements(chans)

;
;-- Accumulation times
;

 if n_elements(accum) eq 0 then begin
  accum=''
  read,'* enter ACCUMULATION times per channel,separated by commas [def= 9s]:',accum
  if accum eq '' then accum=replicate(9,nchans) else accum=str2arr(accum)
 endif

;
;-- Accumulation thresholds
;

 if n_elements(thresh) eq 0 then begin
  thresh=''
  read,'* enter count level THRESHOLD per channel [def= 0; no threshold]:', thresh
  if thresh eq '' then thresh=replicate(0,nchans) else thresh=str2arr(thresh)
 endif

 deadtime=1 & xcorr=1 & fluxcal=1 & wavecal=1 & errcal=0 & dt_interp=1
 print,'* following corrections/calculations will be made: '
 print,' '
 print,' Apply dead-time corrections            - YES'
 print,' Interpolate zero dead-time corrections - YES'
 print,' Apply crystal curvature corrections    - YES'
 print,' Flux calibrate spectra                 - YES'
 print,' Compute wavelength scale               - YES'
 print,' Compute Spectral uncertainties         - NO'
 print,' '
 ans='' & read,'* do you wish to change these [def=n]?: ',ans
 if ans eq '' then ans='N' else ans=strupcase(strmid(ans,0,1))
 print,' '
 if ans eq 'Y' then begin

;-- Deadtime corrections

  ans=''
  read,'* apply DEADTIME correction [def=y]?: ',ans
  if ans eq '' then ans='Y' else ans=strupcase(strmid(ans,0,1))
  if ans eq 'N' then deadtime=0
  

;-- if deadtime then begin
  
  if deadtime then begin
   read,'* interpolate zero DEADTIME correction [def=y]?: ',ans
   if ans eq '' then ans='Y' else ans=strupcase(strmid(ans,0,1))
   if ans eq 'N' then dt_interp=0
  endif

;-- Curvature corrections

  ans='' 
  read,'* apply crystal CURVATURE correction [def=y]?: ',ans
  if ans eq '' then ans='Y' else ans=strupcase(strmid(ans,0,1))
  if ans eq 'N' then xcorr=0

;-- Flux calibration

  ans='' 
  read,'* FLUX calibrate spectra [def=y]?: ',ans
  if ans eq '' then ans='Y' else ans=strupcase(strmid(ans,0,1))
  if ans eq 'N' then fluxcal=0
  if fluxcal and (not xcorr) then begin
   message,'curvature corrections must be applied before flux calibration',/cont
   fluxcal=0
  endif

;-- Wavelength calibration

  ans=''
  read,'* compute WAVELENGTH scale [def=y]?: ',ans
  if ans eq '' then ans='Y' else ans=strupcase(strmid(ans,0,1))
  if ans eq 'Y' then wavecal=1 else wavecal=0

;-- Uncertainty calculation

  ans=''
  read,'* compute spectral UNCERTAINTIES [def=n]?: ',ans
  if ans eq '' then ans='N' else ans=strupcase(strmid(ans,0,1))
  if ans eq 'Y' then errcal=1

 endif

;-- BSC control structure to store selections

 bsa_ans=mk_bsa_ans()
 bsa_ans.xcorr=xcorr
 bsa_ans.deadtime=deadtime
 bsa_ans.fluxcal=fluxcal
 bsa_ans.wavecal=wavecal
 bsa_ans.errcal=errcal
 bsa_ans.chans(chans-1)=chans
 bsa_ans.accum(chans-1)=accum
 bsa_ans.thresh(chans-1)=thresh
 bsa_ans.accum(chans-1)=accum
 bsa_ans.dt_interp=dt_interp
   
 return,bsa_ans
 
 end


