      SUBROUTINE AMABLI(MSSPOT,DELSPT,MSLINE,DELSCN,CNFLIM,ERSTAT)
      IMPLICIT NONE
C**********************************************************************
C 
C   NAME- AMABLI
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 01/10/95    PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 2.0    DATE- 06/29/95    PROGRAMMER- LI LU (SMSRC)
C   VERSION- 2.1    DATE- 09/29/95    PROGRAMMER- LI LU (SMSRC)
C
C     ATOVS-218: REPLACE THE CHECK OF HGOODL AND AGOODL WITH THE
C                CHECK OF SPOT CONFIDENCE OF EACH HIRS SPOT. PASS IN
C                THE SPOT CONFIDENCE MINIMUM LIMIT. REMOVE INCLUDE OF
C                /FRMINF/.
C   VERSION- 2.2    DATE- 02/26/96    PROGRAMMER- DAVE MOREL (SMSRC)
C     ATOVS-300: CHANGED THE DO LOOP 600 RANGE FROM 2 TO HTOTLN-1 TO
C               AVOID ERRORS RETURNED BY CMPWGT TRYING TO INTERPOLATE
C               LINES 1 & 50 OF HIRS.
C   VERSION- 3.0    DATE- 05/08/96    PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.1    DATE- 07/25/97    PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.2    DATE- 05/04/98    PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 3.3    DATE- 08/10/98    PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C   VERSION- 3.4    DATE- 05/26/99    PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C   VERSION- 3.5    DATE- 07/13/99    PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C   VERSION- 3.6    DATE- 04/07/00    PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C   VERSION- 3.7    DATE- 06/13/00    PROGRAMMER- M.P.FERGUSON (RITSS)
C   VERSION- 3.8    DATE- 08/15/00    PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 4.0    DATE- 09/13/00    PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 4.1    DATE- 01/23/04    PROGRAMMER- FRANK TILLEY (RAYTHEON)
C
C   FUNCTION-THIS ROUTINE PERFORMS A BI-LINEAR INTERPOLATION FROM AMSU-A
C            SPOTS TO HIRS SPOT LOCATIONS. ON THE LIMB OF A SCAN
C            (OUTSIDE OF THE LIMB-MOST AMSU-A SPOT) A LINEAR
C            EXTRAPOLATION IS PERFORMED ALONG THE AMSU-A SCAN LINE.
C            WEIGHTS ARE CALCULATED FOR EACH OF THE 4 AMSU-A SPOTS
C            SURROUNDING A HIRS SPOT, AND IN THE CASE OF EXTRAPOLATION
C            WEIGHTS ARE CALCULATED FOR THE TWO AMSU-A SPOTS USED IN THE
C            EXTRAPOLATION. THE WEIGHTS ARE SAVED IN ORDER TO BE USED IN
C            SUBSEQUENT FRAMES AS WELL AS LATER IN PROCESSING WHEN THE
C            INTERPOLATION MUST BE PERFORMED AGAIN. DISTANCES FROM THE
C            AMSU-A SPOTS TO THE HIRS SPOT ARE SAVED IN ORDER TO MORE
C            EASILY RECOMPUTE WEIGHTS IF ONE OF THE FOUR AMSU-A VALUES
C            IS MISSING (IF MORE THAN ONE VALUE IS MISSING THE
C            INTERPOLATION IS NOT PERFORMED).
C
C   VERSION- 3.0 DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE
C               ON THE CRAY.
C   VERSION- 3.1 CONVERT TO NEW 8-BYTE CDB UTILITIES.  REMOVE AMABL$
C                SINCE REALCOEF HANDLES ANY CDB ERRORS.
C   VERSION- 3.2 MOVE LOCAL VARIABLE TLIM TO /I2HINF/.
C   VERSION- 3.3 COMMENTED OUT THE TEST FOR SPOT CONFIDENCE IN THE DO
C                550 LOOP.  THIS ELIMINATED THE MISSING VALUES IN SPOTS
C                2,3,54 AND 55 ON THE HIRS GRID.
C   VERSION- 3.4 ADDED THE AMSU -A SURFACE PRODUCTS TO BE INTERPOLATED/
C                EXTRAPOLATED TO THE HIRS FOV.
C   VERSION- 3.5 ADDED THE REMAINING ELEMENTS OF THE AMADAT BUFFER TO BE
C                INTERPOLATED/EXTRAPOLATED TO THE HIRS FOV.  THIS AMOUNTS
C                TO 28 ELEMENTS.
C   VERSION- 3.6 ADDED A CHECK OF THE BILINEAR INTERPOLATED AMSUA 
C                BRIGHTNESS TEMPERATURES TO SEE IF THEY WERE WITHIN THE
C                TEMPERATURE LIMITS.  USED THE LIMITS DEFINED BY ARRAYS
C                AMINBT AND AMAXBT IN THE COMMON BLOCK IN INPCDB.
C   VERSION- 3.7 MODIFICATIONS MADE TO SET AMSU-A DATA COLLOCATED TO THE
C                HIRS FOV IN A MANNER CONSISTENT WITH ITS TYPE, PARTICULAR-
C                IN THE CASE OF BOOLEAN VARIABLES.  PRECIP EDIT FLAG ADDED
C                TO ELEMENT 162 OF HIRDAT.
C   VERSION- 3.8 FOR WR #100, ADDED COMMON /NOHIRSCF/ TO PICK UP THE
C                NOH1BS FLAG THAT IS SET WHEN NO HIRS 1B* FILE IS
C                AVAILABLE.  ADDED NEW SUBROUTINE AMAHIR TO BE CALLED
C                AFTER APLWGT WHEN NOH1BS IS TRUE TO RE-INTERPOLATE
C                AMSU-A BRIGHTNESS TEMPERATURES.
C   VERSION- 4.0 FOR WR #22, ADDED CODE TO INTERPOLATE/EXTRAPOLATE THE
C                AMSU-B AMBDAT BUFFER (5 ELEMENTS) TO THE HIRS FOV.
C   VERSION- 4.1 REMOVED AMSU-B RELATED CODE AFTER THE DECISION WAS 
C                MADE TO ASSIGN DATA FROM THE AMSU-B SPOT CLOSEST TO
C                A GIVEN HIRS SPOT. THIS WILL BE HANDLED IN A SEPARATE
C                SUBROUTINE.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE  I/O   UNIT   FUNCTIONAL DESCRIPTION
C     ------------------------  ---   ----   ----------------------
C           .../CDB              I   CDBUNT  COEFFICIENT DATABASE
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE        TYPE  I/O  FUNCTION
C     --------        ----  ---  --------
C     MSSPOT          INT    I   DIMENSIONED (INSPARM:HNSPOT,HTOTLN):
C                                THE CLOSEST AMSU-A SPOT TO A GIVEN HIRS
C                                SPOT.
C
C     DELSPT          REAL   I   DIMENSIONED (INSPARM:HNSPOT,HTOTLN):
C                                THE DISPLACEMENT OF THE CLOSEST AMSU-A
C                                FOV TO THE HIRS FOV ALONG A SCAN LINE.
C
C     MSLINE          INT    I   DIMENSIONED (INSPARM:HNSPOT,HTOTLN):
C                                THE CLOSEST AMSU-A LINE TO A GIVEN HIRS
C                                SPOT.
C
C     DELSCN          REAL   I   DIMENSIONED (INSPARM:HNSPOT,HTOTLN):
C                                THE DISPLACEMENT OF THE CLOSEST AMSU-A
C                                FOV TO THE HIRS FOV ALONG A SCAN
C                                POSITION.
C
C     CNFLIM          INT    I   SPOT CONFIDENCE MINIMUM LIMIT.
C
C     ERSTAT          INT    O   ERROR CODE RETURNED TO THE CALLING
C                                ROUTINE.
C
C   COMMON AREA PARAMETERS- INSPARM,INPARM,INSCOM,I2HINF,FRMINF,SPVAL,SPGUNT
C                           ,INPCDB
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE    FUNCTION
C     --------          ----    --------
C
C     AMINBT &           REAL   AMSU-A TEMPERATURE LIMITS FOR EACH CHANNEL.
C     AMAXBT                            
C
C     VALAMA             REAL   DIMENSIONED (INSPARM:ADATEL+1) CONTAINING
C                               INTERPOLATED/EXTRAPOLATED AMSU-A
C                               VALUE AT A GIVEN HIRS FOV LOCATION
C                               FOR EACH CHANNEL.
C
C     INSPOT             INT    HIRS SPOT NUMBER TO BE
C                               INTERPOLATED/EXTRAPOLATED TO.
C
C     INLIN              INT    HIRS LINE NUMBER TO BE
C                               INTERPOLATED/EXTRAPOLATED TO.
C
C     FIRST              LOG    IS TRUE ONLY ON THE FIRST CALL OF
C                               THIS ROUTINE.
C
C     CDBNAM             CH*26  HOLDS THE FULL NAME OF THE
C                               COEFFICIENTS TO BE PULLED FROM
C                               THE CDB.
C
C     STATUS             INT    SUBROUTINE RETURNED ERROR CODE.
C
C     STAT               INT    SUBROUTINE RETURNED ERROR CODE.
C
C     ICH                INT    LOOP INDEX.
C
C   SUBPROGRAMS CALLED- CMPWGT,APLWGT,AMAHIR,REALCOEF
C
C   EXIT STATES- ERSTAT = 99: A FATAL ERROR IN A CALLED ROUTINE.
C
C*********************************************************************
C
      INCLUDE 'INSPARM'
      INCLUDE 'INPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'INPCDB'
      INCLUDE 'I2HINF'
      INCLUDE 'SPGUNT'
      INCLUDE 'SPVAL'
      INCLUDE 'NOHIRSCF'
C
      INTEGER MSSPOT(HNSPOT,HTOTLN),MSLINE(HNSPOT,HTOTLN)
      REAL    DELSPT(HNSPOT,HTOTLN),DELSCN(HNSPOT,HTOTLN)
      INTEGER CNFLIM,ERSTAT
C
      INTEGER INSPOT,INLIN,ICH,STATUS,STAT,IELEM
      REAL    VALAMA(ADATEL+1)
      LOGICAL FIRST
C
      DATA FIRST /.TRUE./
C
      SAVE FIRST
C
      STAT = 0
      ERSTAT = 0
C
C IF THIS IS THE FIRST CALL GET THE TEMPERATURE LIMITS FROM THE CDB
C
      IF (FIRST) THEN
         CALL REALCOEF(CDBUNT,'COLLOC8 ','AMSUA   ','MINTEMP ',
     1                 1,0,0,0,TLIM(1),'GET',STATUS)
         CALL REALCOEF(CDBUNT,'COLLOC8 ','AMSUA   ','MAXTEMP ',
     1                 1,0,0,0,TLIM(2),'GET',STATUS)
         FIRST = .FALSE.
      ENDIF
C
C FOR EACH HIRS SPOT IN AN EXTENDED HIRS BUFFER COMPUTE WEIGHTS BASED
C ON DISTANCE.  IF WEIGHTS WERE PREVIOUSLY COMPUTED, USE THESE WEIGHTS
C TO DETERMINE INTERPOLATED OR EXTRAPOLATED VALUES.  FIRST CHECK LINE
C QUALITY.
C
C COMMENTED OUT THE TEST FOR SPOT CONFIDENCE TO ELIMINATE THE CONSTANT
C MISSING VALUES IN HIRS SPOT LOCATIONS 2, 3, 54, AND 55.
C A. ALLEGRINO 8/10/98
C
      DO 600 INLIN = 2,HTOTLN-1
         DO 550 INSPOT = 1,HNSPOT
            DO IELEM = 1, ADATEL+1
               VALAMA(IELEM) = R4SPV
            END DO
            
C           IF (HIRANC(INSPOT,INLIN,6).LT.CNFLIM) GO TO 550
            IF (.NOT. WGTCAL(INSPOT,INLIN)) THEN
               CALL CMPWGT(INSPOT,INLIN,HIRANC(INSPOT,INLIN,1),
     1              HIRANC(INSPOT,INLIN,2),MSSPOT(INSPOT,INLIN),
     2              DELSPT(INSPOT,INLIN),MSLINE(INSPOT,INLIN),
     3              DELSCN(INSPOT,INLIN),ANSPOT,ATOTLN,ANANC,
     4              AMAANC,CNFLIM,STATUS)
               IF (STATUS .EQ. 99) GOTO 900
            END IF
C
C APPLY WEIGHTS GENERATED IN CMPWGT TO APPROPRIATE AMSU-A SPOTS TO
C CALCULATE AN INTERPOLATED (OR EXTRAPOLATED) VALUE AT THE HIRS LOCATION
C
            IF (WGTCAL(INSPOT,INLIN)) THEN
                CALL APLWGT(INSPOT,INLIN,2,HIRANC(INSPOT,INLIN,1),
     1               HIRANC(INSPOT,INLIN,2),ANSPOT,ATOTLN,(ADATEL+1),
     2               ANANC,ANCHAN,AMADAT,AMAANC,VALAMA,
     3               HIRANC(INSPOT,INLIN,6))
            END IF
C
C IF NO HIRS 1B* IS AVAILABLE, RE-INTERPOLATE THE AMSU-A BRIGHTNESS
C TEMPERATURES IN THE VALAMA BUFFER.
C
            IF (NOH1BS) THEN
               CALL AMAHIR(INSPOT,INLIN,AMADAT,VALAMA)
            END IF
C
C STORE INTERPOLATED/EXTRAPOLATED AMSU-A CHANNEL VALUES IN THE
C HIRDAT BUFFER.
C
            DO ICH = 1,ANCHAN
               IF (WGTCAL(INSPOT,INLIN) .AND. VALAMA(ICH).GE.AMINBT(ICH)
     >             .AND. VALAMA(ICH).LE.AMAXBT(ICH)) THEN
                   HIRDAT(INSPOT,INLIN,ICH+HNCHAN) = VALAMA(ICH)
               ELSE
                  HIRDAT(INSPOT,INLIN,ICH+HNCHAN) = R4SPV
               END IF
            END DO  
C
C STORE THE AMSU-A SURFACE PRODUCTS AND THE CLOUD CLEARED AMSU-A
C CHANNELS 4 AND 5 IN THE HIRDAT BUFFER.
C
            DO IELEM=149,161
               IF(IELEM.NE.152.AND.IELEM.NE.153)THEN
                  IF (WGTCAL(INSPOT,INLIN)) THEN
                     HIRDAT(INSPOT,INLIN,IELEM) = VALAMA(IELEM - 133)
                     IF(IELEM.EQ.154 .OR. IELEM.EQ.155) THEN
                        IF(VALAMA(IELEM-133).LT.AMINBT(IELEM-150)
     >                     .OR.VALAMA(IELEM-133).GT.
     >                      AMAXBT(IELEM-150)) THEN
                               HIRDAT(INSPOT,INLIN,IELEM) = R4SPV
                        ENDIF
                     ENDIF
                  ELSE
                     HIRDAT(INSPOT,INLIN,IELEM) = R4SPV
                  END IF
                ENDIF
            END DO     
            HIRDAT(INSPOT,INLIN,152) = VALAMA(19)
            HIRDAT(INSPOT,INLIN,153) = VALAMA(20)
            HIRDAT(INSPOT,INLIN,158) = VALAMA(28)
            HIRDAT(INSPOT,INLIN,162) = VALAMA(ADATEL+1)
C
C STORE THE INTERPOLATED/EXTRAPOLATED AMSU-B CHANNEL VALUES IN THE
C HIRDAT BUFFER.
C
ccft        IF (WGTCAL(INSPOT,INLIN)) THEN
c               CALL APLWGT(INSPOT,INLIN,3,HIRANC(INSPOT,INLIN,1),
c    1               HIRANC(INSPOT,INLIN,2),BNSPOT,BTOTLN,BDATEL,
c    2               BNANC,BNCHAN,AMBDAT,AMBANC,VALAMB,
c    3               HIRANC(INSPOT,INLIN,6))
c           END IF
c           DO ICH = 1,BNCHAN
c              IF (WGTCAL(INSPOT,INLIN)) THEN
c                 HIRDAT(INSPOT,INLIN,ICH+162) = VALAMB(ICH)
c              ELSE
c                 HIRDAT(INSPOT,INLIN,ICH+162) = R4SPV
c              END IF
c           END DO 
C
  550    CONTINUE
C
  600 CONTINUE
      RETURN
C
  900 ERSTAT = 99
      RETURN
      END
