MODULE SPPCFLSAD_MOD
CONTAINS
SUBROUTINE SPPCFLSAD(KIDIA,KFDIA,KLON &
! INPUT - trajectory
 & , PUMLEV5, PVMLEV5, PQMLEV5, PAPHMS5, PGEOMLEV5, PCPTGZLEV5 &
 & , PCPTS5 , PQSAM5 , PZ0MM5 , PZ0HM5 , PZ0QM5   , PBUOM5 &
! OUPUT - trajectory
 & , PU105  , PV105  , P10NU5 , P10NV5 , PT25   , PD25   , PQ25 &
! INPUT
 & , PUMLEV , PVMLEV , PQMLEV , PAPHMS , PGEOMLEV , PCPTGZLEV &
 & , PCPTS  , PQSAM  , PZ0MM  , PZ0HM  , PZ0QM    , PBUOM &
 ! OUPUT     
 & , PU10   , PV10   , P10NU  , P10NV  , PT2    , PD2    , PQ2 )  

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

USE YOS_CST   , ONLY : RG       ,RD       ,RCPD     ,RETV     ,RTT  
USE YOS_THF   , ONLY : R2ES     ,R3LES    ,R4LES    ,&
 & RVTMP2
USE YOS_EXC   , ONLY : RKAP     ,REPDU2   ,RPARZI

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

!**   *SPPCFLSAD* - COMPUTES THE SURFACE (2 M) TEMPERATURE AND HUMIDITY
!                   WITH STABILITY FROM GELEYN'S INTERPOLATION
!                   (Adjoint)

!  AUTHOR: 
!     M. Janiskova   ECMWF  July 2005 - TL version of SPPCFLS for externalize 
!                                       SURF

!  REVISION HISTORY:
!     H. Hersbach   04-Dec-2009 10-m neutral wind

!     PURPOSE
!     -------

!     COMPUTE WIND COMPONENTS, TEMPERATURE AND DEWPOINT TEMPERATURE
!     AT SCREEN LEVEL HEIGHT
!     (Adjoint)

!     INTERFACE
!     ---------

!     *SPPCFLSAD* IS CALLED BY *SURFPPSAD* from *VDFMAINSAD*

!     INPUT PARAMETERS (INTEGER):

!     *KIDIA*        START POINT
!     *KFDIA*        END POINT
!     *KLON*         NUMBER OF GRID POINTS PER PACKET

!*      INPUT PARAMETERS (REAL):
!  Trajectory  Perturbation  Description                               Unit
!  PUMLEV5     PUMLEV        X-VELOCITY COMPONENT AT T-1,              m/s
!                            lowest atmospheric level
!  PVMLEV5     PVMLEV        Y-VELOCITY COMPONENT AT T-1,              m/s
!                            lowest atmospheric level
!  PQMLEV5     PQMLEV        SPECIFIC HUMUDITY AT T-1,                 kg/kg
!                            lowest atmospheric level
!  PAPHMS5     PAPHMS        Surface PRESSURE AT T-1                   Pa
!  PGEOMLEV5   PGEOMLEV      GEOPOTENTIAL AT T-1,                      J/kg
!                            lowest atmospheric level
!  PCPTGZLEV5  PCPTGZLEV     DRY STATIC ENERGY AT LOWEST MODEL LEVEL   J/kg
!  PCPTS5      PCPTS         DRY STATIC ENERGY AT THE SURFACE          J/kg
!  PQSAM5      PQSAM         SPECIFIC HUMIDITY AT THE SURFACE          kg/kg
!  PZ0MM5      PZ0MM         AERODYNAMIC ROUGHNESS LENGTH              m
!  PZ0HM5      PZ0HM         ROUGHNESS LENGTH FOR TEMPERATURE          m
!  PZ0QM5      PZ0QM         ROUGHNESS LENGTH FOR MOISTURE             m
!  PBUOM5      PBUOM         BUOYANCE FLUX AT THE SURFACE              ???? 

!*      OUTPUT PARAMETERS (REAL):
!  Trajectory  Perturbation  Description                               Unit
!  PU105       PU10          U-COMPONENT WIND AT 10 M                  m/s
!  PV105       PV10          V-COMPONENT WIND AT 10 M                  m/s
!  P10NU5      P10NU         U-COMPONENT NEUTRAL WIND AT 10 M          m/s
!  P10NV5      P10NV         V-COMPONENT NEUTRAL WIND AT 10 M          m/s
!  PT25        PT2           TEMPERATURE AT 2 M                        K
!  PD25        PD2           DEW POINT TEMPERATURE AT 2 M              K
!  PQ25        PQ2           SPECIFIC HUMIDITY AT 2 M                  K


!     METHOD
!     ------

!     ANALYTIC INTERPOLATION - GELEYN (1988) TELLUS 40A 347-351

!     ------------------------------------------------------------------
!     REMARK : THE MAIN JUSTIFICATION OF THIS ROUTINE IS 
!              FOR USE IN TL AND AD VERSIONS OF IFS
!     ------------------------------------------------------------------
!
!     MODIFICATIONS
!     -------------
!
!      P. Lopez, ECMWF, October 2006 : Corrected bug in exponential.
!
!-----------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUMLEV5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVMLEV5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQMLEV5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHMS5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOMLEV5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCPTGZLEV5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCPTS5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSAM5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PZ0MM5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PZ0HM5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PZ0QM5(:) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBUOM5(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PU105(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PV105(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P10NU5(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P10NV5(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PT25(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PD25(:) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQ25(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUMLEV(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVMLEV(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQMLEV(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPHMS(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGEOMLEV(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCPTGZLEV(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCPTS(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQSAM(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0MM(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0HM(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PZ0QM(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PBUOM(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PU10(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PV10(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: P10NU(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: P10NV(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PT2(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PD2(:) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQ2(:) 

!*            LOCAL STORAGE
!             ----- -------

REAL(KIND=JPRB) :: Z1DZ0M5(KLON), Z1DZ0H5(KLON), Z1DZ0Q5(KLON), ZXLNM5(KLON)
REAL(KIND=JPRB) :: ZXLNH5(KLON) , ZXLNQ5(KLON) , ZDU25(KLON)  , ZNLEV5(KLON)
REAL(KIND=JPRB) :: Z1DZ1D5(KLON), ZRICLS5(KLON)
REAL(KIND=JPRB) :: ZCFM5(KLON)  , ZCFH5(KLON)  , ZCFQ5(KLON)
REAL(KIND=JPRB) :: ZCDNM5(KLON) , ZCDNH5(KLON) , ZCDNQ5(KLON) , ZZQM15(KLON)

REAL(KIND=JPRB) :: Z1DZ0M(KLON), Z1DZ0H(KLON), Z1DZ0Q(KLON), ZXLNM(KLON)
REAL(KIND=JPRB) :: ZXLNH(KLON) , ZXLNQ(KLON) , ZDU2(KLON)  , ZNLEV(KLON)
REAL(KIND=JPRB) :: Z1DZ1D(KLON), ZRICLS(KLON)
REAL(KIND=JPRB) :: ZCFM(KLON)  , ZCFH(KLON)  , ZCFQ(KLON)
REAL(KIND=JPRB) :: ZCDNM(KLON) , ZCDNH(KLON) , ZCDNQ(KLON) , ZZQM1(KLON)

REAL(KIND=JPRB) :: Z10UIV5(KLON) , Z10NIV5(KLON), Z2QIV5(KLON), Z2SIV5(KLON), &
 & ZAPH2M5(KLON), ZBD5(KLON)  , ZBH5(KLON)   , ZBN5(KLON)  , ZBNH5(KLON), &
 & ZBNQ5(KLON)  , ZBQ5(KLON)  , ZCH5(KLON)   , ZCH05 (KLON), ZCM5(KLON) , &
 & ZCM05(KLON)  , ZCORQ5(KLON), ZCORS5(KLON) , ZCORU5(KLON), ZCPT2M5(KLON),&
 & ZCQ5(KLON)   , ZCQ05(KLON) , ZDRORO5(KLON), ZFRAC5(KLON), ZLOGQ5(KLON),&
 & ZLOGS5(KLON) , ZSDIV5(KLON), ZMULTI5(KLON), ZLOGU5(KLON), ZMU5(KLON)  ,&
 & ZMUQ5(KLON)  , ZPH5(KLON)  , ZPM5(KLON)   , ZPQ5(KLON)  , ZRS5(KLON)  ,&
 & ZRU5(KLON)   , ZWST25(KLON), Z1EXP5(KLON) , Z2EXP5(KLON), Z3EXP5(KLON),&
 & Z4EXP5(KLON) , Z5EXP5(KLON), Z6EXP5(KLON) , Z1S5(KLON)  , Z2S5(KLON)  ,& 
 & Z3S5(KLON)   , Z4S5(KLON)  , Z5S5(KLON)   , Z6S5(KLON)  , Z7S5(KLON)  ,&
 & Z8S5(KLON)   , Z9S5(KLON)  , Z1T5(KLON)   , Z2T5(KLON)  , Z1X5(KLON)  ,&
 & Z2X5(KLON)  , Z3X5(KLON)   , Z4X5(KLON)   , Z9N5(KLON)
REAL(KIND=JPRB) :: ZPD25(KLON)

REAL(KIND=JPRB) :: Z10M, Z10UIV, Z10NIV, Z2B, Z2M, Z2QIV, Z2SIV, Z3B,&
 & ZAPH2M, ZBD, ZBH , ZBN  , ZBNH , ZBNQ , ZBQ  , ZCH  , &
 & ZCH0  , ZCM, ZCM0, ZCON1, ZCON2, ZCORQ, ZCORS, &
 & ZCORU , ZCPT2M, ZCQ  , ZCQ0 , ZD   , &
 & ZDRORO, ZFRAC , ZIPBL, ZLOGQ, ZLOGS, ZSDIV, ZMULTI, &
 & ZLOGU , ZMU   , ZMUQ , ZPH  , ZPM  , ZPQ  , ZRS, ZRU, &
 & ZWST2 , Z1EXP, Z2EXP, Z3EXP, Z4EXP, Z5EXP, Z6EXP, &
 & Z1S   , Z2S   , Z3S  , Z4S  , Z5S  , Z6S  , Z7S  , Z8S  , &
 & Z9S   , Z1T   , Z2T  , ZEPS1, Z9N  

INTEGER(KIND=JPIM) :: JL
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1.   INITIALIZE CONSTANTS
!             ---------- ----------

IF (LHOOK) CALL DR_HOOK('SPPCFLSAD_MOD:SPPCFLSAD',0,ZHOOK_HANDLE)
ZCON1 = RVTMP2-RETV
ZCON2 = 2.0_JPRB/3.0_JPRB
ZEPS1  =1.0E-12_JPRB

!     CONSTANTS FOR THE STABILITY FUNCTIONS

Z2B = 10.0_JPRB
ZD  = 5.0_JPRB
Z3B = 15.0_JPRB

!     PBL HEIGHT FOR W* - EFFECT

ZIPBL = RPARZI

!     THIS SPECIFIES THE HEIGHT FOR U,V (10 M) AND T,Q (2 M)

Z10M = 10.0_JPRB
Z2M  = 2.0_JPRB

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

!*       2.   COMPUTATION OF SURFACE EXCHANGE COEFFICIENTS
!             ---------------------------------------------

DO JL=KIDIA,KFDIA

!             (W*)**2, WIND SHEAR, RICHARDSON NUMBER,

  IF (PBUOM5(JL)  <  ZEPS1) THEN
    ZWST25(JL) = 0.0_JPRB
  ELSE
    ZWST25(JL) = (PBUOM5(JL)*ZIPBL)**ZCON2
  ENDIF
  Z1S5(JL) = PUMLEV5(JL)**2+PVMLEV5(JL)**2+ZWST25(JL)
  IF (Z1S5(JL) < REPDU2) THEN
    ZDU25(JL) = REPDU2
  ELSE
    ZDU25(JL) = Z1S5(JL)
  ENDIF
  Z2S5(JL) = PCPTGZLEV5(JL)+PCPTS5(JL)-PGEOMLEV5(JL)
  ZDRORO5(JL) = 2.0_JPRB*(PCPTGZLEV5(JL)-PCPTS5(JL))/Z2S5(JL) &
   & -ZCON1*(PQMLEV5(JL)-PQSAM5(JL)) 
  ZRICLS5(JL)=PGEOMLEV5(JL)*ZDRORO5(JL)/ZDU25(JL)


!             COMMON FACTORS IN NEUTRAL FORMULAE AND
!             DRAG COEFFICIENTS.

  ZNLEV5(JL) = PGEOMLEV5(JL)/RG+PZ0MM5(JL)
  Z1DZ0M5(JL) = ZNLEV5(JL)/PZ0MM5(JL)
  Z1DZ0H5(JL) = ZNLEV5(JL)/PZ0HM5(JL)
  Z1DZ0Q5(JL) = ZNLEV5(JL)/PZ0QM5(JL)
  Z1DZ1D5(JL) = Z1DZ0M5(JL)/(Z1DZ0M5(JL)-1.0_JPRB)
  ZXLNM5(JL) = LOG(Z1DZ0M5(JL))
  ZXLNH5(JL) = LOG(Z1DZ0H5(JL))
  ZXLNQ5(JL) = LOG(Z1DZ0Q5(JL))

!              NEUTRAL DRAG COEFFICIENTS

  ZCDNH5(JL) = (RKAP**2)/(ZXLNM5(JL)*ZXLNH5(JL))
  ZCDNQ5(JL) = (RKAP**2)/(ZXLNM5(JL)*ZXLNQ5(JL))
  ZCDNM5(JL) = (RKAP**2)/(ZXLNM5(JL)*ZXLNM5(JL))

!          PARAMETERS DEPENDING ON RATIO OF ROUGHNESS LENGTHS

  Z3S5(JL) = PZ0MM5(JL)/PZ0HM5(JL)
  IF (Z3S5(JL) > 100.0_JPRB) THEN
    ZMU5(JL) = LOG(100.0_JPRB)
  ELSE
    ZMU5(JL) = LOG(Z3S5(JL))
  ENDIF
  Z4S5(JL) = PZ0MM5(JL)/PZ0QM5(JL)
  IF (Z4S5(JL) > 100.0_JPRB) THEN
    ZMUQ5(JL) = LOG(100.0_JPRB)
  ELSE
    ZMUQ5(JL) = LOG(Z4S5(JL))
  ENDIF

  ZCM05(JL) = (6.8741_JPRB + ZMU5(JL)*( 2.6933_JPRB &
   & + ZMU5(JL)*(-0.3601_JPRB + ZMU5(JL) * 0.0154_JPRB)))  
  ZPM5(JL) = (0.5233_JPRB + ZMU5(JL)*(-0.0815_JPRB &
   & + ZMU5(JL)*( 0.0135_JPRB + ZMU5(JL) *(-0.001_JPRB))))  
  ZCH05(JL) = (3.2165_JPRB + ZMU5(JL)*( 4.3431_JPRB &
   & + ZMU5(JL)*( 0.5360_JPRB + ZMU5(JL) *(-0.0781_JPRB))))  
  ZPH5(JL) = (0.5802_JPRB + ZMU5(JL)*(-0.1571_JPRB &
   & + ZMU5(JL)*( 0.0327_JPRB + ZMU5(JL) *(-0.0026_JPRB))))  
  ZCQ05(JL) = (3.2165_JPRB + ZMUQ5(JL)*( 4.3431_JPRB &
   & + ZMUQ5(JL)*(0.5360_JPRB + ZMUQ5(JL) *(-0.0781_JPRB))))  
  ZPQ5(JL) = (0.5802_JPRB + ZMUQ5(JL)*(-0.1571_JPRB &
   & + ZMUQ5(JL)*(0.0327_JPRB + ZMUQ5(JL) *(-0.0026_JPRB))))  

  ZCM5(JL) = ZCM05(JL)*Z1DZ0M5(JL)**ZPM5(JL)
  ZCH5(JL) = ZCH05(JL)*Z1DZ0H5(JL)**ZPH5(JL)
  ZCQ5(JL) = ZCQ05(JL)*Z1DZ0Q5(JL)**ZPQ5(JL)

!            STABILITY FUNCTIONS 

  IF (ZRICLS5(JL) > 0) THEN
    Z5S5(JL) = SQRT(1.0_JPRB+ZD*ZRICLS5(JL))
    ZSDIV5(JL) = 1.0_JPRB+Z2B*ZRICLS5(JL)/Z5S5(JL)
    ZMULTI5(JL) = 1.0_JPRB/(1.0_JPRB+Z3B*ZRICLS5(JL)*Z5S5(JL))

    ZCFM5(JL) = ZCDNM5(JL)/ZSDIV5(JL)
    ZCFH5(JL) = ZCDNH5(JL)*ZMULTI5(JL)
    ZCFQ5(JL) = ZCDNQ5(JL)*ZMULTI5(JL)
  ELSE
    IF (ZRICLS5(JL) < 0.0_JPRB) THEN
      Z6S5(JL) = SQRT(-ZRICLS5(JL))
    ELSE
      Z6S5(JL) = SQRT(ZRICLS5(JL))
    ENDIF

    Z1X5(JL) = 1.0_JPRB/(1.0_JPRB+Z3B*ZCDNM5(JL)*ZCM5(JL)*Z6S5(JL))
    ZCFM5(JL) = ZCDNM5(JL)*(1.0_JPRB-Z2B*ZRICLS5(JL)*Z1X5(JL))

    Z2X5(JL) = 1.0_JPRB/(1.0_JPRB+Z3B*ZCDNH5(JL)*ZCH5(JL)*Z6S5(JL))
    ZCFH5(JL) = ZCDNH5(JL)*(1.0_JPRB-Z3B*ZRICLS5(JL)*Z2X5(JL))

    Z3X5(JL) = 1.0_JPRB/(1.0_JPRB+Z3B*ZCDNQ5(JL)*ZCQ5(JL)*Z6S5(JL)) 
    ZCFQ5(JL) = ZCDNQ5(JL)*(1.0_JPRB-Z3B*ZRICLS5(JL)*Z3X5(JL))
  ENDIF

ENDDO

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

!*       3.   COMPUTATION OF WIND COMPONENT AND TEMPERATURE
!             ----------------------------------------------

DO JL=KIDIA,KFDIA
  Z7S5(JL) = SQRT(ZCDNM5(JL))
  ZBN5(JL) = RKAP/Z7S5(JL)
  ZBNH5(JL) = RKAP*Z7S5(JL)/ZCDNH5(JL)
  ZBNQ5(JL) = RKAP*Z7S5(JL)/ZCDNQ5(JL)
  Z8S5(JL) = SQRT(ZCFM5(JL))
  ZBD5(JL) = RKAP/Z8S5(JL)
  ZBH5(JL) = RKAP*Z8S5(JL)/ZCFH5(JL)
  ZBQ5(JL) = RKAP*Z8S5(JL)/ZCFQ5(JL)

  ZRU5(JL) = Z10M/ZNLEV5(JL)
  ZRS5(JL) = Z2M/ZNLEV5(JL)

  Z1EXP5(JL) = EXP(ZBN5(JL))
  ZLOGU5(JL) = LOG(1.0_JPRB+ZRU5(JL)*(Z1EXP5(JL)-1.0_JPRB))
  Z2EXP5(JL) = EXP(ZBNH5(JL))
  ZLOGS5(JL) = LOG(1.0_JPRB+ZRS5(JL)*(Z2EXP5(JL)-1.0_JPRB))
  Z3EXP5(JL) = EXP(ZBNQ5(JL))
  ZLOGQ5(JL) = LOG(1.0_JPRB+ZRS5(JL)*(Z3EXP5(JL)-1.0_JPRB))

  IF (ZRICLS5(JL) > 0.0_JPRB) THEN
    ZCORU5(JL) = ZRU5(JL)*(ZBN5(JL)-ZBD5(JL))
    ZCORS5(JL) = ZRS5(JL)*(ZBNH5(JL)-ZBH5(JL))
    ZCORQ5(JL) = ZRS5(JL)*(ZBNQ5(JL)-ZBQ5(JL))
  ELSE
    IF ((ZBN5(JL) -ZBD5(JL)) < 0.0_JPRB) THEN
      Z4EXP5(JL) = 1.0_JPRB
    ELSE
      IF ((ZBN5(JL)-ZBD5(JL)) < 200.0_JPRB) THEN
        Z4EXP5(JL) = EXP(ZBN5(JL) -ZBD5(JL))
      ELSE
        Z4EXP5(JL) = EXP(200.0_JPRB)
      ENDIF
    ENDIF

    ZCORU5(JL) = LOG(1.0_JPRB+ZRU5(JL)*(Z4EXP5(JL)-1.0_JPRB))
    IF ((ZBNH5(JL)-ZBH5(JL)) < 0.0_JPRB) THEN
      Z5EXP5(JL) = 1.0_JPRB
    ELSE
      IF ((ZBNH5(JL)-ZBH5(JL)) < 200.0_JPRB) THEN
        Z5EXP5(JL) = EXP(ZBNH5(JL)-ZBH5(JL))
      ELSE
        Z5EXP5(JL) = EXP(200.0_JPRB)
      ENDIF
    ENDIF
    ZCORS5(JL) = LOG(1.0_JPRB+ZRS5(JL)*(Z5EXP5(JL)-1.0_JPRB))

    IF ((ZBNQ5(JL)-ZBQ5(JL)) < 0.0_JPRB) THEN
      Z6EXP5(JL) = 1.0_JPRB
    ELSE
      IF ((ZBNQ5(JL)-ZBQ5(JL)) < 200.0_JPRB) THEN
        Z6EXP5(JL) = EXP(ZBNQ5(JL)-ZBQ5(JL))
      ELSE
        Z6EXP5(JL) = EXP(200.0_JPRB)
      ENDIF
    ENDIF
    ZCORQ5(JL) = LOG(1.0_JPRB+ZRS5(JL)*(Z6EXP5(JL)-1.0_JPRB))
  ENDIF

!     10 M WIND COMPONENT

  Z9S5(JL) = (ZLOGU5(JL)-ZCORU5(JL))/ZBD5(JL)
  IF (Z9S5(JL) > 1.0_JPRB) THEN
    Z10UIV5(JL) = 1.0_JPRB
  ELSEIF (Z9S5(JL) < 0.0_JPRB) THEN
    Z10UIV5(JL) = 0.0_JPRB
  ELSE
    Z10UIV5(JL) = Z9S5(JL)
  ENDIF

  PU105(JL) = PUMLEV5(JL)*Z10UIV5(JL)
  PV105(JL) = PVMLEV5(JL)*Z10UIV5(JL)

!     10 M NEUTRAL WIND COMPONENT

  Z9N5(JL) = ZLOGU5(JL)/ZBD5(JL)
  IF (Z9S5(JL) < 0.0_JPRB) THEN
    Z10NIV5(JL) = 0.0_JPRB
  ELSE
    Z10NIV5(JL) = Z9N5(JL)
  ENDIF

  P10NU5(JL) = PUMLEV5(JL)*Z10NIV5(JL)
  P10NV5(JL) = PVMLEV5(JL)*Z10NIV5(JL)

!     2M TEMPERATURE AND 2M SPECIFIC HUMIDITY

  Z1T5(JL) = (ZLOGS5(JL)-ZCORS5(JL))/ZBH5(JL)
  IF (Z1T5(JL) > 1.0_JPRB) THEN
    Z2SIV5(JL) = 1.0_JPRB
  ELSEIF (Z1T5(JL) < 0.0_JPRB) THEN
    Z2SIV5(JL) = 0.0_JPRB
  ELSE
    Z2SIV5(JL) = Z1T5(JL)
  ENDIF

  Z2T5(JL) = (ZLOGQ5(JL)-ZCORQ5(JL))/ZBQ5(JL)
  IF (Z2T5(JL) > 1.0_JPRB) THEN
    Z2QIV5(JL) = 1.0_JPRB
  ELSEIF (Z2T5(JL) < 0.0_JPRB) THEN
    Z2QIV5(JL) = 0.0_JPRB
  ELSE
    Z2QIV5(JL) = Z2T5(JL)
  ENDIF

  ZCPT2M5(JL) = PCPTS5(JL)+(PCPTGZLEV5(JL)-PCPTS5(JL))*Z2SIV5(JL)
  IF (PQMLEV5(JL) < 1.E-12_JPRB) THEN
    ZZQM15(JL) = 1.E-12_JPRB
  ELSE
    ZZQM15(JL) = PQMLEV5(JL)
  ENDIF
  PQ25(JL) = PQSAM5(JL)+(ZZQM15(JL)-PQSAM5(JL))*Z2QIV5(JL)

!     APPROXIMATE MOISTURE CORRECTION IN CP WITH QNLEV

  PT25(JL) = (ZCPT2M5(JL)-RG*Z2M)/( RCPD*(1.0_JPRB+RVTMP2*ZZQM15(JL)) )

ENDDO

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

!        4.   COMPUTE  DEW POINT TEMPERATURE
!             ------------------------------

DO JL=KIDIA,KFDIA
  Z4X5(JL) = 1.0_JPRB/(RD*PT25(JL)*(1.0_JPRB+RETV*ZZQM15(JL)))
  ZAPH2M5(JL) = PAPHMS5(JL)*(1.0_JPRB-Z2M*Z4X5(JL)) 

! Note the use of saturation with respect to water in conformance to WMO
!  observation dissemination practice

  ZFRAC5(JL) = LOG(ZAPH2M5(JL)*PQ25(JL) &
   & / (R2ES*(1.0_JPRB+RETV*PQ25(JL))))/R3LES
  PD25(JL) = (RTT-ZFRAC5(JL)*R4LES)/(1.0_JPRB-ZFRAC5(JL))
  ZPD25(JL) = PD25(JL)

!     LIMIT DEW POINT TEMPERATURE < TEMPERATURE

  IF (PD25(JL) > PT25(JL)) THEN
    PD25(JL) = PT25(JL)
  ENDIF

ENDDO

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

!*                  -------- ADJOINT COMPUTATIONS --------

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

!*         5.     INITIALIZATION TO ZERO OF LOCAL VARIABLES


Z1DZ0M(:) = 0.0_JPRB
Z1DZ0H(:) = 0.0_JPRB
Z1DZ0Q(:) = 0.0_JPRB
ZXLNM (:) = 0.0_JPRB
ZXLNH (:) = 0.0_JPRB
ZXLNQ (:) = 0.0_JPRB
ZDU2  (:) = 0.0_JPRB
ZNLEV (:) = 0.0_JPRB
Z1DZ1D(:) = 0.0_JPRB
ZRICLS(:) = 0.0_JPRB
ZCFM  (:) = 0.0_JPRB
ZCFH  (:) = 0.0_JPRB
ZCFQ  (:) = 0.0_JPRB
ZCDNM (:) = 0.0_JPRB
ZCDNH (:) = 0.0_JPRB
ZCDNQ (:) = 0.0_JPRB
ZZQM1 (:) = 0.0_JPRB

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

!        4.   COMPUTE  DEW POINT TEMPERATURE
!             ------------------------------

DO JL=KIDIA,KFDIA
  ZFRAC  = 0.0_JPRB
  ZAPH2M = 0.0_JPRB

!     LIMIT DEW POINT TEMPERATURE < TEMPERATURE

  IF (ZPD25(JL) > PT25(JL)) THEN
    PT2(JL) = PT2(JL)+PD2(JL)
    PD2(JL) = 0.0_JPRB
  ENDIF

! Note the use of saturation with respect to water in conformance to WMO
!  observation dissemination practice

  ZFRAC = ZFRAC+(RTT-R4LES)*PD2(JL)/(1.0_JPRB-ZFRAC5(JL))**2
  PD2(JL) = 0.0_JPRB
  PQ2(JL) = PQ2(JL)+ZAPH2M5(JL)*ZFRAC &
   & / (R3LES*ZAPH2M5(JL)*PQ25(JL)*(1.0_JPRB+RETV*PQ25(JL)))
  ZAPH2M = ZAPH2M+PQ25(JL)*(1.0_JPRB+RETV*PQ25(JL))*ZFRAC &
   & / (R3LES*ZAPH2M5(JL)*PQ25(JL)*(1.0_JPRB+RETV*PQ25(JL)))

  PAPHMS(JL) = PAPHMS(JL)+(1.0_JPRB-Z2M*Z4X5(JL))*ZAPH2M
  PT2(JL) = PT2(JL)+Z2M*RD*PAPHMS5(JL)*(Z4X5(JL)**2) &
   & * (1.0_JPRB+RETV*ZZQM15(JL))*ZAPH2M
  ZZQM1(JL) = ZZQM1(JL)+Z2M*RD*PAPHMS5(JL)*(Z4X5(JL)**2) &
   & * RETV*PT25(JL)*ZAPH2M
ENDDO

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

!*       3.   COMPUTATION OF WIND COMPONENT AND TEMPERATURE
!             ----------------------------------------------

DO JL=KIDIA,KFDIA
  ZCPT2M= 0.0_JPRB
  Z2QIV = 0.0_JPRB
  Z2SIV = 0.0_JPRB
  Z2T   = 0.0_JPRB
  ZLOGQ = 0.0_JPRB
  ZCORQ = 0.0_JPRB
  ZBQ   = 0.0_JPRB
  Z1T   = 0.0_JPRB
  ZLOGS = 0.0_JPRB
  ZCORS = 0.0_JPRB
  ZBH   = 0.0_JPRB
  Z10UIV= 0.0_JPRB
  Z10NIV= 0.0_JPRB
  Z9S   = 0.0_JPRB
  Z9N   = 0.0_JPRB
  ZLOGU = 0.0_JPRB
  ZCORU = 0.0_JPRB
  ZBD   = 0.0_JPRB
  ZRS   = 0.0_JPRB
  Z6EXP = 0.0_JPRB
  ZBNQ  = 0.0_JPRB
  Z5EXP = 0.0_JPRB
  ZBNH  = 0.0_JPRB
  ZRU   = 0.0_JPRB
  Z4EXP = 0.0_JPRB
  ZBN   = 0.0_JPRB
  Z3EXP = 0.0_JPRB
  Z2EXP = 0.0_JPRB
  Z1EXP = 0.0_JPRB
  Z8S   = 0.0_JPRB
  Z7S   = 0.0_JPRB

!     APPROXIMATE MOISTURE CORRECTION IN CP WITH QNLEV  

  ZCPT2M = ZCPT2M+PT2(JL)/( RCPD*(1.0_JPRB+RVTMP2*ZZQM15(JL)) )
  ZZQM1(JL) = ZZQM1(JL)-(ZCPT2M5(JL)-RG*Z2M)*RVTMP2*PT2(JL) &
   & / ( RCPD*(1.0_JPRB+RVTMP2*ZZQM15(JL))**2 )
  PT2(JL) = 0.0_JPRB

!     2M TEMPERATURE AND 2M SPECIFIC HUMIDITY

  PQSAM(JL) = PQSAM(JL)+PQ2(JL)
  Z2QIV = Z2QIV+(ZZQM15(JL)-PQSAM5(JL))*PQ2(JL)
  ZZQM1(JL) = ZZQM1(JL)+Z2QIV5(JL)*PQ2(JL)
  PQSAM(JL) = PQSAM(JL)-Z2QI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)*(Z2EXP5(JL)-1.0_JPRB))
  ZBNH = ZBNH+Z2EXP5(JL)*Z2EXP

  ZRU = ZRU+(Z1EXP5(JL)-1.0_JPRB)*ZLOGU &
   & / (1.0_JPRB+ZRU5(JL)*(Z1EXP5(JL)-1.0_JPRB))
  Z1EXP = Z1EXP+ZRU5(JL)*ZLOGU &
   & / (1.0_JPRB+ZRU5(JL)*(Z1EXP5(JL)-1.0_JPRB))
  ZBN = ZBN+Z1EXP5(JL)*Z1EXP

  ZNLEV(JL) = ZNLEV(JL)-Z2M*ZRS/ZNLEV5(JL)**2
  ZNLEV(JL) = ZNLEV(JL)-Z10M*ZRU/ZNLEV5(JL)**2

  Z8S = Z8S+RKAP*ZBQ/ZCFQ5(JL)
  ZCFQ(JL) = ZCFQ(JL)-RKAP*Z8S5(JL)*ZBQ/ZCFQ5(JL)**2
  Z8S = Z8S+RKAP*ZBH/ZCFH5(JL)
  ZCFH(JL) = ZCFH(JL)-RKAP*Z8S5(JL)*ZBH/ZCFH5(JL)**2
  Z8S = Z8S-RKAP*ZBD/Z8S5(JL)**2
  ZCFM(JL) = ZCFM(JL)+0.5*Z8S/Z8S5(JL)
  Z7S = Z7S+RKAP*ZBNQ/ZCDNQ5(JL)
  ZCDNQ(JL) = ZCDNQ(JL)-RKAP*Z7S5(JL)*ZBNQ/ZCDNQ5(JL)**2
  Z7S = Z7S+RKAP*ZBNH/ZCDNH5(JL)
  ZCDNH(JL) = ZCDNH(JL)-RKAP*Z7S5(JL)*ZBNH/ZCDNH5(JL)**2
  Z7S = Z7S-RKAP*ZBN/Z7S5(JL)**2
  ZCDNM(JL) = ZCDNM(JL)+0.5*Z7S/Z7S5(JL)
ENDDO


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

!*       2.   COMPUTATION OF SURFACE EXCHANGE COEFFICIENTS
!             ---------------------------------------------

DO JL=KIDIA,KFDIA
  ZCQ   = 0.0_JPRB
  Z6S   = 0.0_JPRB
  ZCH   = 0.0_JPRB
  ZCM   = 0.0_JPRB
  ZM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((PBUOM5(JL)*ZIPBL)**(ZCON2-1))*ZWST2
  ENDIF
ENDDO

IF (LHOOK) CALL DR_HOOK('SPPCFLSAD_MOD:SPPCFLSAD',1,ZHOOK_HANDLE)
END SUBROUTINE SPPCFLSAD
END MODULE SPPCFLSAD_MOD
