C
      PROGRAM mwvtran
C
      PARAMETER(MWV=5000)
      CHARACTER INFILE*20
      REAL TIME(MWV),XR1(MWV),XR2(MWV),XR3(MWV)
      COMPLEX*8 FX1(MWV),FX2(MWV),FX3(MWV)
C
      WRITE(*,*)
      WRITE(*,*)
      WRITE(*,*) 'PROGRAM MWVPLOT - To obtain filtered seismograms'
      WRITE(*,*) '------------------------------------------------------
     +-----------'
      WRITE(*,*)
C
C  Prompt for input RECON'ed file
C
      WRITE(*,1000) 
 1000 FORMAT(1X,'Enter name of the wave datafile')
      READ(*,1020) INFILE
 1020 format(a20)
C      
C *** Read input data file  ***

      OPEN(11,FILE=INFILE,STATUS='OLD')
        i=1
        read(11,*)nobs
        write(*,*)
        write(*,*)'Number of Observation Points =',nobs
        write(*,*)'Enter Output Observation Point?'
        read(*,*)nout
 1      do j=1,nobs
          if(j.eq.nout)then
            READ(11,*,end=2)time(i),xR1(I),xR2(I),xR3(I)
          else
            read(11,*,end=2)
          end if
        end do
        i=i+1
        goto 1
 2    CLOSE(11)
      dt=time(2)-time(1)
      nt=i-1
      write(*,*)'dt,nt=',dt,nt
      if(nt.gt.mwv)then
        write(*,*)'nt>mwv !!!'
        stop
      end if
c
C *** change of + or -  ***
C
      WRITE(6,*)
      WRITE(6,*)' Change +/-? (YES=1)?? :'
      READ(5,*)MYES
C 
      IF(MYES.EQ.1)THEN
C
        WRITE(6,*)
        WRITE(6,*)' Change from + to - of FILE 1 ? (YES=1)?? :'
        READ(5,*)MY1
         IF(MY1.EQ.1)THEN
           DO 100 I=1,NT
             XR1(I)=-XR1(I)
  100      CONTINUE
         END IF
C
        WRITE(*,*)' Change from + to - of FILE 2 ? (YES=1)?? :'
        READ(5,*)MY2
         IF(MY2.EQ.1)THEN
           DO 110 I=1,NT
             XR2(I)=-XR2(I)
  110      CONTINUE
         END IF
C
        WRITE(*,*)' Change from + to - of FILE 3 ? (YES=1)?? :'
        READ(5,*)MY3
         IF(MY3.EQ.1)THEN
           DO 120 I=1,NT
             XR3(I)=-XR3(I)
  120      CONTINUE
         END IF
C
      END IF
C
C *** FOURIER TRANSFORM ***
C
      NF=2
      DO 200 I=2,14
        NF=2*NF
        IF(NF.GE.NT)GOTO 10
  200 CONTINUE
      WRITE(6,*)' NT>2**14 !!!!!'
      STOP
   10 IFF=I
      WRITE(6,*)
      WRITE(6,*)' IFF,NF=',IFF,NF
C
      DO 300 I=1,NT
        FX1(I)=CMPLX(XR1(I),0.0)
        FX2(I)=CMPLX(XR2(I),0.0)
        FX3(I)=CMPLX(XR3(I),0.0)
  300 CONTINUE
      DO 310 I=NT+1,NF
        FX1(I)=(0.0,0.0)
        FX2(I)=(0.0,0.0)
        FX3(I)=(0.0,0.0)
  310 CONTINUE
C
      CALL WVFAST(NF,FX1,MWV,-1)
      CALL WVFAST(NF,FX2,MWV,-1)
      CALL WVFAST(NF,FX3,MWV,-1)
      DUR=FLOAT(NF)*DT
      DO 400 I=1,NF
        FX1(I)=FX1(I)*DT
        FX2(I)=FX2(I)*DT
        FX3(I)=FX3(I)*DT
  400 CONTINUE
C
      FOM=0.0
      DOM=6.2831853070/DUR
      NOM=NF/2+1
      EOM=DOM*(NOM-1)
      WRITE(6,*)
      WRITE(6,*)' DUR,DOM,NOM,EOM=',DUR,DOM,NOM,EOM
C     
C *** BAND PATH ***
C
      WRITE(*,*)
      WRITE(*,*)' BAND PATH? (YES=1)?? :'
      READ(5,*)NBD
C
      IF(NBD.EQ.1)THEN
C
        WRITE(6,*)
        WRITE(6,*)'  TRAPEZOIDAL BAND PATH FILTER'
        WRITE(6,*)'  ENTER OML1<OML2<OMH1<OMH2'
        READ(5,*)OML1,OML2,OMH1,OMH2
C
        DO 500 I = 1,NOM
           OMEGA=FLOAT(I-1)*DOM
           IF(OMEGA.LE.OML1)then
             FX1(I)=(0.0,0.0)
             FX2(I)=(0.0,0.0)
             FX3(I)=(0.0,0.0)
           end if
           IF(OMEGA.GE.OMH2)then
             FX1(I)=(0.0,0.0)
             FX2(I)=(0.0,0.0)
             FX3(I)=(0.0,0.0)
           end if
           AA=1.0
           IF(OMEGA.LT.OML2)AA=(OMEGA-OML1)/(OML2-OML1)
           IF(OMEGA.GT.OMH1)AA=(OMH2-OMEGA)/(OMH2-OMH1)
	   FX1(I) = AA*FX1(I)
	   FX2(I) = AA*FX2(I)
	   FX3(I) = AA*FX3(I)
  500   CONTINUE
C
      END IF
C
      WRITE(*,*)
      WRITE(*,*)' Integration(=1) or Differentiation(=2)? :'
      READ(5,*)NIT
c
      IF(NIT.EQ.1)THEN
        DO 550 I = 2,NOM
           OMEGA=FLOAT(I-1)*DOM
	   FX1(I) = FX1(I)/cmplx(0.0,omega)
	   FX2(I) = FX2(I)/cmplx(0.0,omega)
	   FX3(I) = FX3(I)/cmplx(0.0,omega)
  550   CONTINUE
      END IF
c
      IF(NIT.EQ.1)THEN
        DO 570 I = 1,NOM
           OMEGA=FLOAT(I-1)*DOM
	   FX1(I) = FX1(I)*cmplx(0.0,omega)
	   FX2(I) = FX2(I)*cmplx(0.0,omega)
	   FX3(I) = FX3(I)*cmplx(0.0,omega)
  570   CONTINUE
      END IF
c
      write(*,*)'Output Amplitude data to amp_dat'
      open(10,file='amp_dat',status='unknown')
      do i=1,nom
        OMEGA=FLOAT(I-1)*DOM
        write(10,2000)omega,fx1(i),fx2(i),fx3(i)
      end do
 2000 format(f8.4,6e12.4)
      close(10)
C
C *** INVERSE FOURIER TRANSFORM  ***
C
      DO 600 I=2,NF/2
        FX1(NF+2-I)=CMPLX(REAL(FX1(I)),-AIMAG(FX1(I)))
        FX2(NF+2-I)=CMPLX(REAL(FX2(I)),-AIMAG(FX2(I)))
        FX3(NF+2-I)=CMPLX(REAL(FX3(I)),-AIMAG(FX3(I)))
  600 CONTINUE
C
      CALL WVFAST(NF,FX1,MWV,1)
      CALL WVFAST(NF,FX2,MWV,1)
      CALL WVFAST(NF,FX3,MWV,1)
C
C *** WAVE DATA ***
C
      DO 700 I = 1,NF
         XR1(I)=REAL(FX1(I))/DUR
         XR2(I)=REAL(FX2(I))/DUR
         XR3(I)=REAL(FX3(I))/DUR
  700 CONTINUE
C
c *** output ***
c
      WRITE(6,*)'Output wave data to wave_dat:' 
      OPEN(11,FILE='wave_dat',STATUS='unknown')
      do i=1,nt
      write(11,2100)time(i),XR1(I),XR2(I),XR3(I)
      end do
 2100 format(f8.3,3e12.4)
      CLOSE(11)
c
      STOP
      END
C
C *** FOURIER TRANSFORM OR INVERSE FOURIER TRANSFORM
C
      SUBROUTINE WVFAST(N,A,MOM,IND)
C
C *** N   : NUMBER OF FOURIER COEFF. OR WAVE ( = 2**I )
C *** A(I): FOURIER COEFFICIENTS ( N/2+1 ) OR WAVE ( REAL PART )
C    ( FOURIER SPECTRUM = FOURIER COEFFICIENTS * DURATION/2.0 )
C *** MOM : N IN MAIN DIMMENSION
C         : IF MWV > 2**13(=8192), CAN NOT USE !!!
C *** IND : = -1 : TRANS.,   = 1 : INVERSE TRANS.
C
      COMPLEX*8 A(MOM),TEMP,THETA
C
C *** CALUCURATION OF FFT
C
      PAI=3.1415926540
      J=1
      DO 140 I=1,N
        IF(I.GE.J) GO TO 110
        TEMP=A(J)
        A(J)=A(I)
        A(I)=TEMP
  110   M=N/2
  120   IF(J.LE.M) GO TO 130
        J=J-M
        M=M/2
        IF(M.GE.2) GO TO 120
  130   J=J+M
  140 CONTINUE
      KMAX=1
  150 IF(KMAX.GE.N) RETURN
      ISTEP=KMAX*2
      DO 170 K=1,KMAX
        THETA=CMPLX(0.0,PAI*FLOAT(IND*(K-1))/FLOAT(KMAX))
        DO 160 I=K,N,ISTEP
          J=I+KMAX
          TEMP=A(J)*CEXP(THETA)
          A(J)=A(I)-TEMP
          A(I)=A(I)+TEMP
  160   CONTINUE
  170 CONTINUE
      KMAX=ISTEP
      GO TO 150
C
      END
