pro shackgrid_shifting, Directory=Directory,letter=letter

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','b','c','d','e','f','g','h','i']
letters=N_elements(letter)
num=25

;Here are the approximate Centers for image 50 of each of the letters a-f,
;along with the 25 grid
x_centers=[542,543,542,542,546,548,543,543,542]
y_centers=[465,466,468,468,465,464,466,466,466]
subx=2*indgen(num)-25
suby=indgen(num)-12


for letter_index=0,letters-1 do begin

;This will set up the reference coordinates for the comparison
x_ref_col=x_centers(letter_index)+12.5*subx
y_ref_col=y_centers(letter_index)+25*suby


	for file_number=1, 99 do begin
                                                                                
		if file_number lt 10 then begin
			mag_file_name=Directory+'mag/hr5132'+letter(letter_index)+'_'+$
			string(file_number,format='(I1)')+'_raw.fits.mag.1.txt'
			coo_file_name=Directory+'coo/hr5132'+letter(letter_index)+'_'+$
	                string(file_number,format='(I1)')+'_raw.fits.coo.1'
			appendage='stats_10_shift/hr5132'+letter(letter_index)+'_'+string(file_number,format='(I1)')+$
			'_stats.txt'
		endif
		if file_number ge 10 and file_number lt 100 then begin
			mag_file_name=Directory+'mag/hr5132'+letter(letter_index)+'_'+$
			string(file_number,format='(I2)')+'_raw.fits.mag.1.txt'
			coo_file_name=Directory+'coo/hr5132'+letter(letter_index)+'_'+$
       	                string(file_number,format='(I2)')+'_raw.fits.coo.1'
			appendage='stats_10_shift/hr5132'+letter(letter_index)+'_'+string(file_number,format='(I2)')+$
       	                '_stats.txt'
		endif
		if file_number ge 100 and file_number lt 1000 then begin
			mag_file_name=Directory+'mag/hr5132'+letter(letter_index)+'_'+$
			string(file_number,format='(I3)')+'_raw.fits.mag.1.txt'
			coo_file_name=Directory+'coo/hr5132'+letter(letter_index)+'_'+$
       	                string(file_number,format='(I3)')+'_raw.fits.coo.1'
			appendage='stats_10_shift/hr5132'+letter(letter_index)+'_'+string(file_number,format='(I3)')+$
         	        '_stats.txt'

			endif

		print,FORMAT='(%"%s\t\%d")',letter(letter_index),file_number
		;Read in the data columns from the output IRAF mag file.  They are
		; XCENTER, YCENTER, XERR, YERR, MAG
		readcol,mag_file_name,x_coord,y_coord,x_err,y_err,mag,format='f,f,f,f,f'

		;Read in the data columns from the output IRAF coo file.  They are 
		; SHARP, ROUNDNESS
		readcol,coo_file_name,sharp,roundness,format='X,X,X,f,f',SKIPLINE=41
	
		
		openw,1,Directory+appendage,/append
		printf,1,FORMAT='(%"%d\t%d")',n_elements(sharp),n_elements(x_coord)
		;This loop will go through all of the possible places where a point could be
		;and check to see if there is an image point near it
		for col_num=0,N_elements(y_ref_col)-1 do begin
			for row_num=0,N_elements(x_ref_col)-1 do begin

				;Pick points where x might be near and those where y might 
				;be near
				x_index_pos=where(abs(x_coord-x_ref_col(row_num)) lt 10)
				y_index_pos=where(abs(y_coord-y_ref_col(col_num)) lt 10)
				
				for y_sub_index=0,N_elements(y_index_pos)-1 do begin
					x_sub_index=where(x_index_pos eq $
							  y_index_pos(y_sub_index))
					if ((x_sub_index(0) ne -1) and (y_index_pos(0) ne -1) and (x_index_pos(0) ne -1)) then begin
						x_index=x_index_pos(x_sub_index)
						y_index=y_index_pos(y_sub_index)
						x_ref_dif=x_coord(x_index)-x_ref_col(row_num)
						y_ref_dif=y_coord(y_index)-y_ref_col(col_num)
						
						printf,1,FORMAT='(%"%3.0d\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f\t%0.3f")',col_num*N_elements(x_ref_col)+$
						row_num,x_coord(x_index),y_coord(y_index),$
						x_err(x_index),y_err(y_index),mag(x_index),$
						sharp(x_index),roundness(x_index),$
						x_ref_dif,y_ref_dif
						found_point=1
						break
					endif else begin
						found_point=0
					endelse
				
				endfor
				
				if found_point eq 0 then printf,1,$
				FORMAT='(%"%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d")',$
				col_num*N_elements(x_ref_col)+row_num,-1,-1,-1,-1,-1,-1,-1,-1,-1
	
			endfor
		endfor 
		close,1

	endfor

endfor

end
