;********************************************************************
;
; Routine for fitting the Tenerife spectra
;
; degree is the degree (sic)
;
; wild is a string for various file name (i.e. 'new', 'old', etc...) 
;
; istart is the frequency number where should start the iteration
;
; tol is the tolerance for the Power algorithm
;
;*********************************************************************

pro tene,degree,wild,istart,tol,printer,as
	common multiplet,peak
	common cross,crossparam
	common alias,ampli
	nn=N_params()
	if (nn LT 6) then begin
		print,''
		if (nn EQ 0) then begin
			read,'give the degree of the mode= ',degree
			print,''
			degree=floor(degree)
			nn=nn+1
		endif
		if (nn EQ 1) then begin
			wild=''
		  	read,'give the wid card of your file as a string= ',wild
			print,''
			nn=nn+1
		endif
		if (nn EQ 2) then begin
		     	read,'give the starting point for the fit= ',istart
			print,''
			istart=floor(istart)
			nn=nn+1
		endif
		if (nn EQ 3) then begin
		        read,'give tolerance for the Powell algorithm (1d-08 works well on a Sparc20...)= ',tol
			print,''
			nn=nn+1
		endif
		if (nn EQ 4) then begin
			printer=''
		        read,'give the printer output as a string (press return to redirect to your monitor)= ',printer
			print,''
			nn=nn+1
		endif
		if (nn EQ 5) then begin
		        read,'Amplitude (1) or power spectra fitting?',as
		endif

        endif
	Neval=0.
;
; Number of modes
;
	N=2*degree+1
;
; Here we set the fixed string
;
;   Frequency, Splitting, Linewidth are NOT fixed
	startstring='000'
;
;   Amplitudes and Noises are NOT fixed
	for m=1,N do begin
		startstring=startstring+'00'
	endfor
	
;
	Nfourier=262144
	Npoints=600
;	Npoints=200

	if (as eq 1) then begin
		buffer=dcomplexarr(Nfourier)
		splitting=dcomplexarr(Nfourier/2,N)
		ydata=dcomplexarr(Npoints,N)
	endif else begin
		buffer=dblarr(Nfourier)
		splitting=dblarr(Nfourier/2,N)
		ydata=dblarr(Npoints,N)
	endelse
;
;

	resol=0.142107d0/2.d0
	xdata=(dindgen(Npoints)-Npoints/2)*resol

;*************************************************************************
; PLEASE ADAPT TO YOUR OWN
;
; read file. 
;
	idata=strpos(wild,'Data')
	if (idata EQ -1) then begin
		wild='spectra3/spec6'+wild 
	endif else begin 
		sep=str_sep(wild,'/')
		wild='Data/spectra3/spec6'+sep(1)
	endelse

	m=0
		filename=wild+strtrim(degree,1)+strtrim(m,1)
		openr,1,filename
		readu,1,buffer
		close,1
		print,degree,m,"done"
		splitting(0:Nfourier/2-1,degree)=buffer(0:Nfourier/2-1)


	for m=1,degree do begin
		filename=wild+strtrim(degree,1)+strtrim(m,1)
		openr,1,filename
		readu,1,buffer
		close,1
		print,degree,m,"done"
		splitting(0:Nfourier/2-1,degree-m)=buffer(0:Nfourier/2-1)
		buffer=reverse(buffer)
		splitting(1:Nfourier/2-1,degree+m)=buffer(0:Nfourier/2-2)
	endfor
	central=dblarr(5,9)
;
;	print,'Warning: special just for l=2'
;	if (degree eq 2) then begin
;		filename=wild+strtrim(3,1)+strtrim(2,1)
;		openr,1,filename
;		readu,1,buffer
;		close,1
;		print,'2 1 done by reading 3 2'
;		splitting(0:Nfourier/2-1,degree-1)=buffer(0:Nfourier/2-1)
;		buffer=reverse(buffer)
;		splitting(1:Nfourier/2-1,degree+1)=buffer(0:Nfourier/2-2)
;	endif

		
;
; PLEASE ADAPT TO YOUR OWN
; ************************************************************************** 
;
; l=2	
	Central=[[0,1,2,3,4,5,6,7,8],$
		 [2292.,2426.,2559.,2694.,2828.,2964.,3091.,3233.,3369.],$
		 [2352.34,2486.18,2619.61,2754.30,2889.35,3024.76,3160.16,3295.00,3430.77],$
		 [2408.11,2541.69,2676.03,2811.39,2947.01,3082.39,3217.82,3354.17,3489.78],$
		 [2458.34,2593.01,2728.66,2864.30,3000.14,3135.85,3271.52,3408.29,3544.29],$
		 [2506.18,2641.54,2777.33,2913.37,3049.78,3186.32,3322.71,3458.,3594.]]

	peakLOI,degree,cindex
		
	Nparam=3+2*N+cindex

	param=dblarr(Nparam)
	error=dblarr(Nparam)
	paramct=dblarr(cindex)
	
; Now input the crosstalk online
		cstart=0
		for m=0,degree-1 do begin
			for i=0,peak(0,m)-2 do begin
				mm=m-degree
				mp=peak(2+i,m)

				
				print,"Crosstalk between mode ",mm,"  and ",mp,"   fixed? (1 for yes)"
				read,blurp
				if (blurp eq 1.) then begin
					startstring=startstring+'1'
					print,"Please input fixed crosstalk between mode (0< <1) ",mm,"  and ",mp,"  ="
					read,blurp
				endif else begin
					startstring=startstring+'0'
					print,"Please input starting crosstalk between mode (0< <1) ",mm,"  and ",mp,"
					read,blurp
				endelse
				paramct(cstart)=-alog(blurp)
				cstart=cstart+1
			endfor
		endfor	
; Now for m=0
		for i=0,(peak(0,degree)-3)/2 do begin
			mm=0
			mp=peak(2+i,degree)
			print,"Crosstalk between mode ",mm,"  and ",mp,"  fixed? (1 for yes)"
			read,blurp
			if (blurp eq 1.) then begin
				startstring=startstring+'1'
				print,"Please input fixed crosstalk between mode (0< <1) ",mm,"  and ",mp,"  ="
				read,blurp
			endif else begin
				startstring=startstring+'0'
				print,"Please input starting crosstalk between mode (0< <1) ",mm,"  and ",mp,"
				read,blurp
			endelse
			paramct(cstart)=-alog(blurp)
			cstart=cstart+1
		endfor

;			param(3+2*N)=-alog(0.1)
;			param(3+2*N+1)=-alog(0.5)
;			param(3+2*N+2)=-alog(0.0000001)
;			param(3+2*N+3)=-alog(0.5)
;			param(3+2*N+4)=-alog(0.1)
		


	for ifreq=istart,8 do begin
		Cen=Central(ifreq,degree)
		Nstart=floor(Cen/resol)
		Freqstart=Nstart*resol
		print,"*******************************************"
		print,""
		print,"Frequency of starting points=",Freqstart
		print,""
		print,"*******************************************"
;
;
;	
		window=Npoints*resol
		print,"Window size=",window
;
		ydata(0:Npoints-1,*)=splitting(Nstart-Npoints/2:Nstart+Npoints/2-1,*)

;
; fit and plot
;
		
		
		param(0)=0.1
		param(1)=-0.4
		param(2)=alog(0.9)
		param(3:2+N)=alog(5.)
		param(3+N:2+2*N)=alog(1.0)
		param(3+2*N:2+2*N+cindex)=paramct(0:cindex-1)
;
		print,tol
;
; set alias amplitude
;
		ampli=0.3
		print,startstring
	fitplot,xdata,ydata,'spectraltene',param,error,degree,tol,Freqstart,'LOI '+wild,printer,fixed=startstring

	print,""
	print, "For degree=",degree
	print,""
	print,"Frequency =",Freqstart+param(0),"   +/-",error(0)

	print,""
	print,"Splitting=",param(1),"   +/-",error(1)

	print,""
	error1=exp(param(2)+error(2))-exp(param(2))
	error2=-exp(param(2)-error(2))+exp(param(2))
	print,"Linewidth=",exp(param(2)),"   +",error1,"   -",error2
	print,"" 

	for i=3,2+N do begin	

	error1=exp(param(i)+error(i))-exp(param(i))
	error2=-exp(param(i)-error(i))+exp(param(i))
	print,"Mode",i-3-degree,"     Amplitude=",exp(param(i)),"   +",error1,"   -",error2
 
	endfor

	print,""

	for i=3+N,2+2*N do begin

	error1=exp(param(i)+error(i))-exp(param(i))
	error2=-exp(param(i)-error(i))+exp(param(i))
	print,"Mode",i-3-N-degree,"     Noise=",exp(param(i)),"   +",error1,"   -",error2

	endfor

	;
; Now take care of the crosstalk
	cstart=3+2*N
	for m=0,degree-1 do begin
		for i=0,peak(0,m)-2 do begin

			mm=m-degree
			mp=peak(2+i,m)
			error1=exp(-abs(param(cstart))+error(cstart))-exp(-abs(param(cstart)))
			error2=-exp(-abs(param(cstart))-error(cstart))+exp(-abs(param(cstart)))
			print,"Crosstalk between mode ",mm,"and ",mp," =",exp(-abs(param(cstart))),"+",error1," -",error2
			cstart=cstart+1
		endfor
	endfor	
; Now for m=0
	for i=0,(peak(0,degree)-3)/2 do begin
		mm=0
		mp=peak(2+i,degree)
		error1=exp(-abs(param(cstart))+error(cstart))-exp(-abs(param(cstart)))
		error2=-exp(-abs(param(cstart))-error(cstart))+exp(-abs(param(cstart)))
		print,"Crosstalk between mode ",mm,"and ",mp," =",exp(-abs(param(cstart))),"+",error1," -",error2
		cstart=cstart+1
	endfor


;	param(0)=B
;	param(1)=D
;	param(2)=alog(C)
;	param(3:2+N)=alog(A)
;	print,E
;	param(3+N:2+2*N)=alog(E)
;	if (cindex GT 0) then begin
;		param(3+2*N:Nparam-1)=alog(CT)
;	endif


;fitplot,xdata,ydata,'spectraltene',param,error,degree,tol,Freqstart,'LOI'+wild,printer,fixed=startstring,/as



;	print,""
;	print, "For degree=",degree
;	print,""
;	print,"Frequency =",Freqstart+param(0),"   +/-",error(0)

;	print,""
;	print,"Splitting=",param(1),"   +/-",error(1)

;	print,""
;	error1=exp(param(2)+error(2))-exp(param(2))
;	error2=-exp(param(2)-error(2))+exp(param(2))
;	print,"Linewidth=",exp(param(2)),"   +",error1,"   -",error2
;	print,"" 

;	for i=3,2+N do begin	

;	error1=exp(param(i)+error(i))-exp(param(i))
;	error2=-exp(param(i)-error(i))+exp(param(i))
;	print,"Mode",i-3-degree,"     Amplitude=",exp(param(i)),"   +",error1,"   -",error2
 
;	endfor

;	print,""

;	for i=3+N,2+2*N do begin

;	error1=exp(param(i)+error(i))-exp(param(i))
;	error2=-exp(param(i)-error(i))+exp(param(i))
;	print,"Mode",i-3-N-degree,"     Noise=",exp(param(i)),"   +",error1,"   -",error2

;	endfor

	;
; Now take care of the crosstalk
;	cstart=3+2*N
;	for m=0,degree-1 do begin
;		for i=0,peak(0,m)-2 do begin

;			mm=m-degree
;			mp=peak(2+i,m)
;			error1=exp(-abs(param(cstart))+error(cstart))-exp(-abs(param(cstart)))
;			error2=-exp(-abs(param(cstart))-error(cstart))+exp(-abs(param(cstart)))
;			print,"Crosstalk between mode ",mm,"and ",mp," =",exp(-abs(param(cstart))),"+",error1," -",error2
;			cstart=cstart+1
;		endfor
;	endfor	
; Now for m=0
;	for i=0,(peak(0,degree)-3)/2 do begin
;		mm=0
;		mp=peak(2+i,degree)
;		error1=exp(-abs(param(cstart))+error(cstart))-exp(-abs(param(cstart)))
;		error2=-exp(-abs(param(cstart))-error(cstart))+exp(-abs(param(cstart)))
;		print,"Crosstalk between mode ",mm,"and ",mp," =",exp(-abs(param(cstart))),"+",error1," -",error2
;		cstart=cstart+1
;	endfor


	endfor
end






