SUBROUTINE AER_STRFIND &
  &( KN, KM, PX, PY, PXC, PYC, PF, PVAL )

!*** *AER_STRFIND* - BI-LINEAR INTEROLATION FOR STRATOS.AEROSOLS

!**   INTERFACE.
!     ----------
!          *AER_STRFIND* IS CALLED FROM *AER_STRAERO*.

!     AUTHOR.
!     -------
!        FROM S.BEKKI's *find*
!        JJMorcrette  ECMWF 20080423 rewritten for IFS

! INPUTS 
! ------
!   PX : Partial pressure of H2O (mb)
!   PY : temperature (K)
!   PXC: Table partial pressure of H2O (mb)
!   PYC: Table temperature (K)
!   PF : Table aerosol H2SO4 weight fraction=f(XC,YC) (percent)


! OUTPUT
! ------ 
!   PVAL: aerosol H2SO4 weight fraction (percent)

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

IMPLICIT NONE
   
!-----------------------------------------------------------------------

!*       0.1   ARGUMENTS
!              ---------

INTEGER(KIND=JPIM),INTENT(IN) :: KN, KM

REAL(KIND=JPRB),INTENT(IN)  :: PX, PY, PXC(KN), PYC(KM), PF(KN,KM)

REAL(KIND=JPRB),INTENT(OUT) :: PVAL

  
!*       0.5   LOCAL VARIABLES
!              ---------------

!INTEGER(KIND=JPIM) :: IERX, IERY, IX, IY, IXP1, IYP1
INTEGER(KIND=JPIM) :: IX, IY, IXP1, IYP1
INTEGER(KIND=JPIM) :: JI

REAL(KIND=JPRB) :: ZSXY, ZSX1Y, ZSX1Y1, ZSXY1
REAL(KIND=JPRB) :: ZTA , ZTB  , ZT    , ZUA  , ZUB, ZU

!#include "aer_strposi.intfb.h"

REAL(KIND=JPRB) :: ZHOOK_HANDLE
!-----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('AER_STRFIND',0,ZHOOK_HANDLE)

!IERX=0
!IERY=0

!CALL AER_STRPOSI ( KN, IX, IERX, PXC, PX )
!CALL AER_STRPOSI ( KM, IY, IERY, PYC, PY )

!IF (IX == 0 .OR. IERY == 1) THEN
!  PVAL=99.99_JPRB
!  RETURN
!ENDIF

!IF (IY == 0 .OR. IERX == 1) THEN
!  PVAL=9.0
!  RETURN
!ENDIF

! replace SB bi-linear interpolation by something less convoluted!

IX=-999
DO JI=1,KN
  IF (PX < PXC(JI)) THEN
    IX=JI-1
  ENDIF
ENDDO
IY=-999
DO JI=1,KM
  IF (PY < PYC(JI)) THEN
    IY=JI-1
  ENDIF
ENDDO

IF (IX == 0 .OR. IY == -999) THEN

  PVAL=99.99_JPRB

ELSEIF (IY == 0 .OR. IX == -999) THEN

  PVAL=9.0_JPRB

ELSE

  IXP1 = IX+1
  IYP1 = IY+1
  ZSXY  = PF(IX,  IY  )
  ZSX1Y = PF(IXP1,IY  )
  ZSX1Y1= PF(IXP1,IYP1)
  ZSXY1 = PF(IX,  IYP1)

  ! x-slope.
  ZTA = PX       -PXC(IX)
  ZTB = PXC(IXP1)-PXC(IX)
  ZT  = ZTA/ZTB

  ! y-slope.
  ZUA = PY       -PYC(IY)
  ZUB = PYC(IYP1)-PYC(IY)
  ZU  = ZUA/ZUB

  ! Use bilinear interpolation to determine function at point X,Y.
  PVAL= (1.0_JPRB-ZT)*(1.0_JPRB-ZU)*ZSXY + ZT*(1.0_JPRB-ZU)*ZSX1Y + &
    &   ZT*ZU*ZSX1Y1                     + (1.0_JPRB-ZT)*ZU*ZSXY1

  IF (PVAL < 9.0_JPRB) PVAL=9.0_JPRB
  IF (PVAL > 99.99_JPRB) PVAL=99.99_JPRB
   
ENDIF

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