SUBROUTINE OPENFPFA(KD,KULFP,CDFPFN,KFILE,KDATE,KNLATI,KNXLON,LDFICP,LDFILAF)

!**** *OPENFPFA*  - OPEN AN EXISTING POST-PROCESSING FILE

!     PURPOSE.
!     --------
!        To open an existing file of post-processing format, and 
!        control the framework (namelist vs file frame).

!**   INTERFACE.
!     ----------
!       *CALL* *OPENFPFA*

!        EXPLICIT ARGUMENTS
!        --------------------
!        KD     : subdomain index
!        KULFP  : logical unit number of file
!        CDFPFN : file name
!        KFILE  : kind of file :
!                 0 => ordinary file
!                 1 => constant climatology values
!                 2 or 3 => monthly climatology values
!        KDATE  : expected date in file
!        KNLATI : number of latitudes in file
!        KNXLON : number of longitudes in file
!        LDFICP : .TRUE. if file contains poles
!        LDFILAF: .TRUE. to list the content of file

!        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 : 99-07-19 from RDCLIMO
!        MODIFIED :
!            01-11-27  Martin Janousek: new definition of LAM geometry
!        R. El Khatib : 01-08-07 Pruning options
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        JD. Gril : 10-02-2005 Mercator Rotated/Tilted
!     ------------------------------------------------------------------

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

USE YOMLUN   , ONLY : NULOUT
USE YOMFPC   , ONLY : CFPFMT
USE YOMCT0   , ONLY : LELAM
USE YOMFPD   , ONLY : NLAT     ,NLON     ,RLATC    ,RLONC    ,&
 & RDELX   ,RDELY    ,&
 & NFPLUX  ,NFPGUX  
USE YOMFPG   , ONLY : NFPMAX   ,NFPRGRI  ,LFPMAP   ,&
 & RFPMU   ,NFPMEN   ,NFPLEV   ,&
 & NFPHTYP ,NFPTTYP  ,NMFPMAX  ,&
 & FPMUCEN ,FPLOCEN  ,FPSTRET  ,&
 & FPLX    ,FPLY     ,&
 & FPLON0  ,FPLAT0   ,LFPMRT   ,FPRPK    ,FPVALH   ,&
 & FPVBH  
USE YOMCST   , ONLY : RPI
USE YOMGEM   , ONLY : VP00

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KD 
INTEGER(KIND=JPIM),INTENT(IN)    :: KULFP 
CHARACTER(LEN=*)  ,INTENT(IN)    :: CDFPFN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFILE 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDATE(11) 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KNLATI 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KNXLON 
LOGICAL           ,INTENT(INOUT) :: LDFICP 
LOGICAL           ,INTENT(IN)    :: LDFILAF 
! ITEST : level of test for framework
! CLLEC : file frame name

INTEGER(KIND=JPIM) :: JPXGEO, JPXIND, JPXPAH, JPXLAT, JPXLEV

PARAMETER(JPXGEO=18, JPXPAH=8,JPXIND=1, JPXLEV=100, JPXLAT=322)

INTEGER(KIND=JPIM) :: INLOPA(JPXLAT),INOZPA(JPXLAT)
INTEGER(KIND=JPIM) :: INLOP(JPXPAH), INOZP(JPXIND)
INTEGER(KIND=JPIM) :: IDATEF(11)

REAL(KIND=JPRB)    :: ZVALH(0:JPXLEV), ZVBH(0:JPXLEV)
REAL(KIND=JPRB)    :: ZSINLA(JPXLAT), ZSIN(JPXGEO)
REAL(KIND=JPRB)    :: ZFPVAH(0:NFPLEV)

INTEGER(KIND=JPIM) :: INBARI, INIVER, IREP, JLEV,&
 & IFPMAX, ITRUN, ITYPTR, JI, ITEST, INFPMAX,IQUAD  

LOGICAL :: LLMAP, LLGARD

REAL(KIND=JPRB) :: ZCLOPO, ZCODIL, ZEPS, ZREF, ZSLAPO, ZSLOPO

CHARACTER (LEN = 16) ::  CLLEC
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "chien.intfb.h"
#include "echien.intfb.h"

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

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

IF (LHOOK) CALL DR_HOOK('OPENFPFA',0,ZHOOK_HANDLE)
INLOP(:)=0
INOZP(:)=0
ZSIN (:)=0.0_JPRB
ZEPS=EPSILON(1.0_JPRB)*10000._JPRB
DO JLEV=0,NFPLEV
  ZFPVAH(JLEV)=FPVALH(JLEV)/VP00
ENDDO

!*       2. OPEN AND CHECK FILE
!           -------------------

!*         2.1 OPEN FILE

IREP=0
INBARI=0
IF (CFPFMT == 'GAUSS'.OR.(.NOT.LELAM.AND.CFPFMT == 'MODEL')) THEN
  WRITE(UNIT=CLLEC,FMT='(''FULPOS.ARP.'',I1.1,I2.2,I2.2)') KFILE, KD, KULFP
ELSE
  WRITE(UNIT=CLLEC,FMT='(''FULPOS.ALD.'',I1.1,I2.2,I2.2)') KFILE, KD, KULFP
ENDIF
CALL FAITOU(IREP,KULFP,.TRUE.,CDFPFN,'OLD',.TRUE.,.TRUE.,0,1,INBARI,CLLEC)
CALL LFIMST(IREP,KULFP,.FALSE.)
IF (LDFILAF) THEN
  CALL LFILAF(IREP,KULFP,.FALSE.)
ENDIF

!*         2.2 CHECK DATE

IF (KFILE == 1) THEN
  WRITE(NULOUT,*) ' YOU READ TARGET CLIMATOLOGY CONSTANT FIELDS'
ELSE
  CALL FADIES(IREP,KULFP,IDATEF)
  IF (KFILE >= 2) THEN
    WRITE(NULOUT,*)&
     & ' YOU READ TARGET CLIMATOLOGIC FIELDS FOR THE MONTH : ',IDATEF(2)  
    IF (IDATEF(2) /= KDATE(2)) THEN
      WRITE(NULOUT,*) ' EXPECTED MONTH : ',KDATE(2)
      WRITE(NULOUT,*) &
       & 'TARGET CLIM. AND INITIAL FILE OR MODEL DISAGREE ON MONTH'  
      CALL ABOR1('OPENFPFA : ABOR1 CALLED')
    ENDIF
  ELSEIF(KFILE == 0) THEN
!   Full date check : 
    DO JI=1,11
      IF (IDATEF(JI) /= KDATE(JI)) THEN
        WRITE(NULOUT,*) 'DATES ARE DIFFERENT !'
        WRITE(NULOUT,*) 'DATE IN FILE  : ',IDATEF
        WRITE(NULOUT,*) 'DATE COMPUTED : ',KDATE
        CALL ABOR1(' OPENFPFA : ERROR IN DATE')
      ENDIF
    ENDDO
  ELSE
    WRITE(NULOUT,*) ' INTERNAL ERROR KFILE : ', KFILE
    CALL ABOR1('OPENFPFA : ABOR1 CALLED')
  ENDIF
ENDIF

!*         2.3 CHECK FRAMEWORK

IF (CFPFMT == 'GAUSS'.OR.(.NOT.LELAM.AND.CFPFMT == 'MODEL')) THEN
  IF (KFILE == 0) THEN
!   Test everything
    ITEST=0
  ELSE
!   Do not test vertical coordinate
    ITEST=-1
  ENDIF
  IQUAD=1
  CALL CHIEN(CLLEC,NFPTTYP,FPMUCEN,FPLOCEN,FPSTRET,NFPMAX(KD),NLAT(KD),&
   & NLON(KD),NFPRGRI,NFPMEN,RFPMU,NFPHTYP,NFPLEV,VP00,ZFPVAH,FPVBH,IQUAD,&
   & ITEST,1,NLAT(KD),ZEPS,LDFICP,NULOUT)  
  IF (LDFICP) THEN
    KNLATI=NLAT(1)+2
  ELSE
    KNLATI=NLAT(1)
  ENDIF
  KNXLON=NLON(1)
ELSE
  IF (KFILE == 0) THEN
!   Test everything
    ITEST=0
  ELSE
!   Do not test vertical coordinate ; allow extension zone or not
    ITEST=-3
  ENDIF
  IF (LFPMRT) THEN
    ZSIN (1)  = -2._JPRB  ! Mercator Rotated/Tilted
  ELSE
    ZSIN (1)  = -1._JPRB
  ENDIF
  ZSIN (3)  = FPLON0*RPI/180._JPRB
  ZSIN (4)  = FPLAT0*RPI/180._JPRB
  ZSIN (5)  = RLONC(KD)*RPI/180._JPRB
  ZSIN (6)  = RLATC(KD)*RPI/180._JPRB
  IF(CFPFMT == 'LALON') THEN
    ZSIN (2)  = -1._JPRB
    ZSIN (7)  = RDELX(KD)*RPI/180._JPRB
    ZSIN (8)  = RDELY(KD)*RPI/180._JPRB
  ELSE
    ZSIN (2)  = FPRPK
    ZSIN (7)  = RDELX(KD)
    ZSIN (8)  = RDELY(KD)
  ENDIF
  ZSIN ( 9) = FPLX
  ZSIN (10) = FPLY
  IF (CFPFMT == 'MODEL') THEN
    INLOP(2) = 1
  ELSEIF (CFPFMT == 'LELAM' .AND. &
     & .NOT.(NLON(KD) == NFPLUX .AND. NLAT(KD) == NFPGUX)) THEN  
    INLOP(2) = -1
  ELSE
    INLOP(2) = 0
  ENDIF
  INLOP(3)  = 1
  INLOP(5)  = 1
  INLOP(7)  = 8
  INLOP(8)  = 8
  IF (CFPFMT == 'LELAM'.OR.(LELAM.AND.CFPFMT == 'MODEL')) THEN
    ZSIN (11) = 2.0_JPRB*RPI/FPLX
    ZSIN (12) = 2.0_JPRB*RPI/FPLY
    INLOP(1)  = MAX(0,MIN(11,NMFPMAX,NFPMAX(KD))-1)
    INLOP(4)  = NFPLUX
    INLOP(6)  = NFPGUX
    IFPMAX=NFPMAX(KD)
    INFPMAX=NMFPMAX
    LLMAP=LFPMAP
  ELSEIF (CFPFMT == 'LALON') THEN
    ZSIN (11) = 0.0_JPRB
    ZSIN (12) = 0.0_JPRB
    INLOP(1)  = 10 ! arbitrary value
    INLOP(4)  = NLON(KD)
    INLOP(6)  = NLAT(KD)
    IFPMAX=2
    INFPMAX=NFPMAX(KD)
    LLMAP=.TRUE.
  ELSE
    CALL ABOR1('INTERNAL ERROR CFPFMT')
  ENDIF
  CALL ECHIEN(CLLEC,KULFP,INFPMAX,LLMAP,IFPMAX,NLAT(KD),NLON(KD),INLOP,INOZP,&
   & ZSIN,NFPLEV,VP00,ZFPVAH,FPVBH,ITEST,ZEPS,NULOUT)  
  LLGARD=.FALSE.
  CALL FACIES(CLLEC,ITYPTR,ZSLAPO,ZCLOPO,ZSLOPO,ZCODIL,ITRUN,KNLATI,KNXLON,&
   & INLOPA,INOZPA,ZSINLA,INIVER,ZREF,ZVALH,ZVBH,LLGARD)  
ENDIF

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