;xy_offsets
;
;PURPOSE:
;	This script will calculate the covariances of the ShackHartman images 
;	using the distorted grid obtained by averaging all of the positions.

pro newest_xyoffset_histogram,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/2005-05-'+Date+$
          '/ShackHartman/'+postscript_dir+'/'

;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
Cxt_full=fltarr(num_stats_files,num,depth)
Cxl_full=fltarr(num_stats_files,num,depth)
Cyt_full=fltarr(num_stats_files,num,depth)
Cyl_full=fltarr(num_stats_files,num,depth)


;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)
	
endfor
	
      
set_plot,'ps'
device,filename=Postscript_dir+'Offset_histograms.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.
!p.multi=[0,1,2]

for i=0,grid_spots-1 do begin

   if ((i mod 25) eq 0) then begin
        plot,[0,file_number],[-6,6],/nodata,$
        xtitle='File Number',ytitle='Offset (Pixels)',$
        xrange=[0,file_number],yrange=[-6,6]
        valid_spots=lonarr(25)
        valid_spot_index=0
   endif else begin
        test=where(x_offset_hor(*,i) eq -10)
        if test(0) eq -1 then begin 
           meanx=mean(x_offset_hor(*,i))
           meany=mean(y_offset_hor(*,i))
           variancex=variance(x_offset_hor(*,i))
           variancey=variance(y_offset_hor(*,i))
           oplot,x_offset_hor(*,i),color=10*(i mod 25)
           valid_spots(valid_spot_index)=i
           valid_spot_index=valid_spot_index+1
         endif
    endelse
    if (((i mod 25) eq 24) or (i eq 624)) then begin
        good_spots=where(valid_spots ne 0)
        ;xoffset_max=floor(max(x_offset_hor((i-24):i,*)))
        ;xoffset_min=floor(min(x_offset_hor((i-24):i,*)))
        if (good_spots(0) ne -1) then begin
	good_spots=valid_spots(good_spots)

        real_good_spots=x_offset_hor(good_spots,*)
	really_good_spots=real_good_spots(where(real_good_spots ne -10))	
	xoffset_max=floor(max(x_offset_hor(good_spots,*)))
        xoffset_min=floor(min(x_offset_hor(good_spots,*)))

	xoffset_hist=histogram(x_offset_hor(good_spots,*),nbins=60,$
        max=xoffset_max,min=xoffset_min,locations=xoff_locations)
        
        plot,xoff_locations,xoffset_hist,psym=10,xtitle='offsets',$
	    xrange=[-5,5],yrange=[0,1500],ytitle='Number',$
	    color=black,background=white
	endif else begin
	plot,[-6,6],[0,5000],/nodata
	endelse
    endif

endfor      


device,/close
set_plot,'x'
stop
end


