;+
; NAME:
;       get_bcscal
; PURPOSE:
;       to find BCS crystal calibration file
; CALLING SEQUENCE:
;       get_bcscal,chan,calfil
; INPUTS:
;       chan         - channel number (1-4)
; OUTPUTS:
;       full name of CALFIL for specified channel
; OPTIONAL OUTPUT KEYWORDS:
;       version = version number of CAL file
;		  (this is equal to the file extension)
; PROCEDURE:
;       Return the name of the BCS cal file.
;       File is assumed to have the form 	Channel
;		$DIR_BCS_CAL/bcsa1.ver_number	  1
;		$DIR_BCS_CAL/bcsa2.ver_number	  2
;		$DIR_BCS_CAL/bcsa1.ver_number     3
;		$DIR_BCS_CAL/bcsa1.ver_number	  4
; HISTORY:
;       Written Oct. 1992 by D. Zarro (Applied Research Corp).
;       Modified Mar'93 (Zarro ARC), to include new DIR_BCS_CAL
;       Modified May 93 (CDP, RAL), Add $ signs to directory logicals
;                                   and trap non-use of version variable.
;       4-nov-93, J. R. Lemen (LPARL), Rewritten.  Changed to aways get 
;				       most recent version on $DIR_BCS_CAL
;-

pro get_bcscal,chan,calfil,version=version

on_error,1		; Return to caller if there is problem

; --- Check the Input Channel Number ------------
if n_elements(chan) eq 0 then message,'Chan is undefined'
bchan = gt_bsc_chan(chan)	; Check for valid channel number
if bchan lt 0 then message,'Invalid channel number supplied'


;-- Construct the file name

path = '$DIR_BCS_CAL'		; Location of cal files

 case chan of
 1: calfil = 'bcsa1.*'
 2: calfil = 'bcsa2.*'
 3: calfil = 'bcsb3.*'
 4: calfil = 'bcsb4.*'
endcase

calfil = concat_dir(path,calfil)
calfil = findfile(calfil,count=count)

; -- Check if the file exists - take the latest version --

if count ge 1 then begin
   calfil=calfil(count-1) 
   break_file,calfil,disk,dir,filnam,ext
   version=fix(strmid(ext,1,strlen(ext)))
   message,'Using BCS calibration file '+concat_dir(path,filnam+ext),/info 
endif else begin
  message,'Cannot find BCS calibration file',/cont
  message,'Please check logical/environment variables'
endelse

end

