;xy_offsets
;
;PURPOSE:
;	This script will return the difference in coordinates between a 
;	ShackHartman image centroid and the corresponding point on the 
;	reference grid
;
;	Since the center of the grid seems to be drifting 

pro xy_offsets_cn_time_jpeg,Date=Date,stats_directory=stats_directory,$
	x_center_file=x_center_file,y_center_file=y_center_file,$
	depth=depth


If (not keyword_set(Date)) then Date='10'
If (not keyword_set(stats_directory)) then stats_directory='stats_temp'
If (not keyword_set(x_center_file)) then x_center_file='x_centers.txt'
If (not keyword_set(y_center_file)) then y_center_file='y_centers.txt'
If (not keyword_set(letter)) then letter=['a','b','c','d','e','f','g','h','i']
If (not keyword_set(depth)) then depth=2
If (not keyword_set(postscript_dir)) then postscript_dir='postscripts'

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+'/'
jpeg_dir='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/jpegs/'

;CONSTANTS
num_of_files=900
num=25
grid_spots=625
index=findgen(625)

;ARRAY REFERENCE 25x25=625 FOR GRID
subx=2*indgen(num)-25           ;The x-reference numbers
suby=indgen(num)-12             ;The y-reference numbers

;HUGE DATA CUBE
x_offset_hor=fltarr(900,625)
y_offset_hor=fltarr(900,625)
x_offset_ver=fltarr(900,625)
y_offset_ver=fltarr(900,625)

;Correlations
Cxt_full=fltarr(900,num,depth)
Cxl_full=fltarr(900,num,depth)
Cyt_full=fltarr(900,num,depth)
Cyl_full=fltarr(900,num,depth)


for file_index=0,num_of_files-1 do begin

	;Take the results from the offset.txt files and fill the cube
	offset_name=Directory+'hr5132'+letter(((file_index)/100))+'_'+$
                  strtrim(string(((file_index) mod 100)+1),2)+'_offsets.txt'
	

	readcol,offset_name,x_offset,y_offset,format='X,X,f,X,f'
	

	not_fitted=where(x_offset eq -1,complement=fitted)
	
	;Obtain the difference ordered in COLUMN, ROW between centroid and grid 	point
	x_offset_hor(file_index,*)=x_offset
	y_offset_hor(file_index,*)=y_offset

	;Obtain the difference ordered in ROW, COLUMN
	x_offset_ver(file_index,*)=x_offset((index mod 25)*25+index/25)
	y_offset_ver(file_index,*)=y_offset((index mod 25)*25+index/25)
	
endfor
	
	;DATA CUBEs are now in place. Begin the covariance

;Loop through all of the files in the 
for file_index=0,num_of_files-depth do begin

	;CORRELATIONS
	Cxl=fltarr(num,depth)
	Cxt=fltarr(num,depth)
	Cyl=fltarr(num,depth)
	Cyt=fltarr(num,depth)

	for time=0, depth-1 do begin

		for spacing=0, num-1 do begin
	
			Cxl_arr=fltarr(num*num)
			Cxt_arr=fltarr(num*num)
			Cyl_arr=fltarr(num*num)
			Cyt_arr=fltarr(num*num)

			;SCAN FIRST ALONG THE ROWS
			;C_num=0
	
			for column_index=0, num-1 do begin
				for row_index=0, num -1 do begin
				spot_index=column_index*num+row_index
			
				;If we have reached the edge, start on the next row
				If (row_index+spacing) ge num then break
				;Check to see if both have valid values
				If (x_offset_hor(file_index,spot_index) ne -1) and $
				   (x_offset_hor(file_index+time,spot_index+spacing) ne -1) then begin

					Cxl_arr(spot_index)=x_offset_hor(file_index,spot_index)*$
					  x_offset_hor(file_index+time,spot_index+spacing)
					Cxt_arr(spot_index)=y_offset_hor(file_index,spot_index)*$
                                          y_offset_hor(file_index+time,spot_index+spacing)
				;print,FORMAT='(%"%i\t%i\t")',spot_index,spot_index+spacing
	
			;	C_num=C_num+1
				endif

				endfor
			endfor
		
			correlated=where(Cxl_arr ne 0)
			If correlated(0) ne -1 then begin
				Cxl(spacing,time)=mean(Cxl_arr(correlated))
				Cxt(spacing,time)=mean(Cxt_arr(correlated))
			endif else begin
				Cxl(spacing,time)=0
				Cxt(spacing,time)=0
			endelse
		
			;printf,xcor,FORMAT='(%"%f\t%f\t%i")',Cxl(spacing-1),Cxt(spacing-1),N_elements(correlated)
		
	
			;SCAN SECOND ALONG Y
	; C_num=0

               		 for column_index=0, num-1 do begin
                       		 for row_index=0, num -1 do begin
                       			 spot_index=column_index*num+row_index
	
        	               		 ;If we have reached the edge, start on the next row
                		        If (row_index+spacing) ge num then break
                     			;Check to see if both have valid values
                       			If (y_offset_ver(file_index,spot_index) ne -1) and $
                           		(y_offset_ver(file_index+time,spot_index+spacing) ne -1) then begin

                                		Cyl_arr(spot_index)=y_offset_ver(file_index,spot_index)*$
                                          	y_offset_ver(file_index+time,spot_index+spacing)
                                		Cyt_arr(spot_index)=x_offset_ver(file_index,spot_index)*$
                                          	x_offset_ver(file_index+time,spot_index+spacing)
                        ;print,FORMAT='(%"%i\t%i\t")',spot_index,spot_index+spacing

         ;               C_num=C_num+1
          	                         endif

                       		 endfor
               		 endfor

               		 correlated=where(Cyl_arr ne 0)
              		 If correlated(0) ne -1 then begin
                       		 Cyl(spacing,time)=mean(Cyl_arr(correlated))
                        	 Cyt(spacing,time)=mean(Cyt_arr(correlated))
                         endif else begin
                        	 Cyl(spacing,time)=0
                       		 Cyt(spacing,time)=0
                	endelse

                ;printf,ycor,FORMAT='(%"%f\t%f\t%i")',Cyl(spacing-1),Cyt(spacing-1),N_elements(correlated)

     	   endfor
	
	endfor	
	;partvelvec,x_offset(fitted),y_offset(fitted),gridx(fitted),$
	;	gridy(fitted),yrange=[150,800],xrange=[200,850]

	;close,xcor
	;free_lun,xcor		
	;close,ycor
	;free_lun,ycor
	
	Cxt_full(file_index,*,*)=Cxt
	Cxl_full(file_index,*,*)=Cxl
	Cyt_full(file_index,*,*)=Cyt
	Cyl_full(file_index,*,*)=Cyl


endfor	
		

Cxt_means=fltarr(num,depth)
Cxl_means=fltarr(num,depth)
Cyt_means=fltarr(num,depth)
Cyl_means=fltarr(num,depth)

for n=0, num-1 do begin
	for m=0, depth-1 do begin

	Cxt_means(n,m)=mean(Cxt_full(*,n,m))
	Cxl_means(n,m)=mean(Cxl_full(*,n,m))
	Cyl_means(n,m)=mean(Cyl_full(*,n,m))
	Cyt_means(n,m)=mean(Cyt_full(*,n,m))
	endfor
endfor

set_plot,'Z'
;device,filename=Postscript_dir+'Cn_time'+string(strtrim(depth,2))+'.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
device, set_resolution=[8000,6000]
device, set_font='Courier'


white='FFFFFF'x
black='000000'x                                                                   
!P.CHARSIZE=10
!P.CHARTHICK=15.
!X.THICK=20.
!Y.THICK=20.
!P.THICK=20.


items_time=['t=0']
sym_time=[1]
items_k=['Data Points','Kolmogorov (n!e1/3!n)']
sym_k=[1,0]

;*************FIRST PLOT
plot,Cxt_means(*,0),psym=1,title="Transverse correlation along X direction",$
               xtitle="n",ytitle="Covariance (Pixels Squared)",$
               background=white,color=black,$
               yrange=[min(Cxt_means(*,0))-.5,max(Cxt_means(*,0))+.5],$
               POSITION=[0.10,0.10,0.9,0.9],$
	       symsize=9

;Over Plot the various separations in time and also generate legends

for i=1,depth-1 do begin

	oplot,Cxt_means(*,i),psym=i+1,color=black,symsize=9
	sym_time=[sym_time,i+1]
	items_time=[items_time,string(strtrim(i*7,2))+' seconds']
	
endfor

legend,items_time,psym=sym_time,position=[.7,.8],/normal,color=black,$
	textcolors=black,outline_color=black

jpgimg=tvrd()
write_jpeg,jpeg_dir+'Cxt_time'+string(strtrim(depth,2))+'.jpg',congrid(jpgimg, 1600, 1200, /interp, /center), quality=100

erase

;*************SECOND PLOT
plot,(Cxt_means(*,0)-Cxt_means(*,depth-1)),psym=1,$
	       title="Transverse correlation along X direction for t=0-t="+$
	       strtrim(string(depth*7),2)+' seconds',$
               xtitle="n",ytitle="Covariance (Pixels Squared)",$
               background=white,color=black,$
               yrange=[0,2],$
               POSITION=[0.10,0.10,0.9,0.9],$
	       symsize=9

oplot,findgen(num-1)+1,(findgen(num-1)+1)^(-1./3.),psym=0,color=black

legend,items_k,psym=sym_k,position=[.6,.8],/normal,color=black,$
        textcolors=black,outline_color=black


jpgimg=tvrd()
write_jpeg,jpeg_dir+'Cxt_subt'+string(strtrim(depth,2))+'.jpg',congrid(jpgimg, 1600, 1200, /interp, /center), quality=100

erase

;***************THIRD PLOT
plot,Cxl_means(*,0),psym=1,title="Longitudinal correlation along X direction",$
               xtitle="n",ytitle="Covariance (Pixels Squared)",$
               background=white,color=black,$
               yrange=[min(Cxl_means(*,0))-.5,max(Cxl_means(*,0))+.5],$
               POSITION=[0.10,0.10,0.9,0.9],$
	       symsize=9

for i=1,depth-1 do begin
	
        oplot,Cxl_means(*,i),psym=i+1,color=black,symsize=9
	
endfor

legend,items_time,psym=sym_time,position=[.7,.8],/normal,color=black,$
        textcolors=black,outline_color=black

jpgimg=tvrd()
write_jpeg,jpeg_dir+'Cxl_time'+string(strtrim(depth,2))+'.jpg',congrid(jpgimg, 1600, 1200, /interp, /center), quality=100

erase 

;**************FOURTH PLOT
plot,(Cxl_means(*,0)-Cxl_means(*,depth-1)),psym=1,$
               title="Longitudinal correlation along X direction for t=0-t="+$
               strtrim(string(depth*7),2)+' seconds',$
               xtitle="n",ytitle="Covariance (Pixels Squared)",$
               background=white,color=black,$
               yrange=[0,max(Cxl_means(*,0)-Cxl_means(*,depth-1))+.5],$
               POSITION=[0.10,0.10,0.9,0.9],$
	       symsize=9

oplot,findgen(num-1)+1,(findgen(num-1)+1)^(-1./3.),psym=0,color=black

legend,items_k,psym=sym_k,position=[.6,.8],/normal,color=black,$
        textcolors=black,outline_color=black


jpgimg=tvrd()
write_jpeg,jpeg_dir+'Cxl_subt'+string(strtrim(depth,2))+'.jpg',congrid(jpgimg, 1600, 1200, /interp, /center), quality=100

erase

;**************FIFTH PLOT
plot,Cyt_means(*,0),psym=1,title="Transverse correlation along Y direction",$
               xtitle="n",ytitle="Covariance (Pixels Squared)",$
               background=white,color=black,$
               yrange=[min(Cyt_means(*,0))-.5,max(Cyt_means(*,0))+.5],$
               POSITION=[0.10,0.10,0.9,0.9],$
	       symsize=9

for i=1,depth-1 do begin

       oplot,Cyt_means(*,i),psym=i+1,color=black,symsize=9

endfor

legend,items_time,psym=sym_time,position=[.7,.8],/normal,color=black,$
        textcolors=black,outline_color=black

jpgimg=tvrd()
write_jpeg,jpeg_dir+'Cyt_time'+string(strtrim(depth,2))+'.jpg',congrid(jpgimg, 1600, 1200, /interp, /center), quality=100

erase

;***************SIXTH PLOT
plot,(Cyt_means(*,0)-Cyt_means(*,depth-1)),psym=1,$
               title="Transverse correlation along Y direction for t=0-t="+$
               strtrim(string(depth*7),2)+' seconds',$
               xtitle="n",ytitle="Covariance (Pixels Squared)",$
               background=white,color=black,$
               yrange=[min(Cyt_means(*,0)-Cyt_means(*,depth-1))-.5,$
                       max(Cyt_means(*,0)-Cyt_means(*,depth-1))+.5],$
               POSITION=[0.10,0.10,0.9,0.9],$
	       symsize=9

oplot,findgen(num-1)+1,(findgen(num-1)+1)^(-1./3.),psym=0,color=black

legend,items_k,psym=sym_k,position=[.6,.8],/normal,color=black,$
        textcolors=black,outline_color=black


jpgimg=tvrd()
write_jpeg,jpeg_dir+'Cyt_sub'+string(strtrim(depth,2))+'.jpg',congrid(jpgimg, 1600, 1200, /interp, /center), quality=100

erase

;***************SEVENTH PLOT
plot,Cyl_means(*,0),psym=1,title="Longitudinal correlation along Y direction",$
               xtitle="n",ytitle="Covariance (Pixels Squared)",$
               background=white,color=black,$
               yrange=[min(Cyl_means(*,0))-.5,max(Cyl_means(*,0))+.5],$
               POSITION=[0.10,0.10,0.9,0.9],$
	       symsize=9

for i=1,depth-1 do begin

       oplot,Cyl_means(*,i),psym=i+1,symsize=9,color=black

endfor

legend,items_time,psym=sym_time,position=[.7,.8],/normal,color=black,$
        textcolors=black,outline_color=black

jpgimg=tvrd()
write_jpeg,jpeg_dir+'Cyl_time'+string(strtrim(depth,2))+'.jpg',congrid(jpgimg, 1600, 1200, /interp, /center), quality=100

erase

;***************EIGTH PLOT
plot,(Cyl_means(*,0)-Cyl_means(*,depth-1)),psym=1,$
               title="Longitudinal correlation along Y direction for t=0-t="+$
               strtrim(string(depth*7),2)+' seconds',$
               xtitle="n",ytitle="Covariance (Pixels Squared)",$
               background=white,color=black,$
               yrange=[0,max(Cyl_means(*,0)-Cyl_means(*,depth-1))+.5],$
               POSITION=[0.10,0.10,0.9,0.9],$
               symsize=9

oplot,findgen(num-1)+1,(findgen(num-1)+1)^(-1./3.),psym=0,color=black

legend,items_k,psym=sym_k,position=[.6,.8],/normal,color=black,$
        textcolors=black,outline_color=black


jpgimg=tvrd()
write_jpeg,jpeg_dir+'Cyl_sub'+string(strtrim(depth,2))+'.jpg',congrid(jpgimg, 1600, 1200, /interp, /center), quality=100

erase

device,/close
set_plot,'X'



end

