SUBROUTINE FPGPNORM(PREAL,KFLDS,KDIMG,KMASK,PAVE,PMIN,PMAX,KSTART,KLEN)

!**** *FPGPNORM*  - Computation of gridpoint norms for a chunk of fields

!     Purpose.
!     --------
!          Computation of gridpoint norms for a chunk of fields

!**   Interface.
!     ----------
!        *CALL* *FPGPNORM(PREAL,KFLDS,KDIMG,KMASK,PAVE,PMIN,PMAX,KSTART,KLEN)*

!        Explicit arguments :
!        --------------------

!            PREAL   : global fields to pack
!            KFLDS   : number of fields to pack
!            KDIMG   : Size of fields in PREAL
!            KMASK   : binary mask ; =0 to skip the field (no computation) ; else =1
!            PAVE    : mean value of fields
!            PMIN    : min. value of fields
!            PMAX    : max. value of fields
!            KSTART  : actual start adress of fields in PREAL
!            KLEN    : actual size of fields in PREAL

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------

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

!     Author.
!     -------
!         RYAD EL KHATIB *METEO-FRANCE*

!     Modifications.
!     --------------
!        ORIGINAL : 95-10-05
!        Modified by K. YESSAD (MARCH 1997): parallelisation of FULL-POS.
!        Modified 98-08-10 by K. YESSAD: removal of LFPOLE option.
!         - removal of LFPOLE in YOMFPG and code under LFPOLE.
!        R. El Khatib : 98-12-18 Rewrite
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

IMPLICIT NONE

!     EXTERNAL REAL FUNCTIONS

INTEGER(KIND=JPIM),INTENT(IN)    :: KFLDS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDIMG 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PREAL(KDIMG,KFLDS) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KMASK(KFLDS) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PAVE(KFLDS) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMIN(KFLDS) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMAX(KFLDS) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTART 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEN 
REAL(KIND=JPRB) :: ZFIELD(KLEN)

INTEGER(KIND=JPIM) :: JFLD

REAL(KIND=JPRB) :: Z1SLEN
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*    0. LOOP ON FIELDS

IF (LHOOK) CALL DR_HOOK('FPGPNORM',0,ZHOOK_HANDLE)
Z1SLEN=1.0_JPRB/REAL(KLEN,JPRB)

DO JFLD=1,KFLDS

  IF (KMASK(JFLD) == 1) THEN

!*        1. Extract a global field from global array

    ZFIELD(1:KLEN)=PREAL(KSTART:KSTART+KLEN-1,JFLD)

!*        2. Compute norms

    PAVE(JFLD)=SUM(ZFIELD(1:KLEN))*Z1SLEN
    PMIN(JFLD)=MINVAL(ZFIELD)
    PMAX(JFLD)=MAXVAL(ZFIELD)

  ENDIF

ENDDO

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

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