SUBROUTINE SUALDYNB

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

USE YOMCT0   , ONLY : NPRINTLEV,LNHDYN   ,LALLOPR
USE YOMLUN   , ONLY : NULOUT
USE YOMDIM   , ONLY : NFLEVL   ,NFLEVG   ,NSMAX    ,NF3D     ,NSPEC
USE YOMDYNA  , ONLY : LSLHD    ,LSLHD_STATIC ,LSLHD_W  ,LSLHD_SVD,&
 & LNHX     ,LNH_PDVD
USE YOMDYN   , ONLY : RDIVOR  ,RDIDIV   ,RDITG    ,RDIGFL   ,&
 & RDIPD    ,RDIVD    ,RDISP    ,GMR      ,RDHI     ,&
 & SCGMAP   ,SIHEG    ,SIHEG2   ,LSIDG    ,LSTRHD   ,&
 & SIHEGB   ,SIHEGB2  ,&
 & RDSVOR   ,RDSDIV   ,RDSVD    ,RDHS     ,SLHDA    ,SLHDD0
USE YOM_YGFL , ONLY : YGFL
USE YOMGEM   , ONLY : NGPTOT

!**** *SUALDYNB* - Routine to allocate space for dynamics:
!                  part called under SUDYN.

!     Purpose.
!     --------
!           Allocate space for the dynamics calc.

!**   Interface.
!     ----------
!        *CALL* *SUALDYNB*

!     Explicit arguments :  None
!     --------------------
!        Called by SUDYN

!     Implicit arguments :
!     --------------------

!     Method.
!     -------
!     Externals.
!     ----------

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

!     Author.
!     -------
!        Karim YESSAD

!     Modifications.
!     --------------
!   Original : 95-11-20  From SUALDYN
!   F. Vana and K. Yessad: modify diffusion when LSLHD=T.
!   F. Vana 15-Feb-2005: Split of the global LSLHD key.
!   K. Yessad (Dec 2005): Horizontal diffusion for stretched NH model.
!   F. Vana 08-Jun-2006 : Two new arrays for SLHD
!   K. Yessad 15-May-2006: memory optimisations for stretched geometry
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!   F. Vana 13-Jan-2009 : Recognized special case LSLHD_STATIC
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM) :: IU,IVTHS,IL

LOGICAL :: LLP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

IF (LHOOK) CALL DR_HOOK('SUALDYNB',0,ZHOOK_HANDLE)

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

!*       1.    ALLOCATE SPACE FOR ARRAYS.
!              --------------------------

LLP = NPRINTLEV >= 1.OR. LALLOPR
IU = NULOUT

! * Semi-implicit scheme for ARPEGE stretched geometry.
IF (LSIDG) THEN
  ALLOCATE(SCGMAP(NSPEC,3))
  IF(LLP)WRITE(IU,9) 'SCGMAP   ',SIZE(SCGMAP),SHAPE(SCGMAP)
  ALLOCATE(SIHEG(NFLEVG,NSPEC,3))
  IF(LLP)WRITE(IU,9) 'SIHEG    ',SIZE(SIHEG),SHAPE(SIHEG)
  ALLOCATE(SIHEG2(NFLEVG,NSMAX+1,2:3))
  IF(LLP)WRITE(IU,9) 'SIHEG2   ',SIZE(SIHEG2),SHAPE(SIHEG2)
  IF (LNHDYN.AND.LNH_PDVD) THEN
    ALLOCATE(SIHEGB(NFLEVG,NSPEC,3))
    IF(LLP)WRITE(IU,9) 'SIHEGB   ',SIZE(SIHEGB),SHAPE(SIHEGB)
    ALLOCATE(SIHEGB2(NFLEVG,NSMAX+1,2:3))
    IF(LLP)WRITE(IU,9) 'SIHEGB2  ',SIZE(SIHEGB2),SHAPE(SIHEGB2)
  ENDIF
ENDIF

! * Horizontal diffusion scheme.
IF (LSTRHD) THEN
  ALLOCATE(GMR   (NSPEC,2))
  IF(LLP)WRITE(IU,9) 'GMR      ',SIZE(GMR),SHAPE(GMR)
  ALLOCATE(RDHI  (NFLEVL*NF3D+1,NSPEC,3))
  IF(LLP)WRITE(IU,9) 'RDHI     ',SIZE(RDHI),SHAPE(RDHI)

  ! * Compute the number of prognostic variables on which the HDS
  !   diffusion is applied (currently IVTHS=(IL/NFLEVL) is between 0 and 4).
  !   Value of IVTHS must be the same in SUALDYNB and SUHDU.
  IVTHS=0
  IF ((.NOT.LSLHD_STATIC).AND.LSLHD_W) IVTHS=IVTHS+2
  IF ((.NOT.LSLHD_STATIC).AND.LNHDYN.AND.LSLHD_SVD) IVTHS=IVTHS+1
  IF ((.NOT.LSLHD_STATIC).AND.LNHX.AND.LSLHD_SVD) IVTHS=IVTHS+1
  IL=IVTHS*NFLEVL
  IF(IVTHS>0) THEN
    ALLOCATE(RDHS  (IL,NSPEC,3))
    IF(LLP)WRITE(IU,9) 'RDHS     ',SIZE(RDHS),SHAPE(RDHS)
  ENDIF
ENDIF
ALLOCATE(RDIVOR(NFLEVL,0:NSMAX))
IF(LLP)WRITE(IU,9) 'RDIVOR   ',SIZE(RDIVOR),SHAPE(RDIVOR)
ALLOCATE(RDIDIV(NFLEVL,0:NSMAX))
IF(LLP)WRITE(IU,9) 'RDIDIV   ',SIZE(RDIDIV),SHAPE(RDIDIV)
ALLOCATE(RDITG (NFLEVG,0:NSMAX))
IF(LLP)WRITE(IU,9) 'RDITG    ',SIZE(RDITG),SHAPE(RDITG)
ALLOCATE(RDIGFL(NFLEVL,0:NSMAX,YGFL%NUMSPFLDS))
IF(LLP)WRITE(IU,9) 'RDIGFL   ',SIZE(RDIGFL),SHAPE(RDIGFL)
ALLOCATE(RDISP (      0:NSMAX))
IF(LLP)WRITE(IU,9) 'RDISP    ',SIZE(RDISP),SHAPE(RDISP)
IF(LNHDYN) THEN
  ALLOCATE(RDIPD(NFLEVL,0:NSMAX))
  IF(LLP)WRITE(IU,9) 'RDIPD    ',SIZE(RDIPD),SHAPE(RDIPD)
  ALLOCATE(RDIVD(NFLEVL,0:NSMAX))
  IF(LLP)WRITE(IU,9) 'RDIVD    ',SIZE(RDIVD),SHAPE(RDIVD)
ENDIF
IF((.NOT.LSLHD_STATIC).AND.LSLHD_W) THEN
  ALLOCATE(RDSVOR(NFLEVL,0:NSMAX))
  IF(LLP)WRITE(IU,9) 'RDSVOR   ',SIZE(RDSVOR),SHAPE(RDSVOR)
  ALLOCATE(RDSDIV(NFLEVL,0:NSMAX))
  IF(LLP)WRITE(IU,9) 'RDSDIV   ',SIZE(RDSDIV),SHAPE(RDSDIV)
ENDIF
IF((.NOT.LSLHD_STATIC).AND.LSLHD_SVD.AND.LNHDYN) THEN
  ALLOCATE(RDSVD(NFLEVL,0:NSMAX))
  IF(LLP)WRITE(IU,9) 'RDSVD    ',SIZE(RDSVD),SHAPE(RDSVD)
ENDIF

! * SLHD scheme
IF((.NOT.LSLHD_STATIC).AND.LSLHD) THEN
  ALLOCATE(SLHDA(NGPTOT))
  ALLOCATE(SLHDD0(NGPTOT))
ENDIF

!     ------------------------------------------------------------------
9 FORMAT(1X,'ARRAY ',A10,' ALLOCATED ',8I8)

!     ------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('SUALDYNB',1,ZHOOK_HANDLE)
END SUBROUTINE SUALDYNB
