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

readcol,stats_directory+'cor_angles.txt',sorted_mags,sorted_angles,$
	sorted_cl_means,sorted_ct_means,format='f,f,f,f'


uniq_indices=uniq(sorted_angles)


set_plot,'Z'

device, set_font='Courier'
device,set_resolution=[800,600]
!p.charsize=1
!p.charthick=1.5
!x.thick=2
!y.thick=2
!p.thick=3
!p.color=0

white='FFFFFF'x
black='000000'x
red='FF0000'x

loadct,39

items=['5/10/05','5/11/05','5/12/05']
syms=[2,2,2]
color_arr=[0,100,200]

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

;Set up plot 
plot,[0,34],[-1.5,1.5],/xstyle,/ystyle,/nodata,xr=[0,26],yr=[-0.5,2],background=white,$
	position=[0.10,0.10,0.8,0.9],title='Average Longitudinal Correlations for 5/'+$
	Date+'/2005',xtitle='Separation in n',ytitle='Covariance'

color_index=0

for ind=0, N_elements(uniq_indices)-2 do begin
  
  if ind eq 0 then begin

     temp_mags=sorted_mags(0:uniq_indices(ind))
     temp_angles=sorted_angles(0:uniq_indices(ind))
     temp_cl_means=sorted_cl_means(0:uniq_indices(ind))
     temp_sorted_indices=sort(temp_mags)
     oplot,temp_mags,temp_cl_means,color=10*color_index,psym=1
     sym_arr=[sym_arr,1]
     color_arr=[color_arr,(10*color_index)]
     angle_arr=[angle_arr,string(strtrim(180*temp_angles(0)/!pi,2))]
     color_index=color_index+1

  endif

  if ((uniq_indices(ind+1)-uniq_indices(ind)) gt 4) then begin
     temp_mags=sorted_mags(uniq_indices(ind)+1:uniq_indices(ind+1))
     temp_angles=sorted_angles(uniq_indices(ind)+1:uniq_indices(ind+1))
     temp_cl_means=sorted_cl_means(uniq_indices(ind)+1:uniq_indices(ind+1))
     temp_sorted_indices=sort(temp_mags)
     print,temp_mags(temp_sorted_indices)
     print,temp_angles(temp_sorted_indices)
     print,temp_cl_means(temp_sorted_indices)
     oplot,temp_mags,temp_cl_means,color=10*color_index,psym=1
     sym_arr=[sym_arr,1]
     color_arr=[color_arr,(10*color_index)]
     angle_arr=[angle_arr,string(strtrim(180*temp_angles(0)/!pi,2))]    
     color_index=color_index+1

   endif
endfor

legend,angle_arr,psym=sym_arr,colors=color_arr,position=[.81,.9],/normal
xyouts,.82,.91,'Grid Rotation Angle',/normal
jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,jpeg_dir+'Long_Angular_Correltations_05_'+Date+'.png',$
        jpgimg,reds,greens,blues

erase


!p.multi=[0,1,1]
plot,[0,30],[-1.5,1.5],/xstyle,/ystyle,/nodata,xr=[0,26],yr=[-.5,2],background=white,$
        position=[0.10,0.10,0.8,0.9],title='Average Transverse Correlations for 5/'+$
        Date+'/2005',xtitle='Separation in n',ytitle='Covariance'

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

color_index=0

for ind=0, N_elements(uniq_indices)-2 do begin
 
  if ind eq 0 then begin

     temp_mags=sorted_mags(0:uniq_indices(ind))
     temp_angles=sorted_angles(0:uniq_indices(ind))
     temp_ct_means=sorted_ct_means(0:uniq_indices(ind))
     temp_sorted_indices=sort(temp_mags)
     oplot,temp_mags,temp_ct_means,color=10*color_index,psym=1
     sym_arr=[sym_arr,1]
     color_arr=[color_arr,(10*color_index)]
     angle_arr=[angle_arr,string(strtrim(180*temp_angles(0)/!pi,2))]
     color_index=color_index+1
                   
  endif
  if ((uniq_indices(ind+1)-uniq_indices(ind)) gt 4) then begin
     temp_mags=sorted_mags(uniq_indices(ind)+1:uniq_indices(ind+1))
     temp_angles=sorted_angles(uniq_indices(ind)+1:uniq_indices(ind+1))
     temp_ct_means=sorted_ct_means(uniq_indices(ind)+1:uniq_indices(ind+1))
     temp_sorted_indices=sort(temp_mags)
     ;print,temp_mags(temp_sorted_indices)
     ;print,temp_angles(temp_sorted_indices)
     ;print,temp_ct_means(temp_sorted_indices)
     oplot,temp_mags,temp_ct_means,color=10*color_index,psym=1
     sym_arr=[sym_arr,1]
     color_arr=[color_arr,(10*color_index)]
     angle_arr=[angle_arr,string(strtrim(180*temp_angles(0)/!pi,2))]
     color_index=color_index+1
                                                                                              
   endif
endfor

legend,angle_arr,psym=sym_arr,colors=color_arr,position=[.81,.9],/normal
xyouts,.82,.91,'Grid Rotation Angle',/normal
jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,jpeg_dir+'Transverse_Angular_correlations_05_'+Date+'.png',$
        jpgimg,reds,greens,blues

erase

device,/close
set_plot,'X'

stop

end


