MODULE EFTDIR_MOD
CONTAINS
SUBROUTINE EFTDIR(ALLOCATOR,HFTDIR,PREEL,PREEL_COMPLEX,KF_FS,AUX_PROC)

USE PARKIND1  ,ONLY : JPIM, JPRB, JPIB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK, JPHOOK

USE TPM_GEN,                ONLY: NCUR_RESOL
USE TPM_DISTR       ,ONLY : D
USE TPM_DIM         ,ONLY : R
USE TPMALD_DIM      ,ONLY : RALD

USE TPM_HICFFT      ,ONLY : EXECUTE_DIR_FFT
USE FTDIR_MOD, ONLY : FTDIR_HANDLE

USE ABORT_TRANS_MOD ,ONLY : ABORT_TRANS

USE ISO_C_BINDING
USE BUFFERED_ALLOCATOR_MOD

!

IMPLICIT NONE

TYPE(BUFFERED_ALLOCATOR), INTENT(IN) :: ALLOCATOR
TYPE(FTDIR_HANDLE) :: HFTDIR
INTEGER(KIND=JPIM), INTENT(IN)  :: KF_FS
REAL(KIND=JPRB),    INTENT(INOUT), POINTER  :: PREEL(:)   ! (IRLEN+2)*NDGLG*KF_FS
REAL(KIND=JPRB),    INTENT(OUT), POINTER  :: PREEL_COMPLEX(:)
EXTERNAL AUX_PROC
OPTIONAL AUX_PROC

INTEGER(KIND=JPIM) :: JLOT, IRLEN

REAL(KIND=JPRB) :: ZDUM
INTEGER(KIND=JPIM) :: INUL

REAL(KIND=JPRB), POINTER, SAVE :: ZREEL(:), ZREEL_COMPLEX(:)

REAL(KIND=JPHOOK) :: ZHOOK_HANDLE

!     ------------------------------------------------------------------

!*       1.       PERFORM LEGENDRE TRANFORM.
!                 --------------------------

IF (LHOOK) CALL DR_HOOK('ELEINV_MOD:ELEINV',0,ZHOOK_HANDLE)

IRLEN=R%NDLON+R%NNOEXTZG


#ifdef IN_PLACE_FFT
    PREEL_COMPLEX => PREEL
#else
    CALL ASSIGN_PTR(PREEL_COMPLEX, GET_ALLOCATION(ALLOCATOR, HFTDIR%HREEL_COMPLEX),&
      & 1_JPIB, 1_JPIB*KF_FS*D%NLENGTF*C_SIZEOF(PREEL_COMPLEX(1)))
#endif



! Periodization of auxiliary fields in x direction
IF(R%NNOEXTZL>0) THEN
  !!! FIXME !!! CALL EXTPER(PREEL,R%NDLON+R%NNOEXTZL,1,R%NDLON,KF_FS,D%NDGL_FS,D%NSTAGTF,0)
  CALL ABORT('FIXME')
ENDIF
IF (PRESENT(AUX_PROC)) THEN
  !!! FIXME !!! CALL AUX_PROC(PREEL,ZDUM,KF_FS,D%NLENGTF,1,D%NDGL_FS,0,.TRUE.,&
  !!! FIXME !!!  & D%NSTAGTF,INUL,INUL,INUL)
  CALL ABORT('FIXME')
ENDIF


JLOT=SIZE(PREEL)/(IRLEN+2)

IF (JLOT==0) THEN
  IF (LHOOK) CALL DR_HOOK('ELEINV_MOD:ELEINV',1,ZHOOK_HANDLE)
  RETURN
ENDIF

!$ACC DATA PRESENT(PREEL,RALD%NLOENS_LON,RALD%NOFFSETS_LON)

CALL EXECUTE_DIR_FFT(PREEL,PREEL_COMPLEX,NCUR_RESOL,JLOT, &
    & LOENS=RALD%NLOENS_LON, &
    & OFFSETS=RALD%NOFFSETS_LON,ALLOC=ALLOCATOR%PTR)

!$ACC END DATA


IF (LHOOK) CALL DR_HOOK('EFTDIR_MOD:EFTDIR',1,ZHOOK_HANDLE)

END SUBROUTINE EFTDIR
END MODULE EFTDIR_MOD