SUBROUTINE INCLI9

!**** *INCLI9*

!     PURPOSE.
!     --------

!      Interpolation of aerosol fields

!**   INTERFACE.
!     ----------

!     CALL INCLI9

!     METHOD.
!     -------

!      The data are read on unit 11, if available, then interpolated.
!      Finally they are written on ARPEGE clim file (unit 3).

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

!     GEO923
!     INTER2
!     FA-LFI package (FAITOU,FACILE,FAIENC,LFILAF,FAIRME)
!     CHIEN

!     AUTHORS.
!     --------

!         D. GIARD  SEP 04 (from INCLIR modified by F. BOUYSSEL)

!     MODIFICATIONS.
!     --------------
!        Modified 95-06-06 by L.Isaksen : Reordering of spectral arrays
!        Modified 98-08-10 by K. YESSAD: partial removal of LRPOLE option.
!         - LRPOLE has been removed from YOMCT0 and 'CALL CHIEN'
!           (assumed to be F).
!         - Call to POLADD and write poles on FA files is still made.
!        Modified 00-09-12 by D. Giard : adapted to new 923, one month, DM
!        Modified 00-10-19 by D. Giard : modifications from R. Mladek (97)
!        Modified 03-10-01 by M.Hamrud : CY28 Cleaning
!        Modified 04-06-15 by F. Bouyssel : change of format and scaling for input
!          climatological data, AEROS.URBAN is renamed by AEROS.SOOT accordingly
!        Modified 04-09-15 by D. Giard : new version renamed INCLI9 and phased
!                                        INCLIR kept for older versions

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

USE YOMDIM   , ONLY : NDGLG    ,NDGSAG   ,NDGENG   ,NDLON    ,&
 & NFLEVG   ,NSMAX
USE YOMLUN   , ONLY : NULOUT
USE YOMCT0   , ONLY : NQUAD
USE YOMLEG   , ONLY : RMU
USE YOMGEM   , ONLY : NLOENG   ,NMENG    ,VALH     ,VBH      ,&
 & NSTTYP   ,NHTYP    ,RMUCEN   ,RLOCEN   ,RSTRET   ,VP00  
USE YOMCLI   , ONLY : NPINT    ,NDATX    ,NDATY    ,LIEEE

IMPLICIT NONE

INTEGER(KIND=JPIM) :: JPBX
INTEGER(KIND=JPIM) :: JPBY
INTEGER(KIND=JPIM) :: JPNCH

PARAMETER (JPBX=2,JPBY=2,JPNCH=4)

!     JPBY : Extra-latitudes 
!          2 when INTER2 or INTER3 are used (even NDATY)
!          1 when INTER4 or INTER5 are used (odd  NDATY)
!     JPNCH: Number of fields to be processed (caution if not 4)

REAL(KIND=JPRB) :: ZS(NDGLG*NDLON,JPNCH)
REAL(KIND=JPRB) :: ZBO(NDGLG+1),ZLONG(NDGLG),ZMU(NDGLG*NDLON),ZSLA(NDGLG*NPINT)&
 & ,ZSLO(NDLON*NPINT,NDGLG),ZCLO(NDLON*NPINT,NDGLG)  
REAL(KIND=JPRB),ALLOCATABLE :: ZFLD(:,:), ZZLA(:), ZZLO(:)
REAL(KIND=JPRB) :: ZEPS, ZSCALE
REAL(KIND=JPRB) :: ZHOOK_HANDLE

INTEGER(KIND=JPIM) :: IARI, IARP, ICO, IDATX, IDATY, IFLD, IINF, ILENE, ILENT,&
 & IMES, INIQ, INIV, INJQ, INUM, IOS, IREP, JF, J, JJ  

CHARACTER :: CLNOMC*16, CLNOMF*10, CLFORM*12
CHARACTER :: CLPRE(JPNCH)*8, CLSUF(JPNCH)*12

LOGICAL :: LLCOSP, LLIMST, LLPOLE, LLDATA, LLOPEN

#include "abor1.intfb.h"
#include "chien.intfb.h"
#include "geo923.intfb.h"
#include "inter2.intfb.h"

!     ------------------------------------------------------------------
!*
!     1. SET INITIAL VALUES.
!        -------------------

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

!     Constants

ZEPS=1.E-10_JPRB
ZSCALE=1._JPRB

!     Grids

ICO=NPINT*NPINT
INJQ=NDGLG*NPINT
INIQ=NDLON*NPINT
ILENT=NDGLG*NDLON
IDATX=NDATX+2*JPBX
IDATY=NDATY+2*JPBY

ILENE=0
CALL GEO923(NPINT,ILENE,ZBO,ZLONG,ZMU,ZSLA,ZSLO,ZCLO)

!     ARPEGE fields

INIV=0
LLCOSP=.FALSE.
DO JF=1,JPNCH
  CLPRE(JF)='SURF    '
ENDDO
CLSUF(1)='AEROS.SEA   '
CLSUF(2)='AEROS.LAND  '
CLSUF(3)='AEROS.SOOT  '
CLSUF(4)='AEROS.DESERT'
ZS(:,:)=0.0_JPRB

!     ARPEGE file identifiers

INUM=3
IREP=0
IARI=0
IARP=1
CLNOMF='Const.Clim'
CLNOMC='Const.Clim.Surfa'
LLIMST=.TRUE.
IMES=1

IINF=0
LLPOLE=.TRUE.

!     Data type

IF (LIEEE) THEN
  CLFORM='UNFORMATTED'
  CALL ABOR1('INCLI9 : STANDARD INPUT FILES NOT YET READY !')
ELSE
  CLFORM='FORMATTED'
ENDIF

!     ------------------------------------------------------------------
!*
!     2. OPEN FILES.
!        -----------

!     ARPEGE file

CALL FAITOU(IREP,INUM,.TRUE.,CLNOMF,'OLD',.TRUE.,LLIMST,&
 & IMES,IARP,IARI,CLNOMC)  
CALL CHIEN(CLNOMC,NSTTYP,RMUCEN,RLOCEN,RSTRET,NSMAX,&
 & NDGLG,NDLON,NLOENG,NMENG,RMU,NHTYP,NFLEVG,VP00,VALH,VBH,&
 & NQUAD,IINF,NDGSAG,NDGENG,ZEPS,LLPOLE,NULOUT)  
IF (LLPOLE) CALL ABOR1(' CLIM. FILES MUST NOT HAVE POLES !')

!     External input data

LLDATA=.FALSE.
LLOPEN=.FALSE.
IOS= 0
INQUIRE(FILE='aero_GL',IOSTAT=IOS,EXIST=LLDATA,OPENED=LLOPEN)
LLDATA= LLDATA .AND. (IOS == 0) .AND. .NOT.LLOPEN

IF (LLDATA) THEN
  OPEN(UNIT=11,FILE='aero_GL',FORM=CLFORM)
ELSE
  CALL ABOR1('INCLI9 : INPUT DATA NOT AVAILABLE !')
ENDIF

!     ------------------------------------------------------------------
!*
!     3. READ AND INTERPOLATE DATA.
!        --------------------------

!     Allocate temporary space

ALLOCATE ( ZFLD(IDATX,IDATY) )
ALLOCATE ( ZZLA(NDATY) )
ALLOCATE ( ZZLO(NDATX) )

!     Read file header

READ(11,9901) ZZLA
READ(11,9901) ZZLO
9901 FORMAT(5F15.4)

!     Loop on fields

DO JF=1,JPNCH

!     Read and scale data
  ZFLD(:,:)=0.0_JPRB
  DO JJ=JPBY+1,JPBY+NDATY
    READ (11,9902) (ZFLD(J,JJ),J=JPBX+1,JPBX+NDATX)
  ENDDO

!     Horizontal interpolation (12 points)
  IFLD=1
  CALL INTER2(NDGLG,NDLON,IDATY,IDATX,IFLD,NPINT,ILENE,ICO,INJQ,INIQ,&
   & NLOENG(1),ILENT,ZS(1,JF),ZFLD,ZSLA,ZSLO,ZCLO)  

!    Control and scale interpolated data
  DO J=1,ILENE
    ZS(J,JF)=MAX(0.0_JPRB,ZS(J,JF))/ZSCALE
  ENDDO

ENDDO
9902 FORMAT(12F10.6)

!     Deallocate temporary space

DEALLOCATE ( ZFLD )
DEALLOCATE ( ZZLA )
DEALLOCATE ( ZZLO )

!     Close input file

CLOSE(11)

!     ------------------------------------------------------------------
!*
!     4. WRITE FIELDS.
!        -------------

!     Write fields

DO JF=1,JPNCH
  CALL FAIENC (IREP,INUM,CLPRE(JF),INIV,CLSUF(JF),ZS(1,JF),LLCOSP)
ENDDO

!     Close ARPEGE file

CALL LFILAF (IREP,INUM,.TRUE.)
CALL FAIRME (IREP,INUM,'KEEP')

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

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