      SUBROUTINE AMALIN(AUTCFS,NUMLIN,CNFDNC,ADATOV,AANCOV,AMADAT,
     1                  AMAANC,RETCOD)
      IMPLICIT NONE
C***********************************************************************
C    NAME- AMALIN
C
C    LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE   COMPUTER- CRAY
C
C    VERSION- 1.0     DATE- 7/24/95  PROGRAMMER- DAVE MOREL(SMSRC)
C    VERSION- 3.0     DATE- 5/13/96  PROGRAMMER- ALEX POZDNYAK (FDC)
C      FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C      INTEGER AND ALL R*4 DECLARATIONS TO REAL.
C    VERSION- 3.1     DATE- 7/07/97  PROGRAMMER- LEAH WILSON (HSTX)
C      ADD NODE TO CMPSLZ CALL FOR RTOVS UPGRADES.
C    VERSION- 3.2     DATE- 1/30/98  PROGRAMMER- LEAH WILSON (RAYTHEON)
C      ADD NODE TO AMAANC(SPOT,LINE,11) FOR OUTPUT TO AODF.
C    VERSION- 3.3     DATE- 5/20/98  PROGRAMMER- LEAH WILSON (RAYTHEON)
C      ADD /GROSSCHK/ TO SPECIFY CHANNELS FOR BTCHEK AND RADCHK.
C      ADD 2 TO CALL TO SATZEN TO SPECIFY INSTRUMENT (1=HIRS,2=AMSU-A).
C    VERSION- 3.4     DATE- 3/29/99  PROGRAMMER- LEAH WILSON (RAYTHEON)
C      ADD /DEBUG/ TO ALLOW 1B*-LEVEL DEBUG PRINTS.
C    VERSION- 3.5     DATE-12/5/00  PROGRAMMER-  M.P.FERGUSON (RITSS)
C      MODS MADE TO ALLOW SELECTIVE REMOVAL OF AMSU-A CHANNELS FROM
C      PROCESSING VIA DUMMYING OF THESE CHANNELS IN CONJUNCTION WITH
C      MARKING THESE CHANNELS AS BAD AND NOT PUTTING THEM ONTO THE
C      RODF, AODF, AND HODF.  COMMON AMASTAT ADDED, AND SUBROUTINE
C      CALLS TO RDCHKA, BTCHKA, AND CT2RDA REPLACE CALLS TO RADCHK,
C      BTCHEK, AND CNT2RD, WHICH WERE MODIFIED SLIGHTLY TO PRODUCE
C      THE FORMER ROUTINES (PR # 28).
C    VERSION- 3.6     DATE-01/16/01 PROGRAMMER-  FRANK TILLEY (RAYTHEON)
C      STORED THE SATELLITE HEIGHT IN ELEMENT 14 OF THE AMSU-A
C      ANCILLARY BUFFER FOR USE IN COMPUTING A LOCAL ZENITH ANGLE
C      FOR THE HIRS INSTRUMENT WHEN NO HIRS 1B* IS AVAILABLE (WR #100).
C    VERSION- 3.7     DATE-06/26/01 PROGRAMMER-  FRANK TILLEY (RAYTHEON)
C      EXTRACTED ORBIT NODE FROM THE AMSU-A 1B* AND REMOVED NODE FROM
C      THE ARGUMENTS FOR SUBROUTINE CMPSLZ. (WR #164)
C    VERSION- 3.8     DATE-11/30/04 PROGRAMMER-  FRANK TILLEY (RAYTHEON)
C      UNDER WR *230 (NOAA-N READINESS), DECLARED 'SHAPE' AS I*4.
C      SUBSTITUTED I*4 'LENGTH' FOR DATA LENGTH IN 1B* UTILITY CALLS.
C      CHANGED 'RETCOD' TO I*4 'STATUS' IN 1B* UTILITY CALLS. ADDED
C      ARRAY 'LATLON' TO CONVERT R*4 EARTH LOCATION TO DEFAULT REAL.            
C
C    FUNCTION-  THIS SUBROUTINE DOES PROCESSING FOR A SCAN LINE OF
C                AMSU/A DATA. AMSU/A COUNTS AND LAT/LON LOCATIONS
C               ARE EXTRACTED FROM THE SCAN LINE BUFFER USING 1B*
C               UTILITIES.  THE CALIBRATION COEFFS. 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/A DATA BUFFER AS ARE THE CORRESPONDING ANCILLARY
C               DATA ELEMENTS IN THE AMSU/A ANCILLARY DATA BUFFER.
C
C    CALLING SEQUENCE PARAMETERS-
C      VARIABLE  TYPE  I/O  DESCRIPTION
C      --------  ----  ---  -----------
C      AUTCFS    REAL   I   2D ARRAY OF AMSU/A CALIBRATION COEFF.
C                           (3 COEFF FOR EACH OF INPARM:ANCHAN
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      ADATOV    REAL   O   3D BUFFER (INSPARM:ANSPOT X INPARM:
C                           ATOTBF X INSPARM:ADATEL) CONTAINING
C                           THE DATA FRAME OVERLAP.
C      AANCOV    REAL   O   3D BUFFER (INSPARM:ANSPOT X INPARM:
C                           ATOTBF X INSPARM:ANANC) CONTAINING
C                           THE ANCILLARY DATA OVERLAP.
C      AMADAT    REAL   O   3D AMSU/A BUFFER (INSPARM:ANSPOT X
C                           INSPARM:ATOTLN X INSPARM:ADATEL)
C                           CONTAINING AMSU/A DATA IN BRIGHTNESS
C                           TEMPERATURES.
C      AMAANC    REAL   O   3D BUFFER(INSPARM:ANSPOT X
C                           INSPARM:ATOTLN X INSPARM:ANANC)
C                           CONTAINING AMSU/A "ANCILLARY" DATA.
C      RETCOD    INT    O   ERROR RETURN CODE. (SEE EXIT STATES)
C
C   COMMON AREA PARAMETERS- INPARM,INSPARM,INPCDB,COLLOC,SPVAL
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'COLLOC'
      INCLUDE 'INPCDB'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPVAL'
      INCLUDE 'GROSSCHK'
      INCLUDE 'AMASTAT'
      INCLUDE 'DEBUG'
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
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/A CHANNEL. DIMENSIONED
C                           INSPARM:ANSPOT.
C      CALRAD        REAL   VECTOR OF CALIBRATED RADIANCES FOR ALL
C                           CHANNELS OF AN AMSU/A SPOT. DIMENSIONED
C                           (INSPARM:ANSPOT X INPARM:ANCHAN).
C      CHAN          INT    DO LOOP COUNTER FOR THE NUMBER OF CHANNELS.
C      CHNAVL        REAL   ARRAY OF CHANNEL AVAILABILITY FOR ALL SPOTS
C                           IN AMSU/A SCAN LINE. DIMENSIONED
C                           INSPARM:ANSPOT.
C      COUNTS        I*2    RAW AMSU/A DATA VALUE FROM THE SCAN LINE
C                           BUFFER. DIMENSIONED INPARM:ANCHAN X
C                           INSPARM:ANSPOT.
C      COUNT         INT    COUNTS CONVERTED FROM I*2
C      HEIGHT        REAL   SATELLITE HEIGHT.
C      INT2VL        I*2    USED FOR EXTRACTING 2 BYTE INTEGER VALUES
C                           FROM THE AMSU/A SCAN LINES.
C      ICHNAV        INT    INTEGER EQUIVALENT TO CHNAVL. DIMENSIONED
C                           INSPARM:ANSPOT.
C      I             INT    DO LOOP COUNTERS.
C      ITEMID       CH*32   NAME OF THE ITEM THAT IS TO BE EXTRACTED.
C      LATLON        REAL   REAL*8 VERSION OF REALVL, USED IN CALLS TO
C                           CMPSLZ.
C      LENGTH        I*4    DATA LENGTH FOR 1B* UTILITY CALLS.
C      NODE          INT    ORBIT NODE FROM 1B* (1=ASC, 2=DES).
C      NODE1         I*1    NODE VALUE FROM 1B*
C      REALVL        R*4    USED FOR EXTRACTING LATITUDE AND LONGITUDE
C                           VALUES FROM THE AMSU-A SCAN LINE.
C                           DIMENSIONED 2 X INSPARM:ANSPOT.
C      SHAPE         I*4    SHAPE FACTOR ARRAY FOR RETRIEVING THE
C                           VALUES FROM THE AMSU/A 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:ANSPOT.
C      STATUS        I*4    RETURN CODE FROM 1B* UTILITIES.
C      ZENANG        REAL   SATELLITE ZENITH ANGLE.
C
C    SUBPROGRAMS CALLED- CMPSLZ,CNT2RD,AAG1I2,AAG1I4,
C                        AAG1R4,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,RETCOD
      REAL      CNFDNC
      REAL      AUTCFS(3,ANCHAN)
      REAL      ADATOV(ANSPOT,ATOTBF,ADATEL),AANCOV(ANSPOT,ATOTBF,ANANC)
      REAL      AMADAT(ANSPOT,ATOTLN,ADATEL),AMAANC(ANSPOT,ATOTLN,ANANC)
      REAL      SETRAD(15)
C
      CHARACTER*32 ITEMID
      INTEGER      COUNT(ANCHAN,ANSPOT)
      INTEGER      BIT,CHAN,I,SPOT, BADLIN(15)
      INTEGER      ICHNAV(ANSPOT),NODE,II,III,IIII,JJ,IRAW
      INTEGER*4    SHAPE(0:7),LENGTH,STATUS
      INTEGER*2    COUNTS(ANCHAN,ANSPOT),INT2VL
      INTEGER*1    NODE1 
      REAL         HEIGHT,SOLZEN,SOLAZM,SATZEN,ZENANG
      REAL         CALRAD(ANSPOT,ANCHAN),CHNAVL(ANSPOT)
      REAL         BRTTMP(ANSPOT),SPTCNF(ANSPOT)
      REAL         LATLON(2,ANSPOT)
      REAL*4       REALVL(2,ANSPOT)
      EQUIVALENCE(ICHNAV,CHNAVL)
C
C  SET DEFAULT RADIANCE VALUES FOR USE IN REPLACEMENT OF MISSING AMSU-A
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
      SETRAD(6) = 0.006
      SETRAD(7) = 0.006
      SETRAD(8) = 0.006
      SETRAD(9) = 0.006
      SETRAD(10) = 0.006
      SETRAD(11) = 0.006
      SETRAD(12) = 0.006
      SETRAD(13) = 0.006
      SETRAD(14) = 0.01
      SETRAD(15) = 0.01
      AMABAD(NUMLIN) = .FALSE.
      DO II = 1,15
         BADLIN(II) = 0
      END DO
C
C
C  INITIALIZE VARIABLES.
C
      RETCOD = 0
      ICHNAV(1) = 0
C
C  INITIALIZE ALL ELEMENTS OF THE SHAPE ARRAY TO 0. RETURN CODE TO 0.
C
      DO 10 I = 1, 8
          SHAPE(I-1) = 0
   10 CONTINUE
      RETCOD = 0
      ICHNAV(1) = 0
C
C  GET THE SATELLITE HEIGHT.
C
      SHAPE(0) = 1
      SHAPE(1) = 1
      ITEMID = 'S/C_ALTITUDE                    '
      LENGTH = 1
      CALL AAG1I2(ITEMID,INT2VL,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
      HEIGHT = FLOAT(INT2VL)
C
C  GET THE ORBIT NODE AND CONVERT IT FROM 0-1 TO
C  1-2 NUMBERING CONVENTION FOR ASCENDING/DESCENDING
C
      ITEMID = 'ORBIT_NODE                      '
      CALL AAG1I1(ITEMID,NODE1,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
      NODE = NODE1+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) = 30
      LENGTH = 60
      CALL AAG1R4(ITEMID,REALVL,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
C
C  CONVERT LATITUDE AND LONGITUDE FROM R*4 TO R*8
C
      DO I = 1, ANSPOT
         LATLON(1,I) = REALVL(1,I)
         LATLON(2,I) = REALVL(2,I)
      END DO 
C
C  EXTRACT THE COUNTS FOR THE FIELD OF VIEW FOR EACH CHANNEL.
C
      ITEMID = 'OBSERVATIONS                    '
      SHAPE(0) = 2
      SHAPE(1) = 15
      SHAPE(2) = 30
      LENGTH = 450
      CALL AAG1I2(ITEMID,COUNTS,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
      DO CHAN = 1,15
         DO I = 1,30
            COUNT (CHAN,I) = COUNTS(CHAN,I)
         END DO
      END DO  
C
C  INITIALIZE CHANNEL AVAILABILITY FOR ALL CHANNELS.
C
      DO 20 CHAN = 1, ANCHAN
          BIT = 32 - CHAN
          ICHNAV(1) = IBSET(ICHNAV(1),BIT)
   20 CONTINUE
C
      DO 30 SPOT = 1,ANSPOT
          AMAANC(SPOT,NUMLIN,1) = LATLON(1,SPOT)
          AMAANC(SPOT,NUMLIN,2) = LATLON(2,SPOT)
          IF (NUMLIN .GE. ABUFLN) THEN
              AANCOV(SPOT,NUMLIN-ABUFLN+1,1) = LATLON(1,SPOT)
              AANCOV(SPOT,NUMLIN-ABUFLN+1,2) = LATLON(2,SPOT)
          ENDIF
C
C  STORE THE SATELLITE HEIGHT IN THE AMSU-A ANCILLARY BUFFER.
C
          AMAANC(SPOT,NUMLIN,14) = HEIGHT
          IF (NUMLIN .GE. ABUFLN) THEN
              AANCOV(SPOT,NUMLIN-ABUFLN+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(LATLON(1,SPOT),LATLON(2,SPOT),SPOT,AMATIM(2),
     1   AMATIM(4),SCANTI(1),SCANTI(2),SCANTI(3),SOLZEN,SOLAZM)
C
        ZENANG=SATZEN(HEIGHT,SPOT,AMAANG,2)
C
        AMAANC(SPOT,NUMLIN,3) = SOLZEN
        AMAANC(SPOT,NUMLIN,4) = ZENANG
        AMAANC(SPOT,NUMLIN,11) = NODE
C
        IF (NUMLIN.GE.ABUFLN) AANCOV(SPOT,NUMLIN-ABUFLN+1,3) = SOLZEN
        IF (NUMLIN.GE.ABUFLN) AANCOV(SPOT,NUMLIN-ABUFLN+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
      DO III = 1,ANCHAN
         IF(AUTCFS(1,III).EQ.0. .AND. AUTCFS(2,III).EQ.0)THEN
          IF(AMAOFF(III))THEN
            AMABAD(NUMLIN)=.TRUE.
            BADLIN(III) = 1
            DO JJ = 1,ANSPOT
               BADARET(JJ,NUMLIN) = .TRUE.
               BADAMA(JJ,NUMLIN,III) = 1
               CALRAD(JJ,III) = SETRAD(III)
            END DO
          ENDIF
         ENDIF
      END DO
      CALL CT2RDA(COUNT,AUTCFS,ANCHAN,ANSPOT,CALRAD,BADLIN)
C
      DO 90 CHAN = 1,ANCHAN
C
C  IF THIS CHANNEL IS NOT AVAILABLE, DO NOT CHECK OR PROCESS IT.
C
          IF(ACH2CHK(CHAN).NE.1) GO TO 90
C
C  CALL RADCHK TO PERFORM THE GROSS LIMITS CHECK ON RADIANCES.
C
          CALL RDCHKA(CALRAD(1,CHAN),CHAN,ANSPOT,AMINRD(CHAN),
     1         AMAXRD(CHAN),INSTR,SETRAD,SPTCNF,ICHNAV,ACONFC(CHAN),
     2         NUMLIN)
C
C
C  ALL CHANNEL VALUES ARE CONVERTED TO BRIGHTNESS TEMPERATURES.
C
          CALL RAD2BT(INSTR,CHAN,BRTTMP,CALRAD(1,CHAN),ANSPOT)
C
C
C  PERFORM GROSS LIMITS CHECK ON ALL CHANNEL VALUES.
C
          CALL BTCHKA(BRTTMP,CHAN,ANSPOT,AMINBT(CHAN),AMAXBT(CHAN),
     1                ACONFC(CHAN),SPTCNF,ICHNAV,NUMLIN)
C
          DO 80 SPOT = 1,ANSPOT
              AMADAT(SPOT,NUMLIN,CHAN) = BRTTMP(SPOT)
              IF (NUMLIN .GE. ABUFLN) THEN
                  ADATOV(SPOT,NUMLIN-ABUFLN+1,CHAN) = BRTTMP(SPOT)
              ENDIF
   80     CONTINUE
   90 CONTINUE
C
C
C  SAVE THE SPOT CONFIDENCE AND CHANNEL AVAILIBILITY VALUES TO THE
C  AMSU/A ANCILLARY BUFFER.
C
      DO 100  SPOT=1,ANSPOT
          AMAANC(SPOT,NUMLIN,5) = SPTCNF(SPOT)
          AMAANC(SPOT,NUMLIN,6) = CHNAVL(SPOT)
          IF(NUMLIN.GE.ABUFLN) THEN
              AANCOV(SPOT,NUMLIN - ABUFLN+1,5) = SPTCNF(SPOT)
              AANCOV(SPOT,NUMLIN - ABUFLN+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-A Line Number ',NUMLIN
C
         IF(ACTIVETYPE(2)) THEN
C
C      PRINT RAW COUNTS
C
            DO IRAW=1,ANCHAN
              WRITE(6,1000) IRAW,(COUNT(IRAW,IIII),IIII=1,ANSPOT)
            ENDDO
         ENDIF
 1000 FORMAT('1BSTAR: AMSU-A Ch. ',I2,' Raw Counts for 30 Spots:',
     1       3(/'1BSTAR: ',10I7))
C
         IF(ACTIVETYPE(4)) THEN
C
C      PRINT CALIBRATION COEFFICIENTS
C
            DO IRAW =1,ANCHAN
               WRITE(6,1100) IRAW,(AUTCFS(IIII,IRAW),IIII=1,3)
            ENDDO
         ENDIF
 1100 FORMAT('1BSTAR: AMSU-A Calibration Coeffs. for Ch. ',I2,
     1       ' = ',3(E15.5,1X))
C
         IF(ACTIVETYPE(6)) THEN
C
C      PRINT RADIANCES
C
            DO IRAW=1,ANCHAN
              WRITE(6,1200) IRAW,(CALRAD(IIII,IRAW),IIII=1,ANSPOT)
            ENDDO
         ENDIF
 1200 FORMAT('1BSTAR: AMSU-A Ch. ',I2,' Radiances for 30 Spots:',
     1       3(/'1BSTAR: ',10F10.5))
C
         IF(ACTIVETYPE(8)) THEN
C
C      PRINT BRIGHTNESS TEMPERATURES
C
            DO IRAW=1,ANCHAN
              WRITE(6,1300)
     1           IRAW,(AMADAT(IIII,NUMLIN,IRAW),IIII=1,ANSPOT)
            ENDDO
         ENDIF
 1300 FORMAT('1BSTAR: AMSU-A Ch. ',I2,' Non-Limb Corrected B-TEMPS ',
     1       'for 30 Spots:',3(/'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
      RETCOD = -1
      RETURN
C
      END
