        parameter (nnsa1=29999)                 ! for max number of sa1        
        real aasa1(nnsa1),ddsa1(nnsa1)         !             for sa1
        byte ebsa1(nnsa1),ersa1(nnsa1)         ! magnitude for b & r color
        integer numsa1(nnsa1),catsa1(nnsa1)
        character*8 file1,file2
	character*12 ac,dc
	character*30 d3
	character*1 cc(16),c1
	integer mc(16)
	real xmag(16),err(16)

	character*133 d0,d1,d2
	character*20 f1,f2
	logical lo
	kk=iargc()
	if(kk.lt.2)then
	  write(*,*)'        Usage: chformat T517i_15.mag T517i_15.mag2'
	  write(*,*)
	  stop
	endif
	call getarg(1,f1)
	call getarg(2,f2)
	k=index(f1,'.')
	if(k.eq.0)f1=f1(1:lnblnk(f1))//'.mag'
	inquire(file=f1,exist=lo)
	if(.not.lo)stop "file not found!"		
        open(1,file=f1,status='old')
 	open(3,file=f2,status='unknown')
        read(1,'(a)')d0
        nc=(lnblnk(d0)-33)/6	
	do 5 i=1,nc
	mc(i)=i
	j=36+i*6
5	cc(i)=d0(j:j)
c	write(*,'(16a3)')(cc(i),i=1,nc)
	do 6 i=1,nc-1
	do 6 j=i+1,nc
	if(cc(i).gt.cc(j))then
	  c1=cc(i)
	  cc(i)=cc(j)
	  cc(j)=c1
	  k=mc(i)
	  mc(i)=mc(j)
	  mc(j)=k
	endif  
6	continue	
c	write(*,'(16a3)')(cc(i),i=1,nc)
	if(d0(23:23).eq.'-')d0(23:24)=' -'
	d1=d0(3:8)//d0(10:22)//d0(24:33)//' 2K'
	do 7 i=1,nc
	j=29+i*6
7	d1(j:j)=cc(i)
	write(3,'(a)')d1
          i24=0
        call 	ihd(d1(7:),x1,y1,1)
          if(x1/54000. .gt. 23.)i24=1
          cosy=cos(y1/3600.*3.141592654/180.)
        x2=x1
	y2=y1	
10      read(1,'(a)',end=19)d1
        read(1,'(a)')d2
        call 	ihd(d1,x,y,1)
	if(x.lt.x1)x1=x
	if(x.gt.x2)x2=x
	if(y.lt.y1)y1=y
	if(y.gt.y2)y2=y
	goto 10	
19	close(1)
        
c find sa2_star
	up_a=x2/54000.
	dn_a=x1/54000.
	up_d=y2/3600.
	dn_d=y1/3600.
          if(up_a.gt.24. .and. dn_a.gt.24.)then
            up_a=up_a-24.
            dn_a=dn_a-24.
          endif 
          if(up_a.lt.24. .and. dn_a.lt.24.)i24=0
	  write(*,*)up_a,dn_a,up_d,dn_d,cosy,i24
        nsa1=0
        i=(up_d+90.)*10.
        i=i/75*75
        write(file1(1:),"('zone',i4.4)")i
        i=(dn_d+90.)*10.
        i=i/75*75
        write(file2(1:),"('zone',i4.4)")i
        call cat_sa1(file1,
     *    aasa1,ddsa1,ebsa1,ersa1,nnsa1,nsa1,
     *    numsa1,up_a,dn_a,up_d,dn_d,i24)
        write(*,*)file1,nsa1
        if(file2.ne.file1)then
	  call cat_sa1(file2,
     *    aasa1,ddsa1,ebsa1,ersa1,nnsa1,nsa1,
     *    numsa1,up_a,dn_a,up_d,dn_d,i24)
        write(*,*)file2,nsa1
	endif
c march
	dx=0
	dy=0
	do 200 k=1,2
	num=0
	xx=0
	yy=0
	nn=0
	do 109 i=1,nsa1
109	catsa1(i)=0
        open(1,file=f1,status='old')
        read(1,'(a)')d0
110      read(1,'(a)',end=119)d1
        read(1,'(a)')d2
        call 	ihd(d1,x,y,i24)
	rr=8.
	 y=y-dy
	 x=x-dx
	do 120 i=1,nsa1
	z=((aasa1(i)-x)*cosy)**2+(ddsa1(i)-y)**2
	if(z.lt.rr)then
	  rr=z
	  m=i
	endif
120	continue
	nn=nn+1
	d3=' '
	if(rr.lt.2.*k.and.(d1(1:1).ne.'b'))then
	  num=num+1
	  xx=xx+x-aasa1(m)
	  yy=yy+y-ddsa1(m)
	  if(k.eq.2)then
	    catsa1(m)=num
            j=ebsa1(m)
            if(j.lt.0)j=j+256
            xbbb=j*0.1
            j=ersa1(m)
            if(j.lt.0)j=j+256
            xrrr=j*0.1
            i=numsa1(m)
            j=i
            i=i/100000000*75+150          ! 31203                  
            j=mod(j,100000000)
c           call toms(x/54000.,ac,1)
c	    call toms(y/3600.,dc,0)
            write(d3(1:),203)i,j,xbbb,xrrr
203         format('zone',i4.4,'.',i8.8,f5.1,'B',f5.1,'R')
c	    call toms(aasa1(m)/54000.,ac,1)
c	    call toms(ddsa1(m)/3600.,dc,0)
c	    write(*,'(6x,a,1x,a,f5.1,f5.1)')ac,dc,x-aasa1(m),y-ddsa1(m) 
	  endif
	endif
	if(k.eq.2)then
	  do 201 i=1,nc
	  read(d1(i*6+33:),'(i5)')j
	  xmag(i)=j*0.001
	  j=i*6+34
	  if(d2(j:j).eq.'.')d2(j:j)=' '
	  read(d2(i*6+33:),'(a1,i4)')cc(i),j
	  err(i)=j*0.001
	  if(err(i).gt.9.99)err(i)=9.99
201	  continue
            if(d1(2:2).eq.'2'.and.d1(3:3).eq.'4')then
	      d1(2:2)='0'
	      d1(3:3)='0'
            endif
	  write(3,'(a,i5,2a,16f6.2)')d1(1:1),nn,' ',d1(2:24),(xmag(mc(i)),i=1,nc)
	  write(3,'(a,16(a2,f4.2))')d3(1:30),(cc(mc(i)),err(mc(i)),i=1,nc)
	endif
	goto 110
119	close(1)
	dx=xx/num
	dy=yy/num
	if(k.eq.1)write(*,'(a,i5)')'all_star:',nn
200	write(*,'(a,i6,a,2f7.1)')'match:',num,'  shift:',dx,dy
	close(3)
c writedown nosa2.cat
	open(1,file='nosa2.cat',status='unknown')
	do 300 m=1,nsa1
          j=ebsa1(m)
          if(j.lt.0)j=j+256
          xbbb=j*0.1
          j=ersa1(m)
          if(j.lt.0)j=j+256
          xrrr=j*0.1
          i=numsa1(m)
          j=i
          i=i/100000000*75+150                  ! 31203
          j=mod(j,100000000)
            xx=aasa1(m)/54000.
            if(xx.ge.24.)xx=xx-24.
          call toms(xx,ac,1)
          call toms(ddsa1(m)/3600.,dc,0)
          write(1,202)ac,dc,catsa1(m),i,j,xbbb,xrrr
202       format(a,1x,a,i5,' zone',i4.4,'.',i8.8,f5.1,'B',f5.1,'R')
300     continue
	close(1)
	write(*,*)' output USNO2 file: nosa2.cat'
        end

        subroutine ihd(a,x,y,i24)
        character*60 a
        read(a(1:),'(2(1x,i2,1x,i2,1x,f5.2))')i1,j1,z1,i2,j2,z2
        if(i2.lt.0)i2=-i2
        if(i24.eq.1.and.i1.lt.23)i1=i1+24.
        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


        subroutine cat_sa1(f1,aa,dd,eb,er,nn,n,
     *    numsa1,up_a,dn_a,up_d,dn_d,i24)
        real aa(nn),dd(nn)
        byte eb(nn),er(nn)
        integer numsa1(nn)
        character*8 f1
        integer*4 a(3)
        real*8 xx
        logical logi
        inquire(file='/USNO2/'//f1//'.cat',exist=logi)
        if(.not.logi)then
	  write(*,*)'Cannot find USNO2'
	  stop
	endif
        open(1,file='/USNO2/'//f1//'.cat',
     *     status='old',access='direct',recl=12)
        open(2,file='/USNO2/'//f1//'.acc',status='old')
          read(f1(5:),*)ifsa1
          ifsa1=(ifsa1/75-2)*100000000               !301203
        do 20 k=1,96
        read(2,*)x1,i
        if(x1+0.25.ge.dn_a)goto 21
20      continue
21      close(2)
        x1=dn_a
        x2=up_a
	i=i-1
110	i=i+1
        read(1,rec=i,err=100)a
          call swap4(a,12)
          xx=a(1)
          ra=xx/5400000.
          xx=a(2)
          de=xx/360000.-90.
        if(ra.gt.x2)goto 100
        if(de.gt.up_d.or.de.lt.dn_d)goto 110
        if(ra.lt.x1)goto 110
          if(n+1.gt.nn)goto 100
          k=a(3)
          if(k.lt.0)k=-k
          n=n+1
          aa(n)=ra*54000.
          dd(n)=de*3600.
          er(n)=mod(k,1000)
          k=k/1000
          eb(n)=mod(k,1000)
          numsa1(n)=ifsa1+i
        goto 110
100     if(i24.eq.0)then
	  close (1)
          return
	endif
	i=0
210	i=i+1
        read(1,rec=i,err=200)a
          call swap4(a,12)
          xx=a(1)
          ra=xx/5400000.+24.
          xx=a(2)
          de=xx/360000.-90.
        if(ra.gt.x2)goto 200
        if(de.gt.up_d.or.de.lt.dn_d)goto 210
          if(n+1.gt.nn)goto 200
          k=a(3)
          if(k.lt.0)k=-k
          n=n+1
          aa(n)=ra*54000.
          dd(n)=de*3600.
          er(n)=mod(k,1000)
          k=k/1000
          eb(n)=mod(k,1000)
          numsa1(n)=ifsa1+i
        goto 210
200     close (1)
        end
