SUBROUTINE GRID_MINMAXAVG(kfld,PGRID)

!    Purpose.
!    --------
!      Print min, max and avg value of fields.

!    Input:
!    ------
!      KFLD  - numer of fields
!      PGRID - array containing grid point fields

!    Author.
!    -------
!      Y.Tremolet

!    Modifications.
!    --------------
!      Original: 29/03/01
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

USE YOMLUN, ONLY : NULERR
USE YOMMP , ONLY : MYPROC
USE YOMDIM, ONLY : NPROMA, NGPBLKS
USE YOMGEM, ONLY : NGPTOTG
USE YOMCT0, ONLY : NPROC
USE MPL_MODULE

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KFLD 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGRID(NPROMA,KFLD,NGPBLKS) 
INTEGER(KIND=JPIM) :: ii,jb,jj,jf,ioproc
REAL(KIND=JPRB) :: zbuf(nproma*ngpblks)
REAL(KIND=JPRB) :: zgrid(NGPTOTG)
REAL(KIND=JPRB) :: zmin, zmax, zavg, zz
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "orcvgpf.intfb.h"
#include "osndgpf.intfb.h"

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

IF (LHOOK) CALL DR_HOOK('GRID_MINMAXAVG',0,ZHOOK_HANDLE)
ioproc=1
DO jf=1,kfld
  ii=0
  DO jb=1,ngpblks
    DO jj=1,nproma
      ii=ii+1
      zbuf(ii)=pgrid(jj,jf,jb)
    ENDDO
  ENDDO

  CALL OSNDGPF(zbuf,ioproc)

  IF (MYPROC==ioproc) THEN
    CALL ORCVGPF(zgrid,zbuf)

    zmin=zgrid(1)
    zmax=zgrid(1)
    zavg=zgrid(1)
    DO ii=2,NGPTOTG
      zz=zgrid(ii)
      zmin=MIN(zz,zmin)
      zmax=MAX(zz,zmax)
      zavg=zavg+zz
    ENDDO
    zavg=zavg/ii
    write(nulerr,999)jf,zmin,zmax,zavg

  ENDIF
ENDDO

IF (NPROC>1) CALL MPL_BARRIER(CDSTRING='GRID_MINMAXAVG:')

999 FORMAT(I5,' MIN,MAX,AVG=',3(2X,ES8.1))

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