;find_center
;
;PURPOSE:
;	find_center will read in the listed coordinates from the files 
;	of type [filename]_mag.txt and try to find the center of the 
; 	coordinate grid     

pro find_all_center_all_letter, Directory=Directory, y_center=y_center, x_center=x_center, $
		 letter=letter,file_number=file_number

If (not keyword_set(Directory)) then Directory='/a/pippin01/Volumes/u08/lsst/CPanalysis/2005-05-10/ShackHartman/'
If (not keyword_set(letter)) then letter='a'
If (not keyword_set(file_number)) then file_number='20'
If (not keyword_set(x_center)) then x_center=[542.5]
If (not keyword_set(y_center)) then y_center=[466]


;Loop through 100 images and obtain the fitted center for each
num=25
num_of_files=100
letters=N_elements(letter)
X_centers=fltarr(letters*(num_of_files-1))
Y_centers=fltarr(letters*(num_of_files-1))

for letter_ind=0,letters-1 do begin

	for file_number=1,num_of_files-1 do begin

	if (file_number lt 10) then filename=Directory+'hr5132'+letter(letter_ind)+'_'+$
        string(file_number,format='(I1)')+'_stats.txt'
        if file_number ge 10 and file_number lt 100 then filename=$
        Directory+'hr5132'+letter(letter_ind)+'_'+string(file_number,format='(I2)')+'_stats.txt'
        if file_number ge 100 and file_number lt 1000 then filename=$
        Directory+'hr5132'+letter(letter_ind)+'_'+string(file_number,format='(I3)')+'_stats.txt'


	;The x coordinates are symmetric about a point that is not a ShackHartman image
	;The y coordinates are symmetric about a ShackHartman image
	subx=2*indgen(num)-25
	suby=indgen(num)-12

	index=findgen(num*num)

	fullx=fltarr(num*num)
	fully=fltarr(num*num)

	;y is constant for 25 entries; x increases for 25 entries and repeats
	;i.e., scan is along x followed by y
	fullx(index)=subx(index mod num)
	fully=rebin(suby,num*num)

	readcol,filename,x_coord,y_coord,format='X,f,f'

	point_indices=where(x_coord ne -1)

	x_coord=x_coord(point_indices)
	y_coord=y_coord(point_indices)
	fullx=fullx(point_indices)
	fully=fully(point_indices)

	x_fit=curvefit(fullx,x_coord,weights,x_center,/noderivative,function_name='linear_function_x',yerror=xerror)
	y_fit=curvefit(fully,y_coord,weights,y_center,/noderivative,function_name='linear_function_y',yerror=yerror)

	X_centers(letter_ind*(num_of_files-1)+file_number-1)=x_center
	Y_centers(letter_ind*(num_of_files-1)+file_number-1)=y_center
	
	print,FORMAT='(%"%d\t\t%f\t\t%f")',file_number,x_center,y_center

	;Errors at each point
	y_err=y_coord-y_fit
	x_err=x_coord-x_fit

	endfor

endfor

set_plot,'ps'
                                                                                
device,filename=Directory+'hr5132'+letter(0)+'_'+letter(letters-1)+'center.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.NOERASE=0 
;!P.POSITION=[0.10,0.10,0.9,0.9]
                                     
!P.multi=[0,1,1]                                         
plot,findgen(letters*(num_of_files-1))+1,y_centers,psym=1,title="Y Center Coordinate for files hr5132 "+letter(0)+'-'+letter(letters-1),$
        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]

;!P.multi=[0,1,1]
plot,findgen(letters*(num_of_files-1))+1,x_centers,psym=1,title="X Center Coordinate for files hr5132 "+letter(0)+'-'+letter(letters-1),$
        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]
                                                                     
device,/close

set_plot,'X'
stop

end

