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


pro fit_simul_wind_one_plot,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/final_report_figs/'
cor_directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/correlations/'

common func_simul_wind_xy, dis, xtrans, xlong, ytrans, ylong
                                                                          
readcol,stats_directory+'Depth_0Correlations.txt',$  
       xlong,xtrans,ylong,ytrans,format='(f,f,f,f)'
                                                                          
dis=findgen(25)*.17
weight=[.03,.03,.03,.03,.03,.03,.03,.03,.03,.03,$
        .03,.03,.03,.03,.03,.03,.03,.03,.03,.03,$
        .03,.03,.03,.03]
                                                                          
xtrans=Ang_factor*xtrans
xtranskeep=xtrans
xlong=Ang_factor*xlong
xlongkeep=xlong
ytrans=Ang_factor*ytrans
ytranskeep=ytrans
ylong=Ang_factor*ylong
ylongkeep=ylong
rs=findgen(6)
                                                                                                                                                     
;The function we are trying to fit is
;y=((d+(k0/kl)^(1/(b-4))*D)/po))^(b-2)
;
;p(0)=ko
;p(1)=wind
;p(2)=kl
;p(3)=kt
;p(4)=b
;p(5)=po
                                                                          
;Minimize the difference
if date eq '10' or date eq '12' then begin
rs=amoeba(1.0e-5,scale=[0.1,0.2,.2,.3,.3,.2],p0=[0.2,1,.2,.1,11./3.,.2],$
        function_value=fval,function_name='fit_simul_wind_function',ncalls=tries,$
        nmax=1000000)
endif else begin
rs=amoeba(1.0e-6,scale=[0.05,0.1,.1,.3,.3,.2],p0=[0.1,1,.1,.3,11./3.,.3],$        function_value=fval,function_name='fit_simul_wind_function',ncalls=tries,$
        nmax=1000000)
endelse

a=float(1./(rs(4)-4))
b=float(rs(4)-2)
koxl=float(rs(0)/rs(2))
koyl=float(rs(0)/rs(2))
koxt=float(rs(0)/rs(3))
koyt=float(rs(0)/rs(3))
                                                                          
fitxt=fltarr(N_elements(dis))
fitxt=rs(3)*(((dis+(koxt^a)*.17)/rs(5))^b*(5e-7/(dis+(koxt^a)*.17))^2)
fitxl=fltarr(N_elements(dis))
fitxl=rs(1)*rs(2)*(((dis+(koxl^a)*.17)/rs(5))^b*(5e-7/(dis+(koxl^a)*.17))^2)
fityt=fltarr(N_elements(dis))
fityt=rs(1)*rs(3)*(((dis+(koyt^a)*.17)/rs(5))^b*(5e-7/(dis+(koyt^a)*.17))^2)
fityl=fltarr(N_elements(dis))
fityl=rs(2)*(((dis+(koyl^a)*.17)/rs(5))^b*(5e-7/(dis+(koyl^a)*.17))^2)

rsk=findgen(6)

;The function we are trying to fit is
;y=((d+(k0/kl)^(1/(b-4))*D)/po))^(b-2)
;
;p(0)=ko
;p(1)=wind
;p(2)=kl
;p(3)=kt
;p(4)=b
;p(5)=po

;Minimize the difference

common func_fit_kolmogorov, disk, xtransk, xlongk, ytransk, ylongk

readcol,stats_directory+'Depth_0Correlations.txt',$
        xlongk,xtransk,ylongk,ytransk,format='(f,f,f,f)'

disk=findgen(25)*.17
weightk=[.03,.03,.03,.03,.03,.03,.03,.03,.03,.03,$
        .03,.03,.03,.03,.03,.03,.03,.03,.03,.03,$
        .03,.03,.03,.03]

xtransk=Ang_factor*xtransk
xtranskeepk=xtransk
xlongk=Ang_factor*xlongk
xlongkeepk=xlongk
ytransk=Ang_factor*ytransk
ytranskeepk=ytransk
ylongk=Ang_factor*ylongk
ylongkeepk=ylongk

if date eq '10' or date eq '12' then begin
rsk=amoeba(1.0e-5,scale=[0.2,0.2,.2,.3,.3,.2],p0=[0.2,1,.2,.1,11./3.,.3],$
        function_value=fval,function_name='fit_kolmogorov_function',ncalls=tries,$
        nmax=1000000)
endif else begin
rsk=amoeba(1.0e-6,scale=[0.1,0.1,.1,.3,.3,.1],p0=[0.1,1,.2,.3,11./3.,.1],$
        function_value=fval,function_name='fit_kolmogorov_function',ncalls=tries,$
        nmax=1000000)
endelse

;Kl is defined in terms of kt
rsk(4)=11./3
rsk(2)=(rsk(4)-3)*rsk(3)
ak=float(1./(rsk(4)-4))
bk=float(rsk(4)-2)
koxlk=float(rsk(0)/rsk(2))
koylk=float(rsk(0)/rsk(2))
koxtk=float(rsk(0)/rsk(3))
koytk=float(rsk(0)/rsk(3))

fitxtk=fltarr(N_elements(dis))
fitxtk=rsk(3)*(((dis+(koxtk^ak)*.17)/rsk(5))^bk*(5e-7/(dis+(koxtk^ak)*.17))^2)
fitxlk=fltarr(N_elements(dis))
fitxlk=rsk(1)*rsk(2)*(((dis+(koxlk^a)*.17)/rsk(5))^bk*(5e-7/(dis+(koxlk^ak)*.17))^2)
fitytk=fltarr(N_elements(dis))
fitytk=rsk(1)*rsk(3)*(((dis+(koytk^a)*.17)/rsk(5))^bk*(5e-7/(dis+(koytk^ak)*.17))^2)
fitylk=fltarr(N_elements(dis))
fitylk=rsk(2)*(((dis+(koylk^ak)*.17)/rsk(5))^bk*(5e-7/(dis+(koylk^ak)*.17))^2)


set_plot,'ps'
device,filename=Postscript_dir+'Correlations_5-'+Date+'-2005.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="Fits for 5/"+Date+'/2005',$
	ytitle='Radians Squared',xtitle="n",$
	yrange=[0,2.5e-12],$
	position=[0.15,.1,.9,.9],/normal
oplot,xtranskeep,psym=1

oplot,fitxl,psym=0

oplot,xlongkeep,psym=2

oplot,fityt,psym=0

oplot,ytranskeep,psym=4

oplot,fityl,psym=0

oplot,ylongkeep,psym=5

device,/close

set_plot,'z'
erase
device, set_font='Courier'
device,set_resolution=[700,600]
!p.charsize=.8
!p.charthick=1.2
!x.thick=2
!y.thick=2
!p.thick=1
!p.noerase=0
red='FF0000'x

colors=[70,210,140,0]
sym_arr=[2,5,4,1]
items=['X Longitudinal','Y Longitudinal','Y Transverse','X Transverse']

plot,fitxt,psym=0,title="Fits for 5/"+Date+'/2005',$
        ytitle='Radians Squared',xtitle="n",$
        yrange=[0,2.5e-12],$
        position=[0.15,.1,.9,.9],/normal,$
	background=white,color=black

oplot,xtranskeep,psym=1,color=0

oplot,fitxl,psym=0,color=black

oplot,xlongkeep,psym=2,color=70

oplot,fityt,psym=0,color=black

oplot,ytranskeep,psym=4,color=140

oplot,fityl,psym=0,color=black


oplot,ylongkeep,psym=5,color=210

legend,items,color=colors,psym=sym_arr,textcolors=[0,0,0,0]

jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,Postscript_dir+'Wind_No_Dainty_Correlations_5-'+Date+'-2005.png',$

jpgimg,reds,greens,blues

set_plot,'z'
erase
device, set_font='Courier'
device,set_resolution=[700,600]
!p.charsize=.8
!p.charthick=1.2
!x.thick=2
!y.thick=2
!p.thick=1
!p.noerase=0
red='FF0000'x

colors=[70,210,140,0]
sym_arr=[2,5,4,1]
items=['X Longitudinal','Y Longitudinal','Y Transverse','X Transverse']
erase

plot,fitxtk,psym=0,title="Fits for 5/"+Date+'/2005',$
        ytitle='Radians Squared',xtitle="n",$
        yrange=[0,2.5e-12],$
        position=[0.15,.1,.9,.9],/normal,$
        background=white,color=black

oplot,xtranskeep,psym=1,color=0

oplot,fitxlk,psym=0,color=black

oplot,xlongkeep,psym=2,color=70

oplot,fitytk,psym=0,color=black

oplot,ytranskeep,psym=4,color=140

oplot,fitylk,psym=0,color=black

oplot,ylongkeep,psym=5,color=210

legend,items,color=colors,psym=sym_arr,textcolors=[0,0,0,0],position=[.7,.85],/normal
jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,Postscript_dir+'Kolmogorov_Correlations_5-'+Date+'-2005.png',$
jpgimg,reds,greens,blues

erase


end
