SUBROUTINE SUQNORM(PQNORM1,PQNORM2,PQNORM3)

!     Purpose.
!     --------
!               Define the metric for digital filter Jc

!**   Interface.
!     ----------
!        *CALL* *SUQNORM

!      Explicit arguments :
!      --------------------
!     Reference.
!     ----------
!     Author.
!     -------
!        P. Gauthier   *CNRM/GMAP*

!     Modifications.
!     --------------
!        Original : 98-10-21
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        Y.Tremolet    17-Nov-2008 Jc-DFI diagnostics in outer loop
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCT0    ,ONLY : LELAM
USE YOMDIM   , ONLY : NFLEVL, NFD2D, NFLSUR, NS3D, NSMAX, LSPT
USE YOMLUN   , ONLY : NULOUT
USE YOMMP    , ONLY :  MYLEVS

USE YOMLAP   , ONLY :  RLAPIN
USE YEMLAP   , ONLY : RLEPINN
USE YOMDYN   , ONLY : SIDELP,SITR,SIRPRG,SIRPRN
USE YOMCST   , ONLY : RCPD, RLVTT
USE YOMLCZ   , ONLY : COEQTERM
USE YOMGEM   , ONLY : VALH, VBH, VP00
USE YOM_YGFL , ONLY : YQ

IMPLICIT NONE

REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQNORM1(NFLEVL,2) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQNORM2(0:NSMAX,NFD2D) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQNORM3(NFLSUR,0:NSMAX,NS3D) 
!     Local variables

INTEGER(KIND=JPIM) :: JLEV, ILEV, JN
REAL(KIND=JPRB) :: ZT, ZHU, ZPP
REAL(KIND=JPRB) :: ZINVLAP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

IF (LHOOK) CALL DR_HOOK('SUQNORM',0,ZHOOK_HANDLE)
WRITE(NULOUT,9001)'SUQNORM- Initializing the inner product'&
 & ,'used to define the Jc based on the DFI'  
9001 FORMAT(//,12X,A,1X,A)

PQNORM3(:,:,:) = 0.0_JPRB
PQNORM2(:,:)   = 0.0_JPRB
PQNORM1(:,:)   = 0.0_JPRB

!     Defining the Q-norm used for the digital filter

DO JN = 0, NSMAX
     IF (LELAM) THEN
   ZINVLAP= RLEPINN(JN+1)
  ELSE
   ZINVLAP= RLAPIN(JN)
  ENDIF
  DO JLEV = 1,NFLEVL
    ILEV = MYLEVS(JLEV)
    ZT   = RCPD*SIDELP(ILEV)/(SITR*2.0_JPRB)
    ZHU  = COEQTERM*((RLVTT**2)/(RCPD*SITR))*SIDELP(ILEV)/2.0_JPRB
    PQNORM3(JLEV,JN,2) = -ZINVLAP*SIDELP(ILEV)/2.0_JPRB
    PQNORM3(JLEV,JN,1) = -ZINVLAP*SIDELP(ILEV)/2.0_JPRB
    IF(LSPT) THEN
      PQNORM3(JLEV,JN,3) = ZT
    ENDIF
    IF(YQ%LSP) THEN
      PQNORM3(JLEV,JN,4) = ZHU
    ENDIF
  ENDDO
ENDDO

!     Surface pressure component

DO JN = 0, NSMAX
  PQNORM2(JN,1) = SIRPRG/SIRPRN/2.0_JPRB
ENDDO

JLEV = NFLEVL/2
ILEV = MYLEVS(JLEV)
ZPP    = (VALH(ILEV) +VBH(ILEV))*VP00/100._JPRB
WRITE(NULOUT,9003)JLEV,ILEV,ZPP
9003 FORMAT(//,'SUQNORM- printing the coefficients of the norm'&
 & ,4x,'No. of local level: ',I2,3X,'No. of global level: ',I2 &
 & ,'Approximate pressure at that level. p = ',F6.1,' hPa'&
 & ,/2x,'n',6x,'Vorticity',3x,'Divergence',3x,'Temperature',3x &
 & ,'Humidity')  
DO JN = 0, NSMAX
  IF(YQ%LSP) THEN
    WRITE(NULOUT,9004) JN, PQNORM3(JLEV,JN,1),PQNORM3(JLEV,JN,2)&
     & ,PQNORM3(JLEV,JN,3),PQNORM3(JLEV,JN,4)  
  ELSE
    WRITE(NULOUT,9002) JN, PQNORM3(JLEV,JN,1),PQNORM3(JLEV,JN,2)&
     & ,PQNORM3(JLEV,JN,3)  
  ENDIF
ENDDO

DO JLEV=1,NFLEVL
PQNORM1(JLEV,:)=SIDELP(JLEV)/2.0_JPRB
ENDDO

9002 FORMAT(1X,I3,3X,3(4X,G12.6))
9004 FORMAT(1X,I3,3X,4(4X,G12.6))

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