pro ellip_offset,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='newest_x_centers.txt'
If (not keyword_set(y_center_file)) then y_center_file='newest_y_centers.txt'
If (not keyword_set(depth)) then depth=1
If (not keyword_set(postscript_dir)) then postscript_dir='distorted_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/fft_ellip/'


;ARRAYS OF FILES
offset_files=file_search(stats_directory+'*5132*newest_offsets.txt',count=num_stats_files)

;CONSTANTS
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(num_stats_files,grid_spots)
y_offset_hor=fltarr(num_stats_files,grid_spots)
x_offset_ver=fltarr(num_stats_files,grid_spots)
y_offset_ver=fltarr(num_stats_files,grid_spots)

;Correlations
e1=fltarr(num_stats_files)
e2=fltarr(num_stats_files)


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

        ;Take the results from the offset.txt files and fill the cube
        readcol,offset_files(file_number),x_offset,y_offset,format='X,X,f,X,f'

        ;Obtain the difference ordered in COLUMN, ROW between centroid and grid         point
        x_offset_hor(file_number,*)=x_offset
        y_offset_hor(file_number,*)=y_offset

        ;Obtain the difference ordered in ROW, COLUMN
        x_offset_ver(file_number,*)=x_offset((index mod 25)*25+index/25)
        y_offset_ver(file_number,*)=y_offset((index mod 25)*25+index/25)

	x_offset(where(x_offset eq -10))=0
	y_offset(where(y_offset eq -10))=0

	e1(file_number)=total(x_offset^2-y_offset^2)/total(x_offset^2+y_offset^2)
	e2(file_number)=total(x_offset*y_offset)/total(x_offset^2+y_offset^2)

endfor

e1mean=mean(e1)
e1var=variance(e1)
e2mean=mean(e2)
e2var=variance(e2)

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

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

plot,e1,xtitle='file number',ytitle='e1'
plot,e2,xtitle='file number',ytitle='e2'
xyouts,.7,.9,'Mean='+strtrim(e1mean,2),/normal
xyouts,.7,.87,'Variance='+strtrim(e1var,2),/normal
xyouts,.7,.45,'Mean='+strtrim(e2mean,2),/normal
xyouts,.7,.42,'Variance'+strtrim(e2var,2),/normal

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

plot,e1,e2,psym=2,xtitle='e1',ytitle='e2'

device,/close
set_plot,'x'

end

