;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,Date=Date,stats_directory=stats_directory,$
	x_center_file=x_center_file,y_center_file=y_center_file


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']


Directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
	  '/ShackHartman/'+stats_directory+'/'

readcol,Directory+x_center_file,x_fit_center,format='X,X,f'
readcol,Directory+y_center_file,y_fit_center,format='X,X,f'


;CONSTANTS
num_of_files=N_elements(x_fit_center)
num=25

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

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


for file_index=0,num_of_files-1 do begin

	;Take the centroid values from the stats.txt files
	file_name=Directory+'hr5132'+letter(((file_index +1)/100))+'_'+$
		  strtrim(string(((file_index) mod 100)+1),2)+'_stats.txt'
	;Print the results to the offset.txt files
	stat_name=Directory+'hr5132'+letter(((file_index +1)/100))+'_'+$
                  strtrim(string(((file_index) mod 100)+1),2)+'_offsets.txt'
	;Get the Cn's and print them to the correlations.txt files
	xcor_name=Directory+'hr5132'+letter(((file_index +1)/100))+'_'+$
                  strtrim(string(((file_index) mod 100)+1),2)+'_xcorrelations.txt'
	ycor_name=Directory+'hr5132'+letter(((file_index +1)/100))+'_'+$
                  strtrim(string(((file_index+1) mod 100)+1),2)+'_ycorrelations.txt'

	
	get_lun,xcor
	openw,xcor,xcor_name
	get_lun,ycor
	openw,ycor,ycor_name

	readcol,file_name,grid_index,x_coord,y_coord,format='f,f,f'
	grid_spots=N_elements(x_coord)

	;Figure out the new reference grid, applied around x_center and y_center
        x_ref_col=12.5*subx+x_fit_center(file_index)
        y_ref_col=25.0*suby+y_fit_center(file_index)
	index=findgen(num*num)
	gridx=fltarr(num*num)
	gridy=fltarr(num*num)
	gridx(index)=x_ref_col(index mod num)
	gridy(index)=y_ref_col(index/num)

	not_fitted=where(x_coord eq -1,complement=fitted)

	;Obtain the difference ordered in COLUMN, ROW between centroid and grid point
	x_offset=x_coord-gridx
	y_offset=y_coord-gridy

	;If point was not fitted, set to -1
 	x_offset(not_fitted)=-1
	y_offset(not_fitted)=-1	

	;Obtain the difference ordered in ROW, COLUMN
	x_offset_ver=x_offset((index mod 25)*25+index/25)
	y_offset_ver=y_offset((index mod 25)*25+index/25)
	;x_coord_ver=x_coord((index mod 25)*25+index/25)	
	
	;All 625 points and their corresponding displacements are available 
	;here
	
	;Start by looking at the correlation at a spacing of 1 and then move up
	;to 25

	;CORRELATIONS
	Cxl=fltarr(24)
	Cxt=fltarr(24)
	Cyl=fltarr(24)
	Cyt=fltarr(24)

	for spacing=1, 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(spot_index) ne -1) and $
			   (x_offset(spot_index+spacing) ne -1) then begin

				Cxl_arr(spot_index)=x_offset(spot_index)*$
					  x_offset(spot_index+spacing)
				Cxt_arr(spot_index)=y_offset(spot_index)*$
                                          y_offset(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-1)=mean(Cxl_arr(correlated))
			Cxt(spacing-1)=mean(Cxt_arr(correlated))
		endif else begin
			Cxl(spacing-1)=0
			Cxt(spacing-1)=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(spot_index) ne -1) and $
                           (y_offset_ver(spot_index+spacing) ne -1) then begin

                                Cyl_arr(spot_index)=y_offset_ver(spot_index)*$
                                          y_offset_ver(spot_index+spacing)
                                Cyt_arr(spot_index)=x_offset_ver(spot_index)*$
                                          x_offset_ver(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-1)=mean(Cyl_arr(correlated))
                        Cyt(spacing-1)=mean(Cyt_arr(correlated))
                endif else begin
                        Cyl(spacing-1)=0
                        Cyt(spacing-1)=0
                endelse

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

        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
 	
	
	
wait,.2	
	
endfor

Cxt_means=fltarr(24)
Cxl_means=fltarr(24)
Cyt_means=fltarr(24)
Cyl_means=fltarr(24)

for n=0, 23 do begin

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

!p.multi=[0,2,2]

plot,Cxt_means
plot,Cxl_means
plot,Cyl_means
plot,Cyt_means

stop

end

