SUBROUTINE PPOBSAZ(KDLEN,KLEN,KDCOUNT,CDPAR,KOBSTYPE,KCOUNT,PZO,&
 & POROG,KSLCT,LDEXTLI,&
 & PXZ5,PXZD5,PGEOPH5,PGEOPF5,PRESH5,PRESF5,&
 & PTSTAR5,&
 & KDPP,PXPP)  

!**** PPOBSAZ - POST-PROCESSING

!     D. VASILJEVIC          ECMWF           91-01-03

!     PURPOSE
!     -------

!        FIND OUT AN EQUIVALENT MODEL PARAMETER FOR A GIVEN OBSERVED.
!        HEIGHT VERTICAL COORDINATE

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

!        CALL PPOBSAZ(KDLEN,KLEN,KDCOUNT,CDPAR,KOBSTYPE,KCOUNT,PZO,
!    C                POROG,KSLCT,LDEXTLI,
!    I                PXZ5,PXZD5,PGEOPH5,PGEOPF5,
!    O                KDPP,PXPP)
!     WHERE KDLEN   : FIRST DIMENSION (OBSERVATIONS)
!           KLEN    : ACTUAL NUMBER OF OBSERVATIONS
!           KDCOUNT : SECOND DIMENSION (Data)
!           CDPAR   : PARAMETER FOR POST-PROCESSING
!           KOBSTYPE: OBS. TYPE
!           KCOUNT  : COUNTER FOR THE PARTICULAR OBS. PARAMETER
!           PZO     : OBSERVED HEIGHTS
!           POROG   : OROGRAPHY
!           KSLCT   : INTERPOLATION CHOICE
!           LDEXTLI : LINEAR EXTRAPOLATION BELOW MODEL'S SURFACE
!           PXZ5    : INTERMEDIATE ARRAY
!           PXZD5   : INTERMEDIATE ARRAY
!           PGEOPH5 : HALF LEVEL GEOPOTENTIALS
!           PGEOPF5 : FULL LEVEL GEOPOTENTIALS
!           PRESH5  : HALF LEVEL PRESSURES
!           PRESF5  : FULL LEVEL PRESSURES
!           KDPP    : Number of fields, dimension of PXPP.
!           PXPP    : P (POST-PROCESSED)

!        PPOBSAZ IS CALLED FROM SURFACEO

!**   METHOD
!     ------

!        CALL TO AN APPROPIRIATE POST-PROCESSING ROUTINE

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

!        PPZHLEV - FIND HEIGHT LEVEL UNDER SPECIFIED HEIGHTS
!        PPPS    - Ps POST-PROCESSING

!**   MODIFICATIONS
!     -------------
!     L. Isaksen  96-02-20 : NFLEV --> NFLEVG
!     E.Andersson 97-11-14 : GOM-variables more flexible
!     E.Andersson 98-06-11 : Pass dimension as argument

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

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

USE YOMDIM   , ONLY : NFLEVG   ,NPPM
USE YOMLUN   , ONLY : NULERR
USE GOMS_MIX     , ONLY : LGOMUA,YGOMV

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KDLEN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEN 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDCOUNT
CHARACTER(LEN=3)  ,INTENT(IN)    :: CDPAR 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBSTYPE 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCOUNT(KDLEN) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PZO(KDLEN,KDCOUNT) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POROG(KDLEN) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KSLCT 
LOGICAL           ,INTENT(IN)    :: LDEXTLI 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXZ5(KDLEN,0:NFLEVG,NPPM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXZD5(KDLEN,0:NFLEVG,NPPM) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOPH5(KDLEN,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOPF5(KDLEN,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRESH5(KDLEN,0:NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRESF5(KDLEN,NFLEVG) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSTAR5(KDLEN) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDPP 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PXPP(KDLEN,KDCOUNT,KDPP) 
INTEGER(KIND=JPIM), ALLOCATABLE :: ILEVB (:,:,:)
LOGICAL, ALLOCATABLE :: LLBELO(:,:),LLBLOW(:),LLBELS(:,:),LLBLES(:)
INTEGER(KIND=JPIM) :: ILEN,IMXCNT,INPP
LOGICAL :: LLEXTR
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "ppps.intfb.h"
#include "ppzhlev.intfb.h"

!     ------------------------------------------------------------------
!*    1.  PREPARATION
!*    1.1 MAX. NO. OF LEVELS

IF (LHOOK) CALL DR_HOOK('PPOBSAZ',0,ZHOOK_HANDLE)
ILEN   = KLEN
IMXCNT = MAXVAL(KCOUNT(1:ILEN))
IF(IMXCNT == 0) GOTO 9
INPP=0

!*    1.2 FIND OUT HEIGHT LEVEL BELOW SPECIFIED (OBSERVED) HEIGHTS

ALLOCATE(ILEVB(KDLEN,IMXCNT,NPPM))
ALLOCATE(LLBELO(KDLEN,IMXCNT))
ALLOCATE(LLBELS(KDLEN,IMXCNT))
ALLOCATE(LLBLOW(IMXCNT))
ALLOCATE(LLBLES(IMXCNT))
CALL PPZHLEV(KDLEN,1,ILEN,NFLEVG,IMXCNT,&
 & PZO  ,PGEOPH5,PGEOPF5,&
 & ILEVB,&
 & LLBELO,LLBELS,LLBLOW,LLBLES)  

!*    2.  Ps COMPONENT

IF(CDPAR == 'PS')     THEN
  IF(.NOT. (LGOMUA(KOBSTYPE,YGOMV%MT) .AND. LGOMUA(KOBSTYPE,YGOMV%MQ))) &
   & CALL ABOR1('ERROR PPOBSAZ: PS REQUIRES T AND Q IN GOM')  
  IF(INPP+1 > KDPP) CALL ABOR1('PPOBSAZ: KDPP EXCEEDED, PS')
  INPP=INPP+1

  LLEXTR = .TRUE.
  CALL PPPS(KDLEN,1,ILEN,NFLEVG,IMXCNT,1,ILEVB,KSLCT,&
   & LDEXTLI,PZO,LLBELO,LLBELS,LLBLOW,LLBLES,LLEXTR,&
   & POROG,&
   & PGEOPH5,PGEOPF5,PXZ5,PXZD5,PRESH5,PRESF5,PTSTAR5,&
   & PXPP(1,1,INPP))  

!*    3.  UNKNOWN PARAETER

ELSE
  WRITE(NULERR,'('' *** REQUESTED PARAMETER='',A)') CDPAR
  CALL ABOR1('ERROR PPOBSAZ: REQUESTED PARAMETER')
ENDIF

!*    9.  FINALLY, DEALLOCATE AND RETURN

DEALLOCATE(ILEVB,LLBELO,LLBELS,LLBLOW,LLBLES)

9 CONTINUE
IF (LHOOK) CALL DR_HOOK('PPOBSAZ',1,ZHOOK_HANDLE)
END SUBROUTINE PPOBSAZ

