      SUBROUTINE LIMCOR2(INSTBF,ANCIL,NUMSPT,NUMLIN,NUMDAT,ANCNUM,
     >CHANNO,ASOCHN)
C**********************************************************************
C
C   NAME- LIMCOR
C
C   LANGUAGE-VS FORTRAN     TYPE-SUBROUTINE     COMPUTER - CEMSCS
C
C   VERSION- 2.0      DATE- 10/13/94     PROGRAMMER-TERESA SIMMS (SMSRC)
C   VERSION- 3.0      DATE- 05/29/96     PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.1      DATE- 01/26/98     PROGRAMMER- LEAH WILSON (RAYTHEON)
C   VERSION- 3.2      DATE- 10/06/98     PROGRAMMER- A. ALLEGRINO (RITSS)
C   VERSION- 3.3      DATE- 05/09/01     PROGRAMMER- A. ALLEGRINO (RITSS)
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C
C        SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C
C   FUNCTION- THIS ROUTINE PERFORMS GLOBAL LIMB CORRECTION ON THE VALUES
C   IN AN INSTRUMENT'S DATA BUFFER AND REPLACES VALUES IN THE BUFFER.
C   
C   VERSION- 3.1  DO NOT ALLOW MORE THAN 1 INTERCEPT AND 3 PREDICTORS.
C   VERSION- 3.2  CHANGED PROGRAM TO USE LIMB COEFFICIENTS THAT WERE  
C                 STRATIFIED ACCORDING TO LAND AND SEA.
C   VERSION- 3.3  ADDED A CHECK OF THE BAD HIRS FLAG, BADHIR, TO NOT
C                 PROCESS IF THE FLAG IS SET TO 1 FOR ANY OF THE PREDICTOR
C                 CHANNELS. ALSO ADDED COMMON AREA HIRSTAT.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function        
C
C   CALLING SEQUENCE PARAMETERS-
C
C   NAME    TYPE  I/O    DESCRIPTION
C   ----    ----  ---    ------------
C   INSTBF   REAL I/O    INSTRUMENT EXTENDED DATA BUFFER, DIMENSIONED
C                        (NUMSPT, NUMLIN, NUMDAT).
C   ANCIL    REAL  I     INSTRUMENT ANCILLARY DATA BUFFER, DIMENSIONED
C                        (NUMSPT, NUMLIN, ANCNUM).
C   NUMSPT   INT   I     NUMBER OF SPOTS IN A INSTRUMENT SCAN LINE.
C   NUMLIN   INT   I     NUMBER OF INSTRUMENT SCAN LINES IN THE BUFFERS.
C   NUMDAT   INT   I     NUMBER OF DATA ELEMENTS IN THE INSTRUMENT DATA
C                        BUFFER.
C   ANCNUM   INT   I     NUMBER OF DATA ELEMENTS IN THE ANCILLARY BUFFER
C   CHANNO   INT   I     THE NUMBER OF INSTRUMENT CHANNELS IN THE
C                        INSTRUMENT DATA BUFFER TO BE LIMB CORRECTED.
C   ASOCHN   INT   I     AN ARRAY WHICH CONTAINS THE ASSOCIATED
C                        CHANNEL INFORMATION FOR EACH OF THE INSTRUMENT
C                        CHANNELS.  (DIMENSIONED CHANNO X CHANNO.)
C
C   COMMON AREA PARAMETERS - SPVAL,LCCOEF,HIRSTAT
C
C   LOCAL VARIABLES-
C
C   NAME            TYPE  DESCRIPTION
C   ----            ----  ------------
C   ACCUM           REAL  ACCUMULATED PRODUCTS OF COEFFICIENTS
C                         AND ASSOCIATED CHANNEL TEMPERATURES.
C   COUNT            INT  COUNTER FOR ASSOCIATED CHANNEL COEFFICIENT.
C   I,J,K,L,KK       INT  DO LOOP INDEXES.
C   TMPVEC(20)      REAL  TEMPORARY VECTOR FOR LIMB CORRECTED DATA.
C
C
C   SUBPROGRAMS CALLED - NONE
C
C   EXIT STATES - NONE
C
C   COMMENTS - IT IS ASSUMED THAT NO INSTRUMENT HAS MORE THAN 20
C              CHANNELS.
C
C**********************************************************************
C
      INTEGER I,J,K,L,KK, NUMSPT, NUMLIN, NUMDAT, CHANNO,
     +          ASOCHN(CHANNO,CHANNO), COUNT,ANCNUM,TEMPER(56)
      REAL    INSTBF(NUMSPT,NUMLIN,NUMDAT), TMPVEC(20),
     +          ACCUM, COEFFS(CHANNO,4,NUMSPT),
     +          ANCIL(NUMSPT,NUMLIN,ANCNUM)
      LOGICAL   ISR4SPV
C
      INCLUDE 'SPVAL'
      INCLUDE 'LCCOEF'
      INCLUDE 'HIRSTAT'


C
C  LOOP OVER THE INSTRUMENT SCAN LINES IN AN EXTENDED DATA BUFFER.
      DO 50 J = 1, NUMLIN
C
C  LOOP OVER THE BEAM POSITIONS IN A INSTRUMENT SCAN LINE.
        DO 40 I= 1, NUMSPT
C
C  LOOP OVER THE INSTRUMENT CHANNELS.  THESE ARE THE CHANNELS THAT ARE
C  BEING LIMB CORRECTED.
C
            DO 20 K = 1, CHANNO
               ACCUM=0.
               COUNT=2
C
C  LOOP OVER THE ASSOCIATED CHANNELS
C
               DO 10 L = 1, CHANNO
                 IF(COUNT.LE.4) THEN
                    IF (ASOCHN(L,K) .EQ. 1) THEN
                       IF (ISR4SPV(INSTBF(I,J,L))) THEN
                          TMPVEC(K)=R4SPV
                          GOTO 20
                       ELSE
C
C    ACCUMULATE THE PRODUCTS OF THE LIMB CORRECTION COEFFICIENTS
C    AND THEIR CORRESPONDING CHANNEL RADIANCE TEMPERATURES.
C    CHANGED TO ACCOUNT FOR ALL OF THE LIMB CORRECTION COEFFICIENTS
C    PASSED IN THRU COMMON. INSTRUMENT BEING PROCESSED IS DETERMINED
C    BY THE NUMBER OF SPOTS ( BEAM POSITIONS)
C
C
C                 - AMSU-A
C
                        IF(NUMSPT .EQ. 30) THEN
                          IF(ANCIL(I,J,8) .EQ.0) THEN
                            ACCUM=ACCUM+SEALAMA(K,COUNT,I)*INSTBF(I,J,L)
                            COUNT=COUNT + 1
                          ELSE
                            ACCUM=ACCUM+LNDLAMA(K,COUNT,I)*INSTBF(I,J,L)
                            COUNT=COUNT + 1
                          ENDIF
                        ENDIF
C
C                 - HIRS
C
                        IF(NUMSPT .EQ. 56) THEN
                          IF(BADHIR(I,J,L) .EQ.1) THEN
                           TMPVEC(K) =R4SPV
                           GO TO 20
                          ENDIF
                          ACCUM=ACCUM+LCCHIR(K,COUNT,I)*INSTBF(I,J,L)
                          COUNT=COUNT + 1
                        ENDIF
C
C                 - MHS
C
                        IF(NUMSPT .EQ. 90) THEN
                          ACCUM=ACCUM+LCCAMB(K,COUNT,I)*INSTBF(I,J,L)
                          COUNT=COUNT + 1
                        ENDIF
                       ENDIF
                    ENDIF
                 ENDIF
   10          CONTINUE
C
C   TEMPORARILY SAVE THE LIMB CORRECTED TEMPERATURE IN A VECTOR.
C   (REPLACEMENT IS NOT DONE HERE SO THAT UNCORRECTED VALUES ARE
C    AVAILABLE TO LIMB CORRECT OTHER CHANNELS).
C
C
C         - AMSU-A
C
               IF(NUMSPT .EQ. 30) THEN
                IF(ANCIL(I,J,8) .EQ.0) THEN
                 TMPVEC(K) = SEALAMA(K,1,I) + ACCUM
                ELSE
                 TMPVEC(K) = LNDLAMA(K,1,I) + ACCUM
                ENDIF
               ENDIF
C
C         - HIRS
C
               IF(NUMSPT .EQ. 56) THEN
                 TMPVEC(K) = LCCHIR(K,1,I) + ACCUM
               ENDIF
C
C         - MHS
               IF(NUMSPT .EQ. 90) THEN
                 TMPVEC(K) = LCCAMB(K,1,I) + ACCUM
               END IF
C
   20       CONTINUE
C
C   REPLACE THE VALUES IN THE INSTRUMENT DATA BUFFER WITH THE LIMB
C   CORRECTED VALUES IN THE VECTOR AND REINITIALIZE THE TEMPORARY
C   VECTOR.
            DO 30 KK=1, CHANNO
                INSTBF(I,J,KK)=TMPVEC(KK)
                TMPVEC(KK)=R4SPV
   30       CONTINUE
C
   40   CONTINUE
   50 CONTINUE
C
      RETURN
      END
