	character*60 f1,f2
	character*12 ac,dc
	real*8 a8(8)
	character*80 head(72)
        character*15 time
	character*10 date
        character*9  exposure
        character*8  skyadu,airmass
	logical logi
	k=iargc()
	if(k.eq.0)then
	  write(*,*)'               usage: j_xy2048ad filename'
	  write(*,*)'                                      '
	  write(*,*)'                  file is come from j_phot'
	  write(*,*)'                  epoch = 2000.0'
	  write(*,*)
	  stop
	endif
	call getarg(1,f2)
        i=index(f2,'.')
	k=lnblnk(f2)
	if(i.ne.0) k=i-1
	f1=f2(1:k)//'.fit'
        write(*,*)f1,"-->cat"
	inquire(file=f1,exist=logi)
	if(.not.logi)stop 'fits file not found !'
	open(2,file=f1,status='old',access='direct',recl=5760)
	read(2,rec=1)head
	close(2)
	ipos=indexpos(head,'DATE-OBS')
	read(head(ipos)(21:29),'(a)')date
	ipos=indexpos(head,'TIME    ')
	read(head(ipos)(16:29),'(a)')time
	ipos=indexpos(head,'EXPOSURE')
	read(head(ipos)(23:30),'(a)')exposure
	ipos=indexpos(head,'VOLT2   ')
	read(head(ipos)(24:30),'(a)')skyadu
	ipos=indexpos(head,'AIRMASS ')
	read(head(ipos)(24:30),'(a)')airmass

	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)

	f1=f2(1:k)//'.ap'
	inquire(file=f1,exist=logi)
	if(.not.logi)stop '.ap file not found !'
	open(1,file=f1,status='old')

	f1=f2(1:k)//'.cat'
	open(2,file=f1,status='unknown')
        write(2,'(6a)')date,time,exposure,skyadu,airmass,
     *  '  Date-S_UT-Exp-Sky-Airmass'

	read(1,'(a)')f2
	read(1,'(a)')f2
	read(1,'(a)')f2
20	read(1,*,err=100,end=100)i,x,y,z
	read(1,*)sky,x1,y1,z1
	x1=2049.-x
	y1=2049.-y
        call xy_rad(x1,y1,a8,alpha,delta)
	call toms(alpha,ac,1)
	call toms(delta,dc,0)
c  alpha delta x y mag s_mag sky
	if(z.lt.90.)write(2,1)ac,dc,x,y,z,z1,sky
1	format(a,1x,a,4f8.3,f9.2)
	goto 20
100	close(1)
	close(2)
	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 xy_rad(x,y,a8,alpha,delta)
c 2048*2048 ccd's X Y (in pixel) -------------> alpha delta (in h,d)
        real x,y,alpha,delta
        real*8 a8(8),pi,xi,xn,rc,dc,cd,td,alph,delt         
        pi=4.*datan(1d0)
        xi=a8(1)*x+a8(3)*y+a8(5)
        xn=a8(2)*x+a8(4)*y+a8(6)
        rc=a8(7)                                ! center of ccd (in rad.)
        dc=a8(8)
        cd=dcos(dc)
        td=dtan(dc)
        alph=datan(xi/cd/(1.-xn*td))
        delt=datan((xn+td)*dcos(alph)/(1.-xn*td))
        alph=alph+rc
        if(abs(dc-delt).gt.2.)then
          delt=-delt
          alph=alph+pi
        endif
        alpha=alph*12./pi
        delta=delt*180./pi
        end
