SUBROUTINE RDCLIMO(LDEZON,PFIELDE,KGPE,PFIELD,KGP,KFIELDS,KCOD,KLI,LDMASK)

!**** *RDCLIMO*  - READ OUTPUT CLIMATOLOGY

!     PURPOSE.
!     --------
!        Open output climatology files and read the needed fields
!        In case of a file ARPEGE, poles values are not read.

!**   INTERFACE.
!     ----------
!       *CALL* *RDCLIMO*

!        EXPLICIT ARGUMENTS
!        --------------------
!        LDEZONE: TRUE if ezone reading is necessary
!        PFIELDE: OUTPUT EZONE FIELD  ARRAY
!        KGPE   : NUMBER OF OUTPUT POINTS IN EZONE
!        PFIELD : OUTPUT FIELDS ARRAY
!        KGP    : NUMBER OF OUTPUT POINTS
!        KFIELDS: NUMBER OF OUTPUT FIELDS
!        KCOD   : FIELDS CODES
!        KLI    : CLIMATOLOGY OPTION (1 => constant values ;
!                                     2 or 3 => monthly values)
!        LDMASK : logical mask in order to skip fields.

!        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
!        g radnoti : 95-02-28: aladin case
!        K. YESSAD (MARCH 1997): parallelisation of FULL-POS.
!        R. El Khatib : 97-11-07 : cleanups
!        R. El Khatib : 98-02-26 : LFPMOIS
!        R. El Khatib : 98-07-23 : cleanups
!        Modified 98-08-10 by K. YESSAD: removal of LRPOLE option.
!         - remove LRPOLE in YOMCT0 and in 'CALL CHIEN'.
!    G. Radnoti   : 98-09-09 : read EZONE arrays
!    R. El Khatib : 98-10-16 : LDMASK (for DM efficiency) ; LFPOSBUF
!    R. El Khatib : 98-12-31 use INI1WRFP
!    R. El Khatib : 99-07-20 use OPENFPFA + SUPPDATE + INI3WRFP
!    R. El Khatib : 99-12-27 remove IPTR0 from INI1WRFP
!    R. El Khatib : 00-04-07 Bugfix on offset
!    R. El Khatib : 01-03-16 IDATE(10)=time of the previous event ; LRFILAF
!    R. El Khatib : 03-04-17 Fullpos improvemnts
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!    R. El Khatib : 05-02-22 Change interface to SUPPDATE
!    R. El Khatib : 05-03-15 Cleanings
!     ------------------------------------------------------------------

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

USE YOMLUN   , ONLY : NULOUT
USE PARFPOS  , ONLY : JPOSLEN
USE YOMFPC   , ONLY : CFPFMT   ,CFPDOM   ,NFPDOM   ,LFPMOIS
USE YOMCT0   , ONLY : LELAM
USE YOMFPD   , ONLY : NLAT     ,NLON     ,NFPLUX   ,NFPGUX
USE YOMFPG   , ONLY : NFPRGRI  ,LFPOSBUF
USE YOMCT1   , ONLY : LRFILAF
USE YOMAFN   , ONLY : GFP_PHYDS

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KGPE 
INTEGER(KIND=JPIM),INTENT(IN)    :: KGP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFIELDS 
LOGICAL           ,INTENT(IN)    :: LDEZON 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFIELDE(KGPE,KFIELDS) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFIELD(KGP,KFIELDS) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCOD(KFIELDS) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLI 
LOGICAL           ,INTENT(IN)    :: LDMASK(KFIELDS) 
! IULFP    : logical unit number of output files
! IPTR     : pointer of each subdomain in the general output array
! ZREAL    : field read on a file ARPEGE(/ALADIN), 
!            including poles(/possible extension zone)
! CLLEC    : name of the frame of an output climate file
! IDTIME   : time indicator for SUPPDATE

INTEGER(KIND=JPIM) :: IDATE(11), IULFP(NFPDOM)

REAL(KIND=JPRB), ALLOCATABLE :: ZREAL(:)

CHARACTER (LEN = (15+JPOSLEN)) :: CLFPFN(NFPDOM)
CHARACTER (LEN = 16) ::  CLLEC(NFPDOM)
CHARACTER (LEN = 16) :: CLNAME
CHARACTER (LEN = 1) :: CLCONF

LOGICAL :: LLCOSP
INTEGER(KIND=JPIM) :: IPTR(NFPDOM)

!     Useless :
LOGICAL :: LLDYN
INTEGER(KIND=JPIM) :: IREC(NFPDOM), ISIZEG(NFPDOM), ISIZPK(NFPDOM)
INTEGER(KIND=JPIM) :: IFIELDST, IBARP

INTEGER(KIND=JPIM) :: ILENZ, IND, IND1,IOFF, IREP, ISTAE,ISTART, ISTCI,IXLAT,&
 & IXLON, JD, JFLD, JFPGL, JFPLON,JI, IDTIME, IFPCT0  
INTEGER(KIND=JPIM) :: IFXLAT, IFXLON ! number of latitudes & longitudes in file

LOGICAL :: LLFICP
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "ini1wrfp.intfb.h"
#include "ini3wrfp.intfb.h"
#include "openfpfa.intfb.h"
#include "suppdate.intfb.h"

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

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

IF (LHOOK) CALL DR_HOOK('RDCLIMO',0,ZHOOK_HANDLE)
LLCOSP=.FALSE.
CLCONF='0'
CALL INI1WRFP(CLCONF,LLCOSP,IFIELDST,IPTR,IREC,LLDYN,ISIZEG,ISIZPK)

IFPCT0=-9
CALL INI3WRFP(CLFPFN,CLLEC,IULFP,IBARP,IFPCT0)

IF (LFPMOIS) THEN
! Date of the starting file
  IDTIME=1
ELSE
! Date of the model
  IDTIME=0
ENDIF
CALL SUPPDATE(KDTIME=IDTIME,KDIME=1,KPPDATE=IDATE)

!*       2. LOOP ON SUBDOMAINS
!           ------------------

DO JD=1,NFPDOM

!*       2.1 OPEN AND CHECK FILE

  CALL OPENFPFA(JD,IULFP(JD),CLFPFN(JD),KLI,IDATE,IFXLAT,IFXLON,LLFICP,LRFILAF)

!*       2.4 READ FIELDS

  ILENZ=IFXLAT*IFXLON
  ALLOCATE(ZREAL(ILENZ))
  DO JFLD=1,KFIELDS

    CLNAME=GFP_PHYDS(KCOD(JFLD))%CLNAME

    IF (LDMASK(JFLD)) THEN
      WRITE(NULOUT,FMT='('' OUTPUT '',A16,'' SKIPPED BY MYPROC '')') CLNAME
      CYCLE
    ELSE
      WRITE(NULOUT,FMT='('' OUTPUT '',A16, &
       & '' READ FROM ARPEGE/ALADIN CLIMATE FILE '',A7)') CLNAME, CFPDOM(JD)  
    ENDIF

    CALL FACILE(IREP,IULFP(JD),CLNAME(1:4),1,CLNAME(5:16),ZREAL,.FALSE.)

    IF (CFPFMT == 'GAUSS'.OR.(.NOT.LELAM.AND.CFPFMT == 'MODEL')) THEN
!           Gaussian grid
      IF (LLFICP) THEN
!             forget the poles
        IOFF=NFPRGRI(1)
      ELSE
!             straight copy
        IOFF=0
      ENDIF
      DO JI=1,KGP
        PFIELD(JI,JFLD)=ZREAL(JI+IOFF)
      ENDDO
    ELSE
!           Limited area grid            
!           Select the "core" area  from the "extension" : 
      IF (CFPFMT == 'LALON') THEN
        IXLAT=NLAT(JD)
        IXLON=NLON(JD)
      ELSE
        IF (LFPOSBUF) THEN
          IXLAT=NFPGUX
          IXLON=NFPLUX
        ELSE
          IXLAT=NLAT(JD)
          IXLON=NLON(JD)
        ENDIF
      ENDIF
      ISTCI=0
      ISTAE=0
      DO JFPGL=1,IXLAT
        DO JFPLON=1,IXLON
          PFIELD(ISTCI+JFPLON+IPTR(JD)-1,JFLD)=ZREAL(ISTAE+JFPLON)
        ENDDO
        ISTAE=ISTAE+IFXLON
        ISTCI=ISTCI+IXLON
      ENDDO
!           Select the extension
      IF (LDEZON) THEN
        IND1=1
        DO JFPGL=1,NLAT(1)
          ISTART=1+NFPLUX*MIN(1,NFPGUX/JFPGL)
          DO JFPLON=ISTART,NLON(1)
            IND=(JFPGL-1)*NLON(1)+JFPLON
            PFIELDE(IND1,JFLD)=ZREAL(IND)
            IND1=IND1+1
          ENDDO
        ENDDO
      ENDIF
    ENDIF

  ENDDO
  DEALLOCATE(ZREAL)

!*       2.5 CLOSE FILE

  CALL FAIRME(IREP,IULFP(JD),'UNKNOWN')

ENDDO

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