	parameter (m=60000)
	character*97 f1,f2,f0,ff,fe
	character*1 ch,color(16),od(16)
	real x1(m),y1(m),xmag1(m),err1(m)
	real x2(m),y2(m),xmag2(m),err2(m)
	integer all(16,m),num(m),jd(16)
	real err(16,m)
	logical logi
	
	if(iargc().lt.1)then
	  write(*,*)'  Usage: all u120i.cat batch out.cat'
	  write(*,*)
	  write(*,*)'           if need order.dat'
	  stop
	endif
	call getarg(1,f1)
	i4=index(f1,'.')-1
	ch=f1(i4:i4)
        open(1,file=f1,status='old')
	read(1,'(a)',end=20)f0
	do 9 i=23,32
9	if(f0(i:i).eq.':'.or.f0(i:i).eq.'.')f0(i:i)=','
        read(f0(23:),*)i,j,k
        z2=i+j/60.+k/3600.
	cosy=cos(z2*3.141592654/180.)
	write(*,*)cosy
	i=0
	j=0
10	i=i+1
	read(1,'(a)',end=20)f0
	if(f0(1:1).ne.' ')then
	  j=j+1
	  i=i-1
	else
  	  call ihd(f0,x1(i),y1(i))
	  read(f0(42:),*)xmag1(i),err1(i),num(i)
	endif
	goto 10
20	n1=i-1
	close(1)
	write(*,*)f1(1:9),'  all:',n1+j,' marked:',j,' readin:',n1
	color(1)=f1(i4:i4)

	inquire(file='order.dat',exist=logi)
	id=0
	if(logi)then
	  open(2,file='order.dat',status='old')
21	  id=id+1
	  read(2,'(a)',end=22)od(id)
	  goto 21
22	  close(2)
	  id=id-1
	endif
	if(color(1).eq.od(1))id=0

	do 101 i=2,15
	do 101 j=1,m
	all(i,j)=0
101	err(i,j)=0
	do 102 j=1,m
	all(1,j)=nint(xmag1(j)*1000)
102	err(1,j)=err1(j)

	call getarg(2,f2)
        open(2,file=f2,status='old')
	ik=1
1018	read(2,'(a)',end=1019)f0
	if(f1.eq.f0)goto 1018
	ik=ik+1
	color(ik)=f0(i4:i4)
	goto 1018
1019	close(2)

        open(2,file=f2,status='old')
	ik=1
118	read(2,'(a)',end=119)f0
	if(f1.eq.f0)goto 118
        ik=ik+1
	  open(1,file=f0,status='old')
	  read(1,'(a)')f2
	i=0
	j=0
110	i=i+1
	read(1,'(a)',end=120)f2
	if(f2(1:1).ne.' ')then
	  j=j+1
	  i=i-1
	else
	  call ihd(f2,x2(i),y2(i))
	  read(f2(42:),*)xmag2(i),err2(i)
	endif
	goto 110
120	n2=i-1
	close(1)
        markj=j

	ip=0
	do 30 i=1,n1
	z1=x1(i)
	z2=y1(i)
        r=4.
	do 31 j=1,n2	
          x=x2(j)-z1
          if(x.gt. 999999.)x=x-1296000.
          if(x.lt.-999999.)x=x+1296000.
          x=x*cosy
        z=x*x+(y2(j)-z2)**2

	if(z.lt.r)then
	  r=z
	  jp=j
	endif
31	continue
	if(r.lt.4.)then
          all(ik,i)=nint(xmag2(jp)*1000)
          err(ik,i)=err2(jp)
	  ip=ip+1
	  xmag2(jp)=0
	endif	
30	continue

	do 130 i=1,n1
        if(all(ik,i).ne.0)goto 130
	z1=x1(i)
	z2=y1(i)
        r=16.
	do 131 j=1,n2	
	if(xmag2(j).eq.0)goto 131
	z=(x2(j)-z1)**2+(y2(j)-z2)**2	
	if(z.lt.r)then
	  r=z
	  jp=j
	endif
131	continue
	if(r.lt.16.)then
          all(ik,i)=nint(xmag2(jp)*1000)
          err(ik,i)=err2(jp)
	  ip=ip+1
	  xmag2(jp)=0
	endif	
130	continue


	  open(1,file=f0,status='old')
	  open(3,file=f0(1:lnblnk(f0))//ch,status='unknown')
	  read(1,'(a)')f2
	  write(3,'(a)')f2
	i=0
	j=0
410	i=i+1
	read(1,'(a)',end=420)f2
	if(f2(1:1).ne.' ')then
	  i=i-1
	else
	  if(xmag2(i).ne.0.)write(3,'(a)')f2
	  if(xmag2(i).ne.0.)j=j+1
	endif
	goto 410
420	close(1)
	close(3)

	write(*,*)f0(1:9),'  all:',n2+markj,' marked:',markj,' readin:',n2,
     *                         ' matched:',ip,' unmated:',j
	
c 2*2 arcsec
	goto 118
119	close(2)
	write(*,*)'ik',ik
c
c  write_down
	call getarg(3,f2)
	open(1,file=f1,status='old')
	open(2,file=f2,status='unknown')
        if(id.ne.0)goto 300
	read(1,'(a)')f0
	write(2,'(2a,15a6)')f0(1:41),(color(i),i=1,ik)
	i=0
210	i=i+1
	read(1,'(a)',end=330)f0
	if(f0(1:1).ne.' ')then
	  i=i-1
	else
	  do 220 j=1,ik
	  if(all(j,i).eq.0)then
	    write(ff(j*6-5:),*)'      '
	    write(fe(j*6-5:),*)'      '
	  else
            write(ff(j*6-5:),'(i6)')all(j,i)
            write(fe(j*6-5:),'(f6.3)')err(j,i)
	    fe(j*6-4:j*6-4)=' '
	  endif
220	  continue
	  ii=lnblnk(ff)
          write(2,'(4a)')f0(1:23),f0(25:31),f0(33:39),ff(1:ii) 
	  write(2,'(29x,i5,3x,a)')num(i),fe(1:ii)
	endif
	goto 210


c order:
300	read(1,'(a)')f0
	write(2,'(2a,15a6)')f0(1:41),(od(i),i=1,id)
c make table
c jd(1)=3, jd(2)=4...,jd(15)=16,jd(16)=16
	do 301 i=1,16
301	jd(i)=16
	do 302 i=1,id
	do 302 j=1,ik
302	if(od(i).eq.color(j))jd(i)=j

	
	i=0
310	i=i+1
	read(1,'(a)',end=330)f0
	if(f0(1:1).ne.' ')then
	  i=i-1
	else
	  do 320 j=1,id
  	  jj=jd(j)        
	  if(all(jj,i).eq.0)then
	    write(ff(j*6-5:),*)'      '
	    write(fe(j*6-5:),*)'      '
	  else
            write(ff(j*6-5:),'(i6)')all(jj,i)
            write(fe(j*6-5:),'(f6.3)')err(jj,i)
	    fe(j*6-4:j*6-4)=' '
	  endif
320	  continue
	  ii=lnblnk(ff)
          write(2,'(a,14x,a)')f0(1:23),ff(1:ii) 
	  write(2,'(29x,i5,3x,a)')num(i),fe(1:ii)
	endif
	goto 310
330	close(2)
	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
	x=(i1*3600+j1*60+z1)*15.
	y= i2*3600+j2*60+z2
	k=index(a(2:18),'-')
	if(k.gt.0)y=-y
	end
