;********************************************************************
;
; Routine for fitting a synthetic m,nu diagram 
;
; Used for debugging
;
; Starting parameters:....
; A amplitude, B frequency, C linewidth, D splitting
; E noise, CT crosstalk between modes, degree of m,nu diagram
; 
; degree is the degree (sic)
;
; tol is the tolerance for the Power algorithm
;
;*********************************************************************

pro simu,A,B,C,D,E,CT,degree,tol,printer
	common multiplet,peak
	common cross,crossparam
	common speed,Neval
	common alias,ampli
	nn=N_params()
	if (nn LT 9) then begin
		print,''
		if (nn EQ 0) then begin
			read,'give the amplitude of the mode= ',A
			print,''
			nn=nn+1
		endif
		if (nn EQ 1) then begin
			wild=''
		  	read,'give the central frequency of the mode (in microHz)= ',B
			print,''
			nn=nn+1
		endif
		if (nn EQ 2) then begin
		     	read,'give the linewidth of the mode (in microHz)= ',C
			print,''
			nn=nn+1
		endif
		if (nn EQ 3) then begin
		        read,'give the splitting of the multiplet of the mode (in microHz)= ',D
			print,''
			nn=nn+1
		endif
		if (nn EQ 4) then begin
		        read,'give the background noise= ',E
			print,''
			nn=nn+1
		endif
		if (nn EQ 5) then begin
		        read,'give the crosstalk between the modes= ',CT
			print,''
			nn=nn+1
		endif
		if (nn EQ 6) then begin
		        read,'give the degree of the mode= ',degree
			print,''
			degree=floor(degree)
			nn=nn+1
		endif
		if (nn EQ 7) 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 8) then begin
			printer=''
		        read,'printer= ',printer
		endif
        endif

	Neval=0.
	Npoints=200
;
; A amplitude, B frequency, C linewidth, D splitting
; E noise, CT crosstalk betweeb modes, degree of m,nu diagram
;
;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
;compute window
	ampli=0.
	xdata=(dindgen(Npoints)-Npoints/2.)*7d-02
	y=dblarr(Npoints,N)
	ydata=dblarr(Npoints,N)
	ydatacmplx=dcomplex(Npoints,N)
;
; define peak characteritics	
;
	peakGONG,degree,cindex
;
	Nparam=3+2*N+cindex
	param=dblarr(Nparam)
	error=dblarr(Nparam)
	paramct=dblarr(cindex)
;
;
	

;
;define line profile
	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


; 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'
					blurp=0.5
				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'
				blurp=0.5
			endelse
			paramct(cstart)=-alog(blurp)
			cstart=cstart+1
		endfor



;
	montecarlo,param,xdata,ydata,ydatacmplx
	if (cindex GT 0) then begin
		param(3+2*N:Nparam-1)=paramct(0:cindex-1)
;alog(0.5)
	endif
	
;
; fit and plot
;
	Freqstart=0.
	fitplot,xdata,ydata,'spectraltene',param,error,degree,tol,Freqstart,'Monte-Carlo',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)
; 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,"Please input AMPLITUDE crosstalk between mode (-1< <1) ",mm,"  and ",mp,"  ="
					read,blurp
				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,"Please input fixed AMPLITUDE between mode (-1< <1) ",mm,"  and ",mp,"  ="
				read,blurp
			paramct(cstart)=blurp
			cstart=cstart+1
		endfor
	if (cindex GT 0) then begin
		param(3+2*N:Nparam-1)=paramct(0:cindex-1)
	endif
	print,param

fitplot,xdata,ydatacmplx,'crosstalk',param,error,degree,tol,Freqstart,'Monte-Carlo',printer,/as,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," =",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," =",param(cstart),"+",error1," -",error2
		cstart=cstart+1
	endfor



;




end




;********************************************************************
;
; Routine for generating a synthetic m,nu diagram 
;
; Used for debugging
;
; Starting parameters:....in param
; 
; x is the frequency
;
; yout is the m,nu diagram given out
;
;
;*********************************************************************

pro montecarlo,param,x,yout,youtcmplx
	common multiplet,peak
	common cross,crossparam
;
; This computes the montecarlo spectrum
;
; param=(amplitude,frequency,linewidth,splitting,noise)
; peak represents the amplitude ratio of the lines
; N is the number of modes (max=2l+1) in a spectrum

	paramline=dblarr(7)
;
; common frequency
	paramline(1)=param(0)
; common splitting
	paramline(3)=param(1)
; common linewidth
	paramline(2)=param(2)
; Here we put the noise at 0
	paramline(4)=-10000.d0
;
	Npoints=N_elements(x)
	Ntotal=N_elements(yout)
	N=Ntotal/Npoints
;
	degree=(N-1)/2
;
	offampl=3+degree
	offnoise=offampl+N
;
; fake crosstalk here at 1.0
	paramline(5)=alog(1.d0)
	paramline(6)=0.
; copy the input array
	yin=yout
;
; 
; 1st for the m mode
;
	for i=0,N-1 do begin
		m=peak(1,i)
		paramline(0)=param(offampl+m)
		paramline(1)=param(0)+m*param(1)
;
; the sqrt is here because we generate amplitude spectrum and NOT power spectrum
;
		yin(*,i)=sqrt(lorentzian(paramline,x(*))+exp(param(offnoise+m)))
	endfor
;
; Note here the complex for the amplitude sectrum
;
;	seed=30
	y1=randomn(seed,Npoints,N)
;	seed=31
	y2=randomn(seed,Npoints,N)
;	seed=2.*i+4
	y3=randomn(seed,Npoints,N)
;	seed=2.*i+5
	y4=randomn(seed,Npoints,N)
;
	yrandom=complex(y1,y2)/sqrt(2.)
;
; If no noise crosstalk  yincmplx=complex(yin,0)*yrandom
;
	yincmplx=complex(yin,0)*yrandom
	youtcmplx=yincmplx
;
; then for the modes leaking into the m mode
;
	for i=0,N-1 do begin
		m=peak(1,i)
		for j=1,peak(0,i)-1 do begin
; m is the azimuthal order of the present peak
;
			mp=peak(j+1,i)
;
; amplitude of the m mode
;
			if ((abs(m) eq 1) and (abs(mp) eq 1)) then begin
				ct=sqrt(exp(param(crossparam(m+degree,mp+degree))))
			endif else begin
				ct=-sqrt(exp(param(crossparam(m+degree,mp+degree))))
			endelse
			print,ct
;
; Here the sqrt because we think of the crosstalk in the power spectrum
			youtcmplx(*,i)=temporary(youtcmplx(*,i))+ct*yincmplx(*,mp+degree)
		endfor
	endfor
;
; The noise is added here only
; noise on the mindex mode
;	for i=0,N-1 do begin
;		m=peak(1,i)
;
; Here the sqrt because we think of the noise in the power spectrum
;		
;	seed=2.*i+4
;	y3=randomn(seed,Npoints,N)
;	seed=2.*i+5
;	y4=randomn(seed,Npoints,N)
;youtcmplx(*,i)=temporary(youtcmplx(*,i))+complex(sqrt(exp(param(offnoise+m))),0)*complex(y3,y4)/sqrt(2.)


;	endfor
;
; Generate power spectra
;
	yout=abs(youtcmplx)^2
end


