      SUBROUTINE RTHLOC(ERSTAT)
C**********************************************************************
C
C   NAME- RTHLOC
C
C   LANGUAGE- FORTRAN 77    TYPE- SUBROUTINE      COMPUTER- CRAY
C
C   VERSION- 1.0    DATE- 12/17/93 PROGRAMMER- FRANK TILLEY (SMSRC)
C   VERSION- 2.0    DATE- 05/06/94   PROGRAMMER- FRANK TILLEY (SMSRC)
C                   XATOV-3 REPLACE COMMON BLOCK /BUFFER/ WITH
C                   PARAMETER FILE INSPARM AND COMMON BLOCK /INSCOM/.
C   VERSION- 2.1    DATE- 11/14/94   PROGRAMMER- TERESA SIMMS (SMSRC)
C                   XATOV-70 REPLACE MSU CHANNELS 1 AND 2 WITH AMSU-A
C                   CHANNELS 3 AND 5.
C   VERSION- 3.0    DATE- 05/10/96 PROGRAMMER- ALEX POZDNYAK (FDC)
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT AND
C      R*4 WITH REAL.
C
C   VERSION- 3.0    DATE- 07/13/98 PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C
C   FUNCTION- THIS SUBROUTINE USES AMSU-A BRIGHTNESS TEMPERATURES TO
C             CHECK FOR EARTH LOCATION ERRORS IN A DATA FRAME.  IT
C             COMPARES BRIGHTNESS TEMPERATURES FOR AMSU-A CHANNELS 3
C             AND 5 AGAINST TEMPERATURE LIMITS FOR GIVEN TERRAIN
C             TYPES AND HEIGHTS.  A COUNT IS KEPT OF SPOTS THAT DO
C             NOT PASS THE SCREENING CRITERIA.  WHEN THE ENTIRE AMSU-A
C             DATA FRAME HAS BEEN CHECKED, THE PERCENTAGE OF SPOTS
C             WITH BAD EARTH LOCATIONS IS COMPUTED.  IF THIS PERCENTAGE
C             IS GREATER THAN ZERO BUT LESS THAN A LIMIT SET IN THE CDB,
C             A WARNING MESSAGE IS WRITTEN TO THE KIDS FILE AND PROCESSI
C             CONTINUES.  IF THE COMPUTED PERCENTAGE MATCHES OR EXCEEDS
C             THE LIMIT, A FATAL ERROR MESSAGE IS WRITTEN TO THE KIDS FI
C             REPORTING THE DATA FRAME NUMBER AND THE PERCENTAGE OF
C             SPOTS WITH BAD EARTH LOCATIONS, THEN AN ERROR STATUS
C             OF 99 IS RETURNED TO THE CALLING ROUTINE.
C
C   VERSION- 3.1 MODIFIED THE AMSU-A CHANNEL 3 AND 5 HARD CODED TEMP
C                LIMITS TO USE THE GROSS TEMPERATURE LIMITS.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C
C   FILES- NONE
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     ERSTAT             INT  O   THE ERROR RETURN CODE.
C                                    0 - SUCCESSFUL COMPLETION.
C                                   99 - DATA FRAME EARTH LOCATION
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  DESCRIPTION
C     --------          ----  -------------------------------------
C     BADSPT             INT  COUNTER FOR AMSU-A SPOTS WITH BAD EARTH
C                             LOCATIONS.
C     AMALAT            REAL  LATITUDE OF AN AMSU-A SPOT.
C     AMA3BT            REAL  AMSU-A CHANNEL 3 BRIGHTNESS TEMPERATURE(K)
C     AMA5BT            REAL  AMSU-A CHANNEL 5 BRIGHTNESS TEMPERATURE(K)
C     AMALNE             INT  LOOP INDEX FOR AMSU-A LINES IN THE DATA
C                             FRAME.
C     AMASPT             INT  LOOP INDEX FOR AMSU-A SPOTS IN AN AMSU-A
C                             SCAN LINE.
C     PCTBAD            REAL  PERCENTAGE OF SPOTS WITH BAD EARTH
C                             LOCATIONS IN AN AMSU-A DATA FRAME.
C     STATUS             INT  I/O STATUS RETURNED FROM RTHLO$.
C     TERHGT            REAL  TERRAIN HEIGHT FROM THE AMSU-A ANCILLARY
C                             BUFFER.
C     TERTYP            REAL  TERRAIN TYPE FROM THE AMSU-A ANCILLARY
C                             BUFFER.
C     TOTSPT             INT  COUNTER FOR TOTAL NUMBER OF SPOTS IN
C                             THE AMSU-A DATA FRAME.
C
C
C   SUBPROGRAMS CALLED-  RTHLO$
C
C   FUNCTIONS USED - ANINT, ABS
C
C
C
C   EXIT STATES- ERSTAT = 99 WHEN A FATAL ERROR OCCURS IN RTHLO$ OR
C                            WHEN EARTH LOCATION FOR THE DATA FRAME
C                            HAS BEEN DETERMINED TO BE BAD.
C
C**********************************************************************
C
      INCLUDE 'INPCDB'
      INCLUDE 'INSPARM'
      INCLUDE 'FRMINF'
      INCLUDE 'INSCOM'
      INCLUDE 'SPVAL'
C
      INTEGER  ERSTAT
      INTEGER  BADSPT, AMALNE, AMASPT, STATUS, TOTSPT
      REAL     AMALAT, PCTBAD, TERHGT, TERTYP, AMA3BT, AMA5BT
      LOGICAL   ISR4SPV
C
C  INITIALIZE VARIABLES.
C
      ERSTAT = 0
      TOTSPT = 0
      BADSPT = 0
      PCTBAD = 0.0
C
C  LOOP THROUGH THE LINES IN THE AMSU-A DATA FRAME.
C
      DO 20, AMALNE = 1, ATOTLN
C
C        IF THE AMSU-A LINE IS GOOD, LOOP THROUGH THE SPOTS ON THE LINE.
C
         IF (AGOODL(AMALNE)) THEN
            DO 10, AMASPT = 1, ANSPOT
C
C              KEEP A COUNT OF THE NUMBER OF SPOTS IN THE AMSU-A DATA
C              FRAME.
C
               TOTSPT = TOTSPT + 1
C
C              EXTRACT THE LATITUDE, TERRAIN HEIGHT, AND TERRAIN
C              TYPE FOR THE SPOT FROM THE AMSU-A ANCILLARY BUFFER.
C              EXTRACT THE BRIGHTNESS TEMPERATURES FOR CHANNELS
C              3 AND 5 FROM THE AMSU-A DATA BUFFER.
C
               AMALAT = AMAANC(AMASPT,AMALNE,1)
               AMALON = AMAANC(AMASPT,AMALNE,2)
               TERHGT = AMAANC(AMASPT,AMALNE,7)
               TERTYP = AMAANC(AMASPT,AMALNE,8)
               AMA3BT = AMADAT(AMASPT,AMALNE,3)
               AMA5BT = AMADAT(AMASPT,AMALNE,5)
C
C              IF THE LATITUDE FOR THE SPOT IS MISSING, SKIP TO THE
C              NEXT SPOT.
C
               IF (ISR4SPV(AMALAT)) GO TO 10
C
C              FOR LATITUDES 30 DEGREES EITHER SIDE OF THE EQUATOR,
C              IF THE TERRAIN TYPE IS LAND AND THE TERRAIN HEIGHT IS
C              LESS THAN 1500 METERS, THE EARTH LOCATION OF THE SPOT
C              IS BAD IF THE AMSU-A CHANNEL 3 BRIGHTNESS TEMPERATURE
C              FOR THE SPOT IS LESS THAN 240 DEGREES KELVIN.  IF THE
C              TERRAIN TYPE IS SEA, THE EARTH LOCATION OF THE SPOT
C              IS BAD IF THE AMSU-A CHANNEL 3 BRIGHTNESS TEMPERATURE
C              IS 270 DEGREES KELVIN OR GREATER.  KEEP A COUNT OF THE
C              NUMBER OF BAD SPOTS IN THE AMSU-A DATA FRAME.
C
C              CHANGED SO THAT IF THE TERRAIN TYPE IS LAND AND THE TERRA
C              HEIGHT IS LESS THAN 1500 METERS, THE EARTH LOCATION OF TH
C              IS BAD IF THE AMSU-A CHANNEL 3 BRIGHTNESS TEMPERATURE
C              FOR THE SPOT IS LESS THAN THE MINIMUM GROSS TEMPERATURE L
C              IF THE TERRAIN TYPE IS SEA, THE EARTH LOCATION OF THE SPO
C              IS BAD IF THE AMSU-A CHANNEL 3 BRIGHTNESS TEMPERATURE
C              IS GREATER THAN THE MAXIMUM GROSS TEMPERATURE LIMIT.  KEE
C              COUNT OF THE NUMBER OF BAD SPOTS IN THE AMSU-A DATA FRAME
C
               IF (ABS(AMALAT).LE.30.0) THEN
C
C                 SKIP TO THE NEXT SPOT IF THE AMSU-A CHANNEL 3
C                 BRIGHTNESS TEMPERATURE IS MISSING.
C
                  IF (ISR4SPV(AMA3BT)) GO TO 10
C
                  IF (ANINT(TERTYP).EQ.1.0.AND.TERHGT.LT.1500.0.AND.
     +                    AMA3BT.LT. AMINBT(3)) THEN
C
                         BADSPT=BADSPT+1
C
                  ELSEIF (ANINT(TERTYP).EQ.0.0.AND.AMA3BT.GE.AMAXBT(3))
     +                    THEN
C
                          BADSPT=BADSPT+1
C
                  END IF
C
C              FOR LATITUDES WITHIN 20 DEGREES OF EITHER POLE, THE
C              EARTH LOCATION OF THE SPOT IS BAD IF THE AMSU-A CHANNEL 5
C              BRIGHTNESS TEMPERATURE IS 253 DEGREES KELVIN OR GREATER.
C
C              CHANGED SO THAT FOR LATITUDES WITHIN 20 DEGREES OF EITHER
C              THE EARTH LOCATION OF THE SPOT IS BAD IF THE AMSU-A CHANN
C              BRIGHTNESS TEMPERATURE IS GREATER THAN THE MAXIMUM GROSS
C              TEMPERATURE LIMIT.
C
               ELSEIF (ABS(AMALAT).GE.70.0.AND.AMA5BT.GE.AMAXBT(5)) THEN
C
                  BADSPT=BADSPT+1
               END IF
   10       CONTINUE
         END IF
   20 CONTINUE
C
C          CALCULATE THE PERCENTAGE OF SPOTS IN THE DATA FRAME
C          THAT HAVE BAD EARTH LOCATIONS.
C
      IF (TOTSPT.NE.0) PCTBAD = ((BADSPT*1.0)/(TOTSPT*1.0))*100.0
C
C          IF THE PERCENTAGE OF BAD SPOTS IS GREATER THAN 0.0
C          BUT LESS THAN THE PERCENTAGE LIMIT FROM THE CDB,
C          WRITE OUT A WARNING MESSAGE TO THE KIDS FILE & RETURN
C          TO THE CALLING ROUTINE.
C
      IF (BADSPT.GT.0.AND.PCTBAD.LT.PCTMAX) THEN
C
C          WRITE A WARNING TO THE KIDS FILE.
C
         CALL RTHLO$(201,PCTBAD,BADSPT,TOTSPT,FRAMNO,STATUS)
         IF (STATUS.EQ.99) ERSTAT = 99
C
C          IF THE PERCENTAGE OF BAD SPOTS EQUALS OR EXCEEDS
C          THE PERCENTAGE LIMIT FROM THE CDB, WRITE OUT A
C          FATAL ERROR MESSAGE TO THE KIDS FILE, SET THE ERROR
C          STATUS TO 99, AND RETURN TO THE CALLING ROUTINE.
C
      ELSE IF (PCTBAD.GE.PCTMAX) THEN
C
C           WRITE A FATAL ERROR TO THE KIDS FILE.
C
         CALL RTHLO$(202,PCTMAX,BADSPT,TOTSPT,FRAMNO,STATUS)
         ERSTAT = 99
      END IF
C
      RETURN
      END
C*********************************************************************
      SUBROUTINE RTHLO$(MSGCOD,PERCNT,BADSPT,TOTSPT,FRAME,STATUS)
C*********************************************************************
C   NAME- RTHLO$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 11/30/93    PROGRAMMER- FRANK TILLEY (SMSRC)
C   VERSION- 3.0    DATE- 10/07/97    PROGRAMMER- LEAH WILSON (HSTX)
C
C   FILES-
C     DSN                      I/O    UNIT    FUNCTIONAL DESCRIPTION
C     ---                      ---    ----    -----------------------
C     ...KIDS                   O     KUNIT   KRAY INTERACTIVE DOCUMENTA
C                                             FILE.
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE RTHLOC.
C   VERSION- 3.0 WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT
C                INCLUDE UNIT NUMBERS IN ERROR MESSAGES WHERE APPLICABLE
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE               TYPE  I/O  FUNCTION
C     --------               ----  ---  --------
C     MSGCOD                  INT   I   MESSAGE CODE USED TO
C                                       IDENTIFY THE ERROR
C                                       PATTERN.
C     PERCNT                 REAL   I   PERCENTAGE OF SPOTS WITH
C                                       EARTH LOCATION ERRORS OR
C                                       PERCENTAGE LIMIT FROM THE
C                                       CDB.
C     BADSPT                  INT   I   NUMBER OF SPOTS WITH
C                                       EARTH LOCATION ERRORS.
C     TOTSPT                  INT   I   TOTAL NUMBER OF SPOTS IN
C                                       THE DATA FRAME.
C     FRAME                   INT   I   AMSU-A FRAME NUMBER IN THE
C                                       ORBIT.
C     STAT                    INT   O   STATUS CODE IF AN ERROR
C                                       OCCURS DURING I/O
C
      INTEGER   MSGCOD, BADSPT, TOTSPT, FRAME, STATUS
      REAL      PERCNT
C
C   SUBROUTINES CALLED- KIDS
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     NAME                    CH*6    THE NAME OF THIS ROUTINE
C     I                        INT    LOOP CONTROL INDEX
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C2TEMP                  CH*2    HOLDING BUFFER FOR WRITING TO KIDS
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C     C6TEMP                  CH*6    HOLDING BUFFER FOR WRITING TO KIDS
C
C
C   EXIT STATES-
C       STATUS =  0; INDICATING NON-FATAL OR NO ERRORS FOUND
C       STATUS = 99; INDICATING A FATAL ERROR WAS DETECTED
C
C*********************************************************************
C
      INTEGER     I
      CHARACTER*2   C2TEMP
      CHARACTER*4   C4TEMP
      CHARACTER*6   C6TEMP
      CHARACTER*100 KMSG
      CHARACTER*6 NAME
C
      STATUS = 0
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
      IF(MSGCOD.EQ.201) THEN
        KMSG(1:50)='* WARNING *        % of AMSU-A spots in fr.    had'
        KMSG(51:100)=' bad Earth Loc.(    /    ) spots w/errors.'
        WRITE(C6TEMP,'(F6.2)') PERCNT
        KMSG(14:19) = C6TEMP
        WRITE(C2TEMP,'(I2)') FRMNBR
        KMSG(45:46) = C2TEMP
        WRITE(C4TEMP,'(I4)') BADSPT
        KMSG(67:70) = C4TEMP
        WRITE(C4TEMP,'(I4)') TOTSPT
        KMSG(72:75) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE IF(MSGCOD.EQ.202) THEN
        KMSG(1:50)='* FATAL ERROR * >      % of AMSU-A spots in fr.   '
        KMSG(51:100)='had bad Earth loc.(    /    )spots w/errors.'
        WRITE(C6TEMP,'(F6.2)') PERCNT
        KMSG(18:23) = C6TEMP
        WRITE(C2TEMP,'(I2)') FRMNBR
        KMSG(48:49) = C2TEMP
        WRITE(C4TEMP,'(I4)') BADSPT
        KMSG(70:73) = C4TEMP
        WRITE(C4TEMP,'(I4)') TOTSPT
        KMSG(75:78) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = ' Invalid Error Message Code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END
