SUBROUTINE FINOEREV(PERERR,POBSERR,PMDI,PFINOERR)

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

IMPLICIT NONE

REAL(KIND=JPRB)   ,INTENT(IN)    :: PERERR 
REAL(KIND=JPRB)   ,INTENT(IN)    :: POBSERR 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMDI 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFINOERR
 
REAL(KIND=JPRB) :: ZOERRSQ, ZPERRSQ
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!****  SUBROUTINE FINOEREV - EVALUATE FINAL PRE-PROCESSING OBS. ERROR

!         D. VASILJEVIC     ECMWF    21/10/94

!      PURPOSE
!      -------

!        FINAL PRE-PROCESSING OBS. ERROR

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

!        CALL FINOEREV(PERERR,POBSERR,PMDI,PFINOERR)
!     WHERE:
!           PERERR   = PERSISTENCE ERROR
!           PMDI     = MISSING IND.
!           POBSERR  = OBS. ERROR
!           PMDI     = MISSING IND.
!           PFINOERR = FINAL OBS. ERROR

!         FINOEREV IS CALLED BY SUBROUTINES

!     METHOD
!     ------

!        THE FINAL PRE-PROCESSING OBS. ERROR IS CREATED AS SQRT OF
!     PERSISTENCE AND OBSERVATION ERROR.

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

!        NONE

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

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

!*

!        0.       PRESET
!                 ------

IF (LHOOK) CALL DR_HOOK('FINOEREV',0,ZHOOK_HANDLE)
PFINOERR = PMDI
ZPERRSQ  = PMDI
ZOERRSQ  = PMDI
!*
!-----------------------------------------------------------------------

!        1.       FINAL PRE-PROCESSING OBS. ERROR
!                 -------------------------------

!*          1.0   BOTH ERRORS MISSING

IF(PERERR  /= PMDI .OR. POBSERR /= PMDI     ) THEN

!*          1.1   SQUARE PERSISTENCE ERROR

  IF(PERERR  /= PMDI) ZPERRSQ  = PERERR**2

!*          1.2   SQUARE OBSERVATION ERROR

  IF(POBSERR /= PMDI) ZOERRSQ  = POBSERR**2

!*          1.3   FINAL OBSERVATION ERROR


  IF(ZPERRSQ == PMDI)       THEN
    PFINOERR = POBSERR
  ELSEIF(ZOERRSQ == PMDI)  THEN
    PFINOERR = PERERR
  ELSE
    PFINOERR = SQRT(ZPERRSQ+ZOERRSQ)
  ENDIF
ENDIF
!*
!-----------------------------------------------------------------------

!        2.       RETURN
!                 ------


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

