pro shackgrid_8_03, 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'

;Here are 27 x reference points and 27 y reference points that define where the spot actually
;should be.  There are a total of 729 possible points
x_ref_col=[225,250,278,302,332,353,380,404,428,452,476,502,528,554,578,604,630,$
	654,680,704,730,754,780,806,832]
y_ref_col=[164,188,214,238,264,290,314,340,366,390,414,440,464,490,516,542,568,$
	592,616,642,668,692,718,742,768]


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

	endif

	print,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
	;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 5)
				y_index_pos=where(abs(y_coord-y_ref_col(col_num)) lt 5)
				
				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

end
