;********************************************************************
;
; 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 gong,degree,wild,istart,tol,printer,as,modeampl
	common multiplet,peak
	common cross,crossparam
	common alias,ampli
	nn=N_params()
	if (nn LT 7) 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
			nn=nn+1
		endif
		if (nn EQ 6) then begin
		        read,'Starting mode amplitude',modeampl
		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
		if ((m eq 1) or (m eq N)) then begin
			startstring=startstring+'0'
		endif else begin
			startstring=startstring+'0'
		endelse
	endfor
	for m=1,N do begin
		startstring=startstring+'0'
	endfor

;
	
;
	Nfourier=51840
	Npoints=150
;	Npoints=1200

	if (as eq 1) then begin
		ydata=dcomplexarr(Npoints,N)
	endif else begin
		ydata=dblarr(Npoints,N)
	endelse
;
;

	resol=1000000./60./Nfourier
	xdata=(dindgen(Npoints)-Npoints/2)*resol

;*************************************************************************
; PLEASE ADAPT TO YOUR OWN
;
; read file. 
;
	splitting=readfits("mrvmp950524d00"+strtrim(degree,1)+"v1.fits")
	central=dblarr(5,9)
		
;
; PLEASE ADAPT TO YOUR OWN
; ************************************************************************** 
;
; l=2	
	Central=[[0,1,2,3,4,5,6,7,8],$
		 [2292.,2426.,2559.,2694.,2828.,2964.,3098.,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.]]

	peakGONG,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 AMPLITUDE crosstalk between mode (0< <1) ",mm,"  and ",mp,"  ="
					read,blurp
				endif else begin
					startstring=startstring+'0'
					blurp=0.5
				endelse
				paramct(cstart)=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'
				blurp=0.5
			endelse
			paramct(cstart)=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
;
		if (as eq 1) then begin
			ydata(0:Npoints-1,*)=sqrt(splitting(Nstart-Npoints/2:Nstart+Npoints/2-1,*,0))*complex(cos(splitting(Nstart-Npoints/2:Nstart+Npoints/2-1,*,1)),sin(splitting(Nstart-Npoints/2:Nstart+Npoints/2-1,*,1)))
		endif else begin
			ydata(0:Npoints-1,*)=splitting(Nstart-Npoints/2:Nstart+Npoints/2-1,*,0)
		endelse
;
; fit and plot
;
		
		
		param(0)=0.1
		param(1)=-0.4
		param(2)=alog(0.9)
		param(3:2+N)=alog(modeampl)
;
;		param(3)=alog(0.0001)
;		param(2+N)=alog(0.0001)
		param(3+N:2+2*N)=alog(30)
;
; Here we transform the amplitude crosstalk to the log version of the
; power crosstalk
;
		param(3+2*N:2+2*N+cindex)=-alog(paramct(0:cindex-1)^2)
;
		print,tol
;
; set alias amplitude
;
		ampli=0.1
		print,startstring


;******************************************************************************************************************
	print,param
	fitplot,xdata,ydata,'spectraltene',param,error,degree,tol,Freqstart,'1st month of GONG data',printer,fixed=startstring,nodfp='10'

	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
;
; Here we really use the amplitude crosstalk
;
	param(3+2*N:2+2*N+cindex)=paramct(0:cindex-1)

	print,param

	fitplot,xdata,ydata,'crosstalk',param,error,degree,tol,Freqstart,'1st month of GONG data',printer,fixed=startstring,nodfp='10',/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," =",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=0.
;exp(-abs(param(cstart))+error(cstart))-exp(-abs(param(cstart)))
		error2=0.
;-exp(-abs(param(cstart))-error(cstart))+exp(-abs(param(cstart)))
		print,"Crosstalk between mode ",mm,"and ",mp," =",param(cstart),"+",error1," -",error2
		cstart=cstart+1
	endfor





	endfor
end






