	parameter (m=20000)
	character*69 f1,f2,f0
	real x1(m),y1(m),xmag1(m),xmag1err(m)
	real x2(m),y2(m),xmag2(m)
        real xk(m),yk(m)
	integer kk(m)
	real z(m)
	logical logi

	if(iargc().lt.2)then
	  write(*,*)'  Usage: all7 a03j.cat6 p....j025.cat6'
	  write(*,*)
	  write(*,*)' match 2 file ,  output *.con'
	  write(*,*)
	  stop
	endif

	call getarg(1,f1)
	call chfile(f1,'.cat6')
        open(1,file=f1,status='old')
	read(1,'(a)',end=20)f0
	i=0
10	i=i+1
	read(1,'(a)',end=20)f0
	if(f0(1:1).ne.' ')then
	  i=i-1
	else
  	  call ihd(f0,x1(i),y1(i))
	  read(f0(26:),*)xmag1(i),xmag1err(i)
	endif
	goto 10
20	n1=i-1
	close(1)
	write(*,*)f1(1:9),'all:',n1+j,' marked:',j,' readin:',n1

	call getarg(2,f2)
	call chfile(f2,'.cat6')
	  write(*,*)f2
        open(1,file=f2,status='old')
	k=index(f2,'.')
	inquire(file=f2(1:k)//'gc',exist=logi)
	if(.not.logi)stop
	open(2,file=f2(1:k)//'gc',status='old')
	open(3,file=f2(1:k)//'con',status='unknown')
22	read(2,'(a)',end=21)f0
	write(3,'(a)')f0
	goto 22
21	close(2)
	read(1,'(a)')f0
	i=0
110	i=i+1
	read(1,'(a)',end=120)f0
	if(f0(1:1).ne.' ')then
	  i=i-1
	else
	  call ihd(f0,x2(i),y2(i))
	  read(f0(26:),*)xmag2(i),x,kk(i),xk(i),yk(i)
	endif
	goto 110
120	n2=i-1
	close(1)
	write(*,*)f2(1:9),'all:',n2+j,' marked:',j,' readin:',n2
	write(*,*)

	r4=4
	ip=0
	do 30 i=1,n1
	z1=x1(i)
	z2=y1(i)
        r=r4
	do 31 j=1,n2	
          x=x2(j)-z1
          if(x.gt. 999999.)x=x-1296000.
          if(x.lt.-999999.)x=x+1296000.
        zz=x*x+(y2(j)-z2)**2
	if(zz.lt.r)then
	  r=zz
	  jp=j
	endif
31	continue
	if(ip.eq.30)goto 30
	if(r.lt.r4)then
	  ip=ip+1
	  z(ip)=xmag2(jp)-xmag1(i)
	  write(*,'(i3,3f8.3)')ip,xmag1(i),xmag2(jp),z(ip)
	  if(ip.le.30)write(3,'(i6,2f8.1,f8.3,2f10.3)')
     c         kk(jp),xk(jp),yk(jp),xmag1err(i),xmag2(jp),xmag1(i)
	endif	
30	continue
        if(ip.gt.30)ip=30
	zz=sigma(z,ip)
	write(*,*)' sigma: ',zz
        write(3,*)'total, sigma: ',ip,zz
	close(3)
	end	

	subroutine chfile(f1,a)
	character*(*) f1,a
	
        i=index(f1,'.')
	k=lnblnk(f1)
	if(i.ne.0) k=i-1
	f1=f1(1:k)//a

	end

        function sigma(b,k)
        real b(k)
        x=0
        do 20 j=1,k
20      x=x+b(j)
        x=x/k
	write(*,*)' average: ',x
	write(3,*)' average: ',x
        xx=0
        do 30 j=1,k
30      xx=xx+(b(j)-x)**2
        sigma=sqrt(xx/k)
        end


	subroutine ihd(a,x,y)
	character*60 a
	read(a(1:),'(2(1x,i2,1x,i2,1x,f5.2))')i1,j1,z1,i2,j2,z2
          y=(i2+j2/60.+z2/3600.)*3.14159265/180.
          y=cos(y)
	x=(i1*3600+j1*60+z1)*15.*y
	y= i2*3600+j2*60+z2
	end
