!********************************************************************
!*                   PROGRAM CCMAIN2D
!********************************************************************
!*
!* Objet      : test les fft C/C 2D de la lib SGI
!*
!*
!* Methode    :
!* 
!* Langage    : Fortran90
!*   
!* Auteur     : $Author$  
!*               
!*
!* $Date$
!*
!********************************************************************
!*
!* $Source$
!*
!* $Log$
!*
!********************************************************************
!*
!* APPELES    :
!*
!********************************************************************
!*
!*
!* ENTREES    :
!*
!* ENTREES/SORTIES :
!*
!* SORTIES    :
!*
!* CODE ERREUR :
!*
!*
!********************************************************************
!*
      PROGRAM CCMAIN2D
!*
      USE numerics
      USE modfft
!*
      IMPLICIT NONE
!*
      COMPLEX(rp),  DIMENSION(:,:),  ALLOCATABLE  ::  U
      COMPLEX(rp),  DIMENSION(:,:),  ALLOCATABLE  ::  V
!*
!*
      INTEGER  ::  i, j
      INTEGER  ::  n, m
!*
!*
      REAL     ::  erreur
      REAL     ::  usn, usm
      REAL     ::  PI2n, PI2m
!*
      INTERFACE
!*
      SUBROUTINE ccfft2d (itrans, ndim, mdim, A, B)
      use numerics
      use modfft
      IMPLICIT NONE
      INTEGER,  INTENT(IN)  ::  itrans
      INTEGER,  INTENT(IN)  ::  ndim, mdim
      COMPLEX(rp),  DIMENSION(0:ndim, 0:mdim-1),  INTENT(IN)   ::  A
      COMPLEX(rp),  DIMENSION(0:ndim, 0:mdim-1),  INTENT(OUT)  ::  B
      END SUBROUTINE ccfft2d
!*
      END INTERFACE
!*
!********************************************************************
!*
!* DEBUT DU PROGRAMME
!*
!********************************************************************
!*
!* Lecture des dimensions
      OPEN (19, file = 'FFT2D.DAT', STATUS = 'OLD', FORM = 'FORMATTED')
      READ (19, *) n
      READ (19, *) m
      CLOSE (19)
!*
      ALLOCATE ( V  (0:n, 0:m-1), U (0:n, 0:m-1), STAT = ierr)
      IF (ierr /= 0) THEN
         WRITE (6,*) ' Probleme allocation U, V'
         STOP
      END IF
!*
      usn  = un / REAL (n)
      usm  = un / REAL (m)
!*
!*
      PI2n = 2.0_rp * ACOS (-un) * usn
      PI2m = 2.0_rp * ACOS (-un) * usm
!*
!* Initialisation et sauvegarde du signal
      DO j = 0, m - 1
         DO i = 0, n - 1
            U(i,j) = SIN( PI2n * REAL(i,rp) ) * COS( PI2m * REAL(j,rp) )
            V(i,j) = U(i,j)
         END DO
      END DO
!*
!* Initialisation des trigs
      CALL ccfft2d(  0, n, m, U, U )
!*
!* FFT directe
      CALL ccfft2d( -1, n, m, U, U )
!*
!* Affichage
      DO j = 0, m - 1
         write (6,*) 'j = ', j
         DO i = 0, n/2 - 1
            write (6, 1010) i, V(i,j), i,   U(i,j)
         END DO
         DO i = n/2, n - 1
            write (6, 1010) i, V(i,j), i-n, U(i,j)
         END DO
         write (6,*) ' '
      END DO
 1010 FORMAT ( 2(i4, 2(E12.4) ) )
!*
!* FFT inverse
       CALL ccfft2d(  1, n, m, U, U )
!*
!* Comparaison
      erreur = zero
      DO j = 0, m - 1
         DO i = 0, n - 1
            erreur = MAX( erreur, ABS( U(i,j) - V(i,j) ) )
         END DO
      END DO
!*
 1020 FORMAT( A, E11.4 )
      WRITE (6, 1020) 'Erreur Loo = ', erreur
!*
!*
      CALL ccfft2d( -2, n, m, U, U )
      DEALLOCATE( U, V )
!*
!*
      STOP
      END PROGRAM CCMAIN2D
