	character*126 d0,d1
	character*1 cc(16),c1
	character*20 f1,f2,f3
	real a(16,10),e(16,10),xc(16)
	integer b(10)
	logical lo

	do 2 i=1,10
	b(i)=i+2
2	if(i.gt.6)b(i)=b(i)+(i-6)

	kk=iargc()
	if(kk.lt.2)then
	  write(*,*)
	  write(*,*)'        Usage: putap TA04i_12.mag2 TA04i_12.mag3'
	  write(*,*)
	  write(*,*)'       if av.dat existed, will use it'
	  write(*,*)
	  stop
	endif
	call getarg(1,f1)
	call getarg(2,f2)
	k=index(f1,'.')
	if(k.eq.0)f1=f1(1:lnblnk(f1))//'.mag2'
	inquire(file=f1,exist=lo)
	if(.not.lo)stop "file not found!"		
        open(1,file=f1,status='old')
 	open(2,file=f2,status='unknown')
        read(1,'(a)')d0
	write(2,'(a)')d0
        nc=(lnblnk(d0)-26)/6	

	f3="12345.ap"
	do 4 i=1,4
	j=ichar(f1(i:i))
	if(j.lt.96.and.j.gt.57)j=j+32
4	f3(i:i)=char(j)

	do 42 i=1,nc
42	xc(i)=0.
	inquire(file="av.dat",exist=lo)
	if(lo)then
	  open(3,file='av.dat',status='old')
	  do 41 i=1,nc
	  read(3,'(a)')d1
	  k=index(d1,'.')+1
	  read(d1(k:),*)cc(i),xc(i)
41	  continue
	  close(3)
	do 43 i=1,nc-1
	do 43 j=i+1,nc
	if(ichar(cc(i)).gt.ichar(cc(j)))then
	  c1=cc(i)
	  cc(i)=cc(j)
	  cc(j)=c1
	  x=xc(i)
	  xc(i)=xc(j)
	  xc(j)=x
	endif
43	continue 	 
	endif	

	do 5 i=1,nc
	j=29+i*6
	f3(5:5)=d0(j:j)
	write(*,*)f3,xc(i)
	open(i+10,file=f3,status='old')
	read(i+10,'(a)')d1
	read(i+10,'(a)')d1
	read(i+10,'(a)')d1
5	continue
        k=0
10	read(1,'(a)',end=99)d0
	read(1,'(a)')d1
	write(2,'(a)')d0
	write(2,'(a)')d1

	k=k+1
	
	do 20 i=1,nc
	read(i+10,'(a)')d1
	read(i+10,'(a)')d0
	read(i+10,'(a)')d1
	do 21 j=1,10
	read(d0(18+j*9:),'(f9.3)')a(i,j)
	if(a(i,j).gt.99.9)a(i,j)=99.
21	if(a(i,j).lt.90.)a(i,j)=a(i,j)+xc(i)
	do 22 j=1,10
	read(d1(18+j*9:),'(f9.4)')e(i,j)
22	if(e(i,j).gt.9.99)e(i,j)=9.99
20	continue

	do 30 j=1,10
	write(2,'(26x,i2,2x,16(f6.2))')b(j),(a(i,j),i=1,nc)
30	write(2,'(30x,16(f6.2))')(e(i,j),i=1,nc)

	goto 10
99	close(2)	
	write(*,*)'total: ',k
	end
