!OPTIONS XOPT(HSFUN)
SUBROUTINE GWSETUPTL &
 & ( KIDIA , KFDIA , KLON , KLEV , LDGWD,&
 & KKCRITH,KCRIT,&
 & KKENVH, KKNU   ,KKNU2,&
 & PAPHM15,PAPM15 ,PUM15  , PVM15, PTM15 ,PGEOM15, PHSTD,&
 & PRHO5  ,PRI5   ,PSTAB5 , PTAU5, PVPH5 ,PPSI5, PZDEP5,&
 & PULOW5 ,PVLOW5,&
 & PTHETA, PGAMMA, PNU5   , PD15 , PD25  ,PDMOD5,&
 & PAPHM1, PAPM1 , PUM1   , PVM1 , PTM1  ,PGEOM1,&
 & PRHO  , PRI   , PSTAB  , PTAU , PVPH  ,PPSI, PZDEP,&
 & PULOW , PVLOW,&
 & PD1  ,  PD2  ,PDMOD         )  

!**** *GWSETUPTL*  (Tangent linear)

!     J.-F. MAHFOUF           E.C.M.W.F.               07/05/96

!     PURPOSE.
!     --------

!**   INTERFACE.
!     ----------
!          FROM *GWDRAGTL*

!        EXPLICIT ARGUMENTS :
!        --------------------

!     ==== INPUTS ===

!        *KIDIA*       START POINT
!        *KFDIA*       END POINT
!        *KLON*        NUMBER OF GRID POINTS PER PACKET
!        *LDGWD*       TRUE IF SSO ACTIVE
!        *PAPHM15      PRESSURE AT HALF LEVELS AT T-1   (Trajectory)
!        *PAPM15       PRESSURE AT FULL LEVELS AT T-1   (Trajectory)
!        *PUM15*       X-VELOCITY COMPONENT AT T-1      (Trajectory)
!        *PVM15*       Y-VELOCITY COMPONENT AT T-1      (Trajectory)
!        *PTM15*       TEMPERATURE AT T-1               (Trajectory)
!        *PGEOM15*     GEOPOTENTIAL AT T-1              (Trajectory)
!        *PAPHM1*      PRESSURE AT HALF LEVELS AT T-1
!        *PAPM1*       PRESSURE AT FULL LEVELS AT T-1
!        *PUM1*        X-VELOCITY COMPONENT AT T-1
!        *PVM1*        Y-VELOCITY COMPONENT AT T-1
!        *PTM1*        TEMPERATURE AT T-1
!        *PGEOM1*      GEOPOTENTIAL AT T-1
!        *PHSTD*       STANDARD DEVIATION OF THE SUBGRID SCALE OROGRAPHY
!        *PTHETA*      GEOGRAPHICAL ORIENTATION 
!                      OF THE SUBGRID SCALE OROGRAPHY
!        *PGAMMA*      ANISOTROPY OF THE SUBGRID SCALE OROGRAPHY

!     ==== OUTPUTS ===

!        *KKCRITH*     MAXIMUM HALF-LEVEL FOR WAVE BREAKING
!        *KCRIT*       CRITICAL LEVEL
!        *KKENVH*      HALF-LEVEL FOR BLOCKING (TOP OF ENVELOPE LAYER)
!        *KKNU*        MODEL LEVEL AT 4*PHSTD HEIGHT 
!        *KKNU2*       MODEL LEVEL AT 3*PHSTD HEIGHT
!        *PRHO5*       AIR DENSITY                           (Trajectory)
!        *PRI5*        MEAN FLOW RICHARDSON NUMBER           (Trajectory)
!        *PSTAB5*      SQUARE OF THE BRUNT-VAISALA FREQUENCY (Trajectory)
!        *PTAU5*       STRESS PRODUCED BY GRAVITY WAVES      (Trajectory)
!        *PVPH5*       WIND PROJECTION ALONG SURFACE STRESS 
!                      AT HALF LEVELS                        (Trajectory)
!        *PPSI5*       ANGLE BETWEEN THE LOW-LEVEL WIND DIRECTION
!                      AND THE PRINCIPAL AXIS OF TOPOGRAPHY  (Trajectory)
!        *PZDEP5*      VERTICAL LEAKINESS (LENGTH OF THE MOUNTAIN SEEN
!                      BY THE FLOW)                          (Trajectory)
!        *PULOW5*      LOW-LEVEL BLOCKED FLOW X-COMPONENT    (Trajectory)
!        *PVLOW5*      LOW-LEVEL BLOCKED FLOW Y-COMPONENT    (Trajectory)
!        *PNU5*        NON-DIMENSIONAL HEIGHT                (Trajectory)
!        *PD15*        X-DIRECTION OF THE SURFACE STRESS     (Trajectory)
!        *PD25*        Y-DIRECTION OF THE SURFACE STRESS     (Trajectory)
!        *PDMOD5*      MODULUS OF THE SURFACE STRESS (PART)  (Trajectory)
!        *PRHO*        AIR DENSITY
!        *PRI*         MEAN FLOW RICHARDSON NUMBER           
!        *PSTAB*       SQUARE OF THE BRUNT-VAISALA FREQUENCY
!        *PTAU*        STRESS PRODUCED BY GRAVITY WAVES
!        *PVPH*        WIND PROJECTION ALONG SURFACE STRESS 
!                      AT HALF LEVELS 
!        *PPSI*        ANGLE BETWEEN THE LOW-LEVEL WIND DIRECTION
!                      AND THE PRINCIPAL AXIS OF TOPOGRAPHY 
!        *PZDEP*       VERTICAL LEAKINESS (LENGTH OF THE MOUNTAIN SEEN
!                      BY THE FLOW) 
!        *PULOW*       LOW-LEVEL BLOCKED FLOW X-COMPONENT
!        *PVLOW*       LOW-LEVEL BLOCKED FLOW Y-COMPONENT
!        *PD1*         X-DIRECTION OF THE SURFACE STRESS
!        *PD2*         Y-DIRECTION OF THE SURFACE STRESS
!        *PDMOD*       MODULUS OF THE SURFACE STRESS (PART)

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

!     METHOD.
!     -------

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

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

!        SEE ECMWF RESEARCH DEPARTMENT DOCUMENTATION OF THE "I.F.S."

!     AUTHOR.
!     -------
!     M.MILLER

!     MODIFICATIONS.
!     --------------
!     F.LOTT  FOR THE NEW-GWDRAG SCHEME NOVEMBER 1993
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

USE YOEGWD   , ONLY : NKTOPG   ,GFRCRIT  ,GRCRIT   ,GSSEC    ,GTSEC    ,GVSEC
USE YOMCST   , ONLY : RG       ,RD       ,RCPD

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 
LOGICAL           ,INTENT(IN)    :: LDGWD(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KKCRITH(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KCRIT(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KKENVH(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KKNU(KLON) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KKNU2(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHM15(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHSTD(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRHO5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRI5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSTAB5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTAU5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVPH5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PPSI5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PZDEP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PULOW5(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVLOW5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTHETA(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PGAMMA(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PNU5(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PD15(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PD25(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDMOD5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHM1(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRHO(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRI(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSTAB(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PTAU(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVPH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PPSI(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PZDEP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PULOW(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PVLOW(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PD1(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PD2(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDMOD(KLON) 
!-----------------------------------------------------------------------

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

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

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

LOGICAL :: LL1(KLON,KLEV+1)
INTEGER(KIND=JPIM) :: IKNUB(KLON),IKNUL(KLON)

REAL(KIND=JPRB) :: ZVPF5(KLON,KLEV),ZDP5(KLON,KLEV),ZSQST5(KLON,KLEV)
REAL(KIND=JPRB) :: ZNORM5(KLON),&
 & ZULOW5(KLON),ZVLOW5(KLON),ZNUP5(KLON),ZNUM5(KLON)  

REAL(KIND=JPRB) :: ZHCRIT(KLON,KLEV),ZVPF(KLON,KLEV),ZDP(KLON,KLEV)
REAL(KIND=JPRB) :: ZNORM(KLON),ZB(KLON),ZC(KLON),ZULOW(KLON),ZVLOW(KLON)

INTEGER(KIND=JPIM) :: ILEVH, JK, JL

LOGICAL :: LLO

REAL(KIND=JPRB) :: ZCONS1, ZCONS2, ZDELP5, ZDWIND, ZDWIND5,&
 & ZGGEENV, ZGGEENV5, ZGGEOM1, ZGGEOM15, ZGVAR, &
 & ZHGEO, ZPHI, ZPHI5, ZRHOM5, ZRHOP5, ZS1, &
 & ZS10, ZS105, ZS15, ZS2, ZS25, ZS3, ZS35, &
 & ZS4, ZS45, ZS55, ZS65, ZS75, ZS85, ZS9, ZS95, &
 & ZST, ZST5, ZSTABM5, ZSTABP5, ZU, ZU5, ZVT1, &
 & ZVT15, ZVT2, ZVT25, ZWIND5  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

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

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

!*         1.1   COMPUTATIONAL CONSTANTS
!                -----------------------

IF (LHOOK) CALL DR_HOOK('GWSETUPTL',0,ZHOOK_HANDLE)
ILEVH =KLEV/3

ZCONS1=1.0_JPRB/RD
ZCONS2=RG**2/RCPD

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

!*         2.
!                --------------

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

!*         2.1     DEFINE LOW LEVEL WIND, PROJECT WINDS IN PLANE OF
!*                 LOW LEVEL WIND AND SET INDICATOR FOR CRITICAL LEVELS.

DO JL=KIDIA,KFDIA
  KKNU(JL)      =KLEV
  KKNU2(JL)     =KLEV
  IKNUB(JL)     =KLEV
  IKNUL(JL)     =KLEV
  PGAMMA(JL)    =MAX(PGAMMA(JL),GTSEC)
  LL1(JL,KLEV+1)=.FALSE.
ENDDO

!*      DEFINE VARIOUS LEVELS IN THE FLOW
!       ----------------------------
DO JK=KLEV,ILEVH,-1
  DO JL=KIDIA,KFDIA
    ZHCRIT(JL,JK)=4._JPRB*PHSTD(JL)
    ZHGEO=PGEOM15(JL,JK)/RG
    LL1(JL,JK)=(ZHGEO > ZHCRIT(JL,JK))
    IF(LL1(JL,JK).NEQV.LL1(JL,JK+1)) THEN
      KKNU(JL)=JK
    ENDIF
  ENDDO
ENDDO
DO JK=KLEV,ILEVH,-1
  DO JL=KIDIA,KFDIA
    ZHCRIT(JL,JK)=3._JPRB*PHSTD(JL)
    ZHGEO=PGEOM15(JL,JK)/RG
    LL1(JL,JK)=(ZHGEO > ZHCRIT(JL,JK))
    IF(LL1(JL,JK).NEQV.LL1(JL,JK+1)) THEN
      KKNU2(JL)=JK
    ENDIF
  ENDDO
ENDDO
DO JK=KLEV,ILEVH,-1
  DO JL=KIDIA,KFDIA
    ZHCRIT(JL,JK)=2.0_JPRB*PHSTD(JL)
    ZHGEO=PGEOM15(JL,JK)/RG
    LL1(JL,JK)=(ZHGEO > ZHCRIT(JL,JK))
    IF(LL1(JL,JK).NEQV.LL1(JL,JK+1)) THEN
      IKNUB(JL)=JK
    ENDIF
  ENDDO
ENDDO
DO JK=KLEV,ILEVH,-1
  DO JL=KIDIA,KFDIA
    ZHCRIT(JL,JK)=PHSTD(JL)
    ZHGEO=PGEOM15(JL,JK)/RG
    LL1(JL,JK)=(ZHGEO > ZHCRIT(JL,JK))
    IF(LL1(JL,JK).NEQV.LL1(JL,JK+1)) THEN
!            IKNUL(JL)=KLEV   !BUG
      IKNUL(JL)=JK
    ENDIF
  ENDDO
ENDDO

DO JL=KIDIA,KFDIA
  KKNU(JL) =MIN(KKNU(JL),NKTOPG)
  IKNUB(JL)=MIN(IKNUB(JL),NKTOPG)
  IF(IKNUB(JL) == NKTOPG) IKNUL(JL)=KLEV
  IF(IKNUB(JL) == IKNUL(JL)) IKNUL(JL)=IKNUL(JL)+1
ENDDO

!C*     INITIALIZE VARIOUS ARRAYS

DO JL=KIDIA,KFDIA
  PTAU5(JL,KLEV+1) =0.0_JPRB
  PRHO5(JL,KLEV+1) =0.0_JPRB
  PSTAB5(JL,KLEV+1)=0.0_JPRB
  PSTAB5(JL,1)     =0.0_JPRB
  PRI5(JL,KLEV+1)  =9999.0_JPRB
  PPSI5(JL,KLEV+1) =0.0_JPRB
  PRI5(JL,1)       =0.0_JPRB
  PVPH5(JL,1)      =0.0_JPRB
  PULOW5(JL)       =0.0_JPRB
  PVLOW5(JL)       =0.0_JPRB
  ZULOW5(JL)       =0.0_JPRB
  ZVLOW5(JL)       =0.0_JPRB
  PTAU(JL,KLEV+1)  =0.0_JPRB
  PRHO(JL,KLEV+1)  =0.0_JPRB
  PSTAB(JL,KLEV+1) =0.0_JPRB
  PRI(JL,KLEV+1)   =0.0_JPRB
  PSTAB(JL,1)      =0.0_JPRB
  PPSI(JL,KLEV+1)  =0.0_JPRB
  PRI(JL,1)        =0.0_JPRB
  PVPH(JL,1)       =0.0_JPRB
  PULOW(JL)        =0.0_JPRB
  PVLOW(JL)        =0.0_JPRB
  ZULOW(JL)        =0.0_JPRB
  ZVLOW(JL)        =0.0_JPRB
  KKCRITH(JL)      =KLEV
  KKENVH(JL)       =KLEV
  KCRIT(JL)        =1
  PNU5 (JL)        =0.0_JPRB
  ZNUM5(JL)        =0.0_JPRB
  LL1(JL,KLEV+1)   =.FALSE.
ENDDO

!*     DEFINE STATIC STABILITY AND AIR DENSITY
!      ---------------------------------------

DO JK=KLEV,2,-1
  DO JL=KIDIA,KFDIA
    IF(LDGWD(JL)) THEN
      ZDP (JL,JK)=PAPM1 (JL,JK)-PAPM1 (JL,JK-1)
      ZDP5(JL,JK)=PAPM15(JL,JK)-PAPM15(JL,JK-1)
      PRHO(JL,JK)=2.0_JPRB*ZCONS1*&
       & (PAPHM1 (JL,JK)*(PTM15(JL,JK)+PTM15(JL,JK-1))-&
       & PAPHM15(JL,JK)*(PTM1 (JL,JK)+PTM1 (JL,JK-1)))/ &
       & (PTM15  (JL,JK)+ PTM15(JL,JK-1))**2  
      PRHO5(JL,JK)=2.0_JPRB*PAPHM15(JL,JK)*ZCONS1/(PTM15(JL,JK)+PTM15(JL,JK-1))
      ZS1 = -RCPD*(PRHO5(JL,JK)*(PTM1 (JL,JK)-PTM1 (JL,JK-1))+&
       & PRHO (JL,JK)*(PTM15(JL,JK)-PTM15(JL,JK-1)))/&
       & ZDP5(JL,JK) +&
       & RCPD* PRHO5(JL,JK)*(PTM15(JL,JK)-PTM15(JL,JK-1))*&
       & ZDP (JL,JK)/ZDP5(JL,JK)**2   
      ZS15=1.0_JPRB-RCPD*PRHO5(JL,JK)*(PTM15(JL,JK)-PTM15(JL,JK-1))/&
       & ZDP5(JL,JK)     
      PSTAB (JL,JK)=2.0_JPRB*ZCONS2*(ZS1 *(PTM15(JL,JK)+PTM15(JL,JK-1))-&
       & ZS15*(PTM1 (JL,JK)+PTM1 (JL,JK-1)))/&
       & (PTM15(JL,JK)+PTM15(JL,JK-1))**2  
      PSTAB5(JL,JK)=2.0_JPRB*ZCONS2*ZS15/(PTM15(JL,JK)+PTM15(JL,JK-1))
      IF (PSTAB5(JL,JK) < GSSEC) THEN
        PSTAB (JL,JK)=0.0_JPRB
        PSTAB5(JL,JK)=GSSEC
      ENDIF
      ZSQST5(JL,JK)=SQRT(PSTAB5(JL,JK))
    ENDIF
  ENDDO
ENDDO

!*         2.2     MEAN BRUNT-VAISALA FREQUENCY 
!*                 AND DENSITY FOR WAVE STRESS

DO JK=ILEVH,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDGWD(JL)) THEN
      IF(JK >= (IKNUB(JL)+1).AND.JK <= IKNUL(JL)) THEN
        ZS3 =-RCPD*(PRHO5(JL,JK)*(PTM1 (JL,JK)-PTM1 (JL,JK-1))+&
         & PRHO (JL,JK)*(PTM15(JL,JK)-PTM15(JL,JK-1)))/&
         & ZDP5 (JL,JK)+&
         & RCPD* PRHO5(JL,JK)*(PTM15(JL,JK)-PTM15(JL,JK-1))*&
         & ZDP  (JL,JK)/ZDP5(JL,JK)**2        
        ZS35=1.0_JPRB-RCPD*PRHO5(JL,JK)*(PTM15(JL,JK)-PTM15(JL,JK-1))/&
         & ZDP5(JL,JK)             
        ZST =ZCONS2*(ZS3*PTM15(JL,JK)-ZS35*PTM1(JL,JK))/PTM15(JL,JK)**2
        ZST5=ZCONS2*ZS35/PTM15(JL,JK)
        PSTAB (JL,KLEV+1)=PSTAB (JL,KLEV+1)+ZST5*ZDP (JL,JK)+ZST *ZDP5(JL,JK)
        PSTAB5(JL,KLEV+1)=PSTAB5(JL,KLEV+1)+ZST5*ZDP5(JL,JK)
        IF (PSTAB5(JL,KLEV+1) <= GSSEC) THEN
          PSTAB (JL,KLEV+1)=0.0_JPRB
          PSTAB5(JL,KLEV+1)=GSSEC
        ENDIF
        PRHO (JL,KLEV+1)=PRHO (JL,KLEV+1)+2.0_JPRB*ZCONS1*(&
         & (PAPHM1 (JL,JK)*ZDP5(JL,JK)+&
         & PAPHM15(JL,JK)*ZDP (JL,JK))*&
         & (PTM15(JL,JK)+PTM15(JL,JK-1))-&
         & PAPHM15(JL,JK)*ZDP5(JL,JK)*&
         & (PTM1 (JL,JK)+PTM1 (JL,JK-1)) )/&
         & (PTM15(JL,JK)+PTM15(JL,JK-1))**2  
        PRHO5(JL,KLEV+1)=PRHO5(JL,KLEV+1)+PAPHM15(JL,JK)*&
         & 2.0_JPRB*ZDP5(JL,JK)*&
         & ZCONS1/(PTM15(JL,JK)+PTM15(JL,JK-1))  
      ENDIF
    ENDIF
  ENDDO
ENDDO

DO JL=KIDIA,KFDIA
  PSTAB (JL,KLEV+1)=(PSTAB (JL,KLEV+1)*(PAPM15(JL,IKNUL(JL))-&
   & PAPM15(JL,IKNUB(JL)))-&
   & PSTAB5(JL,KLEV+1)*(PAPM1 (JL,IKNUL(JL))-&
   & PAPM1 (JL,IKNUB(JL))) )/&
   & (PAPM15(JL,IKNUL(JL))-&
   & PAPM15(JL,IKNUB(JL)))**2  
  PSTAB5(JL,KLEV+1)= PSTAB5(JL,KLEV+1)/(PAPM15(JL,IKNUL(JL))-&
   & PAPM15(JL,IKNUB(JL)))  
  PRHO (JL,KLEV+1)=(PRHO (JL,KLEV+1)*(PAPM15(JL,IKNUL(JL))-&
   & PAPM15(JL,IKNUB(JL)))-&
   & PRHO5(JL,KLEV+1)*(PAPM1 (JL,IKNUL(JL))-&
   & PAPM1 (JL,IKNUB(JL))) )/&
   & (PAPM15(JL,IKNUL(JL))-&
   & PAPM15(JL,IKNUB(JL)))**2  
  PRHO5(JL,KLEV+1)= PRHO5(JL,KLEV+1)/(PAPM15(JL,IKNUL(JL))-&
   & PAPM15(JL,IKNUB(JL)))  
ENDDO

!********************************************************************

!*     DEFINE LOW LEVEL FLOW FOR WAVE STRESS
!      -------------------------------------

DO JK=KLEV,ILEVH,-1
  DO JL=KIDIA,KFDIA
    IF(JK >= IKNUB(JL).AND.JK <= IKNUL(JL)) THEN
      PULOW (JL)=PULOW (JL)+&
       & PUM15(JL,JK)*(PAPHM1 (JL,JK+1)-PAPHM1 (JL,JK))+&
       & PUM1 (JL,JK)*(PAPHM15(JL,JK+1)-PAPHM15(JL,JK))  
      PULOW5(JL)=PULOW5(JL)+PUM15(JL,JK)*(PAPHM15(JL,JK+1)-PAPHM15(JL,JK))
      PVLOW (JL)=PVLOW (JL)+&
       & PVM15(JL,JK)*(PAPHM1 (JL,JK+1)-PAPHM1 (JL,JK))+&
       & PVM1 (JL,JK)*(PAPHM15(JL,JK+1)-PAPHM15(JL,JK))  
      PVLOW5(JL)=PVLOW5(JL)+PVM15(JL,JK)*(PAPHM15(JL,JK+1)-PAPHM15(JL,JK))
    ENDIF
  ENDDO
ENDDO
DO JL=KIDIA,KFDIA
  PULOW (JL)=(PULOW (JL)*&
   & (PAPHM15(JL,IKNUL(JL)+1)-PAPHM15(JL,IKNUB(JL)))-&
   & PULOW5(JL)*&
   & (PAPHM1 (JL,IKNUL(JL)+1)-PAPHM1 (JL,IKNUB(JL))))/&
   & (PAPHM15(JL,IKNUL(JL)+1)-PAPHM15(JL,IKNUB(JL)))**2   
  PULOW5(JL)= PULOW5(JL)/(PAPHM15(JL,IKNUL(JL)+1)-PAPHM15(JL,IKNUB(JL)))
  PVLOW (JL)=(PVLOW (JL)*&
   & (PAPHM15(JL,IKNUL(JL)+1)-PAPHM15(JL,IKNUB(JL)))-&
   & PVLOW5(JL)*&
   & (PAPHM1 (JL,IKNUL(JL)+1)-PAPHM1 (JL,IKNUB(JL))))/&
   & (PAPHM15(JL,IKNUL(JL)+1)-PAPHM15(JL,IKNUB(JL)))**2   
  PVLOW5(JL) =PVLOW5(JL)/(PAPHM15(JL,IKNUL(JL)+1)-PAPHM15(JL,IKNUB(JL)))
  IF (SQRT(PULOW5(JL)**2+PVLOW5(JL)**2) > GVSEC) THEN
    ZNORM (JL)=(PULOW(JL)*PULOW5(JL)+PVLOW(JL)*PVLOW5(JL))/&
     & SQRT(PULOW5(JL)**2+PVLOW5(JL)**2)  
    ZNORM5(JL)= SQRT(PULOW5(JL)**2+PVLOW5(JL)**2)
  ELSE
    ZNORM (JL)=0.0_JPRB
    ZNORM5(JL)=GVSEC
  ENDIF
  PVPH (JL,KLEV+1)=ZNORM (JL)
  PVPH5(JL,KLEV+1)=ZNORM5(JL)
ENDDO

!*******  SETUP OROGRAPHY AXES AND DEFINE PLANE OF PROFILES  *******

DO JL=KIDIA,KFDIA
  LLO=(PULOW5(JL) < GVSEC).AND.(PULOW5(JL) >= -GVSEC)
  IF(LLO) THEN
    ZU =PULOW (JL)
    ZU5=PULOW5(JL)+2.0_JPRB*GVSEC
  ELSE
    ZU =PULOW (JL)
    ZU5=PULOW5(JL)
  ENDIF
  ZPHI =(PVLOW(JL)*ZU5-ZU*PVLOW5(JL))/(PVLOW5(JL)**2+ZU5**2)
  ZPHI5=ATAN(PVLOW5(JL)/ZU5)
  PPSI (JL,KLEV+1)=          -ZPHI
  PPSI5(JL,KLEV+1)=PTHETA(JL)-ZPHI5
  ZB(JL)=1.0_JPRB-0.18_JPRB*PGAMMA(JL)-0.04_JPRB*PGAMMA(JL)**2
  ZC(JL)=0.48_JPRB*PGAMMA(JL)+0.3_JPRB*PGAMMA(JL)**2
  PD1 (JL)=-2.0_JPRB*(ZB(JL)-ZC(JL))*&
   & COS(PPSI5(JL,KLEV+1))*SIN(PPSI5(JL,KLEV+1))*&
   & PPSI(JL,KLEV+1)  
  PD15(JL)=ZB(JL)-(ZB(JL)-ZC(JL))*(SIN(PPSI5(JL,KLEV+1))**2)
  PD2 (JL)=(ZB(JL)-ZC(JL))*&
   & (COS(PPSI5(JL,KLEV+1))**2-SIN(PPSI5(JL,KLEV+1))**2)*&
   & PPSI(JL,KLEV+1)  
  PD25(JL)=(ZB(JL)-ZC(JL))*SIN(PPSI5(JL,KLEV+1))*COS(PPSI5(JL,KLEV+1))
  PDMOD (JL)=(PD1(JL)*PD15(JL)+PD2(JL)*PD25(JL))/&
   & SQRT(PD15(JL)**2+PD25(JL)**2)  
  PDMOD5(JL)= SQRT(PD15(JL)**2+PD25(JL)**2)
ENDDO

!  ************ DEFINE FLOW IN PLANE OF LOWLEVEL STRESS *************

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDGWD(JL))  THEN
      ZVT1       = PULOW (JL)*PUM15(JL,JK)+PVLOW (JL)*PVM15(JL,&
       & JK)+&
       & PULOW5(JL)*PUM1 (JL,JK)+PVLOW5(JL)*PVM1 (JL,JK)  
      ZVT15      = PULOW5(JL)*PUM15(JL,JK)+PVLOW5(JL)*PVM15(JL,JK)
      ZVT2       =- PVLOW (JL)*PUM15(JL,JK)+PULOW (JL)*PVM15(JL,&
       & JK)-&
       & PVLOW5(JL)*PUM1 (JL,JK)+PULOW5(JL)*PVM1 (JL,JK)  
      ZVT25      =- PVLOW5(JL)*PUM15(JL,JK)+PULOW5(JL)*PVM15(JL,JK)
      ZVPF (JL,JK)=((ZVT15*PD1 (JL)+ZVT25*PD2 (JL)+&
       & ZVT1 *PD15(JL)+ZVT2 *PD25(JL))*&
       & (ZNORM5(JL)*PDMOD5(JL))-&
       & (ZVT15*PD15(JL)+ZVT25*PD25(JL))*&
       & (ZNORM (JL)*PDMOD5(JL)+ZNORM5(JL)*PDMOD(JL)))/&
       & (ZNORM5(JL)*PDMOD5(JL))**2  
      ZVPF5(JL,JK)= (ZVT15*PD15(JL)+ZVT25*PD25(JL))/(ZNORM5(JL)*PDMOD5(JL))
    ENDIF

    PTAU(JL,JK)  =0.0_JPRB
    PZDEP(JL,JK) =0.0_JPRB
    PPSI(JL,JK)  =0.0_JPRB
    PTAU5(JL,JK) =0.0_JPRB
    PZDEP5(JL,JK)=0.0_JPRB
    PPSI5(JL,JK) =0.0_JPRB
    LL1(JL,JK)   =.FALSE.
  ENDDO
ENDDO

DO JK=2,KLEV
!DIR$ novector
  DO JL=KIDIA,KFDIA
    IF(LDGWD(JL)) THEN
      ZDP (JL,JK)=PAPM1 (JL,JK)-PAPM1 (JL,JK-1)
      ZDP5(JL,JK)=PAPM15(JL,JK)-PAPM15(JL,JK-1)
      ZS2 = (PAPHM1 (JL,JK)-PAPM1 (JL,JK-1))*ZVPF5(JL,JK  )+&
       & (PAPHM15(JL,JK)-PAPM15(JL,JK-1))*ZVPF (JL,JK  )+&
       & (PAPM1 (JL,JK)-PAPHM1 (JL,JK  ))*ZVPF5(JL,JK-1)+&
       & (PAPM15(JL,JK)-PAPHM15(JL,JK  ))*ZVPF (JL,JK-1)  
      ZS25= (PAPHM15(JL,JK)-PAPM15(JL,JK-1))*ZVPF5(JL,JK  )+&
       & (PAPM15(JL,JK)-PAPHM15(JL,JK  ))*ZVPF5(JL,JK-1)  
      PVPH(JL,JK)=(ZS2*ZDP5(JL,JK)-ZDP(JL,JK)*ZS25)/ZDP5(JL,JK)**2
      PVPH5(JL,JK)=ZS25/ZDP5(JL,JK)
      IF(PVPH5(JL,JK) < GVSEC) THEN
        PVPH (JL,JK)=0.0_JPRB
        PVPH5(JL,JK)=GVSEC
!U            KCRIT(JL)=JK   !BUGFIX
        IF (JK  <=  IKNUL(JL)) KCRIT(JL)=JK
      ENDIF
    ENDIF
  ENDDO
ENDDO
!DIR$ vector  

!*         2.3     MEAN FLOW RICHARDSON NUMBER.

DO JK=2,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDGWD(JL)) THEN
      IF ((ZVPF5(JL,JK)-ZVPF5(JL,JK-1)) > 0.0_JPRB) THEN
        ZS4 = (ZVPF (JL,JK)-ZVPF (JL,JK-1))
        ZS45= (ZVPF5(JL,JK)-ZVPF5(JL,JK-1))
      ELSE
        ZS4 =-(ZVPF (JL,JK)-ZVPF (JL,JK-1))
        ZS45=-(ZVPF5(JL,JK)-ZVPF5(JL,JK-1))
      ENDIF
      IF (ZS45 < GVSEC) THEN
        ZDWIND =0.0_JPRB
        ZDWIND5=GVSEC
      ELSE
        ZDWIND =ZS4
        ZDWIND5=ZS45
      ENDIF
      PRI (JL,JK)=PSTAB (JL,JK)*(ZDP5(JL,JK)/&
       & (RG*PRHO5(JL,JK)*ZDWIND5))**2 +&
       & 2.0_JPRB*RG*PSTAB5(JL,JK)*ZDP5(JL,JK)/&
       & (RG*PRHO5(JL,JK)*ZDWIND5 )**3 *&
       & (ZDP (JL,JK)*PRHO5(JL,JK)*ZDWIND5-&
       & ZDP5(JL,JK)*PRHO (JL,JK)*ZDWIND5-&
       & ZDP5(JL,JK)*PRHO5(JL,JK)*ZDWIND )  
      PRI5(JL,JK)=PSTAB5(JL,JK)*(ZDP5(JL,JK)/(RG*PRHO5(JL,JK)*ZDWIND5))**2
      IF (PRI5(JL,JK) < GRCRIT) THEN
        PRI (JL,JK)=0.0_JPRB
        PRI5(JL,JK)=GRCRIT
      ENDIF
    ENDIF
  ENDDO
ENDDO

!*      DEFINE TOP OF BLOCKED LAYER
!       ----------------------------

DO JK=2,KLEV-1
  DO JL=KIDIA,KFDIA

    IF(LDGWD(JL)) THEN

      IF (JK >= KKNU2(JL)) THEN

        ZNUM5(JL)=PNU5(JL)
        IF (SQRT(PULOW5(JL)**2+PVLOW5(JL)**2) < GVSEC) THEN
          ZS55=GVSEC
        ELSE
          ZS55= SQRT(PULOW5(JL)**2+PVLOW5(JL)**2)
        ENDIF
        ZWIND5=(PULOW5(JL)*PUM15(JL,JK)+PVLOW5(JL)*PVM15(JL,JK))/ZS55
        IF (ZWIND5 < 0.0_JPRB) THEN
          ZWIND5=-ZWIND5
        ENDIF
        IF (ZWIND5 < GVSEC) THEN
          ZWIND5=GVSEC
        ENDIF
        ZDELP5=PAPHM15(JL,JK+1)-PAPHM15(JL,JK)
        ZSTABM5=ZSQST5(JL,JK)
        ZSTABP5=ZSQST5(JL,JK+1)
        ZRHOM5=PRHO5(JL,JK  )
        ZRHOP5=PRHO5(JL,JK+1)
        ZS65=(ZSTABP5/ZRHOP5+ZSTABM5/ZRHOM5)/2.0_JPRB
        PNU5(JL) = PNU5(JL) + (ZDELP5/RG)*ZS65/ZWIND5
        IF((ZNUM5(JL) <= GFRCRIT).AND.(PNU5(JL) > GFRCRIT)&
         & .AND.(KKENVH(JL) == KLEV))&
         & KKENVH(JL)=JK  

      ENDIF

    ENDIF
  ENDDO
ENDDO

!  CALCULATION OF A DYNAMICAL MIXING HEIGHT FOR THE BREAKING
!  OF GRAVITY WAVES:

DO JL=KIDIA,KFDIA
  ZNUP5(JL)=0.0_JPRB
  ZNUM5(JL)=0.0_JPRB
ENDDO

DO JK=KLEV-1,2,-1
  DO JL=KIDIA,KFDIA

    IF(LDGWD(JL)) THEN

      IF (JK < KKENVH(JL)) THEN
        ZNUM5(JL)=ZNUP5(JL)
        IF (SQRT(PULOW5(JL)**2+PVLOW5(JL)**2) < GVSEC) THEN
          ZS75=GVSEC
        ELSE
          ZS75= SQRT(PULOW5(JL)**2+PVLOW5(JL)**2)
        ENDIF
        ZWIND5=(PULOW5(JL)*PUM15(JL,JK)+PVLOW5(JL)*PVM15(JL,JK))/ZS75
        IF (ZWIND5 < 0.0_JPRB) THEN
          ZWIND5=-ZWIND5
        ENDIF
        IF (ZWIND5 < GVSEC) THEN
          ZWIND5=GVSEC
        ENDIF
        ZDELP5=PAPHM15(JL,JK+1)-PAPHM15(JL,JK)
        ZSTABM5=ZSQST5(JL,JK)
        ZSTABP5=ZSQST5(JL,JK+1)
        ZRHOM5=PRHO5(JL,JK  )
        ZRHOP5=PRHO5(JL,JK+1)
        ZS85=(ZSTABP5/ZRHOP5+ZSTABM5/ZRHOM5)/2.0_JPRB
        ZNUP5(JL) = ZNUP5(JL) + (ZDELP5/RG)*ZS85/ZWIND5
        IF((ZNUM5(JL) <= 1.5_JPRB).AND.(ZNUP5(JL) > 1.5_JPRB)&
         & .AND.(KKCRITH(JL) == KLEV))&
         & KKCRITH(JL)=JK  

      ENDIF

    ENDIF

  ENDDO
ENDDO

DO JL=KIDIA,KFDIA
  KKCRITH(JL)=MIN(KKCRITH(JL),KKNU(JL))
ENDDO

!     DIRECTIONAL INFO FOR FLOW BLOCKING *************************

DO JK=ILEVH,KLEV
  DO JL=KIDIA,KFDIA
    IF(JK >= KKENVH(JL)) THEN
      LLO=(PUM15(JL,JK) < GVSEC).AND.(PUM15(JL,JK) >= -GVSEC)
      IF(LLO) THEN
        ZU =PUM1 (JL,JK)
        ZU5=PUM15(JL,JK)+2.0_JPRB*GVSEC
      ELSE
        ZU =PUM1 (JL,JK)
        ZU5=PUM15(JL,JK)
      ENDIF
      ZPHI =(PVM1(JL,JK)*ZU5-PVM15(JL,JK)*ZU)/(PVM15(JL,JK)**2+ZU5**2)
      ZPHI5=ATAN(PVM15(JL,JK)/ZU5)
      PPSI (JL,JK)=           -ZPHI
      PPSI5(JL,JK)=PTHETA(JL)-ZPHI5
    ENDIF
  ENDDO
ENDDO
!      FORMS THE VERTICAL 'LEAKINESS' **************************

DO JK=ILEVH,KLEV
  DO JL=KIDIA,KFDIA
    IF(JK >= KKENVH(JL)) THEN
      ZS9 =(PGEOM1 (JL,KKENVH(JL))+PGEOM1 (JL,KKENVH(JL)-1))/2.0_JPRB
      ZS95=(PGEOM15(JL,KKENVH(JL))+PGEOM15(JL,KKENVH(JL)-1))/2.0_JPRB
      IF (ZS95 < 1.0_JPRB) THEN
        ZGGEENV =0.0_JPRB
        ZGGEENV5=1.0_JPRB
      ELSE
        ZGGEENV =ZS9
        ZGGEENV5=ZS95
      ENDIF
      IF (PGEOM15(JL,JK) < 1.0_JPRB) THEN
        ZGGEOM1 =0.0_JPRB
        ZGGEOM15=1.0_JPRB
      ELSE
        ZGGEOM1 =PGEOM1 (JL,JK)
        ZGGEOM15=PGEOM15(JL,JK)
      ENDIF
      IF (PHSTD(JL)*RG < 1.0_JPRB) THEN
        ZGVAR=1.0_JPRB
      ELSE
        ZGVAR=PHSTD(JL)*RG
      ENDIF
      ZS10 =((ZGGEENV -ZGGEOM1 )*(ZGGEOM15+ZGVAR)-&
       & (ZGGEENV5-ZGGEOM15)* ZGGEOM1) /&
       & (ZGGEOM15+ZGVAR)**2  
      ZS105=(ZGGEENV5-ZGGEOM15)/(ZGGEOM15+ZGVAR)
      PZDEP (JL,JK)=ZS10/(2.0_JPRB*SQRT(ZS105))
      PZDEP5(JL,JK)=SQRT(ZS105)
    ENDIF
  ENDDO
ENDDO

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