       SUBROUTINE BTCHK1(BRTTMP,SPOT,LINE,ELE,LWRLIM,UPRLIM,FACTOR,
     1                   ELEMNT,ANCIL,CHAN,STAT)
C***********************************************************************
C
C  NAME- BTCHK1
C
C  LANGUAGE- FORTRAN 77       TYPE- SUBROUTINE   COMPUTER- CRAY    
C
C  VERSION- 1.0        DATE- 8/10/92    PROGRAMMER- DAVE MOREL (SMSRC)
C  VERSION- 2.0        DATE- 8/23/94    PROGRAMMER- SURYA JANDHYALA (SMS
C   XATOV- 40   REPLACED THE COMMENTS FOR SSU AND MSU INSTRUMENTS WITH T
C               COMMENTS FOR THE AMSU-A AND AMSU-B INSTRUMENTS.
C  VERSION- 3.0    DATE- 05/21/96   PROGRAMMER- ALEX POZDNYAK, FDC
C     FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C     INTEGER, ALL R*4 TO REAL, AND ALL LOG*4 DECLARATIONS TO
C     LOGICAL.
C  VERSION- 3.1    DATE- 05/20/98   PROGRAMMER- LEAH WILSON (RSTX)
C     ADD /GROSSCHK/ TO SPECIFY WHICH CHANNELS ARE AVAILABLE.
C  VERSION- 3.2    DATE- 06/25/98   PROGRAMMER- LEAH WILSON (RSTX)
C     ADD COUNTERS FOR CHANNEL FAILURES FOR LATER OUTPUT TO KIDS.
C     ADD /INSPARM/, /FRMINF/ AND /MONSTA/ FOR THIS PURPOSE.
C   VERSION- 1.3    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function 
C
C  FUNCTION-  THIS SUBROUTINE PERFORMS A GROSS LIMITS CHECK ON
C             BRIGHTNESS TEMPERATURES FOR ALL THE SPOTS IN A DATA
C             BUFFER.  IF A BRIGHTNESS TEMPERATURE FOR ANY SPOT FAILS
C             THE LIMITS CHECK, THE SPOT CONFIDENCE FOR THAT SPOT IS
C             DECREASED BY THE AMOUNT GIVEN IN FACTOR, AND THE CHANNEL
C             AVAILABILITY BIT FOR THE GIVEN CHANNEL IS SET TO 0.
C
C  CALLING SEQUENCE PARAMETERS-
C    VARIABLE   TYPE  I/O   DESCRIPTION
C    --------   ----  ---   -----------
C    BRTTMP     REAL   I    BRIGHTNESS TEMPERATURE VECTOR,
C                           DIMENSIONED (SPOT,LINE,ELE).
C
C    SPOT       INT    I    NUMBER OF SPOTS IN A LINE.
C
C    LINE       INT    I    NUMBER OF LINES IN THE BUFFER BRTTMP.
C
C    ELE        INT    I    NUMBER OF ELEMENTS PER SPOT.
C
C    LWRLIM     REAL   I    LOWER LIMIT ON BRIGHTNESS TEMPERATURE FOR
C                           THE INSTRUMENT CHANNEL, DIMENSIONED (CHAN).
C
C    UPRLIM     REAL   I    UPPER LIMIT ON BRIGHTNESS TEMPERATURE FOR
C                           THE INSTRUMENT CHANNEL, DIMENSIONED (CHAN).
C
C    FACTOR     REAL   I    AMOUNT BY WHICH TO LOWER SPOT CONFIDENCE
C                           IF BRIGHTNESS TEMPERATURE LIMITS FOR
C                           CHANNEL ARE NOT MET.
C
C    ELEMNT     INT    I    NUMBER OF ELEMENTS IN ANCILLARY BUFFER.
C
C    ANCIL      REAL  I/O   ANCILLARY BUFFER, DIMENSIONED (SPOT,
C                           LINE, ELEMNT).
C
C    CHAN       INT     I   NUMBER OF CHANNELS TO LOOP THROUGH.
C
C    STAT       INT     O   ERROR STATUS CODE.
C
C
C  LOCAL VARIABLES-
C    VARIABLE     TYPE    DESCRIPTION
C    --------     ----    -----------
C    BIT          INT     BIT TO SET IN CHANNEL AVAILABILITY.
C    I,J,K        INT     DO LOOP COUNTERS.
C    ICHAN        INT     USED TO CLEAR A BIT FLAG, EQUIVALENT TO
C                         RCHAN.
C    BADTMP       REAL    THE FIRST BAD TEMPERATURE FOUND.
C    BADCHN       INT     THE CHANNEL NUMBER OF THE BAD TEMPERATURE.
C    RCHAN        REAL    USED TO CLEAR A BIT FLAG, EQUIVALENT TO
C                         ICHAN.
C    CONF         INT     ANCILLARY ELEMENT TO USE FOR SPOT CONFIDENCE.
C    CNAV         INT     ANCILLARY ELEMENT TO USE FOR CHANNEL AVAIL.
C    WRTCNT       INT     COUNTER FOR NUMBER OF TIMES BAD TEMPERATURES
C                         WERE DETECTED.
C    CH2CHK       INT     STORES WHICH CHANNELS ARE TO BE CHECKED (1=
C                         CHECK, 0=DO NOT CHECK)
C
C  SUBPROGRAMS CALLED - BTCHK$
C
C  FUNCTIONS USED - IBCLR
C
C  ASSUMPTIONS - THE FIRST CALL TO THE ROUTINE MUST BE FOR THE HIRS.
C
C***********************************************************************
C
      INTEGER SPOT, LINE, ELE, ELEMNT, CHAN, STAT, WRTCNT, CH2CHK(20)
      REAL    BRTTMP(SPOT,LINE,ELE), ANCIL(SPOT,LINE,ELEMNT)
      REAL    UPRLIM(CHAN), FACTOR, LWRLIM(CHAN)
C
      INTEGER BIT, I, J, K, ICHAN, CONF, CNAV, BADCHN
      REAL    RCHAN, BADTMP
      LOGICAL HIRS,AMSUA
      LOGICAL ISR4SPV
C
      SAVE WRTCNT
C
      INCLUDE 'GROSSCHK'
      INCLUDE 'INSPARM'
      INCLUDE 'FRMINF'
      INCLUDE 'MONSTA'
      INCLUDE 'SPVAL'
C
      EQUIVALENCE (ICHAN,RCHAN)
      HIRS=.FALSE.
      AMSUA=.FALSE.
C
C  THE HIRS INSTRUMENT ANCILLARY BUFFER HAS THE SPOT CONFIDENCE AND
C  CHANNEL AVAILABILITY DATA IN DIFFERENT ELEMENTS THAN AMSU-A AND
C  AMSU-B ANCILLARY BUFFERS.  IF ARGUMENT CHAN IS 20 THEN SET UP THE
C  HIRS ELEMENT NUMBERS; ELSE USE THE AMSU-A AND AMSU-B ELEMENT NUMBERS.
C
      WRTCNT=0
      IF(CHAN .EQ. 20) THEN
C
C INITIALIZE COUNTER FOR NUMBER OF WRITE STATEMENTS PER FRAME.
C
         CONF = 6
         CNAV = 7
         DO I=1,CHAN
            CH2CHK(I) = HCH2CHK(I)
         ENDDO
         HIRS=.TRUE.
      ELSE
         CONF = 5
         CNAV = 6
         DO I=1,CHAN
            CH2CHK(I) = ACH2CHK(I)
         ENDDO
         AMSUA=.TRUE.
      ENDIF
      DO 30 J = 1, LINE
         DO 20 I = 1, SPOT
            DO 10 K = 1, CHAN
C
               IF (CH2CHK(K).NE.1) GO TO 10
               IF (ISR4SPV(BRTTMP(I,J,K))) GOTO 10
C
               IF ((BRTTMP(I,J,K).GT.UPRLIM(K)) .OR.
     1             (BRTTMP(I,J,K).LT.LWRLIM(K))) THEN
C
C  BRIGHTNESS TEMP FALLS OUTSIDE OF LIMITS - ADJUST SPOT CONFIDENCE.
C
                  ANCIL(I,J,CONF) = ANCIL(I,J,CONF) - FACTOR
                  IF(HIRS) NBBHIR(FRAMNO,K) = NBBHIR(FRAMNO,K)+1
                  IF(AMSUA) NBBAMA(FRAMNO,K) = NBBAMA(FRAMNO,K)+1
C
C  FORCE VALUE TO ZERO IF LESS THAN ZERO.
C
                  IF (ANCIL(I,J,CONF).LT.0.0) ANCIL(I,J,CONF) = 0.0
C
C  SET BIT FOR CHANNEL IN CHANNEL AVAILABILITY TO ZERO.
C
                  BIT = 32 - K
                  RCHAN = ANCIL(I,J,CNAV)
                  ICHAN = IBCLR(ICHAN, BIT)
                  ANCIL(I,J,CNAV) = RCHAN
C
                  WRTCNT = WRTCNT + 1
                  IF(WRTCNT .EQ. 1) BADTMP = BRTTMP(I,J,K)
                  IF(WRTCNT .EQ. 1) BADCHN = K
C
C WHEN 1-5 TEMPERATURES EXCEED THE LIMIT, WRITE TO THE KIDS.
C
                  IF(WRTCNT .LE. 5) 
     1               CALL BTCHK$(110,BADTMP,WRTCNT,CHAN,BADCHN,STAT)
               ENDIF
C
   10       CONTINUE
   20    CONTINUE
   30 CONTINUE
C
C IF MORE THAN 5 TEMPERATURES EXCEED THE LIMIT,
C THEN WRITE HOW MANY EXCEEDED THE LIMIT THIS FRAME.
C
      IF(WRTCNT .GT. 5) CALL BTCHK$(111,BADTMP,WRTCNT,CHAN,BADCHN,STAT)
C
      RETURN
      END
C***********************************************************************
      SUBROUTINE BTCHK$ ( MSGCOD, BT, COUNT, LINE, CHAN, STAT )
C
C***********************************************************************
C
C   NAME- BTCHK$
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 08/12/92   PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 2.0    DATE- 08/03/94   PROGRAMMER- JEFF MANNING(SMSRC)
C            ATOVS-15  CHANGED THE DESCRIPTION OF THE CALLING ARGUMENT
C                      LINE.
C  VERSION- 2.1        DATE- 8/23/94    PROGRAMMER- SURYA JANDHYALA (SMS
C   XATOV- 40   INCREASED THE LENGTH OF THE VARIABLE BUFF TO CHARACTER
C               LENGTH OF 13. REPLACED THE BUFFER NAMES FOR SSU AND MSU
C               INSTRUMENTS WITH AMSU-A AND AMSU-B INSTRUMENTS.
C   VERSION- 3.0    DATE- 10/07/97    PROGRAMMER- LEAH WILSON (HSTX)
C               WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
C               INCLUDE UNIT NUMBERS IN ERROR MESSAGES WHERE APPLICABLE.
C
C   FUNCTION- ERROR HANDLING ROUTINE FOR BTCHK1.
C
C   FILES-    
C        DSN                     UNIT  I/O   FUNCTION
C        ---                     ----  ---   --------
C     ...KIDS                    KUNIT  O    KRAY INTERACTIVE DOCUMENTATION
C                                            FILE.
C
C
C   CALLING SEQUENCE PARAMETERS-
C
C    VARIABLE  TYPE   I/O  DESCRIPTION
C    --------  ----   ---  -----------
C    MSCGOD    INT     I   RESULT CODE TO BE PROCESSED.
C    BT        REAL    I   BRIGHTNESS TEMPERATURE WHICH EXCEEDED LIMITS.
C    COUNT     INT     I   NUMBER OF TEMPERATURES WHICH EXCEEDED LIMITS.
C    LINE      INT     I   NUMBER OF CHANNELS TO IDENTIFY THE
C                          APPROPRIATE INSTRUMENT BUFFER.
C    CHAN      INT     I   CHANNEL WHERE PROBLEM OCCURRED.
C    STAT      INT    I/O  ERROR STATUS ENCOUNTERED.
C
      INTEGER    MSGCOD, COUNT, LINE, CHAN, STAT
      REAL       BT
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C
C    VARIABLE  TYPE   DESCRIPTION
C    --------  ----   -----------
C    NAME     CH*6    NAME OF THIS SUBROUTINE.
C    I         INT    LOOP COUNTER.
C    BUFF     CH*6    INSTRUMENT BUFFER NAME
C    KMSG    CH*100   KIDS MESSAGE BUFFER
C    C4TEMP   CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C    C5TEMP   CH*5    HOLDING BUFFER FOR WRITING TO KIDS
C
C   SUBROUTINES CALLED- KIDS
C
C   EXIT STATES-
C       STAT =  0; NO OR NON-FATAL ERROR
C       STAT = 99; INDICATING A FATAL ERROR WAS DETECTED
C
C***********************************************************************
      CHARACTER*4   C4TEMP
      CHARACTER*5   C5TEMP
      CHARACTER*100 KMSG
      CHARACTER*6  BUFF
      INTEGER      I
C
      STAT = 0
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
      BUFF = 'MHS   '
      IF(LINE .EQ. 20) BUFF = 'HIRS  '
      IF(LINE .EQ. 15) BUFF = 'AMSU-A'
C
C     PROCESS THE ERROR CODE
C
      IF ( MSGCOD .EQ. 110) THEN
C
        KMSG=' * WARNING * Limits check exceeded with temp of      '
        KMSG(54:100)='-frame      of        ch.     .'
        WRITE(C5TEMP,'(F5.1)') BT
        KMSG(49:53) = C5TEMP
        WRITE(C4TEMP,'(I4)') FRMNBR
        KMSG(61:64) = C4TEMP
        KMSG(69:74) = BUFF
        WRITE(C4TEMP,'(I4)') CHAN  
        KMSG(80:83) = C4TEMP
        print *,KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSEIF ( MSGCOD .EQ. 111) THEN
        KMSG=' * WARNING *       btemps exceed limit in frame      '
        KMSG(54:100)='of the        buffer.'
        WRITE(C5TEMP,'(I5)') COUNT
        KMSG(14:18) = C5TEMP
        WRITE(C4TEMP,'(I4)') FRMNBR
        KMSG(49:52) = C4TEMP
        KMSG(61:66) = BUFF
        print *,KMSG
        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
