SUBROUTINE TRANSDIR_MDL(CDCONF)

!**** *TRANSDIR_MDL * - Direct transforms for model

!     Purpose.  Perform direct transform (gridpoint to spectral)
!     --------

!**   Interface.  CALL TRANSDIR_MDL(...)
!     ---------- 

!     Explicit arguments : 
!     --------------------
!        CDCONF - configuration of work

!        Called by TRANSDIRH

!     Externals.
!     ----------
!     DIR_TRANS - inverse transform (TRANS library)

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        Mats Hamrud *ECMWF*

!     Modifications.
!     --------------
!        Original : 00-10-25
!        Modified : 03-08-01 M.Hamrud - GFL introduction
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        C.Temperton 04-01-29: Fix for SWE (is there a cleaner way?)

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

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

USE YOMDIM   , ONLY : NFTHER ,NSPEC2 , NPROMA
USE YOMMP    , ONLY : NBSETLEV,NBSETSP,MYSETV
USE YOMSP    , ONLY : SPVOR    ,SPDIV    ,SPHV     ,SPSP ,SPGFL ,  SPA3
USE YOMGFL   , ONLY : GFLT1
USE YOM_YGFL , ONLY : YGFL
USE YOMGMV   , ONLY : GMVT1,GMVT1S,YT1

IMPLICIT NONE

CHARACTER(LEN=1)                 :: CDCONF ! Argument NOT used
REAL(KIND=JPRB)    :: ZSP(1,NSPEC2)
INTEGER(KIND=JPIM) :: IVSETSC(1)
INTEGER(KIND=JPIM) :: IST,IOFF,IDIM3
LOGICAL :: LLOK, LLG
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTERFACE
#include "dir_trans.h"
END INTERFACE

#include "abor1.intfb.h"

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

IF (LHOOK) CALL DR_HOOK('TRANSDIR_MDL',0,ZHOOK_HANDLE)
LLOK = CDCONF == 'A'.OR.CDCONF == 'G'
IF(.NOT. LLOK)THEN
  CALL ABOR1('TRANSDIR_MDL - UNKNOWN CONFIGURATION ')
ENDIF
LLG = CDCONF == 'G'

IVSETSC(1) = NBSETSP
IF(NBSETSP == MYSETV) THEN
  IST = 1
ELSE
  IST = 0
ENDIF

IF(YGFL%NUMSPFLDS1 > 0) THEN
  IOFF  = YGFL%NUMFLDS1-YGFL%NUMSPFLDS1
  IDIM3 = YGFL%NUMSPFLDS1
ENDIF

IF(LLG) THEN
! Configuration where grid-point VOR,DIV transformed to spectral (for Jb calc.)
  IF(YGFL%NUMSPFLDS1>0) THEN
    CALL DIR_TRANS(PSPSC2=ZSP(1:IST,:),PSPSC3A=SPA3(:,:,1:2+NFTHER),&
     & PSPSC3B=SPGFL, &
     & KPROMA=NPROMA,KVSETSC2=IVSETSC(1:1),&
     & KVSETSC3A=NBSETLEV,KVSETSC3B=NBSETLEV,&
     & PGP2=GMVT1S(:,1:YT1%NDIMS,:),&
     & PGP3A=GMVT1(:,:,1:YT1%NDIM,:),PGP3B=GFLT1(:,:,IOFF+1:IOFF+IDIM3,:))  
  ELSE
    CALL DIR_TRANS(PSPSC2=ZSP(1:IST,:),PSPSC3A=SPA3(:,:,1:2+NFTHER),&
     & KPROMA=NPROMA,KVSETSC2=IVSETSC(1:1),&
     & KVSETSC3A=NBSETLEV,&
     & PGP2=GMVT1S(:,1:YT1%NDIMS,:),&
     & PGP3A=GMVT1(:,:,1:YT1%NDIM,:))  
  ENDIF
ELSEIF (YT1%NDIM>2) THEN
! Normal configuration (u,v grid-point to vor,div spectral)
  IF(YGFL%NUMSPFLDS1>0) THEN
    CALL DIR_TRANS(PSPVOR=SPVOR,PSPDIV=SPDIV,PSPSC2=ZSP(1:IST,:),PSPSC3A=SPHV,&
     & PSPSC3B=SPGFL, &
     & KPROMA=NPROMA,KVSETUV=NBSETLEV,KVSETSC2=IVSETSC(1:1),&
     & KVSETSC3A=NBSETLEV,KVSETSC3B=NBSETLEV,&
     & PGPUV=GMVT1(:,:,1:2,:),PGP2=GMVT1S(:,1:YT1%NDIMS,:),&
     & PGP3A=GMVT1(:,:,3:YT1%NDIM,:),PGP3B=GFLT1(:,:,IOFF+1:IOFF+IDIM3,:))  
  ELSE
    CALL DIR_TRANS(PSPVOR=SPVOR,PSPDIV=SPDIV,PSPSC2=ZSP(1:IST,:),PSPSC3A=SPHV,&
     & KPROMA=NPROMA,KVSETUV=NBSETLEV,KVSETSC2=IVSETSC(1:1),&
     & KVSETSC3A=NBSETLEV,&
     & PGPUV=GMVT1(:,:,1:2,:),PGP2=GMVT1S(:,1:YT1%NDIMS,:),&
     & PGP3A=GMVT1(:,:,3:YT1%NDIM,:))  
  ENDIF
ELSE
! Normal configuration (u,v grid-point to vor,div spectral)
  IF(YGFL%NUMSPFLDS1>0) THEN
    CALL DIR_TRANS(PSPVOR=SPVOR,PSPDIV=SPDIV,PSPSC2=ZSP(1:IST,:),&
     & PSPSC3B=SPGFL, &
     & KPROMA=NPROMA,KVSETUV=NBSETLEV,KVSETSC2=IVSETSC(1:1),&
     & KVSETSC3B=NBSETLEV,&
     & PGPUV=GMVT1(:,:,1:2,:),PGP2=GMVT1S(:,1:YT1%NDIMS,:),&
     & PGP3A=GMVT1(:,:,3:YT1%NDIM,:),PGP3B=GFLT1(:,:,IOFF+1:IOFF+IDIM3,:))  
  ELSE
    CALL DIR_TRANS(PSPVOR=SPVOR,PSPDIV=SPDIV,PSPSC2=ZSP(1:IST,:),&
     & KPROMA=NPROMA,KVSETUV=NBSETLEV,KVSETSC2=IVSETSC(1:1),&
     & PGPUV=GMVT1(:,:,1:2,:),PGP2=GMVT1S(:,1:YT1%NDIMS,:))
  ENDIF
ENDIF

CALL GSTATS(1036,0)
IF(NBSETSP == MYSETV) THEN
  SPSP(:) = ZSP(IST,:)
ENDIF
CALL GSTATS(1036,1)
IF (LHOOK) CALL DR_HOOK('TRANSDIR_MDL',1,ZHOOK_HANDLE)

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

END SUBROUTINE TRANSDIR_MDL
