      SUBROUTINE RETOD2(ODFBUF,I,J,II,JJ,RDATCY,S,GDDATA,LAYERS,
     1                  RETCLS,SYNCTI,OBSHIR,DELTIME,
     2                  FCSTYM,OLR,LC1,LC2,LC3,LC4,AMASPT,
     3                  AMALNE,OBSQUL,STB1ST,GEOPOT,ERSTAT)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- RETOD2
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 09/11/92   PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 2.0    DATE- 05/06/94   PROGRAMMER- FRANK TILLEY (SMSRC)
C          XATOV-3: REPLACE COMMON BLOCK /BUFFER/ WITH PARAMETER
C                   FILE INSPARM AND COMMON BLOCK /INSCOM/.
C   VERSION- 2.1    DATE- 05/18/94   PROGRAMMER- JEFF MANNING (SMSRC)
C          XATOV-5: REMOVE RNGRDP, RTOTLN, RDATEL, RNANC FROM THE
C                   CALLING ARGUMENTS.  INCLUDE PARAMETER FILE
C                   /RTVPARM/.  CHANGE DIMENSION RDATCY(56) TO
C                   RDATCY(RDATEL).
C   VERSION- 2.2    DATE- 06/14/94   PROGRAMMER- JEFF MANNING (SMSRC)
C          XATOV-21 INCLUDE THE PARAMETER FILE /RETPARM/.  CHANGE THE
C                   HARD-CODED DIMENSIONS FOR S, WVMR, LAYERS, STDIDX,
C                   INVPRS, GEOHGT, GOHGKM, INVDAT.  CHANGE
C                   CORRESPONDING DO LOOPS FROM HARD-CODED VALUES TO
C                   THE APPROPRIATE PARAMETERS.
C   VERSION- 2.3    DATE- 10/24/94   PROGRAMMER- CARLA NEIGH (SMSRC)
C         ATOVS-28: CHANGE DIMENSION OF HORGRD FROM 2 X 20 TO 20.
C   VERSION- 2.4    DATE- 04/19/95   PROGRAMMER- LI LU (SMSRC)
C         ATOVS-97: REMOVE CALLS TO SPGSR4, SPGSI4, STGASG AND
C                   ASSOCIATED VARIABLES. REPLACE WITH CALLS TO SPGFAU.
C                   ADD CODE TO PACK FCST_RH, CLD_LIQW, HIRS8COR,
C                   FCST_SFT, ADJ_PRES, AND BIAS_TMP. REMOVE PACKING OF
C                   SPARE VALUE.
C   VERSION- 2.5    DATE- 08/02/95   PROGRAMMER- LI LU (SMSRC)
C        ATOVS-153: ARGUMENTS FOR FRAME TIME, SOUNDING TIME,
C                   OBSERVATION QUALITY FLAG AND GEOPOTENTIAL HEIGHT
C                   ARE ADDED AS INPUT. CALLS ARE ADDED FOR GETSML AND
C                   STBDVR. CALLS TO PACK ROUTINES ARE ADDED FOR
C                   SURFACE MODEL LEVEL, STABILITY FORECAST INCREMENT,
C                   AVHRR N* VALUE AND N* FLAG. PACKING OF REMAINING
C                   STABILITY DEPARTURE VALUES ARE CHANGED FROM R4SPV
C                   TO PROPER VALUES RETURNED FROM STBDVR.
C   VERSION- 2.6    DATE- 12/8/95    PROGRAMMER- CARLA NEIGH (SMSRC)
C        ATOVS-284: VARIABLE SKNTMP IS REMOVED AND MISSING VALUE R4SPV
C                   IS OUTPUT AS DERIVED SKIN TEMPERATURE.  RETRIEVED
C                   SURFACE TEMPERATURE IS PASSED AS FIRST ARGUMENT
C                   TO GETSML.
C   VERSION- 3.0    DATE- 05/14/96   PROGRAMMER- ALEX POZDNYAK, FDC
C      FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C      INTEGER, ALL L*4 TO LOGICAL, AND ALL R*4 DECLARATIONS TO REAL.
C   VERSION- 3.1    DATE- 07/01/96   PROGRAMMER- FRANK TILLEY, SMSRC
C      DIMENSION OF PREWAT WAS CNANGED FROM 15 TO RETPARM:PRFQLV.
C      IN THE CALL TO SPGFAU PREWAT(3) IS USED TO PICK UP LAYERS 3
C      THROUGH 17.  THE HORIZONTAL GRADIENTS OF TEMPERATURE WERE
C      INITIALIZED FROM 1 TO 20 TO SET HORGRD(INDEX) TO R4SPV.
C   VERSION- 3.2    DATE- 01/31/97   PROGRAMMER- LEAH WILSON, HSTX
C      INCREASE 3RD DIMENSION OF TOZDAT TO 2, AND WRITE SULFUR DIOXIDE
C      TO ODB.
C   VERSION- 4.0    DATE- 09/02/97   PROGRAMMER- LEAH WILSON, HSTX
C      CONVERT ODB TO ODF.  ADD TPW CALCULATIONS AND REMOVE PCPH2O,
C      WHICH IS REPLACED BY THE (IN-LINED) RTOVS ROUTINE NEWH2O.  ADD
C      OBSHIR TO PACK NODE, AND REMOVE HORGRD. ADD /OZONE/ TO GET SML
C      SAVED FOR SUBROUTINE OZOUT.  ADD /ATTENU8/ TO GET CORRECTED
C      HIRS-8.  ADD AMASPT AND AMALNE FOR ACCESSING NON-LIMB CORRECTED
C      DATA.
C   VERSION-4.1     DATE- 05/04/98   PROGRAMMER- BRIAN BARIL (RSTX)
C       REINSTALLED THE STABILITY SECTION.  ADDED STB1ST TO
C       THE CALL TO STBDVR.  INITIALIZED THE STABILITY 
C       VARIABLES TO I4SPV BEFORE THE CALL TO STBDVR.  THIS
C       PREVENTS REPETITOUS DATA FROM BEING WRITTEN TO RODF.
C   VERSION- 4.2    DATE- 08/06/98   PROGRAMMER- LEAH WILSON (RSTX)
C       ADDED /STBCOM/ TO SOFT-CODE STBDVR.
C   VERSION- 4.3    DATE- 08/31/98   PROGRAMMER- LEAH WILSON (RSTX)
C       ADDED /SPGUNT/ AND KIDS MESSAGING, AND LOGIC TO TURN STBDVR OFF
C       IF IT RETURNS ANY NON-ZERO ERROR CODE SO OTHER PROCESSING CONTINUES. 
C   VERSION- 4.4    DATE- 09/28/98   PROGRAMMER- LEAH WILSON (RAYTHEON)
C       SET WORDS FOR TOTAL OZONE AND SO2 TO I2SPV BEFORE DETERMINING IF
C       THEY SHOULD BE FILLED BY TOZDAT.  THIS IS AN ATTEMPT TO SEE THE
C       FILL VALUES IN THE OZONE EDGE IMAGES.
C   VERSION- 4.5    DATE- 06/25/99   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C       MULTIPILED THE PRECIPITABLE WATER VALUES BY 10.0 BECAUSE THE
C       CONSTANT GCX200 GIVES THE PRECIPITABLE WATER VALUES IN CENTIMETERS.
C       BY MULTIPLYING BY 10.0, THE VALUES WILL BE IN MILLIMETERS WHICH IS
C       THE DESIRED UNITS.
C   VERSION- 4.6    DATE- 07/21/99   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C       INCLUDED THE LAYERS 2 (200MB-250MB) AND 3 (250MB-300MB) IN THE TOTAL
C       PRECIPITABLE WATER CALCULATION.
C   VERSION- 4.7    DATE- 08/17/99   PROGRAMMER- M.P.FERGUSON (RAYTHEON)
C       IN THE CASE OF USE OF THE PRECIP AND CLOUD CONTAMINATION-CORRECTED
C       AMSU-A CHANNELS 4 AND 5, THESE CHANNEL VALUES ARE PLACED ONTO THE RODF.
C   VERSION- 4.8    DATE- 08/30/99   PROGRAMMER- M.P.FERGUSON (RAYTHEON)
C       GO BACK TO VERSION 4.6.
C   VERSION- 4.9    DATE- 11/05/99   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C       CORRECTED INDEX ERROR IN PLACING OZONE AND SO2 DATA ONTO THE RODF.
C       (INDEX I CHANGED TO INDEX S(I))
C   VERSION- 4.10   DATE- 08/02/00   PROGRAMMER- M.P.FERGUSON (RITSS)
C       ARRAY BADHIR ACCESSED VIA COMMON HIRSTAT (ADDED) TO WRITE MISSING
C       VALUES TO THE RODF FOR LIMB CORRECTED HIRS, NON-LIMB CORRECTED HIRS,
C       AND BIAS CORRECTED HIRS WHEN A PARTICULAR CHANNEL IS DETERMINED TO
C       BE EITHER BAD OR QUESTIONABLE AT A PARTICULAR RETRIEVAL FOV (WR # 105).
C   VERSION- 4.11  DATE- 12/05/00    PROGRAMMER- M.P.FERGUSON (RITSS)
C       DO NOT WRITE AMSU-A DATA TO RODF IF BADAMA ARRAY INDICATES THAT IT IS
C       BAD AND IT IS NOT USED IN THE RETRIEVAL.
C   VERSION- 5.0    DATE- 09/14/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C       ADDED CODE TO WRITE OUT AMSU-B CHANNEL VALUES TO ODF BUFFER
C       ELEMENTS 461-465 AND NON-CORRECTED AMSU-B VALUES TO ODB BUFFER
C       ELEMENTS 466-470 (WR #22)
C   VERSION- 5.1    DATE- 10/09/02   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C       ADDED CODE TO PRODUCE A FLAG WHICH INDICATES OZONE VALUES IN CLOUDY
C       AREAS ABOVE 350MB. THE FLAG WAS PLACED IN ELEMENT 471 OF THE ODF 
C       BUFFER. (WR#204)
C   VERSION- 5.2    DATE- 10/11/02   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C       REPLACED THE CALL TO GETSML WITH EXTRACTION OF THE SURFACE MODEL
C       LEVEL FROM THE RETRIEVAL ANCILLARY BUFFER. (WR #215)
C   VERSION- 5.3    DATE- 08/04/03   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C       MODIFIED THE CODE TO WRITE HIRS CHANNEL 20 DATA INSTEAD OF THE
C       ORBIT NODE TO RODF WORD 181 (WR #238).
C   VERSION- 6.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C       Changed ODFBUF from INTEGER to I*2 and I4SPV values in
C       ODFBUF to I2SPV. Replaced R4SPV equivalence tests with
C       ISR4SPV function.
C   VERSION- 6.1    DATE- 09/29/05   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C       SET TOTAL OZONE (WORD 442) TO I2SPV WHERE CLOUD TOP PRESSURE
C       IS LESS THAN 350 MB. (WR #286).
C   VERSION- 6.2    DATE- 04/03/06   PROGRAMMER- FRANK TILLEY (ERT)
C       SET CLOUD AMOUNT TO MISSING WHERE CLOUD TOP PRESSURE IS
C       ZERO OR LESS.
C   VERSION- ?.?    DATE- 06/20/06   PROGRAMMER- FRANK TILLEY (IMSG)
C       REPLACED SULFUR DIOXIDE IN WORD 446 WITH CALCULATED AEROSOL
C       AMOUNT.
C
C   NOTE-
C      DUE TO THE VOLUME OF DATA WRITTEN TO THE RODF, A NUMBER OF THE
C      LOOPS IN THIS ROUTINE WERE UNWOUND TO SPEED UP PROCESSING.
C
C   FUNCTION-  THIS SUBROUTINE IS ONLY CALLED BY RETODB AND TOGETHER
C   WITH RETODB AND RETOD1 PACKS RETRIEVAL GRID DATA INTO THE ODF
C   BUFFER FOR OUTPUT TO THE ORBITAL DATA FILE.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  DESCRIPTION
C     --------          ---- ---  ----------------------------------
C     ODFBUF            INT   I/O ODF BUFFER.
C     I                 INT    I  INDEX USED FOR CURRENT GRID POINT
C                                 NUMBER.
C     J                 INT    I  INDEX USED FOR CURRENT GRID LINE
C                                 NUMBER.
C     II                INT    I  INDEX USED FOR THE HIRS SPOT NUMBER.
C     JJ                INT    I  INDEX USED FOR THE HIRS LINE NUMBER.
C     RDATCY            REAL  I/O VECTOR OF DIMENSION RTVPARM:RDATEL,
C                                 CONTAINING RETPARM:PRFTLV TOVS
C                                 TEMPERATURE PROFILE(K), RETPARM:PRFQLV
C                                 WATER VAPOR MIXING RATIOS(G/KG) AND
C                                 ONE SURFACE TEMPERATURE (K).
C     S                 INT    I  VECTOR OF RTVPARM:RNGRDP SORTED SPOT
C                                 NUMBERS.
C     GDDATA            LOG    I  LOGICAL SET TO TRUE IF GOOD RETRIEVAL
C                                 DATA IS PRESENT FOR A GRID POINT.
C     LAYERS            REAL   I  VECTOR OF LENGTH RETPARM:PRFTLV
C                                 CONTAINING LAYER THICKNESS AT THE
C                                 RTPARM:PRFTLV TOVS LEVELS.
C     RETCLS            INT    I  THE RETRIEVAL CLASS INDEX.
C     SYNCTI            INT    O  ARRAY (DIM:3) CONTAINING SOUNDING TIME
C     OBSHIR            REAL   I  OBSERVED HIRS BUFFER.
C     DELTIME           REAL   I  POT.TEMP.TIME MINUS NCEP FCST. TIME
C     FCSTYM            INT    I  FORECAST TIME AS YYMM,DDHH.
C     OLR,LC1-4         INT    I  OUTGOING LONGWAVE RADIATION AND 4 LAYER
C                                 COOLING RATES FOR EACH HIRS SPOT.
C     AMASPT            INT    I  ARRAY OF DIMENSION (INSPARM:HNSPOT,
C                                 INSPARM:HTOTLN) CONTAINS AMSU-A
C                                 COLLOCATED SPOT NUMBERS.
C     AMALNE            INT    I  ARRAY OF DIMENSION (INSPARM:HNSPOT,
C                                 INSPARM:HTOTLN) CONTAINS AMSU-A
C                                 COLLOCATED LINE NUMBERS.
C     GEOPOT            REAL   O  ARRAY (RETPARM:PRFTLV) OF RETRIEVAL
C                                 GEOPOTENTIAL HEIGHTS ON THEIR WAY TO
C                                 STBDVR.
C     ERSTAT            INT    O  ERROR CODE RETURNED.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'RETPARM'
      INCLUDE 'RTVPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'INPARM'
      INCLUDE 'RMPARM'
      INCLUDE 'HIRSTAT'
      INCLUDE 'AMASTAT'
      INCLUDE 'RTPLVL'
      INCLUDE 'RTVGRD'
      INCLUDE 'STBGEO'
      INCLUDE 'ATTENU8'
      INCLUDE 'NONLCDAT'
      INCLUDE 'STBCOM'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  DESCRIPTION
C     --------          ----  ---------------------------------------
C     C2                REAL  EQUALS R/(2.*G) WHERE R IS THE GAS
C                             CONSTANT FOR DRY AIR (287 KG/JOULE/DEG K
C                             AND G IS THE ACCELERATION DUE TO EARTHS
C                             GRAVITY (9.8M/S**2).
C     GASCON            REAL  GAS CONSTANT DIVIDED BY GRAVITY CONSTANT
C                             (2*C2).
C     FGWVMR            REAL  VECTOR OF RETPARM:PRFQLV FIRST GUESS WATER
C                             VAPOR MIXING RATIOS ALOG(G/KG).
C     FCSTP             REAL  THE FORECAST PRESSURE.
C     FCSTT             REAL  THE FORECAST POTENTIAL TEMPERATURE.
C     NVALS             REAL  BUFFER HOLDING N* VALUES.
C     NFLAG             INT   BUFFER HOLDING N* FLAG.
C     K                 INT   LOOP CONTROL INDEX.
C     C                 INT   COUNTER.
C     LYRTHK            REAL  VECTOR OF RETPARM:STDLVL THICKNESS
C                             LAYERS (M).
C     CLREQU            REAL  VECTOR OF RETPARM:NUMCHN + RETPARM:GACCHN
C                             CLEAR EQUIVALENT BLACK BODY
C                             TEMPERATURES (K).
C     STDIDX            INT   VECTOR OF RETPARM:STDLVL STANDARD LEVEL
C                             INDICES.
C     LYRMVT            REAL  VECTOR OF RETPARM:STDLVL-1 LAYER MEAN
C                             VIRTUAL TEMPS (K).
C     TOTWAT            REAL  TOTAL PRECIPITABLE WATER (MM).
C     WAT300            REAL  TOTAL PRECIPITABLE WATER (MM) 300-500 MB.
C     WAT500            REAL  TOTAL PRECIPITABLE WATER (MM) 500-700 MB.
C     WAT700            REAL  TOTAL PRECIPITABLE WATER (MM) 700-1000 MB.
C     ONCE              LOG   SET TRUE FOR DETERMINING THE PRESSURE
C                             LEVEL OF THE CLOUD AMOUNT.
C     PREWAT            REAL  VECTOR OF RETPARM:PRFQLV HOLDING LAYER
C                             PRECIPITABLE WATER(MM).
C     GCX200            REAL  THE GRAVITATION CONSTANT (9.81 M/S**2)
C                             TIMES 200.
C     CLDCMP            INT   CLOUD COMPARISON FLAG.
C                             CLEAR RETV'L + CLEAR CLD.ALG. =  0 (GOOD)
C                             CLEAR RETV'L + CLOUDY CLD.ALG.=  1 (BAD)
C                             CLOUDY RETV'L + CLOUDY CLD.ALG=  0 (GOOD)
C                             CLOUDY RETV'L + CLEAR CLD.ALG.=  2 (SO-SO)
C
C   SUBPROGRAMS CALLED-  SPGFAU, TKLAYR, GETSML, STBDVR
C
C   FUNCTIONS USED- LOG, NINT, ALOG
C
C   EXIT STATES-  0 = NORMAL CONDITION
C                 99 = FATAL ERROR
C
C   COMMENTS-  THE HIRS TIME AND ANCILLARY BUFFERS WILL BE USED FOR
C              THE RETRIEVAL GRID DATA. THESE BUFFERS WILL CONTAIN
C              THE NECESSARY VALUES EVEN IF THE HIRS INSTRUMENT IS
C              NOT PRESENT.
C*********************************************************************
C
      INTEGER     BUFSIZE
      PARAMETER(BUFSIZE=500)
      INTEGER     I, J, II, JJ, ERSTAT, DUMOBS
      INTEGER*2   ODFBUF(BUFSIZE)
      INTEGER     S(RNGRDP), RETCLS, SYNCTI(3), FCSTYM(2), OBSQUL
      REAL        RDATCY(RDATEL), OBSHIR(HNSPOT,HTOTLN,35)
      REAL        OLR(HNSPOT,HTOTLN), LC1(HNSPOT,HTOTLN)
      REAL        LC2(HNSPOT,HTOTLN), LC3(HNSPOT,HTOTLN)
      REAL        LC4(HNSPOT,HTOTLN), DELTIME
      LOGICAL     GDDATA, STB1ST
      LOGICAL     ISR4SPV
      CHARACTER*6 SUBNAM
      CHARACTER*100 KMSG
C
      INTEGER     STDIDX(STDLVL), K, MISCF, C, NFLAG, CLDCMP, LEVEL
      REAL        LYRTHK(STDLVL), TOTWAT, WAT300, WAT500, WAT700
      REAL        PREWAT(PRFQLV),GEOPOT(PRFTLV)
      REAL        CLDABO(10), C2, MR2TDEW, CTEMP, MRTEMP, TDEW
      REAL        LYRMVT(STDLVL-1), FGWVMR(PRFQLV)
      REAL        GASCON, LAYERS(PRFTLV), CLREQU(NUMCHN+GACCHN)
      REAL        NVALS,DELTAH, DELTAL, DELTAS
      INTEGER     DELTAT,AMASPT(HNSPOT,HTOTLN), AMALNE(HNSPOT,HTOTLN)
      INTEGER     KK, IC, IIC, IIII, I0CNT, IDX, JDX, WORD
      LOGICAL     ONCE, SUPERSAT
C
      INCLUDE 'SPVAL'
      INCLUDE 'OZONE'
      INCLUDE 'SPGUNT'
C
C**********************************************************************
      REAL GCX200
C
      DATA STDIDX/4,6,7,9,10,11,13,15,16,18,20,23,24,25,26,28,31,35,
     1            37,40/
C
C     INITIALIZE LOCAL VARIABLES
C
      GCX200 = 1961.33
      C2 = 14.64285
      GASCON = 2 * C2
      LAYERS(PRFTLV) = 0
C
C     PACK THE SATELLITE ID
C
      ODFBUF(2) = SATNM
C
C     CALCULATE AND PACK THE TOTAL PRECIPITABLE WATER PARAMETERS
C
      TOTWAT = 0.0
      WAT300 = 0.0
      WAT500 = 0.0
      WAT700 = 0.0
      PREWAT(1) = 0.0
      DO 70 K = 17, 2, -1
         IF(GDDATA) THEN
            PREWAT(K)=(((RDATCY(K+42)+RDATCY(K+41))) *
     1                (ABS(PROFLV(K+22)-PROFLV(K+23)))/GCX200)*10.0
            TOTWAT = TOTWAT + PREWAT(K)
            IF(K.LE.8 .AND. K.GE.4) WAT300=WAT300+PREWAT(K)
            IF(K.GE.9 .AND. K.LE.12) WAT500=WAT500+PREWAT(K)
            IF(K.GE.13) WAT700=WAT700+PREWAT(K)
         ELSE
            PREWAT(K) = 0.0
         ENDIF
   70 CONTINUE
      ODFBUF(260) = NINT(TOTWAT * 128.0)
      ODFBUF(443) = NINT(WAT300 * 128.0)
      ODFBUF(444) = NINT(WAT500 * 128.0)
      ODFBUF(445) = NINT(WAT700 * 128.0)
C
C
C PACK THE LAYER PRECIPITABLE WATER(MM).
C
      WORD = 260
      DO LEVEL = 3, 17
         WORD = WORD + 1
         ODFBUF(WORD) = NINT(PREWAT(LEVEL) * 128.0)
      END DO
C
C PACK THE LAYER THICKNESS (M).
C
      CALL TKLAYR(LYRTHK,LAYERS)

      WORD = 290
      DO LEVEL = 1, 20
         WORD = WORD + 1
         ODFBUF(WORD) = NINT(LYRTHK(LEVEL))
      END DO
C
C PACK THE LAYER MEAN VIRTUAL TEMPERATURE (K).
C
      DUMOBS = 276
      DO 80 K = 1,STDLVL-1
         KK = K+1
         IF((GDDATA).AND.(.NOT. ISR4SPV(LYRTHK(KK)))) THEN
            LYRMVT(K) = LYRTHK(KK) / (GASCON *
     1            LOG(PROFLV(STDIDX(KK))/PROFLV(STDIDX(K))))
            IF(K.GE.5) ODFBUF(DUMOBS) = NINT(LYRMVT(K) * 64.0)
         ELSE
            LYRMVT(K) = R4SPV
         ENDIF
         IF(K.GE.5) DUMOBS = DUMOBS + 1
   80 CONTINUE
C
C PACK THE CLOUD TOP TEMPERATURE (K).
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,18)))
     1   ODFBUF(439)=NINT(HIRANC(II,JJ,18)*64.0)
C
C PACK THE CLOUD TOP PRESSURE (MB).
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,15))) ODFBUF(440)=NINT(HIRANC(II,JJ,15))
C
C IF THE CLOUD TOP PRESSURE IS ZERO OR LESS, SET THE CLOUD AMOUNT TO
C MISSING.
C
      IF (HIRANC(II,JJ,15).LE.0.0) HIRANC(II,JJ,19) = R4SPV
C
C PACK THE CALCULATED AEROSOL AMOUNT (PERCENT).
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,26)))
     1   ODFBUF(446)=NINT(HIRANC(II,JJ,26) * 100.0)

C
C PACK THE MISCELLANEOUS FLAG: 1 = SPOT SUCCESSFULLY PROCESSED.
C
      ODFBUF(33) = 1
C
C PACK THE FORECAST POTENTIAL TEMPERATURE.
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,11)))
     1    ODFBUF(427)=NINT(HIRANC(II,JJ,11)*64.0)
C
C PACK THE FORECAST SURFACE PRESSURE.
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,12)))
     1   ODFBUF(431)=NINT(HIRANC(II,JJ,12)*10.0)
C
C CALCULATE AND PACK THE CLOUD COMPARISON FLAG
C
      CLDCMP = I2SPV
      IF(.NOT. ISR4SPV(RETANC(S(I),J,4))
     1   .AND. (.NOT. ISR4SPV(HIRANC(II,JJ,15)))) THEN
         IF(RETANC(S(I),J,4).LE.16) THEN
C
C RETRIEVAL IS CLEAR OR NSTAR
C
            IF(HIRANC(II,JJ,15).GT.0.0) THEN
C
C CLOUD ALGORITHM SPOT VALUE SHOWS CLOUDS
C
               CLDCMP = 1
            ELSE
C
C CLOUD ALGORITHM SPOT VALUE SHOWS CLEAR
C
               CLDCMP = 3
            ENDIF
         ELSE
C
C RETRIEVAL IS CLOUDY
C
            IF(HIRANC(II,JJ,15).GT.0.0) THEN
C
C CLOUD ALGORITHM SPOT VALUE SHOWS CLOUDS
C
               CLDCMP = 0
            ELSE
C
C CLOUD ALGORITHM SPOT VALUE SHOWS CLEAR, SO SET FLAG TO 2 (SO-SO)
C
               CLDCMP = 2
            ENDIF
         ENDIF
      ENDIF
      ODFBUF(453) = CLDCMP
C
C PACK THE CLOUD PROCESSING PASS/FAIL FLAG
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,48)))
     +   ODFBUF(457) = NINT(HIRANC(II,JJ,48))
C
C PACK THE CLOUD QC FLAG
C 
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,47)))
     +   ODFBUF(490) = NINT(HIRANC(II,JJ,47))

C
C PACK THE IR/MW N* VALUE.
C
c     IF(.NOT. ISR4SPV(HIRANC(II,JJ,21)))
c    1   ODFBUF(311)=NINT(HIRANC(II,JJ,21)*1024.0)
C
C PACK THE AVHRR N* VALUE.
C
c     IF(.NOT. ISR4SPV(HIRANC(II,JJ,17)))
c    1   ODFBUF(312)=NINT(HIRANC(II,JJ,17)*1024.0)
C
C PACK THE N* FLAG.
C
c     IF(.NOT. ISR4SPV(HIRANC(II,JJ,24)))
c    1   ODFBUF(313)=NINT(HIRANC(II,JJ,24))
C
C PACK THE SEA SURFACE TEMPERATURE.
C
      ODFBUF(323)=NINT(HIRANC(II,JJ,10) * 64.0)
C
C PACK THE DERIVED SKIN TEMPERATURE (K).
C
      ODFBUF(324)=NINT(RDATCY(62) * 64.0)
C
C PACK THE FIRST GUESS TEMPERATURE PROFILE (K).
C
      WORD = 330
      DO LEVEL = 19, 60
         WORD = WORD + 1
         IF (.NOT. ISR4SPV(RETANC(S(I),J,LEVEL)))
     1      ODFBUF(WORD) = NINT(RETANC(S(I),J,LEVEL) * 64.0)
      END DO
C
C PACK THE FIRST GUESS WATER VAPOR MIXING RATIO.
C STORE VALUES FOR LEVELS 18 AND 19 AS MISSING VALUES.
C
       WORD = 372
       IC=PRFTLV+18
       DO K = 1,PRFQLV
          WORD = WORD + 1
          IIC = IC + K
          IF(.NOT. ISR4SPV(RETANC(S(I),J,IIC))) THEN
             FGWVMR(K) = ALOG(RETANC(S(I),J,IIC))
             ODFBUF(WORD) = NINT(FGWVMR(K) * 1024.0)
          END IF
c         ELSE
c            FGWVMR(K) = RETANC(S(I),J,IIC)
C         ENDIF
c         ODFBUF(WORD)=NINT(FGWVMR(K)*1024.0)
       END DO
C
C PACK THE FIRST GUESS RADIANCE TEMPERATURES (K).
C
      WORD = 391
      DO LEVEL = 80, 114
         WORD = WORD + 1
         IF(.NOT. ISR4SPV(RETANC(S(I),J,LEVEL)))
     1      ODFBUF(WORD) = NINT(RETANC(S(I),J,LEVEL) * 64.0)
      END DO
C
C PACK THE RETRIEVAL CLASS INDEX.
C
      ODFBUF(36) = RETCLS
C
C PACK THE SURFACE ELEVATION (M).
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,8)))ODFBUF(11)=NINT(HIRANC(II,JJ,8))
C
C PACK THE CLOUD AMOUNT.
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,19)))
     1   ODFBUF(441)=NINT(HIRANC(II,JJ,19)*100.0)
C
C PACK THE CLEAR EQUIVALENT BLACK BODY TEMPERATURE.
C
      DO 170 K=1,NUMCHN
         CLREQU(K) = HIRDAT(II,JJ,K)
  170 CONTINUE
C
C PACK THE GAC TEMPERATURES
C
      CLREQU(NUMCHN+1) = HIRDAT(II,JJ,NUMCHN+18)
      CLREQU(NUMCHN+2) = HIRDAT(II,JJ,NUMCHN+36)
      IF(.NOT. ISR4SPV(HIRDAT(II,JJ,NUMCHN+(3*18)))) THEN
         CLREQU(NUMCHN+3) = HIRDAT(II,JJ,NUMCHN+54)
      ELSE
         CLREQU(NUMCHN+3) = HIRDAT(II,JJ,NUMCHN+72)
      ENDIF
      CLREQU(NUMCHN+4) = HIRDAT(II,JJ,NUMCHN+90)
      CLREQU(NUMCHN+5) = HIRDAT(II,JJ,NUMCHN+108)
C-- HIRS
      WORD = 86
      DO LEVEL = 1,20
         WORD = WORD + 1
         IF(BADHIR(II,JJ,LEVEL).NE.1)THEN
            ODFBUF(WORD) = NINT(CLREQU(LEVEL) * 64.0)
         ELSE
            ODFBUF(WORD) = I2SPV
         ENDIF
      END DO
C-- AMSU-A AND GAC
      WORD = 106
      DO LEVEL = 21, 40
         WORD = WORD + 1
         ODFBUF(WORD) = NINT(CLREQU(LEVEL) * 64.0)
      END DO
CCLW
      I0CNT = 0
      DO IDX=87,126
         IF(ODFBUF(IDX).EQ.0) I0CNT = I0CNT + 1
      ENDDO
      IF(I0CNT.EQ.40) THEN
         PRINT *, 'ZERO BTEMP PROFILE - CLREQU:'
         PRINT *, CLREQU
         PRINT *, 'HIRDAT: '
         PRINT *, (HIRDAT(II,JJ,JDX),JDX=1,35)
      ENDIF
CCLW
C
C-- MHS SPOT NUMBER AT HIRS FOV

      IF (.NOT. ISR4SPV(HIRDAT(II,JJ,25)))
     1   ODFBUF(460) = NINT(HIRANC(II,JJ,25))
C
C-- MHS LIMB CORRECTED CHANNEL BRIGHTNESS TEMPERATURES
      WORD = 460
      DO LEVEL = 163,167
         WORD = WORD + 1
         IF (.NOT. ISR4SPV(HIRDAT(II,JJ,LEVEL)))
     1      ODFBUF(WORD) = NINT(HIRDAT(II,JJ,LEVEL) * 64.0)
      END DO

C-- MHS OBSERVED CHANNEL BRIGHTNESS TEMPERATURES
      WORD = 465
      DO LEVEL = 168, 172
         WORD = WORD + 1
         IF(.NOT. ISR4SPV(HIRDAT(II,JJ,LEVEL)))
     1      ODFBUF(WORD) = NINT(HIRDAT(II,JJ,LEVEL) * 64.0)
      END DO
C
C PACK THE TOTAL OZONE AND THE OZONE FLAG.
C
C THE OZONE FLAG FLAGS SPOTS WHERE THERE IS OZONE AND CLOUD TOP
C PRESSURES ARE ABOVE 350MB. (ASA, 10/09/02)
C
C SET OZONE TO MISSING WHERE CLOUD TOP PRESSURE IS LESS THAN
C 350 MB. (WR #286, 12/27/05)
C
      ODFBUF(442) = I2SPV
      IF(.NOT. ISR4SPV(TOZDAT(S(I),J,1)))
     1    ODFBUF(442)=NINT(TOZDAT(S(I),J,1))
      IF(.NOT. ISR4SPV(TOZDAT(S(I),J,1)) .AND. 
     >   .NOT. ISR4SPV(HIRANC(II,JJ,15)) .AND.
     >   .NOT. ISR4SPV(HIRANC(II,JJ,19)) .AND.
     >   HIRANC(II,JJ,19) .GT. 0)
     >  THEN
         IF(HIRANC(II,JJ,15) .LT. 350.0) THEN
          ODFBUF(471)= 1
          ODFBUF(442)= I2SPV
         ELSE
          ODFBUF(471)= 0
         ENDIF
      ELSE
       ODFBUF(471) = 0
      ENDIF
ccft
c  Overwrite the Ozone Quality Flag with a flag for
c  any retrieval where QCHECK changed the retrieved
c  mixing ratio.
c
      ODFBUF(471) = NINT(RETANC(S(I),J,9))

C
C PACK THE FORECAST RELATIVE HUMIDITY
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,20)))
     1   ODFBUF(428)=NINT(HIRANC(II,JJ,20)*256.0)
C
C CALL GETSML TO GET SURFACE MODEL LEVEL. CALL STBDVR TO GET
C STABILITY DEPARTURES.
C
ccft  CALL GETSML(RDATCY(62),HIRANC(II,JJ,10),HIRANC(II,JJ,8),SML)
C
C PACK THE SURFACE MODEL LEVEL.
C
      SML = NINT(RETANC(S(I),J,7))
      ODFBUF(325) = SML
      ODFBUF(433) = I2SPV
      ODFBUF(434) = I2SPV
      ODFBUF(435) = I2SPV
C
C
      IF(STABTEST(6).EQ.1) THEN
         CALL STBDVR(STB1ST,FRMTIM,SYNCTI,OBSQUL,GEOPOT,
     1               HIRANC(II,JJ,1),HIRANC(II,JJ,2),DELTAH,
     2               DELTAL,DELTAS,DELTAT,FCINCR,ERSTAT)
C
C   IF STABILITY RETURNS A NON-ZERO CONDITION CODE, TURN FURTHER STABILITY PROCESSING
C   OFF AND WRITE A KIDS MESSAGE.  THIS WILL NOT STOP FURTHER SPGDVR PROCESSING.
C
         IF(ERSTAT.NE.0) THEN
            STABTEST(6) = 0
            SUBNAM='SPGDVR'
            KMSG='STABILTY PROCESSING TURNED OFF PER STBDVR ERROR.'
            CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
            ERSTAT=0
         ENDIF
C
C PACK STABILITY DEPARTURE.
C
         IF(.NOT. ISR4SPV(DELTAS)) ODFBUF(433) = NINT(DELTAS * 512.0)
C
C PACK LOWER STABILITY DEPARTURE.
C
         IF(.NOT. ISR4SPV(DELTAL)) ODFBUF(434) = NINT(DELTAL * 512.0)
C
C PACK UPPER STABILITY DEPARTURE.
C
         IF(.NOT. ISR4SPV(DELTAH)) ODFBUF(435) = NINT(DELTAH * 512.0)
C
C PACK THE TIME DIFFERENCE (SAT -FCST).
C
         ODFBUF(436) = DELTAT
C
C PACK STABILITY FORECAST INCREMENT.
C
         ODFBUF(437) = FCINCR
      ENDIF
C
C PACK THE WATER VAPOR CORRECTED HIRS CHANNEL 8
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,23)))
     1   ODFBUF(327)=NINT(HIRANC(II,JJ,23)*64.0)
C
C PACK THE NMC FORECAST SURFACE TEMPERATURE
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,22)))
     1   ODFBUF(429)=NINT(HIRANC(II,JJ,22)*64.0)
C
C PACK THE ADJUSTED NMC FORECAST SURFACE PRESSURE
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,12)))
     1   ODFBUF(430)=NINT(HIRANC(II,JJ,12)*10.0)
C
C PACK THE BIAS CORRECTED (SHRUNK) TEMPERATURES
      WORD = 161
      DO LEVEL =1,19
         WORD = WORD + 1
         IF(BADHIR(II,JJ,LEVEL).NE.1 .AND.
     +      (.NOT. ISR4SPV(NLCHDAT(II,JJ,LEVEL))))THEN
            ODFBUF(WORD) = NINT(NLCHDAT(II,JJ,LEVEL) * 64.0)
         ELSE
            ODFBUF(WORD) = I2SPV
         ENDIF
      END DO
C
C NLCHDAT(II,JJ,20) CONTAINS ORBIT NODE. USE THE HIRS CHANNEL 20
C DATA IN THE HIRDAT BUFFER FOR WRITING TO THE RODF.
C
      IF (BADHIR(II,JJ,20).NE.1 .AND. 
     +   (.NOT. ISR4SPV(HIRDAT(II,JJ,20)))) THEN
         ODFBUF(181) = NINT(HIRDAT(II,JJ,20) *64.0)
      ELSE
         ODFBUF(181) = I2SPV
      END IF
C-- AMSU-A
      WORD = 181
      DO LEVEL = 1, 15
         WORD = WORD + 1
         IF ((.NOT. ISR4SPV(NLCADAT(AMASPT(II,JJ),AMALNE(II,JJ),LEVEL))) .AND.
     1      (BADAMA(AMASPT(II,JJ),AMALNE(II,JJ),LEVEL).NE.1))THEN
            ODFBUF(WORD) = NINT(NLCADAT(AMASPT(II,JJ),AMALNE(II,JJ),LEVEL) * 64.0)
         ELSE
            ODFBUF(WORD) = I2SPV
         END IF
      END DO
C
C PACK THE SOLAR AZIMUTH ANGLE.
C
      IF(.NOT. ISR4SPV(HIRANC(II,JJ,4)))
     1   ODFBUF(37)=NINT(HIRANC(II,JJ,4)*128.0)
C
C PACK THE HIRS SPOT NUMBER FOR THIS LINE (1-56).
C
      DUMOBS = NINT(RETANC(S(I),J,1))
      IF(ISR4SPV(DUMOBS)) DUMOBS = I2SPV
      IF(DUMOBS.GT.56.AND.DUMOBS.NE.I2SPV) THEN
         DUMOBS = MOD(DUMOBS,56)
         IF(DUMOBS.EQ.0) DUMOBS = 56
      ENDIF
      ODFBUF(38) = DUMOBS
C
C  PACK THE NODE FOR THIS SPOT.
C
      ODFBUF(39) = NINT(OBSHIR(II,JJ,20))
C
C PACK THE POTENTIAL TIME MINUS NCEP FORECAST TIME
C
      ODFBUF(432) = NINT(DELTIME * 100.0)
C
C PACK THE VALID FORECAST TIME (YYMM,DDHH)
C
      ODFBUF(20) = FCSTYM(1)
      ODFBUF(21) = FCSTYM(2)
C
C  PACK THE SURFACE TEMPERATURE ESTIMATES FOR HIRS 8, 18, 19.
C
      IF(.NOT. ISR4SPV(ESTH8(II,JJ)))  ODFBUF(328)=NINT(ESTH8(II,JJ)*64.0)
      IF(.NOT. ISR4SPV(ESTH18(II,JJ))) ODFBUF(329)=NINT(ESTH18(II,JJ)*64.0)
      IF(.NOT. ISR4SPV(ESTH19(II,JJ))) ODFBUF(330)=NINT(ESTH19(II,JJ)*64.0)
C
C  PACK THE OLR AND LAYER COOLING RATES - NOTE THAT LAYER COOLING
C  RATE FOR 240 TO 10 MB (LAYER 4) IS STORED FIRST.
C
      ODFBUF(448) = NINT(OLR(II,JJ) * 10.0)
      ODFBUF(449) = NINT(LC4(II,JJ) * 1000.0)
      ODFBUF(450) = NINT(LC3(II,JJ) * 1000.0)
      ODFBUF(451) = NINT(LC2(II,JJ) * 1000.0)
      ODFBUF(452) = NINT(LC1(II,JJ) * 1000.0)
C
C  PACK THE VALUE OBTAINED FROM FILTER TESTS
C
      ODFBUF(43) = FILTOT(II,JJ)
C
C  PACK THE LIBRARY SEARCH CLOSENESS MEASURE
C
      IF(.NOT. ISR4SPV(PHO(S(I),J))) ODFBUF(454) = NINT(PHO(S(I),J))
C
 200  RETURN
      END
