      program twread
      dimension wv(25000),wn(25000),we(25000)
      character*15 file1,file2
      character*60 line1

c      file1='PT076001.ASC'
c      file2='TCU068.ACC'

      write(*,*)'Taiwan Chi-Chi Earthquake Strong Motion Data (A900)'
      write(*,*)'Enter Input File Name (PT******.ASC):'
      read(*,1000)file1
 1000 format(a15)

      open(10,file=file1)
        write(*,*)'Header'
        do i=1,6
          read(10,1100)line1
          write(*,1100)line1
        end do

        read(10,*)
        write(*,*)
        write(*,*)'Ch1 (Vertical Component)'
        do i=1,11
          read(10,1100)line1
          write(*,1100)line1
        end do
        write(*,*)
        write(*,*)'Enter Output File Name (ex:***.acc):'
        read(*,1000)file2
        write(*,*)
        read(10,*)nv
        write(*,*)'number of samples =',nv
        read(10,*)Hzv
        write(*,*)'samples per second (Hz) =',Hzv
        read(10,*)aminv
        write(*,*)'Min Amplitude =',aminv
        read(10,*)amaxv
        write(*,*)'Max Amplitude =',amaxv
        do i=1,4
          read(10,1100)line1
          write(*,1100)line1
        end do
        do i=1,nv/10
          read(10,1200)(wv((i-1)*10+j),j=1,10)
c          write(*,1200)(wv((i-1)*10+j),j=1,10)
        end do

        read(10,*)
        read(10,*)
        write(*,*)
        write(*,*)'Ch2 (NS Component)'
        do i=1,11
          read(10,1100)line1
c          write(*,1100)line1
        end do
        read(10,*)nn
c        write(*,*)'number of samples =',nn
        read(10,*)Hzn
c        write(*,*)'samples per second (Hz) =',Hzn
        read(10,*)aminn
c        write(*,*)'Min Amplitude =',aminn
        read(10,*)amaxn
c        write(*,*)'Max Amplitude =',amaxn
        do i=1,4
          read(10,1100)line1
c          write(*,1100)line1
        end do
        do i=1,nn/10
          read(10,1200)(wn((i-1)*10+j),j=1,10)
c          write(*,1200)(wn((i-1)*10+j),j=1,10)
        end do

        read(10,*)
        read(10,*)
        write(*,*)'Ch3 (EW Component)'
        do i=1,11
          read(10,1100)line1
c          write(*,1100)line1
        end do
        read(10,*)ne
c        write(*,*)'number of samples =',ne
        read(10,*)Hze
c        write(*,*)'samples per second (Hz) =',Hze
        read(10,*)amine
c        write(*,*)'Min Amplitude =',amine
        read(10,*)amaxe
c        write(*,*)'Max Amplitude =',amaxe
        do i=1,4
          read(10,1100)line1
c          write(*,1100)line1
        end do
        do i=1,ne/10
          read(10,1200)(we((i-1)*10+j),j=1,10)
c          write(*,1200)(we((i-1)*10+j),j=1,10)
        end do

      close(10)
 1100 format(a60)
 1200 format(10f7.0)

      write(*,*)
      n=nv
      if(nn.ne.n)write(*,*)'nn.ne.nv !!'
      if(ne.ne.n)write(*,*)'ne.ne.nv !!'
      Hz=Hzv
      if(Hzn.ne.HZ)write(*,*)'Hzn.ne.Hzv !!'
      if(Hze.ne.HZ)write(*,*)'Hze.ne.Hzv !!'
      amin=aminv
      if(aminn.ne.amin)write(*,*)'aminn.ne.aminv !!'
      if(amine.ne.amin)write(*,*)'amine.ne.aminv !!'
      amax=amaxv
      if(amaxn.ne.amax)write(*,*)'amaxn.ne.amaxv !!'
      if(amaxe.ne.amax)write(*,*)'amaxe.ne.amaxv !!'
      if(-amin.ne.amax)write(*,*)'-amin.ne.amax !!'

      dt=1.0/Hz
      afac=2.0*980/amax
      avgv=0.0
      avgn=0.0
      avge=0.0
      navg=100
      do i=1,n/navg
        avgv=avgv+wv(i)
        avgn=avgn+wn(i)
        avge=avge+we(i)
      end do
      avgv=avgv/n*navg
      avgn=avgn/n*navg
      avge=avge/n*navg

      amaxv=0.0
      amaxn=0.0
      amaxe=0.0
      aminv=0.0
      amine=0.0
      aminn=0.0
      do i=1,n
        wv(i)=(wv(i)-avgv)*afac
        wn(i)=(wn(i)-avgn)*afac
        we(i)=(we(i)-avge)*afac
        if(wv(i).gt.amaxv)amaxv=wv(i)
        if(wn(i).gt.amaxn)amaxn=wn(i)
        if(we(i).gt.amaxe)amaxe=we(i)
        if(wv(i).lt.aminv)aminv=wv(i)
        if(wn(i).lt.aminn)aminn=wn(i)
        if(we(i).lt.amine)amine=we(i)
      end do
      write(*,*)'Max and Min UD Acceleration (gal)=',amaxv,aminv
      write(*,*)'Max and Min NS Acceleration (gal)=',amaxn,aminn
      write(*,*)'Max and Min EW Acceleration (gal)=',amaxe,amine

      write(*,*)
      open(11,file=file2)
        do i=1,n
          write(11,2000)dt*(i-1),wv(i),wn(i),we(i)
        end do
      close(11)
 2000 format(f8.3,3f10.3)
      write(*,*)'Outpout File Name (Acc: Unit gal) = ',file2
      write(*,*)'Data Format(f8.3,3f10.3): time (sec), UD, NS, EW'

      stop
      end
