      SUBROUTINE APLWGT(INSPOT,INLIN,INSTR,HIRLAT,HIRLON,NMDSPT,NMDLIN,
     1           NMDELM,NMAELM,NMCHAN,DATBUF,ANCBUF,VALNEW,SPTCNF)
      IMPLICIT NONE
C**********************************************************************
C
C   NAME- APLWGT
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 01/19/95    PROGRAMMER- TIM GARDNER, HSTX
C   VERSION- 2.0    DATE- 06/19/95    PROGRAMMER- LI LU (SMSRC)
C   VERSION- 3.0    DATE- 04/28/96    PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.1    DATE- 11/19/97    PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.2    DATE- 05/04/98    PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 3.3    DATE- 05/26/99    PROGRAMMER- A.ALLEGRINO (RAYTHEON)
C   VERSION- 3.4    DATE- 07/14/99    PROGRAMMER- A.ALLEGRINO (RAYTHEON)
C   VERSION- 3.5    DATE- 07/30/99    PROGRAMMER- M.FERGUSON  (RITSS)
C   VERSION- 3.6    DATE- 06/13/00    PROGRAMMER- M.P.FERGUSON (RITSS)
C   VERSION- 3.7    DATE- 03/20/02    PROGRAMMER- A. ALLEGRINO (RITSS)
C   VERSION- 3.8    DATE- 02/27/03    PROGRAMMER- Rickey Rollins (CSC)
C
C   FUNCTION-THIS ROUTINE USES WEIGHTS GENERATED PREVIOUSLY TO COMPUTE
C   AN INTERPOLATED OR EXTRAPOLATED VALUE AT A HIRS SPOT FOR EACH
C   CHANNEL.  IF THERE IS AT LEAST ONE BAD SPOT THE WEIGHTS WILL
C   BE RECOMPUTED PROVIDED THERE ARE THREE GOOD SPOTS. IN THE CASE OF AN
C   EXTRAPOLATION, BOTH SPOTS USED IN THE EXTRAPOLATION MUST BE GOOD OR
C   THE INSTRUMENT VALUES AT THE HIRS LOCATION ARE SET TO MISSING.
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE
C                ON THE CRAY.
C   VERSION 3.1- RTOVS UPGRADE TO REMOVE LOWERING HIRS SPOT CONFIDENCE
C                TO ZERO IF THE AMSU-A SPOT IS PRECIPITATING.
C   VERSION 3.2- REMOVE DECLARATIONS FOR TLIM SINCE IT IS NOW IN
C                /I2HINF/.
C   VERSION 3.3- CHANGED PROGRAM TO INCLUDE INTERPOLATION/EXTRAPOLATION
C                OF THE AMSUA SURFACE PRODUCTS. INCLUDED A CHECK TO SEE
C                IF PROCESSING AN AMSU-A TEMPERATURE OR SURFACE PRODUCT.
C                IF TEMPERATURE, THEN USE THE GROSS TEMPERATURE LIMITS
C                TO DEFINE MISSING VALUES. IF PRODUCT, USE THE MISSING
C                VALUE.
C                ALSO CHANGED THE DIMENSION OF VALNEW FROM NMCHAN TO
C                NMDELM.
C   VERSION 3.4- CHANGED PROGRAM TO INCLUDE INTERPOLATION/EXTRAPOLATION
C                OF ALL THE ELEMENTS OF THE AMADAT (DATBUF) BUFFER.
C   VERSION 3.5  USE NEW FORM OF PRECIP SCREENING, INDEX 13 IN HIRS
C                ANCILLIARY BUFFER.
C   VERSION 3.6- MADE MODIFICATIONS TO AVOID DOING INTERPOLATIONS OR
C                EXTRAPOLATIONS FOR BOOLEAN VARIABLES BEING COLOCATED
C                TO THE HIRS FOV.
C   VERSION 3.7- MADE MODIFICATIONS TO COMPUTE THE DISTANCE RATIOS 
C                (DISRAT) AND TOTAL DISTANCE RATIO (TOTRAT) WHETHER OR
C                NOT CHECKD IS SET TO TRUE. THIS SOLVES THE PROBLEM
C                OF SPURIOUS VALUES WITHIN THE INTERPOLATION OF THE 
C                AMSUA SURFACE PRODUCTS. (WR#158/191)
C   VERSION 3.8- Replaced R4SPV equivalence test with ISR4SPV function
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE        TYPE  I/O  FUNCTION
C     --------        ----  ---  --------
C     INSPOT          INT    I   HIRS SPOT NUMBER OF FOV TO BE
C                                INTERPOLATED/EXTRAPOLATED TO.
C
C     INLIN           INT    I   HIRS LINE NUMBER OF FOV TO BE
C                                INTERPOLATED/EXTRAPOLATED TO.
C
C     INSTR           INT    I   INSTRUMENT NUMBER FROM WHICH THE
C                                DATA OBTAINED IS TO BE INTERPOLATED/
C                                EXTRAPOLATED.
C
C     HIRLAT          REAL   I   LATITUDE OF THE HIRS SPOT.
C
C     HIRLON          REAL   I   LONGITUDE OF THE HIRS SPOT.
C
C     NMDSPT          INT    I   NUMBER OF SPOTS PER LINE IN DATBUF.
C
C     NMDLIN          INT    I   NUMBER OF LINES PER FRAME IN DATBUF.
C
C     NMDELM          INT    I   NUMBER OF DATA ELEMENTS IN DATBUF.
C
C     NMAELM          INT    I   NUMBER OF ELEMENTS IN ANCBUF.
C
C     NMCHAN          INT    I   NUMBER OF CHANNELS IN DATBUF FOR WHICH
C                                DATA IS TO BE INTERPOLATED/EXTRAPOLATED
C
C     DATBUF          REAL   I   DIMENSIONED (NMDSPT,NMDLIN,NMDELM):
C                                ARRAY HOLDING DATA TO BE
C                                INTERPOLATED/EXTRAPOLATED TO HIRS FOV.
C
C     ANCBUF          REAL   I   DIMENSIONED (NMDSPT,NMDLIN,NMAELM):
C                                ARRAY CONTAINING ANCILLARY INFORMATION
C                                FOR INTERPOLATED/EXTRAPOLATED DATA.
C
C     VALNEW          REAL   O   DIMENSIONED (NMDELM): ARRAY CONTAINING
C                                INTERPOLATED/EXTRAPOLATED DATA AT A
C                                HIRS FOV FOR EACH CHANNEL.
C
C     SPTCNF          REAL  I/O  SPOT CONFIDENCE VALUE FOR THE
C                                HIRS SPOT.
C
C
C   COMMON AREA PARAMETERS- SPVAL,INSPARM,I2HINF
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     ISPOT                    INT    DIMENSIONED (4): SPOT NUMBER OF
C                                     EACH SPOT IN DATBUF SURROUNDING
C                                     A GIVEN HIRS FOV.
C
C     ILINE                    INT    DIMENSIONED (4): LINE NUMBER OF
C                                     EACH SPOT IN DATBUF SURROUNDING
C                                     A GIVEN HIRS FOV.
C
C     I,ICHN                   INT    DO LOOP COUNTERS.
C
C     TMPVAL                   REAL   DIMENSIONED (4) ARRAY HOLDING DATA
C				      TO BE INTERPOLATED/EXTRAPOLATED
C				      TO A HIRS FOV.
C
C     CHECKD                   LOG    VARIABLE IS TRUE IF ONLY THREE
C                                     FOVS OF DATA TO BE INTERPOLATED
C                                     ARE PRESENT AND INTERPOLATION
C                                     TEST HAS BEEN COMPLETED.
C
C     DISRAT                   REAL   DIMENSIONED 4: RATIO BETWEEN
C                                     DISTANCE FROM ONE INTERPOLATION
C                                     POINT AND TOTAL OF DISTANCES TO
C                                     THE HIRS FOV.
C
C     TOTRAT                   REAL   TOTAL OF DISRAT FOR THE POINTS
C                                     USED IN THE INTERPOLATION.
C
C     NBDPTS                   INT    NUMBER OF UNACCEPTABLE INTERP.
C                                     POINTS.
C
C     IXCLPT                   INT    INTERP. POINT TO BE EXCLUDED.
C
C     NUMPCP                   INT    NUMBER OF POINTS TO BE USED IN
C                                     THE INTERPOLATION WHICH ARE
C                                     PRECIP CONTAMINATED (INSTRUMENT ON
C
C     IPCP                     INT    LAST POINT CHECKED WHICH WAS
C                                     PRECIP CONTAMINATED (AMSU-A ONLY)
C
C     INSIDE                   LOG    SET TO TRUE IF A POINT TO BE
C                                     INTERPOLATED TO IS INSIDE OF
C                                     THREE INTERPOLATION DATA POINTS.
C
C     WGT                      REAL   DIMENSIONED (4) CONTAINING WEIGHTI
C				      COEFFICIENTS FOR THE 4 INSTRUMENT SPOTS.
C
C
C   SUBPROGRAMS CALLED- CHKARA
C
C   EXIT STATES-  NONE
C
C*********************************************************************
C
      INTEGER INSPOT,INLIN,INSTR,NMDSPT,NMDLIN,NMDELM,NMAELM,NMCHAN
      REAL  SPTCNF,HIRLAT,HIRLON,VALNEW(NMDELM+1)
      REAL  ANCBUF(NMDSPT,NMDLIN,NMAELM),DATBUF(NMDSPT,NMDLIN,NMDELM)
C
      INTEGER ISPOT(4),ILINE(4),I,ICHN,NBDPTS,NUMPCP,IXCLPT,IPCP
      INTEGER PRODFLG,INSETVL,ISETVAL,IBADVAL,IIII
      REAL TMPVAL(4),DISRAT(4),WGT(4),TOTRAT, TOTDIS
      LOGICAL CHECKD,INSIDE,ISR4SPV
C
      INCLUDE'SPVAL'
      INCLUDE'INSPARM'
      INCLUDE'I2HINF'
C
C RELATE THE FOUR SURROUNDING SPOTS USED IN THE INTERPOLATION
C (OR EXTRAPOLATION) TO THE HIRS SPOT.
C
      ISPOT(1) = INISPT(INSPOT,INLIN)
      ILINE(1) = INILIN(INSPOT,INLIN)
      ISPOT(2) = ISPOT(1) + 1
      ILINE(2) = ILINE(1)
      IF (.NOT. EXTRAP(INSPOT,INLIN)) THEN
        ISPOT(3) = ISPOT(1)
        ILINE(3) = ILINE(1) + 1
        ISPOT(4) = ISPOT(1) + 1
        ILINE(4) = ILINE(1) + 1
      END IF
C
C CHECK EACH CHANNEL SINCE VALUE COULD BE MISSING FOR ONE CHANNEL
C ON AN INSTRUMENT BUT NOT THE OTHERS. IF THERE ARE ONLY THREE GOOD
C SPOTS SURROUNDING A HIRS FOV CHECK CHECK TO MAKE SURE AN
C INTERPOLATION WILL BE DONE.
C
      CHECKD = .FALSE.
      DO 100 ICHN = 1,NMDELM+1
C
C GET THE TEMPERATURE AND WEIGHT VALUE FOR EACH OF THE SPOTS
C
       IF(ICHN.LE.NMDELM)THEN
          
     
        TMPVAL(1) = DATBUF(ISPOT(1),ILINE(1),ICHN)
        TMPVAL(2) = DATBUF(ISPOT(2),ILINE(2),ICHN)
        WGT(1) = WGTI2H(1,INSPOT,INLIN)
        WGT(2) = WGTI2H(2,INSPOT,INLIN)
C
        IF (.NOT. EXTRAP(INSPOT,INLIN)) THEN
          TMPVAL(3) = DATBUF(ISPOT(3),ILINE(3),ICHN)
          TMPVAL(4) = DATBUF(ISPOT(4),ILINE(4),ICHN)
          WGT(3) = WGTI2H(3,INSPOT,INLIN)
          WGT(4) = WGTI2H(4,INSPOT,INLIN)
        END IF
       ELSE
        TMPVAL(1) = ANCBUF(ISPOT(1),ILINE(1),9)
        TMPVAL(2) = ANCBUF(ISPOT(2),ILINE(2),9)
        WGT(1) = WGTI2H(1,INSPOT,INLIN)
        WGT(2) = WGTI2H(2,INSPOT,INLIN)
C
        IF (.NOT. EXTRAP(INSPOT,INLIN)) THEN
          TMPVAL(3) = ANCBUF(ISPOT(3),ILINE(3),9)
          TMPVAL(4) = ANCBUF(ISPOT(4),ILINE(4),9)
          WGT(3) = WGTI2H(3,INSPOT,INLIN)
          WGT(4) = WGTI2H(4,INSPOT,INLIN)
        END IF
       ENDIF
C
C CHECK TO SEE IF THE VALUES ARE AMSUA BRIGHTNESS TEMPERATURES OR
C AMSUA SURFACE PRODUCTS. IF THEY ARE SURFACE PRODUCTS (ICHN >= 16
C AND ICHN <= 20) THEN SET THE PRODUCT FLAG TO 1.
C
C ADDED THE LAND PRECIPITATION INDEX TO THE MIX (ICHN = 24) AND
C THE NEW PRECIPITATION FLAG (ICHN=28)
C
        PRODFLG = 0
        IF(ICHN.GE.16 .AND. ICHN.LE.20)  PRODFLG =1
        IF(ICHN.EQ.24 .OR. ICHN.EQ.28)  PRODFLG =1
C
C APPLY THE PREVIOUSLY CALCULATED WEIGHTS FOR THE EXTRAPOLATION
C CASE...IF EITHER SPOT TO BE USED IN THE EXTRAPOLATION IS MISSING
C SET THE VALUE TO MISSING. IF THE VALUES ARE TEMPERATURES CHECK
C THE GROSS TEMPERATURE LIMITS. IF THE VALUES ARE SURFACE PRODUCTS
C CHECK FOR MISSING VALUES.
C
c      IF(ICHN.NE.19.AND.ICHN.NE.20.AND.ICHN.NE.NMDELM+1)THEN
       IF(ICHN.NE.20.AND.ICHN.NE.NMDELM+1)THEN
        IF (EXTRAP(INSPOT,INLIN)) THEN
         IF(PRODFLG .EQ. 0) THEN
          IF ((TMPVAL(1) .LT. TLIM(1)).OR.(TMPVAL(1) .GT. TLIM(2))) THEN
            VALNEW(ICHN) = R4SPV
          ELSE IF ((TMPVAL(2) .LT. TLIM(1)).OR.(TMPVAL(2) .GT.
     1                                                   TLIM(2))) THEN
            VALNEW(ICHN) = R4SPV
          ELSE IF (ISPOT(1) .GE. (NMDSPT / 2)) THEN
            VALNEW(ICHN) = TMPVAL(2) + (TMPVAL(2) * WGT(2) -
     1                                            TMPVAL(1) * WGT(1))
          ELSE
            VALNEW(ICHN) = TMPVAL(1) + (TMPVAL(1) * WGT(1) -
     1                                            TMPVAL(2) * WGT(2))
          END IF
         ELSE
          IF ( ISR4SPV(TMPVAL(1)).OR. ISR4SPV(TMPVAL(2))) THEN
            VALNEW(ICHN) = R4SPV
          ELSE IF (ISPOT(1) .GE. (NMDSPT / 2)) THEN
            VALNEW(ICHN) = TMPVAL(2) + (TMPVAL(2) * WGT(2) -
     1                                            TMPVAL(1) * WGT(1))
          ELSE
            VALNEW(ICHN) = TMPVAL(1) + (TMPVAL(1) * WGT(1) -
     1                                            TMPVAL(2) * WGT(2))
          END IF
         ENDIF
C
C IF EXTRAPOLATING AMSU-A DATA, SET SPOT CONFIDENCE TO ZERO IF PRECIP
C CONTAMINATED SPOTS ARE USED - COMMENTED OUT PER RTOVS UPGRADE.
C
C         IF ((INSTR.EQ.2) .AND. ((ANCBUF(ISPOT(1),ILINE(1),13).GT. 0.)
C    1        .OR. (ANCBUF(ISPOT(2),ILINE(2),13) .GT. 0.))) SPTCNF = 0.
        ELSE
C
C POINT WILL BE INTERPOLATED TO...MAKE SURE AT LEAST 3 POINTS HAVE
C VALID DATA. CHECK TO SEE IF THE VALUE IS A AMSU-A TEMPERATURE OR 
C SURFACE PRODUCT.
C
          NBDPTS = 0
          NUMPCP = 0
C
          DO 20 I = 1,4
           IF(PRODFLG .EQ. 0) THEN
            IF ((TMPVAL(I) .LT. TLIM(1)) .OR. (TMPVAL(I) .GT. TLIM(2)))
     1                                                              THEN
              NBDPTS = NBDPTS + 1
              IXCLPT = I
            END IF
           ELSE
            IF (ISR4SPV(TMPVAL(I))) THEN
              NBDPTS = NBDPTS + 1
              IXCLPT = I
            END IF
           ENDIF
C
C IF THERE IS MORE THAN ONE BAD POINT DO NOT DO THE INTERPOLATION
C AND SET THE INTERPOLATED VALUE TO MISSING
C
            IF (NBDPTS .GT. 1) THEN
              VALNEW(ICHN) = R4SPV
              GO TO 100
            END IF
C
C IF AMSU-A IS BEING INTERPOLATED CHECK FOR PRECIP CONTAMINATION
C
            IF ((INSTR .EQ. 2) .AND.
     1                    (ANCBUF(ISPOT(I),ILINE(I),13) .GT. 0.)) THEN
              IPCP = I
              NUMPCP = NUMPCP + 1
            END IF
   20     CONTINUE
C
C IF USING AMSU-A DATA TRY TO USE 3 GOOD SPOTS IF ONE IS PRECIP
C CONTAMINATED, OTHERWISE DROP THE SPOT CONFIDENCE TO ZERO.
C
          IF (NUMPCP .GT. 0) THEN
            IF ((NBDPTS .EQ. 0) .AND. (NUMPCP .EQ. 1)) THEN
              NBDPTS = 1
              IXCLPT = IPCP
            ELSE
              SPTCNF = 0.
            END IF
          END IF
C
C IF ALL FOUR POINTS ARE GOOD OBTAIN THE INTERPOLATED VALUE USING THE
C WEIGHTS WHICH WERE SAVED WHEN THEY WERE PREVIOUSLY GENERATED.
C
          IF (NBDPTS .EQ. 0) THEN
            VALNEW(ICHN) = WGT(1) * TMPVAL(1) + WGT(2) * TMPVAL(2) +
     1        WGT(3) * TMPVAL(3) + WGT(4) * TMPVAL(4)
          ELSE
C
C THERE ARE THREE GOOD POINTS...CHECK TO SEE IF THE POINT TO BE
C INTERPOLATED TO IS WITHIN A TRIANGLE FORMED BY THE POINTS.
C
            IF (.NOT. CHECKD) CALL CHKARA(ISPOT,ILINE,IXCLPT,
     1          NMDSPT,NMDLIN,NMAELM,ANCBUF,HIRLAT,HIRLON,INSIDE)
            IF (INSIDE) THEN
              IF (.NOT. CHECKD) CHECKD = .TRUE.
C
C COMPUTE WEIGHTS BASED ON THE THREE REMAINING GOOD SPOTS (USE
C PREVIOUSLY SAVED DISTANCES).
C
              TOTDIS = 0.
C
              IF (1.NE.IXCLPT)TOTDIS = TOTDIS + DIST2H(1,INSPOT,INLIN)
              IF (2.NE.IXCLPT)TOTDIS = TOTDIS + DIST2H(2,INSPOT,INLIN)
              IF (3.NE.IXCLPT)TOTDIS = TOTDIS + DIST2H(3,INSPOT,INLIN)
              IF (4.NE.IXCLPT)TOTDIS = TOTDIS + DIST2H(4,INSPOT,INLIN)
C
C GENERATE INSTRUMENT VALUE AT THE HIRS SPOT
C
              TOTRAT = 0.
C
              DO 50 I = 1,4
                IF (I .NE. IXCLPT) THEN
                  DISRAT(I) = TOTDIS / DIST2H(I,INSPOT,INLIN)
                  TOTRAT = TOTRAT + DISRAT(I)
                END IF
   50         CONTINUE
C
              VALNEW(ICHN) = 0.
C
              DO 60 I = 1,4
                IF (I .NE. IXCLPT) VALNEW(ICHN) = VALNEW(ICHN) +
     1            (DISRAT(I) / TOTRAT * TMPVAL(I))
   60         CONTINUE

C
            ELSE
C
C POINT TO BE INTERPOLATED TO IS NOT WITHIN TRIANGLE
C
              VALNEW(ICHN) = R4SPV
C
C IF THE POINT DESIGNATED "MISSING" WAS ACTUALLY PRECIP CONTAMINATED
C DO THE INTERPOLATION WITH ALL FOUR SPOTS BUT DROP THE SPOT CONFIDENCE
C
              IF (NUMPCP .EQ. 1) THEN
                VALNEW(ICHN) = WGT(1) * TMPVAL(1) + WGT(2) * TMPVAL(2) +
     1                         WGT(3) * TMPVAL(3) + WGT(4) * TMPVAL(4)
                SPTCNF = 0.
              END IF
            END IF
          END IF
        END IF
       ELSE
C 
C AMSU-A DATA BOOLEAN, NO INTERPOLATION WILL TAKE PLACE, ONLY A COLLOCATION.
C
        ISETVAL = 1
        INSETVL = 0
        IF(ICHN.EQ.NMDELM+1)ISETVAL = 0
        IF(ICHN.EQ.NMDELM+1)INSETVL = 1
        IBADVAL = 0
        DO IIII = 1,4
          IF(TMPVAL(IIII).EQ.1)THEN
             VALNEW(ICHN)=ISETVAL
             GOTO 1414
          ELSE IF (ISR4SPV(TMPVAL(IIII))) THEN
             IBADVAL =IBADVAL+1
          ENDIF
        END DO
        IF(IBADVAL.EQ.4)THEN
          VALNEW(ICHN)=INSETVL
        ELSE
          VALNEW(ICHN)=INSETVL
        ENDIF
 1414   CONTINUE
       ENDIF
  100 CONTINUE
C
  900 RETURN
      END
