SUBROUTINE SC2WRGFP(KPROMA,KFIELDS,PBUF,KSTAGPB,KSTA,KEND,PGP)

!**** *SC2WRG* - Write out gridpoint array to work file

!     Purpose.  Write out the gridpoint array to work file
!     --------  or alternativly copy to grid-point buffer.
!               Optionally the fields are packed before writing out.

!     SC2RDGFP is identical to the old version of SC2RDG, which was used
!     before the introduction of the "gridpoint_buffer" type. It is retained
!     for use with AFPBUF because gridpoint_buffers_mix assume KPROMA=NPROMA.

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

!        Explicit arguments :  KPROMA -  dimensioning         (input)
!        --------------------  KFIELDS - number of fields     (input)
!                              PBUF - grid-point buffer     (output)
!                              KSTAGPB - start address in buf.(input)
!                              KSTA    - start address        (input)
!                              KEND    - end adress           (input)
!                              PGP     - input array         (input)

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

!     Method.
!     -------

!     Externals. MIOWRITE - write routine from MIO package
!     ---------- FILLB    - interface to packing routines

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

!     Author.
!     -------
!        Mats Hamrud  *ECMWF*

!     Modifications.
!     --------------
!        Original : 92-08-01
!        Modified : 92-12-20 M.Hamrud - packing and compacting of fields
!        R. El Khatib : 01-08-07 Pruning options
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KPROMA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELDS 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PBUF(:) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTAGPB 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KEND 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGP(KPROMA,KFIELDS) 
! === END OF INTERFACE BLOCK ===

INTEGER(KIND=JPIM) :: ISTA, IWORDS, JF, IEND
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1.    WRITE OUT GRIDPOINT ARRAY.
!              --------------------------

IF (LHOOK) CALL DR_HOOK('SC2WRGFP',0,ZHOOK_HANDLE)
IWORDS=KEND-KSTA+1

ISTA=KSTAGPB+1
IEND=KSTAGPB+IWORDS
DO JF=1,KFIELDS
  PBUF(ISTA:IEND)=PGP(KSTA:KEND,JF)
  ISTA=ISTA+IWORDS
  IEND=IEND+IWORDS
ENDDO

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

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