SUBROUTINE GEMS_TEND( KIDIA, KFDIA, KLEV, KLON, PTENGFL, PTENC)

!**   INTERFACE.
!     ----------
!          *GEMS_TEND* IS CALLED FROM *CALLPAR*.

! INPUTS:
!  -------

! INPUTS/OUTPUTS:
!---------------

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

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


!     Author
!    --------
!         2008-10-15, R. Engelen 

!     Modifications :
!    ----------------

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

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

USE YOM_YGFL , ONLY : NAERO, NGRG, &
 & YGHG, NGHG, YTRAC , NTRAC, YGFL, YGRG, YAERO

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
REAL(KIND=JPRB), INTENT(INOUT)   :: PTENGFL(KLON,KLEV,YGFL%NDIM1)

! In the declaration below the INTENT attribute has been removed to comply 
! strict f95 standards. The attribute INTENT(INOUT) should be put back after all 
! compiler (especially NEC) support this extension. - R. El Khatib 04-Jun-2009
REAL(KIND=JPRB), POINTER :: PTENC(:,:,:)

INTEGER(KIND=JPIM) :: ITRC
INTEGER(KIND=JPIM) :: JK, JL, JEXT

REAL(KIND=JPRB) :: ZHOOK_HANDLE


!-----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('GEMS_TEND',0,ZHOOK_HANDLE)

ITRC=0
DO JEXT=1,NGHG
  ITRC=ITRC+1 
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      PTENGFL(JL,JK,YGHG(JEXT)%MP1)=PTENC(JL,JK,ITRC)
    ENDDO
  ENDDO
ENDDO
DO JEXT=1,NTRAC
  ITRC=ITRC+1
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      PTENGFL(JL,JK,YTRAC(JEXT)%MP1)=PTENC(JL,JK,ITRC)
    ENDDO
  ENDDO
ENDDO
DO JEXT=1,NGRG
  ITRC=ITRC+1 
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      PTENGFL(JL,JK,YGRG(JEXT)%MP1)=PTENC(JL,JK,ITRC)
    ENDDO
  ENDDO
ENDDO
DO JEXT=1,NAERO
  ITRC=ITRC+1 
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      PTENGFL(JL,JK,YAERO(JEXT)%MP1)=PTENC(JL,JK,ITRC)
    ENDDO
  ENDDO
ENDDO


!-----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('GEMS_TEND',1,ZHOOK_HANDLE)
END SUBROUTINE GEMS_TEND
