	character*79 f1,f2,f3
	real*8 a8(8),adcoef(2,3)
	character*80 head(72)
	real*8 xa,xd,xi,xn,pi,cx,cy
	real x(20),y(20)
	k=iargc()
	if(k.lt.2)then
	  write(*,*)'           usage: j_coo512 fits_file obj_file'
	  write(*,*)
          write(*,*)'                                      2006,5'
	  write(*,*)
	  write(*,*)'           selecting objects, re_order *.coo'
	  write(*,*)
	  write(*,*)'       EX: j_coo512 p3523737UBP5r060 UBP5star.cat'
	  write(*,*)
	  stop
	endif
        pi=4.*datan(1d0)
        cx=pi/12.
        cy=pi/180.

	call getarg(1,f2)
	k=index(f2,'.')-1
	if(k.eq.-1)k=lnblnk(f2)

	f1=f2(1:k)//'.fit'
	open(2,file=f1,status='old',access='direct',recl=5760)
	read(2,rec=1)head
	close(2)
	ipos=indexpos(head,'A81     ')
	if(ipos.eq.73)stop 'do position calibration first!'
        do 10 i=1,8
10      read(head(ipos+i-1)(11:),*)a8(i)
	call xy2ad(a8,adcoef)
	epoch=2000.0

	call getarg(2,f1)
	open(1,file=f1,status='old',iostat=i)
	if(i.ne.0)stop ' OBJECTS file not found!'
	read(1,'(a)')f1
	read(1,'(a)')f1
	i=0
20	read(1,'(a)',end=100)f1
	if(itohd(f1(5:),a,d,dummy).eq.0)then
          write(*,"(' ??? ',a)")f1
	  stop
	endif
	i=i+1
        xa=a*cx
        xd=d*cy
        call d_standc(a8(7),a8(8),xa,xd,xi,xn)
        x(i)=513.-(adcoef(1,1)*xi+adcoef(1,2)*xn+adcoef(1,3))
        y(i)=513.-(adcoef(2,1)*xi+adcoef(2,2)*xn+adcoef(2,3))
c	write(*,'(2a,2f9.2)')f1(1:30),'---> ',x(i),y(i)
	i=i+1
	x(i)=x(i-1)-0.23
	y(i)=y(i-1)-17.0
	goto 20
100	n=i
        close(1)

	f1=f2(1:k)//'.coo'
	open(1,file=f1,status='old',iostat=i)
	if(i.ne.0)then
	  write(*,*)f1,'  not found!'
	  stop
	endif
	open(2,file='j_coo512.tmp',status='unknown')
	do 50 i=1,n
        read(1,'(a)')f1
	if(i.eq.1)write(2,*)f1
        read(1,'(a)')f1
	if(i.eq.1)write(2,*)f1
        read(1,'(a)')f1
	if(i.eq.1)write(2,*)f1
	zz=50.
40      read(1,'(a)',end=30)f1
        read(f1(1:),'(i7,2f9.3)')j,x1,y1
        xx=x(i)-x1
	yy=y(i)-y1
	xx=xx*xx
	yy=yy*yy
	if(xx+yy.lt.zz)then
	  zz=xx+yy
	  f3=f1
	  iz=i
	endif
	goto 40
30      if(zz.ne.50.)then
	  j=(iz+1)/2
	  if(mod(iz,2).eq.0)j=j+10
	  write(f3(1:6),'(i6)')j
	  write(2,*)f3(2:)
	endif
50	rewind 1

	f1=f2(1:k)//'.coo'
	call system('mv j_coo512.tmp '//f1)
	
	write(*,*)n,' stars *.coo file renewed!'
	end

        function indexpos(head,f1)
        character*80 head(72),f1*8
        do 10 indexpos=1,72
10      if(head(indexpos)(1:8).eq.f1)return
        end

        subroutine xy2ad(x,a)
c 2 set 6 plate coef convert
        real*8 x(2,3),a(2,3),z
        z=x(1,1)*x(2,2)-x(1,2)*x(2,1)
        a(1,1)=x(2,2)/z
        a(1,2)=-x(1,2)/z
        a(1,3)=(x(1,2)*x(2,3)-x(1,3)*x(2,2))/z
        a(2,1)=-x(2,1)/z
        a(2,2)=x(1,1)/z
        a(2,3)=(x(1,3)*x(2,1)-x(1,1)*x(2,3))/z
        end

        subroutine d_standc(rc,dc,ra,de,xi,xn)
c alpha delta --------> X Y
c rc dc : center of plate
c rc dc ra de in arc
	real*8 rc,dc,ra,de,xi,xn,tmp
        sd=dsin(dc)
        cd=dcos(dc)
        td=dtan(de)
        co=dcos(ra-rc)
        tmp=sd*td+cd*co
        xi=dsin(ra-rc)/tmp
        xn=(cd*td-sd*co)/tmp
        end
