SUBROUTINE CPPHINP(KST,KEND,&
 & PGEMU,PGELAM,&
 & PUT0,PVT0,PQT0,PQT0L,PQT0M,PQSLT0L,PQSLT0M,&
 & PRDELP0,PEVEL0,PCVGQSL,&
 & PMU0,PMU0LU,PMU0M,PCVGQ)  

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

USE YOMCVER  , ONLY : LVERTFE
USE YOMDIM   , ONLY : NFLEVG   ,NPROMA   ,NPROMM
USE YOMPHY   , ONLY : LMPHYS   ,LRAYFM   ,LRAYFM15, LRAYLU
USE YOEPHY   , ONLY : LEPHYS
USE YOMRIP   , ONLY : RCODEC   ,RSIDEC   ,RCOVSR   ,RSIVSR &
 & ,RCODECLU ,RSIDECLU ,RCOVSRLU ,RSIVSRLU  
USE YOERIP   , ONLY : RCODECM  ,RSIDECM  ,RCOVSRM  ,RSIVSRM
USE YOMRIP15 , ONLY : RCODECM15,RSIDECM15,RCOVSRM15,RSIVSRM15
USE YOMARPHY , ONLY : LMPA
USE YOMDYN   , ONLY : NCOMP_CVGQ
USE YOMPHY2  , ONLY : TSPHY
USE YOMSIMPHL, ONLY : LSIMPH

!**** *CPPHINP*  - ComPute PHysical INPut.

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

!**   Interface.
!     ----------
!        *CALL* *CPPHINP*

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

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

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

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

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

!     Author.
!     -------

!     Modifications.
!     --------------
!        1999-04, J.M. Piriou: Moon radiation.
!        2002-05, K. YESSAD: consistency with finite element scheme.
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        2004-11, Y. Seity: Do not compute Convergence of Humidity for Arome
!        2005-10  Y. Bouteloup : Modification for computation of CVGQ
!     ------------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KST 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEMU(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGELAM(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQT0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQT0L(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQT0M(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSLT0L(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSLT0M(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP0(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PEVEL0(NPROMA,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCVGQSL(NPROMA,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMU0(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMU0LU(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PMU0M(NPROMA) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PCVGQ(NPROMM,NFLEVG) 

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

INTEGER(KIND=JPIM) :: JLEV, JROF

REAL(KIND=JPRB) :: ZED
REAL(KIND=JPRB) :: ZEPS,ZABSTSPHY,ZUSTSPHY

LOGICAL :: LLCOMPUTE_CVGQ

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

IF (LHOOK) CALL DR_HOOK('CPPHINP',0,ZHOOK_HANDLE)

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

!*       1.1   Astronomy.

DO JROF=KST,KEND
  PMU0(JROF)=MAX( RSIDEC*PGEMU(JROF)&
   & -RCODEC*RCOVSR*SQRT(1.0_JPRB-PGEMU(JROF)**2)*COS(PGELAM(JROF))&
   & +RCODEC*RSIVSR*SQRT(1.0_JPRB-PGEMU(JROF)**2)*SIN(PGELAM(JROF))&
   & ,0.0_JPRB)  
ENDDO

!*       Lunar astronomy.

IF(LRAYLU) THEN
  DO JROF=KST,KEND
    PMU0LU(JROF)=MAX( RSIDECLU*PGEMU(JROF)&
     & -RCODECLU*RCOVSRLU*SQRT(1.0_JPRB-PGEMU(JROF)**2)*COS(PGELAM(JROF))&
     & +RCODECLU*RSIVSRLU*SQRT(1.0_JPRB-PGEMU(JROF)**2)*SIN(PGELAM(JROF))&
     & ,0.0_JPRB)  
  ENDDO
ENDIF

IF (LEPHYS.OR.(LMPHYS.AND.LRAYFM)) THEN
  DO JROF=KST,KEND
    PMU0M(JROF)=MAX( RSIDECM*PGEMU(JROF)&
     & -RCODECM*RCOVSRM*SQRT(1.0_JPRB-PGEMU(JROF)**2)*COS(PGELAM(JROF))&
     & +RCODECM*RSIVSRM*SQRT(1.0_JPRB-PGEMU(JROF)**2)*SIN(PGELAM(JROF))&
     & ,0.0_JPRB)  
  ENDDO

ELSEIF (LMPHYS.AND.LRAYFM15) THEN
  DO JROF=KST,KEND
    PMU0M(JROF)=MAX( RSIDECM15*PGEMU(JROF)&
     & -RCODECM15*RCOVSRM15*SQRT(1.0_JPRB-PGEMU(JROF)**2)*COS(PGELAM(JROF))&
     & +RCODECM15*RSIVSRM15*SQRT(1.0_JPRB-PGEMU(JROF)**2)*SIN(PGELAM(JROF))&
     & ,0.0_JPRB)  
  ENDDO

ELSE
  DO JROF=KST,KEND
    PMU0M(JROF)=0.0_JPRB
  ENDDO
ENDIF

!*    1.2   Convergence of humidity and physical wind components.

! ky: variable to be put later in a module, saying if the CVGQ
! calculation is required or not (its definition must be the same
! everywhere in the code).
! For the time being this calculation is needed if MF physics
! (other than AROME physics) is activated.
LLCOMPUTE_CVGQ=(LMPHYS.OR.LSIMPH).AND.(.NOT.LMPA)

IF (LLCOMPUTE_CVGQ) THEN

  IF ((NCOMP_CVGQ == 0) .OR. (NCOMP_CVGQ == 1)) THEN
  
    ! * Eulerian convergence:

    ! a/ Horizontal part of the moisture convergence:
    IF (NCOMP_CVGQ == 0) THEN
      DO JLEV=1,NFLEVG
        DO JROF=KST,KEND
          PCVGQ(JROF,JLEV)=&
           & -PQT0L(JROF,JLEV)*PUT0(JROF,JLEV)&
           & -PQT0M(JROF,JLEV)*PVT0(JROF,JLEV)  
        ENDDO
      ENDDO
    ELSE
      DO JLEV=1,NFLEVG
        DO JROF=KST,KEND
          PCVGQ(JROF,JLEV)=&
           & -PQSLT0L(JROF,JLEV)*PUT0(JROF,JLEV)&
           & -PQSLT0M(JROF,JLEV)*PVT0(JROF,JLEV)  
        ENDDO
      ENDDO
    ENDIF

    ! b/ Vertical part of the moisture convergence:
    IF (LVERTFE) THEN

      ! * "pevel0" contains full-layer "etadot (d prehyd / d eta)"
      !   The current discretisation of the vertical advection of a variable X
      !   which is proposed is:
      !   [etadot d X/d eta](l) = 0.5 * [1 / Delta eta](l)
      !    * [etadot d prehyd/d eta](l) * (X(l+1)-X(l-1))
      !   Remark K.Y.: this discretisation is not fully consistent with the
      !    discretisation of a vertical advection provided by ECMWF between
      !    CY29 and CY29R2 (first coded in CPDYN, then transferred to CPEULDYN),
      !    and it is desirable (but not urgent) to update this discretisation
      !    in the future to make it consistent with CPEULDYN (in this case
      !    one must use routine VERDER to compute the vertical derivative
      !    [d X/d eta](l)).

      ! * Layer 1:

      DO JROF=KST,KEND
        PCVGQ(JROF,1  )=PCVGQ(JROF,1  )+&
         & (PQT0(JROF,1)-PQT0  (JROF,2))&
         & *PEVEL0(JROF,1)*PRDELP0(JROF,1  )  
      ENDDO

      ! * Layers 2 to L-1:

      DO JLEV=2,NFLEVG-1
        DO JROF=KST,KEND
          ZED=0.5_JPRB*PEVEL0(JROF,JLEV)
 
          PCVGQ(JROF,JLEV  )=PCVGQ(JROF,JLEV  )+&
           & (PQT0(JROF,JLEV-1)-PQT0  (JROF,JLEV+1))&
           & *ZED*PRDELP0(JROF,JLEV  )  
        ENDDO
      ENDDO

      ! * Layer L:

      DO JROF=KST,KEND
        PCVGQ(JROF,NFLEVG  )=PCVGQ(JROF,NFLEVG  )+&
         & (PQT0(JROF,NFLEVG-1)-PQT0  (JROF,NFLEVG))&
         & *PEVEL0(JROF,NFLEVG)*PRDELP0(JROF,NFLEVG  )  
      ENDDO

    ELSE

      ! * "pevel0" contains half-layer "etadot (d prehyd / d eta)":
      !   The discretisation of the vertical advection of a variable X writes:
      !   [etadot d X/d eta](l) =
      !    0.5 * [1 / Delta eta](l) *
      !    { [etadot d prehyd/d eta](lbar) * (X(l+1)-X(l))
      !    + [etadot d prehyd/d eta](lbar-1) * (X(l)-X(l-1)) }

      DO JLEV=1,NFLEVG-1
        DO JROF=KST,KEND
          ZED=0.5_JPRB*PEVEL0(JROF,JLEV)
  
          PCVGQ(JROF,JLEV  )=PCVGQ(JROF,JLEV  )+&
           & (PQT0(JROF,JLEV  )-PQT0  (JROF,JLEV+1))&
           & *ZED*PRDELP0(JROF,JLEV  )  

          PCVGQ(JROF,JLEV+1)=PCVGQ(JROF,JLEV+1)+&
            & (PQT0(JROF,JLEV  )-PQT0  (JROF,JLEV+1))&
            & *ZED*PRDELP0(JROF,JLEV+1)  
        ENDDO
      ENDDO
    ENDIF
  
  ELSE
  
    ! * case NCOMP_CVGQ=2: Semi-Lagrangian convergence:

    ZEPS=1.E-12
    ZABSTSPHY=MAX(ZEPS,ABS(TSPHY))
    IF (TSPHY >=0) THEN
      ZUSTSPHY=1.0_JPRB/ZABSTSPHY
    ELSE
      ZUSTSPHY=-1.0_JPRB/ZABSTSPHY
    ENDIF

    DO JLEV=1,NFLEVG
      DO JROF=KST,KEND
        PCVGQ(JROF,JLEV)=PCVGQSL(JROF,JLEV)*ZUSTSPHY
      ENDDO
    ENDDO
  ENDIF     

ENDIF

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

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