; $Id: estpars.pro,v 1.2 2002/01/14 20:59:26 amunter Exp $
pro estpars
;+
;	NAME: estpars
;	DESCRIPTION: Does some initial estimation of the fit parameters
;		for tasxp
;		INTERNAL tasxp PROCEDURE
;	MODIFICATION HISTORY
;		Thom Mason, Riso National Lab, 1993
;	Nov 1993, Paul Schleger, ditto,
;		added a check if we want to fit lorentzians
;-
COMMON data,xax,yax,fit,symboln,dindex,d,xmin,xmax,ymin,ymax,pcomm
COMMON fitp,ngau,nlor,a,vary,pnames,gw,ufunct,nufun
COMMON nums,gb,gn,vback_ids,ln,wgw
COMMON preferences, pref
COMMON com_convolution, condim, epar, resoly, tilt, ghorder

;2D gaussian-lorentzian convolution
resoly = 0.0
tilt = 0.0
epar = 0.0
ghorder = 50
IF pref.vert NE 0 THEN condim = 2 ELSE condim = 1

first_time=1
pnames=strarr(9)
gw=0.0 ; default is not to convolute
ufunct=''
nufun=0
IF pref.multi_gauss NE 0 THEN BEGIN
	a=fltarr(3+2*3)
	vary = intarr(3+2*3)
	vary(3) = 0 & vary(4) = 0 & vary(5) = 0
	vary(6) = 0 & vary(7) = 0 & vary(8) = 0
	nlor = 0
	ngau = 2	;multi_gauss
	ln=0
	gn=0
	a(0)=min(d.data(tasid(yax,d),*))
	x = d.data(tasid(xax,d),*) & y = d.data(tasid(yax,d),*) 
	position = TOTAL(x*(y-a(0)))/TOTAL(y-a(0))
	mx = min(ABS(position-x),index) 
	maxval = max(y)
	mx = min(ABS(maxval/2.0-y),index)
	width = 2.0*ABS(x(index)-position)             
	;maxval=max(y,maxsub)-a(0)
	;position=y(maxsub)
	;width=((max(x)-min(x))/3.0)
	a(3) = maxval/2.0 & a(6) = maxval/2.0
	a(4) = position-0.3*width & a(7) = position+0.3*width
	a(5) = width/2.0 & a(8) = width/2.0
END ELSE BEGIN
	a=fltarr(6)
	vary=intarr(6)
	ngau=1-pref.wantlorentz
	nlor=pref.wantlorentz
	gn=ngau
	ln=nlor
	a(0)=min(d.data(tasid(yax,d),*))                    
	a(3)=max(d.data(tasid(yax,d),*),maxsub)-a(0)
	a(4)=d.data(tasid(xax,d),maxsub)
	a(5)=((max(d.data(tasid(xax,d),*))-min(d.data(tasid(xax,d),*)))/3)
	vary(3)=1 & vary(4)=1 & vary(5)=1
END
;The default background	
vary(0) = 1 & vary(1) = 0 & vary(2) = 0
a(1)=0.
a(2)=0.

;The parameter names
pnames(0)='B0'
pnames(1)='B1'
pnames(2)='B2'
pnames(3)='GI'
pnames(4)='GP'
pnames(5)='GW'
pnames(6)='LI'
pnames(7)='LP'
pnames(8)='LW'
IF pref.wantlorentz THEN a(5)=a(5)/2.0
END
