      SUBROUTINE AB2HIR(AMBDAT,AMBANC,HIRDAT,HIRANC)
      IMPLICIT NONE
C***********************************************************************
C    NAME- AB2HIR
C
C    LANGUAGE- FORTRAN           TYPE- SUBROUTINE     COMPUTER- IBM SP 
C
C    VERSION- 1.0    DATE- 2/17/04  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C
C    FUNCTION-  THIS SUBROUTINE FINDS THE AMSU-B/MHS SPOT AND LINE CLOSEST
C               TO THE CENTER OF EACH HIRS SPOT IN A DATA FRAME AND
C               PUTS THE RAW AND LIMB-CORRECTED AMSU-B/MHS BRIGHTNESS
C               TEMPERATURES FOR THAT AMSU-B/MHS SPOT INTO THE HIRS
C               DATA BUFFER. THE SELECTED AMSU-B/MHS SPOT NUMBER IS STORED
C               IN THE HIRS ANCILLARY BUFFER.
C
C    FILES-  NONE
C
C    CALLING ARGUMENTS-
C    NAME     TYPE   I/O   DESCRIPTION
C    ----     ----   ---   -----------
C    AMBDAT   REAL    I    AMSU-B/MHS DATA BUFFER.
C    AMBANC   REAL    I    AMSU-B/MHS ANCILLARY DATA BUFFER.
C
C    HIRDAT   REAL   I/O   HIRS DATA BUFFER.
C
C    HIRANC   REAL   I/O   HIRS ANCILLARY DATA BUFFER.
C
C
C    COMMON AREA PARAMETERS- 
C
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'NOHIRSCF'
      INCLUDE 'COLLOC'
      INCLUDE 'FRMINF'
      INCLUDE 'SPVAL'
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
C      BFIRST        INT    FIRST LINE IN THE AMSU-B/MHS BUFFER WITH
C                           A VALID SCAN LINE START TIME.
C      BLAST         INT    LAST LINE IN THE AMSU-B/MHS BUFFER WITH
C                           A VALID SCAN LINE START TIME.
C      CHAN          INT    AMSU-B/MHS CHANNEL (1 TO 5)
C      I, J, K       INT    LOOP INDEX.
C      DSTH2B        REAL   DISTANCE IN KILOMETERS BETWEEN A
C                           HIRS SPOT AND AN AMSU-B/MHS SPOT
C      DSTSAV        REAL   SMALLEST DISTANCE BETWEEN A HIRS
C                           SPOT AND AN AMSU-B/MHS SPOT.
C      BSPOT         INT    AMSU-B/MHS SPOT NUMBER OF THE CLOSEST
C                           SPOT
C      BLINE         INT    AMSU-B/MHS LINE NUMBER OF THE CLOSEST
C                           SPOT
C      HSPOT         INT    THE HIRS SPOT TO WHICH THE AMSU-B/MHS
C                           IS BEING COLLOCATED
C      HLINE         INT    THE LINE IN THE HIRS BUFFER TO 
C                           WHICH THE AMSU-B/MHS IS BEING COLLOCATED        
C      HRLAT         REAL   THE LATITUDE OF A HIRS SPOT
C      HRLONG        REAL   THE LONGITUDE OF A HIRS SPOT
C      ABLAT         REAL   THE LATITUDE OF AN AMSU-B/MHS SPOT
C      ABLONG        REAL   THE LONGITUDE OF AN AMSU-B/MHS SPOT
C      HIRAMB        INT    AN ARRAY OF AMSU-B/MHS SPOTS CLOSEST TO
C                           HIRS SPOTS BASED ON SATELLITE ZENITH
C                           ANGLE.
C
C    SUBPROGRAMS CALLED- ARCLEN
C
C    FUNCTIONS USED- NONE
C
C    EXIT STATES- NONE
C**********************************************************************
C 
      REAL     AMBDAT(BNSPOT,BTOTLN,BDATEL), AMBANC(BNSPOT,BTOTLN,BNANC) 
      REAL     HIRDAT(HNSPOT,HTOTLN,HDATEL), HIRANC(HNSPOT,HTOTLN,HNANC)
      
C
      INTEGER  CHAN, I, J, K, BSPOT, BLINE, HSPOT, HLINE, HIRAMB(HNSPOT)
      INTEGER  TIMDIF, LINE, TSTSPT, TSTLIN, SPTSAV, LINSAV, BFIRST, BLAST
      REAL     HRLAT, HRLONG, ABLAT, ABLONG, DSTH2B, DSTSAV
      
      LOGICAL ISR4SPV
      DATA HIRAMB/1, 2, 4, 5, 7, 9, 10, 12, 14, 15, 17, 18, 20, 22, 23,
     +            25, 27, 28, 30, 32, 33, 35, 36, 38, 40, 41, 43, 45,
     +            46, 48, 50, 51, 53, 55, 56, 58, 59, 61, 63, 64, 66,    
     +            67, 69, 71, 73, 74, 76, 77, 79, 81, 82, 84, 86, 87, 89, 90/
C
C 
C   LOCATE THE FIRST AND LAST SCAN LINES WITH VALID SCAN LINE START
C   TIMES IN THE AMSU-B/MHS BUFFERS TO HANDLE INCOMPLETE DATA FRAMES
C   AT THE START AND THE END OF AN ORBIT.
C
      BFIRST = 0
      DO LINE = (BTOTBF-5), BTOTLN
         IF (BFIRST.EQ.0) THEN
            IF (BSCTIM(2,LINE).GT.0) THEN
               BFIRST = LINE
            END IF
         END IF
         IF (BSCTIM(2,LINE).GT.0) THEN
            BLAST = LINE
         END IF
      END DO

C-- LOOP THROUGH THE SCAN LINES IN THE HIRS DATA BUFFER. (LINES 10-47)
C
      DO 300 HLINE = HTOTBF, HTOTLN-1
C
C-- SKIP OVER ANY MISSING SCANS - BASED ON SCAN LINE START TIME.
C
         IF (HSCTIM(1,HLINE).LE.0) GO TO 300
C
C-- INITIALIZE THE SCAN LINE TIME DIFFERENCE TO 6400 MS (THE HIRS SCAN INTERVAL)
C
         TIMDIF = 6400
C
C-- FIND THE AMSU-B SCAN WITH A START TIME CLOSEST TO THE HIRS SCAN LINE
C   START TIME, THEN BACK OFF ONE LINE.
C
         BLINE = 0
         IF (BFIRST.GT.0) THEN
            DO LINE = BFIRST, BLAST
               IF (ABS(HSCTIM(2,HLINE) - BSCTIM(2,LINE)). LE. TIMDIF) THEN
                  TIMDIF = ABS(HSCTIM(2,HLINE) - BSCTIM(2,LINE))
                  BLINE = LINE-1
               END IF
            END DO 
         END IF
C
C-- IF THE TIME DELTA BETWEEN THE HIRS SCAN LINE AND THE CLOSEST AMSU-B
C   SCAN LINE IS GREATER THAN 6.4 SECONDS, THERE IS SOMETHING WRONG IN
C   THE INSTRUMENT-TO-INSTRUMENT SYNCHRONIZATION. PRINT OUT A WARNING
C   AND SKIP TO THE NEXT HIRS SCAN LINE.
C
         IF (BLINE.LT.1) THEN
            PRINT *,' NO AMSU-B/MHS SCAN LINE FOUND WITHIN ',TIMDIF,
     +             ' MILLISECONDS OF HIRS SCAN LINE ',HLINE
            PRINT *,' HIRS SCAN START TIME: ',HSCTIM(2,HLINE)
            IF (BFIRST.GT.0.AND.BLAST.GT.BFIRST) THEN
               PRINT *,' AMSU-B/MHS SCAN START TIME RANGE: ',BSCTIM(2,BFIRST),
     +                 ' FOR SCAN ',BFIRST,' TO ',BSCTIM(2,BLAST),' FOR SCAN ',
     +                   BLAST
            ELSE
               PRINT *,' NO AMSU-B/MHS SCANS FOUND FOR THIS DATA FRAME '
            END IF
            GO TO 300
         END IF
C
C-- LOOP THROUGH THE SPOTS ON THE HIRS SCAN LINE
C
         DO 200 HSPOT = 1, HNSPOT
C
C-- INITIALIZE THE SAVED DISTANCE BETWEEN SPOTS TO AN UNREASONABLE VALUE
C
            DSTSAV = 9999.9
C
C-- SKIP OVER ANY MISSING HIRS SPOTS
C
            IF (ISR4SPV(HIRANC(HSPOT,HLINE,1))) GO TO 200
C
C-- EXTRACT THE HIRS SPOT LATITUDE AND LONGITUDE
C
            HRLAT = HIRANC(HSPOT,HLINE,1)
            HRLONG = HIRANC(HSPOT,HLINE,2)
C
C-- STARTING AT THE AMSU-B SCAN WITH THE START TIME CLOSEST TO THE HIRS
C   SCAN START TIME, SEARCH THROUGH A 3X4 SPOT BOX, CENTERED ON THE 
C   AMSU-B SPOT WHOSE SATELLITE ZENITH ANGLE IS CLOSEST TO THE SATELLITE
C   ZENITH ANGLE OF THE HIRS SPOT, AND FIND THE AMSU-B SPOT CLOSEST TO
C   THE HIRS SPOT.
C
            LINSAV = 0
            SPTSAV = 0
            DO LINE = BLINE, BLINE+4
               IF (LINE.LE.BTOTLN) THEN
                  DO TSTSPT = 1, BNSPOT
                     ABLAT = AMBANC(TSTSPT,LINE,1)
                     ABLONG = AMBANC(TSTSPT,LINE,2)
                     CALL ARCLEN(HRLAT,HRLONG,ABLAT,ABLONG,DSTH2B) 
                     IF (DSTH2B.LT.DSTSAV) THEN
                        DSTSAV = DSTH2B
                        LINSAV = LINE
                        SPTSAV = TSTSPT
                     END IF
                  END DO
               END IF
            END DO
C
C SAVE THE RAW AND LIMB-CORRECTED AMSU-B OR MHS CHANNEL BRIGHTNESS
C TEMPERATURES IN THE HIRS DATA BUFFER. SAVE THE SELECTED AMSU-B
C OR MHS SPOT NUMBER ON THE SCAN LINE IN THE HIRS ANCILLARY BUFFER.
C 
            DO I = 1, 5
               IF (SPTSAV.NE.0.AND.LINSAV.NE.0) THEN
                  HIRDAT(HSPOT,HLINE,162+I) = AMBDAT(SPTSAV,LINSAV,I)
                  HIRDAT(HSPOT,HLINE,167+I) = AMBDAT(SPTSAV,LINSAV,5+I)
                  HIRANC(HSPOT,HLINE,25) = FLOAT(SPTSAV)
               ELSE
                  HIRDAT(HSPOT,HLINE,162+I) = R4SPV
                  HIRDAT(HSPOT,HLINE,167+I) = R4SPV
                  HIRANC(HSPOT,HLINE,25) = R4SPV
               END IF
            END DO
  200    CONTINUE
  300 CONTINUE

      
      RETURN
      END
