      SUBROUTINE RETLNK2(RETANC,RNGRDP,RTOTLN,RNANC,CHNCMB,CLSPTR,LNKPTR,
     1                  NODVAL,HIRDAT,HIRANC,HNSPOT,HTOTLN,HNANC,HDATEL)
      IMPLICIT NONE
C*********************************************************************
C
C   NAME- RETLNK2
C
C   LANGUAGE-    FORTRAN    TYPE- SUBROUTINE    COMPUTER- AIX UNIX
C
C   VERSION- 1.0       DATE- 11/21/91     PROGRAMMER- CARLA NEIGH, SMSRC
C   VERSION- 3.0       DATE- 06/03/96     PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.1       DATE- 06/07/96     PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.2       DATE- 06/17/97     PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.3       DATE- 10/01/98     PROGRAMMER- M.P.FERGUSON (RITTS)
C   VERSION- 3.4       DATE- 02/14/00     PROGRAMMER- M.P.FERGUSON (RITTS)
C   VERSION- 4.0       DATE- 02/27/03     PROGRAMMER- Rickey Rollins (CSC)
C   VERSION- 5.0       DATE- 07/27/13     PROGRAMMER- FRANK TILLEY (IMSG)
C
C   FUNCTION- THIS SUBROUTINE BUILDS THE LINKED LISTS OF GRID POINTS
C             USED IN MAKING RETRIEVALS FOR A RETRIEVAL GRID.  SEPARATE
C             LINKED LISTS ARE CREATED FOR EACH RETRIEVAL PATH AND
C             RADIANCE CLASS. THIS ROUTINE WORKS WITH CLOUD-CLEARED HIRS
C             AND CLW-ADJUSTED AMSU-A BRIGHTNESS TEMPERATURES.
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE
C                ON THE CRAY.
C   VERSION 3.1- CLASS ID IS PULLED READ FROM VARIABLE USING BIT
C                SHIFTING RATHER THAN EQUIVALENCE WITH A 2 BYTE
C                BUFFER.
C   VERSION 3.2- IF A REQUIRED TEMPERATURE IS MISSING, DO NOT INCLUDE
C                THIS GRID POINT IN THE LINKED LIST.  REQUIRED CHANNELS
C                ARE STORED IN THE CHANNEL COMBINATION FLAG.  FOR THIS
C                MODIFICATION, HIRDAT,HNSPOT,HTOTLN, AND HDATEL WERE
C                ADDED TO THE SUBROUTINE CALL.
C   VERSION 3.3- INCREASED DIMENSION OF CHNCMB FROM 2X3 TO 2X4.
C   VERSION 3.4- INCREASED DIMENSION OF CHNCMB FROM 2X4 TO 2X4X2.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C   VERSION 5.0- ADAPTED FROM RETLNK TO USE CLOUD-CLEARED HIRS AND CLW-
C                ADJUSTED AMSU-A BRIGHTNESS TEMPERATURES.
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME   TYPE   I/O   DESCRIPTION
C   ----   ----   ---   -----------
C   RETANC REAL    I    3-D ARRAY (DIMENSIONED RNGRDP X RTOTLN X RNANC)
C                       CONTAINING THE RETRIEVAL GRID ANCILLARY BUFFER.
C   RNGRDP INT     I    NUMBER OF GRID POINTS IN A LINE OF THE RETRIEVAL
C                       ANCILLARY BUFFER.
C   RTOTLN INT     I    NUMBER OF LINES IN THE RETRIEVAL ANCILLARY
C                       BUFFER.
C   RNANC  INT     I    NUMBER OF DATA ELEMENTS PER GRID POINT IN THE
C                       THE RETRIEVAL ANCILLARY BUFFER.
C   CHNCMB INT     I    ARRAY (DIM 2 X 4 X 2) CONTAINING THE BIT FLAGS FOR
C                       THE CHANNELS NEEDED TO MAKE A RETRIEVAL FOR
C                       EACH PATH.
C   CLSPTR INT     O    ARRAY (DIM MAXCL X 3) CONTAINING THE POINTERS
C                       TO THE CLASS LINKED LISTS FOR EACH PATH.
C   LNKPTR INT     O    ARRAY (DIM MPNOD X 3) CONTAINING LINK POINTERS
C                       FOR THE CLASS LINKED LISTS FOR EACH PATH.
C   NODVAL INT     O    ARRAY (DIM MPNOD X 3) CONTAINING THE NODE VALUES
C                       FOR THE CLASS LINKED LISTS FOR EACH PATH; NODE
C                       VALUES ARE POSITIONS OF GRID POINTS WITHIN THE
C                       RETRIEVAL GRID.
C   HIRDAT REAL    I    ARRAY (DIM HNSPOT X HTOTLN X HNANC) CONTAINING
C                       ONE 256 SECOND DATA FRAME OF HIRS SPOTS AND
C                       COLLOCATED INSTRUMENT SPOTS. 
C   HNSPOT INT     I    TOTAL NUMBER OF HIRS SPOTS.
C   HTOTLN INT     I    TOTAL NUMBER OF HIRS LINES.
C   HDATEL INT     I    TOTAL NUMBER OF HIRS DATA ELEMENTS.        
C
C  (NOTE- VALUES MAXCL AND MPNOD ARE FOUND IN THE INCLUDE MEMBER
C   LLPARM)
C
C   LOCAL VARIABLES-
C   NAME   TYPE   DESCRIPTION
C   ----   ----   ------------
C   AVLNOD INT    ARRAY (DIM 3) USED TO UPDATE THE POSITION OF THE
C                 AVAILABLE NODE FOR EACH PATH.
C   BIT    INT    BIT CORRESPONDING TO EACH CHANNEL IN COMBINATION.
C   NODE   INT    DO LOOP COUNTER FOR NODE.
C   CHAN   INT    DO LOOP COUNTER FOR CHANNELS.
C   CLASID INT    CLASS IDENTIFICATION NUMBER FROM RETRIEVAL ANCILLARY
C                 BUFFER.
C   CHNAVL INT    CHANNEL AVAILABILITY BIT FLAGS FROM RETRIEVAL
C                 ANCILLARY BUFFER.
C   CHRSLT INT    ARRAY (DIM 2) CONTAINING RESULTS FROM IAND OPERATION
C                 BETWEEN CHNAVL AND CHNCMB.
C   CLDCND INT    CLOUD-CLEARING CONDITION EXTRACTED FROM THE RETRIEVAL
C                 FLAG.
C   CLSNUM INT    CLASS NUMBER PACKED IN FIRST 2 BYTES OF CLASID.
C   FLAG   INT    HOLDING VARIABLE FOR CHANNEL COMBINATION.
C   GRDPNT INT    DO LOOP COUNTER FOR GRID POINT.
C   GRDPOS INT    GRID POINT POSITION ADDED TO LINKED LIST.
C   HBFLIN INT    HIRS LINE NUMBER CORRESPONDING TO RETRIEVAL GRID POINT.
C   HBFSPT INT    HIRS SPOT NUMBER CORRESPONDING TO RETRIEVAL GRID POINT.
C   HSPTNO INT    HOLDING VARIABLE FOR COMPUTING HBFSPT.
C   LINE   INT    DO LOOP COUNTER FOR LINE.
C   NUMCHN INT    TOTAL NUMBER OF CHANNELS FOR ALL INSTRUMENTS USED IN
C                 MAKING A RETRIEVAL.
C   PATH   INT    DO LOOP COUNTER FOR PATH.
C   PATHID INT    PATH IDENTIFICATION NUMBER BASED ON CLOUD-CLEARING
C                 CONDITION.
C   RCLSID REAL   REAL EQUIVALENT OF CLASID.
C   RCHNAV REAL   REAL EQUIVALENT OF CHNAVL.
C   RETFLG INT    RETRIEVAL FLAG FROM RETRIEVAL ANCILLARY BUFFER.
C
C   COMMON AREA PARAMETERS- LLPARM, INPARM, SPVAL
C
C   SUBPROGRAMS CALLED- CRETSP, INTLML
C
C   FUNCTIONS USED- IAND
C
C   EXIT STATES- NONE
C
C*********************************************************************
C
      INCLUDE 'LLPARM'
      INCLUDE 'INPARM'
C
      INTEGER CLSPTR(MAXCL,3), LNKPTR(MPNOD,3), NODVAL(MPNOD,3)
      INTEGER CHNCMB(2,4,2), RNGRDP, RTOTLN, RNANC
      INTEGER HNSPOT,HTOTLN,HDATEL,HNANC,ILANDSEA
      INTEGER HSPTNO,HBFLIN,HBFSPT,CHAN,BIT,FLAG
      REAL RETANC(RNGRDP,RTOTLN,RNANC)
      REAL HIRDAT(HNSPOT,HTOTLN,HDATEL),HIRANC(HNSPOT,HTOTLN,HNANC)
C
      INTEGER AVLNOD(3), PATH, NODE, CLASID, CLSNUM, RETFLG, CHRSLT(2)
      INTEGER CLDCND, PATHID, GRDPOS, GRDPNT, CHNAVL(2), LINE
      INTEGER NUMCHN, ERSTAT
      REAL RCLSID, RCHNAV(2)
      LOGICAL   ISR4SPV
C
      INCLUDE 'SPVAL'
C
      EQUIVALENCE(RCLSID, CLASID)
      EQUIVALENCE(RCHNAV, CHNAVL)
C
      NUMCHN = HNCHAN + ANCHAN
C
C  INITIALIZE CLASS POINTER ARRAY TO MISSING VALUES.
C
      DO 20 PATH = 1, 3
        DO 10 NODE = 1, MAXCL
          CLSPTR(NODE,PATH) = I4SPV
   10   CONTINUE
   20 CONTINUE
C
C  CREATE THE LINKED LIST STORAGE POOL FOR EACH PATH.
C
      CALL CRETSP(NODVAL(1,1),LNKPTR(1,1),AVLNOD(1))
      CALL CRETSP(NODVAL(1,2),LNKPTR(1,2),AVLNOD(2))
      CALL CRETSP(NODVAL(1,3),LNKPTR(1,3),AVLNOD(3))
C
C  LOOP OVER RETRIEVAL GRID AND LOAD LINKED LISTS FOR EACH CLASS AND
C  PATH.
C
      DO 40 LINE = 1, RTOTLN
        DO 30 GRDPNT = 1, RNGRDP
C
C  EXTRACT CLASS IDENTIFICATION FROM RETRIEVAL ANCILLARY BUFFER.
C
          RCLSID = RETANC(GRDPNT, LINE, 5)
C
C  IF CLASID <= 0, DON'T MAKE A RETRIEVAL FOR THE GRID POINT.
C
          IF (CLASID .LE. 0) GOTO 30
C
C  EXTRACT THE CLASS NUMBER FROM FIRST 2 BYTES OF CLASID.
C
          CLSNUM = ISHFT(CLASID,-16)
C
C  EXTRACT THE CHANNEL AVAILABILITY AND THE RETRIEVAL FLAG FROM THE
C  RETRIEVAL ANCILLARY BUFFER.
C
          RCHNAV(1) = RETANC(GRDPNT, LINE, 2)
          RCHNAV(2) = RETANC(GRDPNT, LINE, 3)
          RETFLG = RETANC(GRDPNT, LINE, 4)
C -- Set the path to CLEAR for Cloud-Cleared retrievals
c         RETFLG = 0.0
C
C  COMPUTE THE CLASSIFICATION PATH FROM THE RETRIEVAL FLAG CLOUD-
C  CLEARING CONDITION.
C
          CLDCND = RETFLG / 16
          IF (CLDCND .LE. 1) THEN
            PATHID = 1
          ELSE
            PATHID = CLDCND
          ENDIF
C
C  CALCULATE HIRS SPOT AND LINE NUMBER TO USE IN ORDER TO CHECK
C  BRIGHTNESS TEMPERATURES.  IF LINE AND SPOT CANNOT BE
C  CALCULATED, I.E. THE HIRS SPOT NUMBER STORED IN RETANC IS INVALID,
C  SKIP THIS GRID POINT.
C
         IF(RETANC(GRDPNT,LINE,1).LE.0.) GO TO 30
         HSPTNO = RETANC(GRDPNT,LINE,1)
         HBFLIN = (HSPTNO/HNSPOT) + 1
         HBFSPT = MOD(HSPTNO,HNSPOT)
         IF(HBFSPT.EQ.0) THEN
            HBFLIN = HBFLIN - 1
            HBFSPT = HNSPOT
         ENDIF
C
C  CHECK THE CHANNEL AVAILABILITY AGAINST THE CHANNEL COMBINATION
C  NEEDED TO MAKE A RETRIEVAL.  IF BITS SET IN THE CHANNEL AVAILABILITY
C  ARE NOT SET IN THE CHANNEL COMBINATION NEEDED, DO NOT MAKE A
C  RETRIEVAL FOR THE GRID POINT.
C
          ILANDSEA = 1
C  TERRAIN TYPE
          IF(HIRANC(HBFSPT,HBFLIN,9).NE.0)ILANDSEA=2         
          CHRSLT(1) = IAND(CHNAVL(1), CHNCMB(1,PATHID,ILANDSEA))
          CHRSLT(2) = IAND(CHNAVL(2), CHNCMB(2,PATHID,ILANDSEA))
C
          IF ((CHRSLT(1).NE.CHNCMB(1,PATHID,ILANDSEA)).OR.
     1     (CHRSLT(2).NE.CHNCMB(2,PATHID,ILANDSEA))) THEN
            RETANC(GRDPNT,LINE,5) = R4SPV
            GOTO 30
          ENDIF
C
C   CHECK BRIGHTNESS TEMPERATURE DATA FOR EVERY 'ON' CHANNEL IN
C   THE CHANNEL COMBINATION FLAG.  IF ANY REQUIRED TEMPERATURE IS
C   MISSING, DO NOT MAKE A RETRIEVAL FOR THIS GRID POINT.
C
         DO 25 CHAN=1,NUMCHN
            IF(CHAN.LE.20) THEN
               FLAG = CHNCMB(1,PATHID,ILANDSEA)
               BIT = 32-CHAN
            ELSE
               FLAG = CHNCMB(2,PATHID,ILANDSEA)
               BIT = 32-CHAN+20
            ENDIF
            
            IF(BTEST(FLAG,BIT)) THEN
C
C   THIS CHANNEL IS 'ON' IN THE CHANNEL COMBINATION, SO ITS TEMPERATURE
C   MUST BE CHECKED.
C
               IF(ISR4SPV(HIRDAT(HBFSPT,HBFLIN,173+CHAN))) GO TO 30
            ENDIF
   25    CONTINUE
C
C  COMPUTE GRID POINT POSITION IN BUFFER.
C
         GRDPOS = ((LINE - 1) * RNGRDP) + (GRDPNT - 1)
C
C  ADD GRID POINT POSITION TO LINKED LIST FOR PATH AND CLASS
C
         CALL INTLML(NODVAL(1,PATHID),LNKPTR(1,PATHID),AVLNOD(PATHID),
     1               CLSPTR(1,PATHID),CLSNUM,GRDPOS,ERSTAT)
         IF (ERSTAT.NE.0) GO TO 50
C
   30   CONTINUE
   40 CONTINUE
C
   50 RETURN
      END
