SUBROUTINE INI1WRFP(CDCONF,LDCOSP,KFIELDS,KPTR,KREC,LDYN,KSIZEG,KSIZPK)

!**** *INI1WRFP*  - PREPARE TO READ/WRITE ARPEGE/ALADIN FILES IN THE TARGET GEOMETRY

!     PURPOSE.
!     --------
!           To compute variables which do not depend of the fields themselves, 
!           prior to writing out to post-processed files or to read 
!           target-formatted climatology files

!**   INTERFACE.
!     ----------
!       *CALL* *INI1WRFP*

!        EXPLICIT ARGUMENTS
!        --------------------
!        CDCONF  : configuration of work : 
!                  '0' <=> setup pointers while reading climatology
!                  'I' <=> physical fields
!                  else dynamical fields
!        LDCOSP  : .TRUE. if spectral data ; else gridpoint data
!        KFIELDS : total number of fields to be extracted
!        KPTR    : pointer of each subdomain in the general output array
!        KREC    : number of fields to be written out on file
!        LDYN    : .TRUE. if dynamic fields conf. ; else physical fields
!        KSIZPK  : global size reserved for each subdomain (including extension 
!                  zone + 2 words for packing
!        KSIZEG  : global size reserved for each subdomain (including extension 
!                  zone

!        IMPLICIT ARGUMENTS
!        --------------------
!         See modules above.

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

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

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

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

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 98-12-17 from wrhfpsm/dm & wrsfpsm/dm
!    R. El Khatib : 02-21-20 Fullpos B-level distribution + remove IO scheme
!    R. El Khatib : 03-04-17 Fullpos improvemnts
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMFPC   , ONLY : NFPDOM
USE YOMFPD   , ONLY : NFPSIZEG ,NFPSIZEL
USE PTRFP4   , ONLY : YRQGFP
USE YOMFPDIM , ONLY : NFPBFOUT
USE YOMFP4L  , ONLY : NDOMFPL  ,NINDFPL  ,NDYNFPL  ,NFPSPBG
USE YOMDIM   , ONLY : NSPEC2G

IMPLICIT NONE

CHARACTER         ,INTENT(IN)    :: CDCONF*1 
LOGICAL           ,INTENT(IN)    :: LDCOSP 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KFIELDS 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KPTR(NFPDOM) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KREC(NFPDOM) 
LOGICAL           ,INTENT(OUT)   :: LDYN 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KSIZEG(NFPDOM) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KSIZPK(NFPDOM) 
INTEGER(KIND=JPIM) :: JD, JF, JL
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*       1. COMPUTE GLOBAL SIZE RESERVED FOR EACH SUBDOMAIN AND POINTERS
!           ------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('INI1WRFP',0,ZHOOK_HANDLE)
IF (LDCOSP) THEN
  KSIZEG(:)=NSPEC2G
  KSIZPK(:)=NSPEC2G+2 ! See FACOND for justification
ELSE
  DO JD=1,NFPDOM
    KSIZEG(JD)=NFPSIZEG(JD)+NFPSIZEL(JD)
    KSIZPK(JD)=NFPSIZEG(JD)+NFPSIZEL(JD)+2 ! See FACOND for justification
  ENDDO
ENDIF
KPTR(1)=1
DO JD=2,NFPDOM
  KPTR(JD)=KPTR(JD-1)+KSIZEG(JD-1)
ENDDO

!*       2. COMPUTE NUMBER OF FIELDS IN BUFFER AND OUTPUT FILES
!           ---------------------------------------------------

IF (CDCONF /= '0') THEN
  IF (CDCONF == 'I') THEN
    KFIELDS=NFPBFOUT
    KREC(:)=0
    DO JF=1, NFPBFOUT
      DO JL=1, YRQGFP%ILEV(JF)
        DO JD=1,YRQGFP%IDOM(JL,JF)
          KREC(YRQGFP%IDMP(JD,JL,JF))=KREC(YRQGFP%IDMP(JD,JL,JF))+1
        ENDDO
      ENDDO
    ENDDO
    LDYN=.FALSE.
  ELSEIF (LDCOSP) THEN
    KFIELDS=NFPSPBG
    KREC(:)=KFIELDS
    LDYN=.TRUE.
  ELSE
    KFIELDS=NDYNFPL
    KREC(:)=0
    DO JF=1, KFIELDS
      DO JD=1,NDOMFPL(JF)
        KREC(NINDFPL(JD,JF))=KREC(NINDFPL(JD,JF))+1
      ENDDO
    ENDDO
    LDYN=.TRUE.
  ENDIF
ENDIF

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

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