      SUBROUTINE AMA2HR(AMAANC,HIRDAT,HIRANC,OBSHIR,SYNTIM)
      IMPLICIT NONE
C***********************************************************************
C    NAME- AMA2HR
C
C    LANGUAGE- FORTRAN           TYPE- SUBROUTINE     COMPUTER- CRAY
C
C    VERSION- 1.0    DATE- 7/25/00  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 1.1    DATE- 1/16/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 1.2    DATE- 4/26/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 2.0    DATE- 2/27/03  ORIGRAMMER- RICKEY ROLLINS (CSC) 
C
C    FUNCTION-  THIS SUBROUTINE INTERPOLATES AMSU-A ANCILLARY DATA TO
C               FILL THE HIRS ANCILLARY DATA BUFFER WHEN THE HIRS
C               INSTRUMENT IS NOT AVAILABLE. IT PUTS REPRESENTATIVE
C               HIRS BRIGHTNESS TEMPERATURES INTO THE HIRS DATA
C               BUFFER.
C
C    VERSION- 1.1 ADDED CODE TO GENERATE AND STORE THE HIRS SATELLITE
C                 ZENITH ANGLE, WHICH WILL BE STORED ON THE RODF
C                 WHEN A RETRIEVAL IS MADE. (WR #100)
C    VERSION- 1.2 CORRECTED THE CODE THAT GENERATES HIRS SCAN TIMES AND
C                 SET THE TIME OF THE FIRST HIRS SCAN TO 32 SECONDS    
C                 (4 AMSU-A SCANS) BEFORE THE TIME OF THE FIRST AMSU-A 
C                 SCAN IN THE ORBIT. (PR #35)                          
C    VERSION- 2.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C
C    FILES-  NONE
C
C    CALLING ARGUMENTS-
C    NAME     TYPE   I/O   DESCRIPTION
C    ----     ----   ---   -----------
C    AMAANC   REAL    I    AMSU-A ANCILLARY DATA BUFFER.
C
C    HIRDAT   REAL    O    HIRS DATA BUFFER.
C
C    HIRANC   REAL    O    HIRS ANCILLARY DATA BUFFER.
C
C    OBSHIR   REAL    O    HIRS OBSERVATION BUFFER.  FOR EACH
C                          SPOT AND LINE, THE 20TH ELEMENT
C                          WILL CONTAIN THE ORBIT NODE.
C                          (1 = ASCENDING,
C                           2 = DESCENDING).
C    SYNTIM   INT    I/O   ARRAY (2 X 3) OF INSTRUMENT SYNCH 
C                          TIMES.  
C
C    COMMON AREA PARAMETERS- 
C
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'NOHIRSCF'
      INCLUDE 'COLLOC'
      INCLUDE 'FRMINF'
      INCLUDE 'HIRSTAT'
      INCLUDE 'DEBUG'
      INCLUDE 'SPVAL'
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
C      ANGIDX        INT    ANGLE INDEX FOR INTERPOLATING AMSU-A
C                           SPOTS.
C      CHAN          INT    HIRS CHANNEL.
C      FIRST         LOG    FLAG SET TO .FALSE. THE FIRST TIME
C                           THIS ROUTINE IS CALLED
C      HEIGHT        REAL   SATELLITE HEIGHT IN KILOMETERS.
C      HSYNTM        INT    HIRS INSTRUMENT SYNCH TIME.
C      I, J, K       INT    LOOP INDEX.
C      ITEMP         INT    TEMPORARY HOLDER FOR INTERPOLATED
C                           INTEGER VALUES.
C      ITERR         INT    ARRAY OF COUNTERS FOR TERRAIN TYPES
C                           OF FOUR AMSU-A SPOTS.
C      MINUS         INT    COUNT OF AMSU-A SPOTS WITH NEGATIVE
C                           (WEST) LONGITUDES.
C      MISSNG        INT    VALUE USED TO CHECK FOR MISSING
C                           AMSU-A LATITUDES OR LONGITUDES.
C      PLUS          INT    COUNT OF AMSU-A SPOTS WITH POSITIVE
C                           (EAST) LONGITUDES.
C      RTEMP         REAL   TEMPORARY HOLDER FOR INTERPOLATED
C                           REAL VALUES.
C      SETBTS        REAL   DEFAULT HIRS CHANNEL BRIGHTNESS   
C                           TEMPERATURES.
C      TERTYP        INT    TERRAIN TYPE.
C      TIMIDX        INT    SCAN TO SCAN INDEX.
C      VAL1-VAL4     REAL   VARIABLES USED IN INTERPOLATING
C                           ACROSS THE INTERNATIONAL DATE
C                           LINE.
C      ZENANG        REAL   HIRS SATELLITE ZENITH ANGLE.
C
C    SUBPROGRAMS CALLED- NONE
C
C    FUNCTIONS USED- SATZEN
C
C    EXIT STATES- NONE
C**********************************************************************
C 
      REAL     AMAANC(ANSPOT,ATOTLN,ANANC) 
      REAL     HIRDAT(HNSPOT,HTOTLN,HDATEL)
      REAL     HIRANC(HNSPOT,HTOTLN,HNANC)
      REAL     OBSHIR(HNSPOT,HTOTLN,20)
      INTEGER  SYNTIM(2,3)
      
C
      INTEGER  I, J, K, ITEMP, ITERR(0:11), TERTYP, MINUS, PLUS
      INTEGER  TIMIDX, ANGIDX, HSYNTM(2), CHAN, MISSNG
      REAL     RTEMP, SETBTS(20), COUNT, VAL1, VAL2, VAL3, VAL4
      REAL     ZENANG, HEIGHT, SATZEN
      LOGICAL  FIRST
      LOGICAL ISR4SPV
      DATA FIRST /.TRUE./
      DATA SETBTS/244.0, 233.0, 231.0, 231.0, 239.0,
     +            248.0, 251.0, 275.0, 244.0, 261.0,
     +            245.0, 229.0, 260.0, 255.0, 253.0,
     +            240.0, 269.0, 282.0, 298.0,  60.0/
      SAVE FIRST, SETBTS
C
C   INITIALIZE THE HIRS ANCILLARY BUFFER.
C
      IF (FIRST) THEN
         DO I = 1, HNSPOT
            DO J = 1, HTOTLN
               DO K = 1, HNANC
                  HIRANC(I,J,K) = R4SPV
               END DO
               DO K = 1, 20
                  OBSHIR(I,J,K) = R4SPV
               END DO
            END DO
         END DO
C                                                       
C  SET THE HIRS SYNCHRONIZATION TIME BACK THE EQUIVALENT
C  OF FOUR AMSU-A SCANS (32,000 MILLISECONDS) FOR THE   
C  FIRST DATA FRAME (PR #35).                           
C                                                       
ccft     SYNTIM(2,1) = SYNTIM(2,1) - 32000              
         FIRST = .FALSE.
      END IF
C
C  GENERATE HIRS SCAN LINE TIMES.
C
      HSYNTM(1)   = SYNTIM(1,1)
      HSYNTM(2)   = SYNTIM(2,1)

C
C     FIRST CALCULATE SCAN LINE TIMES IN THE OVERLAP
C     BUFFER.
C
      DO I = 1, HTOTBF-1
         CALL CMPTIM(HSYNTM,HIRTIM(3),I-HTOTBF,
     +               HSCTIM(1,I))
      END DO
C
C     SET THE FIRST LINE OF THE SCAN LINE TIME
C     ARRAY TO THE HIRS SYNCHRONIZATION TIME.
C
      HSCTIM(1,HTOTBF) = HSYNTM(1)
      HSCTIM(2,HTOTBF) = HSYNTM(2)
C
C     COMPUTE SCAN LINE TIMES FOR THE REMAINDER
C     OF THE HIRS FRAME BUFFER.
C
      DO I = HTOTBF+1, HTOTLN
         CALL CMPTIM(HSYNTM,HIRTIM(3),I-HTOTBF,
     +               HSCTIM(1,I))
      END DO
      DO I = 1, HTOTLN
         IF (DBUGPRT.EQ.1) THEN
            PRINT *,' AMA2HR: HSCTIM FOR HIRS FOR LINE ',I,' = ',
     +                HSCTIM(1,I), HSCTIM(2,I)
         END IF
      END DO
C
C     SET THE HIRS SYNCHRONIZATION TIME.
C
      SYNTIM(1,1) = HSCTIM(1,HTOTLN)
      SYNTIM(2,1) = HSCTIM(2,HTOTLN)
C
C  INTERPOLATE THE AMSU-A LATITUDE, LONGITUDE, AND ORBIT
C  NODE FOR EACH HIRS FOV.
C
      DO I = 1, HTOTLN
C
C        SET THE HIRS GOOD LINE FLAG TO TRUE.
C
         HGOODL(I) = .TRUE.
C
C        SKIP HIRS SPOTS 1 AND 56 UNTIL AN ACCURATE
C        EXTRAPOLATION METHOD HAS BEEN TESTED.
C        
         DO 100 J = 2, HNSPOT-1
C
C           GET THE TIME INDEX
C
            TIMIDX = INDT(I,J)
ccft Try using the nearest integer instead of truncating
c           TIMIDX = NINT(INDT(I,J))
            IF (TIMIDX .LT. 1) TIMIDX = 1
            IF (TIMIDX .GT. (ATOTLN-1)) TIMIDX = ATOTLN -1
C
C           GET THE ANGLE INDEX
C
c           ANGIDX = INDA(J)
ccft Try using the nearest integer instead of truncating
            ANGIDX = NINT(INDA(J))
            IF (ANGIDX .LT. 1) ANGIDX = 1
            IF (ANGIDX .GT. (ANSPOT-1)) ANGIDX = ANSPOT -1
C
C           IF AN AMSU-A LATITUDE OR LONGITUDE IS MISSING,
C           MARK THE HIRS SPOT AS MISSING.
C
            MISSNG = 0
            IF (ISR4SPV(AMAANC(ANGIDX,TIMIDX,1))) MISSNG=MISSNG+1
            IF (ISR4SPV(AMAANC(ANGIDX,TIMIDX+1,1))) MISSNG=MISSNG+1
            IF (ISR4SPV(AMAANC(ANGIDX+1,TIMIDX,1))) MISSNG=MISSNG+1
            IF (ISR4SPV(AMAANC(ANGIDX+1,TIMIDX+1,1))) MISSNG=MISSNG+1
            IF (ISR4SPV(AMAANC(ANGIDX,TIMIDX,2))) MISSNG=MISSNG+1
            IF (ISR4SPV(AMAANC(ANGIDX,TIMIDX+1,2))) MISSNG=MISSNG+1
            IF (ISR4SPV(AMAANC(ANGIDX+1,TIMIDX,2))) MISSNG=MISSNG+1
            IF (ISR4SPV(AMAANC(ANGIDX+1,TIMIDX+1,2))) MISSNG=MISSNG+1
C
            IF (MISSNG.GT.0) THEN
               HIRANC(J,I,1) = R4SPV
               HIRANC(J,I,2) = R4SPV
               DO CHAN = 1, 20
                  BADHIR(J,I,CHAN) = 1
                  HIRDAT(J,I,CHAN) = R4SPV
                  OBSHIR(J,I,CHAN) = R4SPV
               END DO
               GO TO 50
            END IF
               
C
C           GENERATE A HIRS LATITUDE FROM TWO SUBSEQUENT AMSU-A
C           SPOTS ON TWO SUCCESSIVE SCAN LINES.
C
            RTEMP = AA2HIR(4,J,I) * AMAANC(ANGIDX,TIMIDX,1) +
     +              AA2HIR(2,J,I) * AMAANC(ANGIDX,TIMIDX+1,1) +
     +              AA2HIR(3,J,I) * AMAANC(ANGIDX+1,TIMIDX,1) +
     +              AA2HIR(1,J,I) * AMAANC(ANGIDX+1,TIMIDX+1,1)
C
C           STORE THE INTERPOLATED LATITUDE IN THE HIRS ANCILLARY
C           BUFFER.
C
            HIRANC(J,I,1) = RTEMP
C
C           GENERATE A HIRS LONGITUDE FROM TWO SUBSEQUENT AMSU-A
C           SPOTS ON TWO SUCCESSIVE SCAN LINES. SPECIAL CARE
C           MUST BE TAKEN AT THE INTERNATIONAL DATE LINE.
C
C           DETERMINE IF THE INTERNATIONAL DATE LINE IS BEING
C           CROSSED.
C
            MINUS = 0
            PLUS  = 0
            VAL1 = AMAANC(ANGIDX,TIMIDX,2)
            IF (VAL1.LE.-100.0) MINUS=MINUS+1
            IF (VAL1.GE.100.0) PLUS=PLUS+1
            VAL2 = AMAANC(ANGIDX,TIMIDX+1,2)
            IF (VAL2.LE.-100.0) MINUS=MINUS+1
            IF (VAL2.GE.100.0) PLUS=PLUS+1
            VAL3 = AMAANC(ANGIDX+1,TIMIDX,2)
            IF (VAL3.LE.-100.0) MINUS=MINUS+1
            IF (VAL3.GE.100.0) PLUS=PLUS+1
            VAL4 = AMAANC(ANGIDX+1,TIMIDX+1,2)
            IF (VAL4.LE.-100.0) MINUS=MINUS+1
            IF (VAL4.GE.100.0) PLUS=PLUS+1
C
            IF (MINUS.GT.0.AND.PLUS.GT.0) THEN
C
C           SHIFT THE POINTS BY 180 DEGREES.
C
                IF (VAL1.LT.0.0) THEN
                    VAL1 = -1.0 * (180.0 - ABS(VAL1))
                ELSE
                    VAL1 = 180.0 - VAL1
                END IF
                IF (VAL2.LT.0.0) THEN
                    VAL2 = -1.0 * (180.0 - ABS(VAL2))
                ELSE
                    VAL2 = 180.0 - VAL2
                END IF
                IF (VAL3.LT.0.0) THEN
                    VAL3 = -1.0 * (180.0 - ABS(VAL3))
                ELSE
                    VAL3 = 180.0 - VAL3
                END IF
                IF (VAL4.LT.0.0) THEN
                    VAL4 = -1.0 * (180.0 - ABS(VAL4))
                ELSE
                    VAL4 = 180.0 - VAL4
                END IF
            END IF
C
C           INTERPOLATE TO THE HIRS SPOT.
C           

            RTEMP = AA2HIR(4,J,I) * VAL1 +
     +              AA2HIR(2,J,I) * VAL2 +
     +              AA2HIR(3,J,I) * VAL3 +
     +              AA2HIR(1,J,I) * VAL4
C
C           MAKE ANY NECESSARY INTERNATIONAL DATE LINE ADJUSTMENTS.
C
            IF (MINUS.GT.0.AND.PLUS.GT.0) THEN
               IF (RTEMP.GE.0) THEN
                  RTEMP = 180.0 - RTEMP
               ELSE
                  RTEMP = -180.0 - RTEMP
               END IF
            END IF
C
C
C           STORE THE INTERPOLATED LONGITUDE IN THE HIRS
C           ANCILLARY BUFFER.
C
            HIRANC(J,I,2) = RTEMP
C
C           GENERATE THE HIRS SATELLITE ZENITH ANGLE
C          
            HEIGHT = AMAANC(ANGIDX,TIMIDX,14)
            ZENANG = SATZEN(HEIGHT,J,HIRANG,1)
            HIRANC(J,I,5) = ZENANG
C
C           DETERMINE THE ORBIT NODE FOR THE HIRS FOV FROM THE
C           MEAN OF THE ORBIT NODE OF THE FOUR AMSU A SPOTS
C
            COUNT = 0.0
            RTEMP = 0.0
            IF (AMAANC(ANGIDX,TIMIDX,11).GE.1) THEN
               COUNT = COUNT + 1
               RTEMP = RTEMP + AMAANC(ANGIDX,TIMIDX,11)
            END IF 
            IF (AMAANC(ANGIDX,TIMIDX+1,11).GE.1) THEN
               COUNT = COUNT + 1
               RTEMP = RTEMP + AMAANC(ANGIDX,TIMIDX+1,11)
            END IF 
            IF (AMAANC(ANGIDX+1,TIMIDX,11).GE.1) THEN
               COUNT = COUNT + 1
               RTEMP = RTEMP + AMAANC(ANGIDX+1,TIMIDX,11)
            END IF 
            IF (AMAANC(ANGIDX+1,TIMIDX+1,11).GE.1.0) THEN
               COUNT = COUNT + 1
               RTEMP = RTEMP + AMAANC(ANGIDX+1,TIMIDX+1,11)
            END IF 
            IF (COUNT.GT.0) RTEMP = RTEMP / COUNT
C
C           ROUND THE MEAN TO THE NEAREST WHOLE NUMBER.
C           NODE VALUES ARE 1=ASCENDING, 2=DESCENDING.
C
            RTEMP = ANINT(RTEMP)
C
C           STORE THE ORBIT NODE IN THE HIRS OBSERVATION BUFFER.
C
            OBSHIR(J,I,20) = RTEMP
C
C           PUT DEFAULT BRIGHTNESS TEMPERATURES INTO THE HIRS
C           OBSERVATION BUFFER AND THE HIRS DATA BUFFER.
C
            DO CHAN = 1, 19
               OBSHIR(J,I,CHAN) = SETBTS(CHAN)
               HIRDAT(J,I,CHAN) = SETBTS(CHAN)
               BADHIR(J,I,CHAN) = 1 
            END DO
            BADHIR(J,I,20) = 1
C
C           PUT A DEFAULT RADIANCE VALUE INTO THE HIRS
C           DATA BUFFER FOR CHANNEL 20.
C
            HIRDAT(J,I,20) = 60.0
C
   50    CONTINUE
C
C           SET THE BADRET FLAG FOR EACH SPOT AND LINE TO TRUE
C           SO THE HIRS WILL NOT BE USED IN A RETRIEVAL.
C
            BADRET(J,I) = .TRUE.
C           
  100    CONTINUE
      END DO
      
      RETURN
      END
