SUBROUTINE INCGPF(KFIELDS,YD_PGPBUF,KFLDPTP,PREAL)

!**** *INCGPF*  - include 2-D fields into array/workfile

!     Purpose.
!     --------
!     Write model fields into a model gridpoint buffer. 
!     Performs a transpose from ordering by fields to an ordering by the
!     NPROMA based packets used in the gridpoint calculations.

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

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

!        KFIELDS   - number of fields to include into wrk file
!        PGPBUF    - memory buffer ( used in case of no real IO)
!        KFLDPTP   - array of field pointers for inclusion
!        PREAL     - included fields (output)

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

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

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

!     Reference.
!     ----------
!        ECMWF Research Department documentation of the IFS

!     Author.
!     -------
!        R. El Khatib *METEO-FRANCE*

!     Modifications.
!     --------------
!        Original : 95-07-19
!        Modified 98-08-10 by K. YESSAD: removal of LRPOLE option.
!         -> polar values are not computed and stored any longer, only
!            non polar values are stored in the elements 1 to NGPTOTG of PREAL,
!            only non polar values are written on PGPBUF.
!        Modified 98-12-15 by R. El Khatib : Poles aspect fully removed
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      10-Jan-2004 CY28R1 Cleaning
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMDIM   , ONLY : NPROMA
USE YOMGEM   , ONLY : NGPTOT
USE GRIDPOINT_BUFFERS_MIX , ONLY : gridpoint_buffer

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELDS 
TYPE(GRIDPOINT_BUFFER),INTENT(INOUT) :: YD_PGPBUF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLDPTP(YD_PGPBUF%IFIELDS) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PREAL(NGPTOT,KFIELDS) 
REAL(KIND=JPRB) :: ZGPP(NPROMA,YD_PGPBUF%IFIELDS)

INTEGER(KIND=JPIM) :: IEND, IST, JF, JKGLO, JROF
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "sc2rdg.intfb.h"
#include "sc2wrg.intfb.h"

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

!*       1.    COPY FROM LOCAL FIELD FORMAT TO NPROMA FORMAT
!              ---------------------------------------------

IF (LHOOK) CALL DR_HOOK('INCGPF',0,ZHOOK_HANDLE)
IST=1
DO JKGLO=1,NGPTOT,NPROMA
  IEND=MIN(NPROMA,NGPTOT-JKGLO+1)
  IF (KFIELDS /= YD_PGPBUF%IFIELDS) THEN
    !           Read the fields that will not be overwritten
    CALL SC2RDG(YD_PGPBUF,JKGLO,IST,IEND,ZGPP)
  ENDIF
  DO JF=1,KFIELDS
    DO JROF=IST,IEND
      ZGPP(JROF,KFLDPTP(JF))=PREAL(JROF+JKGLO-1,JF)
    ENDDO
  ENDDO
  CALL SC2WRG(YD_PGPBUF,JKGLO,IST,IEND,ZGPP)
ENDDO

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

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