MODULE INTRF
  IMPLICIT NONE
  INTEGER, PARAMETER :: LONG = SELECTED_REAL_KIND(9)
  INTEGER, PARAMETER :: IO = 10
  INTEGER IC, N
  REAL(LONG) F0
  REAL(LONG), PARAMETER :: PI = 4.0_LONG * ATAN (1.0_LONG )
  REAL(LONG), DIMENSION(:), ALLOCATABLE :: FR, FI, GR, GI, FRC, FIC
  CHARACTER(LEN=100) FILES
  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ! Make explicit interfaces for external procedures
  INTERFACE 
    SUBROUTINE DFT(FR,FI,GR,GI,N)
      INTEGER N
      INTEGER, PARAMETER :: LONG = SELECTED_REAL_KIND(9)
      REAL(LONG) PI
      REAL(LONG), DIMENSION(:) :: FR, FI, GR, GI
    END SUBROUTINE DFT
  END INTERFACE
  !+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
END MODULE INTRF
!=======================================================================
PROGRAM DFT_TEST
   USE INTRF
!=======================================================================
! Read data
! 
   PRINT*, 'Please, type number of data'
   READ*, N
   ALLOCATE ( FR(N), FI(N), GR(N), GI(N), FRC(N), FIC(N))
   PRINT*, 'Please, type in name of data file (2 col)'
   READ*, FILES
   OPEN(IO,FILE=TRIM(FILES))
   IC = 0
10 IC = IC + 1 
   READ(IO,*,END=20) FR(IC), FI(IC)
   FRC(IC) = FR(IC)
   FIC(IC) = FI(IC)
   GO TO 10
20 N = IC - 1  
   F0 = 1.0 / SQRT(REAL(N,LONG)) 
!=======================================================================
! Perform DFT
!   
   CALL DFT(FR,FI,GR,GI,N)
!=======================================================================
! Perform inverse DFT
!   
   DO I=1, N
      GI(I) = -GI(I)
   END DO 
   CALL DFT(GR,GI,FR,FI,N)
   DO I=1, N
      FI(I) = -FI(I)
   END DO 
   PRINT*, '        Original data               back-transformed data'
   PRINT*, '-----------------------------------------------------------'
   DO I=1,N
       PRINT '(2(F14.9,X),2X,2(F14.9,X))', FRC(I),FIC(I),FR(I),FI(I)
   END DO
!=======================================================================
END PROGRAM DFT_TEST
!
!***********************************************************************
!
SUBROUTINE DFT(FR,FI,GR,GI,N)
   IMPLICIT NONE
   INTEGER N,I,J
   INTEGER, PARAMETER :: LONG = SELECTED_REAL_KIND(9)
   REAL(LONG), PARAMETER :: PI = 4.0_LONG * ATAN (1.0_LONG )
   REAL(LONG), DIMENSION(:) :: FR, FI, GR, GI
   REAL(LONG) X,Q,F0

   X  = 2. * PI / REAL(N,LONG)
   F0 = 1.0 / SQRT(REAL(N,LONG)) 
   DO I=1, N
      GR(I) = 0.0
      GI(I) = 0.0
      DO J=1,N
         Q = X * (J-1) * (I-1)
         GR(I) = GR(I) + FR(J)*COS(Q) + FI(J)*SIN(Q)
         GI(I) = GI(I) + FI(J)*COS(Q) - FR(J)*SIN(Q)
      END DO
      GR(I) = F0 * GR(I)
      GI(I) = F0 * GI(I)
   END DO
   
END SUBROUTINE DFT
