Viewing contents of file '../idllib/ghrs/pro/calfos_rdbac.pro'
pro calfos_rdbac,name,config,pattern,h,back
;
;+
;			CALFOS_RDBAC
;
; read background reference file
;
; CALLING SEQUENCE:
;	calfos_rdbac,name,config,pattern,h,back
;
; INPUTS:
;	name - reference file name
;	config - configuration vector (from CALFOS_RD)
;	pattern - pattern vector (from CALFOS_RD)
;
; INPUT/OUTPUTS:
;	h - FITS header
;
; OUTPUTS:
;	back - background vector of length (nchnls+overscan-1)*xsteps
;
; HISTORY:
;	version 1.0 D. Lindler  Jan 1980
;-
;--------------------------------------------------------------------------
	fname = strtrim(name)
	if fname eq '' then return		;file needed?
;
; extract configuration and pattern info from input vectors
;
	detector = strtrim(config(0))
	fchnl = pattern(0)
	nchnls = pattern(1)
	overscan = pattern(2)
	nxsteps = pattern(3)
	ns = pattern(8)
;
; open file and get pattern info from header
;
	sxopen,1,fname,href
	det = strtrim(sxpar(href,'detector',fname))
	fc = sxpar(href,'fchnl',fname)
	nc = sxpar(href,'nchnls',fname)
	nxs = sxpar(href,'nxsteps',fname)
	oscn = sxpar(href,'overscan',fname)
;
; check consistency
;
	if (det ne detector) and (det ne 'ANY') then begin
	    print,'CALFOS_RDBAC-- Reference file '+fname+' detector mismatch'
	    retall
	endif

	if (overscan ne oscn) or (fc gt fchnl) or ((fc+nc) lt (fchnl+nchnls)) $
	   or (nxs lt nxsteps) then begin
		print,'CALFOS_RDBAC -- inconsistent pattern information in '+ $
			fname+' with input observation'
		retall
	endif
;
; read background and extract correct portion
;
	back = sxread(1)
	nback = n_elements(back)

	if (fc ne fchnl) or (nchnls ne nc) then begin
		first = (fchnl-fc)*nxs
		last = first + (nchnls+overscan-1)*nxs - 1
		back = back(first:last)
	endif

	if (nxs ne nxsteps) then back = back(indgen(ns)*nxs/nxsteps)

	hist = 'Background reference file: '+fname
	sxaddhist,hist,h
	if !dump gt 0 then print,hist
return
end