SUBROUTINE GPHLUVAD(&
 ! --- INPUT IN TL -----------------------------------------------------------
 & KPROMA,KSTART,KPROF,PUT5,PVT5,PWW5,PWW0,PUT0,PVT0,&
 ! --- OUTPUT IN TL ----------------------------------------------------------
 & PLHU0,PLHV0)

!**** *GPHLUVAD* - wind components calculation in half-levels (adjoint)

!     Purpose.
!     --------
!           Compute wind components in half-levels
!           for non-hydrostatic dynamics in adjoint model

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

!        Explicit arguments :
!        --------------------
!         For comments see GPHLUV.

!        Implicit arguments : none.
!        --------------------

!     Method.
!     -------
!     Interpolation from full-levels using logaritmic pressure profile
!     Then modify values on the top and bottom using boundary condition
!        (at the bottom free-slip condition)
!        Store also the weights of vertical interpolation

!     Externals.
!     ----------
!     Reference.
!     ----------
!        ARPEGE/ALADIN documentation

!     Author.
!     -------
!        Martin Charron,  CNRM/GMAP - visiteur

! Modifications
! -------------
!   Original : November 1998
!   Modified 02-07-02 by C. Fischer : remove computation of trajectory
!   01-Oct-2003 M. Hamrud  CY28 Cleaning
!   08-Jun-2004 J. Masek   NH cleaning (LVSLWBC)
!   21-Mar-2006 K. Yessad  debugging, rewrite according to the TL code.
!   07-Mar-2007 K. Yessad  Remove LVERAVE_HLUV.
!   K. Yessad (Dec 2008): remove dummy CDLOCK
! End Modifications
!------------------------------------------------------------------

USE PARKIND1 , ONLY : JPIM     ,JPRB
USE YOMCVER  , ONLY : LVERTFE
USE YOMDIM   , ONLY : NFLEVG
USE YOMHOOK  , ONLY : LHOOK    ,DR_HOOK

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KPROF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT5(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT5(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWW5(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PWW0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVT0(KPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLHU0(KPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLHV0(KPROMA,0:NFLEVG) 

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

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

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

#include "abor1.intfb.h"

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

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

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

IF (LVERTFE) THEN
  ! * case LVERTFE not yet coded.
  !   For lvertfe=true no half level wind must be computed, the code must use
  !    only full level winds and the proper discretisation of "dV/d eta"
  !    has to be coded.
  CALL ABOR1(' GPHLUVAD: case LVERTFE not yet coded!')
ENDIF

Z13=1.0_JPRB/3.0_JPRB
Z23=2.0_JPRB/3.0_JPRB

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

!*    3. Surface.

DO JROF=KSTART,KPROF
  PVT0(JROF,NFLEVG)=PVT0(JROF,NFLEVG)+PLHV0(JROF,NFLEVG)
  PUT0(JROF,NFLEVG)=PUT0(JROF,NFLEVG)+PLHU0(JROF,NFLEVG)
ENDDO

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

!*    2. Top.

DO JROF=KSTART,KPROF
  PVT0(JROF,1)=PVT0(JROF,1)+PLHV0(JROF,0)
  PUT0(JROF,1)=PUT0(JROF,1)+PLHU0(JROF,0)
ENDDO

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

!*    1. General case:

DO JLEV=NFLEVG-1,1,-1
  DO JROF=KSTART,KPROF

    PVT0(JROF,JLEV)=PVT0(JROF,JLEV)+PWW5(JROF,JLEV)*PLHV0(JROF,JLEV)
    PVT0(JROF,JLEV+1)=PVT0(JROF,JLEV+1)&
     & +(1.0_JPRB-PWW5(JROF,JLEV))*PLHV0(JROF,JLEV)
    PWW0(JROF,JLEV)=PWW0(JROF,JLEV)&
     & +(PVT5(JROF,JLEV)-PVT5(JROF,JLEV+1))*PLHV0(JROF,JLEV)

    PUT0(JROF,JLEV)=PUT0(JROF,JLEV)+PWW5(JROF,JLEV)*PLHU0(JROF,JLEV)
    PUT0(JROF,JLEV+1)=PUT0(JROF,JLEV+1)&
     & +(1.0_JPRB-PWW5(JROF,JLEV))*PLHU0(JROF,JLEV)
    PWW0(JROF,JLEV)=PWW0(JROF,JLEV)&
     & +(PUT5(JROF,JLEV)-PUT5(JROF,JLEV+1))*PLHU0(JROF,JLEV)

  ENDDO
ENDDO

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

!*    0. Reset PLHU0 and PLHV0 to zero:

PLHV0(KSTART:KPROF,0:NFLEVG)=0.0_JPRB
PLHU0(KSTART:KPROF,0:NFLEVG)=0.0_JPRB

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

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