!********************************************************************
!*                   PROGRAM MAIN
!********************************************************************
!*
!* Objet      : test les fft R/C 2D de la lib SGI
!*
!*
!* Methode    :
!* 
!* Langage    : Fortran90
!*   
!* Auteur     : $Author$  
!*               
!*
!* $Date$
!*
!********************************************************************
!*
!* $Source$
!*
!* $Log$
!*
!********************************************************************
!*
!* APPELES    :
!*
!********************************************************************
!*
!*
!* ENTREES    :
!*
!* ENTREES/SORTIES :
!*
!* SORTIES    :
!*
!* CODE ERREUR :
!*
!*
!********************************************************************

      PROGRAM MAIN
!*
      USE numerics
      USE modfft
!*
      IMPLICIT NONE
!*
      REAL(rp),     DIMENSION(:,:),  ALLOCATABLE  ::  UR
      COMPLEX(rp),  DIMENSION(:,:),  ALLOCATABLE  ::  UC
!*
      REAL(rp),     DIMENSION(:,:),  ALLOCATABLE  ::  VR
!*
!*
      INTEGER  ::  i, j
      INTEGER  ::  n, m
!*
      REAL     ::  erreur
      REAL     ::  PI2n, PI2m
!*
!*
      INTERFACE
!*
      SUBROUTINE scfft2d (itrans, ndim, mdim, R, C)
      use numerics
      use modfft
      IMPLICIT NONE
      INTEGER,  INTENT(IN)  ::  itrans
      INTEGER,  INTENT(IN)  ::  ndim, mdim
      REAL(rp),     DIMENSION(0:ndim,   0:mdim-1),  INTENT(INOUT)  ::  R
      COMPLEX(rp),  DIMENSION(0:ndim/2, 0:mdim-1),  INTENT(INOUT)  ::  C
      END SUBROUTINE scfft2d
!*
      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 ( UR(0:n, 0:m-1), UC(0:n/2, 0:m-1),VR (0:n, 0:m-1), &
                 STAT = ierr)
      IF (ierr /= 0) THEN
         WRITE (6,'(A)') ' Probleme allocation UR, UC, VR'
         STOP
      END IF
!*
!*
      PI2n = 2.0 * ACOS( -un ) / REAL (n, rp)
      PI2m = 2.0 * ACOS( -un ) / REAL (m, rp)
!*
!*
!* Initialisation et sauvegarde du signal
      DO j = 0, m - 1
         DO i = 0, n - 1
            UR(i,j) = SIN( PI2n * REAL(i,rp) ) * COS( PI2m * REAL(j,rp) )
            VR(i,j) = UR(i,j)
         END DO
      END DO
!*
!* Initialisation des trigs
      CALL scfft2d(  0, n, m, UR, UC )
!*
!* FFT directe
      CALL scfft2d(  1, n, m, UR, UC )
!*
!*
!* Affichage
      DO j = 0, m - 1
         write (6,*) 'j=', j
         DO i = 0, n/2
            write (6, 1010) i, VR(i,j), i, UC(i,j)
            UR(i,j) = zero
         END DO
         write (6,*) ' '
      END DO
 1010 FORMAT ( (i4, E12.4), (i4, 2(E12.4) ) )
!*
!*
!* FFT inverse
      CALL scfft2d( -1, n, m, UR, UC )
!*
!* Comparaison
      erreur = zero
      DO j = 0, m - 1
         DO i = 0, n - 1
            erreur = MAX( erreur, ABS( UR(i,j) - VR(i,j) ) )
            write (6,1030) UR(i,j), VR(i,j), ABS( UR(i,j) - VR(i,j) )
 1030 FORMAT ( 3E13.4 )
         END DO
      END DO
!*
 1020 FORMAT( A, E11.4 )
      WRITE (6, 1020) 'Erreur Loo = ', erreur
!*
!*
      CALL scfft2d( -2, n, m, UR, UC )
      DEALLOCATE( UR, UC, VR )
!*
!*
      STOP
      END PROGRAM MAIN
