      SUBROUTINE INNOHIRS
      IMPLICIT NONE
C***********************************************************************
C
C   NAME- INNOHIRS
C
C   LANGUAGE- FORTRAN     TYPE- SUBROUTINE       COMPUTER- CRAY
C
C   VERSION- 1.0   DATE- 07/20/00  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C
C   FUNCTION- THIS SUBROUTINE GENERATES COEFFICIENTS USED TO INTERPOLATE
C             AMSU-A TO THE HIRS LOCATIONS WHEN THE HIRS INSTRUMENT IS
C             NOT AVAILABLE.
C
C   FILES- NONE
C
C   CALLING ARGUMENTS-
C   NAME     TYPE   I/O  DESCRIPTION
C   ----     ----   ---  -----------
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'INSPARM'
      INCLUDE 'INPARM'
      INCLUDE 'INPCDB'
      INCLUDE 'NOHIRSCF'
C
C   LOCAL PARAMETERS-
C   NAME     TYPE        DESCRIPTION
C   ----     ----        -----------
C   A1MSU    REAL        AMSU-A SECANT AND TIME VALUES FOR AN EXTENDED
C                        DATA FRAME.
C   ADELSTRT REAL        AMSU-A TIME FROM SYNCH TO START OF FIRST SPOT,
C                        PLUS HALF THE INTEGRATION TIME.
C   ADELTINC REAL        AMSU-A INTEGRATION TIME.
C   ADELTLIN REAL        SCAN TO SCAN TIME DELTA IN MILLISECONDS FOR
C                        THE AMSU-A.
C   AMSUA1   REAL        ANGLE IN DEGREES FROM NADIR FOR THE
C                        FIRST AMSU-A SPOT.
C   AMSUDELA REAL        ANGLE DELTA IN DEGREES BETWEEN AMSU-A SPOTS.
C   CA       REAL        FRACTIONAL PART OF ANGLE INTERPOLATION COEFFICIENT
C                        INDEX.
C   CT       REAL        FRACTIONAL PART OF TIME INTERPOLATION COEFFICIENT
C                        INDEX.
C   DEGRAD   REAL        PARAMETER USED IN CONVERTING DEGREES TO RADIANS.
C   DELT     REAL        DELTA BETWEEN ADJACENT TIME VALUES IN A1MSU.
C   DEN      REAL        DELTA BETWEEN ADJACENT ANGLE VALUES IN A1MSU.
C   RATIO    REAL        RATIO OF EARTH RADIUS PLUS SATELLITE
C                        HEIGHT TO EARTH RADIUS.
C   ERTHRAD  REAL        EARTH RADIUS IN KILOMETERS.
C   HDELSTRT REAL        HIRS TIME FROM SYNCH TO START OF FIRST SPOT,
C                        PLUS HALF THE INTEGRATION TIME.
C   HDELTINC REAL        HIRS INTEGRATION TIME.
C   HDELTLIN REAL        SCAN TO SCAN TIME DELTA IN MILLISECONDS FOR
C                        THE HIRS.
C   HIRS     REAL        HIRS SECANT AND TIME VALUES FOR AN EXTENDED
C                        DATA FRAME.
C   HIRSA1   REAL        ANGLE IN DEGREES FROM NADIR FOR THE
C                        FIRST HIRS SPOT.
C   HIRSDELA REAL        ANGLE DELTA IN DEGREES BETWEEN HIRS SPOTS.
C   I,J,K    INT         LOOP INDEX VARIABLES.
C   IA       INT         LINE INDEX.
C   ICA      INT         ANGLE INTERPOLATION COEFFICIENT INDEX.
C   ICT      INT         TIME INTERPOLATION COEFFICIENT INDEX.
C   INX1     INT         SPOT INDEX VALUE HOLDER.
C   JA       INT         SPOT INDEX.
C   MIS      INT         LOOP INDEX.
C   REF      REAL        REFERENCE VALUE USED TO COMPUTE DEN AND
C                        DELT.
C   SATHGT   REAL        SATELLITE HEIGHT IN KILOMETERS.
C   SUM      REAL        SUM OF COEFFICIENT VALUES TO USE WHEN
C                        ONE OF FOUR AMSU-A SPOTS IS MISSING.
C   T1       REAL        VECTOR OF AMSU-A TIMES FOR EACH HIRS SCAN
C                        POSITION.
C   Z        REAL        ANGULAR OFFSET FROM NADIR IN RADIANS.
C
C   SUBPROGRAMS CALLED- NONE
C
C   FUNCTIONS USED- NONE
C
C   COMMENTS- THIS SUBROUTINE IS BASED ON CODE WRITTEN BY DR. LARRY
C             MCMILLIN.
C
C***********************************************************************
C
C
      REAL    ERTHRAD, SATHGT, ADELSTRT, ADELTINC, HDELSTRT, HDELTINC
      REAL    HIRSDELA, ADELTLIN, AMSUDELA, RATIO, HIRSA1, AMSUA1
      REAL    HIRS(2,HNSPOT,HTOTLN), HDELTLIN, REF, DEN
      REAL    A1MSU(2,ANSPOT,ATOTLN)
      REAL    DELT, SUM, CA, CT, T1(ATOTLN), Z
      REAL    DEGRAD
      INTEGER   I, J, IA, JA, ICA, ICT, MIS, K, INX1
      PARAMETER (ERTHRAD=6371.22)        
      PARAMETER (ADELSTRT=3.55 + 0.5*165.0,ADELTINC=165.0)
      PARAMETER (HDELSTRT=32+3*100+0.5*68.0,HDELTINC=100.0)
      PARAMETER (HIRSDELA=1.8,HDELTLIN=1000.0*256.0/40.0)
      PARAMETER (ADELTLIN=1000.0*256.0/32.0,AMSUDELA=3.0+(1.0/3.0))
      PARAMETER (HIRSA1=(-27.5)*1.8,AMSUA1=-14.5*AMSUDELA)
      PARAMETER (DEGRAD=0.0174532925)

C-- COMPUTE THE EARTH RATIO USING THE SATELLITE HEIGHT FROM THE CDB.
  
      SATHGT = REAL(HTSAT)
      RATIO = (ERTHRAD+SATHGT) / ERTHRAD

C--      GENERATE HIRS SECANT AND TIME VALUES
      DO I = 1, HTOTLN
         DO J = 1, HNSPOT
            Z = DEGRAD*(HIRSA1 + ((J-1) * HIRSDELA))
            HIRS(1,J,I) = 1.0/COS(ASIN(RATIO*SIN(Z)))
            HIRS(2,J,I) = HDELTINC*(J-1) + HDELTLIN*(I-1)
            HIRS(2,J,I) = HIRS(2,J,I) + HDELSTRT
         END DO
      END DO 
C--      GENERATE AMSU-A SECANT AND TIME VALUES
      DO I = 1, ATOTLN
         DO J = 1, ANSPOT
            Z = DEGRAD*(AMSUA1 + ((J-1) * AMSUDELA))
            A1MSU(1,J,I) = 1.0/COS(ASIN(RATIO*SIN(Z)))
            A1MSU(2,J,I) = ADELTINC * (J-1) + ADELTLIN * (I-1)
            A1MSU(2,J,I) = A1MSU(2,J,I) + ADELSTRT
         END DO
      END DO
C
C-- GENERATE INTERPOLATION COEFFICIENTS.  USE BILINEAR INTERPOLATION.
C   THE AMSU-A1 AND AMSU-A2 INSTRUMENTS ACTUALLY HAVE DIFFERENT
C   INTERPOLATION COEFFICIENTS DUE TO DIFFERENCES IN TIMING.
C   THIS DIFFERENCE HAS BEEN IGNORED IN FAVOR OF USING THE
C   TIMING VALUES FOR THE AMSU-A1 INSTRUMENT FOR BOTH.
C
      IA = 1
      JA = 16
C
C--   LOCATE THE AMSU-A SPOT IN THE CROSS-TRACK DIRECTION.
C
      DO J = 29, HNSPOT
         IF (HIRS(1,J,I) .GT. A1MSU(1,JA+1,IA)) THEN
            JA = JA + 1
            IF (JA .GT. 29) JA = 29
         END IF
         REF = A1MSU(1,JA,IA)
         DEN = A1MSU(1,JA+1,IA) - REF
         IF (DEN .NE. 0.0) THEN
ccft        INDA(J) = JA + (HIRS(1,J,I) - REF) / DEN
            INDA(J) = FLOAT(JA) + (HIRS(1,J,I) - REF) / DEN
         ELSE
C
C--         THIS CODE PREVENTS A ZERO DIVIDE
C
            DEN = 2.0 * REF - 1.0
            IF (J .LE. 28) THEN
ccft           INDA(J) = JA + (HIRS(1,J,I) - REF) / DEN
               INDA(J) = FLOAT(JA) + (HIRS(1,J,I) - REF) / DEN
            END IF
         END IF
      END DO
C
      DO J = 1, 28
         INDA(J) = 31.0 - INDA(57 - J)
      END DO
            
C
C     INTERPOLATE ALL HIRS SPOTS IN THIS DIRECTION.  LOCATE
C     THE AMSU-A SPOT IN THE TIME ALONG THE TRACK FOR EACH SPOT
C     ACROSS THE TRACK.
C     1. INTERPOLATE THE AMSU-A TIMES TO THE HIRS SCAN POSITION.
C     2. INTERPOLATE THE TIME ALONG THE TRACK.
C
C--      GENERATE A VECTOR OF AMSU-A TIMES FOR EACH HIRS SCAN
C        POSITION.
C
      DO J = 1, HNSPOT
         INX1 = NINT(INDA(J))
         IF (INX1 .LT. 1) INX1 = 1
         IF (INX1 .GT. 29) INX1 = 29
         DO K = 1, ATOTLN-1
            REF = A1MSU(2,INX1,K)
            DELT = A1MSU(2,INX1+1,K) - A1MSU(2,INX1,K)
c           T1(K) = REF + (INDA(J) - (INX1)) * DELT
            T1(K) = REF + (INDA(J) -FLOAT(INX1)) * DELT
         END DO 
C        T1(ATOTLN) = REF + (INDA(J) - INX1) * DELT + 8000
         T1(ATOTLN) = REF + (INDA(J) - FLOAT(INX1)) * DELT + 8000.0
C
C--      NOW INTERPOLATE THE HIRS TIMES.
C
         IA = 1
         DO I = 1, HTOTLN
            IF (HIRS(2,J,I) .GT. T1(IA+1)) THEN
               IA = IA + 1
               IF (IA .GT. ATOTLN) IA = ATOTLN
            END IF
            IA = IA - 1
            IF (IA .LT. 1) IA = 1
            REF = T1(IA)
            DELT = (T1(IA+1) - T1(IA))
C           INDT(I,J) = IA + (HIRS(2,J,I) - REF) / DELT
            INDT(I,J) = FLOAT(IA) + (HIRS(2,J,I) - REF) / DELT
         END DO
      END DO
              
C
C--   GENERATE THE INTERPOLATION COEFFICIENTS.  NOTE THE CROSS TRACK
C     LOCATIONS ARE REPEATED FOR EACH LINE.
C     FIRST INTERPOLATE ALONG TWO OPPOSITE SIDES, THEN INTERPOLATE
C     ALONG THE LINE CONNECTING THE TWO POINTS.
C
      DO I = 1, HTOTLN
         DO J = 1, HNSPOT
            ICT = INDT(I,J)
            ICA = INDA(J)
c           CT = INDT (I,J) - ICT
            CA = INDA(J) - ICA
            CT = INDT (I,J) - FLOAT(ICT)
c           CA = INDA(J) - FLOAT(ICA)
            AA2HIR(1,J,I) = CT*CA
            AA2HIR(2,J,I) = CT*(1.0 - CA)
            AA2HIR(3,J,I) = (1.0 - CT) * CA
            AA2HIR(4,J,I) = (1.0 - CT) * (1.0 - CA)
         END DO
      END DO
C
C-- CALCULATE THE WEIGHTS TO USE IF ONE AMSU-A SPOT IS MISSING.
C
      DO MIS = 1, 4
         DO I = 1, HTOTLN
            DO J = 1, HNSPOT
               SUM = 0.0
               DO K = 1, 4
                  IF (K .NE. MIS) THEN
                     SUM = SUM + AA2HIR(K,J,I)
                  END IF
               END DO
               DO K = 1, 4
                  AA2HIR(K + (MIS)*4,J,I) = AA2HIR(K,J,I) / SUM
               END DO
               AA2HIR(MIS+(MIS)*4,J,I) = 0.0
            END DO
         END DO
      END DO     
C
      RETURN
      END
