SUBROUTINE LWVBAD &
 & ( KIDIA , KFDIA , KLON  , KLEV  , KUAER,&
 & PGASUR, PGBSUR, PGATOP, PGBTOP,&
 & PABCU5 , PADJD5 , PADJU5,&
 & PB5    , PBINT5 , PBSUR5 , PBTOP5,&
 & PDISD5 , PDISU5 , PEMIS5 , PEMIW5,&
 & PDWFSU5, PFLUC5 ,                 &
 & PABCU  , PADJD  , PADJU  ,&
 & PB     , PBINT  , PBSUR  , PBTOP ,&
 & PDISD  , PDISU  , PEMIS  , PEMIW ,&
 & PDWFSU , PFLUC  )              

!**** *LWVBAD*   - L.W., VERTICAL INTEGRATION, EXCHANGE WITH BOUNDARIES
!                      (adjoint)

!     PURPOSE.
!     --------
!           INTRODUCES THE EFFECTS OF THE BOUNDARIES IN THE VERTICAL
!           INTEGRATION

!**   INTERFACE.
!     ----------

!        EXPLICIT ARGUMENTS :
!        --------------------
!     ==== INPUTS ===
!  Trajectory  Perturbation  Description                         Dimension
!  PABCU5      PABCU         ABSORBER AMOUNTS               (KLON,NUA,3*KLEV+1)
!  PADJD5      PADJD         CONTRIBUTION BY ADJACENT LAYERS      (KLON,KLEV+1)
!  PADJU5      PADJU         CONTRIBUTION BY ADJACENT LAYERS      (KLON,KLEV+1)
!  PB5         PB            SPECTRAL HALF-LEVEL PLANCK 
!                            FUNCTIONS                       (KLON,NSIL,KLEV+1)
!  PBINT5      PBINT         HALF-LEVEL PLANCK FUNCTIONS          (KLON,KLEV+1)
!  PBSUR5      PBSUR         SPECTRAL SURFACE PLANCK FUNCTION     (KLON,NSIL)
!  PBTOP5      PBTOP         SPECTRAL T.O.A. PLANCK FUNCTION      (KLON,NSIL)
!  PDISD5      PDISD         CONTRIBUTION BY DISTANT LAYERS       (KLON,KLEV+1)
!  PDISU5      PDISU         CONTRIBUTION BY DISTANT LAYERS       (KLON,KLEV+1)
!  PEMIS5      PEMIS         SURFACE LW EMISSIVITY                (KLON)
!  PEMIW5      PEMIW         SURFACE LW WINDOW EMISSIVITY         (KLON)
!  PGASUR      ---           SURFACE PADE APPROXIMANTS            (KLON,NIPD,2)
!  PGBSUR      ---           SURFACE PADE APPROXIMANTS            (KLON,NIPD,2)
!  PGATOP      ---           T.O.A. PADE APPROXIMANTS             (KLON,NIPD,2)
!  PGBTOP      ---           T.O.A. PADE APPROXIMANTS             (KLON,NIPD,2)

!     ==== OUTPUTS ===
!  Trajectory  Perturbation  Description                          Dimension
!  PDWFSU5     PDWFSU        SPECTRAL DOWNWARD FLUX AT SURFACE    (KLON,NSIL)
!  PFLUC5      PFLUC         RADIATIVE FLUXES CLEAR-SKY:          (KLON,2,KLEV)
!                    1  ==>  UPWARD   FLUX TOTAL

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

!     METHOD.
!     -------

!          COMPUTES THE ENERGY EXCHANGE WITH TOP AND SURFACE OF THE
!     ATMOSPHERE AND ADDS UP ALL CONTRIBUTIONS TO GET THE CLEAR-SKY 
!     FLUXES

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

!          *LWTTAD*

!     REFERENCE.
!     ----------

!        SEE RADIATION'S PART OF THE MODEL'S DOCUMENTATION AND
!        ECMWF RESEARCH DEPARTMENT DOCUMENTATION OF THE IFS

!     AUTHOR.
!     -------
!        M. Janiskova  *ECMWF*       6-Dec-2006

!     MODIFICATIONS.
!     --------------

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

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

USE YOELW    , ONLY : NSIL     ,NIPD     ,NTRA     ,NUA      ,NG1P1
USE YOPHNC   , ONLY : LWLOPT

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KUAER 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGASUR(KLON,NIPD,2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGBSUR(KLON,NIPD,2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGATOP(KLON,NIPD,2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGBTOP(KLON,NIPD,2) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PABCU5(KLON,NUA,3*KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PADJD5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PADJU5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PB5(KLON,NSIL,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBINT5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBSUR5(KLON,NSIL) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PBTOP5(KLON,NSIL) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDISD5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDISU5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEMIS5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEMIW5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDWFSU5(KLON,NSIL) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFLUC5(KLON,2,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PABCU(KLON,NUA,3*KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PADJD(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PADJU(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PB(KLON,NSIL,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PBINT(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PBSUR(KLON,NSIL) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PBTOP(KLON,NSIL) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDISD(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDISU(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PEMIS(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PEMIW(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDWFSU(KLON,NSIL) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFLUC(KLON,2,KLEV+1) 

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

!*       0.1   ARGUMENTS
!              ---------

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

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

REAL(KIND=JPRB) :: ZTTT5(KLON,NTRA,KLEV+1),ZTTS5(KLON,NTRA,KLEV+1)
REAL(KIND=JPRB) :: ZUUT5(KLON,NUA,KLEV+1),ZUUS5(KLON,NUA,KLEV+1)
REAL(KIND=JPRB) :: ZBSUR5(KLON,NSIL)
REAL(KIND=JPRB) :: ZCNSOL5(KLON) , ZCNTOPS5(KLON) 
REAL(KIND=JPRB) :: ZBSUR(KLON,NSIL), ZTT(KLON,NTRA)
REAL(KIND=JPRB) :: ZUU(KLON,NUA) , ZCNSOL(KLON)   , ZCNTOPS(KLON) 

REAL(KIND=JPRB) :: ZCNTOP15, ZCNTOP25, ZCNTOP35
REAL(KIND=JPRB) :: ZCNTOP45, ZCNTOP55, ZCNTOP65
REAL(KIND=JPRB) :: ZCNTOP1, ZCNTOP2, ZCNTOP3, ZCNTOP4, ZCNTOP5, ZCNTOP6

INTEGER(KIND=JPIM) :: IN, JA, JK, JL

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "lwtt.intfb.h"
#include "lwttad.intfb.h"

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

!*         1.    INITIALIZATION
!                --------------

!*         1.2     INITIALIZE TRANSMISSION FUNCTIONS
!                  ---------------------------------

IF (LHOOK) CALL DR_HOOK('LWVBAD',0,ZHOOK_HANDLE)
DO JK = 1, KLEV+1
  DO JA=1,NTRA
    DO JL=KIDIA,KFDIA
      ZTTT5(JL,JA,JK) = 1.0_JPRB
      ZTTS5(JL,JA,JK) = 1.0_JPRB
    ENDDO
  ENDDO
ENDDO

DO JK = 1, KLEV+1
  DO JA=1,NUA
    DO JL=KIDIA,KFDIA
      ZUUT5(JL,JA,JK) = 1.0_JPRB
      ZUUS5(JL,JA,JK) = 1.0_JPRB
    ENDDO
  ENDDO
ENDDO

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

!*         2.      VERTICAL INTEGRATION
!                  --------------------

!*         2.3     EXCHANGE WITH TOP OF THE ATMOSPHERE
!                  -----------------------------------

DO JK = 1 , KLEV
  IN=(JK-1)*NG1P1+1

  DO JA=1,KUAER
    DO JL=KIDIA,KFDIA
      ZUUT5(JL,JA,JK) = PABCU5(JL,JA,IN)
    ENDDO
  ENDDO

  CALL LWTT &
   & ( KIDIA        , KFDIA        , KLON,&
   & PGATOP(1,1,1), PGBTOP(1,1,1),&
   & ZUUT5(1,1,JK), ZTTT5(1,1,JK) )

  IF (.NOT. LWLOPT) THEN
    DO JL = KIDIA,KFDIA
      ZCNTOP15 = PBTOP5(JL,1)*ZTTT5(JL,1,JK)*ZTTT5(JL,10,JK)
      ZCNTOP25 = PBTOP5(JL,2)*ZTTT5(JL,2,JK)*ZTTT5(JL,7,JK)*ZTTT5(JL,11,JK)
      ZCNTOP35 = PBTOP5(JL,3)*ZTTT5(JL,4,JK)*ZTTT5(JL,8,JK)*ZTTT5(JL,12,JK)
      ZCNTOP45 = PBTOP5(JL,4)*ZTTT5(JL,5,JK)*ZTTT5(JL,9,JK)*ZTTT5(JL,13,JK)
      ZCNTOP55 = PBTOP5(JL,5)*ZTTT5(JL,3,JK)*ZTTT5(JL,14,JK)
      ZCNTOP65 = PBTOP5(JL,6)*ZTTT5(JL,6,JK)*ZTTT5(JL,15,JK)
      ZCNTOPS5(JL) = ZCNTOP15+ZCNTOP25+ZCNTOP35+ZCNTOP45+ZCNTOP55+ZCNTOP65
      PFLUC5(JL,2,JK)=ZCNTOPS5(JL)-PBINT5(JL,JK)-PDISD5(JL,JK)-PADJD5(JL,JK)
      IF (JK == 1) THEN
        PDWFSU5(JL,1) = ZCNTOP15-PB5(JL,1,JK)-PDWFSU5(JL,1)
        PDWFSU5(JL,2) = ZCNTOP25-PB5(JL,2,JK)-PDWFSU5(JL,2)
        PDWFSU5(JL,3) = ZCNTOP35-PB5(JL,3,JK)-PDWFSU5(JL,3)
        PDWFSU5(JL,4) = ZCNTOP45-PB5(JL,4,JK)-PDWFSU5(JL,4)
        PDWFSU5(JL,5) = ZCNTOP55-PB5(JL,5,JK)-PDWFSU5(JL,5)
        PDWFSU5(JL,6) = ZCNTOP65-PB5(JL,6,JK)-PDWFSU5(JL,6)
      ENDIF
    ENDDO
  ENDIF

ENDDO

JK = KLEV+1
IN=(JK-1)*NG1P1+1

IF (.NOT. LWLOPT) THEN
  DO JL = KIDIA,KFDIA
    ZCNTOPS5(JL) = PBTOP5(JL,1) + PBTOP5(JL,2) + PBTOP5(JL,3)&
     &           + PBTOP5(JL,4) + PBTOP5(JL,5) + PBTOP5(JL,6) 
    PFLUC5(JL,2,JK) = ZCNTOPS5(JL)-PBINT5(JL,JK)-PDISD5(JL,JK)-PADJD5(JL,JK)
  ENDDO
ENDIF

!*         2.5     EXCHANGE WITH LOWER LIMIT
!                  -------------------------

JK = 1
IN=(JK-1)*NG1P1+1

DO JL = KIDIA,KFDIA
  ZBSUR5(JL,1) = PBSUR5(JL,1)*PEMIS5(JL) &
   & -(1.0_JPRB-PEMIS5(JL))*PDWFSU5(JL,1)
  ZBSUR5(JL,2) = PBSUR5(JL,2)*PEMIS5(JL) &
   & -(1.0_JPRB-PEMIS5(JL))*PDWFSU5(JL,2)
  ZBSUR5(JL,3) = PBSUR5(JL,3)*PEMIW5(JL) &
   & -(1.0_JPRB-PEMIW5(JL))*PDWFSU5(JL,3)
  ZBSUR5(JL,4) = PBSUR5(JL,4)*PEMIW5(JL) &
   & -(1.0_JPRB-PEMIW5(JL))*PDWFSU5(JL,4)
  ZBSUR5(JL,5) = PBSUR5(JL,5)*PEMIS5(JL) &
   & -(1.0_JPRB-PEMIS5(JL))*PDWFSU5(JL,5)  
  ZBSUR5(JL,6) = PBSUR5(JL,6)*PEMIS5(JL) &
   & -(1.0_JPRB-PEMIS5(JL))*PDWFSU5(JL,6)
  PFLUC5(JL,1,JK) = ZBSUR5(JL,1) + ZBSUR5(JL,2) + ZBSUR5(JL,3)&
   &              + ZBSUR5(JL,4) + ZBSUR5(JL,5) + ZBSUR5(JL,6)  

  ZBSUR5(JL,1) = ZBSUR5(JL,1)-PB5(JL,1,1) 
  ZBSUR5(JL,2) = ZBSUR5(JL,2)-PB5(JL,2,1)
  ZBSUR5(JL,3) = ZBSUR5(JL,3)-PB5(JL,3,1)
  ZBSUR5(JL,4) = ZBSUR5(JL,4)-PB5(JL,4,1)
  ZBSUR5(JL,5) = ZBSUR5(JL,5)-PB5(JL,5,1)
  ZBSUR5(JL,6) = ZBSUR5(JL,6)-PB5(JL,6,1)
ENDDO

DO JK = 2 , KLEV+1
  IN=(JK-1)*NG1P1+1

  DO JA=1,KUAER
    DO JL=KIDIA,KFDIA
      ZUUS5(JL,JA,JK) = PABCU5(JL,JA,1)-PABCU5(JL,JA,IN)
    ENDDO
  ENDDO

  CALL LWTT &
   & ( KIDIA        , KFDIA        , KLON,&
   & PGASUR(1,1,1), PGBSUR(1,1,1),&
   & ZUUS5(1,1,JK), ZTTS5(1,1,JK) )

  IF (.NOT. LWLOPT) THEN
    DO JL = KIDIA,KFDIA
      ZCNSOL5(JL) = ZBSUR5(JL,1)*ZTTS5(JL,1,JK)*ZTTS5(JL,10,JK)&
       & + ZBSUR5(JL,2)*ZTTS5(JL,2,JK)*ZTTS5(JL,7,JK)*ZTTS5(JL,11,JK)&
       & + ZBSUR5(JL,3)*ZTTS5(JL,4,JK)*ZTTS5(JL,8,JK)*ZTTS5(JL,12,JK)&
       & + ZBSUR5(JL,4)*ZTTS5(JL,5,JK)*ZTTS5(JL,9,JK)*ZTTS5(JL,13,JK)&
       & + ZBSUR5(JL,5)*ZTTS5(JL,3,JK)*ZTTS5(JL,14,JK)&
       & + ZBSUR5(JL,6)*ZTTS5(JL,6,JK)*ZTTS5(JL,15,JK)  
      PFLUC5(JL,1,JK) = ZCNSOL5(JL)+PBINT5(JL,JK)-PDISU5(JL,JK)-PADJU5(JL,JK)
    ENDDO
  ENDIF

ENDDO

!*         0.    ADJOINT CALCULATIONS
!                --------------------

!* Set local variables to zero

ZTT(:,:) = 0.0_JPRB
ZUU(:,:) = 0.0_JPRB
ZBSUR(:,:) = 0.0_JPRB
ZCNSOL(:)  = 0.0_JPRB
ZCNTOPS(:) = 0.0_JPRB


!*         1.2.    VERTICAL INTEGRATION
!                  --------------------

!*         1.2.5     EXCHANGE WITH LOWER LIMIT
!                    -------------------------

DO JK = KLEV+1 , 2 , -1
  IN=(JK-1)*NG1P1+1

  DO JL = KIDIA,KFDIA
    ZCNSOL(JL) = ZCNSOL(JL)+PFLUC(JL,1,JK)
    PBINT(JL,JK) = PBINT(JL,JK)+PFLUC(JL,1,JK)
    PDISU(JL,JK) = PDISU(JL,JK)-PFLUC(JL,1,JK)
    PADJU(JL,JK) = PADJU(JL,JK)-PFLUC(JL,1,JK)
    PFLUC(JL,1,JK) = 0.0_JPRB
    ZTT(JL,10) = ZTT(JL,10)+ZBSUR5(JL,1)*ZTTS5(JL,1,JK)*ZCNSOL(JL)
    ZTT(JL,1) = ZTT(JL,1)+ZBSUR5(JL,1)*ZTTS5(JL,10,JK)*ZCNSOL(JL)
    ZBSUR(JL,1) = ZBSUR(JL,1)+ZTTS5(JL,1,JK)*ZTTS5(JL,10,JK)*ZCNSOL(JL)
    ZTT(JL,11) = ZTT(JL,11) &
     & + ZBSUR5(JL,2)*ZTTS5(JL,2,JK)*ZTTS5(JL,7,JK)*ZCNSOL(JL)
    ZTT(JL,7) = ZTT(JL,7) &
     & + ZBSUR5(JL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(JL,14,JK)*ZCNSOL(JL)
    ZBSUR(JL,5) = ZBSUR(JL,5)+ZTTS5(JL,3,JK)*ZTTS5(JL,14,JK)*ZCNSOL(JL)
    ZTT(JL,15) = ZTT(JL,15)+ZBSUR5(JL,6)*ZTTS5(JL,6,JK)*ZCNSOL(JL)
    ZTT(JL,6) = ZTT(JL,6)+ZBSUR5(JL,6)*ZTTS5(JL,15,JK)*ZCNSOL(JL)
    ZBSUR(JL,6) = ZBSUR(JL,6)+ZTTS5(JL,6,JK)*ZTTS5(JL,15,JK)*ZCNSOL(JL)
    ZCNSOL(JL) = 0.0_JPRB
  ENDDO

  CALL LWTTAD &
   & ( KIDIA        , KFDIA        , KLON,&
   & PGASUR(1,1,1), PGBSUR(1,1,1),&
   & ZUUS5(1,1,JK), ZTTS5(1,1,JK), ZUU, ZTT &
   & )

  DO JA=1,KUAER
    DO JL=KIDIA,KFDIA
      PABCU (JL,JA,1) = PABCU (JL,JA,1)+ZUU(JL,JA)
      PABCU(JL,JA,IN) = PABCU(JL,JA,IN)-ZUU(JL,JA)
      ZUU(JL,JA) = 0.0_JPRB
    ENDDO
  ENDDO
ENDDO

JK = 1
IN=(JK-1)*NG1P1+1

DO JL = KIDIA,KFDIA
  PB(JL,6,1) = PB(JL,6,1)-ZBSUR(JL,6)
  PB(JL,5,1) = PB(JL,5,1)-ZBSUR(JL,5)
  PB(JL,4,1) = PB(JL,4,1)-ZBSUR(JL,4)
  PB(JL,3,1) = PB(JL,3,1)-ZBSUR(JL,3)
  PB(JL,2,1) = PB(JL,2,1)-ZBSUR(JL,2)
  PB(JL,1,1) = PB(JL,1,1)-ZBSUR(JL,1)
  ZBSUR(JL,1) = ZBSUR(JL,1)+PFLUC(JL,1,JK)
  ZBSUR(JL,2) = ZBSUR(JL,2)+PFLUC(JL,1,JK)
  ZBSUR(JL,3) = ZBSUR(JL,3)+PFLUC(JL,1,JK)
  ZBSUR(JL,4) = ZBSUR(JL,4)+PFLUC(JL,1,JK)
  ZBSUR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-(1.0_JPRB-PEMIS5(JL))*ZBSUR(JL,2)
  ZBSUR(JL,2) = 0.0_JPRB
  PEMIS(JL) = PEMIS(JL)+(PBSUR5(JL,1)+PDWFSU5(JL,1))*ZBSUR(JL,1)
  PBSUR(JL,1) = PBSUR(JL,1)+PEMIS5(JL)*ZBSUR(JL,1)
  PDWFSU(JL,1) = PDWFSU(JL,1)-(1.0_JPRB-PEMIS5(JL))*ZBSUR(JL,1)
  ZBSUR(JL,1) = 0.0_JPRB
ENDDO

!*         1.2.3     EXCHANGE WITH TOP OF THE ATMOSPHERE
!                    -----------------------------------

JK = KLEV+1
IN=(JK-1)*NG1P1+1

DO JL = KIDIA,KFDIA
  ZCNTOPS(JL) = ZCNTOPS(JL)+PFLUC(JL,2,JK)
  PBINT(JL,JK) = PBINT(JL,JK)-PFLUC(JL,2,JK)
  PDISD(JL,JK) = PDISD(JL,JK)-PFLUC(JL,2,JK)
  PADJD(JL,JK) = PADJD(JL,JK)-PFLUC(JL,2,JK)
  PFLUC(JL,2,JK) = 0.0_JPRB
  PBTOP(JL,1) = PBTOP(JL,1)+ZCNTOPS(JL)
  PBTOP(JL,2) = PBTOP(JL,2)+ZCNTOPS(JL)
  PBTOP(JL,3) = PBTOP(JL,3)+ZCNTOPS(JL)
  PBTOP(JL,4) = PBTOP(JL,4)+ZCNTOPS(JL)
  PBTOP(JL,5) = PBTOP(JL,5)+ZCNTOPS(JL)
  PBTOP(JL,6) = PBTOP(JL,6)+ZCNTOPS(JL)
  ZCNTOPS(JL) = 0.0_JPRB
ENDDO

DO JK = KLEV , 1 , -1
  IN=(JK-1)*NG1P1+1

  DO JL = KIDIA,KFDIA
    ZCNTOP1 = 0.0_JPRB
    ZCNTOP2 = 0.0_JPRB
    ZCNTOP3 = 0.0_JPRB
    ZCNTOP4 = 0.0_JPRB
    ZCNTOP5 = 0.0_JPRB
    ZCNTOP6 = 0.0_JPRB
    
    IF (JK == 1) THEN
      ZCNTOP6 = ZCNTOP6+PDWFSU(JL,6)
      PB(JL,6,JK) = PB(JL,6,JK)-PDWFSU(JL,6)
      PDWFSU(JL,6) = -PDWFSU(JL,6)
      ZCNTOP5 = ZCNTOP5+PDWFSU(JL,5)
      PB(JL,5,JK) = PB(JL,5,JK)-PDWFSU(JL,5)
      PDWFSU(JL,5) = -PDWFSU(JL,5)
      ZCNTOP4 = ZCNTOP4+PDWFSU(JL,4)
      PB(JL,4,JK) = PB(JL,4,JK)-PDWFSU(JL,4)
      PDWFSU(JL,4) = -PDWFSU(JL,4)
      ZCNTOP3 = ZCNTOP3+PDWFSU(JL,3)
      PB(JL,3,JK) = PB(JL,3,JK)-PDWFSU(JL,3)
      PDWFSU(JL,3) = -PDWFSU(JL,3)
      ZCNTOP2 = ZCNTOP2+PDWFSU(JL,2)
      PB(JL,2,JK) = PB(JL,2,JK)-PDWFSU(JL,2)
      PDWFSU(JL,2) = -PDWFSU(JL,2)
      ZCNTOP1 = ZCNTOP1+PDWFSU(JL,1)
      PB(JL,1,JK) = PB(JL,1,JK)-PDWFSU(JL,1)
      PDWFSU(JL,1) = -PDWFSU(JL,1)
    ENDIF
    ZCNTOPS(JL) = ZCNTOPS(JL)+PFLUC(JL,2,JK)
    PBINT(JL,JK) = PBINT(JL,JK)-PFLUC(JL,2,JK)
    PDISD(JL,JK) = PDISD(JL,JK)-PFLUC(JL,2,JK)
    PADJD(JL,JK) = PADJD(JL,JK)-PFLUC(JL,2,JK)
    PFLUC(JL,2,JK) = 0.0_JPRB
    ZCNTOP1 = ZCNTOP1+ZCNTOPS(JL)
    ZCNTOP2 = ZCNTOP2+ZCNTOPS(JL)
    ZCNTOP3 = ZCNTOP3+ZCNTOPS(JL)
    ZCNTOP4 = ZCNTOP4+ZCNTOPS(JL)
    ZCNTOP5 = ZCNTOP5+ZCNTOPS(JL)
    ZCNTOP6 = ZCNTOP6+ZCNTOPS(JL)
    ZCNTOPS(JL) = 0.0_JPRB
    ZTT(JL,15) = ZTT(JL,15)+PBTOP5(JL,6)*ZTTT5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5(JL,2)*ZTTT5(JL,2,JK)*ZTTT5(JL,7,JK)*ZCNTOP2
    ZTT(JL,7) = ZTT(JL,7) &
     & + PBTOP5(JL,2)*ZTTT5(JL,2,JK)*ZTTT5(JL,11,JK)*ZCNTOP2
    ZTT(JL,2) = ZTT(JL,2) &
     & + PBTOP5(JL,2)*ZTTT5(JL,7,JK)*ZTTT5(JL,11,JK)*ZCNTOP2
    PBTOP(JL,2) = PBTOP(JL,2) &
     & + ZTTT5(JL,2,JK)*ZTTT5(JL,7,JK)*ZTTT5(JL,11,JK)*ZCNTOP2
    ZTT(JL,10) = ZTT(JL,10)+PBTOP5(JL,1)*ZTTT5(JL,1,JK)*ZCNTOP1
    ZTT(JL,1) = ZTT(JL,1)+PBTOP5(JL,1)*ZTTT5(JL,10,JK)*ZCNTOP1
    PBTOP(JL,1) = PBTOP(JL,1)+ZTTT5(JL,1,JK)*ZTTT5(JL,10,JK)*ZCNTOP1
  ENDDO

  CALL LWTTAD &
   & ( KIDIA        , KFDIA        , KLON,&
   & PGATOP(1,1,1), PGBTOP(1,1,1),&
   & ZUUT5(1,1,JK), ZTTT5(1,1,JK), ZUU, ZTT &
   & )

  DO JA=1,KUAER
    DO JL=KIDIA,KFDIA
      PABCU(JL,JA,IN) = PABCU(JL,JA,IN)+ZUU(JL,JA)
      ZUU(JL,JA) = 0.0_JPRB
    ENDDO
  ENDDO

ENDDO

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

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