	character*30 f1
	real*8 a8(8),adcoef(2,3)
	character*80 head(72),a80
	real*8 xa,xd,xi,xn,pi,cx,cy
	logical logi
	k=iargc()
	if(k.eq.0)then
	  write(*,*)'               usage: j_ad2xy fits'
	  write(*,*)'                         2004,5'
	  write(*,*)
	  stop
	endif
        pi=4.*datan(1d0)
        cx=pi/12.
        cy=pi/180.

	call getarg(1,f1)
	k=index(f1,'.')-1
	if(k.le.0)k=lnblnk(f1)
	inquire(file=f1(1:k)//'.fit',exist=logi)
	if(.not.logi)stop ' file not found!'
	open(2,file=f1(1:k)//'.fit',status='old',
     *         access='direct',recl=5760)
	read(2,rec=1)head
	close(2)
        is6=1	
	ipos=indexpos(head,'NAXIS1  ')
        read(head(ipos)(26:30),'(i5)')j
        if(j.eq.6144)is6=3
	if(j.ge.4000.and.j.le.4096)is6=0
	ipos=indexpos(head,'A81     ')
	if(ipos.eq.73)stop 'do position calibration first!'
	epoch=2000.0
11      do 10 i=1,8
10      read(head(ipos+i-1)(11:),*)a8(i)

	call xy2ad(a8,adcoef)
	
	open(1,file='../deep/'//f1(1:k)//'.coo',status='old')
	read(1,'(a)')a80
	write(*,'(a)')a80
	read(1,'(a)')a80
	write(*,'(a)')a80
	write(*,*)
	close(1)

	open(1,file=f1(1:k-1)//'.coo',status='old')
20	read(1,'(a)',end=100)f1
	read(f1(25:),*)i
	if(itohd(f1(1:24),a,d,dummy).eq.0)then
          write(*,"(' ??? ',a)")f1
	  goto 20
	endif
        xa=a*cx
        xd=d*cy
        call d_standc(a8(7),a8(8),xa,xd,xi,xn)
        x=adcoef(1,1)*xi+adcoef(1,2)*xn+adcoef(1,3)
        y=adcoef(2,1)*xi+adcoef(2,2)*xn+adcoef(2,3)
	if(is6.ne.0)write(*,'(i6,2f9.3)')i,(2049.-x)*is6,(2049.-y)*is6
	if(is6.eq.0)write(*,'(i6,2f9.3)')i,x,y
	goto 20
100	end

        function indexpos(head,f1)
        character*80 head(72),f1*8
        byte bell
        data bell/7/
        do 10 indexpos=1,72
10      if(head(indexpos)(1:8).eq.f1)return
        if(indexpos.eq.73)write(*,'(1x,a)')bell
        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
