SUBROUTINE FINDML(PCMPPP,KMODLE,PDIFF)

!**** SUBROUTINE FINDML - FIND CLOSEST QUASI MODEL PRESSURE LEVEL

!          LARS ISAKSEN    ECMWF          15/AUG/2000
!         (BASED ON FINDL BY HEIKKI JARVINEN)

!*    PURPOSE
!     -------

!        FIND CLOSEST PRESSURE LEVEL BASED ON STANDARD ATMOSPHERE FOR 
!        MODEL LEVELS OVER FLAT TERRAIN. THIS WILL APPROXIMATELY THIN
!        THE DATA TO MAXIMUM ONE OBSERVATION PER MODEL LEVEL BELOW 50 hPA

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

!     ***CALL FINDML(PCMPPP,KMODLE,PDIFF)
!     WHERE PCMPPP = GIVEN PRESSURE OF OBSERVATION (IN PASCALS)
!           KMODLE = INDEX OF CLOSEST MODEL PRESSURE LEVEL
!           PDIFF  = DISTANCE TO THIS STD- LEVEL (IN PASCALS)

!        FINDML IS CALLED BY SUBROUTINE LEVEL

!*    METHOD
!     ------

!*    EXTERNALS
!     ---------

!**   MODIFICATIONS
!     -------------

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

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

USE YOMSCC   , ONLY : SELAIREPPRE

IMPLICIT NONE

REAL(KIND=JPRB)   ,INTENT(IN)    :: PCMPPP 
INTEGER(KIND=JPIM),INTENT(OUT)   :: KMODLE 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFF 

INTEGER(KIND=JPIM) :: J, INUMLEVELS

REAL(KIND=JPRB) :: ZDIFF
REAL(KIND=JPRB), ALLOCATABLE :: ZSELECTPRE(:)
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!*
!     ------------------------------------------------------------------

!        1.       INITIALIZE
!                 ----------
!*
!     ------------------------------------------------------------------

!        2.       FIND CLOSEST QUASI MODEL LEVEL AND DISTANCE TO IT
!                 -------------------------------------------------

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

INUMLEVELS=SIZE(SELAIREPPRE)
ALLOCATE(ZSELECTPRE(INUMLEVELS))
ZSELECTPRE(:)=SELAIREPPRE(:)

KMODLE = 1
PDIFF  = ABS(PCMPPP - ZSELECTPRE(1))
DO J = 2, INUMLEVELS
  ZDIFF = ABS(PCMPPP - ZSELECTPRE(J))
  IF(ZDIFF < PDIFF) THEN
    PDIFF  = ZDIFF
    KMODLE = J
  ENDIF
ENDDO
DEALLOCATE(ZSELECTPRE)

!*
!     ------------------------------------------------------------------

!        3.       RETURN
!                 ------
IF (LHOOK) CALL DR_HOOK('FINDML',1,ZHOOK_HANDLE)
END SUBROUTINE FINDML

