;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 newest_xy_try__offsets_mpeg,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='11'
If (not keyword_set(stats_directory)) then stats_directory='stats/'
If (not keyword_set(letter)) then letter=['a','b','c','d','e','f','g','h','i']
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'


;DIRECTORIES
Shack_Directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-'+Date+$
	  '/ShackHartman/'
Stats_Directory=Shack_Directory+stats_directory
Cor_Directory=Shack_Directory+'distorted_correlations/'
Raw_Directory=Shack_Directory+'raw/'

;FILES
stats_files=file_search(stats_directory+'*stats.txt',count=num_files)
offset_files=file_search(Stats_directory+'*5132*newest_offsets.txt',count=num_o_files)
raw_files=file_search(Raw_directory+'*5132*.fits',count=num_r_files)
xcor_files=file_search(Cor_directory+'*newest_x_correlations.txt',count=num_x_files)
ycor_files=file_search(Cor_directory+'*newest_y_correlations.txt',count=num_y_files)

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

;Time will be coded as 2005*1000+Day_Number+hr/24+min/60+second/3600
year_offset=double(2005000)
time_gap=0.0001
num_of_telescope_shifts=0

;MOVIE                                  
mpegname=Shack_Directory+'mpegs/'+date+'_newest_xy_offsets_stats.mpeg'

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

;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=[1000,768]

for file_number=0,num_of_files-1 do begin

        readcol,xcor_files(file_number),lon_x,tr_x,format='f,f'
        readcol,ycor_files(file_number),lon_y,tr_y,format='f,f'

	stats_name=stats_files(file_number)
	offset_name=offset_files(file_number)
	
	;Create a number for the file to be put on the screen
        stats_path_parts=strsplit(stats_files(file_number),'/',/extract)
        stats_file_ref=strsplit(stats_path_parts(N_elements(stats_path_parts)-$
	1),'stats.txt',/extract,/regex)
        pre_letter=strsplit(stats_file_ref,'_',/extract)
        pre_number=pre_letter(1)
        pre_letter=pre_letter(0)
	
	;Get the time of observation from the fits header
	unshifted_filename=Raw_files(file_number)
	fits_read,unshifted_filename,unshifted_image,header
        uttime=SXPAR(header,'DATE-OBS')

	readcol,offset_name,grid_index,x_coord,x_off,y_coord,y_off,$
	format='i,f,f,f,f'

	grid_spots=N_elements(x_coord)

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

	;Obtain the difference between centroid and grid point
	x_offset=x_off
	y_offset=y_off

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

	 ;Translate Center coordinates to strings
         a=valid_num(x_fit_center(file_number),x_coord_str)
         b=valid_num(y_fit_center(file_number),y_coord_str)

	erase
	
	!p.noerase=1
	!p.charsize=1
	partvelvec,x_offset(fitted),y_offset(fitted),x_coord(fitted),$
                y_coord(fitted),yrange=[100,820],xrange=[180,880],xstyle=17,$
                ystyle=17,position=[.05,.05,.7,.9]
                                                                                
        xyouts,80,700,pre_letter+' '+pre_number,/device
	xyouts,320,700,uttime,/device        
	xyouts,550,700,'X center=',/device
        xyouts,570,700,+x_coord_str,/device
        xyouts,550,720,'Y center=',/device
 
       xyouts,570,720,+y_coord_str,/device
                 
                                                       
        plot,[x_fit_center(file_number)],[y_fit_center(file_number)],$
                xstyle=17,ystyle=17,psym=1,position=[.05,.05,.7,.9],$
                yrange=[100,820],xrange=[180,880]

	!p.charsize=.7
	plot,lon_x,xstyle=17,ystyle=17,psym=1,position=[.75,.05,.95,.24],$
                yrange=[-3,5],title='longitudinal x'
        plot,tr_x,xstyle=17,ystyle=17,psym=1,position=[.75,.29,.95,.48],$
                yrange=[-3,5],title='transverse x'
        plot,lon_y,xstyle=17,ystyle=17,psym=1,position=[.75,.53,.95,.72],$
                yrange=[-3,5],title='longitudinal y'
        plot,tr_y,xstyle=17,ystyle=17,psym=1,position=[.75,.77,.95,.96],$
                yrange=[-3,5],title='transverse y'

	
	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

