	parameter (n=9000)
	parameter (second=4)               ! err_bar =2 pixel
	character*80 f0,f1,f2
	real ax2(n),ay2(n),az2(n)
	ip=0
	ich=0
        if(iargc().eq.0)then
	  write(*,*) 
	  write(*,*)'            Usage: all4 batch_file          97,7,3'
	  write(*,*)
	  write(*,*)'     example:'
	  write(*,*)'             ls *.res >1.1  (or *.bri)'
	  write(*,*)'             all4 1.1'
	  write(*,*)
	  write(*,*)'       output file: all4.tmp'
	  write(*,*)
	  stop
	endif

        pi=4.*datan(1d0)
        cx=pi/12.
        cy=pi/180.

	open(1,file='gscno.par',status='old')
	read(1,'(a)')f0
	close(1)
	call itohd3(f0,alpha,delta,xmag)
	cosdelta=cos(delta*cy)

        call getarg(1,f1)
	open(51,file=f1,status='old')

	read(51,'(a)')f1
	inquire(file=f1,exist=i)
	if(i.eq.0)then
	  write(*,*)'not found: '//f1
	  stop
	endif

	read(51,'(a)',end=100)f2
	if(f2(1:1).eq.' ')goto 100
	inquire(file=f2,exist=i)
	if(i.eq.0)then
	  write(*,*)'not found: '//f2
	  stop
	endif

101	i=0
	open(1,file=f2,status='old') 
	read(1,'(a)')f0
	read(f0(12:),*)zm
	read(1,'(a)')f0

40	read(1,'(a)',end=41)f0
	i=i+1
	call itohd3(f0,alpha,delta,xmag)
	ax2(i)=alpha*54000.*cosdelta
	ay2(i)=delta*3600.
	az2(i)=xmag+zm
	goto 40
41	close(1)
	n2=i

	open(2,file='all4.tmp',status='unknown')
	open(1,file=f1,status='old')	
	read(1,'(a)')f0
	read(f0(12:),*)zm
	write(f0(12:),'(a)')'  0.0'
	write(2,'(a)')f0
	read(1,'(a)')f0
	write(2,'(a)')f0
	if(f0(30:33).eq."usno")ich=ich+1
	read(1,'(a)')f0
	kk=lnblnk(f0)
	goto 29

30	read(1,'(a)',end=31)f0
29	call itohd3(f0,alpha,delta,xmag)
	x=alpha*54000.*cosdelta
	y=delta*3600.
	r=30.
	do 80 j=1,n2
	z=(ax2(j)-x)**2+(ay2(j)-y)**2
	if(z.lt.r)k=j
80	if(z.lt.r)r=z
	if(r.lt.second)then
	  if(zm.ne.0.)then
	    read(f0(24:),*)z
	    z=z+zm
	    write(f0(24:28),'(f5.1)')z
	  endif
	  write(2,'(a,f5.1)')f0(1:kk),az2(k)
	endif
	goto 30
31	close(1)
	close(2)
	ip=ip+1
	inquire(file='all4.ttt',exist=i)
	if(i.ne.0)call unlink('all4.ttt')
	read(51,'(a)',end=100)f2
	if(f2(1:1).eq.' ')goto 100
	inquire(file=f2,exist=i)
	if(i.eq.0)then
	  write(*,*)'not found: '//f2
	  goto 100
	endif
	call system('mv all4.tmp all4.ttt')
	f1='all4.ttt'
	goto 101
100	close(51)
	
	if(ip.ne.0)then
	  if(ich.ne.0)then
	    open(1,file='all4.tmp',status='old')
	    open(2,file='all4.ttt',status='unknown')
103	    read(1,'(a)',end=102)f0
	    f1=f0
	    f1(24:28)=f0(29:33)
	    f1(29:33)=f0(24:28)
	    write(2,'(a)')f1
	    goto 103
102	    close(1)
	    close(2)
	    call unlink('all4.tmp')
	    call system('mv all4.ttt all4.tmp')
	  endiF	    
	  write(*,*)' out file is: all4.tmp'
	endif
	end

	subroutine stat_mag(a,n,z,m)
	real a(n)
100	x=0
	m=0
	do 10 i=1,n
	if(a(i).lt.99)then
	  x=x+a(i)
	  m=m+1
	endif
10	continue
	z=x/m

	xx=0
	do 20 i=1,n
20	if(a(i).lt.99) xx=xx+(a(i)-z)**2
	zz=sqrt(xx/m)
	zz=3*zz
	   write(*,*)m,zz,z
	mm=0
	do 30 i=1,n
	  if(abs(a(i)-z).gt.zz)then
	    a(i)=99.99
	  else
	    mm=mm+1
          endif
30	continue
	if(mm.ne.m)goto 100
	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
	if(ipos.gt.70)stop '  not batc_fits_file!    '
        do 10 i=1,8
10      read(head(i+ipos)(12:),*)a8(i)
        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 itohd3(cc,alpha,delta,xmag)
	character*80 cc
	character*80 dd
	k1=index(cc(2:),' ')
	k2=index(cc,',')
	k=min(k1,k2)
	if(k1.eq.0)k=k2
	if(k2.eq.0)k=k1
	if(k.lt.2.or.k.gt.13)goto 100
	dd=cc(1:k)
	do 10 i=1,9
10	if(dd(i:i).eq.':')dd(i:i)=','
	x=0
	y=0
	z=0
	read(dd(1:),*,end=15,err=100)x,y,z
15	alpha=x+y/60.+z/3600.
	do 16 i=k+1,k+6
	if(cc(i:i).ne.' ')goto 17
16	continue
	goto 100
17	dd=cc(i:)
	k1=index(dd,' ')
	k2=index(dd,',')
	k=min(k1,k2)
	if(k1.eq.0)k=k2
	if(k2.eq.0)k=k1
	do 20 i=1,k
20	if(dd(i:i).eq.':')dd(i:i)=','
	x=0
	y=0
	z=0
	read(dd(1:k),*,end=30,err=100)x,y,z
30	if(x.lt.0.)x=-x
	delta=x+y/60.+z/3600.
	if(index(cc,'-').ne.0)delta=-delta
	read(dd(k+1:),*,end=100,err=100)xmag
100	return
	end
