;xy_offsets
;
;PURPOSE:
;	This script will return the average offset over the whole night for a given
;	point on the ShackHartman grid.  The covariance spectrum will then be obtained.
;

pro plot_xy_offsets_average,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(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


postscript_dir='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/'+postscript_dir+'/'
image_dir='/a/pippin01/Volumes/u08/lsst/CPanalysis/final_report_figs'

;ARRAYS OF FILES

Date_Arr=['10','11','12']
x_offsets_arr=fltarr(4,625)
y_offsets_arr=fltarr(4,625)

for i=0, n_elements(Date_arr)-1 do begin

stats_directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date_Arr(i)+$
          '/ShackHartman/stats/'
off_name=stats_directory+'average_offsets.txt'
readcol,off_name,xoffsets,yoffsets,format='X,X,f,X,f'

x_offsets_arr(i,*)=xoffsets
y_offsets_arr(i,*)=yoffsets

endfor

readcol,image_dir+'average_offsets.txt',xoffsets,yoffsets,format='X,f,f'
x_offsets_arr(3,*)=xoffsets
y_offsets_arr(3,*)=yoffsets


readcol,'/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date_Arr(0)+$
          '/ShackHartman/stats_temp/average_offsets.txt',x_coord_avg,y_coord_avg,format='X,f,X,f,X'

x_coord_avg_arr=fltarr(4,625)
y_coord_avg_arr=fltarr(4,625)

for i=0, 3 do begin
 x_coord_avg_arr(i,*)=x_coord_avg
 y_coord_avg_arr(i,*)=y_coord_avg
endfor

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

set_plot,'ps'
device,filename=Postscript_dir+'average_offsets.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=1.2

items_k_tran=['5/10/05','5/11/05','5/12/05','Calibrated']
sym_k_tran=[0,80,160,240]
line_k_tran=[0,0,0,0]


!p.multi=[0,1,1]
fitted=where(x_offsets_arr(0,*) ne 0)

!p.noerase=1
plot,[100,820],[180,880],/xstyle,/ystyle,xrange=[100,820],yrange=[180,880],$
	/nodata,color=black,background=white

for i=0,3 do begin
 partvelvec,x_offsets_arr(i,fitted),y_offsets_arr(i,fitted),$
       x_coord_avg_arr(i,fitted),y_coord_avg_arr(i,fitted),$
       yrange=[100,820],xrange=[180,880],$
       background=white,color=sym_k_tran(i),xstyle=5,ystyle=5

endfor

legend,items_k_tran,colors=sym_k_tran,psym=line_k_tran
device,/close
set_plot,'X'

for i=0,3 do begin
 partvelvec,x_offsets_arr(i,fitted),y_offsets_arr(i,fitted),$
       x_coord_avg_arr(i,fitted),y_coord_avg_arr(i,fitted),$
       yrange=[100,820],xrange=[180,880],$
       background=white,color=sym_k_tran(i),xstyle=5,ystyle=5

endfor

jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,image_dir+'Test_Cal_Image.png',$
jpgimg,reds,greens,blues

end


