SUBROUTINE INI2WRFP(CDCONF,LDYN,LDCOSP,KFIELDS,KFPDOM,KOFF,CDPREF,&
 & CDSUFF,KLEV,KBITS,KMASK)  

!**** *INI2WRFP*  - PREPARE TO WRITE OUT THE HORIZONTALLY POST-PROCESSED 
!                   FIELDS TO ARPEGE/ALADIN FILES -FIELD-DEPENDANT VARIABLES

!     PURPOSE.
!     --------
!           To compute variables which depend of the fields themselves, 
!           prior to writing out to post-processed files

!**   INTERFACE.
!     ----------
!       *CALL* *INI2WRFP(CDCONF,LDYN,LDCOSP,KFIELDS,KFPDOM,KOFF,CDPREF,
!                        CDSUFF,KLEV,KBITS,KMASK)*

!        EXPLICIT ARGUMENTS
!        --------------------
!        CDCONF  : configuration of work
!        LDYN    : .TRUE. if dynamic fields ; else physical fields
!        LDCOSP  : .TRUE. if spectral fields ; else gridpoint fields
!        KFIELDS : number of fields
!        KFPDOM  : number of subdomains
!        KOFF    : Field offset to get absolute field pointer
!        CDPREF  : prefix of field names
!        CDSUFF  : suffix of field names
!        KLEV    : field level to be written out on file
!        KBITS   : number of bits for coding
!        KMASK   : binary mask ; =0 to skip the field (no write out) ; else =1

!        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 and wrsfpsm/dm
!        R. El Khatib : 00-08-18 derived types
!    R. El Khatib : 02-21-20 Fullpos B-level distribution + remove IO scheme
!    R. El Khatib : 03-04-17 Fullpos improvemnts
!    R. El Khatib : 03-05-27 Enable Pressure level below 1000 hPa despite *FA*
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ------------------------------------------------------------------

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

USE YOMAFN   , ONLY : TFP_DYNDS,GFP_PHYDS
USE YOMFP4L  , ONLY : NFLDFPL  ,NFPSFLDG ,NDOMFPL  ,NINDFPL  ,&
 & LFPSRFL  ,LFPSSRFG ,RLEVFPL  ,RFPSLEVG  
USE PTRFP4   , ONLY : YRQGFP

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELDS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFPDOM 
CHARACTER         ,INTENT(IN)    :: CDCONF*1 
LOGICAL           ,INTENT(IN)    :: LDYN 
LOGICAL           ,INTENT(IN)    :: LDCOSP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOFF 
CHARACTER(LEN=*)  ,INTENT(INOUT) :: CDPREF(KFIELDS) 
CHARACTER(LEN=*)  ,INTENT(OUT)   :: CDSUFF(KFIELDS) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KLEV(KFIELDS) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KBITS(KFIELDS) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KMASK(KFIELDS,KFPDOM) 
!     ZUNIT   : unit for level value that will be written out on files

!     IFLDR  : relative field pointer 
!     IFLDA  : absolute field pointer 
!     JFL    : local field pointer

!     NFPSFLDG : field pointers for concatenated spectral arrays
!     RFPSLEVG : level pointers for concatenated spectral arrays

REAL(KIND=JPRB) :: ZUNIT

INTEGER(KIND=JPIM) :: ID, IFLDA, IFLDR, JD, JFL
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"

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

!*       1. PREPARATIONS
!           ------------

IF (LHOOK) CALL DR_HOOK('INI2WRFP',0,ZHOOK_HANDLE)
IF (LEN(CDPREF) < 7) CALL ABOR1('INI2WRFP : CDPREF TOO SHORT')

IF (LDYN) THEN
  IF (CDCONF == 'E') THEN
    CDPREF(:)='P'
    ZUNIT=1.0_JPRB
  ELSEIF (CDCONF == 'U') THEN
    CDPREF(:)='H'
    ZUNIT=1.0_JPRB
  ELSEIF (CDCONF == 'Y') THEN
    CDPREF(:)='V'
!   Potential vorticity levels are written in deci-PVU :
    ZUNIT=10000000._JPRB
  ELSEIF (CDCONF == 'M') THEN
    CDPREF(:)='T'
    ZUNIT=1.0_JPRB
  ELSEIF (CDCONF == 'Z') THEN
    CDPREF(:)='S'
    ZUNIT=1.0_JPRB
  ELSE
    CALL ABOR1(' INI2WRFP : UNKNOWN CONFIGURATION')
  ENDIF
ENDIF

!*       2. SETUP PARAMETERS FOR PACKING AND FIELD NAME CONSTRUCTION
!           --------------------------------------------------------

KMASK(:,:)=0
DO JFL=1,KFIELDS
  IFLDR=KOFF+JFL
  IF (CDCONF=='I') THEN
!   Not ready for upper air physical post-processing
    DO JD=1,YRQGFP%IDOM(1,IFLDR)
      ID=YRQGFP%IDMP(JD,1,IFLDR)
      KMASK(JFL,ID)=1
    ENDDO
    IFLDA=YRQGFP%ICOD(IFLDR)
    KBITS(JFL)=GFP_PHYDS(IFLDA)%IBITS
    KLEV(JFL)=0
    CDPREF(JFL)=GFP_PHYDS(IFLDA)%CLNAME(1:4)
    CDSUFF(JFL)=GFP_PHYDS(IFLDA)%CLNAME(5:)
  ELSEIF(LDCOSP) THEN
    IFLDA=NFPSFLDG(IFLDR)
    KBITS(JFL)=TFP_DYNDS(IFLDA)%IBITS
    KMASK(:,:)=1
    IF (LFPSSRFG(IFLDR)) THEN
      CDPREF(JFL)=TFP_DYNDS(IFLDA)%CLNAME(1:4)
      KLEV(JFL)=0
      CDSUFF(JFL)=TFP_DYNDS(IFLDA)%CLNAME(5:16)
    ELSE
      KLEV(JFL)=NINT(RFPSLEVG(IFLDR)*ZUNIT)
      CDSUFF(JFL)=TFP_DYNDS(IFLDA)%CLNAME(1:12)
      SELECT CASE (CDCONF)
      CASE ('E')
!         This enables pressure level below 1000 hPa in spite of *FA* limit.
!         level value is then written on 4 digits instead of 3
        IF (KLEV(JFL) > 100000) THEN
          CDPREF(JFL)(2:)=' '
          WRITE(CDPREF(JFL)(2:7),'(I6)') KLEV(JFL)
        ENDIF 
      END SELECT
    ENDIF
  ELSE
    DO JD=1,NDOMFPL(IFLDR)
      ID=NINDFPL(JD,IFLDR)
      KMASK(JFL,ID)=1
    ENDDO
    IFLDA=NFLDFPL(IFLDR)
    KBITS(JFL)=TFP_DYNDS(IFLDA)%IBITS
    IF (LFPSRFL(IFLDR)) THEN
      CDPREF(JFL)=TFP_DYNDS(IFLDA)%CLNAME(1:4)
      KLEV(JFL)=0
      CDSUFF(JFL)=TFP_DYNDS(IFLDA)%CLNAME(5:16)
    ELSE 
      KLEV(JFL)=NINT(RLEVFPL(IFLDR)*ZUNIT)
      CDSUFF(JFL)=TFP_DYNDS(IFLDA)%CLNAME(1:12)
      SELECT CASE (CDCONF)
      CASE ('E')
!         This enables pressure level below 1000 hPa in spite of *FA* limit.
!         level value is then written on 4 digits instead of 3
        IF (KLEV(JFL) > 100000) THEN
          CDPREF(JFL)(2:)=' '
          WRITE(CDPREF(JFL)(2:7),'(I6)') KLEV(JFL)
        ENDIF
      END SELECT
    ENDIF
  ENDIF
ENDDO

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

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