Viewing contents of file '../idllib/ghrs/pro/calfos_sky.pro'
pro calfos_sky,areas,line_beg,line_end,nshift,ypos,widths,config,pattern, $
		gross,sky,eps_sky,h,eps,net
;+
;			CALFOS_SKY
;
; subtracts smoothed sky spectrum from object spectra.
;
; CALLING SEQUENCE:
;	calfos_sky,areas,line_beg,line_end,nshift,ypos,widths,config,pattern,
;			gross,sky,eps_sky,h,eps,net
;
; INPUTS:
;	areas - relative aperture areas (from routine calfos_ccs0)
;			areas(0) - upper aperture
;			areas(1) - lower aperture
;	line_beg - beggining positions of emmision lines not to
;		be smoothed (from routine calfos_ccs2)
;	line_end - ending positions (from routine calfos_ccs2)
;	nshift - amount to shift the sky before subtraction
;		(from routine calfos_ccs5)
;	ypos - ybases of the upper and lower aperture (from routine
;		calfos_ccs1)
;	widths - median and mean filter widths for smoothing the sky.
;		(from routine calfos_ccs3)
;	config - instrument configuration (from routine calfos_rd)
;	pattern - pattern vector (from routine calfos_rd)
;	gross - object spectra
;	sky - sky spectra
;	eps_sky - data quality for sky spectra
;
; INPUT/OUTPUT:
;	h - FITS header
;	eps - data quality for the gross/net
;
; OUTPUTS:
;	net - the net spectra
;
; HISTORY:
;	version 1  D. Lindler   Jan 1990
;-
;---------------------------------------------------------------------------	
;
; get pattern and configuration information
;
	ybase = pattern(4)
	yrange = pattern(5)
	ysteps = pattern(6)
	slices = pattern(9)
	nreads = pattern(10)
	ns = pattern(8)
	nframes = slices*nreads
	ytypes = strarr(3)
	for i=0,2 do ytypes(i) = config(4+i)
;
; Determine the ratio of the object and sky aperture areas and which nshift to
; use
;
	for i=0,2 do begin
	  	if strtrim(ytypes(i)) eq 'SKY' then begin
	    		y = ybase + yrange*32.0/ysteps * i
	    		if abs(y-ypos(0)) lt abs(y-ypos(1)) then begin
				ratio = areas(1)/areas(0)
				n_shift = nshift(0)
	      		    end else begin
				ratio = areas(0)/areas(1)
				n_shift = nshift(1)
			end
	    	endif
	endfor
;
; determine number of object spectra per slice
;
	s=size(gross) & nobj = s(2)
	net = fltarr(ns,nobj,nframes)
;
; loop on frames of data
;
	for frame = 0,nframes-1 do begin
;
; repair bad data in sky
;
	    s = sky(*,0,frame)
	    eps_s = eps_sky(*,0,frame)
	    mask = eps_s gt 200
	    repaired = where(mask) & nrepaired = !err
	    calfos_repair,mask,s
	    if !err lt 0 then begin
		hist = 'All sky data bad for frame number '+string(frame+1)
		sxaddhist,hist,h
		print,hist
	    endif
;
; filter sky and multiply by ratio and shift
;
	    s = calfos_median(s,widths(0),line_beg,line_end) * ratio
	    for i=1,2 do s = calfos_mean(s,widths(1),line_beg,line_end)
	    s = shift(s,n_shift)
;
; loop on object spectra
;
	    for i=0,nobj-1 do begin
		d = gross(*,i,frame) - s
		e = eps(*,i,frame)
		if nrepaired gt 0 then e(repaired) = e(repaired) > 120
		bad = where(e ge 200) & nbad = !err
		if nbad gt 0 then d(bad) = 0
		net(0,i,frame) = d
	    endfor
	endfor
;
; history processing
;
	nlines = n_elements(line_beg)		;number of emission lines
	if line_beg(0) lt 0 then nlines = -1	;none
	hist = strarr(5+nlines)
	hist(0) = 'Sky median filter width ='+string(widths(0))
	hist(1) = 'Sky mean filter width   ='+string(widths(1))
	hist(2) = 'Sky scaled by relative aperture area ratio ='+string(ratio)
	hist(3) = 'Sky shifted by '+strtrim(n_shift,2)+' pixels'
	if nlines gt 0 then begin
		hist(4) = 'The following sky regions where not smoothed'
		for i=0,nlines-1 do hist(5+i)='    '+strtrim(line_beg(i),2)+ $
				      ' to '+strtrim(line_end(i),2)
	endif
	sxaddhist,hist,h
	if !dump gt 0 then print,hist
return
end