Viewing contents of file '../idllib/iuedac/iuelib/pro/g1bckgnd.pro'
;************************************************************************
;+
;*NAME:
;
;   	G1BCKGND
;  
;*CLASS:
;  
;  	Spectral Fitting
;  
;*CATEGORY:
;
;*PURPOSE:
;
;    	Procedure to fit a linear baseline to data points.
;  
;*CALLING SEQUENCE:
;
;    	G1BCKGND,X,Y,XL,XR,A,SIG
;  
;*PARAMETERS:
;
;   	X	(REQ) (I) (1) (I L F D)
;		Vector of independent variables.
;
;       Y	(REQ) (I) (1) (I L F D)
;		Vector of dependent variables.
;
;       XL	(REQ) (I) (1) (I)
;		Indice of left signal edge.
;
;      	XR	(REQ) (I) (1) (I)
;		Indice of right signal edge.
;
;   	A	(REQ) (I/O) (1) (F)
;		Floating point array with 3+3*NCOMP elements where (input).
;                NCOMP = # of features (1 for GEX1, see GAUSSFIT) (input).
;		Vector of function parameters (output).
;
;       SIG	(REQ) (O) (1) (F D)
;		Standard deviation (i.e. total(y-yfit)^2/n-1 ).
;  
;  
;  
;*SYSTEM VARIABLES USED:
;
;*INTERACTIVE INPUT:
;  
;*SUBROUTINES CALLED:
;  
;	PARCHECK
;    	LINFIT
;  
;*FILES USED:
;  
;*SIDE EFFECTS:
;  
;*RESTRICTIONS: 
;
;	modified for unix/sun idl version 1.1
;  
;*NOTES:
;  
;       tested with IDL Version 2.1.0 (sunos sparc)	16 Jul 91
;       tested with IDL Version 2.1.0 (ultrix mispel)	n/a
;       tested with IDL Version 2.1.0 (vms vax)    	16 Jul 91
;
;*PROCEDURE:
;  
;    	Uses LINFIT to fit a straight line to region outside region of 
;	feature.  Baseline parameters are stored in last 3 terms of vector A 
;	(last term reserved for 2nd order fit).
;  
;*I_HELP nn:
;  
;*MODIFICATION HISTORY:
;
;   nov-21-89 jtb@gsfc modified for unix/sun idl
;   8 July 91 LLT add PARCHECK, updated prolog, cleaned up, tested on VAx.
;   16 Jul 91 PJL tested on SUN and VAX; updated prolog
;  
;-
;************************************************************************
 pro g1bckgnd,x,y,xl,xr,a,sig
;
 nparms = n_params()
 if nparms eq 0 then begin
    print,' G1BCKGND, X,Y,X1,XR,A,SIG '
    retall  
 endif  ; nparms
 parcheck,nparms,6,'G1BCKGND'
;
 npts = n_elements(x)
 nterms = n_elements(a)
 num = npts + xl - xr
;
; extract baseline segments from outside feature region
;
 ybase=fltarr(num)
 xbase=ybase
 ybase(0) = y(0:xl-1)
 ybase(xl) = y(xr+1:npts-1)
 xbase(0) = x(0:xl-1)
 xbase(xl) = x(xr+1:npts-1)
;
; fit a polynomial baseline to full background file
;
 linfit,xbase,ybase,1.0,ac,bc,si
;
; keep points that deviate by less than 2*sig from the fit
; (re-fit background if any points were removed)
;
 dif = abs(ybase-ac-bc*xbase) - 2.0 * si(5)
 ind = where(dif le 0.0,count)
 if count ne num then begin
    ybase = ybase(ind)
    xbase = xbase(ind)
    linfit,xbase,ybase,1.0,ac,bc,si
 endif  ; count
;
; store baseline parameters in 'a' starting in a(nterms-3)
;
 a([nterms-3,nterms-2])=[ac,bc]
 sig = si(5)
;
 return
 end  ; g1bckgnd.pro