	parameter (n=49999)
        character*30 f1,f2,f3
	character*133 d0,d1
	real a(n),d(n),c(n),z(n)
	real x(n),y(n)

        if(iargc().lt.3)then
        write(*,*)
        write(*,*)
        write(*,*)'     **********   append 4k data to *.imag1 *******'
        write(*,*)
        write(*,*)
        write(*,*)'     Usage:  app4k Ta24i_12.mag1 TA24a.cat av_mag'
        write(*,*)
        write(*,*)
        stop
	endif

	call getarg(1,f1)
	open(1,file=f1,status='old')
	do 10 i=1,n
	read(1,'(a)',end=11,err=11)d0
	read(1,'(a)',end=11,err=11)d1
        call tohd(d1(2:24),a(i),d(i))
        c(i)=15.1
	read(d1(39:),*,end=10,err=10)j
	c(i)=j*0.001
10	continue
11	nn=i-1
	close(1)
	write(*,*)'*.mag1 read_in  ',nn
	call getarg(3,f2)
	read(f2(1:5),*)av_mag
	write(*,*)'av_mag: ',av_mag
	call getarg(2,f2)
	open(1,file=f2,status='old')
	k=0
	do 19 i=1,nn
19	x(i)=0.
	read(1,'(a)')d0
20	read(1,'(a)',end=30)d1
        call tohd(d1(2:24),aa,dd)
	s=2.
	do 21 i=1,nn
        r=(aa-a(i))**2*60.+(dd-d(i))**2
	if(r.lt.s)then
	  s=r
          j=i
	endif
21	continue
	s=s*3600*3600.
	ss=1.
	if(c(j).lt.15) ss=1.-(c(j)-15)*0.1
	if(s.lt.ss)then
	  k=k+1	
	  x(j)=j
	  read(d1(43:),*)y(j),z(j)
	endif

	goto 20
30	write(*,*)k
	close(1)
	read(f1(7:8),*)k
	f3=f1
	k=k+1
	write(f3(7:8),'(i2)')k
        if(f3(7:7).eq.char(32))f3(7:7)=char(48);
	write(*,*)'output file: ',f3
	open(2,file=f3,status='unknown')
	open(1,file=f1,status='old')
	read(1,'(a)')d0
	d0(k*6+36:k*6+36)=f2(5:5)
	write(2,'(a)')d0
	do 40 i=1,nn
	read(1,'(a)')d0
	read(1,'(a)')d1
	if(x(i).ne.0.)then
	  write(d0(k*6+33:),'(i5)')nint((y(i)+av_mag)*1000)
	  write(d1(k*6+33:),'(f5.3)')z(i)
          d1(k*6+33:k*6+33)=' '
	endif
	write(2,'(a)')d0
	write(2,'(a)')d1
40	continue
	close(1)
	close(2)
	
	end

        subroutine tohd(cc,alpha,delta)
        character*(*) cc
        character*20 dd
        k1=index(cc,' ')
        k2=index(cc,',')
        k=min(k1,k2)
        if(k1.eq.0)k=k2
        if(k2.eq.0)k=k1
        dd=cc(1:k)
        do 10 i=1,9
10      if(dd(i:i).eq.':')dd(i:i)=','
        read(dd(1:),*,end=15)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
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)=','
        read(dd(1:K),*,end=30)x,y,z
30      if(x.lt.0.)x=-x
        delta=x+y/60.+z/3600.
        if(index(cc,'-').ne.0)delta=-delta
        end


