SUBROUTINE EXTFPF(KNFG,KNFL,KFIELDS,PGPBUF,KLENGPB,KSTAGPB,KSTAF,PREALG,KFPD1,&
 & KNFD,KFLDOFF)  

!**** *EXTFPF*  - extracts 2-D fields from gridpoint post-processing file

!     PURPOSE.
!     --------
!        Write out the post-processed fields of the post-processing gridpoint
!        buffer. 
!        Performs a transposision from ordering by the NFPROMA-based
!        sub-rows of packets used in the gridpoint calculations to an ordering
!        by fields. The result is in the form of an array PREALG dimensioned
!        PREALG containing the global extracted fields sorted out from the 
!        first subdomain to the last subdomain.
!        Before leaving the subroutine, the data is distributed by fields
!        among the "I/O" processors.

!**   INTERFACE.
!     ----------
!       *CALL* *EXTFPF*

!        EXPLICIT ARGUMENTS
!        --------------------
!        INPUT:
!          KNFG     - number of fields to be extracted from work file
!          KNFL     - (local) number of global field treated by my proc.
!          KFIELDS  - number of fields in work file
!          PGPBUF   - memory buffer ( used in case of no real IO)
!          KLENGPB  - length of memory buffer
!          KSTAGPB  - start adresses in memory buffer.
!          KSTAF    - first field to be extracted
!          KFPD1    - first dimension of output array PREALG
!          KNFD     - number of fields for each processor
!          KFLDOFF  - offset before the first global field belonging to my proc.
!        OUTPUT:
!          PREALG   - extracted fields (output)

!        IMPLICIT ARGUMENTS
!        --------------------

!     METHOD.
!     -------
!        SEE DOCUMENTATION

!     EXTERNALS.
!     ----------

!     REFERENCE.
!     ----------
!        ECMWF Research Department documentation of the IFS
!        Documentation about FULL-POS.

!     AUTHOR.
!     -------
!        RYAD EL KHATIB *METEO-FRANCE*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 94-04-08
!        Modified by K. YESSAD (MARCH 1997): parallelisation of FULL-POS.
!        Modified : R. El Khatib 98-08-20 : cleanups ; Remove poles aspect
!                   G. Radnoti   98-09-21: go from circular to continuous
!                                          distribution (optional for the
!                                                       time being)
!        R. El Khatib : 98-10-07 Simplified scanning
!        R. El Khatib : 99-02-04 Remove KFLDPTP, cleanups
!        R. El Khatib : 01-08-07 Pruning options
!        R. El Khatib : 03-01-29 Remove SM aspects.
!        R. El Khatib : 03-04-17 Fullpos improvemnts
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      10-Jan-2004 CY28R1 Cleaning
!        K. Yessad    : 31-01-2007 Cleanings + adapt call to DIWRGRFP.
!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMFPSC2 , ONLY : NFPBLOCS ,NFPROMA  ,NFPSTART ,NFPEND   ,NFPSORT
USE YOMFPG   , ONLY : NFPRGPL  ,NFPRGPLX
USE YOMCT0   , ONLY : NPROC
USE YOMFPGIND, ONLY : NFPRGPNUM,NFPRGPIND
USE YOMTAG   , ONLY : MTAGDISTGP

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KNFL 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLENGPB 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFPD1 
INTEGER(KIND=JPIM),INTENT(IN)    :: KNFG 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELDS 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGPBUF(KLENGPB) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTAGPB(NFPBLOCS) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSTAF 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PREALG(KFPD1,KNFL) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KNFD(NPROC) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFLDOFF(NPROC) 

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

REAL(KIND=JPRB) :: ZGFP(NFPROMA,KFIELDS), ZREALL(NFPRGPL,KNFG)

INTEGER(KIND=JPIM) :: IEND, IFLD, IST, JBLOC, JFLD, JFP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

#include "diwrgrfp.intfb.h"
#include "sc2rdgfp.intfb.h"

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

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

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

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

DO JBLOC=1,NFPBLOCS
  
  IST =NFPSTART(JBLOC)
  IEND=NFPEND(JBLOC)

  CALL SC2RDGFP(NFPROMA,KFIELDS,PGPBUF,KSTAGPB(JBLOC),IST,IEND,ZGFP)

  DO JFLD=1,KNFG
    IFLD=JFLD+KSTAF-1
    DO JFP=IST,IEND
      ZREALL(NFPSORT(JFP,JBLOC),JFLD)=ZGFP(JFP,IFLD)
    ENDDO
  ENDDO

ENDDO

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

!*       2. DISTRIBUTE FIELDS TO "I/O" PROCESSORS
!           -------------------------------------

CALL DIWRGRFP(MTAGDISTGP,KNFG,KFPD1,NFPRGPL,NFPRGPLX,KNFL, &
 & NFPRGPIND,NFPRGPNUM,KNFD,KFLDOFF,ZREALL,PREALG)

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

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