SUBROUTINE EXCHCO_VDFAD(&
 & KPROMA,KIDIA,KFDIA,&
! Nonlinear dependencies
 & LDUN5,PUB5,PVB5,PUN5,PVN5,&
 & PAPHI5,PAZO5,&
! Linear perturbations
 & PUB ,PVB ,PUN ,PVN ,&
 & PAPHI ,PAZO ,&
 & PBN ,PBM)


!**** *EXCHCO_VDF* - COMPUTATION OF THE MODIFIED ROUGHNESS LENGTHS

!     PURPOSE
!     -------
!          COMPUTE THE FUNCTION OF DRAG FOR MOMENTUM
!          BASED ON SURFACE ROUGNESS, LOWEST-LEVEL WIND AND 10M NEUTRAL WIND

!**   INTERFACE
!     ---------
!       *CALL* *EXCHCO_VDF(
!    C                 KPROMA,KIDIA,KFDIA,
!    I                 ZUF5(1,NFLEVG),ZVF5(1,NFLEVG),ZUN5(1,NFLEVG),ZVN5(1,NFLEVG),
!    I                 PAPHI5(1,NFLEVG),PAZO5(1,NFLEVG),
!    O                 ZBN5(1,NFLEVG),ZBM5(1,NFLEVG)
!        EXPLICIT ARGUMENTS
!        ------------------

!        KPROMA             - HORIZONTAL DIMENSION.                      (INPUT-C)
!        KIDIA              - INDEX OF START POINT                       (INPUT-C)
!        KFDIA              - INDEX OF END   POINT                       (INPUT-C)
!        LDUN5(KPROMA)      - ALLOW PERTURBATIONS IN NEUTRAL WIND        (INPUT)
!        PAPHI5(KPROMA)     - GEOPOTENTIAL     AT LOWER LEVEL M**2/S**2  (INPUT)
!        PUB5(KPROMA)       - U-WIND COMPONENT AT LOWER LEVEL    M/S     (INPUT)
!        PVB5(KPROMA)       - V-WIND COMPONENT AT LOWER LEVEL    M/S     (INPUT)
!        PUN5(KPROMA)       - U-WIND NEUTRAL COMPONENT AT 10M    M/S     (INPUT)
!        PVN5(KPROMA)       - V-WIND NEUTRAL COMPONENT AT 10M    M/S     (INPUT)
!        PAZO5(KPROMA)      - ROUGHNESS DISTRIBUTION              M      (INPUT)
!        PBN5(KPROMA)       - NEUTRAL COEFFICIENT                        (OUTPUT)
!        PBM5(KPROMA)       - MOMENTUM COEFFICIENT                       (OUTPUT)

!        IMPLICIT ARGUMENTS :  NONE.
!        --------------------

!     METHOD.
!     -------
!        SEE DOCUMENTATION

!     EXTERNALS.
!     ----------
!        NONE

!     REFERENCE.
!     ----------
!        ECMWF RESEARCH DEPARTMENT DOCUMENTATION OF THE IFS

!     AUTHOR.
!     -------
!        HANS HERSBACH *ECMWF*

!        ORIGINAL   : 28-10-2009
!     ------------------------------------------------------------------

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

USE YOMCST   , ONLY : RG
USE YOEVDF   , ONLY : REPDU2

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
!* NONLINEAR
LOGICAL           ,INTENT(IN)    :: LDUN5 (KPROMA)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHI5(KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUB5  (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVB5  (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUN5  (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVN5  (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAZO5 (KPROMA) 

!* TANGENT LINEAR
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPHI (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUB   (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVB   (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUN   (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVN   (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAZO  (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PBN   (KPROMA) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PBM   (KPROMA) 

INTEGER(KIND=JPIM) :: JL

REAL(KIND=JPRB) :: ZH5,ZDUS5,ZDNS5,ZDU5,ZDN5,ZL5,ZDL5,ZBL5,ZDH5,ZBH5,ZBN5,ZBM5
REAL(KIND=JPRB) ::     ZDUS ,ZDNS ,ZDU ,ZDN ,ZL ,ZDL ,ZBL ,ZDH ,ZBH

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*         1.     COMPUTATION OF THE MODIFIED ROUGHNESS LENGTH.
!                 ---------------------------------------------

ZH5=10.0_JPRB

DO JL=KIDIA,KFDIA
!* NONLINEAR DEPENDENCIES

  ZDUS5=MAX(PUB5(JL)**2+PVB5(JL)**2,REPDU2)
  ZDNS5=MAX(PUN5(JL)**2+PVN5(JL)**2,REPDU2)
  ZDU5 =SQRT(ZDUS5)
  ZDN5 =SQRT(ZDNS5)

  ZL5  =PAPHI5(JL)/RG

  ZDL5 =ZL5/PAZO5(JL)
  ZBL5 =LOG(1.0_JPRB+ZDL5)

  ZDH5 =ZH5/PAZO5(JL)
  ZBH5 =LOG(1.0_JPRB+ZDH5)

  ZBN5 =ZBL5
  ZBM5 =ZBH5*ZDU5/ZDN5

!* ADJOINT
  IF (.NOT.LDUN5(JL)) THEN
     PBN(JL)  = PBN(JL)+(ZBM5/ZBN5)*PBM(JL)
     PBM(JL)  = 0.0_JPRB
  ENDIF

  ZDN      =-PBM(JL)*ZBM5/ZDN5
  ZDU      = PBM(JL)*ZBH5/ZDN5
  ZBH      = PBM(JL)*ZDU5/ZDN5
  ZBL      = PBN(JL)

  ZDH      = ZBH/(1.0_JPRB+ZDH5)
  PAZO(JL) = PAZO(JL) - ZDH*ZDH5/PAZO5(JL)
  ZDL      = ZBL/(1.0_JPRB+ZDL5)
  PAZO(JL) = PAZO(JL)-ZDL*ZDL5/PAZO5(JL)
  ZL       =         +ZDL     /PAZO5(JL)
  PAPHI(JL)= PAPHI(JL)+ZL/RG

  ZDNS = 0.5_JPRB*ZDN/ZDN5
  IF (ZDNS5>REPDU2) THEN
     PUN(JL)=PUN(JL) + 2.0_JPRB*ZDNS*PUN5(JL)
     PVN(JL)=PVN(JL) + 2.0_JPRB*ZDNS*PVN5(JL)
  ENDIF
  
  ZDUS = 0.5_JPRB*ZDU/ZDU5
  IF (ZDUS5>REPDU2) THEN
     PUB(JL)=PUB(JL) + 2.0_JPRB*ZDUS*PUB5(JL)
     PVB(JL)=PVB(JL) + 2.0_JPRB*ZDUS*PVB5(JL)
  ENDIF

  PBM(JL)=0.0_JPRB
  PBN(JL)=0.0_JPRB

ENDDO

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

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

