;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 cn0_full_angle,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,num_stats_files=num_stats_files


If (not keyword_set(Date)) then Date='10'
If (not keyword_set(stats_directory)) then stats_directory='stats'
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(depth)) then depth=1
If (not keyword_set(postscript_dir)) then postscript_dir='postscripts'
If (not keyword_set(x_off)) then x_off=0
If (not keyword_set(y_off)) then y_off=0

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

;ARRAYS OF FILES-Averages have already been subtracted
offset_files=file_search(stats_directory+'*5132*offsets.txt',count=num_off_files)
If (not keyword_set(num_stats_files)) then num_stats_files=num_off_files

;CONSTANTS
num=25
grid_spots=625
index=findgen(625)
num_angles=20

;DATA CUBE CONTAINING ALL ANGLES AND MAGNITUDES
phi_arr=fltarr(num_stats_files,grid_spots)
magnitude_arr=fltarr(num_stats_files,grid_spots)

;FILL THE DATA CUBES
for file_number=0, num_stats_files-1 do begin

	;Take the magnitude and angles from the offset.txt files and fill the cube
	readcol,offset_files(file_number),magnitude,phi,format='X,X,X,X,X,f,f'
	magnitude_arr(file_number,*)=magnitude
	phi_arr(file_number,*)=phi

endfor

;DATA CUBEs are now in place. Begin the covariance
;CORRELATION ARRAYS
Cl_arr=fltarr(num_stats_files,num_angles+1,depth)
Ct_arr=fltarr(num_stats_files,num_angles+1,depth)
num_spots_per_file=indgen(num_stats_files)
angles=fltarr(num_angles+1)

;Loop through all of the files in the stack
for file_number=0, num_stats_files-depth do begin
  print,file_number,offset_files(file_number)

  for time=0, depth-1 do begin
    
    for angle=0, num_angles do begin
      
     
	;THE ANGLE AND SEPARTATION FOR A GIVEN SET OF DELTAS
	grid_phi=2*!pi*float(angle)/float(num_angles)
	if file_number eq 0 then angles(angle)=grid_phi
         
	;Cl=fltarr(grid_spots,depth)
        Ct=fltarr(grid_spots)
	Cl=fltarr(grid_spots)

	;This index represents the number of points going into the sum for
	;a given separation
	c_index=0

	  for spot_index=0, grid_spots-1 do begin
	  
	      ;If it is on the grid, check to see if it has a partner
	        if (phi_arr(file_number,spot_index) ne -10) then begin
                ;Longitudinal will be cosine and transverse will be sine
	        Cl(c_index,time)=(magnitude_arr(file_number,spot_index)*$
			 cos(phi_arr(file_number,spot_index)-grid_phi))^2
	        Ct(c_index,time)=(magnitude_arr(file_number,spot_index)*$
                         sin(phi_arr(file_number,spot_index)-grid_phi))^2
	        c_index=c_index+1

	        endif

          endfor
      
	
	 ;Correlations for a given delta_x and delta_y are now in place
	 correlated=where(Cl ne 0)
	 num_spots_per_file(file_number)=c_index
	 ;If there were any pairs for a covariance, get there mean
	 If correlated(0) ne -1 then begin
	    Cl_arr(file_number,angle,time)=$
	    mean(Cl(correlated))
	    Ct_arr(file_number,angle,time)=$
	    mean(Ct(correlated))
	
	endif else begin
	 ;If there were no pairs, assign a system no value
	    Cl_arr(file_number,angle,time)=!values.f_nan
	    Ct_arr(file_number,angle,time)=!values.f_nan
	 endelse
	   

    endfor

  endfor

endfor

Cl_means=fltarr(num_angles+1)
Ct_means=fltarr(num_angles+1)

;Get the average for each of these guys
for n=0, num_angles do begin

  finite_val=where(finite(cl_arr(*,n)) eq 1) 
  if finite_val(0) ne -1 then begin
  Cl_means(n)=mean(cl_arr(where(finite(cl_arr(*,n)) eq 1),n))
  Ct_means(n)=mean(ct_arr(where(finite(ct_arr(*,n)) eq 1),n))
  endif else begin
  Cl_means(n)=!values.f_nan
  Ct_means(n)=!values.f_nan
  endelse

endfor

angles_degrees=180*angles/(!pi)


set_plot,'ps'
device,filename=Postscript_dir+'Cn0_vs_angle'+date+'.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=.7
!P.THICK=4.

color_arr=[0]
angle_arr=[' ']
sym_arr=[0]

;Set up plot 
plot,angles_degrees,Cl_means,background=white,position=[0.10,0.10,0.9,0.9],$
     title='Average Variance at different angles for 05/'+$
        Date+'/2005',xtitle='Rotation of axes (Degrees)',ytitle='Variance',psym=1,$
	yrange=[.5,1.5]

oplot,angles_degrees,Ct_means,psym=2

legend,['Transverse','Longitudinal'],psym=[2,1],position=[.7,.4],/normal

device,/close
set_plot,'X'

stop

end


