;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 center_histogram, 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(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/'


stats_directory='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+$
          '/ShackHartman/stats_1_31/'
Postscript_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/2005-05-'+Date+$
	'/distorted_postscripts/'

image_dir='/nfs/slac/g/ki/ki08/lsst/CPanalysis/1_31_figs/'

;FILES AND HOLDING
readcol,stats_directory+'newest_x_centers.txt',x_center,x_fit_center,$
        format='X,f,f'
readcol,stats_directory+'newest_y_centers.txt',y_center,y_fit_center,$
        format='X,f,f'
x_off_cen=x_center-x_fit_center
y_off_cen=y_center-y_fit_center


;Offset Numbers
avg_xoff=mean(x_off_cen)
avg_yoff=mean(y_off_cen)
var_xoff=variance(x_off_cen)
var_yoff=variance(y_off_cen)

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

erase
!p.multi=[0,1,2]
xdiff_max=ceil(max(x_off_cen))
xdiff_min=floor(min(x_off_cen))
no_bins=(xdiff_max-xdiff_min)*5+1
xdiff_histogram=histogram(x_off_cen,nbins=no_bins,$
	max=xdiff_max,min=xdiff_min,locations=xdiff_locations)

!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,title='Offsets from Fitted Center for 5'+Date+'05'
!p.charsize=2
!p.charthick=3.3
xyouts,.7,.85,'mean='+strtrim(avg_xoff,2),color=black,/normal
xyouts,.7,.8,'variance='+strtrim(var_xoff,2),color=black,/normal


ydiff_max=ceil(max(y_off_cen))
ydiff_min=floor(min(y_off_cen))
no_bins=(ydiff_max-ydiff_min)*5+1
ydiff_histogram=histogram(y_off_cen,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
xyouts,.7,.4,'mean='+strtrim(avg_yoff,2),color=black,/normal
xyouts,.7,.35,'variance='+strtrim(var_yoff,2),color=black,/normal


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


stop

end

