      SUBROUTINE RETODB(RETDAT, RETANC, TOZDAT, AMASPT, AMALNE, CNFLIM,
     1                  FRMTIM, ODFREC, PRFLAG, OBSHIR, DELTIME, FCSTYM,
     2                  OLR,LC1,LC2,LC3,LC4,STB1ST,HMINBT,HMAXBT,
     3                  AMINBT,AMAXBT,OQCHEKD,OQBAD,ERSTAT)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- RETODB
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY    
C
C   VERSION- 1.0    DATE- 04/01/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.
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 AND FROM CALLS TO RETOD1 AND
C                   RETOD2.  INCLUDE PARAMETER FILE /RTVPARM/.  CHANGE
C                   DIMENSION RDATCY(  ) TO RDATCY(RDATEL).
C   VERSION- 2.2    DATE- 06/13/94   PROGRAMMER- JEFF MANNING (SMSRC)
C          XATOV-20 INCLUDE PARAMETER FILE /RETPARM/ TO ACCOMODATE NEW
C                   DIMENSION (PARAMETERS).
C   VERSION- 2.3    DATE- 04/14/95   PROGRAMMER- LI LU (SMSRC)
C        ATOVS-95   CALLS TO PACBUF ARE REMOVED. CALLS TO STGASG
C                   AND SPGSI4 AND ASSOCIATED VARIABLES ARE REMOVED
C                   AND REPLACED WITH CALLS TO SPGFAU. VARIABLES FOR
C                   MSU ARE CHANGED TO AMSU-A. GDDATA IS SET FALSE
C                   IF ALL ELEMENTS OF RDATCY ARE MISSING, RATHER
C                   THAN JUST 1.
C        ATOVS-107  IN CALL TO NSSORT, 'RDATEL' IS CHANGED TO 'RNANC'.
C   VERSION- 2.4    DATE- 07/27/95   PROGRAMMER- LI LU (SMSRC)
C        ATOVS-151  ARRAY FOR FRAME TIME IS ADDED AS AN INPUT ARGUMENT
C		    AND IS ALSO ADDED TO RETOD2 CALL. VALUES OF SOUNDING
C		    TIME, OBSERVATION QUALITY FLAG AND GEOPOTENTIAL
C		    HEIGHT ARE ADDED TO CALLS FOR RETOD1 AND RETOD2.
C   VERSION- 2.5    DATE- 12/11/95   PROGRAMMER- CARLA NEIGH (SMSRC)
C       ATOVS-287:  CALLING ARGUMENT ADDED TO RETOD1 FOR CLASS INDEX.
C   VERSION- 3.0    DATE- 06/19/96   PROGRAMMER- ALEX POZDNYAK (FDC)  
C   VERSION- 3.1    DATE- 01/31/97   PROGRAMMER- LEAH WILSON (HSTX)
C                   INCREASE THIRD DIMENSION OF TOZDAT TO 2 TO ALLOW
C                   FOR STORAGE OF SULFUR DIOXIDE. 
C   VERSION- 4.0    DATE- 09/02/97   PROGRAMMER- LEAH WILSON
C                   REPLACE ODB WITH ORBITAL DATA FILE (FLAT FILE). REMOVE
C                   RETOD$.  ADD /OZONE/ TO ALLOW OZONE ON/OFF FLAG.
C   VERSION- 4.1    DATE- 05/04/98   PROGRAMMER- BRIAN BARIL (RSTX)
C                   ADDED STB1ST FROM SPGDVR TO THE CALL TO RETOD2.
C                   RENAMED GEOHGT TO AVOID CONFLICT WITH COMMON 
C                   STBGEO.
C   VERSION- 4.2    DATE- 04/19/99   PROGRAMMER- A.ALLEGRINO (RAYTHEON)
C                   ADDED CHECK OF GROSS TEMPERATURES FOR HIRS AND AMSU-A.
C                   FLAGS ARE SET AND PASSED INTO RETOD1.
C   VERSION- 4.3    DATE- 05/04/99   PROGRAMMER- BRIAN BARIL (R-ITSS)
C                   REMOVED CALL TO OZOUT AND ALL I/O TO THE OZONE FILE
C   VERSION- 4.4    DATE- 05/05/99   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C                   INCREASED OBSHIR DIMENSION FROM 20 TO 35 TO ACCOUNT
C                   FOR THE AMSU-A AT THE HIRS LOCATIONS.
C   VERSION- 4.5    DATE- 08/06/99   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C                   MOVED INITIALZATION OF THE ODF BUFFER (ODFBUF) SO
C                   THAT IT IS INITIALIZED FOR EVERY GRID POINT NOT JUST
C                   FOR EVERY FRAME.
C   VERSION- 4.6    DATE- 08/17/99   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C                   IF PRECIPITATION CONTAMINATED DO NOT PLACE ONTO THE
C                   RODF.
C   VERSION- 4.7    DATE- 08/19/99   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C                   ONLY CERTAIN PARAMETERS WILL BE SCREENED FOR PRECIP
C                   FOR PLACEMENT ON THE RODF.
C   VERSION- 4.8    DATE- 03/21/00   PROGRAMMER- L. WILSON (RAYTHEON)
C                   ADD /FRAME1/, IN ORDER TO DETERMINE WHICH LINES
C                   OF THE FIRST FRAME SHOULD BE SCREENED FOR GROSS
C                   ERRORS.
C   VERSION- 4.9    DATE- 05/01/00   PROGRAMMER- M.P. FERGUSON (RITSS)
C                   CORRECT CALCULATION OF HIRS FRAME SPOT NUMBER BY
C                   ADDING A +0.01 OFFSET TO PREVENT THE INTEGER TRUN-
C                   CATION FROM RESULTING IN TOO SMALL A VALUE.
C   VERSION-4.10    DATE- 04/04/03   PROGRAMMER- F. TILLEY (RAYTHEON)
C                   SINCE CHKSUP, CALLED BY RETOD1, CAN CHANGE THE
C                   RETRIEVED TEMPERATURE PROFILE USED BY CLDTOP,
C                   COPY THE TEMPERATURE PROFILE RETURNED BY RETOD1
C                   BACK INTO THE RETRIEVAL DATA BUFFER (WR #215).
C   VERSION-4.11    DATE- 07/03/03   PROGRAMMER- F. TILLEY (RAYTHEON)
C                   ADDED CALLING ARGUMENTS OQCHEKD AND OQBAD TO TRACK
C                   COUNTS OF RETRIEVALS CHECKED FOR OBSERVATON QUALITY
C                   AND RETRIEVALS WITH BAD OBS QUALITY (WR #234) 
C   VERSION-5.0     DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C                   Changed ODFBUF from INTEGER to I*2. Replaced
C                   R4SPV equivalence check with ISR4SPV function.
C
C   FUNCTION-  DUE TO THE AMOUNT OF DATA PACKED INTO A RETRIEVAL
C   GRID IT WAS DECIDED TO BREAK THIS SUBROUTINE INTO THREE PARTS
C   TO BETTER CONFORM WITH OSDPD STANDARDS.
C
C   THIS SUBROUTINE ALONG WITH RETOD1 AND RETOD2 WRITE OUT A FRAME
C   OF RETRIEVAL GRID DATA TO THE ORBITAL DATA FILE A CYCLE(SPOT)
C   AT A TIME.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O  UNIT   FUNCTIONAL DESCRIPTION
C     ------------------------ ---  ----   ----------------------
C     ......./ODF               O  ODBUNT  ORBITAL DATA FILE
C     .../KIDS                  O  KUNIT   KIDS MESSAGE FILE.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  DESCRIPTION
C     --------          ---- ---  ---------------------------------
C     RETDAT            REAL I/O  ARRAY OF DIMENSION (RTVPARM:RNGRDP,
C                                 RTVPARM:RTOTLN,RTVPARM:RDATEL), WHICH
C                                 HOLDS ONE DATA FRAME OF 256 SECONDS
C                                 OF RETRIEVAL GRID CYCLES IN BRIGHTNESS
C                                 TEMPERATURES(K).
C     RETANC            REAL I/O  ARRAY OF DIMENSION (RTVPARM:RNGRDP,
C                                 RTVPARM:RTOTLN,RTVPARM:RNANC) WHICH
C                                 HOLDS THE ANCILLARY RETRIEVAL GRID
C                                 INFORMATION.
C     TOZDAT            REAL   I  ARRAY OF DIMENSION (RTVPARM:RNGRDP,
C                                 RTVPARM:RTOTLN,2) WHICH HOLDS ONE
C                                 DATA FRAME OF 256 SECONDS OF OZONE
C                                 AMOUNT AND SULFUR DIOXIDE VALUES.
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     CNFLIM            INT    I  THE SPOT CONFIDENCE LIMIT.
C     FRMTIM            INT    I  ARRAY (DIM:3) CONTAINING FRAME TIME.
C     ODFREC            INT   I/O ODF RECORD NUMBER.  
C     PRFLAG            LOG    I  POLAR REDUNDANT FLAGS FOR EACH GRID PT.
C     OBSHIR            REAL   I  OBSERVED (NON-LIMB CORRECTED) 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         REAL   I  OUTGOING LONGWAVE RADIATION AND 4 LAYER
C                                 COOLING RATES FOR EACH HIRS SPOT.
C     HMINBT,HMAXBT     REAL   I  HIRS TEMPERATURE LIMITS (DIM: 20)
C     AMINBT,AMAXBT     REAL   I  AMSU-A TEMPERATURE LIMITS (DIM: 15)
C     OQCHECK           INT   I/O COUNT OF RETRIEVALS CHECKED FOR OBS
C                                 QUALITY.
C     OQBAD             INT   I/O COUNT OF RETRIEVALS WITH BAD OBS
C                                 QUALITY.
C     ERSTAT            INT    O  NORMALLY SET TO 0 BUT IF AN ERROR
C                                 OCCURS AN ERROR STATUS CODE IS SET.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'MONSTA'
      INCLUDE 'RETPARM'
      INCLUDE 'RTVPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'OZONE'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPGUNT'
      INCLUDE 'FRAME1'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     MISSNG            LOG   LOGICAL SET TO TRUE IF A SCAN LINE IS
C                             NOT TO BE PROCESSED(NO CLASS INDEX).
C     GDDATA            LOG   LOGICAL SET TO TRUE IF GOOD RETRIEVAL
C                             DATA IS PRESENT FOR A GRID POINT.
C     I,J,K             INT   LOOP CONTROL INDICES.
C     STAT              INT   ERROR STATUS RETURNED BY FMU'S.
C     S                 INT   VECTOR OF RTVPARM:RNGRDP SPOT NUMBERS TO
C                             BE SORTED.
C     L                 INT   VECTOR OF RTVPARM:RNGRDP LINE NUMBERS TO
C                             BE SORTED.
C     DUMMY             INT   VECTOR OF DIMENSION 2 HOLDING THE PLACE
C                             OF THE RECORD AND BYTE POINTERS.
C     RDATCY            REAL  VECTOR OF LENGTH RTVPARM:RDATEL,
C                             RETPARM:PRFTLV TOVS TEMPERATURE
C                             PROFILE(K), RETPARM:PRFQLV H2O VAPOR
C                             MIXING RATIOS ALOG(G/KG) AND ONE
C                             SURFACE TEMPERATURE (K).
C     SPOTNR            INT   SPOT NUMBER WITHIN THE DATA FRAME.
C     JJ                INT   INDEX USED FOR THE HIRS LINE NUMBER.
C     REALSP            REAL  REAL EQUIVALENT OF INSPARM:HNSPOT.
C     II                INT   INDEX USED FOR THE HIRS SPOT NUMBER.
C     LAYERS            REAL  VECTOR OF RETPARM:PRFTLV CONTAINING LAYER
C                             THICKNESS AT THE RETPARM:PRFTLV TOVS
C                             LEVELS (DERIVED IN RETOD1).
C     MASK              INT   MASK TO KEEP ONLY THE UPPER 2 BYTES.
C     REALCL            REAL  HOLDS THE RETRIEVAL CLASS INDEX AND
C                             IS EQUIVALENCED TO RETCLS.
C     RETCLS            INT   INTEGER TO HOLD THE RETRIEVAL CLASS
C                             VALUE EQUIVALENCED TO REALCL.
C     SYNCTI            INT   ARRAY (DIM:3) CONTAINING SOUNDING TIME
C                             OF THE SCAN SPOT.
C     OBSQUL	        INT   OBSERVATION QUALITY FLAG OF THE SPOT.
C     GEOPOT/GEOGHT    REAL  ARRAY (RETPARM:PRFTLV) CONTAINS
C			      GEOPOTENTIAL HEIGHTS.
C
C     C4TEMP           CH*4   TEMPOARARY FOR WRITING TO KIDS.
C     C6TEMP           CH*6   TEMPOARARY FOR WRITING TO KIDS.
C     KMSG            CH*100  KIDS MESSAGE BUFFER.
C     ODFBUF           I*2    ODF RECORD BUFFER.
C     C2ODF            CH*1   CHARACTER ODF RECORD BUFFER.
C     STB1ST           LOG    SET TO TRUE FOR FIRST CALL TO STBDVR.
C     HFLAG,AFLAG      INT    HIRS & AMSU-A GROSS TEMPERATURE FLAGS
C
C   SUBPROGRAMS CALLED-  NSSORT, RETOD1, RETOD2, KIDS
C
C   FUNCTIONS USED- IOR, ISHFT, MOD, REAL
C
C   EXIT STATES-
C       ERSTAT =  0; INDICATING NO ERRORS FOUND.
C       ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
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     ERSTAT, CNFLIM, FRMTIM(3), ODFREC
      INTEGER*2   ODFBUF(BUFSIZE)
      INTEGER     AMASPT(HNSPOT,HTOTLN), AMALNE(HNSPOT,HTOTLN)
      INTEGER     FCSTYM(2), OQCHEKD, OQBAD
      REAL        RETDAT(RNGRDP,RTOTLN,RDATEL)
      REAL        RETANC(RNGRDP,RTOTLN,RNANC), DELTIME
      REAL        TOZDAT(RNGRDP,RTOTLN,2), OBSHIR(HNSPOT,HTOTLN,35)
      REAL        OLR(HNSPOT,HTOTLN), LC1(HNSPOT,HTOTLN)
      REAL        LC2(HNSPOT,HTOTLN), LC3(HNSPOT,HTOTLN),AMAXBT(15)
      REAL        LC4(HNSPOT,HTOTLN),HMINBT(20),HMAXBT(20),AMINBT(15)
C
      CHARACTER*1 C2ODF(BUFSIZE*2)
      CHARACTER*4 C4TEMP
      CHARACTER*6 C6TEMP
      CHARACTER*100 KMSG
      INTEGER     SPOTNR, STAT, S(RNGRDP), SYNCTI(3)
      INTEGER     I, J, K, RETCLS, IOERR, OBSQUL, HFLAG, AFLAG
      INTEGER     DUMMY(2), MASK, II, JJ, L(RNGRDP)
      INTEGER     IDX, IHCHAN, KK, LL, IACHAN, ISWCH, IERR
      REAL        RDATCY(RDATEL), REALSP, REALCL, LAYERS(PRFTLV)
      REAL        GEOHGT(PRFTLV),GEOPOT(PRFTLV)
      LOGICAL     MISSNG, GDDATA, FORCE, PRFLAG(RNGRDP,RTOTLN)
      LOGICAL     STB1ST, ISR4SPV
C
      EQUIVALENCE (RETCLS,REALCL)
      EQUIVALENCE (C2ODF, ODFBUF)
C
      INCLUDE 'SPVAL'
C
C**********************************************************************
C
      DATA MASK /Z'FF00'/
C
C     INITIALIZE LOCAL VARIABLES
C
      SPOTNR = 0
      IOERR = 0
      REALSP = REAL(HNSPOT)
      MISSNG = .TRUE.
      FORCE = .FALSE.
      NFRRET(FRMNBR) = 0
C
C THE RETRIEVAL GRID BUFFERS CONTAIN RTVPARM:RNGRDP GRID POINTS,
C RTVPARM:RTOTLN LINES AND RTVPARM:RDATEL ELEMENTS.
C
C BEGIN LOOP OF ALL LINES.
C
      DO 200 J = 1, RTOTLN
C
C THE FIRST ELEMENT OF THE RETANC BUFFER IS THE HIRS SPOT THAT
C IS ASSOCIATED WITH THE RETRIEVAL. AS THESE SPOTS ARE NOT STORED
C IN ASCENDING ORDER (PER LINE), A SORT OF THE HIRS SPOTS, PER LINE,
C IS PERFORMED TO OUTPUT THE SPOTS TO THE ODB IN ASCENDING ORDER.
C
         DO 20 K = 1,RNGRDP
            S(K) = K
            L(K) = J
   20    CONTINUE
         CALL NSSORT(RNGRDP,RTOTLN,RNANC,RETANC,S,L,1,RNGRDP)
C
C LOOP THROUGH ALL GRID POINTS.
C
         DO 190 I = 1, RNGRDP
C
C  RE-INITIALIZE THE ODF BUFFER
C
          DO IDX=1,BUFSIZE
            ODFBUF(IDX) = I2SPV
          END DO
C
C COUNT EACH GRID POINT FOR REFERENCE IN THE FRAME.
C
             SPOTNR = SPOTNR + 1
C
C INITIALIZE THE RETRIEVAL GRID BUFFER.
C
             IF(MISSNG) GOTO 30
             MISSNG = .TRUE.
C
C PACK THE GRID RETRIEVED TEMPERATURES AT THE RETPARM:PFRTLV TOVS
C PRESSURE LEVELS (K), THE NATURAL LOG OF RETRIEVED WATER VAPOR
C MIXING RATIOS AT THE BOTTOM RETPARM:PFRQLV TOVS PRESSURE LEVELS (G/KG)
C AND THE RETRIEVED SURFACE TEMPERATURE (K).
C
C
   30        DO 40 K = 1, RDATEL
                RDATCY(K)=RETDAT(S(I),J,K)
   40        CONTINUE
C
C COMPUTE THE HIRS LINE AND SPOT NUMBER FROM THE HIRS SPOT
C NUMBER STORED IN RETANC(I,J,1) UNLESS THE HIRS NUMBER IS MISSING,
C IN WHICH CASE THE RETRIEVAL WILL BE SKIPPED, AS NONE WAS DONE.
C
             IF (ISR4SPV(RETANC(S(I),J,1))) GOTO 190
             JJ = (RETANC(S(I),J,1) / HNSPOT) + 1.01
             II = MOD(RETANC(S(I),J,1),REALSP)
             IF(II .EQ. 0) THEN
                JJ = JJ - 1
                II = HNSPOT
             ENDIF
C
C CHECK TEMPERATURE TO SEE IF THEY ARE WITHIN THE GROSS TEMPERATURE
C LIMITS FOR HIRS AND AMSU-A. IF ALL ARE FINE THEN THE FLAG WILL BE SET
C TO 0, BUT IF ANY FAIL, THE FLAG WILL BE SET TO 1. THESE FLAGS WILL BE
C USED TO SET THE OBSERVATIONAL QUALITY IN RETOD1.  FOR THE FIRST FRAME,
C DO NOT CHECK THOSE LINES IN THE HIRDAT AND AMADAT BUFFER THAT WERE
C SKIPPED.
C
            HFLAG = 0
            AFLAG = 0
            IF(FRMNBR.EQ.1.AND.JJ.LT.SKIPHIR) GO TO 44
            DO 43 IHCHAN =1,20
               IF(HIRDAT(II,JJ,IHCHAN) .GT. HMAXBT(IHCHAN) .OR.
     1         HIRDAT(II,JJ,IHCHAN) .LT. HMINBT(IHCHAN)) THEN
                  HFLAG=1
C                  WRITE(6,*) 'hflag is set to 1 for chan ', IHCHAN,
C     1                       ' max value: ',HMAXBT(IHCHAN),
C     2                       ' min value: ',HMINBT(IHCHAN),
C     3                       ' actual value: ',HIRDAT(II,JJ,IHCHAN)
               ENDIF
   43       CONTINUE

   44       KK=AMASPT(II,JJ)
            LL=AMALNE(II,JJ)
            IF(FRMNBR.EQ.1.AND.LL.LT.SKIPAMA) GO TO 46
            DO 45 IACHAN =1,15
               IF(AMADAT(KK,LL,IACHAN) .GT. AMAXBT(IACHAN) .OR.
     1         AMADAT(KK,LL,IACHAN) .LT. AMINBT(IACHAN)) THEN
                  AFLAG=1
C                  WRITE(6,*) 'aflag is set to 1 for chan ', IACHAN,
C     1                       ' max value: ',AMAXBT(IaCHAN),
C     2                       ' min value: ',AMINBT(IACHAN),
C     3                       ' actual value: ',AMADAT(KK,LL,IACHAN)
               ENDIF
   45       CONTINUE

C
C CHECK THE RETRIEVAL CLASS INDEX TO SEE IF THIS GRID POINT SHOULD
C BE WRITTEN TO THE ODB. A REAL*4 MISSING VALUE INDICATES NO
C RETRIEVAL WAS MADE, A 0 INDICATES A RETRIEVAL COULD NOT BE MADE
C BUT THE ANCILLARY GRID POINT INFORMATION SHOULD BE WRITTEN TO
C THE ODB, ANY OTHER VALUE INDICATES THE RETRIEVAL CLASS IN THE FIRST
C TWO BYTES AND THE SECOND TWO BYTES ARE THE FIRST PRINCIPLE COMPONENT
C SCORE INDEX.
C
   46        IF(.NOT. ISR4SPV(RETANC(S(I),J,5))) THEN
                MISSNG = .FALSE.
                IF(RETANC(S(I),J,5) .NE. 0.0) THEN
                   GDDATA = .FALSE.
                   NFRRET(FRMNBR) = NFRRET(FRMNBR) + 1
C
C WE NEED TO CHECK THE DATA BUFFER TO ENSURE IT IS NOT ALL MISSING
C VALUES. NOTE: IF ONE DATA ELEMENT IS A VALID VALUE, ALL ARE VALID.
C
                   DO 50 K = 1, RDATEL
                      IF(.NOT. ISR4SPV(RDATCY(K))) THEN
                         GDDATA = .TRUE.
                         GO TO 60
                      END IF
  50               CONTINUE
  60               CONTINUE
C
C                  REALCL IS EQUIVALENCED TO RETCLS, MASK OUT THE
C                  SECOND TWO BYTE VALUES
C
                   REALCL = RETANC(S(I),J,5)
                   RETCLS = IOR(RETCLS,MASK)
                   RETCLS = ISHFT(RETCLS,-16)
                ELSE
                   GDDATA = .FALSE.
                   RETCLS = I2SPV
                ENDIF
             ELSE
                RETCLS = I2SPV
             ENDIF
C
             IF(J .EQ. RTOTLN .AND. I .EQ. RNGRDP) THEN
                FORCE = .TRUE.
             ENDIF
C
             IF(MISSNG) THEN
                GDDATA = .FALSE.
                GOTO 190
             ENDIF
C
C PACK THE ODF RECORD TYPE AND FRAME NUMBER
C
             ODFBUF(1) = 2
             ODFBUF(3) = FRMNBR
C
C PACK THE STARTING AND ENDING ORBIT NUMBER.
C
             ODFBUF(4) = SORB(1)
             ODFBUF(5) = EORB(1)
C
C PACK THE RETRIEVED MIXING RATIO ADJUSTMENT FLAG
C
             ODFBUF(6) = NINT(RETANC(S(I),J,10))
C
C PACK THE FOV_CNTR NUMBER, IN REFERENCE TO THE DATA FRAME.
C
             ODFBUF(23) = SPOTNR
C
C PACK THE CONFIGURATION ID (TEST OR PRODUCTION VERSION).
C
             ODFBUF(32) = 1
C
C THE REST OF THE GRID DATA WILL BE PACKED INTO THE FILE WORK SPACE
C IN RETOD1 AND RETOD2. INCREMENT RECORD NUMBER IN ODF PRIOR TO
C RETOD1/RETOD2 CALLS TO CORRECTLY IDENTIFY LOCATION OF ANY I/O 
C ERRORS.
C
             ODFREC = ODFREC + 1

             CALL RETOD1(RETANC,ODFBUF,I,J,II,JJ,RETCLS,RDATCY,S,
     1                   GDDATA,LAYERS,AMASPT,AMALNE,CNFLIM,
     2                   SYNCTI,OBSQUL,GEOHGT,PRFLAG,HFLAG,AFLAG,
     3                   ERSTAT)
             IF(ERSTAT .NE. 0) GO TO 900
C
C TRACK THE NUMBER OF RETRIEVALS CHECKED FOR OBSERVATION QUALITY
C AND THE NUMBER OF RETRIEVALS WITH BAD OBS QUALITY.
C
             OQCHEKD = OQCHEKD + 1
             IF (OBSQUL.NE.0) OQBAD = OQBAD + 1
C
C COPY THE RETRIEVAL TEMPERATURE ARRAY RETURNED BY RETOD1 BACK
C INTO THE RETRIEVAL DATA BUFFER (WR #215)
C
             DO K = 1, RDATEL
                RETDAT(S(I),J,K) = RDATCY(K)
             END DO
ccft
             DO ISWCH=1,PRFTLV
                GEOPOT(ISWCH)=GEOHGT(ISWCH)
             ENDDO

             CALL 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)
             IF(ERSTAT .NE. 0) GO TO 900
C
C  TEST FOR PRECIPITATION CONTAMINATION ON SELECT PARAMETERS (EG., TEMPERATURE)
C  IF PRECIPITATION CONTAMINATED, SET THESE PARAMETERS TO MISSING.
C
ccft [commented out for testing purposes 13 April 2011]
c            DO IDX=1,500
c             IF((IDX.GE.45 .AND. IDX.LE.86) .OR.
c    >           (IDX.GE.197 .AND. IDX.LE.322) .OR.
c    >           (IDX.EQ.326) .OR. (IDX.EQ.327) .OR.
c    >           (IDX.GE.331 .AND. IDX.LE.426) .OR.
c    >           (IDX.GE.433 .AND. IDX.LE.435) .OR.
c    >           (IDX.GE.443 .AND. IDX.LE.445)) THEN
c                 IF(RETANC(S(I),J,8) .EQ. 1) ODFBUF(IDX) = I2SPV
c             ENDIF
c            END DO

C
             WRITE(UNIT=ODBUNT,REC=ODFREC,IOSTAT=IOERR) C2ODF
             IF(IOERR.NE.0) THEN
                PRINT *,' IOSTAT WRITING TO RODF: ',IOERR
                GO TO 900
             END IF
C
  190     CONTINUE
C
  200 CONTINUE
      GO TO 1000
C
900   KMSG(1:51)='* FATAL ERROR * Writing Record        of RODF FILE'
      KMSG(52:100)=' for frame     '
      WRITE(C6TEMP,'(I6)') ODFREC
      KMSG(32:37) =  C6TEMP
      WRITE(C4TEMP,'(I4)') FRMNBR
      KMSG(63:66) = C4TEMP
      IF(IOERR.NE.0) THEN
         KMSG(68:80) = 'with IOSTAT = '
         WRITE(C4TEMP,'(I4)') IOERR
         KMSG(81:84) = C4TEMP
      ENDIF
      PRINT *,KMSG
      SUBNAM='SPGDVR'
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
C  SINCE SOMETHING WAS WRONG WITH THIS RECORD, DECREMENT THE RECORD
C  NUMBER COUNTER FOR THIS FILE
C
      ODFREC = ODFREC - 1
      ERSTAT = 99
C
 1000 RETURN
      END
