C
C Assumes that you've dumped the id, xc, yc, mag, and merr, and PERROR 
C from *mag.1 with selection criteria = "yes"
C into temp:
C
	character*80 inline,inline2
	open(unit=1,file="temp",status="old")
C       open(unit=2,file="pospst",status="new")
	iloc=0
C Get another potential PSF star
1	read(1,2,end=99) inline
Cwrite(6,2) inline
2	format(a80)
	iloc=iloc+1
C First, does this star have a valid magitude?
	read(inline,*,err=1) id,xc,yc,amag,amerr
Cwrite(6,*) id,xc,yc,amag,amerr
C yes.  Is the star bright enough?
	if(amerr.gt..05) go to 1
C OK, now see if this star has something nearby
C star at the top of the list
	rewind(1)
3	read(1,2,end=88) inline2
Cwrite(6,33) inline2(1:30)
33	format('Checking the nose':,a30)
	if(inline.eq.inline2) go to 3
C see if the x and y are close
	read(inline2,*) id2,xc2,yc2
	dist2=(xc-xc2)**2+(yc-yc2)**2
Cwrite(6,34) xc,xc2,yc,yc2,dist2
34	format('Distance=',5f12.0)
	if(dist2.gt.(11+11)**2) go to 3
Cwrite(6,35)
35	format('It is closeby:',f8.2)
C OK, it's nearby.  Now what?
	read(inline2,*,err=90) id2,xc2,yc2,amag2,amerr2
C If we are here, then amag2 is not indef.
C How much fainter is amag2?
	dif=amag-amag2
C      -10=  15 -  25
C       -5=  15 -  20
C        0=  15 -  15
C        5=  15 -  10
C      
Cwrite(6,80) id2,amag2,amerr2,dif
80	format('This star is closeby:',i5,3f10.2)
	if(dif.lt.-5) go to 3
C don't worry about it in other words
C If we are here then the star is too bright, and we need to try another one.
Cwrite(6,81)
81	format('Too bright!')
	go to 90
88      write(2,89) id,xc,yc,amag,amerr
Cwrite(6,87) id,xc,yc,amag,amerr
87	format('We have a winner:' i5,4f10.3)
89	format(i5,4f10.3)
90	rewind(1)
	iloc2=0
91	read(1,2) inline2
	iloc2=iloc2+1
Cwrite(6,92) iloc2,inline2(1:30)
92	format('Relocating:',i4,a30)
	if(iloc.eq.iloc2) go to 1
	go to 91
99	stop
	end
