      SUBROUTINE RETDVR2(CHNCMB,CLDCHN,ULCHIR,CLRHGT,LIMHGT,LIMHG2,
     +                  SIMCHN,NRTHLM,STHLIM,ERSTAT)
      IMPLICIT NONE
C
C*********************************************************************
C
C   NAME- RETDVR2
C
C   LANGUAGE- VS FORTRAN    TYPE- SUBROUTINE    COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 11/21/91     PROGRAMMER- CARLA NEIGH, SMSRC
C   VERSION- 2.0    DATE- 05/05/94   PROGRAMMER- FRANK TILLEY (SMSRC)
C                   XATOV-1 REPLACE COMMON BLOCK /RETGRD/ WITH PARAMETER
C                   FILE RTVPARM AND COMMON BLOCK /RTVGRD/.  REPLACE
C                   COMMON BLOCK /BUFFER/ WITH PARAMETER FILE INSPARM
C                   AND COMMON BLOCK /INSCOM/.
C   VERSION- 2.1    DATE- 07/13/94   PROGRAMMER- ALEX POZDNYAK (SMSRC)
C                   SATOV-17 LOCAL VARIABLE RGUESS CHANGED TO PGUESS AND
C                   HAS DIMENSION NUMCHN + NANCIL. ANCILLARY ELEMENTS
C                   FOR PREDVC WERE CHANGED TO: LATITUDE, LONGITUDE, AND
C                   SOLAR ZENITH ANGLE. THE LAST 3 ELEMENTS OF PREDVC
C                   WERE SET TO ZERO. ARGUMENTS NUMCHN, NANCIL, AND
C                   NUMLVL WERE REMOVED FROM MAKRET CALL.
C   VERSION- 2.2    DATE- 08/11/94   PROGRAMMER- SURYA JANDHYALA (SMSRC)
C                   XATOV-25 REPLACED A CONSTANT IN THE ARGUMENT OF THE
C                   ARRAY RETANC WITH PARAMETERS.
C   VERSION- 2.3    DATE- 09/26/95   PROGRAMMER- ALEX POZDNYAK (SMSRC)
C                   ATOVS- 186: WHERE THE VARIABLE AVNSTR IS ASSIGNED,
C                   THE N* FLAG (ELEMENT 24 OF HIRANC BUFFER) IS EXAMINE
C                   IF THE N* FLAG IS MISSING, AVNSTR IS SET TO MISSING.
C                   IF THE N* FLAG INDICATES AVHRR N* WAS USED, AVNSTR I
C                   SET TO ELEMENT 17 OF THE HIRANC BUFFER. IF THE N*
C                   FLAG INDICATES THAT IR/MW WAS USED, AVNSTR IS SET TO
C                   ELEMENT 21 OF THE HIRS ANCILLARY BUFFER.
C   VERSION- 2.4    DATE- 03/06/96   PROGRAMMER- DAVE MOREL (SMSRC)
C                   ATOVS- 353: CHANGED THE HIRANC ELEMENT FROM 8 TO 9
C                   FOR EXTRACTION OF TERRAIN TYPE.
C   VERSION- 3.0    DATE- 06/03/96   PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.1    DATE- 01/14/97   PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.2    DATE- 05/02/97   PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.3    DATE- 06/17/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.4    DATE- 10/07/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.5    DATE- 04/15/98   PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 3.6    DATE- 05/07/98   PROGRAMMERS-B. GOCKEL &
C                                                L. WILSON  (RSTX)
C   VERSION- 3.7    DATE- 06/24/98   PROGRAMMER- M. FERGUSON (RSTX)
C   VERSION- 3.8    DATE- 07/28/98   PROGRAMMERS-B. GOCKEL &
C                                                M. FERGUSON (RSTX)
C   VERSION- 3.9    DATE- 10/01/98   PROGRAMMER- M.P.FERGUSON(RITTS)
C   VERSION- 3.11   DATE- 01/27/99   PROGRAMMER- M.P.FERGUSON(RITTS)
C   VERSION- 3.12   DATE- 05/10/99   PROGRAMMER- M.P.FERGUSON(RITSS)
C   VERSION- 3.13   DATE- 06/07/99   PROGRAMMER- B.GOCKEL (RITSS)
C   VERSION- 3.14   DATE- 06/11/99   PROGRAMMER- B.GOCKEL(RITSS)
C   VERSION- 3.15   DATE- 06/15/99   PROGRAMMER- M.FERGUSON(RITSS)
C   VERSION- 3.16   DATE- 08/20/99   PROGRAMMER- M.FERGUSON(RITSS)
C   VERSION- 3.17   DATE- 08/30/99   PROGRAMMER- M.FERGUSON(RITSS)
C   VERSION- 3.18   DATE- 09/10/99   PROGRAMMER- M.FERGUSON(RITSS)
C   VERSION- 3.19   DATE- 02/14/00   PROGRAMMER- M.FERGUSON(RITSS)
C   VERSION- 3.20   DATE- 03/16/00   PROGRAMMER- LEAH WILSON (RAYTHEON)
C   VERSION- 3.21   DATE- 05/03/00   PROGRAMMER- M.P.FERGUSON(RITSS)
C   VERSION- 3.22   DATE- 06/15/00   PROGRAMMER- M.P.FERGUSON(RITSS)
C   VERSION- 3.23   DATE- 07/07/00   PROGRAMMER- M.P.FERGUSON(RITSS)
C   VERSION- 3.24   DATE- 07/21/00   PROGRAMMER- M.P.FERGUSON(RITSS)
C   VERSION- 3.25   DATE- 11/01/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 3.26   DATE- 03/05/01   PROGRAMMER- M.P.FERGUSON(RITSS)
C   VERSION- 3.27   DATE- 05/03/01   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 3.28   DATE- 12/17/01   PROGRAMMER- M.P.FERGUSON(RITTS)
C   VERSION- 3.29   DATE- 06/05/02   PROGRAMMER- RICH KELLEY (CSC) 
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C   VERSION- 4.1    DATE- 09/04/03   PROGRAMMER- Rickey Rollins (CSC)
C   VERSION- 4.2    DATE- 03/21/07   PROGRAMMER- FRANK TILLEY (IMSG)
C   VERSION- 5.0    DATE- 07/22/13   PROGRAMMER- FRANK TILLEY (IMSG)
C
C
C   FUNCTION- THIS SUBROUTINE IS THE DRIVER ROUTINE FOR PERFORMING
C             RE-RETRIEVALS FOR A DATA FRAME.  THE RADIANCE PATH AND CLASS
C             FOR EACH RETRIEVAL GRID POINT ARE DETERMINED.  THE
C             POSITION OF EACH GRID POINT IS THEN ADDED TO A LINKED LIST
C             SO THAT GRID POINTS FOR EACH UNIQUE COMBINATION OF CLASS
C             AND PATH ARE LINKED TOGETHER.  THIS IS DONE SO THAT DATA
C             DEPENDENT ON CLASS AND PATH NEEDED FROM THE CDB AND OPCDB
C             TO COMPUTE A RETRIEVAL ARE ONLY ACCESSED ONCE.  THE 
C             CHANNEL MEASUREMENT UNCERTAINTIES FOR THE CLASS
C             AND PATH ARE OBTAINED FROM THE CDB,   AND THE
C             RETRIEVAL OPERATOR COMPONENTS FOR THE CLASS AND PATH
C             ARE OBTAINED FROM THE OPCDB.  THE FIRST GUESS VALUES ARE
C             OBTAINED USING A LIBRARY SEARCH, THEN THE RETRIEVAL
C             IS COMPUTED FOR EACH GRID POINT IN THE LINKED LIST FOR
C             THE CLASS AND PATH.  THE RETRIEVAL DATA ARE PLACED IN
C             THE RETRIEVAL DATA BUFFER, AND THE FIRST GUESS DATA ARE
C             PLACED IN THE RETRIEVAL ANCILLARY BUFFER.  ONCE ALL THE
C             RETRIEVALS IN THE RETRIEVAL BUFFER ARE PROCESSED, THE
C             TOTAL OZONE BUFFER IS FILLED WITH TOTAL OZONE RETRIEVALS.
C
C   VERSION- 3.0 DOCUMENTATION AND SYNTAX CHANGES FOR THE ATOVS BASELINE
C                ON THE CRAY.
C   VERSION- 3.1 BUFFER CONTAINING SIMULATED RADIANCES WAS ADDED.
C   VERSION- 3.2 MODIFICATIONS WERE ADDED TO INCREASE THE ACCURRACY OF
C                OF UPPER LEVEL RETRIEVALS.
C   VERSION- 3.3 ADD HIRDAT,HNSPOT,HTOTLN, AND HDATEL TO CALL TO RETLNK.
C                ADD ULCHIR TO CALL TO TOZONE.
C   VERSION- 3.4 STORE CLOSENESS MEASURE IN RTVGRD:PHO.  ERROR HANDLE FOR
C                PLRAD7, WHICH RETURNS R4SPV FOR ANY INPUT TEMP. < 100.0.
C   VERSION- 3.5 ADD /MONSTA/ TO COUNT DIFFERENT WAYS RETRIEVAL FLAGS
C                ARE SET TO 0.
C   VERSION- 3.6 REMOVED THE FOLLOWING 3 PARAMETERS, WHICH USED TO BE 
C                EXTRACTED FROM THE CDB BY SUBROUTINE GCDBRT:
C                1. MEANTQ - CLASS MEAN RADIANCE AND WATER VAPOR MIXING
C                RATIO PROFILE, BASED ON MDB RADIOSONDE PROFILES
C                2. MEANP - PREDICTOR ARRAY (CLASS MEANS OF PER-CHANNEL
C                RADIANCE, PLUS ANCILLARY DATA)
C                3. TRNSMT - CLASS MEANS OF CHANNEL-SPECIFIC TRANSMIT-
C                TANCES AT EVERY LEVEL, BASED ON MDB DATA.
C                SINCE THIS REDUCED GCDBRT TO A SINGLE CDB CALL, GCDBRT
C                WAS IN-LINED INTO RETDVR.
C   VERSION- 3.7 SUPPLY THE CORRECT AMSU-A BRIGHTNESS TEMPERATURES TO THE
C                FIRST GUESS ARRAY (CORRESPONDING TO RTOVS SSU CHANNELS).
C   VERSION- 3.8 CHANGE THE SIMULATED RADIANCE SUBSTITUTION FOR TO BE FOR
C                AMSU-A CHANNELS 12, 13, AND 14.  IN RTOVS, THE SUB-
C                STITUTION WAS DONE FOR SSU CHANNELS 1-3 (SLOTS 25-27).
C   VERSION- 3.9 CHANGED DIMENSION OF CHNCMB FROM 2X3 TO 2X4.
C   VERSION- 3.10 ADD AMSU-A 11 TO CHANNELS USING SIMULATED RADIANCES IN
C                THE GUESS (ARRAY PGUESS).
C   VERSION- 3.11 ADD LIMHGT TO INPUT ARGUMENT LIST AND USE IT TO LIMIT
C                HEIGHT AT WHICH CLEAR RETRIEVALS CAN BE MADE.
C   VERSION- 3.12 FOR ONLINE FIRST GUESS SELECTION, ICE WILL NOW BE CONSIDERED
C                LAND, AS WILL COAST.  IN ADDITION, THE USE OF THE HIGH TERRAIN
C                CHANNEL COMBINATION FOR ALL LAND RETRIEVALS S OF -60 S WILL BE
C                DISCONTINUED.
C   VERSION- 3.13 ADDED RETRIEVAL OPERATOR SUBSTITUTION FOR THE ANT-
C                ARCTIC REGION.  FOR RETRIEVALS SOUTH OF 60S, IF THE
C                TERRAIN TYPE IS SEA, THE SECONDARY (BIN 8) RETRIEVAL
C                OPERATORS ARE USED; IF THE TERRAIN TYPE IS NOT SEA
C                (E.G., IF IT'S COAST OR LAND) USE THE PRIMARY (BIN 9)
C                OPERATORS.
C   VERSION- 3.14 (PI) ADDED RETRIEVAL OPERATOR SUBSTITUTION FOR THE
C                ARCTIC REGION.  THIS CHANGE IS A "MIRROR IMAGE" CHANGE
C                TO 3.13 (DESCRIBED ABOVE): IT SUBSTITUTES SECONDARY
C                (BIN 2) OPERATORS INTO OVER-SEA PORTIONS OF BIN 1.
C                ELSEWHERE IN BIN 1, THE PRIMARY (BIN 1) OPERATORS ARE
C                USED.
C   VERSION- 3.15 HIGH TERRAIN RETRIEVALS TO BE MADE SOUTH OF -60 S WILL BE
C                INDICATED BY SETTING HIGH2 TO "TRUE", WHICH WHEN INDICATED,
C                WILL CAUSE THE CHANNEL COMBINATION SET FOR THE RETRIEVAL IN
C                SUBROUTINE BLDCMP TO BE FURTHER DEGRADED BY EXCLUDING AMSU-A
C                CHANNEL 6.
C   VERSION- 3.16 AMSU-A 4 AND 5 CORRECTED FOR PRECIPITATION/CLOUD CONTAMINA-
C                TION ARE USED INSTEAD OF THE UN-CORRECTED CHANNELS FOR THE
C                RETRIEVAL.
C   VERSION- 3.17 AMSU-A 4 AND 5 CHANGES COMMENTED OUT FOR NOW.
C   VERSION- 3.18 ICE S OF -60 S NOW TREATED AS CLOUDY.
C   VERSION- 3.19 CHANGED DIMENSION OF CHNCMB FROM 2X4 TO 2X4X2.
C   VERSION- 3.20 ADD FRAME COUNTERS FOR /MONSTA/ - RETRETSP00 AND 
C                 RETRETNG0.  SLIGHTLY MODIFY CALCULATION OF HBFLIN,
C                 HBFSPT TO BE CONSISTENT WITH HOW CONVERSIONS BETWEEN
C                 HIRS AND RETRIEVAL GRID COORDINATES ARE DONE ELSE-
C                 WHERE IN SPGDVR. IMPROVE DOCUMENTATION.
C   VERSION- 3.21 INCORPORATED USE OF ALTERNATE HIGH TERRAIN CHANNEL COMBINATION
C                 WHEN TERRAIN HEIGHT EXCEEDS LIMHG2.
C   VERSION- 3.22 ADDED LIMHG2 TO ARGUMENT LIST, USED TO DETERMINE WHEN TO USE
C                 HITERRAIN 2 CHANNEL COMBO.
C   VERSION- 3.23 ADDED NRTHLM AND STHLIM AS NORTHERN AND SOUTHER LATITUDE 
C                 LIMITS FOR PERFORMING CLEAR RETRIEVALS. (WR#88)
C   VERSION- 3.24 ADDED CLRHGT TO INPUT ARGUMENT LIST FOR THE PURPOSE OF SETTING
C                 THE HEIGHT AT WHICH ONLY A CLOUDY CHANNEL COMBINATION (OR A
C                 A DEGRADATION THEREOF) WILL BE USED IN THE RETRIEVAL. THIS 
C                 HEIGHT (CLRHGT) RESIDES ON THE CDB, ALONG WITH LIMHGT AND 
C                 LIMHG2. TO MAKE SENSE, THE RELATIONSHIP OF THESE VARIABLES IS
C                 CLRHGT < LIMHGT < LIMHG2.
C   VERSION- 3.25 INCLUDED COMMON /HIRSTAT/. ADDED CODE TO SKIP CALLS TO TOZONE
C                 WHEN THE HIRS IS NOT AVAILABLE (WHEN COMMON VARIABLE 'HIROFF'
C                 IS SET TO 1). [WR #121]
C   VERSION- 3.26 MADE MODIFICATIONS TO USE OF NRTHLIM AND STHLIM SO THAT THE
C                 APPLICATION OCCURS AFTER USE OF CLRHGT, LIMHGT, AND LIMHG2 SO
C                 THAT USEPTH IS NOT THEN INADVERTANTLY RESET TO CLEAR AFTER
C                 BEING SET TO CLOUDY BECAUSE OF CHECKS OF NRTHLIM AND STHLIM.
C                 PR #    .
C   VERSION- 3.27 FOR WR #153 - REMOVED THE REQUIREMENT FOR NCEP FORECAST DATA
C                 FOR MAKING A RETRIEVAL UNLESS HIRS CHANNEL 8 IS SPECIFIED IN
C                 THE CHANNEL COMBINATION FLAGS FOR CLOUDY RETRIEVALS. INCLUDED
C                 'CLASIF'.
C   VERSION- 3.28 DISCONTINUED NONUSE OF AMSU-A CHS 5 AND 6 FOR HIGH TERRAIN FOVS
C                 WHEN THOSE FOVS ARE BETWEEN 60 S LATITUDE AND 60 N LATITUDE.
C                 (WR # 183).
C   VERSION- 3.29 ABSORBED VALUES OF TWO CONSTANTS, CNVFC1 AND CNVFC2 
C                 INTO INCLUDE MEMBER PLPHSCON.  THIS WAS DONE IN RESPONSE TO
C                 FPDT PR47, DATED 8/13/2001.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function 
C   VERSION-4.1     DATE- 09/04/03   PROGRAMMER- Rickey Rollins (CSC)
C                 Added INT function to integerize RETANC and REALSP before
C                 use in MOD function.
C   VERSION- 4.2  REMOVED ARGUMENT 'ICECHK' FROM CALL TO LBSRCH, WHICH DOES NOT
C                 HAVE THAT ARGUMENT.
C   VERSION- 5.0  ADAPTED FROM SUBROUTINE RETDVR TO PERFORM RE-RETRIEVAl USING
C                 CLOUD-CLEARED HIRS AND CLW-ADJUSTED AMSU-A CHANNELS.
C
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME   TYPE   I/O   DESCRIPTION
C   ----   ----   ---   -----------
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   CLDCHN  INT    I    ARRAY (DIM 2) CONTAINING THE BIT FLAGS FOR
C                       CHANNELS THAT ARE AFFECTED BY CLOUDS.
C   ULCHIR  REAL   I    UN-LIMB CORRECTED HIRS BRIGHTNESS TEMPERATURES
C                       (DIM HNSPOT,HTOTLN,2, WHERE 1=HIRS CH.8 AND
C                       2=HIRS CH.9)
C   LIMHGT  REAL   I    HEIGHT LIMIT FOR TERRAIN HEIGHT CHECKS.
C   SIMCHN  INT    I    ARRAY (DIM 35) SIMULATED RADIANCES FOR
C                       HIRS AND AMSU-A.
C   ERSTAT  INT    O    ERROR RETURN CODE FROM RETDVR
C                        = 0 IF NO ERROR OCCURRED
C                       <> 0 IF ERROR OCCURRED.
C
C   LOCAL VARIABLES-
C   NAME   TYPE   DESCRIPTION
C   ----   ----   ------------
C   ASAT   REAL   ARRAY (DIM NUMCHN X NUMCHN) CONTAINING THE PRIMARY
C                 RETRIEVAL OPERATOR COMPONENT MATRIX ASA-TRANSPOSE.
C   ASAT2  REAL   LIKE ASAT, BUT ASAT2 HOLDS THE SECONDARY ASAT MA-
C                 TRIX FROM EITHER 45N-60N(BIN 2) OR 45S-60S(BIN 8).
C                 THIS ASAT2 MATRIX IS SOMETIMES SUBSTITUTED FOR THE 
C                 ADJACENT (POLAR) MATRIX.  THAT IS, THE BIN 2 MATRIX
C                 (ASAT2) IS SOMETIMES SUBSTITUTED FOR BIN 1 ASAT
C                 MATRIX.  LIKEWISE, IN THE SOUTHERN HEMISPHERE, THE
C                 BIN 8 ASAT2 MATRIX IS SOMETIMES SUBSTITUTED FOR
C                 THE BIN 9 ASAT MATRIX.  SEE CHANGES RELATED TO 
C                 TO VERSIONS 3.13 AND 3.14, DESCRIBED ABOVE.
C   AVNSTR REAL   AVERAGE NSTAR VALUE FROM HIRS ANCILLARY BUFFER.
C   BTVCTR REAL   VECTOR (DIM NUMCHN) OF BRIGHTNESS TEMPERATURES (IN K)
C                 EXTRACTED FROM THE HIRS DATA BUFFER.
C   CHAN   INT    DO LOOP COUNTER FOR CHANNEL.
C   CHNUNC REAL   VECTOR (DIM NUMCHN) CONTAINING CHANNEL MEASUREMENT
C                 UNCERTAINTIES - EXTRACTED FROM CDB.
C   CLASS  INT    DO LOOP COUNTER FOR CLASS.
C   CLOPHO REAL   LOCAL VARIABLE TO STORE CLOSENESS MEASURE.
C   CLSPTR INT    ARRAY (DIM MAXCL X 3) CONTAINING THE LINK POINTERS
C                 TO THE CLASS LINKED LISTS FOR EACH PATH.
C   FIRST  LOG    FLAG SET TO .FALSE. AFTER THE FIRST CALL TO RETDVR.
C   GRDPNT INT    GRID POINT IN LINE OF RETRIEVAL GRID FROM GRDPOS.
C   GRDPOS INT    GRID POINT POSITION EXTRACTED FROM LINKED LIST.
C   HBFLIN INT    LINE OF HIRS DATA BUFFER CORRESPONDING TO HSPTNO
C   HBFSPT INT    SPOT IN LINE OF HIRS DATA BUFFER CORRESPONDING TO
C                 HSPTNO.
C   HIGH   INT    HIGH TERRAIN RETRIEVAL FLAG.
C   HSPTNO INT    HIRS SPOT NUMBER FROM RETRIEVAL ANCILLARY BUFFER.
C   LINE   INT    LINE OF RETRIEVAL GRID COMPUTED FROM GRDPOS.
C   LNKELM INT    VALUE POINTING TO NEXT ELEMENT OF LINKED LIST.
C   LNKPTR INT    ARRAY (DIM MPNOD X 3) CONTAINING THE LINK POINTERS
C                 FOR THE CLASS LINKED LISTS FOR EACH PATH.
C   LONG   REAL   LONGITUDE FROM HIRS ANCILLARY BUFFER.
C   LVL    INT    DO LOOP COUNTER FOR LEVELS.
C   MNPRFL REAL   VECTOR (DIM PRFTLV+PRFQLV+1) CONTAINING THE
C                 INTERPOLATED MEAN TEMPERATURE/MIXING RATIO VECTOR
C                 (TEMPS IN K, MIXING RATIO IN G/KG).
C   NODVAL INT    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   NSTARF REAL   N* FLAG FROM HIRS ANCILLARY BUFFER.
C   PATH   INT    DO LOOP COUNTER FOR PATH.
C   PGUESS REAL   ARRAY (DIM RETPARM:NUMCHN+NANCIL) CONTAINING THE FIRST
C                 GUESS RADIANCE VECTOR.
C   PREDVC REAL   VECTOR (DIM NUMCHN+NANCIL) CONTAINING VALUES FROM
C                 BTVCTR CONVERTED TO PLANCK RADIANCES AT 700 CM-1
C                 PLUS ANCILLARY ELEMENTS TO MAKE UP THE PREDICTOR
C                 VECTOR.
C   PROFTQ REAL   VECTOR (DIM PRFTLV+PRFQLV+1) CONTAINING THE
C                 INTERPOLATED TEMPERATURE/WATER VAPOR MIXING RATIO
C                 PROFILE (TEMPS IN K, MIXING RATIO IN G/KG).
C   RETFLG INT    RETRIEVAL FLAG FROM RETRIEVAL ANCILLARY BUFFER
C   RETTQ  REAL   VECTOR (DIM NUMLVL) OF RETRIEVED TEMPERATURE AND
C                 MIXING RATIO VALUES (TEMPS IN PLANCK RADIANCE AT
C                 700 CM-1, MIXING RATIO IN G/KG).
C   RLAT   REAL   LATITUDE FROM HIRS ANCILLARY BUFFER.
C   REALSP REAL   REAL VALUE OF HIRS TOTAL NUMBER OF SPOTS.
C   RTCMPG REAL   ARRAY (DIM NUMCHN X NUMCHN) CONTAINING THE RETRIEVAL
C                 COMPONENT G COMPUTED IN BLDCMP.
C   RTFGES REAL   VECTOR (DIM NUMLVL) CONTAINING VALUES FROM RETTQ
C                 WITH TEMPERATURES CONVERTED TO PLANCK RADIANCES
C                 AT 700 CM-1.
C   RTVCTR REAL   VECTOR (DIM NUMLVL) CONTAINING VALUES FROM RETTQ
C                 WITH TEMPERATURES CONVERTED TO PLANCK RADIANCES AT
C                 700 CM-1.
C   SAT    REAL   ARRAY (DIM NUMLVL X NUMCHN) CONTAINING THE PRIMARY
C                 RETRIEVAL OPERATOR COMPONENT MATRIX SA-TRANSPOSE.
C   SAT2   REAL   LIKE SAT, BUT SAT2 HOLDS THE SECONDARY SAT MATRIX
C                 FROM EITHER 45N - 60N (BIN 2) OR 45S - 60S (BIN 8).
C                 THIS SAT2 MATRIX IS SOMETIMES SUBSTITUTED FOR THE 
C                 ADJACENT (POLAR) MATRIX.  THAT IS, THE BIN 2 MATRIX
C                 (SAT2) IS SOMETIMES SUBSTITUTED FOR BIN 1 SAT
C                 MATRIX.  LIKEWISE, IN THE SOUTHERN HEMISPHERE, THE
C                 BIN 8 SAT2 MATRIX IS SOMETIMES SUBSTITUTED FOR
C                 THE BIN 9 SAT MATRIX.  SEE CHANGES RELATED TO 
C                 TO VERSIONS 3.13 AND 3.14, DESCRIBED ABOVE.
C   SHRKMX REAL   2D ARRAY (DIM NUMCHN X NUMCHN+NANCIL) CONTAINING
C                 THE SHRINKAGE ESTIMATOR COVARIANCE MATRIX.
C   SIMRAD REAL   ARRAY (DIM NUMCHN) CONTAINING SIMULATED RADIANCES.
C   STRPTR INT    POINTER TO START OF LINKED LIST FOR CLASS AND PATH.
C   SZAN   REAL   SOLAR ZENITH ANGLE FROM HIRS ANCILLARY BUFFER.
C   TERTY1 INT    TERRAIN TYPE USED FOR CALL TO BLDCMP.
C   TERTYP INT    TERRAIN TYPE FROM HIRS ANCILLARY BUFFER CONVERTED
C                 TO SEA/LAND ONLY FLAG.
C   TTERTYP INT   TERRAIN TYPE FROM HIRS ANCILLARY BUFFER CONVERTED
C                 TO SEA/LAND ONLY FLAG, USED SO TERTYP NOT OVERWRITTEN.
C   USEFCS LOG    FLAG SET TO .TRUE. IF THE FORECAST SURFACE POTENTIAL
C                 TEMPERATURE IS TO BE USED TO REPLACE HIRS CHANNEL 8
C                 IN THE RETRIEVAL STEP FOR CLOUDY RETRIEVALS.
C   USEPTH INT    PATH TO USE FOR FIRST GUESS AND RETRIEVAL COMPUTATION.
C                 FOR HIGH TERRAIN ALWAYS INDICATES CLOUDY PATH.
C   VGUESS REAL   ARRAY (DIM RETPARM:NUMLVL) CONTAINING THE FIRST
C                 GUESS TEMPERATURE AND MIXING RATIO VECTOR.
C
C   COMMON AREA PARAMETERS- LLPARM, INSCOM, RTVGRD, RTPLVL, FRMINF,
C                           SPVAL, RETPARM, COLLOC, INSPARM, RTVPARM
C
C   SUBPROGRAMS CALLED- RETLNK, PTHNAM, REALCOEF, GCDBOP, LBSRCH2, BLDCMP, 
C                       MAKRET, RTPRFL, QCHECK, TOZONE, RETDV$
C
C   FUNCTIONS USED- MOD, NINT
C
C   EXIT STATES-
C     ERSTAT = 99 : ERROR IN ACCESSING CDB OR OPCDB; OR
C                   ERROR IN RETRIEVAL INTERPOLATION; OR
C                   EXCESSIVE NEGATIVE VALUES IN RETRIEVALS.
C
C*********************************************************************
C
      INCLUDE'LLPARM'
      INCLUDE'INSPARM'
      INCLUDE'INSCOM'
      INCLUDE'RTVPARM'
      INCLUDE'RTVGRD'
      INCLUDE'FRMINF'
      INCLUDE'RETPARM'
      INCLUDE'RTPLVL'
      INCLUDE'COLLOC'
c     INCLUDE'MONSTA'
      INCLUDE'HIRSTAT'
      INCLUDE'CLASIF'
      INCLUDE'PLPHSCON'
C
      INTEGER CHNCMB(2,4,2), CLDCHN(2), ERSTAT

C
      INTEGER PATH, RETFLG, GRDPOS, CLASS, LNKELM, HBFSPT, HBFLIN
      INTEGER CLSPTR(MAXCL,3), NODVAL(MPNOD,3), LNKPTR(MPNOD,3)
      INTEGER HSPTNO, STRPTR, LINE, GRDPNT, CHAN, LVL, TERTYP, SMODLV
      INTEGER USEPTH,SIMCHN(35),TTERTYP,TERTY1,IDX,JDX,III,IFLG,IBT
      integer qflag
      integer hirchn

      REAL AVNSTR, RLAT, LONG, SZAN, LIMHGT, LIMHG2 
      REAL CLRHGT
      REAL ASAT(NUMCHN,NUMCHN), SAT(NUMLVL,NUMCHN), CHNUNC(NUMCHN)
      REAL ASAT2(NUMCHN,NUMCHN), SAT2(NUMLVL,NUMCHN)
      REAL RETTQ(NUMLVL), STHLIM, NRTHLM
      REAL RTFGES(NUMLVL), RTCMPG(NUMCHN,NUMCHN), BTVCTR(NUMCHN)
      REAL PREDVC(NUMCHN+NANCIL), RTVCTR(NUMLVL), SIMRAD(NUMCHN)
      REAL PROFTQ(PRFTLV+PRFQLV+1), MNPRFL(PRFTLV+PRFQLV+1)
      REAL SHRKMX(NUMCHN,NUMCHN+NANCIL)
      REAL VGUESS(NUMLVL), PGUESS(NUMCHN+NANCIL), NSTARF
      REAL ULCHIR(HNSPOT,HTOTLN,2), CLOPHO, REALSP

      CHARACTER*8 NAME
C
      LOGICAL HIGH,HIGH2,HIGH3,USEFCS,FIRST,ICECHK
      LOGICAL   ISR4SPV
      logical chgflg
C
      INCLUDE'SPVAL'
      INCLUDE'SPGUNT'
C
      DATA FIRST/.TRUE./, USEFCS/.FALSE./
      SAVE FIRST, USEFCS
      ERSTAT = 0
C
C  CHECK TO SEE IF HIRS CHANNEL 8 IS TO BE USED FOR CLOUDY RETRIEVALS.
C  IF IT IS, SET THE 'USEFCS' FLAG TO .TRUE. TO SPECIFY THAT THE
C  FORECAST SURFACE POTENTIAL TEMPERATURE MUST BE USED TO REPLACE
C  HIRS CHANNEL 8 IF A CLOUDY RETRIEVAL IS TO BE MADE. (ADDED FOR
C  WR #153)
C
      IF (FIRST) THEN
         FIRST = .FALSE.
C
C        CHECK FOR HIRS CHANNEL 8 FOR CLOUDY, SEA
C
         IFLG = P2EGFL(1,1)
         IBT  = 32-8
         IF (BTEST(IFLG,IBT)) USEFCS = .TRUE.
C
C        CHECK FOR HIRS CHANNEL 8 FOR CLOUDY, NONSEA
C
         IFLG = P2EGFL(1,2)
         IBT  = 32-8
         IF (BTEST(IFLG,IBT)) USEFCS = .TRUE.
      END IF
 
C
C  INITIALIZE RTVGRD:PHO FOR THIS FRAME
C
      DO IDX=1,RNGRDP
         DO JDX=1,RTOTLN
           PHO(IDX,JDX) = R4SPV
         ENDDO
      ENDDO
C
C  GET THE REAL VALUE OF THE HIRS TOTAL NUMBER OF SPOTS.
C
      REALSP = REAL(HNSPOT)
C
C  BUILD THE LINKED LISTS OF RETRIEVAL GRID POINT POSITIONS
C  CORRESPONDING TO ALL UNIQUE COMBINATIONS OF CLASS AND PATH.
C
      CALL RETLNK(RETANC,RNGRDP,RTOTLN,RNANC,CHNCMB,CLSPTR,LNKPTR,
     1            NODVAL,HIRDAT,HIRANC,HNSPOT,HTOTLN,HNANC,HDATEL)
C
C  NOW THAT ALL GRID POINTS TO USE IN MAKING RETRIEVALS HAVE BEEN ADDED
C  TO THE LINKED LIST, PROCESS ALL THE RETRIEVALS FOR EACH COMBINATION
C  OF CLASS AND PATH.
C
      DO 100 PATH = 1, 3
        DO 90 CLASS = 1, MAXCL
        ICECHK=.FALSE.
C
C  THE VALUE IN THE CLSPTR ARRAY SHOULD GIVE THE STARTING VALUE OF THE
C  LINKED LIST FOR THE CLASS IN THE LNKPTR ARRAY.  IF IT IS A MISSING
C  VALUE, NO LINKED LIST EXISTS FOR THE CLASS.
C
C
           IF (CLSPTR(CLASS,PATH).EQ.I4SPV) GOTO 90
           STRPTR = CLSPTR(CLASS,PATH)
           LNKELM = LNKPTR(STRPTR,PATH)
C
C  OBTAIN THE CLASSIFICATION FIRST GUESS VALUES FROM THE CDB TO USE
C  IN MAKING A RETRIEVAL.
C
C     DETERMINE APPROPRIATE MIDDLE NAME (CLR##/CLDY##/NOHIR##)
C     BASED ON PATH ID AND CLASS...
C
            CALL PTHNAM(PATH,CLASS,NAME)
C
C  OBTAIN CHANNEL MEASUREMENT UNCERTAINTIES VECTOR FROM CDB.
C
            CALL REALCOEF(CDBUNT,'RETRIEVE',NAME,'SIGMA   ',
     1                    NUMCHN,0,0,0,CHNUNC,'GET',ERSTAT)
            IF (ERSTAT.NE.0) THEN
             ERSTAT = 99
             RETURN
            ENDIF
C
C  OBTAIN THE CLASSIFICATION RETRIEVAL OPERATOR COMPONENTS FROM THE
C  OPCDB TO USE IN MAKING A RETRIEVAL.
C
C  IF WE'RE IN A POLAR CLASS (I.E., 1,10 & 17 FOR THE NORTH POLE, 
C  9,16, & 23 FOR THE SOUTH), OBTAIN THE SECONDARY RETRIEVAL
C  OPERATORS.  TO ACCOMPLISH THIS, FILL SAT2 AND ASAT2 ARRAYS WITH
C  OPERATOR COMPONENTS FROM THE ADJACENT CLASS...
C
      IF ((CLASS.EQ.9).OR.(CLASS.EQ.16).OR.(CLASS.EQ.23)) THEN
         CALL GCDBOP(PATH,8,NUMCHN,NUMLVL,NANCIL,SAT2,ASAT2,
     1               SHRKMX,ERSTAT)
           IF (ERSTAT.EQ.99) RETURN
        ELSEIF ((CLASS.EQ.1).OR.(CLASS.EQ.10).OR.(CLASS.EQ.17)) THEN
         CALL GCDBOP(PATH,2,NUMCHN,NUMLVL,NANCIL,SAT2,ASAT2,
     1               SHRKMX,ERSTAT)
           IF (ERSTAT.EQ.99) RETURN
        ELSEIF ((CLASS.EQ.2).OR.(CLASS.EQ.11).OR.(CLASS.EQ.18)) THEN
         CALL GCDBOP(PATH,1,NUMCHN,NUMLVL,NANCIL,SAT2,ASAT2,
     1               SHRKMX,ERSTAT)
           IF (ERSTAT.EQ.99) RETURN
        ELSEIF (CLASS.EQ.8) THEN
         CALL GCDBOP(PATH,9,NUMCHN,NUMLVL,NANCIL,SAT2,ASAT2,
     1               SHRKMX,ERSTAT)
           IF (ERSTAT.EQ.99) RETURN
      END IF
C
C   NEXT, GET THE PRIMARY RETRIEVAL OPERATORS FROM THIS CLASS, AND
C   OVERRIDE ANY SECONDARY SHRINKAGE COEFFICIENTS THAT MIGHT HAVE
C   BEEN PLACED IN SHRKMX...
C
           CALL GCDBOP(PATH,CLASS,NUMCHN,NUMLVL,NANCIL,SAT,ASAT,SHRKMX,
     1                 ERSTAT)
           IF (ERSTAT.EQ.99) RETURN
C
C  LOOP OVER ELEMENTS OF THE LINKED LIST AND PROCESS GRID POINTS.
C
   10      GRDPOS = NODVAL(LNKELM,PATH)
C
           LINE = (GRDPOS / RNGRDP) + 1
           GRDPNT = MOD(GRDPOS, RNGRDP) + 1
C
C  EXTRACT THE HIRS SPOT NUMBER, AND RETRIEVAL FLAG FROM THE RETRIEVAL
C  ANCILLARY BUFFER.  NOTE- HIRS SPOT NUMBER 1 CORRESPONDS TO LINE = 1,
C  SPOT = 1 OF HIRS DATA BUFFER.
C
           HSPTNO = NINT(RETANC(GRDPNT, LINE, 1))
           RETANC(GRDPNT, LINE, 4) = 0.0
           RETFLG = RETANC(GRDPNT, LINE, 4)
C
C  EXTRACT BRIGHTNESS TEMPERATURE VECTOR FROM HIRS DATA BUFFER AND
C  CONVERT TO PLANCK RADIANCE AT 700 CM-1.
C
           HBFLIN = (HSPTNO / HNSPOT) + 1
           HBFSPT = MOD(INT(RETANC(GRDPNT,LINE,1)),HNSPOT)
           IF (HBFSPT .EQ. 0) THEN
              HBFLIN = HBFLIN - 1
              HBFSPT = HNSPOT
           ENDIF

C
C  CHECK TO SEE IF THE RETRIEVAL WILL BE MADE OVER HIGH TERRAIN.  IF
C  SO USE PATH 2 (CLOUDY) CHANNEL COMB. FOR THE RETRIEVAL. RETRIEVALS
C  OVER LAND POLEWARD OF 60 S WILL ALSO BE CONSIDERED HIGH TERRAIN.
C
           TERTYP = NINT(HIRANC(HBFSPT,HBFLIN,9))
           IF (TERTYP.NE.0) TERTYP = 1
           RLAT = HIRANC(HBFSPT,HBFLIN,1)

           HIGH2 = .FALSE.
           HIGH3 = .FALSE.
           IF ((HIRANC(HBFSPT,HBFLIN,8) .GT. CLRHGT)) THEN
             HIGH = .TRUE.
             USEPTH = 2
             IF(HIRANC(HBFSPT,HBFLIN,8).GT. LIMHG2)HIGH3=.TRUE.
            IF(RLAT.LE.-60.0.OR.RLAT.GE.60.0)THEN
             IF(HIRANC(HBFSPT,HBFLIN,8).GT.LIMHGT)HIGH2 = .TRUE.
            ENDIF
           ELSE
             HIGH = .FALSE.
             HIGH2 = .FALSE.
             HIGH3 = .FALSE.
             USEPTH = PATH
           END IF
           IF(RLAT.LT.STHLIM .OR. RLAT .GT. NRTHLM)USEPTH = 2
           TTERTYP = NINT(HIRANC(HBFSPT,HBFLIN,9))
C-- INITIALIZE THE RETRIEVAL FLAG TO CLEAR
           HIRANC(GRDPNT, LINE, 4) = 0.0
C-- SET THE RETRIEVAL FLAG TO CLOUDY IF OVER HIGH TERRAIN OR IN THE POLES.
           IF (USEPTH .EQ. 2) HIRANC(GRDPNT, LINE, 4) = 16.0
           RETFLG = HIRANC(GRDPNT, LINE, 4)

C
C-- USE CLOUD-CLEARED CHANNEL BRIGHTNESS TEMPERATURES
C
           DO 20 CHAN = 1, NUMCHN
             BTVCTR(CHAN) = HIRDAT(HBFSPT, HBFLIN, CHAN)
             IF ((.NOT. ISR4SPV(BTVCTR(CHAN))) .AND. (CHAN .NE. 20)) THEN
               PREDVC(CHAN) = CNVFC1/(EXP(CNVFC2/(BTVCTR(CHAN)))-1.0)
             ELSE
               PREDVC(CHAN) = 0.0
             ENDIF
   20      CONTINUE
C
C  FOR CLOUDY RETRIEVALS, THE FORECAST SURFACE POTENTIAL TEMPERATURE IS
C  USED IN PLACE OF HIRS CHANNEL 8.  IF THE VALUE IS MISSING, A
C  RETRIEVAL CANNOT BE MADE.  ALSO HANDLE CASES IF FCST SFC. POT. TEMP.
C  MIGHT BE BAD (I.E. < 100.0)
C
C  FOR WR #153 - CHECK AND USE THE FORECAST SURFACE POTENTIAL TEMPERATURE
C  TO REPLACE HIRS CHANNEL 8 ONLY IF HIRS CHANNEL 8 IS SPECIFIED IN THE
C  CLOUDY RETRIEVAL CHANNEL COMBINATIONS.
C 
           IF (USEFCS .AND. PATH .EQ. 2) THEN
              IF (ISR4SPV(HIRANC(HBFSPT,HBFLIN,11))) THEN
                 RETANC(GRDPNT,LINE,5) = 0.0
c                RETRETSPT = RETRETSPT + 1
c                RETRETSP00(FRAMNO) = RETRETSP00(FRAMNO) + 1
                 GOTO 80
              ENDIF
              PREDVC(8) = CNVFC1/(EXP(CNVFC2/
     1                     (HIRANC(HBFSPT,HBFLIN,11)))-1.0)
              IF(ISR4SPV(PREDVC(8))) GO TO 80        
           ENDIF
C
C  GET THE ANCILLARY ELEMENTS FOR THE PREDICTOR VECTOR (LATITUDE,
C  LONGITUDE, AND SOLAR ZENITH ANGLE). THE LAST THREE ELEMENTS OF
C  THE PREDICTOR VECTOR ARE SET TO ZERO.
C
           RLAT = HIRANC(HBFSPT,HBFLIN,1)
           LONG = HIRANC(HBFSPT,HBFLIN,2)
           SZAN = HIRANC(HBFSPT,HBFLIN,3)
C
           PREDVC(NUMCHN+1) = RLAT
           PREDVC(NUMCHN+2) = LONG
           PREDVC(NUMCHN+3) = SZAN
C
           PREDVC(NUMCHN+4) = 0.0
           PREDVC(NUMCHN+5) = 0.0
           PREDVC(NUMCHN+6) = 0.0
C
C  EXTRACT N* FLAG FROM THE ELEMENT 24 OF THE HIRANC BUFFER.
C
           NSTARF = HIRANC(HBFSPT,HBFLIN,24)
C
C  N* FLAG IS MISSING VALUE. AVNSTR IS SET TO MISSING.
C
           IF (ISR4SPV(NSTARF)) THEN
              AVNSTR = R4SPV
C
C  N* FLAG INDICATES AVHRR N* IS USED. AVNSTR IS SET TO ELEMENT
C  17 OF THE HIRANC BUFFER.
C
           ELSEIF (NINT(NSTARF) .LT . 10) THEN
              AVNSTR = HIRANC(HBFSPT,HBFLIN,17)
C
C  N* FLAG INDICATES IR/MW N* IS USED. AVNSTR IS SET TO ELEMENT
C  21 OF THE HIRANC BUFFER.
C
           ELSE
              AVNSTR = HIRANC(HBFSPT,HBFLIN,21)
           ENDIF
C
C  GET THE FIRST GUESS USING LIBRARY SEARCH.
C
           TERTYP = NINT(HIRANC(HBFSPT,HBFLIN,9))
           IF (TERTYP.NE.0) TERTYP = 1
C
           CALL LBSRCH2(USEPTH,BTVCTR,HIRANC(HBFSPT,HBFLIN,11),RLAT,
     1                 TERTYP,VGUESS,PGUESS,SIMRAD,CLOPHO,ERSTAT)
           IF (ERSTAT .EQ. 99) RETURN
           IF (ERSTAT .EQ. 10) THEN
C
C IF NO FIRST GUESS COULD BE MADE, SET THE RETRIEVAL OPERATOR CLASS
C NUMBER TO ZERO
C
              RETANC(GRDPNT,LINE,5) = 0.0
              PHO(GRDPNT,LINE) = R4SPV
              GOTO 80
           ELSE
              PHO(GRDPNT,LINE) = CLOPHO
           ENDIF
              
C
C  BUILD THE NOISE COVARIANCE MATRIX AND ADD IT TO THE RETRIEVAL
C  COMPONENT (ASA)-TRANSPOSE.
C
           TERTY1 = TERTYP + 1
      IF ((NINT(HIRANC(HBFSPT,HBFLIN,9)).EQ.0) .AND. ((CLASS.EQ.1).OR.
     &    (CLASS.EQ.10).OR.(CLASS.EQ.17).OR.(CLASS.EQ.9).OR.
     &    (CLASS.EQ.16).OR.(CLASS.EQ.23))) THEN
C          USE THE SECONDARY ASAT (I.E., ASAT2)...
           CALL BLDCMP(RETFLG,CHNCMB(1,USEPTH,TERTY1),
     1     CHNCMB(1,4,2),CHNCMB(1,4,1),HIGH,HIGH2,HIGH3,CLDCHN,
     2     CHNUNC,AVNSTR,ASAT2,RTCMPG)
      ELSE 
     &IF ((NINT(HIRANC(HBFSPT,HBFLIN,9)).EQ.10) .AND. ((CLASS.EQ.2).OR.
     &    (CLASS.EQ.11).OR.(CLASS.EQ.18).OR.(CLASS.EQ.8))) THEN
C          USE THE SECONDARY ASAT (I.E., ASAT2)...
           CALL BLDCMP(RETFLG,CHNCMB(1,USEPTH,TERTY1),
     1      CHNCMB(1,4,2),CHNCMB(1,4,1),HIGH,HIGH2,HIGH3,CLDCHN,
     2      CHNUNC,AVNSTR,ASAT2,RTCMPG)
       ELSE
C          USE THE PRIMARY ASAT...
           CALL BLDCMP(RETFLG,CHNCMB(1,USEPTH,TERTY1),
     1     CHNCMB(1,4,2),CHNCMB(1,4,1),HIGH,HIGH2,HIGH3,CLDCHN,
     2     CHNUNC,AVNSTR,ASAT,RTCMPG)
      END IF
      DO 29 III = 1,NUMCHN
         IF(SIMCHN(III).EQ.1)PGUESS(III)=SIMRAD(III)
 29   CONTINUE
C
      IF ((NINT(HIRANC(HBFSPT,HBFLIN,9)).EQ.0) .AND. ((CLASS.EQ.1).OR.
     &    (CLASS.EQ.10).OR.(CLASS.EQ.17).OR.(CLASS.EQ.9).OR.
     &    (CLASS.EQ.16).OR.(CLASS.EQ.23))) THEN
C          WE'RE OVER SEA AND IN A POLAR CLASS, SO SUBSTITUTE
C          SECONDARY OPERATOR COMPONENTS...
           CALL MAKRET(SAT2,RTCMPG,VGUESS,PGUESS,PREDVC,SHRKMX,RETTQ)
      ELSE 
     &IF ((NINT(HIRANC(HBFSPT,HBFLIN,9)).EQ.10) .AND. ((CLASS.EQ.2).OR.
     &    (CLASS.EQ.11).OR.(CLASS.EQ.18).OR.(CLASS.EQ.8))) THEN
C          WE'RE OVER ICE AND IN A SEMI-POLAR CLASS, SO SUBSTITUTE
C          SECONDARY OPERATOR COMPONENTS...
           CALL MAKRET(SAT2,RTCMPG,VGUESS,PGUESS,PREDVC,SHRKMX,RETTQ)
        ELSE
C          USE PRIMARY OPERATOR COMPONENTS
           CALL MAKRET(SAT,RTCMPG,VGUESS,PGUESS,PREDVC,SHRKMX,RETTQ)
      END IF

C
C  CHECK FOR ANY NEGATIVE TEMPERATURE VALUES IN THE RETRIEVAL.  IF
C  FOUND, WRITE A MESSAGE TO THE KIDS AND SKIP THIS RETRIEVAL.
C  THE CLASS ID IN THE RETRIEVAL ANCILLARY BUFFER IS SET TO ZERO
C  SO THE INSTRUMENT VALUES WILL BE WRITTEN TO THE ODB.
C
      DO 30 LVL = 1, RETTLV + 1
        IF (RETTQ(LVL) .LE. 0.0) THEN
           CALL RETDV2$(110,CLASS,PATH,RETTQ(LVL),LVL,ERSTAT)
           IF (ERSTAT.EQ.99) RETURN
           RETANC(GRDPNT,LINE,5) = 0.0
c          RETRETNEG = RETRETNEG + 1
c          RETRETNG0(FRAMNO) = RETRETNG0(FRAMNO) + 1
           GOTO 80
        ENDIF
   30 CONTINUE
C
C  CONVERT PLANCK RADIANCES AT 700 CM-1 IN RETRIEVAL VECTOR AND
C  RETRIEVAL FIRST GUESS TO TEMPERATURE IN K.
C
           DO LVL = 1, RETTLV + RETQLV + 1
              IF (LVL .LE. RETTLV+1) THEN
                 RTVCTR(LVL) = CNVFC2/ALOG((CNVFC1/RETTQ(LVL)) + 1.0)
                 RTFGES(LVL) = CNVFC2/ALOG((CNVFC1/VGUESS(LVL))+ 1.0)
              ELSE
                 RTVCTR(LVL) = RETTQ(LVL)
                 RTFGES(LVL) = VGUESS(LVL)
              ENDIF
           END DO
C
C  INTERPOLATE THE RETRIEVAL TO THE TOVS TEMPERATURE AND WATER VAPOR
C  MIXING RATIO LEVELS.
C
           CALL RTPRFL(RTVCTR,PRETLV,RETTLV,RETQLV,PROFLV,PRFTLV,
     1                 PRFQLV,PROFTQ,ERSTAT)
           IF (ERSTAT.EQ.99) RETURN
C
C  INTERPOLATE THE RETRIEVAL FIRST GUESS TO THE TOVS TEMPERATURE
C  AND WATER VAPOR MIXING RATIO LEVELS.
C
           CALL RTPRFL(RTFGES,PRETLV,RETTLV,RETQLV,PROFLV,PRFTLV,
     1                 PRFQLV,MNPRFL,ERSTAT)
           IF (ERSTAT.EQ.99) RETURN
C
C  CHECK THE WATER VAPOR MIXING RATIO VALUES TO BE SURE THEY ARE
C  NOT LESS THAN ZERO OR GREATER THAN THE SATURATION MIXING RATIO.
C  ANY VALUES NOT MEETING THIS CRITERIA ARE CORRECTED.
C
           CALL QCHECK(PROFTQ(PRFTLV+2),MNPRFL(PRFTLV+2),
     1      PROFTQ(PRFTLV-PRFQLV+1),PROFLV(PRFTLV-PRFQLV+1),chgflg,
     2      qflag)

           if (chgflg) then
              RETANC(GRDPNT,LINE,9) = 1.0
              RETANC(GRDPNT,LINE,10) = FLOAT(qflag)
           else
              RETANC(GRDPNT,LINE,9) = 0.0
              RETANC(GRDPNT,LINE,9) = 0.0
           end if
C
C  PLACE INTERPOLATED TEMPERATURE AND WATER VAPOR MIXING RATIO VECTOR
C  IN THE RETRIEVAL DATA BUFFER.  PLACE THE FIRST GUESS TEMPERATURE/
C  WATER VAPOR MIXING RATIO VECTOR AND FIRST GUESS RADIANCE TEMPERATURE
C  VECTOR (CONVERTED TO TEMP IN K) IN THE RETRIEVAL ANCILLARY BUFFER.
C
C-- ADD THE RE-RETRIEVED PROFILES TO THE RETRIEVAL DATA AND ANCILLARY
C   BUFFERS
           DO 50 LVL = 1, PRFTLV
             RETDAT(GRDPNT,LINE,LVL+63) = PROFTQ(LVL)
             RETANC(GRDPNT,LINE,LVL+149) = MNPRFL(LVL)
   50      CONTINUE
C
           RETDAT(GRDPNT,LINE,PRFTLV+PRFQLV+1) = PROFTQ(PRFTLV+1)
C
           DO 60 LVL = 1, PRFQLV
             RETDAT(GRDPNT,LINE,LVL+105) = PROFTQ(LVL+PRFTLV+1)
             RETANC(GRDPNT,LINE,LVL+191) = MNPRFL(LVL+PRFTLV+1)
   60      CONTINUE
C
C CONVERT THE FIRST GUESS CHANNEL RADIANCES TO BRIGHTNESS TEMPERATURES
C
           DO 70 CHAN = 1, NUMCHN
             IF (PGUESS(CHAN).GT.0) THEN
               RETANC(GRDPNT,LINE,CHAN+18+PRFTLV+PRFQLV) = 
     1          CNVFC2/ALOG((CNVFC1/PGUESS(CHAN) ) + 1.0)    
             ELSE
               RETANC(GRDPNT,LINE,CHAN+18+PRFTLV+PRFQLV) = PGUESS(CHAN)
             ENDIF
   70      CONTINUE
C  COMPUTE THE SURFACE MODEL LEVEL AND STORE IT IN ELEMENT 7 OF THE
C  RETRIEVAL ANCILLARY BUFFER FOR EACH SOUNDING.
C
C-- USE RE-RETRIEVED SURFACE TEMPERATURE

           CALL GETSML(RETDAT(GRDPNT,LINE,105),
     +                 HIRANC(HBFSPT,HBFLIN,10),
     +                 HIRANC(HBFSPT,HBFLIN,8),SMODLV)
           RETANC(GRDPNT,LINE,211) = SMODLV
C
c
C
C  CHECK LINK ELEMENT TO FIND NEXT ELEMENT OF THE LINKED LIST.  IF THE
C  LINK ELEMENT IS 0, THE END OF THE LIST HAS BEEN REACHED.  OTHERWISE,
C  CONTINUE PROCESSING THE LIST.
C
   80      LNKELM = LNKPTR(LNKELM, PATH)
           IF (LNKELM.NE.0) GOTO 10
C
   90   CONTINUE
  100 CONTINUE
C
C  FILL TOTAL OZONE DATA BUFFER WITH TOTAL OZONE RETRIEVALS.
C  (SKIP THIS CALL IF THE HIRS IS NOT AVAILABLE OR IS BAD).
C
      IF (.NOT.(HIROFF)) 
     1   CALL TOZONECC(HIRDAT,HIRANC,HNSPOT,HTOTLN,HDATEL,HNANC,RETANC,
     2               RNGRDP,RTOTLN,RNANC,TOZDAT,ULCHIR,ERSTAT)
C
      RETURN
      END
C
      SUBROUTINE RETDV2$(MSGCOD,CLASS,PATH,TEMP,LEV,STAT)
      IMPLICIT NONE
C
C*********************************************************************
C   NAME- RETDV2$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY   
C
C   VERSION- 1.0    DATE- 07/28/92    PROGRAMMER- CARLA NEIGH(SMSRC)
C   VERSION- 3.0    DATE- 07/10/97    PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.1    DATE- 04/22/98    PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 3.2    DATE- 06/23/98    PROGRAMMER- LEAH WILSON (RSTX)
C
C   FILES-
C    DSN                       I/O  UNIT   FUNCTIONAL DESCRIPTION
C    ---                       ---  ----   -----------------------
C    /....KIDS                  O   KUNIT  KRAY I/A DOCUMENTATION SYSTEM
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE RETDVR
C   VERSION- 3.0 CONVERT FROM MMLF TO KIDS FORMAT.
C   VERSION- 3.1 MODIFY COUNT TO ONLY PRINT OUT 10 MESSAGES PER FRAME.
C   VERSION- 3.2 ADD LEVEL TO ERROR MESSAGE.
C
C   PROGRAM CONSTANTS-
      INCLUDE 'ERPARM'
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE      TYPE  I/O  FUNCTION
C     --------      ----  ---  --------
C     MSGCOD        INT   I   MESSAGE CODE USED TO IDENTIFY THE ERROR
C                             PATTERN.
C     CLASS         INT   I   CLASS IDENTIFICATION NUMBER.
C     PATH          INT   I   PATH IDENTIFICATION NUMBER.
C     TEMP          REAL  I   TEMPERATURE VALUE.
C     LEV           INT   I   LEVEL OF TEMPERATURE.
C     STAT          INT   O   99 = FATAL ERROR, ELSE 0 
C
      INTEGER   MSGCOD, CLASS, PATH, STAT, LEV
      REAL TEMP
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE  'ERINFO'
      INCLUDE  'SPGUNT'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     COUNT                    I*4    COUNT OF HOW MANY TIMES MESSAGE
C                                     HAS BEEN WRITTEN
C     KMSG                    CH*100  KIDS MESSAGE BUFFER.
C     C4TEMP                   CH*4   HOLDING BUFFER FOR WRITING TO KMSG
C     C3TEMP                   CH*3   HOLDING BUFFER FOR WRITING TO KMSG
C     C2TEMP                   CH*2   HOLDING BUFFER FOR WRITING TO KMSG
C
C   SUBPROGRAMS CALLED- KIDS 
C
C   ASSUMPTIONS- THIS PROGRAM ASSUMES KIDS HAS BEEN CALLED PREVIOUSLY TO
C                OPEN THE KIDS FILE.
C
C   EXIT STATES-
C     STAT =  0; INDICATING NO OR A NON-FATAL ERROR HAS OCCURRED
C     STAT = 99; INDICATING A FATAL ERROR HAS OCCURRED
C
C*********************************************************************
C
      INTEGER COUNT(28),COUNT2
      CHARACTER*2 C2TEMP 
      CHARACTER*3 C3TEMP 
      CHARACTER*4 C4TEMP 
      CHARACTER*8 NAME
      CHARACTER*12 C12TMP 
      CHARACTER*100 KMSG
C
      SAVE COUNT,COUNT2
      DATA COUNT /28*0/, COUNT2 /0/
C
C  SET ERINFO:SUBNAM TO MAIN MODULE NAME, SINCE KIDS EXTRACTS SUBROUTINE
C  NAMES INTERNALLY.
C
      SUBNAM = 'SPGDVR '
C
C DETERMINE WHICH ERROR MESSAGE TO WRITE
C
      IF (COUNT(FRMNBR) .EQ. 10) RETURN
C
      IF (MSGCOD .EQ. 110) THEN
        COUNT(FRMNBR) = COUNT(FRMNBR) + 1
        KMSG(1:50)='* WARNING * Fr.    class    path    unreasonable r'
        KMSG(51:100)='etrieval temp:              (level    )'
        WRITE(C2TEMP,'(I2)') FRMNBR
        KMSG(17:18) = C2TEMP
        WRITE(C2TEMP,'(I2)') CLASS
        KMSG(26:27) = C2TEMP
        WRITE(C2TEMP,'(I2)') PATH 
        KMSG(34:35) = C2TEMP
        WRITE(C12TMP,'(F12.4)') TEMP
        KMSG(66:77) = C12TMP
        WRITE(C3TEMP,'(I3)') LEV  
        KMSG(86:88) = C3TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=0
        IF(COUNT(FRMNBR).EQ.10) THEN
           KMSG(1:43)='No more unreasonable temp. messages will be'
           KMSG(44:100)=' printed for this frame. '
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
           IF(COUNT2.EQ.0) THEN
              KMSG(1:40)='Final tally of unreasonable temperature '
              KMSG(41:100)='counts are provided at SPGDVR end.'
              CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
           ENDIF
           COUNT2 = 1
        ENDIF
C
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = ' Invalid error message code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END


