SUBROUTINE FOUTCNORM(LDFOUTCNORM,KU,KF,KDGL,KNMAX,CDF,PF)

!**** *FOUTCNORM*   - PRINTS NORMS ON FOURIER COEFFICIENTS OF TRANSMISSION
!                     COEFFICIENTS

!     Purpose.
!     --------

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

!        Explicit arguments :
!        --------------------
!        * INPUT:
!          LDFOUTCNORM     - controls which norms have to be printed.
!          KU              - logical output unit.
!          KF              - pseudo-vertical dimension of array PF.
!          KDGL            - number of latitudes.
!          KNMAX           - truncation.
!          CDF             - field identifier.
!          PF              - field, statistics on which have to be done.

!        * OUTPUT:

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

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

!     Reference.
!     ----------

!     Author.
!     -------
!        K. YESSAD (CNRM/GMAP). 

!     Modifications.
!     --------------
!        Original : FEB 1999
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

IMPLICIT NONE

! * DUMMY:
INTEGER(KIND=JPIM),INTENT(IN)    :: KF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDGL 
INTEGER(KIND=JPIM),INTENT(IN)    :: KNMAX 
LOGICAL           ,INTENT(IN)    :: LDFOUTCNORM(4) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KU 
CHARACTER(LEN=5)  ,INTENT(IN)    :: CDF 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PF(1:KF,0:2*KNMAX,1:KDGL) 
INTEGER(KIND=JPIM) :: JF
INTEGER(KIND=JPIM) :: JGL
INTEGER(KIND=JPIM) :: JM

LOGICAL :: LLFOUTCNORM

REAL(KIND=JPRB) :: ZIM
REAL(KIND=JPRB) :: ZMOY
REAL(KIND=JPRB) :: ZMOY2(1:KDGL)
REAL(KIND=JPRB) :: ZMOY3(1:KF)
REAL(KIND=JPRB) :: ZSTD
REAL(KIND=JPRB) :: ZSTD2(1:KDGL)
REAL(KIND=JPRB) :: ZSTD3(1:KF)
REAL(KIND=JPRB) :: ZSTD4(1:2*KNMAX)
REAL(KIND=JPRB) :: ZRE
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

! 0/ Preliminary prints:

IF (LHOOK) CALL DR_HOOK('FOUTCNORM',0,ZHOOK_HANDLE)
LLFOUTCNORM=LDFOUTCNORM(2).OR.LDFOUTCNORM(3).OR.LDFOUTCNORM(4)

IF (LLFOUTCNORM) THEN
  CALL FLUSH(KU)
  WRITE (KU,*)
  WRITE (KU,'(1X,A)') ' ===  STATISTICS IN FOUTCNORM === '
  WRITE (KU,*)
ENDIF

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

! 1/ Global statistics:

IF (LDFOUTCNORM(1)) THEN
! * average:
  ZMOY=0.0_JPRB
  DO JF=1,KF
    DO JGL=1,KDGL
      ZMOY=ZMOY+PF(JF,0,JGL)
    ENDDO
  ENDDO
  ZMOY=ZMOY/(REAL(KF,JPRB)*REAL(KDGL,JPRB))
! * standard deviation:
  ZSTD=0.0_JPRB
  DO JM=1,KNMAX
    DO JF=1,KF
      DO JGL=1,KDGL
        ZSTD=ZSTD+2.0_JPRB*PF(JF,2*JM-1,JGL)*PF(JF,2*JM-1,JGL) &
         & +2.0_JPRB*PF(JF,2*JM,JGL)*PF(JF,2*JM,JGL)  
      ENDDO
    ENDDO
  ENDDO 
  ZSTD=ZSTD/(REAL(KF,JPRB)*REAL(KDGL,JPRB))
  ZSTD=SQRT(ZSTD)
  WRITE(KU,'(1X,A,A,A,E20.14,A,E20.14)') &
   & ' - GLOBAL AVERAGE AND ST. DEV. OF FIELD ',CDF,': ', &
   & ZMOY,', ',ZSTD  
ENDIF

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

! 2/ Statistics per latitude:

IF (LDFOUTCNORM(2)) THEN
! * average:
  ZMOY2(1:KDGL)=0.0_JPRB
  DO JF=1,KF
    ZMOY2(1:KDGL)=ZMOY2(1:KDGL)+PF(JF,0,1:KDGL)
  ENDDO
  ZMOY2(1:KDGL)=ZMOY2(1:KDGL)/REAL(KF,JPRB)
! * standard deviation:
  ZSTD2(1:KDGL)=0.0_JPRB
  DO JM=1,KNMAX
    DO JF=1,KF
      ZSTD2(1:KDGL)=ZSTD2(1:KDGL) &
       & +2.0_JPRB*PF(JF,2*JM-1,1:KDGL)*PF(JF,2*JM-1,1:KDGL) &
       & +2.0_JPRB*PF(JF,2*JM,1:KDGL)*PF(JF,2*JM,1:KDGL)  
    ENDDO
  ENDDO
  ZSTD2(1:KDGL)=ZSTD2(1:KDGL)/REAL(KF,JPRB)
  ZSTD2(1:KDGL)=SQRT(ZSTD2(1:KDGL))
  WRITE(KU,*)
  DO JGL=1,KDGL
    WRITE(KU,'(1X,A,A,A,I4,A,E20.14,A,E20.14)') &
     & ' - AVERAGE AND ST. DEV. OF FIELD ',CDF, &
     & ' FOR LATITUDE ',JGL,': ', &
     & ZMOY2(JGL),', ',ZSTD2(JGL)  
  ENDDO
ENDIF

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

! 3/ Statistics per layer:

IF (LDFOUTCNORM(3)) THEN
! * average:
  ZMOY3(1:KF)=0.0_JPRB
  DO JGL=1,KDGL
    ZMOY3(1:KF)=ZMOY3(1:KF)+PF(1:KF,0,JGL)
  ENDDO
  ZMOY3(1:KF)=ZMOY3(1:KF)/REAL(KDGL,JPRB)
! * standard deviation:
  ZSTD3(1:KF)=0.0_JPRB
  DO JM=1,KNMAX
    DO JGL=1,KDGL
      ZSTD3(1:KF)=ZSTD3(1:KF) &
       & +2.0_JPRB*PF(1:KF,2*JM-1,JGL)*PF(1:KF,2*JM-1,JGL) &
       & +2.0_JPRB*PF(1:KF,2*JM,JGL)*PF(1:KF,2*JM,JGL)  
    ENDDO
  ENDDO
  ZSTD3(1:KF)=ZSTD3(1:KF)/REAL(KDGL,JPRB)
  ZSTD3(1:KF)=SQRT(ZSTD3(1:KF))
  WRITE(KU,*)
  DO JF=1,KF
    WRITE(KU,'(1X,A,A,A,I4,A,E20.14,A,E20.14)') &
     & ' - AVERAGE AND ST. DEV. OF FIELD ',CDF, &
     & ' FOR LAYER ',JF,': ',ZMOY3(JF),', ',ZSTD3(JF)  
  ENDDO
ENDIF

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

! 4/ Statistics per wavenumber:

IF (LDFOUTCNORM(4)) THEN
  ZSTD4(1:2*KNMAX)=0.0_JPRB
  DO JF=1,KF
    DO JGL=1,KDGL
      ZSTD4(1:2*KNMAX)=ZSTD4(1:2*KNMAX) &
       & +2.0_JPRB*PF(JF,1:2*KNMAX,JGL)*PF(JF,1:2*KNMAX,JGL)  
    ENDDO
  ENDDO
  ZSTD4(1:2*KNMAX)=ZSTD4(1:2*KNMAX)/(REAL(KDGL,JPRB)*REAL(KF,JPRB))
  ZSTD4(1:2*KNMAX)=SQRT(ZSTD4(1:2*KNMAX))
  WRITE(KU,*)
  DO JM=1,KNMAX
    ZRE=ZSTD4(2*JM-1)
    ZIM=ZSTD4(2*JM)
    WRITE(KU,'(1X,A,A,A,I4,A,E20.14,A,E20.14)') &
     & ' - RE AND IM CONTRIBUTION TO ST. DEV. OF FIELD ',CDF, &
     & ' FOR WAVENUMBER ',JM,': ', &
     & ZRE,', ',ZIM  
  ENDDO
ENDIF

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

! 5/ End prints:

IF (LLFOUTCNORM) THEN
  WRITE (KU,*)
  WRITE (KU,'(1X,A)') ' ===  END STATISTICS IN FOUTCNORM === '
  WRITE (KU,*)
  CALL FLUSH(KU)
ENDIF

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

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