SUBROUTINE SPNORMAVE(KSMAX,PMFG,KDIM1,KLEVXG,LDKINER,PNORM,PAVE)

!**** *SPNORMAVE* - Compute averages of norms in spectral space

!     Purpose.
!     --------
!        Compute the average of norms in spectral space for each level
!        and for all levels

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

!        Explicit arguments :
!        --------------------
!        KSMAX   : truncation
!        PMFG    : spectrum for each n
!        KDIM1   : first dimension of input and output arrays PMFG and PNORM
!        KLEVXG  : number of levels to compute
!        LDKINER : true if kinetic energy computation
!        PNORM   : Average norm for each level
!        PAVE    : Average norm for all levels

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

!     Method.
!     -------

!     Externals. None
!     ----------
!      Called by SPNORMBM.

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        Philippe Courtier  *ECMWF*

!     Modifications.
!     --------------
!        Original : 92-12-20
!        Modified : 95-06-06 L.Isaksen   Reordering of spectral arrays
!        Modified : 96-08-08 L.Isaksen   Merged with MP version
!        Modified : 99-02-09 R. El Khatib NSMAX => arg KSMAX
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

IMPLICIT NONE
INTEGER(KIND=JPIM),INTENT(IN)    :: KSMAX 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDIM1 
REAL(KIND=JPRB)                  :: PMFG(KDIM1,0:KSMAX) ! Argument NOT used
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEVXG 
LOGICAL           ,INTENT(IN)    :: LDKINER 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PNORM(KDIM1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PAVE 
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1.    Computes norm.
!              --------------

!CDIR NOVECTOR
IF (LHOOK) CALL DR_HOOK('SPNORMAVE',0,ZHOOK_HANDLE)
CALL GSTATS(1950,0)
PNORM(1:KLEVXG)=SUM(PMFG(1:KLEVXG,:),DIM=2)
IF (.NOT.LDKINER) THEN
  PNORM(1:KLEVXG)=SQRT(PNORM(1:KLEVXG))
ENDIF

PAVE=SUM(PNORM(1:KLEVXG))
PAVE=PAVE/REAL(KLEVXG,JPRB)
CALL GSTATS(1950,1)

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

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