;+
;
;  Name:
;       READ_CAL_SPEC
;
;
; PURPOSE:  read calibration line data for BATSE SPEC detectors
;
;
; CATEGORY:  spectral analysis, SPEX, BATSE
;
;
; CALLING SEQUENCE:  read_cal_spec
;
;
; CALLED BY:
;
;
; CALLS:
;	none
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	flare - an array or single flare number
;
; OUTPUTS:
;       out_rec - 1 or more cal_spec records determined by flare
;
; OPTIONAL OUTPUTS:
;	none
;
; COMMON BLOCKS:
;	cal_catalog
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	must pass valid flare numbers, implemented only in vms
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;	ras, 15-feb-95
;               RAS, 7-Jul-1997, changed PERM_DATA to SSWDB_BATSE
;-
pro read_cal_spec, flare, out_rec

;Structure definition for BATSE SPEC calibration table
;Data are extracted from Marshall Data base
;See spectral_history.str


cal_spec_1 = {cal_spec, $
		flare_num:0L, $		;flare archive number
		utime:dblarr(2,8), $	;calibration time before and after for each id
		chan_511:fltarr(2,8) }  ;position of 511 line in linear channels 

common cal_catalog, cal_spec

if n_elements(cal_spec) eq 0 then begin 
	openr, lu,/get,concat_dir(getenv('SSWDB_BATSE'),'cal_spec.dat')
	fnum = 0L
	readu, lu, fnum
	cal_spec = replicate(cal_spec_1,fnum)
	for i=0,fnum-1 do begin
		readu, lu, cal_spec_1
		cal_spec(i) = cal_spec_1
	endfor
	free_lun,lu
endif

if keyword_set(flare) then begin
	extreme = minmax(flare)
        if extreme(0) lt 1 or extreme(1) gt fnum then message,'Invalid flare number(s) passed.'
	out_rec = cal_spec(flare -1)
endif

end

