
C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C
C SUBPROGRAM:    OEFG01
C   PRGMMR: D. A. KEYSER     ORG: NP22       DATE: 2007-09-14
C
C ABSTRACT: FUNCTION WHICH RETURNS THE OBSERVATION ERROR FOR A
C   REQUESTED VARIABLE INTERPOLATED TO A DEFINED PRESSURE LEVEL FOR A
C   DEFINED REPORT TYPE.  IT IS OBTAINED FROM AN INPUT ARRAY CONTAINING
C   OBSERVATION ERRORS ON FIXED PRESSURE LEVELS BY VARIABLE AND REPORT
C   TYPE (READ EARLIER FROM THE EXTERNAL OBSERVATION ERROR TABLE)
C
C PROGRAM HISTORY LOG:
C 1995-05-17  J. WOOLLEN (NP20) - ORIGINAL AUTHOR (FUNCTION OEF)
C 2007-09-14 D. A. KEYSER -- MODIFIED TO USE EXACT LOGIC AS IN GSI
C    (MINIMUM LIMITING VALUE FOR OBS ERROR BASED ON VARIABLE TYPE,
C     LEVEL PRESSURE LIMITED TO MAX OF 2000 MB AND MIN OF ZERO MB, A
C     FEW OTHER MINOR CHANGES)
C
C USAGE:    XX = OEFG01(P,TYP,IE,OEMIN)
C   INPUT ARGUMENT LIST:
C     P        - REAL PRESSURE LEVEL (MB) TO INTERPOLATE OBS ERROR TO
C     TYP      - REAL PREPBUFR REPORT TYPE
C     IE       - VARIABLE TYPE BEING INTERPOLATED (=2 - TEMPERATURE,
C              - =3 - MOISTURE, =4 - WIND, =5 - SURFACE PRESSURE, =6 -
C              - PRECIPITABLE WATER)
C              - (USED ONLY IN PREVENTS MODE)
C     OEMIN    - REAL MINIMUM VALUE FOR OBS ERROR (FOR VARIABLE BEING
C              - INTERPOLATED)
C
C REMARKS: 'OEFG01' RETURNED IS OBSERVATION ERROR FOR VARIABLE "IE" IN
C     REPORT TYPE "TYP", INTERPOLATED TO PRESSURE LEVEL "P".
CC
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN 90
C   MACHINE:  IBM-SP
C
C$$$
      FUNCTION OEFG01(P,TYP,IE,OEMIN)

      COMMON /GBEVDD/ERRS(300,33,6)

      OEFG01 = 10E10

C  LOOK UP ERRORS FOR PARTICULAR OB TYPES
C  --------------------------------------

      KX = TYP

      P = MAX(0.,MIN(P,2000.))

      IF(P.GE.ERRS(KX,1,1)) K1 = 1

      DO KL = 1,32
         IF(P.GE.ERRS(KX,KL+1,1).AND.P.LE.ERRS(KX,KL,1))  K1 = KL
      ENDDO

      IF(P.LE.ERRS(KX,33,1)) K1 = 5

      K2 = K1 + 1

      EDIFF = ERRS(KX,K2,1) - ERRS(KX,K1,1)
      IF(ABS(EDIFF).GT.0.0)  THEN
         DEL = (P - ERRS(KX,K1,1))/EDIFF
      ELSE
         DEL = 1.E11
      ENDIF

      DEL = MAX(0.,MIN(DEL,1.0))

      OEFG01 = ((1.0 - DEL) * ERRS(KX,K1,IE)) + (DEL * ERRS(KX,K2,IE))
      OEFG01 = MAX(OEFG01,OEMIN)

C  SET MISSING ERROR VALUE TO 10E10
C  --------------------------------

      IF(OEFG01.GE.5E5) OEFG01 = 10E10

      RETURN

      END
