;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 mpeg_xy_offsets,Date=Date,stats_directory=stats_directory,$
	x_center_file=x_center_file,y_center_file=y_center_file


If (not keyword_set(Date)) then Date='10'
If (not keyword_set(stats_directory)) then stats_directory='stats_temp'
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(letter)) then letter=['a','b','c','d','e','f','g','h','i']


Directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
	  '/ShackHartman/'+stats_directory+'/'

readcol,Directory+x_center_file,x_fit_center,format='X,X,f'
readcol,Directory+y_center_file,y_fit_center,format='X,X,f'

;MOVIE                                  
mpegname='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
	 '/ShackHartman/mpegs/all_vector_field.mpg'

movie=mpeg_open([768,768])
frame_no=0
frame_gap=8

;CONSTANTS
num_of_files=N_elements(x_fit_center)
num=25

;ARRAY REFERENCE 25x25=625 FOR GRID
subx=2*indgen(num)-25           ;The x-reference numbers
suby=indgen(num)-12             ;The y-reference numbers

;Correlations
C=fltarr(25)

;SET PLOT TO MEMORY BUFFER IN ORDER TO BE ABLE TO DO OTHER THINGS
set_plot,'Z'

device,set_resolution=[768,768]

for file_index=0,num_of_files-1 do begin

	;Take the centroid values from the stats.txt files
	file_name=Directory+'hr5132'+letter(((file_index +1)/100))+'_'+$
		  strtrim(string((file_index+1) mod 100),2)+'_stats.txt'
	stat_name=Directory+'hr5132'+letter(((file_index +1)/100))+'_'+$
                  strtrim(string((file_index+1) mod 100),2)+'_offsets.txt'

	readcol,file_name,grid_index,x_coord,y_coord,format='f,f,f'
	grid_spots=N_elements(x_coord)

	;Figure out the new reference grid, applied around x_center and y_center
        x_ref_col=12.5*subx+x_fit_center(file_index)
        y_ref_col=25.0*suby+y_fit_center(file_index)
	index=findgen(num*num)
	gridx=fltarr(num*num)
	gridy=fltarr(num*num)
	gridx(index)=x_ref_col(index mod num)
	gridy(index)=y_ref_col(index/num)

	not_fitted=where(x_coord eq -1,complement=fitted)

	;Obtain the difference between centroid and grid point
	x_offset=x_coord-gridx
	y_offset=y_coord-gridy

	;If point was not fitted, set to -1
 	x_offset(not_fitted)=-1
	y_offset(not_fitted)=-1	

	;All 625 points and their corresponding displacements are available 
	;here
	
	;Start by looking at the correlation at a spacing of 1 and then move up
	;to 25

	erase

	!p.noerase=1

	partvelvec,x_offset(fitted),y_offset(fitted),gridx(fitted),$
                gridy(fitted),yrange=[100,820],xrange=[180,880],xstyle=17,$
                ystyle=17,position=[.1,.1,.9,.9]
                                                                                
        xyouts,120,100,pre_letter+' '+pre_number,/device
                                                                                
        plot,[x_fit_center(file_number)],[y_fit_center(file_number)],$
                xstyle=17,ystyle=17,psym=1,position=[.1,.1,.9,.9],$
                yrange=[100,820],xrange=[180,880]
                                                                                
        vec_field=tvrd()


	for i=0,frame_gap do begin
		mpeg_put,movie,IMAGE=vec_field,frame=(frame_no*frame_gap+i),$
		/order
	endfor
	
	frame_no=frame_no+1	
		 
	
endfor

mpeg_save,movie,FILENAME=mpegname
mpeg_close,movie

end

