SUBROUTINE ADDOER(KOBS,KBODY,PTFG,PMPS)

!****  ADDOER - INFLATE OBSERVATION ERROR FOR SYNOP Z/PS

!     HEIKKI JARVINEN          15/MAR/95        ECMWF
!     DRASKO VASILJEVIC        21/10/99         ECMWF

!     PURPOSE
!     -------

!        INCREASE OBSERVATION ERROR FOR SYNOP GEOPOTENTIALS IF THE STATION
!     ALTITUDE IF FAR FROM THE LEVEL FOR WHICH THE GEOPOTENTIAL IS REPORTED.
!     OR FOR SURFACE PRESSURES IF THE LEVEL FOR WHICH THE PRESSURE IS
!     REPORTED.

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

!     ***CALL*ADDOER(KOBS,KBODY,PTFG,PMPS)
!     WHERE KOBS   = OBSERVATION POINTER
!           KBODY  = POSITION OF THE DATUM IN ODB BODY
!           PTFG   = LOWEST MODEL LEVEL T
!           PMPS   = MODEL SURFACE PRESSURE

!        ADDOER IS CALLED FROM PRECH

!**   METHOD
!     ------

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

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

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMCST   , ONLY : RG       ,RD
USE YOMCOCTP , ONLY : NSYNOP
USE YOMANCS  , ONLY : RMDI
USE YOMVNMB  , ONLY : NVNUMB
USE YOMNMCOD , ONLY : NPRESCD
USE YOMNMEV  , ONLY : ND1EVENT
USE YOMDB

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KOBS 
INTEGER(KIND=JPIM),INTENT(IN)    :: KBODY 
REAL(KIND=JPRB), INTENT(IN) :: PMPS, PTFG
INTEGER(KIND=JPIM) :: ICMVCO, ICMVNM, IOBTYP, ISYPCD, IEVENT28

REAL(KIND=JPRB) :: ZCMFOE, ZCMOER, ZCMPER, ZCMPPP, ZINFOE, ZMODOR,&
 & ZPLEV, ZSTALT
REAL(KIND=JPRB) :: ZMPS, ZTFG

REAL(KIND=JPRB) :: ZDZ
REAL(KIND=JPRB) :: ZHOOK_HANDLE


#include "fcobs.h"

!        0.       STATEMENT FUNCTION FOR HEIGHT DIFF. OF PRESSURE LEVELS
!                 ------------------------------------------------------

ZDZ(ZPLEV) = (ZMODOR+(RD/RG)*ZTFG*LOG(ZMPS/ZPLEV)) - ZSTALT

!*                STATEMENT FUNCTION FOR HEIGHT AT A PRESSURE LEVEL
!     SZPLEV(PPLEV) = RD/G*RTSURF*LOG(RMPMSL/PPLEV) ! oi function
!*
!     ------------------------------------------------------------------

#include "openmp_obs.h"

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

!        2.       CALCULATE THE DISTANCE OF EXTRAPOLATION
!                 ---------------------------------------

!*          2.1   FIND OUT REPORT INFORMATION

IF (LHOOK) CALL DR_HOOK('ADDOER',0,ZHOOK_HANDLE)
IOBTYP = ROBHDR(KOBS,MDBOTP)
ZSTALT = ROBHDR(KOBS,MDBALT)
ZMODOR = ROBHDR(KOBS,MDB_OROGRAPHY)
!*
!           2.2   FIND OUT OBSERVATION INFORMATION

ICMVNM = ROBODY(KBODY,MDBVNM)
ICMVCO = ROBODY(KBODY,MDBVCO)
ZCMPPP = ROBODY(KBODY,MDBPPP)
ISYPCD = ROBODY(KBODY,MDBSYPC)
ZINFOE = 0.0_JPRB
!*
!           2.3   CHECK OBSERVATION CHARACTER

IF(IOBTYP /= NSYNOP)     GOTO 3
IF(ZSTALT == RMDI)       GOTO 3
!*
!           2.4   FIRST GUESS TEMPERATURE AT LOWEST MODEL LEVEL

ZTFG = PTFG
!*
!           2.5   MODEL SURFACE PRESSURE

ZMPS = PMPS
!*
!           2.6   CALCULATE THE OBSERVATION ERROR INFLATION

IF(ICMVNM == NVNUMB( 3)) THEN
  IF(ISYPCD == NPRESCD( 1)) ZINFOE = ABS(ZSTALT)
  IF(ISYPCD == NPRESCD(11)) ZINFOE = ABS(ZDZ(1000.E02_JPRB))
  IF(ISYPCD == NPRESCD(13)) ZINFOE = ABS(ZDZ( 925.E02_JPRB))
  IF(ISYPCD == NPRESCD(10)) ZINFOE = ABS(ZDZ( 900.E02_JPRB))
  IF(ISYPCD == NPRESCD( 3)) ZINFOE = ABS(ZDZ( 850.E02_JPRB))
  IF(ISYPCD == NPRESCD( 4)) ZINFOE = ABS(ZDZ( 700.E02_JPRB))
  IF(ISYPCD == NPRESCD(12)) ZINFOE = ABS(ZDZ( 500.E02_JPRB))
  ZINFOE = RG*0.02_JPRB*ZINFOE
ELSEIF(ICMVNM == NVNUMB(47)) THEN
  IF(ISYPCD == NPRESCD( 5)) ZINFOE = ABS(ZSTALT- 500.0_JPRB)
  IF(ISYPCD == NPRESCD( 6)) ZINFOE = ABS(ZSTALT-1000.0_JPRB)
  IF(ISYPCD == NPRESCD( 7)) ZINFOE = ABS(ZSTALT-2000.0_JPRB)
  IF(ISYPCD == NPRESCD( 8)) ZINFOE = ABS(ZSTALT-3000.0_JPRB)
  IF(ISYPCD == NPRESCD( 9)) ZINFOE = ABS(ZSTALT-4000.0_JPRB)
  ZINFOE = RG*0.02_JPRB*ZINFOE*1.225_JPRB
ELSE
  GOTO 3
ENDIF
!*
!           2.8   CHECK if OBSERVATION ERROR was increased

IEVENT28 = IBITS(INT(ROBODY(KBODY,MDBDEV1)),28-1,1)
IF(IEVENT28 == 1) GO TO 3
!*
!           2.8   INCREASE OBSERVATION ERROR

ZCMOER = ROBODY(KBODY,MDBOER)
ZCMOER = ZCMOER + ZINFOE
ROBODY(KBODY,MDBOER) = ZCMOER
!*
!           2.9   RECALCULATE FINAL OBSERVATION ERROR

ZCMPER = ROBODY(KBODY,MDBPER)
IF(ZCMPER == RMDI) THEN
  ZCMFOE = ZCMOER
ELSE
  ZCMFOE = SQRT(ZCMOER*ZCMOER + ZCMPER*ZCMPER)
ENDIF
ROBODY(KBODY,MDBFOE) = ZCMFOE
!*
!           2.10  SET EVENT

ROBODY(KBODY,MDBDEV1) = ZCHEV1( ROBODY(KBODY,MDBDEV1),ND1EVENT(28))
!*
!     ------------------------------------------------------------------

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