      SUBROUTINE HIRLIN(AUTCFS,NUMLIN,CNFDNC,HDATOV,HANCOV,HIRDAT,
     1                  HIRANC,RETCOD,OBSHIR,N16HIR)
      IMPLICIT NONE
C***********************************************************************
C    NAME- HIRLIN
C
C    LANGUAGE- FORTRAN 77    TYPE- SUBROUTINE   COMPUTER- CRAY  
C
C    VERSION- 1.0     DATE- 3/01/91  PROGRAMMER- JEFF MANNING (SMSRC)
C    VERSION- 2.0     DATE- 4/13/95  PROGRAMMER- SURYA JANDHYALA(SMSRC)
C     **ATOVS-88**   REPLACED THE HARD CODING OF ARRAY DIMENSIONS WITH
C                    PARAMETERS. REPLACED UNPACKING OF 1B VALUES WITH
C                    CALLS TO 1B* ROUTINES. REPLACED CALL TO CALSCL WITH
C                    CALL TO CNT2RD. REMOVED SOME REDUNDANT CALLING
C                    ARGUMENTS. ADDED A NEW CALLING ARGUMENT CNFDNC AND
C                    REMOVED CALL TO CMPCNF. REMOVED THE CALLING
C                    ARGUMENT, FLAGS AND FLGCOD.
C                    ADDED HCONFC(CHAN) TO END OF CALL TO RADCHK.
C    VERSION- 2.1     DATE- 11/16/95  PROGRAMMER- DIANE BEITZELL(SMSRC)
C     **ATOVS-250**  REMOVED CALLS TO 1B* FILE PERTAINING TO SCAN LINE
C                    TIME.  THIS TIME WILL BE PASSED IN THROUGH ERINFO.
C                    CALLING ARGUMENT FOR RADADJ CHANGED FROM HIRADJ TO
C                    HIRADJ(1,CHAN) TO ACCESS THE FLEMING COEFFS. FOR
C                    EACH CHANNEL.
C    VERSION- 3.0     DATE- 5/09/96  PROGRAMMER- ALEX POZDNYAK (FDC)   
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT AND
C      R*4 WITH REAL.
C    VERSION- 3.1     DATE- 07/07/97  PROGRAMMER- LEAH WILSON (HSTX)
C      RTOVS UPGRADES - ADDED OBSHIR TO GET NODE.
C    VERSION- 3.2     DATE- 05/20/98  PROGRAMMER- LEAH WILSON (RSTX)
C      ADD /GROSSCHK/ TO SPECIFY AVAILABLE CHANNELS FOR BTCHEK AND RADCHK.
C      ADD 1 TO CALL TO SATZEN TO SPECIFY INSTRUMENT (1=HIRS,2=AMSU-A)
C    VERSION- 3.3     DATE- 12/09/98  PROGRAMMER- LEAH WILSON (RAYTHEON)
C      ADD /AZIMUTH/ TO STORE LOCAL AZIMUTH FOR EACH SPOT FOR USE ON HODF.
C    VERSION- 3.4     DATE- 02/05/99  PROGRAMMER- LEAH WILSON (RAYTHEON)
C      REMOVE RADADJ - REPLACED BY IN-LINING.
C    VERSION- 3.5     DATE- 03/29/99  PROGRAMMER- LEAH WILSON (RAYTHEON)
C      ADD /DEBUG/ TO ALLOW 1B*-LEVEL DEBUG PRINTS.
C    VERSION- 3,6     DATE- 05/19/99  PROGRAMMER- BRAIN BARIL (RAYTHEON)
C      ADD CODE TO ASSIGN NODE VALUES FOR HIRS LINES 8, 9, AND 10 SINCE
C      THESE LINES ARE ONLY CALCULATED IN FRAME ONE.
C    VERSION- 3.7     DATE- 07/26/00  PROGRAMMER- M.P.FERGUSON (RITSS)
C      ADD TEST CODE TO ASSIGN HIRS RADIANCES FOR THE PURPOSE OF TESTING
C      A NO-HIRS CAPABILITY FOR ATOVS.
C    VERSION- 3.8     DATE- 08/02/00  PROGRAMMER- M.P.FERGUSON (RITSS)
C      ADD ADDITIONAL CODE TO CHECK ALL CALIBRATION COEFFICIENTS, RA-
C      DIANCES, AND BRIGHTNESS TEMPERATURES AND SET FLAGS TO DISABLE
C      CLOUD DETECTION AND USE OF HIRS IN THE RETRIEVALS (BUT ALLOW
C      PROCESSING OF RETRIEVALS IN A NON-HIRS MODE) FOR THE SPOT IN
C      QUESTION IF ANY OF THE AFOREMENTIONED TESTS FAILS (WR # 105).
C    VERSION- 3.9     DATE- 11/20/00  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      ADDED CODE TO ADJUST THE HIRS ALBEDO BY DIVIDING THE VALUE IN
C      CHANNEL 20 BY THE COSINE OF THE SOLAR ZENITH ANGLE (WR #125).
C    VERSION- 3.10    DATE- 12/04/00  PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C      CHANGED CODE TO USE THE SOLAR ZENITH ANGLE IN RADIANS AT EACH SPOT
C      AND REDUCE THE LATITUDE LIMIT FROM 90 TO 70. (WR #125)
C    VERSION- 3.11    DATE- 02/07/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      ADDED THE N16HIR FLAG TO THE CALLING ARGUMENTS. WHEN THIS FLAG
C      IS '1', THE HIRS BRIGHTNESS TEMPERATURES FOR EACH SCAN LINE ARE
C      SHIFTED TO THE RIGHT BY ONE SPOT, ALL DATA FOR SPOT 1 ARE
C      SET TO MISSING AND THE SPOT CONFIDENCE IS SET TO ZERO. (WR #131).
C      WHEN THIS FLAG IS SET TO 2, ALL DATA, ANCILLARY AND OBSERVED,
C      ARE MOVED TO THE RIGHT BY 1 SPOT AND SPOT 1 IS SET TO MISSING. 
C      (WR #131a)
C    VERSION- 3.12    DATE- 06/15/01  PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C      INITIALIZED THE CHANNEL AVAILABILITY FLAG (ICHANV(1)) TO ZERO FOR
C      EACH NEW FRAME BEFORE THE BITS ARE SET. THIS PREVENTS THE OCCASIONAL
C      MISSING ORBIT DUE TO ERROR 136. (WR#139)
C    VERSION- 3.13    DATE- 06/26/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      ADDED 'IMPLICIT NONE' AND DECLARED PREVIOUSLY UNDECLARED VARIABLES.
C      ADDED A CALL TO EXTRACT ORBIT NODE FROM THE HIRS 1B* AND REMOVED
C      THE 'NODE' ARGUMENT FROM THE CALLING ARGUMENTS TO SUBROUTINE
C      'CMPSLZ'. (WR #164) ALSO ADDED CODE TO ENSURE THE NOAA-16 SPOT
C      1 HIRS BRIGHTNESS TEMPERATURES, SPOT CONFIDENCE, AND CHANNEL
C      AVAILABILITY ARE SET TO MISSING AND ZERO.      
C    VERSION- 3.14    DATE- 09/05/02  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      IN THE 'DO 70' LOOP CODE WAS ADDED TO CHANGE ANY NEGATIVE HIRS
C      CHANNEL 20 VALUES TO ZERO. THIS IS AN EMERGENCY CHANGE.
C    VERSION- 3.15    DATE- 12/02/03  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      CHANGED DECLARATION OF INTEGER VARIABLES USED IN CALLS TO 1B*
C      UTILITIES TO INTEGER*4. CHANGED DECLARATION OF VARIABLES USED
C      TO HOLD 1B* VALUES TO MATCH THE NUMBER OF BYTES INDICATED IN
C      THE UTILITY ROUTINE USED, THEN CONVERTED THEM TO DEFAULT 
C      LENGTH VARIABLES WHERE NECESSARY FOR FOLLOW-ON PROCESSING.
C      CHANGED THE ITEMID 'LOCAL_ZENITH_ANGLE' TO 'RELATIVE_AZIMUTH_ANGLE'
C      TO REFLECT CHANGES TO THE 1B* STRUCTURE FILE.
C    VERSION- 3.16    DATE-  5/03/05  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      ADDED CODE TO EXTRACT SATELLITE ZENITH ANGLE FROM THE 1B* FILE
C      (WR #265).
C      
C  
C    FUNCTION-  THIS SUBROUTINE DOES PROCESSING FOR A SCAN LINE OF
C               HIRS DATA.  HIRS COUNTS AND CALIBRATION COEFFICIENTS ARE
C               EXTRACTED FROM THE SCAN LINE USING 1B* UTILITIES.  THE
C               CALIBRATION IS APPLIED TO THE COUNTS TO OBTAIN RADIANCES
C               WHICH ARE CONVERTED 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 HIRS DATA BUFFER AS ARE THE CORRESPONDING ANCILLARY
C               DATA ELEMENTS IN THE HIRS ANCILLARY DATA BUFFER.
C
C    CALLING SEQUENCE PARAMETERS-
C      VARIABLE  TYPE  I/O  DESCRIPTION
C      --------  ----  ---  -----------
C      AUTCFS    REAL   I   2D ARRAY OF HIRS CALIBRATION COEFF.
C                           (3 COEFF FOR EACH OF INPARM:HNCHAN
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      HDATOV    REAL   O   3D HIRS BUFFER (INSPARM:HNSPOT X
C                           INSPARM:HTOTBF X INSPARM:HDATEL)
C                           CONTAINING THE DATA FRAME OVERLAP.
C      HANCOV    REAL   O   3D HIRS BUFFER (INSPARM:HNSPOT X
C                           INSPARM:HTOTBF X INSPARM:HNANC)
C                           CONTAINING THE ANCILLARY DATA
C                           OVERLAP.
C      HIRDAT    REAL   O   3D HIRS BUFFER (INSPARM:HNSPOT X
C                           INSPARM:HTOTLN X INSPARM:HDATEL)
C                           CONTAINING HIRS DATA IN BRIGHTNESS
C                           TEMPERATURES.
C      HIRANC    REAL   O   3D BUFFER(INSPARM:HNSPOT X
C                           INSPARM:HTOTLN X INSPARM:HNANC)
C                           CONTAINING HIRS "ANCILLARY" DATA.
C      RETCOD    INT    O   ERROR RETURN CODE. (SEE EXIT STATES)
C      OBSHIR    REAL   O   ARRAY (DIMENSIONED HNSPOT X HTOTLN X 20)
C                           CONTAINING THE NODE IN THE 20TH ELEMENT.
C      N16HIR    INT    I   FLAG SET TO 1 IF HIRS BRIGHTNESS TEMPS
C                           ARE TO BE SHIFTED ONE SPOT TO THE RIGHT
C                           TO CORRECT FOR INSTRUMENT MISALIGNMENT.
C                           FLAG SET TO 2 IF ALL DATA ARE TO BE
C                           SHIFTED TO THE RIGHT ONE SPOT. 
C
C   COMMON AREA PARAMETERS- INPARM,INSPARM,INPCDB,COLLOC,ERPARM,ERINFO,SPVAL
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'COLLOC'
      INCLUDE 'INPCDB'
      INCLUDE 'GROSSCHK'
      INCLUDE 'AZIMUTH'
      INCLUDE 'HIRSTAT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPVAL'
      INCLUDE 'DEBUG'
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
C      BADLIN        INT    ARRAY SET TO 1 FOR EACH CHANNEL IN THE LINE
C                           HAS BAD CALIBRATION COEFFICIENTS AND CANNOT
C                           BE USED.
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 HIRS CHANNEL. DIMENSIONED
C                           INSPARM:HNSPOT.
C      CALRAD        REAL   VECTOR OF CALIBRATED RADIANCES FOR ALL
C                           CHANNELS OF A HIRS SPOT. DIMENSIONED
C                           INSPARM:HNSPOT.
C      CHAN          INT    DO LOOP COUNTER FOR THE NUMBER OF CHANNELS.
C      CHNAVL        REAL   ARRAY OF CHANNEL AVAILABILITY FOR ALL SPOTS
C                           IN HIRS SCAN LINE. DIMENSIONED
C                           INSPARM:HNSPOT.
C      COUNTS        INT    RAW HIRS DATA VALUE FROM THE SCAN LINE
C                           BUFFER. DIMENSIONED INSPARM:HNCHAN X
C                           INSPARM:HNSPOT.
C      HEIGHT        REAL   SATELLITE HEIGHT.
C      INT2VL        INT    USED FOR EXTRACTING 2 BYTE INTEGER VALUES
C                           FROM THE HIRS SCAN LINES.
C      ICHNAV        INT    INTEGER EQUIVALENT TO CHNAVL.
C      I,J           INT    DO LOOP COUNTERS.
C      IRAW          INT    HIRS CHANNEL NUMBER FOR PRINTING RAW COUNTS
C      ITEMID       CH*32   NAME OF THE ITEM THAT IS TO BE EXTRACTED
C                           FROM THE HIRS 1B* SCAN LINE.
C      LATLON        REAL   R*8 VERSION OF REALVL.
C      RADSZEN       REAL   SOLAR ZENITH ANGLE IN RADIANS.
C      REALVL        R*4    USED FOR EXTRACTING LATITUDE AND LONGITUDE
C                           VALUES FROM THE HIS SCAN LINE.
C                           DIMESIONED 2 X INSPARM:HNSPOT.
C      SETRAD        REAL   DUMMY RADIANCE VALUES USED AS SUBSTITUTES FOR
C                           CHANNEL RADIANCES WHICH HAVE FAILED THE GROSS
C                           LIMITS CHECKS, SET TO ALLOW PROCESSING TO CON-
C                           TINUE.  SUCH SPOTS ARE MARKED AS HAVING BAD
C                           HIRS DATA AND A NON-HIRS RETRIEVAL IS PROCESSED.
C                           SETRAD CONTAINS RADIANCES FOR EACH OF THE 20
C                           HIRS CHANNELS WHICH WILL PASS THE GROSS LIMITS
C                           CHECKS.
C      SHAPE         INT    SHAPE FACTOR ARRAY FOR RETRIEVING THE
C                           VALUES FROM THE HIRS SCAN LINE.
C                           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:HNSPOT.
C      START         INT    START INDEX FOR LOOPING THROUGH SPOTS. THIS
C                           VALUE IS SET TO 1 UNLESS THE "NOAA-16 HIRS"
C                           FLAG IS SET, IN WHICH CASE THE START VALUE
C                           IS SET TO 2.
C      ZENANG        REAL   SATELLITE ZENITH ANGLE.
C      ZENSAT        REAL   SATELLITE ZENITH ANGLE ARRAY FROM THE 1B*.
C      LOCAZM        REAL   BUFFER FOR HOLDING LOCAL AZIMUTH ANGLE.
C
C    SUBPROGRAMS CALLED- CMPSLZ,CNT2RD,HRG1I2,HRG1I4,HRG1R4,
C                        RADCHK,RAD2BT,BTCHEK,VSUBK,VDIVK
C
C    FUNCTIONS USED- SATZEN, IBSET, FLOAT, COS 
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,N16HIR
      REAL    CNFDNC,AUTCFS(3,HNCHAN),RADSZEN
      REAL    HDATOV(HNSPOT,HTOTBF,HDATEL),HANCOV(HNSPOT,HTOTBF,HNANC)
      REAL    HIRDAT(HNSPOT,HTOTLN,HDATEL),HIRANC(HNSPOT,HTOTLN,HNANC)
      REAL    OBSHIR(HNSPOT,HTOTLN,20),SETRAD(20)
C
      CHARACTER*32 ITEMID
      INTEGER*2 COUNT2(HNCHAN,HNSPOT),INT2VL
      INTEGER   COUNTS(HNCHAN,HNSPOT),HRSPOT,HRCHAN      
      INTEGER   BIT,CHAN,I,J,SPOT,BADLIN(20), START
      INTEGER   ICHNAV(HNSPOT),II,NODE,III,JJ,IRAW,IIII
      INTEGER*4 SHAPE(0:7),LENGTH,STATUS
      INTEGER*1 NODE1
      REAL      HEIGHT,SOLZEN,SOLAZM,SATZEN,ZENANG
      REAL      CALRAD(HNSPOT,HNCHAN),BRTTMP(HNSPOT)
      REAL      CHNAVL(HNSPOT),SPTCNF(HNSPOT)
      REAL      LATLON(2,HNSPOT)
      REAL*4    REALVL(2,HNSPOT),LOCAZM(56),ZENSAT(56)
      EQUIVALENCE(ICHNAV,CHNAVL)
C
C  SET DEFAULT RADIANCE VALUES FOR USE IN REPLACEMENT OF MISSING HIRS RA-
C  DIANCE VALUES.  THEN HIRS LINE QUALITY IS INITIALIZED TO GOOD.
C
      SETRAD(1) = 70.0      
      SETRAD(2) = 57.0
      SETRAD(3) = 54.0
      SETRAD(4) = 53.0
      SETRAD(5) = 59.0
      SETRAD(6) = 68.0
      SETRAD(7) = 70.0
      SETRAD(8) = 80.0
      SETRAD(9) = 30.0
      SETRAD(10) = 75.0                              
      SETRAD(11) = 10.0
      SETRAD(12) = 2.8
      SETRAD(13) = 0.7
      SETRAD(14) = 0.5
      SETRAD(15) = 0.4
      SETRAD(16) = 0.2
      SETRAD(17) = 0.4
      SETRAD(18) = 0.5
      SETRAD(19) = 0.6
      SETRAD(20) = 60.0
      HIRBAD(NUMLIN)=.FALSE.
      DO II = 1,20
         BADLIN(II) = 0
      END DO
C
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
C
C  GET THE SATELLITE HEIGHT.
C
      SHAPE(0) = 1
      SHAPE(1) = 1
      ITEMID = 'S/C_ALTITUDE                    '
      LENGTH = 1
      CALL HRG1I2(ITEMID,INT2VL,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
      HEIGHT = FLOAT(INT2VL)
C
C GET THE ORBIT NODE
C
      ITEMID = 'ORBIT_NODE                      '
      CALL HRG1I1(ITEMID,NODE1,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
C
C CHANGE THE ORBIT NODE NUMBERING CONVENTION FROM 0-1
C TO 1-2 FOR ASCENDING AND DESCENDING, RESPECTIVELY.
C
      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) = HNSPOT
      LENGTH = 2*HNSPOT
      CALL HRG1R4(ITEMID,REALVL,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
C
C  CONVERT THE R*4 EARTH LOCATION DATA TO R*8.
C
      DO I = 1, HNSPOT
         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) = HNCHAN
      SHAPE(2) = HNSPOT
      LENGTH = HNCHAN*HNSPOT
      CALL HRG1I2(ITEMID,COUNT2,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
C
C  CONVERT THE COUNTS FROM INTEGER*2 TO INTEGER
C
      DO HRCHAN = 1, HNCHAN
         DO HRSPOT = 1,HNSPOT
            COUNTS(HRCHAN,HRSPOT) = COUNT2(HRCHAN,HRSPOT)
         END DO
      END DO
C
C  EXTRACT THE RELATIVE AZIMUTH ANGLE 
C
      ITEMID = 'RELATIVE_AZIMUTH_ANGLE          '
      SHAPE(0) = 1
      SHAPE(1) = HNSPOT
      SHAPE(2) = 0      
      LENGTH = HNSPOT
      CALL HRG1R4(ITEMID,LOCAZM,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
C
C  EXTRACT THE SATELLITE ZENITH ANGLE 
C
      ITEMID = 'SATELLITE_ZENITH_ANGLE          '
      SHAPE(0) = 1
      SHAPE(1) = HNSPOT
      SHAPE(2) = 0      
      LENGTH = HNSPOT
      CALL HRG1R4(ITEMID,ZENSAT,LENGTH,SHAPE,STATUS)
      RETCOD = STATUS
      IF (RETCOD .NE. 0) GO TO 900
C   
C-- WR #131a - IF THE N16HIR FLAG IS SET TO INDICATE THAT
C   THE HIRS SPOT 1 EARTH LOCATION IS WHERE AMSU-A
C   EXPECTS HIRS SPOT 2 TO BE, SHIFT ALL HIRS ANCILLARY
C   DATA TO THE RIGHT BY ONE SPOT AND SET SPOT 1 TO
C   MISSING. THIS SHIFT IS NECESSARY BECAUSE OF A
C   MISALIGNMENT OF THE HIRS INSTRUMENT ON NOAA-16.
C
      START = 1
      IF (N16HIR.GT.0) THEN
         IF (N16HIR.EQ.2) THEN
            DO SPOT = HNSPOT, 2, -1
               LATLON(1,SPOT) = LATLON(1,SPOT-1)
               LATLON(2,SPOT) = LATLON(2,SPOT-1)
               LOCAZM(SPOT)   = LOCAZM(SPOT-1)
               ZENSAT(SPOT)   = ZENSAT(SPOT-1)
            END DO
         END IF
         LATLON(1,1) = R4SPV
         LATLON(2,1) = R4SPV
         LOCAZM(1)   = R4SPV
         ZENSAT(1)   = R4SPV
         START = 2
      END IF
                     
C
C  INITIALIZE CHANNEL AVAILABILITY FOR ALL CHANNELS.
C
      ICHNAV(1) =0
      DO 20 CHAN = 1, HNCHAN
          BIT = 32 - CHAN
          ICHNAV(1) = IBSET(ICHNAV(1),BIT)
   20 CONTINUE
C
      DO 30 SPOT = START,HNSPOT
          HIRANC(SPOT,NUMLIN,1) = LATLON(1,SPOT)
          HIRANC(SPOT,NUMLIN,2) = LATLON(2,SPOT)
          LOCLAZM(SPOT,NUMLIN) = LOCAZM(SPOT)
          IF (NUMLIN .GE. HBUFLN) THEN
              HANCOV(SPOT,NUMLIN-HBUFLN+1,1) = LATLON(1,SPOT)
              HANCOV(SPOT,NUMLIN-HBUFLN+1,2) = LATLON(2,SPOT)
          ENDIF
C
C  INITIALIZE THE SPOT CONFIDENCE TO LINE CONFIDENCE AND CHANNEL
C  AVAILABILITY TO ALL CHANNELS.
C
          SPTCNF(SPOT) = CNFDNC
          ICHNAV(SPOT) = ICHNAV(1)
C
C
C  CALL CMPSLZ TO CALCULATE THE SOLAR ZENITH ANGLE FOR THE FIELD OF
C  VIEW AND PLACE IN PARAMETERS BUFFER(S).
C
C
        CALL CMPSLZ(LATLON(1,SPOT),LATLON(2,SPOT),SPOT,HIRTIM(2),
     1       HIRTIM(4),SCANTI(1),SCANTI(2),SCANTI(3),SOLZEN,SOLAZM)
C
C  STORE NODE IN 20TH ELEMENT OF OBSHIR.
C
       OBSHIR(SPOT,NUMLIN,20) = REAL(NODE)
C
C  THIS PREVENTS LINES 8, 9 AND 10 FROM HAVING WHATEVER NODE IS SET FOR
C  FRAME 1 CARRIED THROUGHT FOR THE ENTIRE ORBIT
C
        IF(NUMLIN.EQ.11) THEN
         OBSHIR(SPOT,8,20) =  REAL(NODE)
         OBSHIR(SPOT,9,20) =  REAL(NODE)
         OBSHIR(SPOT,10,20) = REAL(NODE)
        ENDIF

C
ccft    ZENANG=SATZEN(HEIGHT,SPOT,HIRANG,1)
C
        HIRANC(SPOT,NUMLIN,3) = SOLZEN
        HIRANC(SPOT,NUMLIN,4) = SOLAZM
ccft    HIRANC(SPOT,NUMLIN,5) = ZENANG
        HIRANC(SPOT,NUMLIN,5) = ZENSAT(SPOT)
C
        IF (NUMLIN.GE.HBUFLN) HANCOV(SPOT,NUMLIN-HBUFLN+1,3) = SOLZEN
        IF (NUMLIN.GE.HBUFLN) HANCOV(SPOT,NUMLIN-HBUFLN+1,4) = SOLAZM
ccft    IF (NUMLIN.GE.HBUFLN) HANCOV(SPOT,NUMLIN-HBUFLN+1,5) = ZENANG
        IF (NUMLIN.GE.HBUFLN) HANCOV(SPOT,NUMLIN-HBUFLN+1,5) = ZENSAT(SPOT)
C
   30 CONTINUE
C
C  CALL CT2RDH 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,HNCHAN
         IF(AUTCFS(1,III).EQ.0. .AND. AUTCFS(2,III).EQ.0)THEN
            HIRBAD(NUMLIN)=.TRUE. 
            BADLIN(III) = 1
            DO JJ = 1,HNSPOT
               BADRET(JJ,NUMLIN) = .TRUE.
               BADHIR(JJ,NUMLIN,III)=1
               CALRAD(JJ,III) = SETRAD(III)
            END DO
         ENDIF
      END DO
      CALL CT2RDH(COUNTS,AUTCFS,HNCHAN,HNSPOT,CALRAD,BADLIN) 
C
C  CHECK THE RADIANCE OF CHANNEL 1 TO SEE IF THE CORRECTION BIAS
C  NEEDS TO BE APPLIED.
C
      DO 90 CHAN = 1,HNCHAN
C
C  IF THE CHANNEL IS NOT AVAILABLE, DO NOT CHECK IT OR PROCESS IT.
C
ccft      IF(HCH2CHK(CHAN).NE.1) GO TO 90
C
          IF (CHAN.EQ. 1) THEN
              IF(CALRAD(1,1) .GT. HCNTBS) THEN
                  DO 60 J = 1, HNSPOT
                      CALRAD(J,1) = CALRAD(J,1) - HCNTBS
   60             CONTINUE
              ENDIF
          ENDIF
C
C  CALL RADCHK TO PERFORM THE GROSS LIMITS CHECK ON RADIANCES.
C  THE CHECK IS SKIPPED FOR HIRS CHANNEL 20 DUE TO OBSERVED PROBLEMS
C  WITH NEGATIVE ALBEDOS AT NIGHT.
C
          IF (CHAN .NE. 20) THEN
              CALL RDCHKH(CALRAD(1,CHAN),CHAN,HNSPOT,HMINRD(CHAN),
     1             HMAXRD(CHAN),INSTR,SETRAD,SPTCNF,ICHNAV,
     2             HCONFC(CHAN),NUMLIN)
          ENDIF
C
C       APPLY COEFFICIENTS TO ADJUST RADIANCE VECTOR
C       USING RADTMP(I) = (RADTMP(I)-C1) / C2
C
          CALL VSUBK (CALRAD(1,CHAN), HNSPOT, HIRADJ(1,CHAN))
          CALL VDIVK (CALRAD(1,CHAN), HNSPOT, HIRADJ(2,CHAN))
C
          IF (CHAN .NE. 20) THEN
C
C  ALL CHANNEL VALUES BUT CHANNEL 20 ARE CONVERTED TO BRIGHTNESS
C  TEMPERATURES.
C
            CALL RAD2BT(INSTR,CHAN,BRTTMP,CALRAD(1,CHAN),HNSPOT)
C
          ELSE
C
C  IT HAS BEEN OBSERVED IN THE 1B DATA THAT HIRS CHANNEL 20
C  ALBEDOS MAY BE LESS THAN ZERO AT NIGHT.  THESE VALUES ARE CORRECTED
C  TO ZERO.  ALL CHANNEL 20 ALBEDOS ARE PUT INTO THE BRIGHTNESS
C  TEMPERATURES ARRAY FOR CALL TO BTCHEK. THE SOLAR ZENITH ANGLE
C  IS USED TO CORRECT THE ALBEDO.
C
C
C  CHANGED ANGLE FROM DEGREES TO RADIANS AND CORRECTED CODE TO USE
C  SOLAR ZENITH ANGLE AT EACH SPOT AND REDUCED THE LATITUDE LIMIT FROM
C  90 TO 70.
C
              DO 70, SPOT=1,HNSPOT
               RADSZEN = HIRANC(SPOT,NUMLIN,3)*3.1415/180.0
                  IF (HIRANC(SPOT,NUMLIN,3) .GT. 70.0) THEN
                      BRTTMP(SPOT) = calrad(spot,chan)
                  ELSE
                      BRTTMP(SPOT) = CALRAD(SPOT,CHAN)
                      IF (COS(RADSZEN).NE.0.0) THEN
                         BRTTMP(SPOT) = BRTTMP(SPOT) / COS(RADSZEN)
                      END IF
                  ENDIF
               if(brttmp(spot) .gt. 100.0) brttmp(spot)= 100.0
               if(brttmp(spot) .lt. 0.0) brttmp(spot)= 0.0
   70         CONTINUE
          END IF
C   
C-- IF THE HIRS LOCATION CORRECTION FLAG IS SET, SHIFT THE
C   BRIGHTNESS TEMPERATURES ONE SPOT TO THE RIGHT AND SET
C   THE FIRST SPOT BRIGHTNESS TEMPERATURE TO MISSING.
C
          IF (N16HIR.GT.0) THEN
             DO SPOT = HNSPOT, 2, -1
                BRTTMP(SPOT) = BRTTMP(SPOT-1)
             END DO
             BRTTMP(1) = R4SPV
          END IF
C
C  PERFORM GROSS LIMITS CHECK ON ALL CHANNEL VALUES.
C
          CALL BTCHKH(BRTTMP,CHAN,HNSPOT,HMINBT(CHAN),HMAXBT(CHAN),
     1                HCONFC(CHAN),SPTCNF,ICHNAV,NUMLIN)
C
          IF (N16HIR.GT.0) THEN
             BRTTMP(1) = R4SPV
             SPTCNF(1) = 0.0
             ICHNAV(1) = 0
          END IF
          DO 80 SPOT = 1,HNSPOT
              HIRDAT(SPOT,NUMLIN,CHAN) = BRTTMP(SPOT)
              IF (NUMLIN .GE. HBUFLN) THEN
                  HDATOV(SPOT,NUMLIN-HBUFLN+1,CHAN) = BRTTMP(SPOT)
              ENDIF
   80     CONTINUE
C
   90 CONTINUE
C
C  SAVE THE SPOT CONFIDENCE AND CHANNEL AVAILIBILITY VALUES TO THE
C  HIRS ANCILLARY BUFFER.
C
      DO 100  SPOT=1,HNSPOT
        HIRANC(SPOT,NUMLIN,6) = SPTCNF(SPOT)
        HIRANC(SPOT,NUMLIN,7) = CHNAVL(SPOT)
        IF(NUMLIN.GE.HBUFLN) THEN
           HANCOV(SPOT,NUMLIN - HBUFLN+1,6) = SPTCNF(SPOT)
           HANCOV(SPOT,NUMLIN - HBUFLN+1,7) = 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 HIRS Line Number ',NUMLIN
C
         IF(ACTIVETYPE(1)) THEN
C
C      PRINT RAW COUNTS
C
            DO IRAW=1,HNCHAN
              WRITE(6,1000) IRAW,(COUNTS(IRAW,IIII),IIII=1,HNSPOT)
            ENDDO
         ENDIF
 1000 FORMAT('1BSTAR: HIRS Ch. ',I2,' Raw Counts for 56 Spots:',
     1       7(/'1BSTAR: ',8I7))
C
         IF(ACTIVETYPE(3)) THEN
C
C      PRINT CALIBRATION COEFFICIENTS
C
            DO IRAW =1,HNCHAN
               WRITE(6,1100) IRAW,(AUTCFS(IIII,IRAW),IIII=1,3)
            ENDDO
         ENDIF
 1100 FORMAT('1BSTAR: HIRS Calibration Coeffs. for Ch. ',I2,
     1       ' = ',3(E15.5,1X))
C
         IF(ACTIVETYPE(5)) THEN
C
C      PRINT RADIANCES 
C
            DO IRAW=1,HNCHAN
              WRITE(6,1200) IRAW,(CALRAD(IIII,IRAW),IIII=1,HNSPOT)
            ENDDO
         ENDIF
 1200 FORMAT('1BSTAR: HIRS Ch. ',I2,' Radiances for 56 Spots:',
     1       7(/'1BSTAR: ',8F10.5))
C
         IF(ACTIVETYPE(7)) THEN
C
C      PRINT BRIGHTNESS TEMPERATURES
C
            DO IRAW=1,HNCHAN
              WRITE(6,1300) 
     1           IRAW,(HIRDAT(IIII,NUMLIN,IRAW),IIII=1,HNSPOT)
            ENDDO
         ENDIF
 1300 FORMAT('1BSTAR: HIRS Ch. ',I2,' Non-Limb Corrected B-TEMPS for ',
     1       '56 Spots:',7(/'1BSTAR: ',8F10.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
