;new_center_drift
;
;PURPOSE:
;	This file will take the fitted grid centers from the file centers.txt 
;	(returned by coo_mag_to_stats.pro) and perform a linear fit of the 
;	center vs. image number (i.e. time).
;
;CALLING SEQUENCE:
;       new_center_drift,[Date=Date,[Directory=Directory,[stats_directory=$
;	stats_directory,[xcenter=xcenter,[ycenter=ycenter,$
;	center_file=center_file]]]]]]
;
;INPUTS
;       DATE: the string '10','11',or '12' corresponding to
;             2005-05-10,2005-05-11, and 2005-05-12
;	STATS_DIRECTORY: the directory in which the statistics are located
;	CENTER_FILE: The name of the file containing the fitted centers and
;		     their corresponding erros
;	X_CENTERS:   The name of the file to which the linear fit for the 
;		     x coordinate will be written
;	Y_CENTERS:   The The name of the file to which the linear fit for the
;                    y coordinate will be written
;
;OUTPUTS:
;
;	FILES:
;	x_centers.txt-this file contains the following fields
;		File-The number of the file in the directory.
;		X center-The individually fitted center for each image
;		X fit Center-The center returned by the linear fit
;		m-The slope of the linear fit
;		b-The y-intercept of the linear fit
;		diff-The difference between the individually fitted center
;		     and the linear fitted center across images
;
;	y_centers.txt-this file contains the following fields
;               File-The number of the file in the directory.
;               Y center-The individually fitted center for each image
;               Y fit Center-The center returned by the linear fit
;               m-The slope of the linear fit
;               b-The y-intercept of the linear fit
;               diff-The difference between the individually fitted center
;                    and the linear fitted center across images
;
;	[shifted_dir]/h5132

pro newest_center_drift_png, Date=Date,shift_directory=shift_directory,$
		stats_directory=stats_directory, center_file=center_file,$
		x_centers=x_centers,y_centers=y_centers,noab=noab

If (not keyword_set(Date)) then Date='10'
If (not keyword_set(stats_directory)) then stats_directory='stats_1_31/'
If (not keyword_set(shift_directory)) then shift_directory='shifted/'
Shack_directory='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+'/ShackHartman/'
If (not keyword_set(center_file)) then center_file=shack_directory+$
	stats_directory+'centers.txt'
If (not keyword_set(x_centers_file)) then x_centers_file=shack_Directory+$
	stats_directory+'newest_x_centers.txt'
If (not keyword_set(y_centers_file)) then y_centers_file=shack_Directory+$
	stats_directory+'newest_y_centers.txt'
If (not keyword_set(noab)) then noab=0
postscript_directory='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/distorted_postscripts/'


If noab then begin
stats_directory='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/stats/'
postscript_directory='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/distorted_postscripts_no_ab/'
endif else begin
Postscript_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+$
	'/distorted_postscripts/'
endelse

;FOR GENERAL PURPOSE
;image_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/final_report_figs/'
;FOR LATEX PURPOSE
image_dir='/afs/slac/u/ki/lances/latex/'
;FILES AND HOLDING

readcol,center_file,x_centers,y_centers,format='f,f'
num_files=N_elements(x_centers)
raw_files=FILE_SEARCH(shack_Directory+'raw/h*.fits',count=num_raw_files)
get_lun,xfile
get_lun,yfile

;SUBARRAYS
x_slopes=fltarr(num_files)
y_slopes=fltarr(num_files)
start_index=0
x_fit_guess=fltarr(2)
y_fit_guess=fltarr(2)
file_index_arr=findgen(num_files)

;CUMULATIVE ARRAYS
x_fit_centers=fltarr(num_files)
y_fit_centers=fltarr(num_files)

openw,xfile,x_centers_file
openw,yfile,y_centers_file

printf,xfile,FORMAT='(%"%s\t%s\t%s\t%s\t\t%s\t\t%s")',$
      'File ','X center','X Fit Center','m','b','diff'
printf,yfile,FORMAT='(%"%s\t%s\t%s\t%s\t\t%s\t\t%s")',$
      'File ','Y center','Y Fit Center','m','b','diff'

set_plot,'ps'

device,filename=postscript_directory+'newest_centers.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.2
!P.THICK=4.
!P.multi=[0,1,2]

;Difference between fitted center and linear fitted center
x_diff_arr=fltarr(num_files-1)
y_diff_arr=fltarr(num_files-1)

for file_index=0,num_files-2 do begin
	
	;Build array of x and y coordinates and corresponding slopes to feed 
	;to the linear fitting functions
	
	x_slopes(file_index)=x_centers(file_index+1)-x_centers(file_index)
	y_slopes(file_index)=y_centers(file_index+1)-y_centers(file_index)

	raw_path_parts=strsplit(raw_files(file_index),'/',/extract)
        raw_file_ref=strsplit(raw_path_parts(N_elements(raw_path_parts)-1),$
	'raw.fits',/extract,/regex)
	pre_letter=strsplit(raw_file_ref,'_',/extract)
	pre_letter0=pre_letter(0)
	pre_letter1=pre_letter(1)
	print,raw_file_ref
	print,pre_letter1
	is_pre_letter1=strnumber(pre_letter1,sub_num)	

	;These are the places where the tracking seems to jump somehow...
	If (((y_slopes(file_index) gt 3.5) or (x_slopes(file_index) gt 3.5)) $
	   and (file_index-start_index gt 2))$
	   or (file_index eq num_files-2) or (sub_num eq 100) $
	   or ((file_index eq 70) and (date eq 10)) $
	   or ((file_index eq 171) and (date eq 10))$
	   or ((file_index eq 430) and (date eq 10))$
	   or ((file_index eq 536) and (date eq 10))$
	   or ((file_index eq 615) and (date eq 10))$
	   or ((file_index eq 642) and (date eq 10))$
	   or ((file_index eq 669) and (date eq 10))$
	   or ((file_index eq 715) and (date eq 10))$
	   or ((file_index eq 747) and (date eq 10))$
	   or ((file_index eq 782) and (date eq 10))$
	   or ((file_index eq 852) and (date eq 10))$
	   or ((file_index eq 347) and (date eq 11))$
	   ;a and b files are removed for night of the 11
	   or ((file_index eq 661-200) and (date eq 11))$
	   or ((file_index eq 708-200) and (date eq 11))$
	   or ((file_index eq 738-200) and (date eq 11))$
           or ((file_index eq 767-200) and (date eq 11))$
	   or ((file_index eq 824-200) and (date eq 11))$
	   or ((file_index eq 858-200) and (date eq 11))$
	   or ((file_index eq 892-200) and (date eq 11))$
	   or ((file_index eq 118) and (date eq 12))$
	   or ((file_index eq 133) and (date eq 12))$
	   or ((file_index eq 158) and (date eq 12))$  
  	   or ((file_index eq 227) and (date eq 12))$
	   or ((file_index eq 256) and (date eq 12))$
	   or ((file_index eq 328) and (date eq 12))$
	   or ((file_index eq 434) and (date eq 12))$ 
	then begin

	   		
		begin_num=start_index mod 100
		end_num=file_index mod 100
		
		

                y_fit_guess(0)=float(y_centers(start_index))
                y_fit_guess(1)=float(mean(y_slopes[start_index:file_index]))
                x_fit_guess(0)=float(x_centers(start_index))
                x_fit_guess(1)=float(mean(x_slopes[start_index:file_index]))
 
		y_fit=curvefit(findgen(file_index-start_index+1),$
                y_centers[start_index:file_index],weights,y_fit_guess,$
                /noderivative,function_name='linear_function_2_y',$
                yerror=yerror)

		x_fit=curvefit(findgen(file_index-start_index+1),$
                x_centers[start_index:file_index],xerrs,x_fit_guess,$
                /noderivative,function_name='linear_function_2_x',$
                yerror=xerror)

					
		for sub_index=0,n_elements(y_fit)-1 do begin   
			y_t_center=y_centers(start_index+sub_index)
			y_f_center=y_fit(sub_index)
			y_diff=y_f_center-y_t_center                                                             
                	x_t_center=x_centers(start_index+sub_index)
                        x_f_center=x_fit(sub_index)
                        x_diff=x_f_center-x_t_center 
	
			printf,yfile,FORMAT='(%"%d\t%f\t%f\t%f\t%f\t%f\t%s")',$
			start_index+sub_index,y_t_center,$
			y_f_center,y_fit_guess(0),$
			y_fit_guess(1),y_diff,pre_letter0+$
			strtrim(string((start_index+sub_index) mod 100)+1,2)
			y_diff_arr(start_index+sub_index)=y_diff
			printf,xfile,FORMAT='(%"%i\t%f\t%f\t%f\t%f\t%f\t%s")',$
                        start_index+sub_index,x_t_center,x_f_center,$
			x_fit_guess(0),$
                 	x_fit_guess(1),x_diff,pre_letter0+$
			strtrim(string((start_index+sub_index) mod 100)+1,2)
			x_diff_arr(start_index+sub_index)=x_diff

		
		endfor

			plot,x_centers[start_index:file_index],$
                	psym=1,title="Fitted X coordinates for "+$
			pre_letter0+' '+$
			strtrim(string(start_index mod 100),2)+'-'+$
                	strtrim(string(file_index mod 100),2),$
			xtitle="File Sub Number",ytitle="X Coordinate",$
                	background=white,color=black,yrange=[525,535],$
			POSITION=[0.10,0.10,0.9,0.45]                                                                                     
                	oplot,x_fit

			plot,y_centers[start_index:file_index],$
                 	psym=1,title="Fitted Y coordinates for "+$
			pre_letter0+' '+$
			strtrim(string(start_index mod 100),2)+'-'+$
                 	strtrim(string(file_index mod 100),2),xtitle="File Sub Number",$
			ytitle="Y Coordinate",$
                 	background=white,color=black,yrange=[460,472],$
			POSITION=[0.10,0.55,0.9,0.95]                                                                                 
                	oplot,y_fit

			
			start_index=file_index+1

		 
        endif


	print,file_index                                                                                


endfor	

close,xfile
close,yfile


free_lun,xfile
free_lun,yfile

;Center Correlations
depth=5
xcenter_temp=fltarr(depth)
ycenter_temp=fltarr(depth)

for i=0,depth-1 do begin
   xcenter_temp(i)=mean(x_diff_arr(0:num_files-2-i)*x_diff_arr(i:num_files-2))
   ycenter_temp(i)=mean(y_diff_arr(0:num_files-2-i)*y_diff_arr(i:num_files-2))
endfor
    
;Offset Numbers
avg_xoff=mean(x_diff_arr)
avg_yoff=mean(y_diff_arr)
var_xoff=variance(x_diff_arr)
var_yoff=variance(y_diff_arr)

;PLOT THE CENTERS ON ONE PLOT
!p.multi=[0,1,1]
plot,findgen(num_files)+1,y_centers,psym=1,title="Y Center Coordinates for 2005-05-"+Date,$
        xtitle="File Number",ytitle="Y Coordinate",background=white,color=black, $
        yrange=[min(y_centers)-10,max(y_centers)+10],$
        POSITION=[0.10,0.10,0.9,0.9]


xyouts,.7,.85,'t     <X(0)*X(t)>',/normal
for i=0,depth-1 do begin
  xyouts,.7,.81-.04*i,'t='+strtrim(i,2)+'   '+strtrim(xcenter_temp(i),2),/normal
endfor

!p.multi=[0,1,1]
plot,findgen(num_files)+1,x_centers,psym=1,title="X Center Coordinate for 2005-05-"+Date,$
        xtitle="File Number",ytitle="X Coordinate",background=white,color=black, $
        yrange=[min(x_centers)-10,max(x_centers)+10],$
        POSITION=[0.10,0.10,0.9,0.9]

xyouts,.7,.85,'t     <Y(0)*Y(t)>',/normal
for i=0,depth-1 do begin
  xyouts,.7,.81-.04*i,'t='+strtrim(i+1,2)+'   '+strtrim(ycenter_temp(i),2),/normal
endfor


device,/close

set_plot,'z'
erase
device, set_font='Courier'
device,set_resolution=[800,600]
!p.charsize=.8
!p.charthick=1.2
!x.thick=2
!y.thick=2
!p.thick=.8
!p.noerase=0

device,set_resolution=[1400,1200]
!p.charsize=3
!p.charthick=5
!x.thick=4
!y.thick=4
!p.thick=2

white='FFFFFF'x
black='000000'x
red='FF0000'x

;PLOT THE CENTERS ON ONE PLOT
!p.multi=[0,1,2]
plot,findgen(num_files)+1,y_centers,psym=1,$;title="Y Center Coordinates for 2005-05-"+Date,$
        xtitle="File Number",ytitle="Y Coordinate (Pixels)",background=white,color=black, $
        yrange=[min(y_centers)-10,max(y_centers)+10]


;xyouts,.7,.85,'t     <X(0)*X(t)>',/normal
for i=0,depth-1 do begin
;  xyouts,.7,.81-.04*i,'t='+strtrim(i,2)+'   '+strtrim(xcenter_temp(i),2),/normal
endfor

plot,findgen(num_files)+1,x_centers,psym=1,$;title="X Center Coordinate for 2005-05-"+Date,$
        xtitle="File Number",ytitle="X Coordinate (Pixels)",background=white,color=black, $
        yrange=[min(x_centers)-10,max(x_centers)+10]

;xyouts,.7,.85,'t     <Y(0)*Y(t)>',/normal
for i=0,depth-1 do begin
  ;xyouts,.7,.81-.04*i,'t='+strtrim(i+1,2)+'   '+strtrim(ycenter_temp(i),2),/normal
endfor

jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,image_dir+'Centers_5-'+Date+'-2005.png',$
jpgimg,reds,greens,blues

erase

!p.multi=[0,2,2]
plot,findgen(num_files)+1,y_centers,psym=1,title="Y Center Coordinates for 2005-05-"+Date,$
        xtitle="File Number",ytitle="Y Coordinate",background=white,color=black, $
        yrange=[min(y_centers)-10,max(y_centers)+10]


;xyouts,.7,.85,'t     <X(0)*X(t)>',/normal
for i=0,depth-1 do begin
;  xyouts,.7,.81-.04*i,'t='+strtrim(i,2)+'   '+strtrim(xcenter_temp(i),2),/normal
endfor
plot,findgen(num_files)+1,x_diff_arr,yrange=[-3,3],xtitle=file_number,$
	ytitle='Difference of X center from Fit (pixels)',$
	background=white,color=black,psym=4

plot,findgen(num_files)+1,x_centers,psym=1,$;title="X Center Coordinate for 2005-05-"+Date,$
        xtitle="File Number",ytitle="X Coordinate (px)",background=white,color=black, $
        yrange=[min(x_centers)-10,max(x_centers)+10]

;xyouts,.7,.85,'t     <Y(0)*Y(t)>',/normal
for i=0,depth-1 do begin
  ;xyouts,.7,.81-.04*i,'t='+strtrim(i+1,2)+'   '+strtrim(ycenter_temp(i),2),/normal
endfor

plot,findgen(num_files)+1,y_diff_arr,yrange=[-3,3],xtitle=file_number,$
        ytitle='Difference of Y center from Fit (pixels)',$
	background=white,color=black,psym=1

erase

!p.multi=[0,1,2]
!p.thick=3

plot,findgen(num_files)+1,y_centers,psym=1,$
        xtitle="File Number",ytitle="Y Coordinate",background=white,color=black, $
        yrange=[min(y_centers)-10,max(y_centers)+5]


oplot,findgen(num_files)+1,y_diff_arr+min(y_centers)-7,psym=2,color=black

plot,findgen(num_files)+1,x_centers,psym=1,$
        xtitle="File Number",ytitle="X Coordinate",background=white,color=black, $
        yrange=[min(x_centers)-10,max(x_centers)+10]

oplot,findgen(num_files)+1,x_diff_arr+min(x_centers)-7,psym=4,color=black
jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,image_dir+'Centers+Offset_5-'+Date+'-2005.png',$
jpgimg,reds,greens,blues



erase
xdiff_max=ceil(max(x_diff_arr))
xdiff_min=floor(min(x_diff_arr))
no_bins=(xdiff_max-xdiff_min)*5+1
xdiff_histogram=histogram(x_diff_arr,nbins=no_bins,$
	max=xdiff_max,min=xdiff_min,locations=xdiff_locations)
;xdiff_location=xdiff_locations+(xdiff_max-xdiff_min+2)/no_bins

!p.thick=8
!p.charsize=3
!p.charthick=5
plot,xdiff_locations,xdiff_histogram,psym=10,background=white,color=black,$
     xtitle='X center offsets (Pixels)',ytitle='Number',$
     xrange=[-3,3],xstyle=1
!p.charsize=2
!p.charthick=3.3


ydiff_max=ceil(max(y_diff_arr))
ydiff_min=floor(min(y_diff_arr))
no_bins=(ydiff_max-ydiff_min)*5+1
ydiff_histogram=histogram(y_diff_arr,nbins=no_bins,$
        max=ydiff_max,min=ydiff_min,locations=ydiff_locations)
;xdiff_location=xdiff_locations+(xdiff_max-xdiff_min+2)/no_bins

                                                                  
!p.charsize=3
!p.charthick=5
plot,ydiff_locations,ydiff_histogram,psym=10,background=white,color=black,$
     xtitle='Y center offsets (Pixels)',ytitle='Number',$
     xrange=[-3,3],xstyle=1

!p.charsize=2
!p.charthick=3.3


jpgimg=tvrd()
tvlct,reds,greens,blues,/get
write_png,image_dir+'Centers_Histogram-'+Date+'-2005.png',$
jpgimg,reds,greens,blues


stop

end

