SUBROUTINE CPG_PT ( KSTART, KPROF,LDGET,KFLDN,KFLDX,  &
           &PTENDU, PTENDV, PTENDT, &
           &PTENDQ, PTENDO3,PTENDL, PTENDI,PTENDA,PTENDS, & 
           &PTENDR, PTENDG, PTENDH, PTENDTKE, PTENDEXT, PTENDSP, &
           &PGFLPT, PGFLT1, PGFL, PGMV, PGMVS)

! -----------------------------------------------------------------------------
!     --------------------------------------------
!**** *CPG_PT*  physics tendencies written to or extracted from GFLPT
!     --------------------------------------------

!     Purpose.
!     --------
!       Copy the physics tendencies calculated (for ex in predictor)
!       to GFLPT to be retrieved and used during corrector

!       This routine is called for lpc_full=true only.

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

!     Input arguments
!     ------------------
!       KSTART  : start of horizontal loop
!       KPROF   : end of horizontal loop
!       LDGET   : T = get tendencies from buffers.
!                 F = save tendencies in buffers.
!       KFLDN,KFLDX: for second dimension of PTENDEXT.
!       PTEND[X]: tendency of X variable from phy.
!       PGFLPT  : tendency of X variable from phy.
!       PGFL    : tendency of X variable from phy.
!       PGMV    : upper air GMV variables at time t.
!       PGMVS   : surface GMV variables at time t.

!     Output arguments
!     --------------------
!       PTEND[X]: tendency of X variable from phy.
!       PGFLPT  : tendency of X variable from phy.
!       PGFLT1  : tendency of X variable from phy.
!       PGMV    : upper air GMV variables at time t.
!       PGMVS   : surface GMV variables at time t.

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

!     Author.
!     -------
!        Martina Tudor

!     Modifications.
!     --------------
!   Original : 03-10-22
!   Y. Seity : 04-05-18 : Add AROME new GFL variables
!   K. Yessad and J. Masek, Jun 2004: cleanings.
!   Y. Seity : 05-09-28 : Add YEXT GFL field
!   K. Yessad and D.Salmond : 25-04-06 : Generalisation for PC scheme
!   N. Wedi and K. Yessad (Jan 2008): different dev for NH model and PC scheme
!        Y.Seity : 07-12-18 : Add YH (Hail) GFL tendency
! -----------------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOM_YGFL , ONLY : YGFL, YQ, YL, YO3, YA, YI, YR, YS, YG, YH, YTKE,&
                     &  YCPF,YEXT,NGFL_EXT
USE YOMDIM   , ONLY : NPROMA, NFLEVG
USE YOMGMV   , ONLY : YT9,NDIMGMV,NDIMGMVS

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN) :: KSTART
INTEGER(KIND=JPIM),INTENT(IN) :: KPROF
LOGICAL,INTENT(IN)            :: LDGET
INTEGER(KIND=JPIM),INTENT(IN) :: KFLDN
INTEGER(KIND=JPIM),INTENT(IN) :: KFLDX
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDU(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDV(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDT(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDQ(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDO3(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDL(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDI(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDA(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDR(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDS(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDG(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDH(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDTKE(NPROMA,NFLEVG)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDEXT(NPROMA,KFLDN:KFLDX,NGFL_EXT)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PTENDSP(NPROMA)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PGFLPT(NPROMA,NFLEVG,YGFL%NDIMPT)
REAL(KIND=JPRB),OPTIONAL,INTENT(OUT)   :: PGFLT1(NPROMA,NFLEVG,YGFL%NDIM1)
REAL(KIND=JPRB),OPTIONAL,INTENT(IN)    :: PGFL(NPROMA,NFLEVG,YGFL%NDIM)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PGMV(NPROMA,NFLEVG,NDIMGMV)
REAL(KIND=JPRB),OPTIONAL,INTENT(INOUT) :: PGMVS(NPROMA,NDIMGMVS)

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

REAL(KIND=JPRB) :: ZHOOK_HANDLE
INTEGER(KIND=JPIM) :: JEXT

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

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

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

!*       1.  PREDICTOR FOR LPC_FULL.
!        ---------------------------

IF (.NOT.LDGET) THEN

  ! * GMV:
  IF (PRESENT(PTENDU)) THEN
    PGMV(KSTART:KPROF,1:NFLEVG,YT9%MCUPT)=PTENDU(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDV)) THEN
    PGMV(KSTART:KPROF,1:NFLEVG,YT9%MCVPT)=PTENDV(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDT)) THEN
    PGMV(KSTART:KPROF,1:NFLEVG,YT9%MCTPT)=PTENDT(KSTART:KPROF,1:NFLEVG)
  ENDIF

  ! * GMVS:
  IF (PRESENT(PTENDSP)) THEN
    PGMVS(KSTART:KPROF,YT9%MCSPPT)=PTENDSP(KSTART:KPROF)
  ENDIF

  ! * GFL (must be later rewritten with a global treatment of GFL,
  !   loop on JGFL)
  IF (PRESENT(PTENDQ).AND.YQ%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YQ%MPPT)=PTENDQ(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDO3).AND.YO3%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YO3%MPPT)=PTENDO3(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDI).AND.YI%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YI%MPPT)=PTENDI(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDL).AND.YL%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YL%MPPT)=PTENDL(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDA).AND.YA%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YA%MPPT)=PTENDA(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDS).AND.YS%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YS%MPPT)=PTENDS(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDR).AND.YR%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YR%MPPT)=PTENDR(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDG).AND.YG%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YG%MPPT)=PTENDG(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDH).AND.YH%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YH%MPPT)=PTENDH(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDTKE).AND.YTKE%LPT) THEN
    PGFLPT(KSTART:KPROF,1:NFLEVG,YTKE%MPPT)=PTENDTKE(KSTART:KPROF,1:NFLEVG)
  ENDIF
  IF (PRESENT(PTENDEXT)) THEN
    DO JEXT=1,NGFL_EXT
      IF (YEXT(JEXT)%LPT) THEN
        PGFLPT(KSTART:KPROF,1:NFLEVG,YEXT(JEXT)%MPPT) &
         & = PTENDEXT(KSTART:KPROF,1:NFLEVG,JEXT)
      ENDIF
    ENDDO
  ENDIF
  ! IF (PRESENT(PGFLT1).AND.YCPF%LACTIVE) THEN
  !   PGFL(KSTART:KPROF,1:NFLEVG,YCPF%MP)=PGFLT1(KSTART:KPROF,1:NFLEVG,YCPF%MP1)
  ! ENDIF

ENDIF

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

!*       2.  CORRECTOR FOR LPC_FULL.
!        ---------------------------

IF (LDGET) THEN

  ! * GMV:
  IF (PRESENT(PTENDU)) THEN
    PTENDU(KSTART:KPROF,1:NFLEVG)=PGMV(KSTART:KPROF,1:NFLEVG,YT9%MCUPT)
  ENDIF
  IF (PRESENT(PTENDV)) THEN
    PTENDV(KSTART:KPROF,1:NFLEVG)=PGMV(KSTART:KPROF,1:NFLEVG,YT9%MCVPT)
  ENDIF
  IF (PRESENT(PTENDT)) THEN
    PTENDT(KSTART:KPROF,1:NFLEVG)=PGMV(KSTART:KPROF,1:NFLEVG,YT9%MCTPT)
  ENDIF

  ! * GMVS:
  IF (PRESENT(PTENDSP)) THEN
    PTENDSP(KSTART:KPROF)=PGMVS(KSTART:KPROF,YT9%MCSPPT)
  ENDIF

  ! * GFL (must be later rewritten with a global treatment of GFL,
  !   loop on JGFL)
  IF (PRESENT(PTENDQ).AND.YQ%LPT) THEN
    PTENDQ(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YQ%MPPT)
  ENDIF
  IF (PRESENT(PTENDO3).AND.YO3%LPT) THEN
    PTENDO3(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YO3%MPPT)
  ENDIF
  IF (PRESENT(PTENDI).AND.YI%LPT) THEN
    PTENDI(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YI%MPPT)
  ENDIF
  IF (PRESENT(PTENDL).AND.YL%LPT) THEN
    PTENDL(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YL%MPPT)
  ENDIF
  IF (PRESENT(PTENDA).AND.YA%LPT) THEN
    PTENDA(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YA%MPPT)
  ENDIF
  IF (PRESENT(PTENDR).AND.YR%LPT) THEN
    PTENDR(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YR%MPPT)
  ENDIF
  IF (PRESENT(PTENDS).AND.YS%LPT) THEN
    PTENDS(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YS%MPPT)
  ENDIF
  IF (PRESENT(PTENDG).AND.YG%LPT) THEN
    PTENDG(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YG%MPPT)
  ENDIF
  IF (PRESENT(PTENDH).AND.YH%LPT) THEN
    PTENDH(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YH%MPPT)
  ENDIF
  IF (PRESENT(PTENDTKE).AND.YTKE%LPT) THEN
    PTENDTKE(KSTART:KPROF,1:NFLEVG)=PGFLPT(KSTART:KPROF,1:NFLEVG,YTKE%MPPT)
  ENDIF
  IF (PRESENT(PTENDEXT)) THEN
    DO JEXT=1,NGFL_EXT
      IF (YEXT(JEXT)%LPT) THEN
        PTENDEXT(KSTART:KPROF,1:NFLEVG,JEXT) &
         & = PGFLPT(KSTART:KPROF,1:NFLEVG,YEXT(JEXT)%MPPT)
      ENDIF
    ENDDO
  ENDIF
  IF (PRESENT(PGFLT1).AND.YCPF%LACTIVE) THEN
    PGFLT1(KSTART:KPROF,1:NFLEVG,YCPF%MP1)=PGFL(KSTART:KPROF,1:NFLEVG,YCPF%MP)
  ENDIF

ENDIF

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

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