SUBROUTINE ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,PLAT,PLON,PTIMDIF,&
 & KVNM,KSTAT,PPRES,PPRL,&
 & PPERR,POERR,PFERR,PWEIGHT)  

!**** SUBROUTINE DRIBUIN - PROCESS INPUT DRIBU

!        D. VASILJEVIC     ECMWF     13/12/00 (original 04/07/94)

!     PURPOSE
!     -------

!        PROCESS INPUT DRIBU

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

!        CALL ERRSTAT(KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,PLAT,PLON,PTIMDIF,
!                     KVNM,KSTAT,PPRES,PPRL,
!                     PPERR,POERR,PFERR,PWEIGHT)
!     WHERE:
!            KOBTYP  = OBS. TYPE
!            KCDTYP  = CODE TYPE
!            KOBSQNO = OBS. TYPE SQ. NO.
!            KCDSQNO = CODE TYPE SQ. NO.
!            PLAT    = LAT.
!            PLON    = LAT.
!            PTIMDIF = TIME DIFFERENCE
!            KVNM    = VARIABLE NO.
!            KSTAT   = STATUS
!            PPRES   = PRESSURE
!            PPRL    = REFERENCE LEVEL PRESSURE
!            PPERR   = PERSISTENCE ERROR
!            POERR   = OBSERVATION ERROR
!            PFERR   = FINAL OBSERVATION ERROR
!            PWEIGHT = WEIGHTING FACTOR

!        ERRSTAT IS CALLED BY SUBROUTINE ????????

!     METHOD
!     ------

!     EXTERNALS
!     ---------

!        OBSPERR  - OBS. PRSISTENCE ERROR
!        OBSERR   - OBS. ERROR
!        FINOERR  - FINAL OBS. ERROR

!     MODIFICATIONS
!     -------------
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Hamrud      01-Dec-2003 CY28R1 Cleaning

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

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

USE YOMANCS  , ONLY :      RMDI     ,RADIANS  ,EPSMAC   ,&
 & RTICE    ,RTWAT    ,R2ES     ,R3IES    ,&
 & R3LES    ,R4IES    ,R4LES    ,RTT  
USE YOMMKODB , ONLY : LERRPERS
USE YOMLUN   , ONLY : NULOUT

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

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KOBTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KOBSQNO 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCDTYP 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCDSQNO 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLAT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLON 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTIMDIF 
INTEGER(KIND=JPIM),INTENT(IN)    :: KVNM 
INTEGER(KIND=JPIM),INTENT(INOUT) :: KSTAT 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPRES 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PPRL 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PPERR 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: POERR 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFERR 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PWEIGHT 
LOGICAL :: LLMESAGE

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

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "finoerr.intfb.h"
#include "obserr.intfb.h"
#include "obsperr.intfb.h"

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

#include "fcobs.h"
#include "stmfun_ifs.h"

!*

!        1.             PRESET
!                       ------

IF (LHOOK) CALL DR_HOOK('ERRSTAT',0,ZHOOK_HANDLE)
LLMESAGE = .FALSE.

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

!        2.             ERROR STATISTICS
!                       ----------------

!*          2.1         PERSISTENCE ERROR

IF(LERRPERS)      THEN
  CALL OBSPERR(KVNM,PPRES,PLAT,PTIMDIF,&
   & PWEIGHT,PPERR)  
  IF(PPERR == RMDI)        THEN
    WRITE(NULOUT,'(''0ERRSTAT; PERSISTENCE ERROR'',&
     & '' NOT ASSIGNED,'',&
     & '' HENCE BODY ENTRY REJACTED:'')')  
    KSTAT = ICHSTAT_REJECT( KSTAT )
    LLMESAGE = .TRUE.
  ENDIF
ENDIF

!*          2.2         OBSERVATION ERROR

CALL OBSERR(KVNM,KOBTYP,KOBSQNO,KCDTYP,KCDSQNO,PLAT,PLON,&
 & PPRES,&
 & POERR)  
IF(POERR == RMDI)        THEN
!  WRITE(NULOUT,'(''0ERRSTAT; OBSERVATION ERROR'',&
!   & '' NOT ASSIGNED,'',&
!   & '' HENCE BODY ENTRY REJECTED FOR:'')')  
  KSTAT = ICHSTAT_REJECT( KSTAT )
  LLMESAGE = .FALSE.
ENDIF

!*          2.3         FINAL OBSERVATION ERROR

CALL FINOERR(PPERR,POERR,PFERR)
!*
!-----------------------------------------------------------------------

!        3.             IF OUTPUT MESSAGE; PRINT LAT/LON
!                       --------------------------------

3 CONTINUE
IF(LLMESAGE)           THEN
  WRITE(NULOUT,'(''    LAT         : '',F10.2)') PLAT
  WRITE(NULOUT,'(''    LON         : '',F10.2)') PLON
  WRITE(NULOUT,'(''    OBS TYPE    : '',I10)  ') KOBTYP
  WRITE(NULOUT,'(''    CODE TYPE   : '',I10)  ') KCDTYP
  WRITE(NULOUT,'(''    VARIABLE    : '',I10)  ') KVNM
  WRITE(NULOUT,'(''    PRESSURE    : '',F10.2)') PPRES
  WRITE(NULOUT,'(''    PRESSURE RL : '',F10.2)') PPRL
ENDIF
!*
!-----------------------------------------------------------------------

!        4.             RETURN
!                       ------

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