C
      PROGRAM tran
C
      PARAMETER(MWV=33000)
      CHARACTER INFILE*20
      REAL time(MWV)
      REAL XR1(MWV),XR2(MWV),XR3(MWV),X1(MWV),X2(MWV),X3(MWV)
      COMPLEX*8 FX1(MWV),FX2(MWV),FX3(MWV)
C
      WRITE(*,*)
      WRITE(*,*) 'FFT and Band-Pass Filter for Taiwan Data'
      WRITE(*,*) 'Read from "***.acc" '
      WRITE(*,*) '------------------------------------------------------
     +-----------'
      WRITE(*,*)
C
C  Prompt for input RECON'ed file
C
      WRITE(*,1010) 
 1010 FORMAT(1X,'Enter name "***.acc')
      READ(*,1020) INFILE
 1020 FORMAT(A20)
C      
C *** Read input data file  ***
C
      OPEN(11,FILE=INFILE)
        i=1
   10   read(11,*,end=11)time(i),xr1(i),xr2(i),xr3(i)
        i=i+1
        goto 10
   11 CLOSE(11)
c
      NT=i-1
      dt=time(2)-time(1)
      write(*,*)'NT and DT=',NT,DT
C
C *** FOURIER TRANSFORM ***
C
      NF=2
      DO 200 I=2,16
        NF=2*NF
        IF(NF.GE.NT)GOTO 30
  200 CONTINUE
      WRITE(*,*)' NT>2**16 !!!!!'
      STOP
   30 IF1=I
      WRITE(*,*)
      WRITE(*,*)' IF,NF=',IF1,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
c      FOM=0.0
      DOM=6.2831853070e0/DUR
      NOM=NF/2+1
      EOM=DOM*NOM
      WRITE(*,*)
      WRITE(*,*)' DUR,DOM,NOM,EOM=',DUR,DOM,NOM,EOM
C
C *** BAND PATH ***
C
      WRITE(*,*)
      WRITE(*,*)' BAND PASS? (YES=1)?? :'
      READ(*,*)NBD
C
      IF(NBD.EQ.1)THEN
C
        WRITE(*,*)
        WRITE(*,*)'  TRAPEZOIDAL BAND PATH FILTER'
c        WRITE(*,*)'  INITIAL AND FINAL OM=',DOM*IOM,DOM*(NDATA-1)
        WRITE(*,*)'  ENTER OML1<OML2<OMH1<OMH2'
        READ(*,*)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(*,*)' Integ.(=1), Double Integ.(=2) or Diff.(=3) ?:'
      READ(*,*)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.2)THEN
        DO 560 I = 2,NOM
           OMEGA=FLOAT(I-1)*DOM
	   FX1(I) = FX1(I)/(-omega*omega)
	   FX2(I) = FX2(I)/(-omega*omega)
	   FX3(I) = FX3(I)/(-omega*omega)
  560   CONTINUE
      END IF
c
      IF(NIT.EQ.3)THEN
        DO 570 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)
 570   CONTINUE
      END IF
c
c * Output Fourier Spectrum data *
c
      write(*,*)
      write(*,*)'The Fourier Spectrum data are stored in "amp_dat"'
      open(10,file='amp_dat',status='unknown')
      DO 680 I = 2,NOM
        om=FLOAT(I-1)*DOM
        fr=om/6.283185307
        write(10,1900)om,fr,cabs(FX1(I)),cabs(FX2(I)),cabs(FX3(I))
  680 CONTINUE
 1900 format(f8.2,f8.3,3e12.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
         time(I) = FLOAT(I-1)*DT
         XR1(I)=REAL(FX1(I))/DUR
         XR2(I)=REAL(FX2(I))/DUR
         XR3(I)=REAL(FX3(I))/DUR
  700 CONTINUE
c
c *** Cut ***
c
      do i=1,nt
        x1(i)=xr1(i)
        x2(i)=xr2(i)
        x3(i)=xr3(i)
      end do

      write(*,*)
      write(*,*)'Cut data? (No=0,Yes=1)'
      read(*,*)icut
      if(icut.eq.1)then
        write(*,*)'Enter Starting Time (sec)'
        read(*,*)stime
        nstime=stime/dt
        write(*,*)'Number of Cutting Data =',nstime
        write(*,*)'Enter Duration (sec)'
        read(*,*)dur
        ndur=dur/dt+1
        write(*,*)'Number of data =',ndur
        do i=1,ndur
          if((i+nstime).le.0)then
            x1(i)=0.0
            x2(i)=0.0
            x3(i)=0.0
	  else
            x1(i)=xr1(i+nstime)
            x2(i)=xr2(i+nstime)
            x3(i)=xr3(i+nstime)
          end if
        end do
	nt=ndur
        write(*,*)'New nt =',nt
      end if
C
c *** output ***
c
      write(*,*)
      write(*,*)'Skip Data (no=0; yes=skip number)?'
      read(*,*)iskip
      iskip=iskip+1
      WRITE(*,*)'wave2_dat:' 
      OPEN(11,FILE='wave2_dat',STATUS='unknown')
      do i=1,nt,iskip
      write(11,2000)dt*(i-1),X1(I)-x1(1),x2(i)-x2(1),x3(i)-x3(1)
      end do
 2000 format(4e13.5)
      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.1415926540e0
      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
