;FIT_CORRELATIONS
;
;PURPOSE:
;	To try and fit the autocorrelation of the ShackHartman grid
;
;INPUT:
;	The function fit_function.pro is used


pro newest_fit_correlations_chi,Date=Date,stats_directory=stats_directory,$
        x_center_file=x_center_file,y_center_file=y_center_file,$
        depth=depth,x_off=x_off,y_off=y_off


If (not keyword_set(Date)) then Date='10'
If (not keyword_set(stats_directory)) then stats_directory='stats'
If (not keyword_set(postscript_dir)) then postscript_dir='distorted_postscripts'

;This is the factor that converts between an pixel spacing on the ShackHartmann and an
;angle on the primary mirror
Ang_factor=float(((24e-6/60e-3)*(3.5e-3))^2)
stats_directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/'+stats_directory+'/'
postscript_dir='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/'+postscript_dir+'/'
cor_directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/correlations/'

common func_chi_xy, dis, xtrans

readcol,stats_directory+'Depth_0Correlations.txt',xtrans,format='(x,f,x,x)'
dis=findgen(25)*.17
xtrans=Ang_factor*xtrans
xtranskeep=xtrans

rxt=findgen(4)

b=11./3.
kl=.097
ko=.18
a=float(1./(b-4))
c=float(b-2)
y=0.097*((dis+(ko/kl)^a*0.17)/.15)^c*(5e-7/(dis+(ko/kl)^a*.17))^2
;Minimize the difference
rxt=amoeba(1.0e-5,scale=[0.4,0.2,3,.5],p0=[0.4,0.2,11./3.,.5],$
	function_value=fval,function_name='fit_function_chi',ncalls=tries,$
	nmax=1000000)

a=float(1./(rxt(2)-4))
b=float(rxt(2)-2)
k=float(rxt(0)/rxt(1))
fitxt=fltarr(N_elements(dis))
fitxt=rxt(1)*(((dis+(k^a)*.17)/rxt(3))^b*(5e-7/(dis+(k^a)*.17))^2)
common func_chi_xy, dis, ytrans
readcol,stats_directory+'Depth_0Correlations.txt',ytrans,$
        format='(x,x,x,f)'
dis=findgen(25)*.17
ytrans=Ang_factor*ytrans
ytranskeep=ytrans

ryt=amoeba(1.0e-5,scale=[0.4,0.2,3,.5],p0=[0.4,0.21,11./3.,.5],$
        function_value=fval,function_name='fit_function_chi',ncalls=tries,$
        nmax=100000)
a=float(1./(ryt(2)-4))
b=float(ryt(2)-2)
k=float(ryt(0)/ryt(1))
fityt=fltarr(N_elements(dis))
fityt=ryt(1)*(((dis+(k^a)*.17)/ryt(3))^b*(5e-7/(dis+(k^a)*.17))^2)
common func_chi_xy, dis, ylong
readcol,stats_directory+'Depth_0Correlations.txt',ylong,$
        format='(x,x,f,x)'
dis=findgen(25)*.17
ylong=Ang_factor*ylong
ylongkeep=ylong                                                                                        
                                                                                        
ryl=amoeba(1.0e-5,scale=[0.4,0.2,3,.5],p0=[0.4,0.2,11./3.,.5],$
        function_value=fval,function_name='fit_function_chi',ncalls=tries,$
        nmax=100000)
a=float(1./(ryl(2)-4))
b=float(ryl(2)-2)
k=float(ryl(0)/ryl(1))
fityl=fltarr(N_elements(dis))
fityl=ryl(1)*(((dis+(k^a)*.17)/ryl(3))^b*(5e-7/(dis+(k^a)*.17))^2)
common func_chi_xy, dis, xlong

readcol,stats_directory+'Depth_0Correlations.txt',xlong,$
        format='(f,x,x,x)'
dis=findgen(25)*.17
xlong=Ang_factor*xlong
xlongkeep=xlong                                                                                         
rxl=amoeba(1.0e-5,scale=[0.2,0.2,3,.5],p0=[0.19,0.19,11./3.,.5],$
        function_value=fval,function_name='fit_function_chi',ncalls=tries,$
        nmax=100000)
a=float(1./(rxl(2)-4))
b=float(rxl(2)-2)
k=float(rxl(0)/ryl(1))
fitxl=fltarr(N_elements(dis))
fitxl=rxl(1)*(((dis+(k^a)*.17)/rxl(3))^b*(5e-7/(dis+(k^a)*.17))^2)


set_plot,'ps'
device,filename=Postscript_dir+'Correlations_Fitted_Chi.ps'
                                                                                
loadct,39                       ;load color table 39
device,/color                   ;allow color on the postscript
device,ysize=8.5,/inches        ;Height of plot in y
device,xsize=10.0,/inches        ;Width of plot in x
device,yoffset=1.0,/inches      ;Y position of lower left corner
                                                                                
white='FFFFFF'x
black='000000'x
!P.CHARSIZE=1.2
!P.THICK=4.

sym_arr=[2,0]
items=['Data Points','Fit']

;**************PLOT XTRANS AND FIT
plot,fitxt,psym=0,title="Fit for Transverse X Covariance (5-"+Date+')',$
	xtitle="n",$
	ytitle="Radians Squared"

oplot,xtranskeep,psym=1

legend,items,psym=sym_arr
xyouts,.7,.7,'k!l0!N='+strtrim(string(rxt(1)),2),/normal
xyouts,.7,.65,'k!lT!N='+strtrim(string(rxt(0)),2),/normal
xyouts,.7,.6,'r!lo!N='+strtrim(string(rxt(3)),2),/normal
xyouts,.7,.55,'!4b!6=='+strtrim(string(rxt(2)),2),/normal

;*************PLOT XLONG AND FIT
plot,fitxl,psym=0,title="Fit for Longitudinal X Covariance (5-"+Date+')',$
	 xtitle="n",$
         ytitle="Radians Squared"

oplot,xlongkeep,psym=1
legend,items,psym=sym_arr
xyouts,.7,.7,'k!l0!N='+strtrim(string(rxl(1)),2),/normal
xyouts,.7,.65,'k!lL!N='+strtrim(string(rxl(0)),2),/normal
xyouts,.7,.6,'r!lo!N='+strtrim(string(rxl(3)),2),/normal
xyouts,.7,.55,'!4b!6='+strtrim(string(rxl(2)),2),/normal

;*************PLOT YTRANS AND FIT
plot,fityt,psym=0,title="Fit for Transverse Y Covariance (5-"+Date+')',$
	 xtitle="n",$
         ytitle="Radians Squared"

oplot,ytranskeep,psym=1
                                                                                        
legend,items,psym=sym_arr
xyouts,.7,.7,'k!l0!N='+strtrim(string(ryt(1)),2),/normal
xyouts,.7,.65,'k!lT!N='+strtrim(string(ryt(0)),2),/normal
xyouts,.7,.6,'r!lo!N='+strtrim(string(ryt(3)),2),/normal
xyouts,.7,.55,'!4b!6='+strtrim(string(ryt(2)),2),/normal

;*************PLOT YTRANS AND FIT
plot,fityl,psym=0,title="Fit for Longitudinal Y Covariance (5-"+Date+')',$
	 xtitle="n",$
         ytitle="Radians Squared"

oplot,ylongkeep,psym=1
                                                                                        
legend,items,psym=sym_arr
xyouts,.7,.7,'k!l0!N='+strtrim(string(ryl(1)),2),/normal
xyouts,.7,.65,'k!lL!N='+strtrim(string(ryl(0)),2),/normal
xyouts,.7,.6,'r!lo!N='+strtrim(string(ryl(3)),2),/normal
xyouts,.7,.55,'!4b!6='+strtrim(string(ryl(2)),2),/normal


device,/close
set_plot,'X

stop

end
