SUBROUTINE GPGRXYBAD(&
 ! --- INPUT --------------------------------------
 & KPROMA,KD,KF,KFLEV,PVC,PVBH,&
 ! --- INPUT IN TL --------------------------------
 & PREL,PREM,PRDELP,PRPRE,PRPP,PLNPR,PALPH,PRTGR,&
 ! --- OUTPUT IN TL -------------------------------
 & PLNPRL,PLNPRM,PALPHL,PALPHM,PALPHPLL,PALPHPLM,&
 ! --- INPUT (TRAJECTORY) -------------------------
 & PCOEFD5,PCOEFAPL5,PCOEFA5, &
 & PRE5L,PRE5M,PRDELP5,PRPP5,PLNPR5,PALPH5,PRTGR5 &
 & )  

!**** *GPGRXYBAD* - AD of routine "GPGRXYB".
!                 Computation of the horizontal gradient of quantities
!                 "alpha" and "delta" on model layers.

!     Purpose.
!     --------

!      See GPGRXYB

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

!        Explicit arguments :
!        --------------------
!         * INPUT:
!           KPROMA       : horizontal dimension
!           KD           : start of work
!           KF           : working length
!           KFLEV        : number of levels
!           PVC          : A[lbar]*B[lbar-1] - A[lbar-1]*B[lbar]
!           PVBH         : B[lbar] on interlayers

!         * INPUT IN TL:
!           PREL         : zonal component of "rssavnabla prehyds"
!           PREM         : meridian component of "rssavnabla prehyds"
!           PRDELP       : 1./( prehyd[lbar] - prehyd[lbar-1] )
!           PRPRE        : 1./(prehyd[lbar]) (on interlayers) 
!           PRPP         : 1./((prehyd[lbar])*(prehyd[lbar-1]))
!           PLNPR        : "delta" on layers (is equal to
!                          "log(prehyd[lbar]/prehyd[lbar-1])" if "ndlnpr=0")
!           PALPH        : "alpha" on layers
!           PRTGR        : scalar ratio of vectors "(rssavnabla prehyd)/prehyd"
!                          and "rssavnabla prehyds" (computed in GPXYB).

!         * OUTPUT IN TL:
!           PLNPRL       : zonal component of "rssavnabla(delta)" on layers
!           PLNPRM       : meridian component of "rssavnabla(delta)" on layers
!           PALPHL       : zonal component of "rssavnabla(alpha)" on layers
!           PALPHM       : meridian component of "rssavnabla(alpha)" on layers
!           PALPHPLL     : zonal component of "rssavnabla (alpha + log prehyd)"
!                          on layers
!           PALPHPLM     : meridian component "rssavnabla (alpha + log prehyd)"
!                          on layers

!         * INPUT-TRAJECTORY:
!           PCOEFD5,PCOEFAPL5,PCOEFA5,
!           PRE5L,PRE5M,PRDELP5,PRPP5,PLNPR5,PALPH5,PRTGR5:
!           cf. PCOEFD,PCOEFAPL,PCOEFA,
!           PREL,PREM,PRDELP,PRPP,PLNPR,PALPH,PRTGR in GPGRXYB5:

!        Implicit arguments :   None.
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.    None.
!     ----------

!     Reference.
!     ----------

!     Author.
!     -------
!        K. YESSAD

!     Modifications.
!     --------------
!        Original : March 2006
!     K. Yessad (Dec 2008): remove dummy CDLOCK
!     ------------------------------------------------------------------

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

USE YOMDYN   , ONLY : NDLNPR
USE YOMCVER  , ONLY : LVERTFE
USE YOMGEM   , ONLY : VDELB

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KD 
INTEGER(KIND=JPIM),INTENT(IN)    :: KF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVC(KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVBH(0:KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PREL(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PREM(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRDELP(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRPRE(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRPP(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLNPR(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PALPH(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRTGR(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLNPRL(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLNPRM(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PALPHL(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PALPHM(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PALPHPLL(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PALPHPLM(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCOEFD5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCOEFAPL5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCOEFA5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRE5L(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRE5M(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRPP5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLNPR5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PALPH5(KPROMA,KFLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRTGR5(KPROMA,KFLEV) 

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

REAL(KIND=JPRB) :: ZCOEFA(KPROMA), ZCOEFAPL(KPROMA), ZCOEFD(KPROMA)

INTEGER(KIND=JPIM) :: JLEV, JROF
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

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

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

!*    2/ Calculation of "rssavnabla (alpha + log prehyd)" on layers
!        discretised as "rssavnabla alpha + (rssavnabla prehyd) / prehyd ",
!        and calculation of "rssavnabla alpha" on layers.

IF(.NOT.LVERTFE) THEN
  IF(NDLNPR == 0) THEN
    DO JLEV=1,KFLEV
      ZCOEFA(KD:KF)=0.0_JPRB
      ZCOEFAPL(KD:KF)=0.0_JPRB
      DO JROF=KD,KF
        ZCOEFA(JROF)=ZCOEFA(JROF) &
         & +PRE5L(JROF)*PALPHL(JROF,JLEV)+PRE5M(JROF)*PALPHM(JROF,JLEV)
        PREL(JROF)=PREL(JROF)+PCOEFA5(JROF,JLEV)*PALPHL(JROF,JLEV)
        PREM(JROF)=PREM(JROF)+PCOEFA5(JROF,JLEV)*PALPHM(JROF,JLEV)
        ZCOEFAPL(JROF)=ZCOEFAPL(JROF) &
         & +PRE5L(JROF)*PALPHPLL(JROF,JLEV)+PRE5M(JROF)*PALPHPLM(JROF,JLEV)
        PREL(JROF)=PREL(JROF)+PCOEFAPL5(JROF,JLEV)*PALPHPLL(JROF,JLEV)
        PREM(JROF)=PREM(JROF)+PCOEFAPL5(JROF,JLEV)*PALPHPLM(JROF,JLEV)
        ZCOEFAPL(JROF)=ZCOEFAPL(JROF)+ZCOEFA(JROF)
        PRTGR(JROF,JLEV)=PRTGR(JROF,JLEV)-ZCOEFA(JROF)
        PRPRE(JROF,JLEV)=PRPRE(JROF,JLEV)+PVBH(JLEV)*ZCOEFAPL(JROF)
      ENDDO
    ENDDO
  ELSEIF(NDLNPR == 1) THEN
    ! ky: for NH purpose, not yet validated.
    DO JLEV=1,KFLEV
      ZCOEFA(KD:KF)=0.0_JPRB
      ZCOEFAPL(KD:KF)=0.0_JPRB
      DO JROF=KD,KF
        ZCOEFA(JROF)=ZCOEFA(JROF) &
         & +PRE5L(JROF)*PALPHL(JROF,JLEV)+PRE5M(JROF)*PALPHM(JROF,JLEV)
        PREL(JROF)=PREL(JROF)+PCOEFA5(JROF,JLEV)*PALPHL(JROF,JLEV)
        PREM(JROF)=PREM(JROF)+PCOEFA5(JROF,JLEV)*PALPHM(JROF,JLEV)
        ZCOEFAPL(JROF)=ZCOEFAPL(JROF) &
         & +PRE5L(JROF)*PALPHPLL(JROF,JLEV)+PRE5M(JROF)*PALPHPLM(JROF,JLEV)
        PREL(JROF)=PREL(JROF)+PCOEFAPL5(JROF,JLEV)*PALPHPLL(JROF,JLEV)
        PREM(JROF)=PREM(JROF)+PCOEFAPL5(JROF,JLEV)*PALPHPLM(JROF,JLEV)
        ZCOEFA(JROF)=ZCOEFA(JROF)+ZCOEFAPL(JROF)
        PRTGR(JROF,JLEV)=PRTGR(JROF,JLEV)+ZCOEFAPL(JROF)
        PRPP(JROF,JLEV)=PRPP(JROF,JLEV) &
         & -(PVC(JLEV)*PALPH5(JROF,JLEV)/PLNPR5(JROF,JLEV))*ZCOEFA(JROF)
        PALPH(JROF,JLEV)=PALPH(JROF,JLEV) &
         & -(PVC(JLEV)*PRPP5(JROF,JLEV)/PLNPR5(JROF,JLEV))*ZCOEFA(JROF)
        PLNPR(JROF,JLEV)=PLNPR(JROF,JLEV) &
         & +( PVC(JLEV)*PRPP5(JROF,JLEV)*PALPH5(JROF,JLEV) &
         & /(PLNPR5(JROF,JLEV)*PLNPR5(JROF,JLEV)) )*ZCOEFA(JROF)
      ENDDO
    ENDDO
  ENDIF
ENDIF

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

!*    1/ Calculation of "rssavnabla delta" on layers.

IF(LVERTFE) THEN
  DO JLEV=1,KFLEV
    ZCOEFD(KD:KF)=0.0_JPRB
    DO JROF=KD,KF
      ZCOEFD(JROF)=ZCOEFD(JROF) &
       & +PRE5L(JROF)*PLNPRL(JROF,JLEV)+PRE5M(JROF)*PLNPRM(JROF,JLEV)
      PREL(JROF)=PREL(JROF)+PCOEFD5(JROF,JLEV)*PLNPRL(JROF,JLEV)
      PREM(JROF)=PREM(JROF)+PCOEFD5(JROF,JLEV)*PLNPRM(JROF,JLEV)
      PRDELP(JROF,JLEV)=PRDELP(JROF,JLEV) &
       & +VDELB(JLEV)*PLNPR5(JROF,JLEV)*ZCOEFD(JROF)
      PRTGR(JROF,JLEV)=PRTGR(JROF,JLEV)-PLNPR5(JROF,JLEV)*ZCOEFD(JROF)
      PLNPR(JROF,JLEV)=PLNPR(JROF,JLEV) &
       & +(VDELB(JLEV)*PRDELP5(JROF,JLEV)-PRTGR5(JROF,JLEV))*ZCOEFD(JROF)
    ENDDO
  ENDDO
ELSE
  DO JLEV=1,KFLEV
    ZCOEFD(KD:KF)=0.0_JPRB
    DO JROF=KD,KF
      ZCOEFD(JROF)=ZCOEFD(JROF) &
       & +PRE5L(JROF)*PLNPRL(JROF,JLEV)+PRE5M(JROF)*PLNPRM(JROF,JLEV)
      PREL(JROF)=PREL(JROF)+PCOEFD5(JROF,JLEV)*PLNPRL(JROF,JLEV)
      PREM(JROF)=PREM(JROF)+PCOEFD5(JROF,JLEV)*PLNPRM(JROF,JLEV)
      PRPP(JROF,JLEV)=PRPP(JROF,JLEV)-PVC(JLEV)*ZCOEFD(JROF)
    ENDDO
  ENDDO
ENDIF

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

!*    0/ Final set to zero.

PLNPRL(KD:KF,1:KFLEV)=0.0_JPRB
PLNPRM(KD:KF,1:KFLEV)=0.0_JPRB
PALPHL(KD:KF,1:KFLEV)=0.0_JPRB
PALPHM(KD:KF,1:KFLEV)=0.0_JPRB
PALPHPLL(KD:KF,1:KFLEV)=0.0_JPRB
PALPHPLM(KD:KF,1:KFLEV)=0.0_JPRB

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

IF (LHOOK) CALL DR_HOOK('GPGRXYBAD',1,ZHOOK_HANDLE)
END SUBROUTINE GPGRXYBAD
