!********************************************************************
!*                   SUBROUTINE RCFFT2D
!********************************************************************
!*
!* Objet      : routine d'interface pour les fft R/C 2D de la lib IBM
!*
!*
!* Methode    :
!*
!* Langage    : Fortran90
!*
!* Auteur     : Guy Moebs
!*
!*
!* $Date$
!*
!********************************************************************
!*
!* $Source$
!*
!* $Log$
!*
!********************************************************************
!*
!* APPELES    :
!*
!********************************************************************
!*
!*
!* ENTREES         :  itrans, ndim, mdim
!*
!* ENTREES/SORTIES :  X, Y
!*
!* SORTIES         :
!*
!* CODE ERREUR     :
!*
!*
!********************************************************************
!*
      SUBROUTINE rcfft2d (itrans, ndim, mdim, X, Y)
!      
!--------------------
! modules necessaires
!--------------------
      use numerics
      use modfft
!
      IMPLICIT NONE
!
!--------------------------
! description des arguments
!--------------------------
!     * itrans (E ,I ) Entier definissant la transformation
!     * ndim   (E ,I ) Nombre de points de la transformantion
!     * mdim   (E ,I ) Nombre de points de la transformantion
!     * X      (ES,R ) Tableau des valeurs reelles
!     * Y      (ES,CX) Tableau des valeurs complexes
!
!--------------------------
! declaration des arguments
!--------------------------
      INTEGER,  INTENT(IN)  ::  itrans
      INTEGER,  INTENT(IN)  ::  ndim
      INTEGER,  INTENT(IN)  ::  mdim
!
      REAL(rp),     DIMENSION(0:ndim+1, 0:mdim-1),  INTENT(INOUT)  ::  X
      COMPLEX(rp),  DIMENSION(0:ndim/2, 0:mdim-1),  INTENT(INOUT)  ::  Y
!
!----------------------------------
! declaration des variables locales
!----------------------------------
      REAL(rp)  ::  scale
      REAL(rp)  ::  dummy = 0.0_rp
!
      INTEGER   ::  n, m, init, isign
      INTEGER   ::  inc2x, inc2y
      INTEGER   ::  ndummy = 0
!
!-----------------------
! debut de la subroutine
!-----------------------
!
      SELECT CASE (itrans)
!
!----------------------------------------------------
! Passage de l'espace spectral dans l'espace physique
!----------------------------------------------------
!
      CASE (-1)
           n = ndim
           m = mdim
!
           init = 0
!
           inc2x = ndim+2
!
           inc2y = ndim/2+1
!
           isign = -1
           scale = 1.0_rp / ( REAL(n, rp) * REAL(m, rp) )
!
           CALL DRCFT2 ( init, X, inc2x, Y, inc2y, n, m, isign, &
                         scale, AUX1m, naux1, AUX2, naux2, dummy, ndummy )
!
!----------------------------------------------------
! Passage de l'espace physique dans l'espace spectral
!----------------------------------------------------
!
      CASE ( 1)
           n = ndim
           m = mdim
!
           init = 0
!
           inc2x = ndim+2
!
           inc2y = ndim/2+1
!
           isign = 1
           scale = 1.0_rp
!
           CALL DCRFT2 ( init, Y, inc2y, X, inc2x, n, m, isign, &
                         scale, AUX1p, naux1, AUX2, naux2, dummy, ndummy )
!
!--------------------------
! Initialisations des TRIGS
!--------------------------
!
      CASE (0)
           n = ndim
           m = mdim
!
           init = 1
!
           inc2x = ndim+2
!
           inc2y = ndim/2+1
!
           IF ( MAX(ndim/2,mdim) <= 2048 ) THEN
              naux1 = 42000
           ELSE
              naux1 = 40000 + INT( 1.64_rp * ndim + 2.28_rp * mdim ) + 1
           END IF
!
           IF ( mdim < 252 ) THEN
!
              IF ( ndim <= 4096 ) THEN
                 naux2 = 20000
              ELSE
                 naux2 = 20000 + INT( 1.14_rp * ndim ) + 1
              END IF
!
           ELSE
!
              IF ( ndim <= 4096 ) THEN
                 naux2 = 20000 + INT( (mdim+256)*(2.28_rp+MIN(64,1+ndim/2)) ) + 1
              ELSE 
                 naux2 = 20000 + INT( (mdim+256)*(2.28_rp+MIN(64,1+ndim/2)) + 1.14_rp * ndim ) + 1
              END IF
!

           END IF
!
           ALLOCATE( AUX1p(naux1), AUX1m(naux1), AUX2(naux2), STAT = ierr)
           IF( ierr /= 0 ) THEN
              WRITE (6,*) ' Probleme allocation AUX1p, AUX1m, AUX2'
              STOP
           END IF
!
           isign = 1
           scale = 1.0_rp
           CALL DCRFT2 ( init, Y, inc2y, X, inc2x, n, m, isign, &
                         scale, AUX1p, naux1, AUX2, naux2, dummy, ndummy )
!
           isign = -1
           scale = 1.0_rp / ( REAL(n, rp) * REAL(m, rp) )
           CALL DRCFT2 ( init, X, inc2x, Y, inc2y, n, m, isign, &
                         scale, AUX1m, naux1, AUX2, naux2, dummy, ndummy )
!
!-----------------------
! Finalisation des TRIGS
!-----------------------
!
      CASE (-2)
           DEALLOCATE( AUX1p, AUX1m, AUX2 )
!
!-------
! Erreur
!-------
!
      CASE DEFAULT
           WRITE (6,'(A)') 'ERREUR : PARAMETRE itrans INCONNU'
           STOP
!
      END SELECT
!
!---------------------
! Fin de la subroutine
!---------------------
!
      RETURN
      END SUBROUTINE rcfft2d
