Viewing contents of file '../idllib/iuedac/iuelib/pro/g2fit.pro'
;************************************************************************
;+
;*NAME:
;
;   	G2FIT
;  
;*CLASS:
;
;	Menu Procedrue
;	Spectral Fitting
;  
;*CATEGORY:
;  
;*PURPOSE:
;
;   	Procedure to fit gaussians and a linear baseline to data points 
;	(from GAUSSFIT.PRO).
;  
;*CALLING SEQUENCE:
;
;   	G2FIT,X,Y,NCEN,CENTER,A,HTSIG,SIGMA
;  
;*PARAMETERS:
;
;	X	(REQ) (I) (1) (I L F D)
;		Vector of independent variable elements.
;
;    	Y	(REQ) (I) (1) (I L F D)
;		Vector of dependent variable elements.
;
;    	NCEN	(REQ) (I) (0) (I)
;		Number of orders in signal region.
;
;    	CENTER	(REQ) (I) (1) (I)
;		Indice of central order (i.e. central order - 1).
;
;    	A	(REQ) (I/O) (1) (F D)
;		Input/output vector in which A(0)= fixed width,
;        	A(1)= fixed center position, A(3)= background y-intercept
;        	and A(4)= background slope.
;		Vector of function parameters.
;
;     	HTSIG	(REQ) (O) (0) (F D)
;		Uncertainty in gaussian height   
;             	(calculated according to bevington chapter 8).
;
;     	SIGMA	(REQ) (O) (0) (F D)
;		Scatter about best gaussian fit.
;
;*SYSTEM VARIABLES USED:
;
;*INTERACTIVE INPUT:
;  
;*SUBROUTINES CALLED:
;
;       PARCHECK
;	GAUSS
;  
;*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:
;
;   	Procedure to fit gaussians and a linear baseline to data points 
;	(from GAUSSFIT.PRO).	
;  
;*I_HELP nn:
;  
;*EXAMPLES:
;  
;*MODIFICATION HISTORY:
;   
;   nov-21-89 jtb@gsfc modifications for unix/sun idl
;   8 July 91 llt add parcheck, update prolog, clean up, tested on VAX.
;   16 Jul 91 PJL tested on SUN and VAX; updated prolog
;
;-
;************************************************************************
 pro g2fit,x,y,ncen,center,a,htsig,sigma
;
 npar=n_params(0)
 if npar eq 0 then begin
    print,'G2FIT,X,Y,NCEN,CENTER,A,HTSIG,SIGMA'
    retall
 endif  ; npar
 parcheck,npar,7,'G2FIT'
;
 b=fltarr(3)
 b([0,1,2]) = [a(0),a(1),y(center)]    ; fixed center & width, & estimate height
 range = ncen/2
 lind = center - range                 ; indice of left signal edge
 rind = center + range                 ; indice of right signal edge
;
; extract feature & subtract backgound
;
 xg = x(lind:rind)
 yb = a(3) + a(4)*xg + a(5)*xg*xg                ; calc bg vector
 yg = y(lind:rind) - yb                          ; subtract bckgnd   
;
; since center positions & widths are fixed, calculate best fit
; heights of gaussians directly from data
; (since weights are all equal here, they cancel out)
;
 gauss,xg,b(0),b(1),1.,ygf      
 alph00 = total(ygf*ygf)
 b(2)=total(ygf*yg)/alph00                              
 ygf = b(2)*ygf
 sigma = sqrt( total((ygf-yg)*(ygf-yg))/(ncen-1) ) ; estimate from data 
 htsig = sigma/sqrt(alph00)
;
; check for (and remove) points that are more than fac*sigma away 
; from best fit gaussian.
;
 fac = 1
 absnet = abs(yg-ygf)
 repeat begin
    fac = fac + 1
    dif=absnet - fac*sigma         ; dif is excess over fac*sigma
    ind=where(dif le 0.,nkept)
 endrep until nkept gt range       ; cont until gt half points kept
;
; re-fit gaussian for better parameters if any pts removed
;
 if nkept ne ncen then begin
    yg = yg(ind)
    xg = xg(ind)
    gauss,xg,b(0),b(1),1.,ygf
    alph00=total(ygf*ygf)
    b(2)=total(ygf*yg)/alph00
    ygf = b(2)*ygf
    sigma = sqrt( total((ygf-yg)*(ygf-yg))/(nkept-1) ) ;estimate from data 
    htsig = sigma/sqrt(alph00)
 endif  ; nkept ne ncen
;
; add final gaussian parameters to a
;
 a(0) = b
 return
 end  ; g2fit