      SUBROUTINE AMBLIN(BUTCFS,NUMLIN,CNFDNC,BDATOV,ABNCOV,AMBDAT,
     1                  AMBANC,STATUS)
      IMPLICIT NONE
C***********************************************************************
C    NAME- AMBLIN
C
C    LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE   COMPUTER- IBM SP
C
C    VERSION- 1.0     DATE-  1/15/04  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 1.1     DATE- 11/09/04  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C                     INCLUDED COMMON /RFICOR/ AND ADDED CODE TO PERFORM
C                     RFI CORRECTION IF NEEDED.
C    VERSION- 1.2     DATE- 12/05/04  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C                     FOR WR #230 (NOAA-N READINESS) CHANGED RETCOD TO
C                     STATUS IN THE CALLING ARGUMENTS AND USED RETCOD
C                     AS AN I*4 RETURN CODE FOR 1B* UTILITY CALLS. MADE
C                     NECESSARY CHANGES TO ARGUMENT DECLARATIONS FOR
C                     1B* UTILITY ROUTINE CALLS. INCLUDED COMMON /HIRUSE/
C                     AND ADDED CODE TO READ MHS "EARTH_VIEW_DATA" INSTEAD
C                     OF AMSU-B "OBSERVATIONS" WHEN THE HIRS/4 INSTRUMENT
C                     IS PRESENT.
C
C    FUNCTION-  THIS SUBROUTINE DOES PROCESSING FOR A SCAN LINE OF
C                AMSU-B DATA. AMSU-B COUNTS AND LAT/LON LOCATIONS
C               ARE EXTRACTED FROM THE SCAN LINE BUFFER USING 1B*
C               UTILITIES.  THE CALIBRATION COEFFICIENTS ARE APPLIED TO
C               THE COUNTS TO OBTAIN RADIANCES WHICH ARE CONVERTED
C               TO BRIGHTNESS TEMPERATURES.
C
C               A GROSS LIMITS CHECK IS THEN PERFORMED ON ALL
C               CHANNEL VALUES. THE VALUES PER SCAN LINE ARE PLACED IN
C               THE AMSU-B DATA BUFFER AS ARE THE CORRESPONDING ANCILLARY
C               DATA ELEMENTS IN THE AMSU-B ANCILLARY DATA BUFFER.
C
C    CALLING SEQUENCE PARAMETERS-
C      VARIABLE  TYPE  I/O  DESCRIPTION
C      --------  ----  ---  -----------
C      BUTCFS    REAL   I   2D ARRAY OF AMSU-B CALIBRATION COEFF.
C                           (3 COEFF FOR EACH OF INPARM:BNCHAN
C                           CHANNELS).
C      NUMLIN    INT    I   NUMBER OF LINES WRITTEN TO FRAME BUFFER.
C      CNFDNC    REAL   I   SCAN LINE CONFIDENCE (100 HAVING TOTAL
C                           CONFIDENCE, ZERO HAVING NO CONFIDENCE).
C      BDATOV    REAL   O   3D BUFFER (INSPARM:BNSPOT X INPARM:
C                           BTOTBF X INSPARM:BDATEL) CONTAINING
C                           THE DATA FRAME OVERLAP.
C      ABNCOV    REAL   O   3D BUFFER (INSPARM:BNSPOT X INPARM:
C                           BTOTBF X INSPARM:ANBNC) CONTAINING
C                           THE ANCILLARY DATA OVERLAP.
C      AMBDAT    REAL   O   3D AMSU-B BUFFER (INSPARM:BNSPOT X
C                           INSPARM:BTOTLN X INSPARM:BDATEL)
C                           CONTAINING AMSU-B DATA IN BRIGHTNESS
C                           TEMPERATURES.
C      AMBANC    REAL   O   3D BUFFER(INSPARM:BNSPOT X
C                           INSPARM:BTOTLN X INSPARM:BNANC)
C                           CONTAINING AMSU-B "ANCILLARY" DATA.
C      STATUS    INT    O   ERROR RETURN CODE. (SEE EXIT STATES)
C
C   COMMON AREA PARAMETERS- 
C                          
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'COLLOC'
      INCLUDE 'INPCDB'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPVAL'
      INCLUDE 'GROSSCHK'
      INCLUDE 'AMASTAT'
      INCLUDE 'DEBUG'
      INCLUDE 'RFICOR'
      INCLUDE 'HIRUSE'
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
C      BINSTR        INT    AMSU-B INSTRUMENT NUMBER (=3)
C      BIT           INT    VALUE SPECIFYING WHICH BIT IS TO BE SET.
C      BRTTMP        REAL   ARRAY OF BRIGHTNESS TEMPERATURES (IN K) FOR
C                           EACH SPOT FOR ONE AMSU-B CHANNEL. DIMENSIONED
C                           INSPARM:BNSPOT.
C      CALRAD        REAL   VECTOR OF CALIBRATED RADIANCES FOR ALL
C                           CHANNELS OF AN AMSU-B SPOT. DIMENSIONED
C                           (INSPARM:BNSPOT X INPARM:BNCHAN).
C      CHAN          INT    DO LOOP COUNTER FOR THE NUMBER OF CHANNELS.
C      CHNAVL        REAL   ARRAY OF CHANNEL AVAILABILITY FOR ALL SPOTS
C                           IN AMSU-B SCAN LINE. DIMENSIONED
C                           INSPARM:BNSPOT.
C      COUNT         INT    RAW AMSU-B DATA VALUE FROM THE SCAN LINE
C                           BUFFER. DIMENSIONED INPARM:BNCHAN X
C                           INSPARM:BNSPOT.
C      COUNT2        I*2    I*2 VERSION OF COUNT.
C      HEIGHT        REAL   SATELLITE HEIGHT.
C      INT1VL        I*1    USED FOR EXTRACTING 1 BYTE INTEGER VALUES
C                           FROM THE AMSU-B SCAN LINES.
C      INT2VL        I*2    USED FOR EXTRACTING 2 BYTE INTEGER VALUES
C                           FROM THE AMSU-B SCAN LINES.
C      INT4VL        I*4    USED FOR EXTRACTING 4 BYTE INTEGER VALUES
C                           FROM THE AMSU-B SCAN LINES.
C      ICHNAV        INT    INTEGER EQUIVALENT TO CHNAVL. DIMENSIONED
C                           INSPARM:BNSPOT.
C      I, J          INT    DO LOOP COUNTERS.
C      ITEMID       CH*32   NAME OF THE ITEM THAT IS TO BE EXTRACTED.
C      LENGTH        I*4    DATA LENGTH FOR 1B* UTILITY CALLS.
C      NODE          INT    NODE FROM THE AMSU-B 1B* (1=ASC, 2=DES).
C      REALVL        REAL   LATITUDE AND LONGITUDE VALUES FOR AN AMSU-B
C                           SCAN.
C      REALV4        R*4    USED FOR EXTRACTING LATITUDE AND LONGITUDE
C                           VALUES FROM THE AMSU-B SCAN LINE.
C                           DIMENSIONED 2 X INSPARM:BNSPOT.
C      RETCOD        I*4    RETURN CODE FROM 1B* UTILITIES.
C      SHAPE         I*4    SHAPE FACTOR ARRAY FOR RETRIEVING THE
C                           VALUES FROM THE AMSU-B SCAN LINE USING
C                           1B* UTILITIES. DIMENSIONED 8.
C      SOLAZM        REAL   SOLAR AZIMUTH ANGLE (DEGREES)- RETURNED
C                           FROM CMPSLZ.
C      SOLZEN        REAL   SOLAR ZENITH ANGLE (DEGREES).
C      SPOT          INT    DO LOOP COUNTER FOR THE NUMBER OF SPOTS.
C      SPTCNF        REAL   ARRAY OF SPOT CONFIDENCE VALUES FOR ONE
C                           SCAN LINE.DIMENSIONED INSPARM:BNSPOT.
C      ZENANG        REAL   SATELLITE ZENITH ANGLE.
C
C    SUBPROGRAMS CALLED- CMPSLZ,CNT2RD,ABG1I2,ABG1I4,
C                        ABG1R4,RADCHK,RAD2BT,BTCHEK
C
C    FUNCTIONS USED- SATZEN, IBSET, FLOAT
C
C    EXIT STATES-  RETCOD =  0 - NORMAL COMPLETION, NO ERRORS DETECTED.
C                           -1 - FATAL ERROR RETURNED FROM CALL TO
C                                A 1B* UTILITY.
C
C***********************************************************************
C
      INTEGER   NUMLIN,STATUS
      REAL      CNFDNC,BUTCFS(3,BNCHAN)
      REAL      BDATOV(BNSPOT,BTOTBF,BDATEL),ABNCOV(BNSPOT,BTOTBF,BNANC)
      REAL      AMBDAT(BNSPOT,BTOTLN,BDATEL),AMBANC(BNSPOT,BTOTLN,BNANC)
      REAL      SETRAD(5)
C
      CHARACTER*32 ITEMID
      INTEGER      COUNT(BNCHAN,BNSPOT),II,III,IIII,JJ
      INTEGER      BIT,CHAN,I,SPOT, BADLIN(5), NODE, IRAW, J
      INTEGER      ICHNAV(BNSPOT),BINSTR
      INTEGER*1    INT1VL
      INTEGER*2    INT2VL, COUNT2(BNCHAN,BNSPOT)
      INTEGER*4    INT4VL, SHAPE(0:7), RETCOD, LENGTH
      REAL         HEIGHT,SOLZEN,SOLAZM,SATZEN,ZENANG
      REAL         CALRAD(BNSPOT,BNCHAN),CHNAVL(BNSPOT)
      REAL         BRTTMP(BNSPOT),SPTCNF(BNSPOT)
      REAL         REALVL(2,BNSPOT)
      REAL*4       REALV4(2,BNSPOT)
      EQUIVALENCE(ICHNAV,CHNAVL)
C
      BINSTR = 3
C
C  SET DEFAULT RADIANCE VALUES FOR USE IN REPLACEMENT OF MISSING AMSU-B
C  RADIANCE VALUES.  THEN, AMSU-A LINE QUALITY IS INTIALIZED TO GOOD.
C 
      SETRAD(1) = 0.001
      SETRAD(2) = 0.002
      SETRAD(3) = 0.004
      SETRAD(4) = 0.005
      SETRAD(5) = 0.005
ccft  AMBBAD(NUMLIN) = .FALSE.
      DO II = 1,5
         BADLIN(II) = 0
      END DO
C
C
C  INITIALIZE VARIABLES.  
C
      STATUS = 0
      ICHNAV(1) = 0 
C
C  INITIALIZE ALL ELEMENTS OF THE SHAPE ARRAY TO 0. 
C
      DO I = 1, 8
          SHAPE(I-1) = 0
      END DO
C
C  GET THE SATELLITE HEIGHT.
C
      SHAPE(0) = 1
      SHAPE(1) = 1
      LENGTH = 1
      ITEMID = 'S/C_ALTITUDE                    '
      CALL ABG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
      STATUS = RETCOD
      IF (STATUS .NE. 0) GO TO 900
      HEIGHT = FLOAT(INT2VL)
C
C  GET THE ORBIT NODE AND CONVERT IT FROM 0-1 TO 1-2
C  CONVENTION FOR ASCENDING AND DESCENDING, RESPECTIVELY.
C
      ITEMID = 'ORBIT_NODE                      '
      CALL ABG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
      STATUS = RETCOD
      IF (STATUS .NE. 0) GO TO 900
      NODE = INT1VL + 1
C
C  UNPACK LATITUDE AND LONGITUDE VALUES FOR FIELD OF VIEW AND PLACE
C  IN PARAMETERS BUFFER(S).
C
      ITEMID = 'LAT_LON_DEGREES                 '
      SHAPE(0) = 2
      SHAPE(1) = 2
      SHAPE(2) = BNSPOT
      LENGTH = 2 * BNSPOT
      CALL ABG1R4(ITEMID,REALV4,LENGTH,SHAPE,RETCOD)
      STATUS = RETCOD
      IF (STATUS .NE. 0) GO TO 900
C
C  CONVERT THE LATITUDES AND LONGITUES FROM R*4 TO R*8
C
      DO I = 1, BNSPOT
         REALVL(1,I) = REALV4(1,I)
         REALVL(2,I) = REALV4(2,I)
      END DO
C
C  EXTRACT THE COUNTS FOR THE FIELD OF VIEW FOR EACH CHANNEL.
C  IF THE HIRS/4 INSTRUMENT IS SPECIFIED, ASSUME THAT THE
C  MHS INSTRUMENT IS PRESENT INSTEAD OF THE AMSU-B.
C
      IF (HIRTYP.EQ.3) THEN
         ITEMID = 'OBSERVATIONS                    '
      ELSE IF (HIRTYP.EQ.4) THEN
         ITEMID = 'EARTH_VIEW_DATA                 '
      END IF
      SHAPE(0) = 2
      SHAPE(1) = BNCHAN
      SHAPE(2) = BNSPOT
      LENGTH = (BNCHAN*BNSPOT)
      CALL ABG1I2(ITEMID,COUNT2,LENGTH,SHAPE,RETCOD)
      STATUS = RETCOD
      IF (STATUS .NE. 0) GO TO 900
C
C  CONVERT THE COUNTS FROM I*2 TO I*8.
C
      DO I = 1, BNCHAN
         DO J = 1, BNSPOT
            COUNT(I,J) = COUNT2(I,J)
            IF (COUNT(I,J).LE.0) COUNT(I,J) = COUNT(I,J) + 65536
         END DO
      END DO 
C
C  APPLY RFI CORRECTIONS TO THE CHANNEL COUNTS IF NECESSARY.
C
      IF (CORRFI) THEN
         CALL ADJRAW(COUNT)
      END IF
C
C  INITIALIZE CHANNEL AVAILABILITY FOR ALL CHANNELS.
C
      DO 20 CHAN = 1, BNCHAN
          BIT = 32 - CHAN
          ICHNAV(1) = IBSET(ICHNAV(1),BIT)
   20 CONTINUE
C
      DO 30 SPOT = 1,BNSPOT
          AMBANC(SPOT,NUMLIN,1) = REALVL(1,SPOT)
          AMBANC(SPOT,NUMLIN,2) = REALVL(2,SPOT)
          IF (NUMLIN .GE. BBUFLN) THEN
              ABNCOV(SPOT,NUMLIN-BBUFLN+1,1) = REALVL(1,SPOT)
              ABNCOV(SPOT,NUMLIN-BBUFLN+1,2) = REALVL(2,SPOT)
          ENDIF
C
C  STORE THE SATELLITE HEIGHT IN THE AMSU-B ANCILLARY BUFFER.
C
          AMBANC(SPOT,NUMLIN,14) = HEIGHT
          IF (NUMLIN .GE. BBUFLN) THEN
              ABNCOV(SPOT,NUMLIN-BBUFLN+1,14) = HEIGHT
          ENDIF
C
C  INITIALIZE THE SPOT CONFIDENCE TO LINE CONFIDENCE AND
C  CHANNEL AVAILABILITY FOR ALL SPOTS.
C
          SPTCNF(SPOT) = CNFDNC
          ICHNAV(SPOT) = ICHNAV(1)
C
C  CALL CMPSLZ TO CALCULATE THE SOLAR ZENITH ANGLE FOR THE FIELD OF
C  VIEW AND PLACE IN PARAMETERS BUFFER(S).
C
        CALL CMPSLZ(REALVL(1,SPOT),REALVL(2,SPOT),SPOT,AMBTIM(2),
     1   AMBTIM(4),SCANTI(1),SCANTI(2),SCANTI(3),SOLZEN,SOLAZM)
C
        ZENANG=SATZEN(HEIGHT,SPOT,AMBANG,2)
C
        AMBANC(SPOT,NUMLIN,3) = SOLZEN
        AMBANC(SPOT,NUMLIN,4) = ZENANG
        AMBANC(SPOT,NUMLIN,11) = NODE  
C
        IF (NUMLIN.GE.BBUFLN) ABNCOV(SPOT,NUMLIN-BBUFLN+1,3) = SOLZEN
        IF (NUMLIN.GE.BBUFLN) ABNCOV(SPOT,NUMLIN-BBUFLN+1,4) = ZENANG
C
   30 CONTINUE
C
C  CALL CNT2RD TO COMPUTE THE CALIBRATED RADIANCES FOR A SCAN LINE,
C  THEN SEND THE RADIANCE VALUES TO RAD2BT TO CONVERT THE RADIANCE
C  VALUES TO BRIGHTNESS TEMPERATURES.
C
ccft  DO III = 1,BNCHAN
c        IF(BUTCFS(1,III).EQ.0. .AND. BUTCFS(2,III).EQ.0)THEN
c         IF(AMBOFF(III))THEN
c           AMBBAD(NUMLIN)=.TRUE.
c           BADLIN(III) = 1
c           DO JJ = 1,BNSPOT
c              BADBRET(JJ,NUMLIN) = .TRUE.
c              BADAMB(JJ,NUMLIN,III) = 1
c              CALRAD(JJ,III) = SETRAD(III)
c           END DO
c         ENDIF
c        ENDIF
c     END DO
c
      CALL CT2RDB(COUNT,BUTCFS,BNCHAN,BNSPOT,CALRAD,BADLIN)
C
      DO 90 CHAN = 1,BNCHAN
C
C  IF THIS CHANNEL IS NOT AVAILABLE, DO NOT CHECK OR PROCESS IT.
C
ccft      IF(BCH2CHK(CHAN).NE.1) GO TO 90
C
C  CALL RADCHK TO PERFORM THE GROSS LIMITS CHECK ON RADIANCES.
C
          CALL RDCHKB(CALRAD(1,CHAN),CHAN,BNSPOT,BMINRD(CHAN),
     1         BMAXRD(CHAN),BINSTR,SETRAD,SPTCNF,ICHNAV,
     2         BCONFC(CHAN),NUMLIN)
C
C
C  ALL CHANNEL VALUES ARE CONVERTED TO BRIGHTNESS TEMPERATURES.
C
          CALL RAD2BT(BINSTR,CHAN,BRTTMP,CALRAD(1,CHAN),BNSPOT)
C
C
C  PERFORM GROSS LIMITS CHECK ON ALL CHANNEL VALUES.
C
          CALL BTCHKB(BRTTMP,CHAN,BNSPOT,BMINBT(CHAN),BMAXBT(CHAN),
     1                BCONFC(CHAN),SPTCNF,ICHNAV,NUMLIN)
C
C-- SAVE THE RAW BRIGHTNESS TEMPERATURES IN AMBDAT SLOTS 6-10,
C   BECAUSE THE FIRST FIVE BRIGHTNESS TEMPERATURES GET CHANGED
C   DURING LIMB CORRECTION.
C
          DO 80 SPOT = 1,BNSPOT
              AMBDAT(SPOT,NUMLIN,CHAN) = BRTTMP(SPOT)
              AMBDAT(SPOT,NUMLIN,CHAN+5) = BRTTMP(SPOT)
              IF (NUMLIN .GE. BBUFLN) THEN
                  BDATOV(SPOT,NUMLIN-BBUFLN+1,CHAN) = BRTTMP(SPOT)
                  BDATOV(SPOT,NUMLIN-BBUFLN+1,CHAN+5) = BRTTMP(SPOT)
              ENDIF
   80     CONTINUE
   90 CONTINUE
C
C
C  SAVE THE SPOT CONFIDENCE AND CHANNEL AVAILIBILITY VALUES TO THE
C  AMSU-B ANCILLARY BUFFER.
C
      DO 100  SPOT=1,BNSPOT
          AMBANC(SPOT,NUMLIN,5) = SPTCNF(SPOT)
          AMBANC(SPOT,NUMLIN,6) = CHNAVL(SPOT)
          IF(NUMLIN.GE.BBUFLN) THEN
              ABNCOV(SPOT,NUMLIN - BBUFLN+1,5) = SPTCNF(SPOT)
              ABNCOV(SPOT,NUMLIN - BBUFLN+1,6) = CHNAVL(SPOT)
          END IF
  100 CONTINUE
C
C   PRINT OUT ANY DEBUG PRINT REQUESTED -----------------------------
C
      IF(DBG1BSTR.EQ.1) THEN
C
         PRINT *,'1BSTAR: ======================================='
         PRINT *,'1BSTAR: Debug Print for AMSU-B Line Number ',NUMLIN
C
         IF(ACTIVETYPE(2)) THEN
C
C      PRINT RAW COUNTS
C
            DO IRAW=1,BNCHAN
              WRITE(6,1000) IRAW,(COUNT(IRAW,IIII),IIII=1,BNSPOT)
            ENDDO
         ENDIF
 1000 FORMAT('1BSTAR: AMSU-B Ch. ',I2,' Raw Counts for 90 Spots:',
     1       9(/'1BSTAR: ',10I7))
C
         IF(ACTIVETYPE(4)) THEN
C
C      PRINT CALIBRATION COEFFICIENTS
C
            DO IRAW =1,BNCHAN
               WRITE(6,1100) IRAW,(BUTCFS(IIII,IRAW),IIII=1,3)
            ENDDO
         ENDIF
 1100 FORMAT('1BSTAR: AMSU-B Calibration Coeffs. for Ch. ',I2,
     1       ' = ',3(E15.5,1X))
C
         IF(ACTIVETYPE(6)) THEN
C
C      PRINT RADIANCES 
C
            DO IRAW=1,BNCHAN
              WRITE(6,1200) IRAW,(CALRAD(IIII,IRAW),IIII=1,BNSPOT)
            ENDDO
         ENDIF
 1200 FORMAT('1BSTAR: AMSU-B Ch. ',I2,' Radiances for 90 Spots:',
     1       9(/'1BSTAR: ',10F10.5))
C
         IF(ACTIVETYPE(8)) THEN
C
C      PRINT BRIGHTNESS TEMPERATURES
C
            DO IRAW=1,BNCHAN
              WRITE(6,1300) 
     1           IRAW,(AMBDAT(IIII,NUMLIN,IRAW),IIII=1,BNSPOT)
            ENDDO
         ENDIF
 1300 FORMAT('1BSTAR: AMSU-B Ch. ',I2,' Non-Limb Corrected B-TEMPS for',
     1       ' 90 Spots:',9(/'1BSTAR: ',10F10.5))
C
      ENDIF
C
C   -----------------------------------------------------------------
C
C  RETURN TO THE CALLING ROUTINE.
C
      RETURN
C
C ** ERROR HANDLING
C  SET THE RETURN CODE TO -1.
C  RETURN TO THE CALLING ROUTINE.
C
  900 CONTINUE
      STATUS = -1
      RETURN
C
      END
