	character*80 f0,f1,f2,head(72)
	real*8 a8(8),b8(8),adcoef(2,3)
        real a(2048*2048),b(2048*2048)

	if(iargc().lt.3)then
	  write(*,*)
	  write(*,*)'         ******** turn around 2048*2048 Pic. ********'
	  write(*,*)
	  write(*,*)'    turn reference_file file out_file'
	  write(*,*)
          write(*,*)'                                      2000.7'
	  stop
	endif

	call getarg(1,f1)         ! refenence
	call getarg(2,f2)         ! orient
	call getarg(3,f0)         ! out_put file

	write(*,2)
2	format(24x,'shift(x,y)         UT.     exp.'/57('-'))

	call gethead(f1,head)
	call geta8(head,a8)
	call xytoad(a8,adcoef)
	x=a8(7)
	y=a8(8)

	call gethead(f2,head)
	call geta8(head,b8)
	ipos=indexpos(head,'A87     ')
	alpha=b8(7)
	delta=b8(8)
        call standc(x,y,alpha,delta,xi,xn)
        x2=adcoef(1,1)*xi+adcoef(1,2)*xn+adcoef(1,3)-1024.5
        y2=adcoef(2,1)*xi+adcoef(2,2)*xn+adcoef(2,3)-1024.5
        ipos1=indexpos(head,'TIME    ')
        ipos2=indexpos(head,'EXPOSURE')

	write(*,1)f2(1:22),x2,y2,head(ipos1)(16:27),head(ipos2)(24:30)
1	format(a,2f6.1,2x,2a)

	call readfits(f2,a,n1,n2)
	turn=(a8(2)+a8(3)-b8(2)-b8(3))/(a8(1)-a8(4))
	write(*,*)'turn: ',turn
	do 10 i=1,2048*2048
10	b(i)=0.
        call t_s(a,b,turn)
	call swap4(b,n1*n2*4)
	ipos=indexpos(head,'A81     ')
        do 11 i=1,4
11      write(head(ipos-1+i)(11:30),'(E20.12)')a8(i)

        i=lnblnk(f0)
	f0(i+1:i+1)=char(0)
	call writefits(f0,head,80*72,1)
	call writefits(f0,b,n1*n2*4,0)
        write(*,*)'re coord !'
        call system("coord "//f0(1:i)//" !")

c do shift end
	end

        subroutine t_s(a,b,turn)
        real a(2048,2048),b(2048,2048)
	do 10 j=1,2048
	do 10 i=1,2048
	y=j-(i-1024.5)*turn
	if(y.lt.1.or.y.gt.2048.)goto 10
	x=i+(j-1024.5)*turn
	if(x.lt.1.or.x.gt.2048.)goto 10
	iy0=y
	iy1=iy0+1
	wy=y-iy0
        zy=1.-wy
	ix0=x
	ix1=ix0+1
	wx=x-ix0
        zx=1.-wx
        x1=zx*zy
        x2=wx*zy
        x3=zx*wy
        x4=wx*wy
	b(i,j)=a(ix0,iy0)*x1+a(ix1,iy0)*x2+a(ix0,iy1)*x3+a(ix1,iy1)*x4

10	continue
        return
        end

	subroutine gethead(f1,head)
	character*80 f1,head(72)
	open(52,file=f1,status='old',access='direct',recl=5760)
	read(52,rec=1)head
	close (52)
	end
	
	subroutine geta8(head,a8)
	real*8 a8(8)
	character*80 head(72)
	ipos=indexpos(head,'A81     ')-1
	do 10 i=1,8
10	read(head(i+ipos)(12:),*)a8(i)
	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)                            ! from fits_head "A81"--"A88"
        pi=4.*atan(1.)
        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=cos(dc)
        td=tan(dc)
        alpha=atan(xi/cd/(1.-xn*td))
        delta=atan((xn+td)*cos(alpha)/(1.-xn*td))
        alpha=alpha+rc
        if(abs(dc-delta).gt.2.)then
          delta=-delta
          alpha=alpha+pi
        endif
	alpha=alpha*12./pi
	delta=delta*180./pi
        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
