SUBROUTINE MEANUV_AVERAGEAD(KDLEN,KLEN,KDCOUNT,KLEV,PUV5,PUV,PW5,PW,PXPP)

!**** *MEANUV_AVERAGEAD*   Adjoint of weighted mean calculation for U and V

!  N. Bormann     19/02/2001    ECMWF

!     PURPOSE
!     -------

!     Calculates the gradient of U & V profiles given the gradient of 
!     weighted means for U and V for a whole set of obs.

!**   INTERFACE
!     ---------
!     *CALL* *MEANUV_AVERAGEAD(KDLEN,KLEN,KDCOUNT,KLEV,PUV,PW,PXPP)*
!     WHERE KDLEN = First dimension: Max number of members in set
!           KLEN  = Actual number of members
!           KDCOUNT = Second dimension (Data per observations)
!           KLEV  = Number of levels data is given on
!           PUV5  = U and V on given levels
!           PUV   = U and V on given levels
!           PW5   = weights
!           PW    = weights
!           PXPP  = Result of laywer averaging (output)

!     MEANUV_AVERAGEAD is called from HOPAD

!**   METHOD
!     ------
!     Weighted average over given levels.

!**   EXTERNALS
!     --------- 

!**   MODIFICATIONS
!     -------------
!     Niels Bormann   2001/02/19: Original
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning
!     Niels Bormann   2009/10/09: Inclusion of variable weights
!----------------------------------------------------------

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

IMPLICIT none

! Arguments
INTEGER(KIND=JPIM),INTENT(IN)    :: KDLEN ! First dimension: Max number of members in set
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEN ! 1st dimension, number of obs
INTEGER(KIND=JPIM),INTENT(IN)    :: KDCOUNT ! Data per observations
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV ! Number of levels
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUV5(KDLEN,KLEV,2) ! U and V on given levels
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUV(KDLEN,KLEV,2) ! U and V on given levels
REAL(KIND=JPRB)   ,INTENT(IN)    :: PW5(KDLEN,KLEV) ! Weights for levels
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PW(KDLEN,KLEV) ! Weights for levels
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXPP(KDLEN,KDCOUNT,2) ! Averaged values
! Local variables
INTEGER(KIND=JPIM)    :: ILEV
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
!=============================================================

IF (LHOOK) CALL DR_HOOK('MEANUV_AVERAGEAD',0,ZHOOK_HANDLE)
IF(KDCOUNT > 1)&
 & CALL ABOR1('MEANUV_AVERAGEAD: MULTISPECTRAL WINDS NOT YET IMPLEMENTED')  

DO ILEV = 1, KLEV
  PUV(1:KLEN,ILEV,1) = PUV(1:KLEN,ILEV,1) + PW5(1:KLEN,ILEV) * PXPP(1:KLEN,1,1)
  PW(1:KLEN,ILEV)    = PW(1:KLEN,ILEV) + PUV5(1:KLEN,ILEV,1) * PXPP(1:KLEN,1,1)
  PUV(1:KLEN,ILEV,2) = PUV(1:KLEN,ILEV,2) + PW5(1:KLEN,ILEV) * PXPP(1:KLEN,1,2)
  PW(1:KLEN,ILEV)    = PW(1:KLEN,ILEV) + PUV5(1:KLEN,ILEV,2) * PXPP(1:KLEN,1,2)
ENDDO

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