      SUBROUTINE CMPWGT(INSPOT,INLIN,RLATHR,RLONHR,ICLSPT,DELTSP,
     1			ICLLIN,DELTSC,NMDSPT,NMDLIN,NMAELM,
     2			ANCBUF,CNFLIM,ERSTAT)
      IMPLICIT NONE
C**********************************************************************
C
C   NAME- CMPWGT
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- HDS 9000
C
C   VERSION- 1.0    DATE- 01/10/95    PROGRAMMER- TIM GARDNER, HSTX
C   VERSION- 2.0    DATE- 06/26/95    PROGRAMMER- LI LU (SMSRC)
C   VERSION- 2.1    DATE- 09/28/95    PROGRAMMER- LI LU (SMSRC)
C     ATOVS-220: REPLACE CALLING ARGUMENT AGOODL WITH CNFLIM AND
C                CHANGE THE LOGIC FROM RETURNING IF A SCAN LINE IS
C                BAD TO CHECKING THE SPOT CONFIDENCE AGAINST THE
C                CNFLIM FOR THE AMSU-A SPOTS WHICH NEED TO BE USED
C                FOR INTERPOLATION TO THE HIRS SPOT. IF MORE THAN
C                TWO SPOTS FAIL TO MEET OR BEAT THE CNFLIM THEN
C                INTERPOLATION WILL NOT BE PERFORMED FOR THAT SPOT.
C   VERSION- 3.0    DATE- 04/28/96    PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.1    DATE- 02/05/99    PROGRAMMER- LEAH WILSON (RAYTHEON)
C
C   FUNCTION- THIS ROUTINE COMPUTES WEIGHTS TO BE APPLIED TO FOUR
C   INSTRUMENT SPOTS SURROUNDING A GIVEN HIRS SPOT. IN THE CASE OF A
C   HIRS SPOT NEAR THE LIMB, WEIGHTS ARE CALCULATED FOR THE NEAREST TWO
C   SPOTS ALONG THE CLOSEST LINE (FOR EXTRAPOLATION ALONG THE LINE).
C   THE CALCULATED WEIGHTS ARE STORED IN THE COMMON BLOCK /I2HINF/.
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE
C                ON THE CRAY.
C   VERSION 3.1- CONVERT FROM MMLF TO KIDS.
C   VERSION- 3.2    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace 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 FOR WHICH WEIGHTS
C                                WILL BE GENERATED.
C
C     INLIN           INT    I   HIRS LINE NUMBER FOR WHICH WEIGHTS
C                                WILL BE GENERATED.
C
C     RLATHR	      REAL   I   LATITUDE OF HIRS SPOT TO BE
C				 INTERPOLATED/EXTRAPOLATED TO.
C
C     RLONHR	      REAL   I   LONGITUDE OF HIRS SPOT TO BE
C 				 INTERPOLATED/EXTRAPOLATED TO.
C
C     ICLSPT          INT    I   SPOT # OF THE INSTRUMENT SPOT WHICH IS
C                                CLOSEST TO THE HIRS FOV.
C
C     DELTSP          REAL   I   DISPLACEMENT OF ICLSPT FROM HIRS FOV
C                                ALONG A SCAN LINE.
C
C     ICLLIN          INT    I   LINE # OF THE INSTRUMENT SPOT WHICH IS
C                                CLOSEST TO THE HIRS FOV.
C
C     DELTSC          REAL   I   DISPLACEMENT OF ICLLIN FROM HIRS FOV
C                                ALONG A SCAN POSITION.
C
C     NMDSPT          INT    I   NUMBER OF SPOTS PER LINE IN ANCBUF.
C
C     NMDLIN          INT    I   NUMBER OF LINES PER FRAME IN ANCBUF.
C
C     NMAELM          INT    I   NUMBER OF ELEMENTS IN ANCBUF.
C
C     ANCBUF          REAL   I   DIMENSIONED (NMDSPT,NMDLIN,NMAELM)
C                                ARRAY CONTAINING ANCILLARY INFORMATION
C                                FOR INTERPOLATED/EXTRAPOLATED DATA.
C
C     CNFLIM          INT    I   THE SPOT CONFIDENCE MINIMUM LIMIT.
C
C     ERSTAT          INT    O   CONTAINS ERROR CODE TO BE RETURNED TO
C                                CALLING ROUTINE IF AN ERROR OCCURRED.
C
C   COMMON AREA PARAMETERS- SPVAL,INSPARM,FRMINF,I2HINF
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     INISP0                   INT    SPOT NUMBER OCCURRING EARLIEST
C			    	      IN THE FOUR SURROUNDING SPOTS.
C
C     INILN0                   INT    LINE NUMBER OCCURRING EARLIEST
C 				      IN THE FOUR SURROUNDING SPOTS.
C
C     RLAT                     REAL   DIMENSIONED (4): LATITUDE OF FOUR
C                                     SPOTS WHICH SURROUND THE HIRS FOV.
C
C     RLON                     REAL   DIMENSIONED (4): LONGITUDE OF FOUR
C                                     SPOTS WHICH SURROUND THE HIRS FOV.
C
C     TOTDIS                   REAL   TOTAL DISTANCES FROM DATBUF FOVS
C                                     TO THE HIRS FOV TO BE INTERPOLATED
C                                     /EXTRAPOLATED TO.
C
C     ALLPTS                   LOG    SET TRUE IF ALL FOVS SURROUNDING
C                                     A HIRS FOV ARE GOOD.
C
C     DISRAT                   REAL   DIMENSIONED (4): RATIO BETWEEN THE
C                                     TOTAL DISTANCE FROM HIRS FOV TO
C                                     INTERPOLATION FOVS.
C
C     TOTRAT                   REAL   TOTAL OF THE DISRAT FOR THE FOUR
C                                     FOVS USED IN THE INTERPOLATION.
C
C     BADSPT                   INT    COUNTER OF SPOTS USED IN THE
C                                     INTERPOLATION/EXTRAPOLATION WHICH
C                                     DID NOT PASS THE CONFIDENCE CHECK.
C
C     I                        INT    LOOP INDEX.
C
C
C   SUBPROGRAMS CALLED- ARCLEN, CMPWG$
C
C   EXIT STATES-
C        ERSTAT =  0; INDICATING NO ERRORS FOUND
C        ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED
C
C*********************************************************************
C
C
      INCLUDE 'SPVAL'
      INCLUDE 'INSPARM'
      INCLUDE 'FRMINF'
      INCLUDE 'I2HINF'
C
      INTEGER INSPOT,INLIN,ICLSPT,ICLLIN,NMDSPT,NMDLIN,NMAELM
      INTEGER CNFLIM,ERSTAT
      REAL    ANCBUF(NMDSPT,NMDLIN,NMAELM)
      REAL    RLATHR,RLONHR,DELTSP,DELTSC
C
      INTEGER INISP0,INILN0,BADSPT,I
      REAL    RLAT(4),RLON(4),DISRAT(4)
      REAL    TOTDIS,TOTRAT
      LOGICAL   ALLPTS
      LOGICAL ISR4SPV
C
      ERSTAT = 0
      EXTRAP(INSPOT,INLIN) = .FALSE.
      WGTCAL(INSPOT,INLIN) = .FALSE.
C
C RETURN TO CALLING PROGRAM IF THE LATITUDE OR LONGITUDE VALUE IS
C MISSING.
C

      IF ((ISR4SPV(RLATHR)) .OR. (ISR4SPV(RLONHR))) RETURN
      IF ((RLATHR .GT. 90.0) .OR. (RLATHR .LT. -90.0) .OR.
     1    (RLONHR .GT. 180.0) .OR. (RLONHR .LT. -180.0)) THEN
        CALL CMPWG$(111)
        ERSTAT = 99
        RETURN
      END IF
C
C IF THE HIRS SPOT LIES OUTSIDE OF THE SCAN OF THE INSTRUMENT DATA
C TO BE EVALUATED AT THE HIRS SPOT THEN AN EXTRAPOLATION WILL BE
C PERFORMED.  SAVE THE SPOT NUMBER CLOSEST TO THE BEGINNING OF THE SCAN.
C
      IF ((ICLSPT .EQ. NMDSPT) .AND. (DELTSP .GT. 0.)) THEN
        EXTRAP(INSPOT,INLIN) = .TRUE.
        INISPT(INSPOT,INLIN) = NMDSPT - 1
        INILIN(INSPOT,INLIN) = ICLLIN
      ELSE IF ((ICLSPT .EQ. 1) .AND. (DELTSP .LT. 0.)) THEN
        EXTRAP(INSPOT,INLIN) = .TRUE.
        INISPT(INSPOT,INLIN) = 1
        INILIN(INSPOT,INLIN) = ICLLIN
      ELSE
C
C DETERMINE WHERE CLOSEST SPOT IS IN RELATION TO THE HIRS SPOT TO
C BE INTERPOLATED TO. SET VARIABLES WHICH SAVE THE SURROUNDING
C SPOT WHICH IS EARLIEST OF THE FOUR IN THE FRAME.
C
        IF (DELTSP .GT. 0.) THEN
          INISPT(INSPOT,INLIN) = ICLSPT
        ELSE
          INISPT(INSPOT,INLIN) = ICLSPT - 1
        END IF

        IF (DELTSC .GT. 0.) THEN
          INILIN(INSPOT,INLIN) = ICLLIN
        ELSE
          INILIN(INSPOT,INLIN) = ICLLIN - 1
        END IF
      END IF

      INISP0 = INISPT(INSPOT,INLIN)
      INILN0 = INILIN(INSPOT,INLIN)
C
C IF THE INTERPOLATION IS TO BE DONE OUTSIDE OF THE FIRST OR LAST
C LINE INDICATE THIS AND RETURN TO THE CALLING ROUTINE.
C
      IF (.NOT. EXTRAP(INSPOT,INLIN)) THEN
        IF ((INILN0 .LT. 1) .OR. (INILN0 .EQ. NMDLIN)) THEN
          CALL CMPWG$(112)
          ERSTAT = 99
          RETURN
        END IF
      END IF
C
C CHECK THE SPOT CONFIDENCE AGAINST THE CNFLIM FOR THE AMSU-A SPOTS.
C IN CASE OF EXTRAPOLATION, IF ONE OF THE TWO SPOTS IS NO GOOD, OR IN
C CASE OF INTERPOLATION MORE THAN ONE SPOT OF THE FOUR ARE NO GOOD,
C THEN FOR THE CORRESPONDING HIRS SPOT NO INTERPOLATION/EXTRAPOLATION
C WILL BE PERFORMED.
C
      IF (EXTRAP(INSPOT,INLIN)) THEN
         IF ((ANCBUF(INISP0,INILN0,5).LT.REAL(CNFLIM)) .OR.
     1       (ANCBUF(INISP0+1,INILN0,5).LT.REAL(CNFLIM))) RETURN
      ELSE
         BADSPT = 0
         IF (ANCBUF(INISP0,INILN0,5).LT.REAL(CNFLIM))
     1       BADSPT = BADSPT+1
         IF (ANCBUF(INISP0+1,INILN0,5).LT.REAL(CNFLIM))
     1       BADSPT = BADSPT+1
         IF (ANCBUF(INISP0,INILN0+1,5).LT.REAL(CNFLIM))
     1       BADSPT = BADSPT+1
         IF (ANCBUF(INISP0+1,INILN0+1,5).LT.REAL(CNFLIM))
     1       BADSPT = BADSPT+1
         IF (BADSPT .GT. 1) RETURN
      END IF
C
C ASSIGN LAT/LONS FOR THE FOUR SPOTS SURROUNDING THE HIRS SPOT.
C
      RLAT(1) = ANCBUF(INISP0,INILN0,1)
      RLON(1) = ANCBUF(INISP0,INILN0,2)
      RLAT(2) = ANCBUF(INISP0 + 1,INILN0,1)
      RLON(2) = ANCBUF(INISP0 + 1,INILN0,2)

      IF (.NOT. EXTRAP(INSPOT,INLIN)) THEN
        RLAT(3) = ANCBUF(INISP0,INILN0 + 1,1)
        RLON(3) = ANCBUF(INISP0,INILN0 + 1,2)
        RLAT(4) = ANCBUF(INISP0 + 1,INILN0 + 1,1)
        RLON(4) = ANCBUF(INISP0 + 1,INILN0 + 1,2)
      END IF
C
C CALCULATE THE DISTANCE BETWEEN THE HIRS FOV TO BE INTERPOLATED TO AND
C EACH OF THE FOUR FOVS TO BE USED IN THE INTERPOLATION.
C
      TOTDIS = 0.
      ALLPTS = .TRUE.

      DO 30 I = 1, 4

        IF (EXTRAP(INSPOT,INLIN) .AND. (I .GT. 2)) THEN
          DIST2H(I,INSPOT,INLIN) = R4SPV
        ELSEIF ((ISR4SPV(RLAT(I))) .OR. (ISR4SPV(RLON(I)))) THEN
          DIST2H(I,INSPOT,INLIN) = R4SPV
          ALLPTS = .FALSE.
        ELSE
          CALL ARCLEN(RLATHR,RLONHR,RLAT(I),RLON(I),
     1                                   DIST2H(I,INSPOT,INLIN))
          IF (DIST2H(I,INSPOT,INLIN) .LT. .1) DIST2H(I,INSPOT,INLIN)=.1
          TOTDIS = TOTDIS + DIST2H(I,INSPOT,INLIN)
        END IF

   30 CONTINUE
C
C CALCULATE WEIGHTS...FOR EXTRAPOLATION, ONLY WEIGHTS FOR THE TWO
C NEAREST INSTRUMENT SPOTS ALONG THE SCAN LINE ARE CALCULATED.
C INTERPOLATION WEIGHTS FOR THE FOUR SURROUNDING SPOTS ARE CALCULATED
C
      IF (EXTRAP(INSPOT,INLIN) .AND. ALLPTS) THEN
        IF (ICLSPT .EQ. NMDSPT) THEN
          WGTI2H(1,INSPOT,INLIN) = DIST2H(2,INSPOT,INLIN) /
     1                                    DIST2H(1,INSPOT,INLIN)
        ELSE IF (ICLSPT .EQ. 1) THEN
          WGTI2H(1,INSPOT,INLIN) = DIST2H(1,INSPOT,INLIN) /
     1                                    DIST2H(2,INSPOT,INLIN)
        END IF

        WGTI2H(2,INSPOT,INLIN) = WGTI2H(1,INSPOT,INLIN)
        WGTCAL(INSPOT,INLIN) = .TRUE.

      ELSE IF (ALLPTS) THEN
        TOTRAT = 0
        DO 40 I = 1,4

          DISRAT(I) = TOTDIS / DIST2H(I,INSPOT,INLIN)
          TOTRAT = TOTRAT + DISRAT(I)

   40   CONTINUE
        WGTI2H(1,INSPOT,INLIN) = DISRAT(1) / TOTRAT
        WGTI2H(2,INSPOT,INLIN) = DISRAT(2) / TOTRAT
        WGTI2H(3,INSPOT,INLIN) = DISRAT(3) / TOTRAT
        WGTI2H(4,INSPOT,INLIN) = DISRAT(4) / TOTRAT
        WGTCAL(INSPOT,INLIN) = .TRUE.
      END IF

      RETURN
      END
C
      SUBROUTINE CMPWG$(MSGCOD)
C
C*********************************************************************
C   NAME- CMPWG$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 03/25/93    PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 2.0    DATE- 06/26/95    PROGRAMMER- LI LU (SMSRC)
C   VERSION- 3.0    DATE- 02/05/99    PROGRAMMER- LEAH WILSON (RAYTHEON)
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE CMPWGT.
C   VERSION- 3.0 CONVERT TO KIDS CONVENTION.
C
C   FILES-
C     DSN                       I/O UNIT  FUNCTIONAL DESCRIPTION
C     ---                       --- ----  -----------------------
C     ...KIDS                    O  KUNIT KRAY INTERACTIVE DOCUMENTATION
C                                         FILE.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE  TYPE  I/O  FUNCTION
C     --------  ----  ---  --------
C     MSGCOD    INT   I   MESSAGE CODE USED TO IDENTIFY THE ERROR
C                         PATTERN
C
C   COMMON AREA PARAMETERS-  ERPARM,ERINFO,SPGUNT
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     I                       INT     DO LOOP COUNTER
C     C4TEMP                  CH*4    TEMP FOR WRITING TO KIDS
C
C   SUBPROGRAMS CALLED- KIDS 
C
C   EXIT STATES-
C       STAT =  0; INDICATING NO ERRORS FOUND
C       STAT =  1; INDICATING AN NON-FATAL ERROR
C       STAT = 99; INDICATING A FATAL ERROR WAS DETECTED
C
C*********************************************************************
C
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPGUNT'
      CHARACTER*4 C4TEMP
      CHARACTER*100 KMSG
C
      INTEGER   MSGCOD,STAT,I
C
C  SET ERINFO:SUBNAM
C
      STAT = 0
      SUBNAM = 'SPGDVR'
C
C DETERMINE WHICH ERROR MESSAGE TO WRITE, IF MSGCOD = 111
C
      IF (MSGCOD .EQ. 111) THEN
        KMSG(1:50)='* FATAL ERROR * Invalid HIRS lat/lon value - no co'
        KMSG(51:100)='llocation occurred.'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=99
C
C
C IF MSGCOD = 112,
C
      ELSE IF (MSGCOD .EQ. 112) THEN
        KMSG(1:50)='* FATAL ERROR * Interpolation attempted outside of'
        KMSG(51:100)=' grid.ion occurred.'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=99
C
C OTHERWISE NOT A VALID MSGCOD
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
