C
      PROGRAM tran1x
C
      PARAMETER(MWV=33000)
      IMPLICIT REAL*8(A-H,O-Z)
      CHARACTER INFILE*30,head1*20,head2*20,head3*20,head4*20
      dimension time(MWV)
      dimension XR1(MWV),XR2(MWV),XR3(MWV),X1(MWV),X2(MWV),X3(MWV)
      COMPLEX*16 FX1(MWV),FX2(MWV),FX3(MWV)
C
C  Prompt for input RECON'ed file
C
      WRITE(*,1010) 
 1010 FORMAT(1X,/'Enter input file of waveform data')
      READ(*,1020) INFILE
 1020 FORMAT(A30)
C      
C *** Read input data file  ***
C
      OPEN(11,FILE=INFILE)
        read(11,*)head1,head2,head3,head4
        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(*,*)
      write(*,*)'NT and DT=',NT,DT
c
C *** change of + or -  ***
C
      WRITE(*,*)
      WRITE(*,*)' Change +/-? (YES=1)?? :'
      READ(*,*)MYES
C 
      IF(MYES.EQ.1)THEN
C
        WRITE(*,*)
        WRITE(*,*)' Change from + to - of FILE 1 ? (YES=1)?? :'
        READ(*,*)MY1
C
         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(*,*)MY2
C
         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(*,*)MY3
C
         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,15
        NF=2*NF
        IF(NF.GE.NT)GOTO 30
  200 CONTINUE
      WRITE(*,*)' NT>2**15 !!!!!'
      STOP
   30 IF1=I
      WRITE(*,*)
      WRITE(*,*)' IF1,NF=',IF1,NF
C
      DO 300 I=1,NT
        FX1(I)=dCMPLX(XR1(I),0.d0)
        FX2(I)=dCMPLX(XR2(I),0.d0)
        FX3(I)=dCMPLX(XR3(I),0.d0)
  300 CONTINUE
      DO 310 I=NT+1,NF
        FX1(I)=(0.d0,0.d0)
        FX2(I)=(0.d0,0.d0)
        FX3(I)=(0.d0,0.d0)
  310 CONTINUE
C
      CALL WVFAST(NF,FX1,MWV,-1)
      CALL WVFAST(NF,FX2,MWV,-1)
      CALL WVFAST(NF,FX3,MWV,-1)
      DUR=dFLOAT(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.d0
      DOM=6.2831853070d0/DUR
      NOM=NF/2+1
      EOM=DOM*NOM
      WRITE(*,*)
      WRITE(*,*)' DUR,DOM,NOM,EOM=',DUR,DOM,NOM,EOM
      WRITE(*,*)' Highest Frequency (hz)=',EOM/6.2831853070d0
C     
C *** BAND PATH ***
C
      WRITE(*,*)
      WRITE(*,*)' Band Pass Filter? (YES=1)?? :'
      READ(*,*)NBD
C
      IF(NBD.EQ.1)THEN
C
        WRITE(*,*)
        WRITE(*,*)'  TRAPEZOIDAL BAND PATH FILTER'
        WRITE(*,*)'  ENTER fL1<fL2<fH1<fH2 (Hz)'
        READ(*,*)fL1,fL2,fH1,fH2
        OML1=fL1*6.2831853070d0
        OML2=fL2*6.2831853070d0
        OMH1=fH1*6.2831853070d0
        OMH2=fH2*6.2831853070d0
C
        DO 500 I = 1,NOM
           OMEGA=dFLOAT(I-1)*DOM
           IF(OMEGA.LE.OML1)then
             FX1(I)=(0.d0,0.d0)
             FX2(I)=(0.d0,0.d0)
             FX3(I)=(0.d0,0.d0)
           end if
           IF(OMEGA.GE.OMH2)then
             FX1(I)=(0.d0,0.d0)
             FX2(I)=(0.d0,0.d0)
             FX3(I)=(0.d0,0.d0)
           end if
           AA=1.d0
           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/Differentiation:'
      WRITE(*,*)' NO Change         :                 (Enter=0)'
      WRITE(*,*)' Integration       : Ex.) acc -> vel (Enter=1)'
      WRITE(*,*)' Double Integration: Ex.) acc -> dis (Enter=2)'
      WRITE(*,*)' Differentiation:  : Ex.) Vel -> acc (Enter=3)'
      READ(*,*)NIT
c
      IF(NIT.EQ.1)THEN
        DO 550 I = 2,NOM
           OMEGA=dFLOAT(I-1)*DOM
	   FX1(I) = FX1(I)/dcmplx(0.d0,omega)
	   FX2(I) = FX2(I)/dcmplx(0.d0,omega)
	   FX3(I) = FX3(I)/dcmplx(0.d0,omega)
  550   CONTINUE
      END IF
c
      IF(NIT.EQ.2)THEN
        DO 560 I = 2,NOM
           OMEGA=dFLOAT(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=dFLOAT(I-1)*DOM
	   FX1(I) = FX1(I)*dcmplx(0.d0,omega)
	   FX2(I) = FX2(I)*dcmplx(0.d0,omega)
	   FX3(I) = FX3(I)*dcmplx(0.d0,omega)
  570   CONTINUE
      END IF
C
c * Output Fourier Spectrum data *
c
      write(*,*)
      write(*,*)'Fourier Spectrum data are stored in "amp3.csv"'
      open(10,file='amp3.csv',status='unknown')
      write(10,*)'frequency(Hz),',head2,',',head3,',',head4
      DO 680 I = 2,NOM
        f=dFLOAT(I-1)*DOM/6.2831853070d0
        write(10,1900)f,cdabs(FX1(I)),cdabs(FX2(I)),cdabs(FX3(I))
  680 CONTINUE
 1900 format(f8.4,6(',',e12.4))
      close(10)
C
C *** INVERSE FOURIER TRANSFORM  ***
C
      DO 600 I=2,NF/2
           FX1(NF+2-I)=dCMPLX(dREAL(FX1(I)),-dIMAG(FX1(I)))
           FX2(NF+2-I)=dCMPLX(dREAL(FX2(I)),-dIMAG(FX2(I)))
           FX3(NF+2-I)=dCMPLX(dREAL(FX3(I)),-dIMAG(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)=dREAL(FX1(I))/DUR
         XR2(I)=dREAL(FX2(I))/DUR
         XR3(I)=dREAL(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(*,*)'Cutting data? (No=0,Yes=1)'
      read(*,*)icut
      if(icut.eq.1)then
        write(*,*)'Enter Starting Time (sec)'
        read(*,*)stime
        nstime=int(stime/dt)
        write(*,*)'Number of Cutting Data =',nstime
        write(*,*)'Enter Duration (sec)'
        read(*,*)dur
        ndur=int(dur/dt)+1
        write(*,*)'Number of data =',ndur
        do i=1,ndur
          if((i+nstime).le.0)then
            x1(i)=0.d0
            x2(i)=0.d0
            x3(i)=0.d0
	  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(*,2010) 
 2010 FORMAT(/1X,'Enter output file name of waveform (csv format file)')
      READ(*,2020) INFILE
 2020 FORMAT(A30)
      OPEN(11,FILE=INFILE,STATUS='unknown')
      write(11,*)'time(s),',head2,',',head3,',',head4
      do i=1,nt,iskip
      write(11,2000)dt*(i-1),X1(I),x2(i),x3(i)
c      write(11,2000)dt*(i-1),X1(I)-X1(1),x2(i)-X2(1),x3(i)-x3(1)
      end do
 2000 format(f9.3,4(',',f12.4))
      CLOSE(11)
      read(*,*)
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
      IMPLICIT REAL*8(A-H,O-Z)
      COMPLEX*16 A(MOM),TEMP,THETA
C
C *** CALUCURATION OF FFT
C
      PAI=3.1415926540d0
      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=dCMPLX(0.d0,PAI*dFLOAT(IND*(K-1))/dFLOAT(KMAX))
        DO 160 I=K,N,ISTEP
          J=I+KMAX
          TEMP=A(J)*CdEXP(THETA)
          A(J)=A(I)-TEMP
          A(I)=A(I)+TEMP
  160   CONTINUE
  170 CONTINUE
      KMAX=ISTEP
      GO TO 150
C
      END
