      PROGRAM SPGDVR
      IMPLICIT NONE
C***********************************************************************
C    NAME- SPGDVR
C
C    LANGUAGE- VS FORTRAN     TYPE- MAIN         COMPUTER- CRAY J916
C
C    VERSION- 1.0    DATE- 5/2/91     PROGRAMMER- DAVE MOREL(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- 05/18/94   PROGRAMMER- JEFF MANNING (SMSRC)
C           XATOV-5: REMOVE RNGRDP, RTOTLN, RDATEL, RNANC FROM THE
C                    CALL TO RETODB.
C    VERSION- 2.2    DATE- 06/02/94   PROGRAMMER- FRANK TILLEY (SMSRC)
C           XATOV-9: INCLUDE PARAMETER FILE CDBPARM.  ADD FILE
C                    WORKSPACE TO THE CALLING ARGUMENTS FOR SETCDB.
C                    REPLACE THE CALL TO SETOCD WITH A CALL TO SETCDB
C                    WITH COMOCD:OCDWS AS THE WORKSPACE ARGUMENT.
C                    USE PARAMETERS FROM INPARM TO DIMENSION ARRAYS
C                    HCALAV, MCALAV, AND SCALAV.
C    VERSION- 2.3    DATE- 09/15/94   PROGRAMMER- DAVE MOREL (SMSRC)
C          ATOVS-21: ADD CHECK ON FRMCNF FOR THE MSU BIT BEFORE CALLING
C                    RTHLOC.
C    VERSION- 2.4    DATE- 09/28/94   PROGRAMMER- CARLA NEIGH(SMSRC)
C          ATOVS-24: RESET MSU VALUES IN HIRS DATA BUFFER TO MISSING
C                    VALUES AFTER NSDVR CALL.
C    VERSION- 2.5    DATE- 12/11/95   PROGRAMMER- CHARLES A. DEAN (SMSRC
C         ATOVS-280: NEW ROUTINES AND CODE ARE ADDED FOR 1B* INPUT.
C                    ALL REFERENCES TO MSU AND SSU CHANGED TO AMSU-A
C                    AND AMSU-B.  ARGUMENTS TO CALLED ROUTINES CHANGED
C                    AS PER CHANGES PREVIOUSLY MADE TO THOSE ROUTINES.
C                    CHANGE PARTITION OF ODB FROM 4 TO 5.  OUTPUT OF
C                    INSTRUMENT CHANGED FROM OBD TO IRDB. CALL TO FILHIR
C                    AND PTOVS REMOVED.  CODE ADDED TO $-ROUTINE TO OUTP
C                    1B* ERROR MESSAGE BUFFER.
C    VERSION- 2.6    DATE- 01/18/96   PROGRAMMER- DAVE MOREL (SMSRC)
C         ATOVS-375: ADDED DELSCN TO CALL TO CONDVR.  ADDED VARIABLE
C                    RECID1.  REMOVED VARIABLES CHANNO AND FRSTTM.
C                    ADDED OPENS OF CLRUNT AND CLDUNT.
C    VERSION- 3.0    DATE- 05/28/96   PROGRAMMER- TIM GARDNER (HSTX)
C    VERSION- 3.1    DATE- 07/03/97   PROGRAMMER- LEAH WILSON (HSTX)
C    VERSION- 4.1    DATE- 08/29/97   PROGRAMMER- LEAH WILSON (HSTX)
C    VERSION- 4.2    DATE- 03/03/98   PROGRAMMER- LEAH WILSON,
C                                                 BRIAN BARIL (R-STX)
C    VERSION- 4.3    DATE- 04/06/98   PROGRAMMER- AMERICO ALLEGRINO,
C                                                 LEAH WILSON (R-STX)
C    VERSION- 4.4    DATE- 05/04/98   PROGRAMMER- MICHAEL FERGUSON,
C                                                 LEAH WILSON (R-STX)
C    VERSION- 4.5    DATE- 05/20/98   PROGRAMMER- LEAH WILSON (R-STX)
C    VERSION- 4.6    DATE- 08/06/98   PROGRAMMER- A. ALLEGRINO(R-STX)
C    VERSION- 4.7    DATE- 08/28/98   PROGRAMMER- A. ALLEGRINO(R-STX)
C    VERSION- 4.8    DATE- 09/15/98   PROGRAMMER- BRIAN BARIL (R-STX)
C    VERSION- 4.9    DATE- 09/18/98   PROGRAMMER- A. ALLEGRINO(R-ITSS)
C    VERSION- 4.10   DATE- 10/01/98   PROGRAMMER- M.P.FERGUSON (RITSS)
C    VERSION- 5.0    DATE- 10/05/98   PROGRAMMER- A. ALLEGRINO(R-ITSS)
C    VERSION- 5.1    DATE- 10/20/98   PROGRAMMER- A. ALLEGRINO(R-ITSS)
C    VERSION- 5.2    DATE- 11/06/98   PROGRAMMER- A. ALLEGRINO(R-ITSS)
C    VERSION- 5.3    DATE- 11/13/98   PROGRAMMER- A. ALLEGRINO(R-ITSS)
C    VERSION- 5.4    DATE- 11/19/98   PROGRAMMER- A. ALLEGRINO(R-ITSS)
C    VERSION- 5.5    DATE- 12/03/98   PROGRAMMER- A. ALLEGRINO(R-ITSS)
C    VERSION- 5.6    DATE- 12/21/98   PROGRAMMER- BRIAN BARIL (R-ITSS)
C    VERSION- 5.7    DATE- 01/29/99   PROGRAMMER- M.P. FERGUSON (R-ITSS)
C    VERSION- 5.8    DATE- 02/01/99   PROGRAMMER- M.P. FERGUSON (R-ITSS)
C    VERSION- 5.9    DATE- 02/10/99   PROGRAMMER- M.P. FERGUSON (R-ITSS)
C    VERSION- 5.10   DATE- 04/12/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 5.11   DATE- 04/14/99   PROGRAMMER- LEAH WILSON  (R-ITSS)
C    VERSION- 5.12   DATE- 04/19/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 5.13   DATE- 05/04/99   PROGRAMMER- BRIAN BARIL  (R-ITSS)
C    VERSION- 6.0    DATE- 05/05/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.1    DATE- 05/11/99   PROGRAMMER- LEAH WILSON  (R-ITSS)
C    VERSION- 6.2    DATE- 05/26/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.3    DATE- 06/02/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.4    DATE- 06/17/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.5    DATE- 06/21/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.6    DATE- 08/16/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.7    DATE- 09/07/99   PROGRAMMER- LEAH WILSON  (R-ITSS)
C    VERSION- 6.8    DATE- 10/25/99   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.9    DATE- 04/12/00   PROGRAMMER- D. FREY
C    VERSION- 6.91   DATE- 02/14/00   PROGRAMMER- M.P.FERGUSON (R-ITSS)
C    VERSION- 6.10   DATE- 02/15/00   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.11   DATE- 03/17/00   PROGRAMMER- LEAH WILSON (RAYTHEON)
C    VERSION- 6.12   DATE- 03/28/00   PROGRAMMER- A. ALLEGRINO (R-ITSS)
C    VERSION- 6.13   DATE- 04/28/00   PROGRAMMER- LEAH WILSON (RAYTHEON)
C    VERSION- 6.14   DATE- 06/05/00   PROGRAMMER- M.P.FERGUSON (RITSS)
C    VERSION- 6.15   DATE- 06/14/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 6.16   DATE- 07/05/00   PROGRAMMER- M.P. FERGUSON (RITSS)
C    VERSION- 6.17   DATE- 07/21/00   PROGRAMMER- M.P. FERGUSON (RITSS)
C    VERSION- 6.18   DATE- 08/09/00   PROGRAMMER- MIKE FERGUSON (RAYTHEON)
C    VERSION- 6.19   DATE- 08/02/00   PROGRAMMER- A. ALLEGRINO  (RITSS)
C    VERSION- 6.20   DATE- 08/07/00   PROGRAMMER- A. ALLEGRINO  (RITSS)
C    VERSION- 6.21   DATE- 08/17/00   PROGRAMMER- A. ALLEGRINO  (RITSS)
C    VERSION- 6.22   DATE- 08/21/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 6.23   DATE- 09/18/00   PROGRAMMER- A. ALLEGRINO  (RITSS)
C    VERSION- 6.24   DATE- 09/12/00   PROGRAMMER- M.P.FERGUSON (RITSS)
C    VERSION- 6.25   DATE- 11/01/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 6.26   DATE- 11/07/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 6.27   DATE- 11/09/00   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 6.28   DATE- 12/04/00   PROGRAMMER- M.P.FERGUSON (RITSS)
C    VERSION- 6.29   DATE- 12/15/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 6.30   DATE- 02/06/01   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 6.31   DATE- 02/07/01   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 6.32   DATE- 02/14/01   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 6.33   DATE- 03/05/01   PROGRAMMER- M.P.FERGUSON (RITSS)
C    VERSION- 6.34   DATE- 09/08/01   PROGRAMMER- M.P.FERGUSON (RITSS)
C    VERSION- 6.35   DATE- 10/23/01   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 6.36   DATE- 02/27/02   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 6.37   DATE- 04/25/02   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 6.38   DATE- 05/30/02   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 6.39   DATE- 07/03/03   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 7.0    DATE- 08/06/02   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 7.1    DATE- 12/13/02   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 7.2    DATE- 02/25/03   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 7.3    DATE- 02/27/03   PROGRAMMER- RICKEY ROLLINS (CSC)
C    VERSION- 7.4    DATE- 01/09/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 7.5    DATE- 11/17/04   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C    VERSION- 7.6    DATE- 12/06/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 7.7    DATE- 07/15/05   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 7.8    DATE- 02/06/06   PROGRAMMER- FRANK TILLEY (ERT)
C
C    FUNCTION-  THIS IS THE MAIN DRIVING PROGRAM FOR THE ATOVS SOUNDING
C               PRODUCTS GENERATION SUBSYSTEM.  THE MAIN FUNCTION OF
C               THIS PROGRAM IS TO INVOKE THE ERROR HANDLING PROCESS
C               AND ORDERLY CALL THE SUBSYSTEM DRIVERS TO PROCESS
C               AN ORBIT'S 1B* DATA FROM THE HIRS, AMSU-A, AMSU-B, AND
C               AVHRR INSTRUMENTS INTO A PRODUCT RETRIEVAL FOR THE
C               NOAA-K SERIES SATELLITES. (FOR FURTHER INFORMATION
C               REFER TO THE SPGS PROGRAM DESCRIPTION DOCUMENT)
C
C    VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE
C                 ON THE CRAY.
C    VERSION 3.1- REPLACE MMLF WITH KIDS FILE.  REPLACE CDB UTILITES
C                 WITH NEW 8-BYTE CDB UTILITIES.  ADD RTOVS UPGRADES.
C                 COMMENT OUT ANY AMSU-B PROCESSING.
C    VERSION 4.0- REMOVE ALL FILE ACCESS UTILITES.  REPLACE ODB WITH
C                 ODF, AND REPLACE IRDB WITH AODF AND HODF. INLINE
C                 EFFTMP, AND ADD /EFFCOE/.  ADD ULCHIR89 BUFFER FOR
C                 USE IN TOTAL OZONE CALCULATION.  ADD STUBS FOR R1T.
C                 MAKE THE OZONE FILE A STANDARD FORTRAN I/O FILE.
C                 REDEFINE PREVIOUSLY PROCESSED FILE AND IT'S USE.
C    VERSION 4.1- ALLOWS AMSU-A ONLY DATA TO BE PROCESSED, ALTHOUGH
C                 NO RETRIEVALS ARE MADE, AS IN THE CASE OF A LAUNCH
C                 WHERE THE AMSU-A IS AVAILABLE WELL BEFORE THE HIRS
C                 OR AVHRR INSTRUMENTS.
C    VERSION 4.2- ADD ROTATING 1T AND CLDTOP.  ALSO ADD ON/OFF FLAG
C                 FOR OZONE FILE.  ADD /NONLCDAT/ FOR
C                 STORING NON-LIMB CORRECTED, COLLOCATED HIRS AND
C                 AMSU-A DATA FOR THE RODF.
C    VERSION 4.3- MODIFIED SO THAT DATA COLLECTION FOR THE LIMB
C                 CORRECTION COEFFICIENT GENERATION CAN BE TURNED
C                 ON OR OFF DEPENDING ON A CDB VALUE.  ALSO, ADD
C                 READ FOR CDB COEFFICIENT CONTROLLING DEBUG PRINTS.
C    VERSION 4.4- MODIFICATIONS FOR VERTICAL LIMB CORRECTION.  ADDED
C                 /I2HINF/ AND /LCCOEF/.  ALSO, REMOVED BDCLDPRC AND
C                 REPLACED IT WITH A DATA FILE THAT CAN EASILY BE
C                 MODIFIED.
C    VERSION 4.5- ADD /GROSSCHK/ TO SPECIFY AVAILABLE HIRS AND AMSU-A
C                 CHANNELS.
C    VERSION 4.6- PLACE LIMB CORRECTED  AMSU-A TEMPERATURE VALUES
C                 INTO THE AODF.
C    VERSION 4.7- READ THE RADIO FREQUENCY INTERFERANCE TABLE AND PASS
C                 IT TO AMAODB
C    VERSION 4.8- ADDED A SWITCH TO TURN ON OR OFF THE GENERATION AND
C                 USE OF THE HIRS ATTENUATION CHANNEL 8.  CURRENTLY
C                 WANT TO USE ONLY HIRS CHANNEL 8.  IF SWITCHED BACK
C                 THE THRESHOLD (THRESD) MUST BE CHANGED TO WORK WITH
C                 THE CURRENT ATTENU8 COEFFICIENTS (TRY 5-10)
C    VERSION 4.9- REMOVE  RADIO FREQUENCY INTERFERANCE TABLE ADJUSTMENT
C                 FROM THE LCDC
C    VERSION 4.10-CHANGE DIMENSION OF CHNCMB TO 2X4 FROM 2X3 TO ACCOMODATE
C                 HIGH TERRAIN CHANNEL COMBINATION READ IN FROM CDB.
C    VERSION 5.0- ADD SEPARATE LAND AND SEA LIMB CORRECTION COEFFICIENTS
C                 TO BE APPLIED TO AMSU-A DATA. ALSO ADD FLAG TO DETERMINE
C                 WHETHER OR NOT TO USE THE GENERAL COEFFICIENTS ON THE CDB
C                 OR THE STRATIFIED COEFFICIENTS IN SEPARATE FILES (ONE LAND,
C                 THE OTHER, SEA)
C    VERSION 5.1- REMOVED ALL POLARIZATION VARIABLES AND ARRAYS SINCE IT WAS
C                 DISCOVERED THAT THE AMSU-A POLARIZATION WAS AN ARTIFICAL
C                 MANIFESTATION OF THE LIMB ADJUSTMENT COEFFICIENT GENERATION.
C                 WILL LEAVE AN ARRAY TO INCLUDE TSAN MO'S AMSU-A ANTENNA
C                 ADJUSTMENT WHEN THE GOLDBERG LIMB COEFFICIENTS ARE TESTED.
C    VERSION 5.2- PLACED LIMB ADJUSTED HIRS DATA INTO THE HODF.
C    VERSION 5.3- ADDED FLAG TO DETERMINE WHETHER OR NOT LIMB ADJUSTED AMSU-A
C                 WILL BE USED TO CALCULATE TOTAL PRECIPITABLE WATER, CLOUD
C                 LIQUID WATER, ETC IN SUBROUTINE AMAODB. FLAG WILL BE READ
C                 FROM SOFTFLAGS FILE (UNIT 70).
C    VERSION 5.4- IMPLEMENTED MITCH GOLDBERG'S LIMB ADJUSTMENTS AND TSAN
C                 MO'S ANTENNA TEMPERATURE ADJUSTMENTS FOR AMSU-A CHANNELS
C                 1,2,3, AND 15.
C    VERSION 5.5- CHANGED TO READ 2 ADDITIONAL PARAMETERS FROM SOFTFLAGS AND
C                 PASS THEM INTO AMAODB. THESE PARAMETERS ARE THE MAXIMUM
C                 ALLOWABLE VALUES FOR THE TOTAL PRECIPITABLE WATER AND
C                 CLOUD LIQUID WATER.
C    VERSION 5.6- REMOVED THE FILE ACCESS UTILITIES, CHANGING CALL TO MAPDVR.
C                 AND ADDING AN OPENCDB CALL FOR THE MAPPARM CDB.
C    VERSION 5.7- WHEN THE AMSU-A LIMB CORRECTION DATA COLLECTION IS TURNED ON
C                 THE AMSU-A LIMB FILE IS OPENED AND THEN THE PROGRAM GOES TO
C                 130. THE 'GO TO 130' WAS REPLACED WITH 'GO TO 128'.  WITH THE
C                 'GO TO 130', THE PROGRAM WAS SKIPPING THE INPUT OF TSAN
C                 MO'S COEFFICIENTS AND THE INPUT OF THE AMSU-A LIMB
C                 ADJUSTMENTS.
C    VERSION 5.8  FOV SELECT PERCENTAGE THRESHOLDS ARE OBTAINED FROM CDB.
C    VERSION 5.9  ADD ARGUMENT TO CALL OF RCDBV: LIMHGT, THE MAXIMUM TERRAIN IN
C                 METERS AT WHICH A CLEAR RETRIEVAL MAY BE MADE, OBTAINED OFF OF
C                 THE CDB IN RCDBV, AND ADDED AS AN ARGUMENT TO CALL OF RETDVR FOR
C                 THAT VERY PURPOSE.
C    VERSION 5.10 ADD CALL TO RETRIEVE THE SUPER ADIABATIC ADDITIVE FROM THE
C                 CDB.  ALSO ADDED THE COMMON BLOCK FLGCOM TO PASS THE
C                 ADDITIVE SUPER ADIABATIC VALUE TO THE SUBROUTINE CHKSUP.
C    VERSION 5.11 CHANGE HOW THE 1B* VERSION NUMBER IS READ FROM FT05.
C    VERSION 5.12 ADDED HIRS AND AMSU-A GROSS TEMPERATURE CHECKS. FIRST MUST
C                 PASS THE TEMPERATURE LIMITS INTO RETODB. RETODB DOES THE
C                 CHECKING.
C    VERSION 5.13 REMOVED ALL REFERENCES TO THE OZONE FILE.  PARAMETERS FOR
C                 THE OZONE CALCULATION ARE WRITTEN TO THE R1T.
C    VERSION 6.0  REVAMPED SPGDVR SO THAT LIMB CORRECTION IS ONLY DONE
C                 ONCE.  THIS WILL RESULT IN HIRDAT AND AMADAT
C                 CONTAINING THE LIMB CORRECTED DATA AND, OBSHIR
C                 AND OBSAMA CONTAINING THE UNLIMB CORRECTED DATA.
C    VERSION 6.1  REMOVED ALL EXTRANEOUS FAU SOFTWARE (BDMAXPTR, BDVERID,
C                 AND UVALITEM).
C    VERSION 6.2  ADDED A NEW SUBROUTINE TO COMPUTE THE AMSU-A SURFACE
C                 PRODUCTS CALLED AMASFC.
C    VERSION 6.3  ADDED 2 ARRAYS TO BE READ FROM THE SOFTFLAGS DATA SET
C                 X2COEF AND XCOEF. THESE WILL BE USED TO
C                 TRANSFER THE COEFFICIENTS INTO AMASFC FOR COMPUTING
C                 THE CLOUD CLEARED CHANNELS 4,5 & 6.
C    VERSION 6.4  ADDED THE CLOUD LIQUID WATER THRESHOLD TO BE READ FROM THE
C                 SOFTFLAGS DATASET.
C    VERSION 6.5  ADDED THE LAND PRECIPITATION THRESHOLD TO BE READ FROM THE
C                 SOFTFLAGS DATASET.
C    VERSION 6.6  ADDED  A LOOP WHICH STORES THE HIRS CH. 20 DATA ON THE
C                 HIRDAT BUFFER (ELEMENT 20).
C    VERSION 6.7  ADDED AMSU-A DATA MISLOCATION (CHALFANT ALGORITHM).
C    VERSION 6.8  ADDED A LATITUDE VALUE TO THE SOFTFLAGS DATA SET TO BE
C                 USED BY THE SUBROUTINE PRECIP.F AND COLDVR.F
C    VERSION 6.9  ADDED CALL TO WRTCOL AND ADDITIONAL VARIABLES
C    VERSION 6.91 CHANGE DIMENSION OF CHNCMB TO 2X4X2 FROM 2X4 TO ACCOMODATE
C                 CLEAR AND CLOUDY RETRIEVAL CHANNEL COMBINATIONS READ IN
C                 FROM CDB.
C    VERSION 6.10 ADDED A CHANNEL SELECTION VALUE TO THE SOFTFLAGS DATA SET
C                 TO BE USED BY THE SUBROUTINE PRECIP.F.
C    VERSION 6.11 MODIFICATIONS TO ADD RETRIEVAL FAILURE SUMMARY PER FRAME
C                 TO KIDS, REMOVE USE_CAL_PROB, AND ADJUST BADHFLAG
C                 AND BADAFLAG TO REFLECT NEW USAGE. MODIFICATIONS TO CALL
C                 TO INPDVR RELATING TO USE_CAL REMOVAL. MOVE APPLICATION
C                 OF TSAN MO'S COEFFICIENTS TO RIGHT BEFORE GLCDVR, & ADD
C                 ON/OFF FLAG FOR APPLYING TSAN MO'S COEFFICIENTS TO
C                 SOFTFLAGS DATASET. COUNTLESS COSMETIC AND DOCUMENTATION
C                 MODIFICATIONS.
C    VERSION 6.12 MODIFICATIONS TO ADD MORE VARIABLES TO BE READ IN VIA
C                 SOFTFLAGS DATA SET TO BE USED IN SUBROUTINE AMASFC.F
C                 ALSO ADDED A NEW LOGICAL ARRAY CALLED PRECIPFG TO
C                 DISPLAY THE PRECIPITATION CONTAMINATION AT THE HIRS FOV.
C    VERSION 6.13 ADD SMLIMIT TO READ OF FT34.
C    VERSION 6.14 LIMHG2 VARIABLE ADDED TO SUBROUTINE CALLING LISTS FOR
C                 RCDBV AND RETDVR.
C    VERSION 6.15 ADDED ANOTHER READ FOR THE SOFTFLAGS DATASET TO INPUT
C                 THE DIMENSION OF THE SQUARE, THE PRECIPITATION
C                 CONTAMINATION CHECK FLAG AND THE PERCENTAGE OF OPEN
C                 OCEAN FOR USE BY THE MEDIAN FILTER.  THESE VARIABLES
C                 WERE ADDED TO THE CALL OF CONDVR AND THE VARIABLE CHNSEL
C                 WAS REMOVED. (WR#74)
C    VERSION 6.16 ADD NRTHLM AND STHLIM TO CALLING LISTS OF SUBROUTINES
C                 RCDBV AND RETDVR FOR TRANSFERRENCE BETWEEN THE TWO
C                 SUBROUTINES. (WR#88)
C    VERSION 6.17 ADD CLRHGT TO CALLING LISTS OF SUBROUTINES RCDBV AND
C                 RETDVR FOR TRANSFERENCE BETWEEN THE TWO SUBROUTINES.
C                 (WR#88)
C    VERSION 6.18 THE 10 SOURCE CODES AND COMMON BLOCK
C                 CHANGED OR CREATED COMBINE, ALONG
C                 WITH A NEW FLAG READ OFF OF THE CDB TO
C                 ALLOW CONTROL OVER THE USE  OF HIRS
C                 IN  THE ATOVS SYSTEM FIRST GUESS AND
C                 RETRIEVAL  CHANNEL COMBINATION. IF HIROFF
C                 (USER MANUAL SWITCH)  ON THE CDB IS SET TO
C                 1 (ON), HIRS USE IS ELIMINATED  AND  THE CLOUD
C                 DETECTION IS TURNED OFF  (SINCE IT USES HIRS
C                 CHANNEL RELATIONSHIPS, AND DUMMY VALUES
C                 ARE BEING PROCESSED ONLINE IN THE HIRS ARRAYS
C                 TO ALLOW  PROCESSING TO CONTINUE). IN THIS
C                 CASE THE TARM PATH IS SET TO RED FOR ALL
C                 FOV PROCESSED, SIGNIFYING "NOHIRS" RETRIEVALS.
C                 ALL "GOOD" HIRS BRIGHTNESS  TEMPERATURES ARE
C                 PUT INTO THE OUTPUT RODF AND HODF FILES,
C                 BUT BAD (HIRS FAILING GROSS LIMITS) DATA
C                 ARE REPLACED WITH MISSING VALUES ON THESE
C                 FILES. IF THE  FLAG IS OFF, THE HIRS ARE STILL
C                 CHECKED, AND IF ONE OR MORE HIRS CHANNELS
C                 FAILS GROSS LIMITS (MISSING CALIBRATION COEFFICIENTS.
C                 OR RADIANCE OR BT FAILURES), THE  CLOUD DETECTION
C                 IS TURNED OFF FOR THAT SPOT, A RED NOHIRS
C                 CLOUD PATH IS WRITTEN TO THE RRODF, AND
C                 MISSING VALUES FOR THE BAD CHANNELS ARE
C                 PLACED  INTO THE RODF AND HODF (WR #105).
C    VERSION 6.19 ADDED SEVERAL MEDIAN FILTER LIMITS TO PASS THRU TO THE
C                 SUBROUTINE CONDVR. (WR#74)
C
C*********************************************************************
C                 VERSION 6.20 HAS NOT BEEN DELIVERED AS OF NOV, 2001
C    VERSION 6.20 ADDED NEW LIMITS TO HELP REMOVE WATER VAPOR FROM
C                 AMSU-A CHANNELS 4 AND 5. (WR#92)
C*********************************************************************
C    VERSION 6.21 ADDED ANOTHER MEDIAN FILTER LIMIT TO PASS THRU TO THE
C                 SUBROUTINE CONDVR. (WR#74)
C    VERSION 6.22 ADDED COMMON /NOHIRSCF/, SET COMMON FLAG 'NOH1BS' TO
C                 DENOTE THAT A HIRS 1B* IS NOT AVAILABLE.  ADDED A CALL
C                 TO INNOHIRS TO GENERATE AMSU-A TO HIRS INTERPOLATION
C                 COEFFICIENTS IN COMMON /NOHIRSCF/.  REMOVED REFERENCES
C                 TO THE 'AMSUAONLY' CONDITION. (WR#100)
C
C*********************************************************************
C                 VERSION 6.23 HAS NOT BEEN DELIVERED AS OF NOV, 2001
C    VERSION 6.23 ADDED 2 MORE SOFTCODED VARIABLES TO BE READ FROM
C                 THE SOFTFLAGS DATA SET AND PASSED INTO THE ROUTINE AMASFC.F.
C                 (WR# 102 & 110)
C*********************************************************************
C    VERSION 6.24 CALL TO REALCOEF CHANGED TO RETRIEVE PCTCLR INSTEAD OF PCTCLD
C                 FROM THE CDB, AND PCTCLD IS THEN DETERMINED FROM PCTCLR, INSTEAD
C                 OF THE REVERSE.  ALL THESE CHANGES MADE TO ALLOW MODIFICATIONS
C                 DONE IN COLDVR AND THE ADDITION OF NEW ROUTINE PIKSPT, AND DELE-
C                 TION OF USE OF CLRSPT AND CLDSPT.
C    VERSION 6.25 TO PREVENT BAD OZONE AND RADIATION PRODUCTS FROM BEING GENERATED
C                 FROM BAD OR MISSING HIRS DATA, SET THE 'CLDONOFF' FLAG TO '0'
C                 (OFF) TO PREVENT 'CLDTOP' FROM BEING CALLED WHEN THE 'HIROFF'
C                 FLAG IS SET TO '1'. CHANGED DECLARATION OF 'AT8ONOFF' AND
C                 'CLDONOFF' FROM REAL TO INTEGER. [WR #121] ADDED CODE TO CHECK
C                 THE NUMBER OF RECORDS WRITTEN TO THE ROTATING 1T FILE. IF NO
C                 RECORDS WERE WRITTEN, THE FILE IS CLOSED WITHOUT UPDATING THE
C                 HEADER RECORD.
C    VERSION 6.26 CHANGED CODE TO SKIP GAC INPUT IF THERE IS NO HIRS OR IF THE
C                 HIROFF FLAG IS SET. THIS REQUIRED MOVING CODE THAT OPENS THE
C                 CDB FILES AND READS THE HIROFF FLAG TO BE EXECUTED BEFORE
C                 THE 1B* FILES ARE OPENED. [WR #122]
C    VERSION 6.27 CHANGED CODE TO ADD A FLAG TO EACH FRAME HEADER RECORD OF THE
C                 RODF TO INDICATE ORBITS WHICH HAVE MISLOCATED DATA.
C    VERSION 6.28 FLAG IAMAOFF READ OFF OF THE CDB AND ARRAY AMAOFF (AMASTAT) IS
C                 SET ACCORDINGLY, TO ALLOW PROCESSING OF RETRIEVALS WHEN ONE OR
C                 MORE AMSU-A CHANNELS ARE UNUSABLE.  OF COURSE, FIRST GUESS
C                 AND RETRIEVAL CHANNEL COMBINATIONS MUST BE SET ACCORDINGLY (PR
C                 # 28).
C    VERSION 6.29 ADDED FLAG 'N16HIR' TO BE READ FROM SOFTFLAGS AND TO BE PASSED
C                 TO INPDVR TO CAUSE HIRS BRIGHTNESS TEMPERATURES TO BE SHIFTED
C                 ONE SPOT TO THE RIGHT TO CORRECT FOR INSTRUMENT MISALIGNMENT.
C                 (WR #131). ADDED 'N16HIR' TO THE CALL TO FRMRET.
C    VERSION 6.30 ADDED CODE TO CHECK N16HIR FLAG IN ORDER TO ENSURE THAT THE
C                 FIRST BEAM POSITION IN THE HIRDAT ARRAY FOR ALL CHANNELS IS
C                 SET TO MISSING IF THE N16HIR FLAG IS SET TO ONE, AFTER LIMB
C                 CORRECTION. THIS WILL, ALONG WITH CHANGES IN THE R1T EDGE
C                 DRIVER, CORRECT THE BOGUS DATA IN BEAM POSITION ONE ON THE
C                 R1T. (WR#134A)
C    VERSION 6.31 ADDED CODE TO READ NEW CLW LIMIT FOR DETERMINING IF THE CLW
C                 CORRECTIONS TO AMSU-A CH 4 AND 5 ARE ZERO. READ FROM
C                 SOFTFLAGS DATA SET. (WR#120)
C    VERSION 6.32 MODIFIED CODE TO USE THE AMSU-A CHANNEL 2 AT THE HIRS FOV
C                 TO CHECK FOR DATA MISLOCATION AND ADDED AN EXIT CODE OF
C                 88 TO SPGDVR IN ORDER TO NOTIFY PERSONNEL OF MISLOCATED
C                 ORBITS. (WR#104,WR#128 AND PR#26)
C    VERSION 6.33 CORRECTED MISPLELLING OF ARGUMENT NRTHLM IN CALL TO SUBROU
C                 TINE RETDVR (PR #   ).
C    VERSION 6.34 ADD CODE TO DUMP REQUIRED NUMBER OF CLEAR SOUNDINGS OF EITHER
C                 SEA OR NON-SEA TYPE PRESENT IN A BOX NECESSARY TO MAKE A CLEAR
C                 SOUNDING.  THIS VARIABLE IS CALLED NUMCLEAR.
C    VERSION 6.35 ADDED CODE TO READ IN THE SLOPES AND INTERCEPTS FOR THE
C                 INTERPOLATION OF THE CLOUD COEFFICIENTS. (WR#176)
C                 ERROR. (WR#139)
C    VERSION 6.36 REMOVED THE LOGICAL ARRAY PRECIPFG FROM THE ROUTINE AND
C                 ADDED MORE VARIABLES TO BE READ FROM THE SOFTFLAGS FILE.
C                 (WR#158 AND 191)
C    VERSION 6.37 ADDED VARIABLES CLWADJ4, CHN4SEL, CHN5SEL, A5THRES, AND
C                 CLWADJ5 READ FROM THE SOFTFLAGS FILE AND REMOVED UNUSED
C                 VARIABLES MAXCH6, MINCH6, FUDGEU4, FUDGEU5, AND A4THRES
C                 WHICH WERE READ FROM THE SOFTFLAGS FILE. ALSO ADDED
C                 VARIABLES CHN5LIM AND C5THRES TO BE READ FROM THE SOFT
C                 FLAGS FILE FOR FILTERING OF AMSU-A CHANNEL 5.
C                 (WR *196).
C    VERSION 6.38 INCREASED THE RECORD SIZE OF THE ROTATING 1T FROM 568 TO
C                 740 (WR#186/187)
C    VERSION 6.39 ADDED A CHECK FOR PERCENTAGE OF RETRIEVALS WITH BAD
C                 OBSERVATION QUALITY FLAGS. (WR #234)
C    VERSION 7.0  MODIFIED TO PROCESS AMSU-B DATA UNDER WR #22.
C    VERSION 7.1  ADDED PRINT DIAGNOSTICS TO MONITOR THE CLDTOP OUTPUT.
C                 THESE DIAGNOSTICS CONSIST OF THE NUMBER OF MISSINGS,
C                 THE NUMBER OF CLEARS, THE NUMBER OF LOW CLOUDS, THE
C                 NUMBER OF "TRACE" CLOUD AMOUNTS AND TOTAL NUMBER OF
C                 SPOTS PER ORBIT. (WR# 210)
C    VERSION 7.2  ADDED PRINT DIAGNOSTICS TO MONITOR THE NUMBER OF CHANGES
C                 MADE BY THE NEW CLOUD AMOUNT ALGORITHM. (WR#205/210)
C    VERSION 7.3  REPLACED R4SPV EQUIVALENCE TEST WITH ISR4SPV FUNCTION
C    VERSION 7.4  CHANGED AMSU-B INPUT TO USE 1B* UTILITIES. REMOVED
C                 LINES THAT READ IN SLOPES AND INTERCEPTS FOR CLDTOP.
C    VERSION 7.5  increased the size of the AMSu-b limb data array from 976
C                 to 2896 within the open statement (WR#261)
C    VERSION 7.6  MODIFIED TO READ MHS DATA WHEN THE HIRS/4 INSTRUMENT IS
C                 PRESENT.
C    VERSION 7.7  ADDED CODE TO READ 'H1REPLACE' FROM SOFTFLAGS AND SET
C                 /HIRUSE/:H1REPL ACCORDINGLY TO CONTROL REPLACEMENT OF
C                 HIRS CH1 WITH MEAN OF AMSU-A CH 10-14 IN TOTAL OZONE AND
C                 PREDICTED CLEAR HIRS CH 8 CALCULATIONS.(WR #286)
C    VERSION 7.8  ADDED LOGICAL FLAG 'FULFRM' TO CALLING ARGUMENTS FOR
C                 SUBROUTINE INPDVR. 'FULFRM' IS RETURNED AS FALSE FOR
C                 THE PARTIAL DATA FRAMES AT THE BEGINNING AND END OF
C                 THE 1B* FILES. RETRIEVAL PROCESSING IS SKIPPED FOR
C                 THESE PARTIAL FRAMES TO AVOID MAKING DUPLICATE
C                 RETRIEVALS. (WR #300)
C
C
C    FILES-
C      DSN/DESCRIPTIVE TITLE   I/O  UNIT   FUNCTIONAL DESCRIPTION
C      ---------------------   ---  ----   ----------------------
C      .../HIRX....             I   HUNIT  HIRS 1B* DATASET
C      .../AMSU-A..             I   AUNIT  AMSU-A 1B* DATASET
C      .../AMSU-B..             I   BUNIT  AMSU-B 1B* DATASET
C      .../GHRR....             I   GUNIT  AVHRR 1B* DATASET
C      .../CDB                  I   CDBUNT CDB
C      .../MAPPARM              I   MAPUNT MAP
C      .../ODB                  O   ODBUNT RETRIEVAL ODF
C      .../HODF                 O   HIDFU  HIRS ODF
C      .../AODF                 O   AIDFU  AMSU-A ODF
C      .../BODF                 O   BIDFU  AMSU-B ODF
C      .../OPCDB                I   UNTOPC OPCDB
C      .../PROCESED            I/O  FPUNIT PREVIOUSLY PROCESSED FILE
C      .../KIDS                 O   KUNIT  KIDS MESSAGE FILE
C      .../DSDR1T              I/O  R1TUNT ROTATING 1T FILE
C      .../*AMAX*.LIMOBS        O   ALMUNT AMSU-A LIMB CORRECTION SUB-1B*
C      .../*AMBX*.LIMOBS        O   BLMUNT AMSU-B LIMB CORRECTION SUB-1B*
C      .../*HIRX*.LIMOBS        O   HLMUNT HIRS LIMB CORRECTION SUB-1B*
C      .../TMOS.TEMPADJ         I   TSAN MO'S ANTENNA PATTERN ADJUSTMENTS
C
C    COMMON AREA PARAMETERS- INPCDB, FRMINF, INSCOM,
C                   RTVGRD, MONSTA,  MAPPARM, ERPARM,
C                   COLLOC, INPARM, SPGUNT, SPVAL, INSPARM, RTVPARM,
C                   RETPARM, CDBPARM, EFFCOE, STBCOM, NMCCOM, RTPLVL,
C                   R1TDAT, NONLCDAT, FLGCOM, NOHIRSCF, MSG1BS
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
C      AANAME       CH*98   AMSU-A 1B* DATASET NAME
C      AAVNUM        I*2    AMSU-A VERSION NUMBER.
C      ABNAME       CH*98   AMSU-B 1B* DATASET NAME
C      ABVNUM        I*2    AMSU-B VERSION NUMBER.
C      ACTUALRET     INT    SUM OF /MONSTA/:NFRRET CONTAINING # OF ACTUAL
C                           RETRIEVALS.
C      ALTERLIM      REAL   MEDIAN FILTER LIMIT FOR COASTS, ICE,
C                           SNOW, ISOLATED SEA AND ISOLATED LAND
C      AMADAT        REAL   CONTAINS THE LIMB CORRECTED AMSU-A
C                           DATA FOR CHANNELS 1-15.
C      AMALNE        INT    AMSU-A COLLOCATED LINE NUMBER (TO HIRS),
C                           DIMENSIONED (INSPARM:HNSPOT,INSPARM:HTOTLN).
C      AMASPT        INT    AMSU-A COLLOCATED SPOT NUMBER (TO HIRS),
C                           DIMENSIONED (INSPARM:HNSPOT,INSPARM:HTOTLN).
C      AMBINPUT      REAL   ARRAY FOR READING IN AMSU-B LIMB CORRECTION
C                           COEFFICIENTS.
C      AVLLIM        REAL   THE MINIMUM PERCENTAGE OF AVAILABLE SPOTS
C                           NECESSARY FOR THE MEDIAN FILTER
C      AVNAME       CH*98   GAC 1B* DATASET NAME
C      AVVNUM        I*2    GAC VERSION NUMBER.
C      A5THRES       REAL   CLW THRESHOLD USED IN CONJUNCTION WITH
C                           AMSU-A CHANNEL 5 BRIGHTNESS TEMPERATURE FOR
C                           PRECIPITATION FLAGGING IN HIRODB.
C      BADUNT        INT    UNIT NUMBER OF FILE THAT COULD NOT BE OPENED.
C      CDBST         INT    ERROR RETURN CODE FROM A CDB ROUTINE.
C      CHNCMB        INT    ARRAY DIMENSIONED (2,4,2) WHERE THE FIRST
C                           DIMENSION CONTAINS THE CHANNEL COMBINATION
C                           FLAGS AND THE SECOND DENOTES CLEAR, CLOUDY
C                           OR NOHIRS, WHILE THE THIRD DIMENSION IS SEA
C                           OR LAND.
C      CH4THRES      REAL   AMSUA CH. 4 TEMPERATURE THRESHOLD TO DETERMINE
C                           WHICH PROCESS TO USE IN COMPUTING THE PRECIP/
C                           CLOUD SCREENING FOR AMSUA CH 4 AND 5.
C      CH4LIM        REAL   MEDIAN FILTER LIMIT FOR AMSU CH4
C      CH5LIM        REAL   MEDIAN FILTER LIMIT FOR AMSU CH5
C      CHKPREC       INT    FLAG TO DETERMINE WHETHER OR NOT TO
C                           CHECK FOR PRECIPITATION CONTAMINATION
C                           WITHIN THE MEDIAN FILTER BOX. (=1,YES;
C                           =0,NO)
C      CHN4SEL       REAL   UPPER LIMIT OF AMSU-A CHANNEL 4 TEMPERATURE
C                           FOR SETTING THE TROPICS/MID LATITUDES
C                           BORDER.
C      CHN5SEL       REAL   LOWER LIMIT OF AMSU-A CHANNEL 5 TEMPERATURE
C                           FOR DETERMINING A SOUNDING IS IN THE
C                           TROPICS.
C      CLDCHN        INT    VECTOR OF LENGTH 2 CONTAINING THE CLOUDY
C                           CHANNEL BIT FLAGS.
C      CLOUDY        LOG    ARRAY DIMENSION (HNSPOT,HTOTLN) TO INDICATE
C                           TRUE IF CLOUD OR PRECIP CONTAMINATED FALSE IF CLEAR.
C      CLWADJ        REAL   VALUE USED TO ADJUST THE CLOUD LIQUID WATER
C                           VALUE. USED IF AMSU CH4 IS <= CH4THRES.
C      CLWADJ5       REAL   CLOUD LIQUID WATER THRESHOLD FOR AMSU-A CHANNEL 5.
C      CLWTHR        REAL   CLOUD LIQUID WATER THRESHOLD READ FROM SOFTFLAGS
C                           DATASET
C      CNFLIM        INT    SPOT CONFIDENCE LIMIT FROM THE CDB.
C      CUMRET        INT    CUMULATIVE NUMBER OF RETRIEVAL RECORDS WRITTEN
C                           TO RODF.
C      DAY           INT    THE UNPACKED DATE OF THE PARTITION HEADER.
C      DELSCN        REAL   DISPLACEMENT OF COLLOCATED INSTRUMENT X SPOT
C                           FROM THE INSTRUMENT Y FOV ALONG A SCAN POSIT
C      DELSPT        REAL   DISPLACEMENT OF COLLOCATED INSTRUMENT X SPOT
C                           FROM THE INSTRUMENT Y FOV ALONG A SCAN LINE.
C      DUMMY         INT    A VECTOR OF 2 USED AS A POSITION HOLDER.
C      DUMPPR        INT    ARRAY (DIM 7) FOR READING PREV.PROC. FILE.
C      DUMTIM        INT    ARRAY (DIM 3) USED AS A DUMMY FOR CALCULATING
C                           FRAME HOUR AND MINUTE, USED IN PREVIOUSLY
C                           PROCESSED LOGIC.
C      ENPPJ         INT    END JDAY IN PREV.PROC. FILE (DIM 50)
C      ENPPT         INT    END HHMM IN PREV.PROC. FILE (DIM 50)
C      ENPPTIME      INT    ARRAY (DIM 50) OF ENPPY*1000000+ENPPJ*1000*ENPPT.
C      ENPPY         INT    END YEAR IN PREV.PROC. FILE (DIM 50)
C      ERSTAT        INT    ERROR RETURN CODE FROM SUBROUTINE, = 0.
C                           IF NO ERRORS OCCURRED IN THE EXECUTION OF
C                           SUBROUTINE, OTHERWISE <> 0
C      FIRST         LOG    A LOGICAL SET TRUE FOR THE FIRST CALL
C                           TO ROUTINE RCDBV ONLY.
C      FRAMNUM       INT    FRAME NUMBER.
C      FRDATE        INT    THE LATEST DATE OF A 1B* DATA SET.
C      FRHR          INT    THE HOUR RETURNED FROM ROUTINE YRJDAY.
C      FRHREC        INT    AN ARRAY OF THE LOCATION OF FRAME HEADER
C                           RECORDS ON THE RODF.
C      FRMTIM        INT    A VECTOR OF 3 CONTAINING THE FRAME SYNC TIME
C      FRYR          INT    THE YEAR RETURNED FROM ROUTINE YRJDAY.
C      FSTFRM        LOG    ARRAY OF FLAGS INDICATING IF FRAME IS FIRST
C                           FRAME PROCESSED FOLLOWING SYNCHRONIZATION.
C                           (1 ELEMENT FOR EACH INSTRUMENT, DIM 4)
C      FULFRM        LOG    'FULL FRAME' FLAG. WHEN RETURNED AS .FALSE.
C                           BY SUBROUTINE INPDVR, THE INSTRUMENT DATA
C                           BUFFERS CONTAIN DATA FROM A PARTIAL DATA
C                           FRAME AT THE BEGINNING OR END OF 1B*
C                           PROCESSING AND NO RETRIEVALS ARE MADE.
C      GETLIMOBA     INT    FLAG WHICH DETERMINES WHETHER OR NOT THE
C                           LIMB COEFFICIENT DATA COLLECTION PROCEEDS
C                           FOR THE AMSU-A (1=COLLECT, 0=DO NOT)
C      GETLIMOBB     INT    FLAG WHICH DETERMINES WHETHER OR NOT THE
C                           LIMB COEFFICIENT DATA COLLECTION PROCEEDS
C                           FOR THE AMSU-B (1=COLLECT, 0=DO NOT)
C      GETLIMOBH     INT    FLAG WHICH DETERMINES WHETHER OR NOT THE
C                           LIMB COEFFICIENT DATA COLLECTION PROCEEDS
C                           FOR THE HIRS (1=COLLECT, 0=DO NOT)
C      HDRMXT        INT    ARRAY (DIMENSIONED 2) OF THE LATEST HEADER
C                           START TIME (TIME IS IN FORMAT CENTURY DAY,
C                           MILLISECOND OF DAY).
C      HIRDAT        REAL   CONTAINS THE LIMB CORRECTED HIRS DATA
C                           FOR CHANNELS 1-19. CHANNEL 20 IS NOT
C                           LIMB CORRECTED.
C      HIRLIN1       INT    SCAN LINE NUMBER OF THE FIRST ACTIVE HIRS
C                           LINE IN A FRAME.
C      HRNAME       CH*98   HIRS 1B* DATASET NAME
C      HRVNUM        I*2    HIRS VERSION NUMBER.
C      I,J,K         INT    DO LOOP INDEX.
C      IDATIM(6)     INT    CONTAINS THE SYSTEM DATE AND TIME AS
C                           RETURNED BY DATIM1.
C      IE,JE,KE      INT    DO LOOP INDEX FOR EFFECTIVE TEMP LOOP.
C      INSTON        INT    FOR EACH INSTRUMENT, HIRS, AMSU-A, AMSU-B,
C                           AND AVHRR, 1 INDICATES THE PRESENCE OF A
C                           1B* FILE NAME IN UNIT 5 INPUT, 0 INDICATES
C                           NO 1B* FOR THAT FILE.
C      KMSG         CH*100  KIDS MESSAGE BUFFER
C      LATLMT        REAL   LATITUDE THRESHOLD READ FROM SOFTFLAGS DATASET
C      LIMAREC,
C      LIMRECB,
C      LIMHREC       INT    COUNTER FOR NUMBER OF RECORDS IN THE LIMB
C                           COEFFICIENT DATA COLLECTION PROCESS.
C      LNDINPUT      REAL   INPUT ARRAY OF THE LAND LIMB COEFFICIENTS
C      LNDLAMA       REAL   ARRAY OF THE LAND LIMB COEFFICIENTS
C      LNDSEAFG      INT    FLAG TO DETERMINE WHETHER OR NOT TO APPLY THE
C                           SEPARATE LAND/SEA LIMB CORRECTION COEFFICIENTS
C                           (LNDSEAFG=1), OR THE GENERAL COEFFICEINTS
C                           (LNDSEAFG=0)
C      MAPST         INT    ERROR RETURN CODE FROM THE MAP CDB ROUTINE.
C      MAXLMT        INT    AMSUA PARAMETER MAXIMUM LIMIT ARRAY WHICH HOLDS
C                           THE MAXIMUM VALUES FOR TPW (MAXLMT(1)) AND CLW
C                           (MAXLMT(2))
C      MIDLATTR      REAL   MID LATITUDE THRESHOLD FOR 30N-60N AND
C                           30S-60S
C      MISLAND       INT    AMSU-A MISLOCATION LIMIT FOR LAND.
C      MISLOCFG      INT    FLAG TO DETERMINE IF A FRAME HAS MISLOCATED
C                           DATA. WRITTEN TO FRAME HEADER (ELEMENT 23).
C      MISSEA        INT    AMSU-A MISLOCATION LIMIT FOR SEA.
C      MISSTAT       INT    EXIT CODE FOR MISLOCATED ORBITS ( SET TO 88).
C      NPPROC        INT    NUMBER OF RECORDS IN PREV. PROC. FILE
C      NXTPPROC      INT    NEXT REC. TO BE WRITTEN TO PREV. PROC. FILE
C      NDIM          INT    DETERMINES THE DIMENSION OF THE SQUARE
C                           BOX SUROUNDING THE AMSU-A SPOT WITHIN
C                           THE MEDIAN FILTER.
C      OBSAMA        REAL   CONTAINS THE NON-LIMB CORRECTED AMSU-A
C                           DATA FOR CHANNELS 1-15.
C      OBSAMB        REAL   CONTAINS THE NON-LIMB CORRECTED AMSU-B
C                           DATA FOR CHANNELS 1-5.
C      OBSHIR        REAL   CONTAINS THE NON- LIMB CORRECTED HIRS DATA
C                           FOR CHANNELS 1-19 AND THE NODE IS STORED
C                           IN PLACE OF CHANNEL 20. IT ALSO CONTAINS
C                           AMSU-A COLLOCATED TO THE HIRS FOV (ELEMENTS 21-35).
C      OLR,LC1-4     REAL   ARRAYS (DIMENSIONED HNSPOT X HTOTLN) STORING
C                           OUTGOING LONGWAVE RADIATION AND 4 LAYER
C                           COOLING RATES, RESPECTIVELY.
C      ONCE          LOG    USED TO FIND HIRLIN1.
C      OQBAD         INT    NUMBER OF RETRIEVALS WITH BAD OBS QUALITY.
C      OQCHEKD       INT    NUMBER OF RETRIEVALS CHECKED FOR BAD OBS
C                           QUALITY.
C      OQLIMT        REAL   FAILURE THRESHOLD FOR PERCENTAGE OF BAD
C                           RETRIEVALS WITHIN AN ORBIT.
C      OQPCNT        REAL   PERCENTAGE OF RETRIEVALS WITH A BAD
C                           OBSERVATION QUALITY.
C      OQSTAT        INT    OBSERVATION QUALITY STATUS. SET TO 89 IF
C                           OQPCNT EXCEEDS OQLIMT.
C      OUTPPR        INT    ARRAY (DIM 7) CONTAINING INFO. FOR THE PREV.
C                           PROC. FILE.
C      PCLNBD        REAL   PERCENTAGE OF BAD CLEAR LAND RETRIEVALS
C      PCLNGD        REAL   PERCENTAGE OF GOOD CLEAR LAND RETRIEVALS
C      PCLNQU        REAL   PERCENTAGE OF QUESTIONABLE CLEAR LAND
C                           RETRIEVALS
C      PCOCBD        REAL   PERCENTAGE OF BAD CLEAR OCEAN RETRIEVALS
C      PCOCGD        REAL   PERCENTAGE OF GOOD CLEAR OCEAN RETRIEVALS
C      PCOCQU        REAL   PERCENTAGE OF QUESTIONABLE CLEAR OCEAN
C                           RETRIEVALS
C      POLARTHR      REAL   POLAR THRESHOLD FOR 90N-60N AND 60S-90S
C      PPFRAME       INT    COUNTER FOR PREVIOUSLY PROCESSED FRAMES.
C      PPR           INT    LOOP CONTROL FOR READING PREV.PROC.FILE
C      PPR1ST        LOG    A LOGICAL SET TRUE UNTIL THE START TIME OF
C                           ACUTAL DATA IS SET FOR THE PREV.PROC.FILE
C      PRECIPFG      LOG    A LOGICAL ARRAY SET TO TRUE IF THE MEDIAN
C                           FILTER AND/OR THE CLOUD LIQUID WATER TEST
C                           INDICATE PRECIPITATION, AT THE HIRS FOV.
C      PRCNTML       REAL   PERCENTAGE LIMIT FOR DETERMINING IF AN ORBIT
C                           HAS MISLOCATED DATA.
C      PRFLAG        LOG    POLAR REDUNDANCY FLAG FOR EACH GRID POINT
C      PPRFLG        INT    ARRAY (DIM 50) OF PROCESSED FLAG FROM PREV.
C                           PROC. FILE.  0=DATA WAS PROCESSED, BUT NO
C                           RET'LS. WERE MADE,SO IT IS ELIGIBLE FOR
C                           RE-PROCESSING.  1=DATA WAS PROCESSED, RET'LS
C                           WERE MADE, NO RE-PROCESSING WILL BE ALLOWED.
C      Q4,Q5,& Q4P   REAL   SOFTCODED EXPONENTS TO BE USED IN SUBROUTINE
C                           AMASFC.F FOR AMSUA CH4 & CH5 CONTAMINATION
C                           REMOVAL
C      RETCOD        I*4    RETURN CODE FROM 1B* UTILITY CALLS.
C      RETMET        INT    VECTOR OF LENGTH 3 CONTAINING THE RETRIEVAL
C                           METHODS FOR CLEAR, CLOUDY, AND NOHIRS.
C      RODFBUF       I*2    INTEGER VERSION OF THE RETRIEVAL ODF BUFFER.
C      SEAINPUT      REAL   INPUT ARRAY OF THE SEA  LIMB COEFFICIENTS
C      SEALAMA       REAL   ARRAY OF THE SEA  LIMB COEFFICIENTS
C      SMSSTAT       INT    STATUS FROM CALL TO STATS.
C      STAT1T        INT    I/O STATUS CODE FOR R1T-RELATED OPERATIONS.
C      STATUS        INT    ERROR RETURN CODE FROM A FAU ROUTINE.
C      STPPJ         INT    START JDAY IN PREV.PROC. FILE (DIM 50)
C      STPPT         INT    START HHMM IN PREV.PROC. FILE (DIM 50)
C      STPPTIME      INT    ARRAY (DIM 50) OF STPPY*1000000+STPPJ*1000*STPPT.
C      STPPY         INT    START YEAR IN PREV.PROC. FILE (DIM 50)
C      SUPADD        REAL   ADDITIVE VALUE TO SUPERAADIABATIC CONDITIONS
C      SYNCTR        INT    ARRAY OF SYNCHRONIZATION DUMMY LINE
C                           COUNTERS FOR EACH OF THE SOUNDINGS
C                           INSTRUMENTS (DIM 3).
C      SYNTIM        INT    2D ARRAY OF TIMES ASSOCIATED WITH
C                           SYNCHRONIZATION POINT FOR EACH OF THE
C                           SOUNDINGS INSTRUMENTS (DIM 2 X 3).
C                           (TIME IS IN FORMAT CENTURY DAY, MILLISECOND
C                            OF DAY).
C      TERRPERC      INT    MINIMUM PERCENTAGE OF SPOTS NECESSARY
C                           TO DECLARE AN AREA OPEN OCEAN.
C      TIME          INT    THE PARTITION HEADER TIME SET TO 0000Z.
C      TROPTHR       REAL   TROPICAL THRESHOLD FOR 30N-30S
C      TSAN_ON       INT    ON/OFF FLAG FOR APPLICATION OF TSAN MO'S
C                           ADJUSTMENTS.
C      TMOSTADJ      REAL   ARRAY OF TSAN MO'S AMSU-A TEMPERATURE ANTENNA
C                           ADJUSTMENTS (DIM: ANSPOT X ANCHAN X 3)
C      ULCHIR89      REAL   UN-LIMB CORRECTED HIRS CHANNELS 8 AND 9.
C      X2COEF        REAL   ARRAY OF DIMENSION 3 WHICH CONTAINS THE
C                           X**2 TERM COEFFICIENTS FOR AMSU-A CHANNELS
C                           4,5, & 6 (CLOUD CLEARED)
C      XCOEF         REAL   ARRAY OF DIMENSION 3 WHICH CONTAINS THE
C                           X TERM COEFFICIENTS FOR AMSU-A CHANNELS
C                           4,5, & 6 (CLOUD CLEARED)
C
C    BLOCK DATA- B$MAXPTR, B$RTPLVL, B$LEVELS, B$VERID
C
C    SUBPROGRAMS CALLED- OPENCDB, GTCDBV, SYNDVR, DREADM, DATIM1,
C        HRG1IN, AAG1IN, ABG1IN,  AVG1IN, COLX2Y, YRJDAY, BTCHK1, CLOSFL,
C        SPGDV$, INPDVR, MAPDVR, CLRBUF, CONDVR, FRMHIR, HIRODB,
C        FRMAMA, AMAODB, FRMAMB, AMBODB, CLCDBV, RCDBV,  GLCDVR,
C        COLDVR, BINDVR, CLDTOP, NSDVR,   RETDVR, FRMRET,
C        RETODB, INIMON, CNFRNG, LCCDBV, RTHLOC, KIDS, ODBTIM
C        POLRED, STATS,GLCDVR2,AMASFC, INNOHIRS, MHG1IN, B1SERR
C
C    FUNCTIONS USED- LCONFG, BTEST
C
C    EXIT STATES- UPON A RETURN OF ERSTAT = 99 FROM ANY
C                 SUBPROGRAM, PROGRAM TERMINATION IS PERFORMED.
C                 ERSTAT OF 99 INDICATES A FATAL ERROR WAS ENCOUNTERED.
C                 AN EXIT STATE OF 88 INDICATES THAT A MISLOCATED ORBIT
C                 WAS ENCOUNTERED.
C
C***********************************************************************
C
      INCLUDE'INPCDB'
      INCLUDE'BOXPRM'
      INCLUDE'INSPARM'
      INCLUDE'FRMINF'
      INCLUDE'INPARM'
      INCLUDE'ERPARM'
      INCLUDE'ERINFO'
      INCLUDE'INSCOM'
      INCLUDE'COLLOC'
      INCLUDE'SPGUNT'
      INCLUDE'RTVPARM'
      INCLUDE'RETPARM'
      INCLUDE'RTVGRD'
      INCLUDE'MONSTA'
      INCLUDE'OZONE'
      INCLUDE'STBCOM'
      INCLUDE'NMCCOM'
      INCLUDE'SPVAL'
      INCLUDE'RTPLVL'
      INCLUDE'EFFCOE'
      INCLUDE'HIRSTAT'
      INCLUDE'AMASTAT'
      INCLUDE'R1TDAT2000'
      INCLUDE'ATTENU8'
      INCLUDE'FILTRCNT'
      INCLUDE'NONLCDAT'
      INCLUDE'DEBUG'
      INCLUDE'I2HINF'
      INCLUDE'LCCOEF'
      INCLUDE'CLDPRC'
      INCLUDE'GROSSCHK'
      INCLUDE'FLGCOM'
      INCLUDE'NOHIRSCF'
      INCLUDE'MSG1BS'
      INCLUDE'TSTCNT'
      INCLUDE'RFICOR'
      INCLUDE'HIRUSE'
C
      INTEGER      AODFLN, AHEDLN, BODFLN, BHEDLN
      PARAMETER (AODFLN=232,AHEDLN=AODFLN/4)
      PARAMETER (BODFLN=100,BHEDLN=BODFLN/4)
      CHARACTER*1  C1TEMP
      CHARACTER*2  C2TEMP
      CHARACTER*3  C3TEMP
      CHARACTER*4  C4TEMP
      CHARACTER*5  C5TEMP
      CHARACTER*6  C6TEMP
      CHARACTER*8  SATID, NAME1, NAME2, NAME3
      CHARACTER*10 ACCES
      CHARACTER*33 SHORTNAME
      CHARACTER*98 HRNAME, AANAME, ABNAME, AVNAME, HOLDNAME
      CHARACTER*100 KMSG
C
      INTEGER PPRLEN
      PARAMETER (PPRLEN=50)
      INTEGER STPPY(PPRLEN),STPPJ(PPRLEN),STPPT(PPRLEN),ENPPY(PPRLEN)
      INTEGER ENPPJ(PPRLEN),ENPPT(PPRLEN),FRAMETIME,IDATIM(6)
      INTEGER STPPTIME(PPRLEN), ENPPTIME(PPRLEN),PPRFLG(PPRLEN)
      INTEGER TOTRET, CUMRET,IAMAOFF(15)
      INTEGER FRMTIM(3),BADUNT, ODFREC, AODREC, HODREC, DUMTIM(3)
      INTEGER STAT1T, R1TLEN, BODREC, RFIUNT, CDAY, MSEC, IDUMMY
      INTEGER*4 HODFHD(100), AODFHD(AHEDLN), RODFHD(250)
      INTEGER*4 BODFHD(BHEDLN)
      INTEGER SYNCTR(3), SYNTIM(2,3), CDBST, STATUS, DAY, FRDATE
      INTEGER DUMMY(2), TIME, ERSTAT, HDRMXT(2), FRYR,FRAMNUM
      INTEGER MAPST, CHNCMB(2,4,2),CLDCHN(2),RETMET(3),FRHR,SPGTEST
      INTEGER I, J, K, AMASPT(HNSPOT,HTOTLN),SPGMAXFRM
      INTEGER*2 RODFBUF(500), HODFBUF(200), HRVNUM, AAVNUM, ABVNUM, AVVNUM
      INTEGER*4 RETCOD, SCNAMA, SCNAMB, SCNHIR, STAT1B, SCNDUM
      INTEGER AMALNE(HNSPOT,HTOTLN), CNFLIM,ORBDUM(3),FRHREC(30),SHORTORB(2)
      INTEGER ITEMP1,ITEMP2,INMC,IE,JE,KE,H1,H2,FCSTYM(2), AOSTAT
      INTEGER NPPROC, NXTPPROC,PPR, SMSSTAT, ACTUALRET,MISLOCFG
      INTEGER OUTPPR(7),INSTON(4),BSHFT,HODF1ST,HLAST,AODF1ST,ALAST
      INTEGER HIRCNT,HIRFRMREC,AMACNT,AMAFRMREC,HIRLIN1,HIRLNLAST
      INTEGER LIMAREC, LIMHREC, GETLIMOBH, GETLIMOBA, RUNFILTR,IDM(6)
      INTEGER AMBFRMREC, AMBCNT, BODF1ST, BLAST, LIMBREC
      INTEGER LNDSEAFG,LIMBADJ,SIMCHN(35),MISLCHECK,MISLOC,TSAN_ON
      INTEGER NDIM,CHKPREC,TERRPERC,IHIROFF,AT8ONOFF,CLDONOFF
      INTEGER N16HIR,MISSTAT,LIMRECB, GETLIMOBB, RFCORR
      INTEGER IEND, ICH, ISTART, ISTAT, IRET
      INTEGER ITERRAIN, IHS, IHL, IHCH, CLDCOUNT
      INTEGER IAS, IAL, IACH, IERR, H1REPLACE, EXPTERRAIN
      INTEGER IDUM, IB, ISI, ICI, IKI, ICOEFF, ICH2, PPFRAME
      INTEGER IFND, IDUMDY, IDUMHR, IDUMMIN, OQCHEKD, OQBAD, OQSTAT
      INTEGER aerocnt, extcnt, CTPEXIT(15), cpelt5pct, aeroqc2, countqc3
      integer clearcount(4), failcount
      real    clearavg(4,4)
      REAL DELSPT(HNSPOT,HTOTLN),DELSCN(HNSPOT,HTOTLN)
      REAL*4 HKBUFR(4)
      REAL OBSHIR(HNSPOT,HTOTLN,35),HLDNOD(HNSPOT,HTOTLN)
      REAL ULCHIR89(HNSPOT,HTOTLN,2),ETA(15),CLRHGT,LIMHGT,LIMHG2
      REAL OLR(HNSPOT,HTOTLN), LC1(HNSPOT,HTOTLN), MISLAND, MISSEA
      REAL LC2(HNSPOT,HTOTLN), LC3(HNSPOT,HTOTLN)
      REAL LC4(HNSPOT,HTOTLN), DELTIME,VALAMA(ADATEL)
      REAL OBSAMA(ANSPOT,ATOTLN,ADATEL),X2COEF(6),XCOEF(6)
      REAL OBSAMB(BNSPOT,BTOTLN,BDATEL)
      REAL TMOSTADJ(ANSPOT,15,3),SEAINPUT(16),LNDINPUT(16)
      REAL PCLNGD,PCLNQU,PCLNBD,PCOCGD,PCOCQU,PCOCBD
      REAL MAXLMT(2),CLWTHR,LPITHR,SAVECH20(HNSPOT,HTOTLN)
      REAL LATLMT,CH4THRES,CLWADJ,NRTHLM,STHLIM
      REAL CH5LIM,CH4LIM,AVLLIM,ALTERLIM
      REAL CLWZERO,CHN5LIM,C5THRES,LT5PCT, RTEMP, PCTCLEAR
      REAL AMBINPUT(6)
      REAL POLARTHR,MIDLATTR,TROPTHR,Q4,Q5,Q4P,CLWADJ5
      REAL CLWADJ4, CHN4SEL, CHN5SEL, A5THRES, CH5THRES
      REAL PRCNTML, PCGOOD, PCQUES, PCBAD, RDUMMY,OQLIMT, OQPCNT
      LOGICAL FSTFRM(4), CLOUDY(HNSPOT,HTOTLN), LCONFG, FIRST
      LOGICAL PRFLAG(RNGRDP,RTOTLN),R1T1ST,CLD1ST,PPR1ST, ONCE
      LOGICAL STB1ST,RTHLOCERR,PRECIPFG(HNSPOT,HTOTLN)
      LOGICAL FULFRM, METOP
      PARAMETER (R1TLEN=2000, RFIUNT=100)
      CHARACTER*5 STARTORB
      CHARACTER*7 ORBITNUMS
      CHARACTER*15  STABDESCR(5)
      INTEGER COLUNT, CRECL
      PARAMETER(COLUNT=46,CRECL=680)
      DATA ETA /.01,.08,.03,.04,.04,.03,.03,7*.04,.11/
      DATA STABDESCR / 'NOT USED       ',
     2                 'SUPERADIABATIC ',
     3                 'OBS. QUALITY   ',
     4                 'SML NOT = 40   ',
     5                 'MISSING HEIGHTS'/

C
C     DATA IFIL/ 35, 36, 37, 38, 39/
C
      FIRST = .TRUE.
      R1T1ST = .TRUE.
      CLD1ST = .TRUE.
      PPR1ST = .TRUE.
      STB1ST = .TRUE.
      BADUNT = I4SPV
      CUMRET = 0
      ACTUALRET = 0
      OQCHEKD = 0
      RTHLOCERR = .FALSE.
      DO I=1,32
         BADHFLAG(I) = 0
         BADAFLAG(I) = 0
         BADMFLAG(I) = 0
      END DO
      NOH1BS = .FALSE.
      ACCES = 'DIRECT    '
      SCNAMA = 0
      SCNAMB = 0
      SCNHIR = 0
      PPFRAME = 0
      cpelt5pct = 0
      failcount = 0
C
C  READ IN THE SATELLITE ID FROM THE SCRIPT, UNIT 5.  THEN
C  RE-READ THIS LINE, READING THE LAST 2 CHARACTERS AS
C  INTEGER.
C
       READ(5,'(A8)') SATID
       REWIND(5)
       READ(5,'(A6,I2)') C6TEMP,SATNM
       SID=SATNM
       PRINT *,'Satellite ID is ',SATID


       IF (SATID(1:1).EQ.'M') THEN
          METOP = .TRUE.
       ELSE
          METOP = .FALSE.
       END IF

       print *,' METOP Flag = ',METOP
C
C  READ IN THE AVAILABLE INSTRUMENTS FLAG FROM UNIT 5. 1 DENOTES
C  THE INSTRUMENT IS ON, 0 INDICATES IT IS NOT. THE INSTRUMENTS
C  ARE ORDERED HIRS, AMSU-A, AMSU-B/MHS, AND AVHRR, I.E., 1, 2, 3,
C  AND 4, RESPECTIVELY. FOR EACH OF THESE INSTRUMENTS, READ
C  THE FLAG WHICH INDICATES THE PRESENCE OF A 1B* FILE NAME IN
C  UNIT 5 INPUT (1=PRESENT, 0=NOT PRESENT)
C
       READ(5,*) INSTON(1),INSTON(2),INSTON(3),INSTON(4)
C
C SET THE NO-HIRS 1B* FLAG TO TRUE IF NO HIRS 1B* IS AVAILABLE.
C
      IF(INSTON(1).NE.1) NOH1BS = .TRUE.
C
C  READ IN THE HIRS DATASET NAME FROM THE SCRIPT, UNIT 5.
C
      IF(INSTON(1).EQ.1) READ(5,'(A98)') HRNAME
C
C  READ IN THE AMSU-A DATASET NAME FROM THE SCRIPT, UNIT 5.
C
      IF(INSTON(2).EQ.1) READ(5,'(A98)') AANAME
C
C  READ IN THE AMSU-B DATASET NAME FROM THE SCRIPT, UNIT 5.
C
        IF(INSTON(3).EQ.1) READ(5,'(A98)') ABNAME
C
C  READ IN THE GAC DATASET NAME FROM THE SCRIPT, UNIT 5.
C
        IF(INSTON(4).EQ.1) READ(5,'(A98)') AVNAME
C
C READ IN THE ORBIT NUMBER FROM THE SCRIPT, UNIT 5 AND CONVERT TO
C START AND END ORBIT NUMBERS FOR ALL INSTRUMENTS.  ALSO READ
C HIRS, AMSU-A, GAC, AND AMSU-B 1B* STRUCTURE VERSION NUMBERS.
C
      ORBITNUMS = ' '
      READ(5,'(A7)') ORBITNUMS
      STARTORB(1:5) = ORBITNUMS(1:5)
      IF (ORBITNUMS(6:7).EQ.'__') THEN
         READ(STARTORB,'(I3,I2)') SHORTORB
         ORBDUM(1) = SHORTORB(1)
         ORBDUM(2) = SHORTORB(2)
         IF (ORBDUM(2).EQ.99) THEN
            ORBDUM(3) = 00
         ELSE
            ORBDUM(3) = ORBDUM(2) + 1
         END IF
      ELSE
         READ(ORBITNUMS,'(I3,I2,I2)') ORBDUM
      END IF
      SORB(1) = ORBDUM(1)*100 + ORBDUM(2)
      EORB(1) = ORBDUM(1)*100 + ORBDUM(3)
      IF(EORB(1).LT.SORB(1)) EORB(1) = EORB(1) + 100
      SORB(2) = SORB(1)
      SORB(3) = SORB(1)
      EORB(2) = EORB(1)
      EORB(3) = EORB(1)
C
C  INITIALIZE COUNTERS FROM COMMON /MONSTA/.
C
      CALL INIMON
C
C  INITIALIZE KIDS FILE AND SET START TIMES IN /MONSTA/.
C
      SUBNAM = 'SPGDVR'
      CALL DATIM1(IDATIM)
      IF(IDATIM(1).EQ.98.OR.IDATIM(1).EQ.99) THEN
         IDATIM(1) = 1900 + IDATIM(1)
      ELSE
         IDATIM(1) = 2000 + IDATIM(1)
      ENDIF
      IDATIM(2) = IDATIM(5)/100
      IDATIM(5) = IDATIM(5)-(IDATIM(2)*100)
      IDATIM(6) = IDATIM(6)/100
C
C  STORE THE START DATE AND TIME FOR THE MONITORING STATS FILE.
C
      STRDAY = ((IDATIM(1)*10000)+(IDATIM(3)*100)+IDATIM(4))
      BEGTIM = ((IDATIM(2)*10000)+(IDATIM(5)*100)+IDATIM(6))
C
C  OPEN KIDS FILE
C
      KMSG = '*** '
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,1)
C
C  WRITE NAME OF ORBIT TO KIDS FILE. SINCE THE AMSU-A MUST
C  BE PRESENT IN ORDER TO MAKE RETRIEVALS, USE THE AMSU-A
C  1B* NAME FOR THE ORBIT NAME.
C
      IEND=0
      DO 1 ICH=98,1,-1
         IF(AANAME(ICH:ICH).NE.' ') THEN
            IEND=ICH
            GO TO 2
         ENDIF
    1 CONTINUE
    2 IF(IEND.NE.0) THEN
         ISTART = IEND-32
         SHORTNAME=AANAME(ISTART:IEND)
      ENDIF
      KMSG(1:50)='*** B E G I N  O R B I T A L  P R O C E S S I N G '
      KMSG(51:100)=' FOR                                            '
      KMSG(56:88) = SHORTNAME
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
C
C  CALL OPENCDB TO SET UP THE CDB, OPCDB AND MAPPARM FILES WORKSPACE.
C
      SUBNAM='SPGDVR'
      CALL OPENCDB(CDBUNT,KUNIT,SUBNAM,CDBST)
      IF(CDBST.NE.0) THEN
         ERSTAT=99
         GO TO 1010
      ENDIF
      CALL OPENCDB(UNTOPC,KUNIT,SUBNAM,CDBST)
      IF(CDBST.NE.0) THEN
         ERSTAT=99
         GO TO 1010
      ENDIF
      CALL OPENCDB(MAPUNT,KUNIT,SUBNAM,CDBST)
      IF(CDBST.NE.0) THEN
         ERSTAT=99
         GO TO 1010
      ENDIF

C
C  GET THE HIRS-REMOVAL ON-OFF FLAG.
C

      CALL INTCOEF(CDBUNT,'RETRIEVE','NOHIR   ','HIROFF  ',1,0,0,0,
     1             IHIROFF,'GET',STATUS)
      IF (IHIROFF .EQ. 1) HIROFF = .TRUE.
      IF (NOH1BS) HIROFF = .TRUE.
C
C  GET THE AMSU-A CHANNEL IGNORE FLAG.
C
      CALL INTCOEF(CDBUNT,'RETRIEVE','NOAMA   ','AMAOFF  ',15,0,0,0,
     1            IAMAOFF,'GET',STATUS)
      DO I=1,15
         IF(IAMAOFF(I).EQ.1)THEN
            AMAOFF(I)=.TRUE.
         ELSE
            AMAOFF(I)=.FALSE.
         ENDIF
      END DO
C
C  CALL GTCDBV TO GET THE CDB VALUES USED IN DATA SYNCHRONIZATION AND
C  INPUT.
C
      CALL GTCDBV(CDBUNT,AT8ONOFF,CDBST)
      IF(CDBST.NE.0) THEN
         ERSTAT=99
         GOTO 1010
      ENDIF
C
C INITIALIZE AND OPEN 1B* DATASETS, IF A 1B* DOES INDEED EXIST.
C
      CONFIG = INTCFG
      IF(INSTON(1).EQ.1) THEN
         CALL HRG1IN(HUNIT,HSTRUC,HRVNUM,HRNAME,ACCES,RETCOD)
         HOLDNAME=HRNAME
         ERSTAT = RETCOD
         IF(ERSTAT .LT. 0) THEN
            BADUNT = HUNIT
            STATUS=ERSTAT
            ERSTAT=99
            GOTO 920
         ENDIF
C
C IF NO GOOD HIRS SCANS ARE FOUND, SET FLAGS TO PROCESS IN
C AMSU-A ONLY MODE.
C
         CALL GETTIM(1,CDAY,MSEC,SCNDUM,ERSTAT)
         IF (.NOT.(BTEST(CONFIG,31))) THEN
            INSTON(1) = 0
            NOH1BS    = .TRUE.
            HIROFF    = .TRUE.
            IDUMMY = IBSET(CONFIG,31)
            KMSG( 1:48)='GETTIM DID NOT FIND ANY GOOD HIRS SCANS. SPGDVR '
            KMSG(49:82)='IS PROCESSING IN NO HIRS 1B* MODE.'
            PRINT *,KMSG
            CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         END IF
      ENDIF
      IF(INSTON(2).EQ.1) THEN
         CALL AAG1IN(AUNIT,ASTRUC,AAVNUM,AANAME,ACCES,RETCOD)
         HOLDNAME=AANAME
         ERSTAT = RETCOD
         IF(ERSTAT .LT. 0) THEN
            BADUNT = AUNIT
            STATUS=ERSTAT
            ERSTAT=99
            GOTO 920
         ENDIF
      ENDIF
C
C  DETERMINE IF THIS IS A TEST RUN OF SPGDVR (1=TEST,0=NO-TEST). IF IT IS,
C  THE SECOND NUMBER IS THE MAXIMUM NUMBER OF FRAMES TO PROCESS.  IF THIS
C  IS NOT A TEST RUN, THE SECOND NUMBER IS IGNORED IN FURTHER PROCESSING.
C  THE THIRD NUMBER IS THE VERSION OF HIRS BEING USED. A 3 IS HIRS/3
C  (NOAA-KLM), WHILE A 4 IS HIRS/4 (STARTING WITH NOAA-N).

      READ(70,*) SPGTEST,SPGMAXFRM,HIRTYP
      IF(SPGTEST.EQ.1) THEN
         KMSG(1:50)='W A R N I N G  - THIS IS A TEST RUN OF SPGDVR - NO'
         KMSG(51:100)=' MORE THAN    FRAMES WILL BE PROCESSED!         '
         WRITE(C2TEMP,'(I2)') SPGMAXFRM
         KMSG(62:63) = C2TEMP
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
C     IF THE AMSU-B INSTRUMENT IS PRESENT, OPEN THE AMSU-B 1B*.
C     IF THE HIRS/4 INSTRUMENT IS PRESENT, ASSUME THE MHS INSTRUMENT
C     IS BEING USED INSTEAD OF THE AMSU-B.
C
      IF (INSTON(3).EQ.1) THEN
         IF (HIRTYP.EQ.3) THEN
            CALL ABG1IN(BUNIT,BSTRUC,ABVNUM,ABNAME,ACCES,RETCOD)
         ELSE IF (HIRTYP.EQ.4) THEN
            CALL MHG1IN(BUNIT,BSTRUC,ABVNUM,ABNAME,ACCES,RETCOD)
         END IF
         ERSTAT = RETCOD
         IF(ERSTAT .LT. 0) THEN
            BADUNT = BUNIT
            HOLDNAME = ABNAME
            STATUS = ERSTAT
            ERSTAT = 99
            GO TO 920
         END IF
      END IF
C
C     IF THE HIRS IS AVAILABLE AND IS TO BE USED, OPEN THE GAC 1B*.
C
      IF (HIROFF) INSTON(4) = 0
      IF(INSTON(4).EQ.1) THEN
         CALL AVG1IN(GUNIT,GSTRUC,AVVNUM,AVNAME,ACCES,RETCOD)
         ERSTAT = RETCOD
         IF(ERSTAT .LT. 0) THEN
            BADUNT = GUNIT
            STATUS= ERSTAT
            ERSTAT=99
            GOTO 920
         ENDIF
      ENDIF
C
C  READ ON/OFF FLAG FOR TSAN MO'S ADJUSTMENTS AND ON/OFF FLAG FOR
C  REPLACING HIRS 1 WITH MEAN OF AMSU-A 10-14 IN TOTAL OZONE AND
C  PREDICTED CLEAR HIRS 8 COMPUTATION.
C
      READ(70,*) TSAN_ON, H1REPLACE
      IF(TSAN_ON.NE.0.AND.TSAN_ON.NE.1) TSAN_ON = 0
      IF(TSAN_ON.EQ.1) THEN
         KMSG(1:50)='AMSU-A TEMPERATURE ANTENNA ADJUSTMENTS WILL NOT BE'
         KMSG(51:100)=' APPLIED TO THIS ORBIT.'
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
      H1REPL = .FALSE.
      IF (H1REPLACE.EQ.1) H1REPL = .TRUE.
C
C  OPEN THE FIRST GUESS LIBRARIES
C
      OPEN(CLRUNT,IOSTAT=STATUS,ACCESS='DIRECT',RECL=FGLBRL)
      IF(STATUS .NE. 0) THEN
         BADUNT=CLRUNT
         ERSTAT=99
         GOTO 950
      ENDIF
      OPEN(CLDUNT,IOSTAT=STATUS,ACCESS='DIRECT',RECL=FGLBRL)
      IF(STATUS .NE. 0) THEN
         BADUNT=CLDUNT
         ERSTAT=99
         GOTO 950
      ENDIF
C
C  OPEN THE R1T FILE AND READ THE HEADER INTO R1THKP IN COMMON
C  R1TDAT2000.
C
      OPEN(R1TUNT,IOSTAT=STAT1T,ACCESS='DIRECT',RECL=R1TLEN)
      IF(STAT1T.NE.0) THEN
         BADUNT=R1TUNT
         GO TO 950
      ENDIF
      READ(R1TUNT,REC=1,IOSTAT=STAT1T) R1THKP
      IF(STAT1T.NE.0) GO TO 815
C
C  INITIALIZE THE R1T RECORD COUNT FOR THIS ORBIT TO ZERO.
C
      R1TORB = 0
C
C     OPEN STABILITY INPUT FILE.
C
      OPEN (UNIT=34, ERR=800, ACCESS='SEQUENTIAL', FORM='FORMATTED')
C
C     READ FOUR PRESSURE FOR STABILITY DEPARTURE CALCULATIONS.
C
      READ(34, '(4I5)', ERR=800) PINPUT
C
C     READ STABILITY DEBUG LEVEL.
C
      READ(34, '(I1)', ERR=800) STDBUG
C
C     READ MAXIMUM DIFFERENCE FOR SEARCHING FCST FILES.
C
      READ(34, '(I3)', ERR=800) STBDIF
C
C     READ SURFACE MODEL LEVEL TEST LIMIT.
C
      READ(34, '(I3)', ERR=800) SMLIMIT
C
      DO 20 I=1,4
         IPRESS(I) = 0
         DO 10 J=1,PRFTLV
            IF (REAL(PINPUT(I)) .EQ. PROFLV(J)) THEN

               IPRESS(I) = J
               GO TO 20
            ENDIF
   10    CONTINUE
C
         IF(IPRESS(I) .EQ. 0) GOTO 810
C
   20 CONTINUE
C
C  LOOP OVER THE 5 NMC FILES.
C
      DO 30 INMC = 1, 5
C
C  OPEN NMC FORECAST FILE FOR A DIRECT READ.
C
         OPEN (IFIL(INMC),ERR=820,ACCESS='DIRECT',RECL=21460)
C
C  READ THE HOUSEKEEPING RECORD.
C
         READ(IFIL(INMC),REC=1,ERR=820) HKBUFR
C
C  UNPACK THE FOUR BYTE WORDS INTO THE EIGHT BYTE REAL ARRAY.
C
         YEARS(INMC) = HKBUFR(1)
         MONS(INMC)  = HKBUFR(2)
         DAYS(INMC)  = HKBUFR(3)
         HOURS(INMC) = HKBUFR(4)
C
   30 CONTINUE
C
C  GET DEBUG PRINT FLAGS FROM CDB.
C
      CALL INTCOEF(CDBUNT,'GOODIES ','DEBUG   ','PRINT   ',1,0,0,0,
     1             DBUGPRT,'GET',STATUS)
      IF(STATUS.NE.0) GO TO 1010
C 
      CALL INTCOEF(CDBUNT,'GOODIES ','DEBUG   ','1BSTAR  ',1,0,0,0,
     1             DBG1BSTR,'GET',STATUS)
      IF(STATUS.NE.0) GO TO 1010
C
C  IF 1B* PRINT FLAG IS ON, READ DEBUG PRINT FILE TO DETERMINE WHAT TYPE
C  OF DATA TO PRINT.  IF THERE ARE ANY PROBLEMS READING THE FILE, TURN
C  THE 1B* PRINT FLAG OFF.
C
      IF(DBG1BSTR.EQ.1) THEN
         CALL RDBG1BS(STATUS)
         IF(STATUS.NE.0) THEN
            DBG1BSTR = 0
            KMSG='1B* DEBUG PRINT TURNED OFF DUE TO ERROR IN RDG1BS'
         ELSE
            KMSG='1B* DEBUG PRINT IS ON - BEWARE LARGE FT06 OUTPUT'
         ENDIF
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
C
c  Commented out 6/13/2006 Frank Tilley
C  GET FILTER FLAG FROM CDB - 1 = RUN FILTER, ELSE = DO NOT RUN FILTER
C
c     CALL INTCOEF(CDBUNT,'CHECK3D ','RUN     ','D3CON   ',1,0,0,0,
c    1             RUNFILTR,'GET',STATUS)
c     IF(STATUS.NE.0) GO TO 1010
c     IF(RUNFILTR.NE.1) RUNFILTR = 0
c     IF(RUNFILTR.EQ.0) THEN
c       KMSG='FILTER (D3CON) IS NOT BEING RUN FOR THIS ORBIT.'
c       CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
c     ELSE
c       KMSG(1:50)='D3CON CDB FLAG CHECK3D.RUN.D3CON IS TURNED ON, BUT'
c       KMSG(51:100)=' D3CON IS COMMENTED OUT IN THE SOFTWARE SINCE IT'
c       CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
c       KMSG(1:50)='CANNOT RUN WITHOUT SIGNIFICANT MODIFICATIONS.  THE'
c       KMSG(51:100)=' RUNFILTR FLAG IS THUS TURNED OFF FOR THIS RUN.'
c       CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
c       RUNFILTR=0
c     ENDIF
C
C  GET THE RETRIEVAL SPOT SELECTION CONTROL PARAMETERS.
C
      CALL REALCOEF(CDBUNT,'RETRIEVE','PICKSPOT','PCTCLR  ',1,0,0,0,
     1            PCTCLR,'GET',STATUS)
      PCTCLD = 1 - PCTCLR
      CALL REALCOEF(CDBUNT,'RETRIEVE','PICKSPOT','PCTSEA  ',1,0,0,0,
     1            PCTSEA,'GET',STATUS)
      CALL INTCOEF(CDBUNT,'RETRIEVE','PICKSPOT','NUMCLR  ',1,0,0,0,
     1            NUMCLEAR,'GET',STATUS)
C
C  GET THE SUPER ADIABATIC ADDITION VALUE (SUPADD).
C
      CALL REALCOEF(CDBUNT,'ADJUST  ','CHKSUP  ','SUPADD  ',1,0,0,0,
     1            SUPADD,'GET',STATUS)
C
C  READ CLOUDTEST FLAGS TO SEE WHICH ARE ON.
C
      PRCLSE = .FALSE.
      PRALBE = .FALSE.
      PRWCHN = .FALSE.
      PRSFCT = .FALSE.
      PRNAVR = .FALSE.
      PRICHR = .FALSE.
      PRADJS = .FALSE.
      PRHSFC = .FALSE.
      DO I=1,6
         NAVCMP(I) = .FALSE.
         IF(I.LE.5) ICHCMP(I) = .FALSE.
         IF(I.LE.3) ADJCMP(I) = .FALSE.
      END DO
      READ(70,'(A6,3X,I1)') C6TEMP,IDUM
      IF(IDUM.EQ.1) PRCLSE = .TRUE.
      READ(70,'(A6,3X,I1)') C6TEMP,IDUM
      IF(IDUM.EQ.1) PRALBE = .TRUE.
      READ(70,'(A6,3X,I1)') C6TEMP,IDUM
      IF(IDUM.EQ.1) PRWCHN = .TRUE.
      READ(70,'(A6,3X,I1)') C6TEMP,IDUM
      IF(IDUM.EQ.1) PRSFCT = .TRUE.
      READ(70,'(A6,3X,I1)') C6TEMP,IDUM
      IF(IDUM.EQ.1) PRNAVR = .TRUE.
      READ(70,'(A6,3X,I1)') C6TEMP,IDUM
      IF(IDUM.EQ.1) PRICHR = .TRUE.
      READ(70,'(A6,3X,I1)') C6TEMP,IDUM
      IF(IDUM.EQ.1) PRADJS = .TRUE.
      READ(70,'(A6,3X,I1)') C6TEMP,IDUM
      IF(IDUM.EQ.1) PRHSFC = .TRUE.
      READ(70,'(A6,3X,6(I1,1X))') C6TEMP,(IDM(I),I=1,6)
      DO I=1,6
         IF(IDM(I).EQ.1) NAVCMP(I) = .TRUE.
      END DO
      READ(70,'(A6,3X,5(I1,1X))') C6TEMP,(IDM(I),I=1,5)
      DO I=1,5
         IF(IDM(I).EQ.1) ICHCMP(I) = .TRUE.
      END DO
      READ(70,'(A6,3X,3(I1,1X))') C6TEMP,(IDM(I),I=1,3)
      DO I=1,3
         IF(IDM(I).EQ.1) ADJCMP(I) = .TRUE.
      END DO
C
C  GET CHANNELS TO BE GROSS LIMIT CHECKED
C
      READ(70,*) HCH2CHK
      KMSG(1:50)='HIRS CHANNELS TO GROSS LIMIT CHECK (1=AVAIL,0=N/A)'
      J = 0
      DO I=1,HNCHAN
         WRITE(C1TEMP,'(I1)') HCH2CHK(I)
         K = 51+ J
         KMSG(K:K) = C1TEMP
         J = J + 2
      END DO
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      READ(70,*) ACH2CHK
      KMSG(1:50)='AMSU-A CHANS. TO GROSS LIMIT CHECK (1=AVAIL,0=N/A)'
      J = 0
      DO I=1,ANCHAN
         WRITE(C1TEMP,'(I1)') ACH2CHK(I)
         K = 51+J
         KMSG(K:K) = C1TEMP
         J = J + 2
      END DO
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
C
C     GET CLDTOP ON/OFF FLAG
C
      READ(70,*) CLDONOFF
      IF(CLDONOFF.NE.1.AND.CLDONOFF.NE.0) CLDONOFF=0
      aerocnt = 0
      aeroqc2 = 0
      countqc3 = 0
      extcnt = 0
      
C
C     GET HIRS ATTENUATION FLAG ON/OFF
C     IF USING THE COEFFICIENTS, MUST ALSO CHANGE THE THRESHOLD
C     (THRESD)..WITH CURRENT COEFS TRY A VALUE BETWEEN 5 & 10.
C

      READ(70,*) AT8ONOFF
      IF(AT8ONOFF.NE.1.AND.AT8ONOFF.NE.0) CLDONOFF=0
C
C     SET THE CLDTOP ON/OFF FLAG TO OFF IF THE HIRS IS
C     TURNED OFF.
C
      IF (HIROFF) CLDONOFF = 0
      IF(CLDONOFF.EQ.0) THEN
         KMSG='CLOUD ALGORITHM IS TURNED OFF.'
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
C     GET STABILITY ON/OFF TESTS.
C
      READ(70,*) (STABTEST(I),I=1,6)
      IF(STABTEST(6).EQ.0) THEN
         KMSG='STABILTY DEPATURE DRIVER IS TURNED OFF.'
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ELSE
C
C    TEST 1 IN STBDVR IS COMMENTED OUT, SO DO NOT CHECK THAT FLAG.
C
         DO I=2,5
            IF(STABTEST(I).NE.1) THEN
               KMSG='STABILTY TEST   (               ) IS TURNED OFF.'
               WRITE(C1TEMP,'(I1)') I
               KMSG(15:15) = C1TEMP
               KMSG(18:32) = STABDESCR(I)
               CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
            ENDIF
         END DO
      ENDIF
C
C  GET FLAG TO DETERMINE WHAT LIMB CORRECTION TO USE
C
      READ(70,*) LNDSEAFG
      IF(LNDSEAFG .EQ. 0) THEN
        KMSG='USING THE GLOBAL LIMB COEFFICIENTS FOUND IN THE CDB.'
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ELSE
        KMSG='USING THE LIMB COEFFICIENTS STRATIFIED BY LAND/SEA.'
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
C  GET FLAG TO DETERMINE IF THE LIMB CORRECTED TEMPERATURE ARE
C  TO BE USED IN DETERMINING TPW & CLW
C  GET FLAG TO DETERMINE WHETHER OR NOT HIRS BRIGHTNESS TEMPS ARE
C  TO BE SHIFTED ONE SPOT TO THE RIGHT TO CORRECT FOR INSTRUMENT
C  MISALIGNMENT.
C  GET FLAG TO DETERMINE WHETHER OR NOT AMSU-B RFI CORRECTIONS ARE
C  TO BE APPLIED.
C
      READ(70,*) LIMBADJ, N16HIR, RFCORR
      IF(LIMBADJ  .EQ. 0) THEN
        KMSG='USING THE NON-LIMB CORRECTED AMSU-A FOR TPW CALCULATION'
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ELSE
        KMSG='USING THE LIMB CORRECTED AMSU-A FOR TPW CALCULATION'
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
C  THE CORRFI FLAG IN COMMON MEMBER /RFICOR/ CONTROLS WHETHER OR NOT
C  AMSU-B RFI CORRECTION IS PERFORMED. IF THIS FLAG IS SET TO TRUE,
C  CALL SUBROUTINE GETRFI TO READ RFI CORRECTION TABLES FROM A
C  DATA MEMBER.
C
      CORRFI = .FALSE.
      IF (RFCORR.EQ.1) THEN
         CORRFI = .TRUE.
         KMSG='AMSU-B RFI CORRECTION WILL BE PERFORMED FOR THIS ORBIT'
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         CALL GETRFI(RFIUNT)
      ELSE
         KMSG='NO AMSU-B RFI CORRECTION WILL BE PERFORMED FOR THIS ORBIT'
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      END IF
C
C  GET THE UPPER LIMITS OF TPW AND CLW FROM SOFTFLAGS DATASET
C
      READ(70,*) MAXLMT(1),MAXLMT(2)
C
C  GET THE COEFFICIENTS USED TO COMPUTE THE CLOUD CLEARED AMSU-A
C  CHANNELS. THE 1ST 3 COEFFICIENTS ARE FOR SEA THE 2ND 3 COEFFICIENTS
C  ARE FOR LAND
C
      READ(70,*) X2COEF(1),X2COEF(2),X2COEF(3),X2COEF(4),X2COEF(5),
     1           X2COEF(6)
      READ(70,*) XCOEF(1),XCOEF(2),XCOEF(3),XCOEF(4),XCOEF(5),XCOEF(6)
C
C  READ THE CLOUD LIQUID WATER AND LAND PRECIPITATION THRESHOLD VALUES
C  AND THE LATITUDE THRESHOLD AND AMSUA SELECTED CHANNEL FOR THE
C  PRECIPITATION SCREENING VIA MEDIAN FILTER AND CLOUD LIQUID WATER.
C  ALSO ADDED AMSUA CH4 TEMPERATURE THRESHOLD AND THE CLOUD LIQUID WATER
C  ADJUSTMENT FOR USE IN PRECIP/CLOUD SCREENING OF AMSUA CHANNEL 4 AND 5.
C  ADDED CLWZERO TO THE LIST.
C  ADDED Q4,Q5,Q4P,A4THRES,POLARTHR,MIDLATTR,TROPTHR (2/27/02,ASA)
C  REMOVED MAXCH6, MINCH6, FUDGEU4, FUDGEU5 AND A4THRES AND ADDED
C  CLWADJ5 (4/25/02,FHT)
C
      READ(70,*) CLWTHR,LPITHR,LATLMT,CH4THRES,CLWADJ,CLWZERO,
     >           POLARTHR,MIDLATTR,TROPTHR,Q4,Q5,Q4P,CLWADJ5
ccft  PRINT *, CLWTHR,LPITHR,LATLMT,CH4THRES,CLWADJ,CLWZERO

C
C  READ THE DATA MISLOCATION PROTECTION LIMITS AND THE PERCENT
C  MISLOCATED LIMIT AS WELL AS VALUES USED IN CLW CORRECTION TO
C  AMSU-A CHANNELS 4 AND 5. (WR 196, 04/24/2002)
C  INITIALIZE NUMBER OF FOVS CHECKED FOR MISLOCATION.
C
      READ(70,*) MISLAND, MISSEA, PRCNTML, CLWADJ4, CHN4SEL, CHN5SEL,
     >           A5THRES, CH5THRES, CHN5LIM, C5THRES, OQLIMT
      MISLCHECK = 0
C
C  READ THE SIZE OF THE SQUARE, THE PRECIPITATION CONTAMINATION FLAG AND
C  THE TERRAIN PERCENTAGE LIMIT FOR THE MEDIAN FILTER.
C  ADDED THE SEPARATE MEDIAN FILTER LIMITS FOR CHANNELS 4 AND 5 AND,
C  THE MINIMUM AVAILABILITY LIMITS FOR THE MEDIAN FILTER. (8/2/00)
C
      READ(70,*) NDIM,CHKPREC,TERRPERC,CH4LIM,CH5LIM,AVLLIM,ALTERLIM
ccft  PRINT *, ' FOR MEDIAN FILTER ',NDIM,CHKPREC,TERRPERC,CH4LIM,
c    >CH5LIM,AVLLIM,ALTERLIM
      IF(MOD(NDIM,2) .EQ. 0) THEN
       NDIM =7
       PRINT *, 'CHOSEN NDIM NOT AN ODD NUMBER, RESETTING NDIM TO 7.'
      ENDIF
C
C   IF NO HIRS 1B* IS AVAILABLE, CALL THE ROUTINE THAT GENERATES
C   AMSU-A TO HIRS INTERPOLATION COEFFICIENTS.
C
      IF (NOH1BS) THEN
         CALL INNOHIRS()
      END IF
C
C   BASED ON THE PRESENCE/ABSENCE OF ANY 1B* FILE, CLEAR THE INITIAL
C   CONFIG OF THAT BIT.  THIS WILL PREVENT ANY FURTHER PROCESSING FROM
C   TRYING TO READ THIS MISSING FILE. [IF NO HIRS 1B* IS AVAILABLE
C   DO NOT CLEAR THE HIRS CONFIGURATION BIT.  THE NOH1BS FLAG IS
C   USED INSTEAD.]
C

      DO IB=2,4
         IF(INSTON(IB).NE.1) THEN
            BSHFT = 32 - IB
            CONFIG = IBCLR(INTCFG,BSHFT)
            INTCFG = CONFIG
            IF(IB.EQ.2) THEN
              KMSG='CLEARED AMSA CONFIGURATION BIT BASED ON FT05 INPUT.'
            ELSEIF(IB.EQ.3) THEN
              KMSG='CLEARED AMSB CONFIGURATION BIT BASED ON FT05 INPUT.'
            ELSEIF(IB.EQ.4) THEN
              KMSG='CLEARED GAC CONFIGURATION BIT BASED ON FT05 INPUT.'
            ENDIF
            PRINT *, KMSG
            CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
            KMSG='THIS INSTRUMENT NOT USED IN FURTHER PROCESSING.'
            CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         ENDIF
      ENDDO
C
C  CALL SYNDVR TO OBTAIN COUNTERS FOR INSTRUMENT SYNCHRONIZATION
C  POINTS AND SYNCHRONIZATION TIMES.
C
      CALL SYNDVR(SYNCTR,SYNTIM,HDRMXT,SCNHIR,SCNAMA,SCNAMB,ERSTAT)
C
         KMSG='SYNCTR IS     ,     ,     .'
         WRITE(C4TEMP,'(I4)') SYNCTR(1)
         KMSG(11:14) = C4TEMP
         WRITE(C4TEMP,'(I4)') SYNCTR(2)
         KMSG(17:20) = C4TEMP
         WRITE(C4TEMP,'(I4)') SYNCTR(3)
         KMSG(23:26) = C4TEMP
         PRINT *, KMSG
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      IF (ERSTAT.NE.0) THEN
         ERSTAT=99
         GOTO 1010
      ENDIF
C
C  OPEN RODF, HODF, AODF, AND BODF FILES, AND INITIALIZE RECORD COUNTERS.
C
      OPEN(UNIT=ODBUNT,ACCESS='DIRECT',FORM='UNFORMATTED',
     1     RECL=1000,ERR=950)
      OPEN(UNIT=HIDFU,ACCESS='DIRECT',FORM='UNFORMATTED',
     1     RECL=400,ERR=950)
      OPEN(UNIT=AIDFU,ACCESS='DIRECT',FORM='UNFORMATTED',
     1     RECL=AODFLN,ERR=950)
      OPEN(UNIT=BIDFU,ACCESS='DIRECT',FORM='UNFORMATTED',
     1     RECL=BODFLN,ERR=950)
      ODFREC = 2
      HODREC = 2
      AODREC = 2
      BODREC = 2
C
C  OPEN THE PREVIOUSLY PROCESSED FILE, AND READ INFORMATION PERTAINING
C  TO LAST NPPROC-1 ORBITS PROCESSED BY THE SPGDVR.  CONVERT EACH
C  RECORD TO THE FORM YEAR*10000000+JDAY*100000*+HHMM.  DO NOT DO THIS
C  FOR AMSU-A ONLY MODE.
C
      OPEN(UNIT=FPUNIT,ERR=825,ACCESS='SEQUENTIAL',FORM='FORMATTED')
      READ(FPUNIT,107) NPPROC,NXTPPROC
      IF(NPPROC.GT.PPRLEN) THEN
        KMSG(1:50)='* WARNING! * PREVIOUSLY PROCESSED FILE CONTAINS MO'
        KMSG(51:100)='RE THAN    RECORDS - SPGDVR WILL ONLY CHECK   . '
        WRITE(C2TEMP,'(I2)') PPRLEN
        KMSG(59:60) = C2TEMP
        KMSG(95:96) = C2TEMP
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
        KMSG='             CHECK PREVIOUSLY PROCESSED FILE.    '
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
        NPPROC=PPRLEN
      ENDIF
      DO PPR=2,NPPROC
         READ(FPUNIT,109,ERR=825)STPPY(PPR),STPPJ(PPR),STPPT(PPR),
     1                           ENPPY(PPR),ENPPJ(PPR),ENPPT(PPR),
     1                           PPRFLG(PPR)
         STPPTIME(PPR)=STPPY(PPR)*10000000+STPPJ(PPR)*10000+STPPT(PPR)
         ENPPTIME(PPR)=ENPPY(PPR)*10000000+ENPPJ(PPR)*10000+ENPPT(PPR)
      ENDDO
C
C   CLOSE FILE UNTIL PROCESSING IS COMPLETED
C   AND INITIALIZE THE PROCESSED FLAG FOR THIS ORBIT.
C
      CLOSE(FPUNIT)
      OPEN(COLUNT,ACCESS='DIRECT',RECL=CRECL)
      OUTPPR(7) = 0

C
C  INITIALIZE THE CTP ADJUST COUNTERS
C
      DO I = 1, 15
         CTPEXIT(I) = 0
      END DO
      do i = 1, 4
         clearcount(i) = 0
         do j = 1, 4
            clearavg(i,j) = 0.0
         end do
      end do
C
C  INITIALIZE INSTRUMENT FIRST FRAME FLAGS.
C
  110 FSTFRM(1) = .TRUE.
      FSTFRM(2) = .TRUE.
      FSTFRM(3) = .TRUE.
      FSTFRM(4) = .TRUE.
C
      FRAMNO = 0
      GOODCNT = 0.0
      BADCNT = 0.0
      QUESCNT = 0.0
      TOTCONT = 0.0
      DO I=1,30
       FRHREC(I)=0
      ENDDO
      MISLOCFG = 0
      MISLOC = 0
      MISSTAT  = 0
C
C   INITIALIZE THE LIMB COEFFICIENT DATA COLLECTION COUNTER AND RETRIEVE
C   THE LIMB COEFFICIENT DATA COLLECTION FLAG.
C
      LIMAREC = 0
      LIMHREC = 0
      LIMRECB = 0
      CALL INTCOEF(CDBUNT,'LIMBCORR','AMSUA   ','GETOBS  ',1,0,0,0,
     1             GETLIMOBA,'GET',STATUS)
      IF(STATUS.NE.0) GO TO 1010
      IF(GETLIMOBA.EQ.1) THEN
         KMSG='AMSU-A LIMB CORRECTION DATA COLLECTION IS TURNED ON.'
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
      CALL INTCOEF(CDBUNT,'LIMBCORR','AMSUB   ','GETOBS  ',1,0,0,0,
     1             GETLIMOBB,'GET',STATUS)
      IF(STATUS.NE.0) GO TO 1010
      IF(GETLIMOBB.EQ.1) THEN
         KMSG='AMSU-B LIMB CORRECTION DATA COLLECTION IS TURNED ON.'
         PRINT *, KMSG
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
      CALL INTCOEF(CDBUNT,'LIMBCORR','HIRS    ','GETOBS  ',1,0,0,0,
     1             GETLIMOBH,'GET',STATUS)
      IF(STATUS.NE.0) GO TO 1010
      IF(GETLIMOBH.EQ.1) THEN
         KMSG='HIRS LIMB CORRECTION DATA COLLECTION IS TURNED ON.'
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
C   IF LIMB COLLECTION IS TO BE DONE, OPEN THE OUTPUT FILES.  IF THESE
C   FILES CANNOT BE OPENED, WRITE A WARNING MESSAGE, BUT THIS IS NOT
C   FATAL, SO ALLOW THE ORBIT TO PROCEED.
C
      IF(GETLIMOBH.EQ.1) THEN
         BADUNT = HLMUNT
         OPEN(UNIT=HLMUNT,ACCESS='DIRECT',FORM='UNFORMATTED',
     1        RECL=5168,ERR=112)
         GO TO 113
  112    CALL SPGDV$(303,BADUNT,HOLDNAME,STATUS)
         GETLIMOBH = 0
      ENDIF
  113 IF(GETLIMOBA.EQ.1) THEN
         BADUNT = ALMUNT
         OPEN(UNIT=ALMUNT,ACCESS='DIRECT',FORM='UNFORMATTED',
     1        RECL=2176,ERR=114)
         GO TO 115
  114    CALL SPGDV$(303,BADUNT,HOLDNAME,STATUS)
         GETLIMOBA = 0
      ENDIF
  115 IF(GETLIMOBB.EQ.1) THEN
         BADUNT = BLMUNT
         OPEN(UNIT=BLMUNT,ACCESS='DIRECT',FORM='UNFORMATTED',
     1        RECL=2896,ERR=116)
         GO TO 128
  116    CALL SPGDV$(303,BADUNT,HOLDNAME,STATUS)
         GETLIMOBB = 0
      ENDIF

C     ***********************************************************
C     THE FOLLOWING CODE READS IN TSAN MO'S AMSU-A TEMPERATURE
C     ADJUSTMENT. INITIALLY IT WILL BE SET TO ZERO UNTIL MITCH
C     GOLDBERG'S LIMB CORRECTION COEFFICIENTS ARE TESTED.
C     THIS CODE WAS MOVED BEFORE THE FRAME PROCESSING SO THAT IT
C     WOULD BE ONLY READ ONCE INSTEAD OF FOR EACH FRAME.
C     ***********************************************************

  128  IF(TSAN_ON.EQ.1) THEN
          DO ISI=1,ANSPOT
             READ(71,8110)((TMOSTADJ(ISI,ICI,IKI),IKI=1,3),ICI=1,5)
             READ(71,8110)((TMOSTADJ(ISI,ICI,IKI),IKI=1,3),ICI=6,10)
             READ(71,8110)((TMOSTADJ(ISI,ICI,IKI),IKI=1,3),ICI=11,15)
          ENDDO
          CLOSE(71)
       ENDIF
 8110  FORMAT(15F10.6)

C     ***********************************************************
C     THE FOLLOWING CODE READS IN THE LAND AND SEA VERSIONS OF
C     THE LIMB CORRECTION COEFFICIENTS. IT IS ASSUMED THAT THE
C     PREDICTORS ARE THE SAME FOR THE LAND AND SEA COEFFICIENTS
C     THIS IS FOR AMSU-A ONLY.
C     ***********************************************************
       DO ICH=1,ANCHAN
       DO ISI=1,ANSPOT
        READ(73,8111) (SEAINPUT(ICI),ICI=1,ANCHAN+1)
        READ(74,8111) (LNDINPUT(ICI),ICI=1,ANCHAN+1)
 8111    FORMAT(16(1X,F13.7))
        ICOEFF=0
        DO ICH2=1,ANCHAN+1
         IF(SEAINPUT(ICH2) .NE. 0) THEN
          ICOEFF=ICOEFF + 1
          SEALAMA(ICH,ICOEFF,ISI)=SEAINPUT(ICH2)
          LNDLAMA(ICH,ICOEFF,ISI)=LNDINPUT(ICH2)
         ENDIF
        ENDDO
       ENDDO
       ENDDO
       CLOSE(73)
       CLOSE(74)
C
C     ***********************************************************
C     THE FOLLOWING CODE READS IN THE GLOBAL LIMB CORRECTION
C     COEFFICIENTS FOR AMSU-B.
C     ***********************************************************
       DO ICH= 1, BNCHAN
          DO ISI = 1, BNSPOT
             READ(75,8112) (AMBINPUT(ICI),ICI=1,6)
             ICOEFF = 0
             DO ICH2 = 1, 6
                IF (AMBINPUT(ICH2).NE.0.0) THEN
                   ICOEFF = ICOEFF + 1
                   LCCAMB(ICH,ICOEFF,ISI) = AMBINPUT(ICH2)
                END IF
             END DO
          END DO
       END DO
       CLOSE(75)
 8112  FORMAT(F11.6,5(1X,F11.6))
C
C
C   LOOP OVER DATA FRAMES WHILE CONFIGURATION IS ACCEPTABLE:
C
  130 CONTINUE
C
C   STOP PROCESSING IF THE AMSU-A IS NOT AVAILABLE.
C
      IF ((.NOT.(BTEST(CONFIG,30)).OR.(.NOT.(LCONFG(CONFIG,CDBCFG))))) THEN
C
C   WE HAVE DROPPED BELOW MINIMUM CONFIGURATION - STOP PROCESSING.
C
        CALL SPGDV$(401, BADUNT, HOLDNAME, STATUS)
        GOTO 1000
C
      ENDIF
C
C   CONFIGURATION IS ACCEPTABLE - KEEP PROCESSING.
C   CALL INPDVR TO INPUT DATA INTO THE HIRS, GAC, AMSU-A,
C   AND AMSU-B BUFFERS PER FRAME.
C
      FRAMNO = FRAMNO + 1
C
C   COPY THE FRAME NUMBER TO THE COMMON ERINFO SO IT IS AVAILABLE
C   FOR THE KIDS MESSAGE LOG FILE.
C
      FRMNBR = FRAMNO
      IF(SPGTEST.EQ.1.AND.FRMNBR.GT.SPGMAXFRM) GO TO 1000
C
      CALL INPDVR(FSTFRM,SYNCTR,SYNTIM,OBSHIR,ERSTAT,N16HIR,
     +            SCNHIR,SCNAMA,SCNAMB,INSTON,FULFRM,METOP)
      IF (ERSTAT.EQ.99) THEN
         GO TO 1030
      END IF
C
C   IF THE INSTRUMENT BUFFERS CONTAIN DATA FROM A PARTIAL DATA FRAME
C   AT THE BEGINNING OR END OF A 1B* FILE, SKIP RETRIEVAL PROCESSING
C   TO AVOID MAKING DUPLICATE RETRIEVALS.
C
      IF (.NOT.(FULFRM)) GO TO 130
C
C     PRINT SUMMARY OF GROSS ERROR LIMITS FAILURES FOR HIRS AND AMSU-A
C     TO KIDS FILE, IF NON-ZERO.
C
      DO I=1,HNCHAN
         IF(NBRHIR(FRAMNO,I).GT.0) THEN
             KMSG='      HIRS CH.   FAILED GROSS LIMITS CHECK - FR.  '
             WRITE(C5TEMP,'(I5)') NBRHIR(FRAMNO,I)
             KMSG(1:5) = C5TEMP
             WRITE(C2TEMP,'(I2)') I
             KMSG(15:16) = C2TEMP
             WRITE(C2TEMP,'(I2)') FRAMNO
             KMSG(49:50) = C2TEMP
             KMSG(52:100) = '  (AFTER INPDVR)'
             PRINT *, KMSG
             CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         ENDIF
      ENDDO
      DO I=1,ANCHAN
         IF(NBRAMA(FRAMNO,I).GT.0) THEN
             KMSG='      AMAX CH.   FAILED GROSS LIMITS CHECK - FR.  '
             WRITE(C5TEMP,'(I5)') NBRAMA(FRAMNO,I)
             KMSG(1:5) = C5TEMP
             WRITE(C2TEMP,'(I2)') I
             KMSG(15:16) = C2TEMP
             WRITE(C2TEMP,'(I2)') FRAMNO
             KMSG(49:50) = C2TEMP
             KMSG(52:100) = '  (AFTER INPDVR)'
             PRINT *, KMSG
             CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         ENDIF
      ENDDO
C
C     IF ERSTAT IS 1 THEN WE DO NOT HAVE A GOOD FRAME CONFIGURATION.
C
      IF (ERSTAT.EQ.1) THEN
          TOTFRR(FRAMNO) = TOTFRR(FRAMNO) + 1
          IF(FRAMNO.GT.30) THEN
             KMSG(1:44)='* FATAL ERROR * FRAME NUMBER    IS TOO HIGH.'
             KMSG(45:100)=' PROBABLE DATA SYNCHRONIZATION PROBLEM.'
             WRITE(C2TEMP,'(I2)') FRAMNO
             KMSG(30:32) = C2TEMP
             CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
             ERSTAT = 99
             GO TO 1000
         ENDIF
      ENDIF
C
C    PER E. BROWN, SINCE THE FLEMING COEFFICIENTS ARE CURRENTLY SET
C    IN BOTH RTOVS AND ATOVS TO RENDER THIS CORRECTION NON-EXISTENT,
C    THIS CALCULATION IS COMMENTED OUT OF ATOVS TO SPEED UP PROCESSING
C    TIME.
C
C     IF(BTEST(FRMCNF,31)) THEN
C
C        CORRECT THE BRIGHTNESS TEMPERATURES OF THE HIRS DATA.
C
C        DO 133 IE = 1, HNSPOT
C           DO 132 JE = 1, HTOTLN
C              DO 131 KE = 1, 19
C                 IF (HIRDAT(IE,JE,KE).NE.R4SPV) THEN
C                    HIRDAT(IE,JE,KE)= (HIRDAT(IE,JE,KE)-CORRB(KE))
C    1                                  / CORRC(KE)
C                 ENDIF
C 131          CONTINUE
C 132       CONTINUE
C 133    CONTINUE
C
C     ENDIF
C
C  CHECK TO SEE IF THIS DATA HAS BEEN PREVIOUSLY PROCESSED IF THE
C  HIRS IS AN AVAILABLE INSTRUMENT.  IF THIS IS AMSU-A ONLY MODE,
C  NO PREVPROC CHECKING IS DONE, SINCE NO RETRIEVALS CAN BE MADE.
C
C  FIRST, GET FIRST ACTIVE HIRS SCAN LINE.
C
      HIRLIN1 = 0
      DO I = 10, HTOTLN-1
         IF (HIRLIN1.EQ.0) THEN
            IF (HGOODL(I)) THEN
               HIRLIN1 = I
            ENDIF
         ENDIF
      END DO
C
C  GET LAST ACTIVE HIRS SCAN LINE IN EACH DATA FRAME
C
      HIRLNLAST = 0
      DO I = HTOTLN-1, 10, -1
         IF (HIRLNLAST.EQ.0) THEN
            IF (HGOODL(I)) THEN
               HIRLNLAST = I
            ENDIF
         ENDIF
      END DO

C
C  FIRST, CONVERT CENTURY DAY OF HIRS TO YEAR AND JULIAN DAY
C  THEN CONVERT MILLISECONDS OF HIRS TO HOUR. IF THE INPUT
C  HIRS TIMES ARE MISSING, WRITE WARNING TO KIDS FILE AND DO
C  NOT PERFORM REDUNDANT DATA CHECK.
C
      IF(HIRLIN1.LE.0) GO TO 126
      IF(HSCTIM(1,HIRLIN1).EQ.R4SPV.OR.HSCTIM(2,HIRLIN1).EQ.R4SPV) THEN
        KMSG(1:50)='* WARNING!* HIRS TIME FOR FRAME      LINE    IS MI'
        KMSG(51:100)='SSING - NO PREV. PROCCESSED DATA CHECKS DONE FOR'
        WRITE(C4TEMP,'(I4)') FRAMNO
        KMSG(35:38) = C4TEMP
        WRITE(C2TEMP,'(I2)') HIRLIN1
        KMSG(43:44) = C2TEMP
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
        KMSG=' THIS FRAME.'
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ELSE
C
C  IF THIS IS THE FIRST FULL FRAME PROCESSED, SAVE THE TIME OF
C  THE FIRST HIRS SCAN LINE FOR THE SPGSSTAT FILE.
C
         IF (ORBSTM(1).EQ.0) THEN
           ORBSTM(1) = HSCTIM(1,HIRLIN1)
           ORBSTM(2) = HSCTIM(2,HIRLIN1)
         ENDIF
C
C  SAVE THE TIME OF THE LAST HIRS SCAN LINE FOR THE SPGSSTAT FILE.
C  THIS VALUE GETS REFRESHED FOR EVERY FULL DATA FRAME.
C
         ORBETM(1) = HSCTIM(1,HIRLNLAST)
         ORBETM(2) = HSCTIM(2,HIRLNLAST)

C
         CALL YRJDAY(HSCTIM(1,HIRLIN1),FRYR,FRDATE)
C
C  SINCE YRJDAY RETURNS A 2-DIGIT YEAR, ADJUST THE FRAME YEAR TO A
C  4-DIGIT YEAR.  THIS ASSUMES THIS PROGRAM WILL NOT BE RUNNING IN
C  THE YEAR 2097!
C
         IF(FRYR.GE.97.AND.FRYR.LE.99) THEN
            FRYR=1900+FRYR
         ELSE
            FRYR=2000+FRYR
         ENDIF
C
C   GET FRAME START TIME HOUR AND MINUTES FROM ODBTIM.
C
         CALL ODBTIM(HSCTIM(1,HIRLIN1),HSCTIM(2,HIRLIN1),DUMTIM)
         IDUMDY = DUMTIM(2) / 100
         IDUMHR = DUMTIM(2) - IDUMDY*100
         IDUMMIN = DUMTIM(3) / 100
         FRHR = (IDUMHR*100) + IDUMMIN
         FRAMETIME = FRYR * 10000000 + FRDATE * 10000 + FRHR
C
C   CHECK FRAME TIME AGAINST PREV.PROCESSED TIMES - IF FRAME TIME
C   FALLS WITHIN PREV.PROC. TIMES, THIS FRAME IS NOT PROCESSED.
C   IF THE PROCESSED FLAG OF A RECORD IS SET TO 0, THAT RECORD IS
C   NOT EVEN CHECKED - THIS ALLOW RE-PROCESSING IF DESIRED.
C
         DO 125 PPR=2,NPPROC
            IF(PPRFLG(PPR).EQ.0) GO TO 125
            IF(FRAMETIME.LT.STPPTIME(PPR).OR.FRAMETIME.GT.ENPPTIME(PPR))
     1         GO TO 125
            IF (METOP.AND.FRAMNO.EQ.1) GO TO 125
C
C   IF THIS POINT IS REACHED, THE DATA HAS BEEN PROCESSED PREVIOSLY AND
C   IT IS NOT SUPPOSED TO BE AGAIN, SO WRITE A MESSAGE TO THE KIDS
C   AND GO TO THE NEXT FRAME. KEEP TRACK OF THE NUMBER OF PREVIOSLY
C   PROCESSED FRAMES.
C
            PPFRAME = PPFRAME + 1
            KMSG(1:39)='FRAME    (JDAY     HOUR      YEAR     )'
            KMSG(40:78)=' HAS BEEN PREVIOUSLY PROCESSED AND WILL'
            KMSG(79:100)=' NOT BE RE-PROCESSED.'
            WRITE(C2TEMP,'(I2)') FRAMNO
            KMSG(7:8) = C2TEMP
            WRITE(C3TEMP,'(I3)') FRDATE
            KMSG(16:18) = C3TEMP
            WRITE(C4TEMP,'(I4)') FRHR
            KMSG(25:28) = C4TEMP
            WRITE(C4TEMP,'(I4)') FRYR
            KMSG(35:38) = C4TEMP
            CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
            GOTO 130
  125    CONTINUE
      ENDIF
C
C   SET START TIME OF ACTUAL DATA FOR PREVIOUSLY PROCESSED FILE IF
C   IT HAS NOT ALREADY BEEN SET.
C
      IF(PPR1ST) THEN
         OUTPPR(1)=FRYR
         OUTPPR(2)=FRDATE
         OUTPPR(3)=FRHR
         PPR1ST = .FALSE.
      ENDIF
C
C  GET MAPPED INFORMATION BY CALLING MAPDVR.
C
  126 CALL MAPDVR(MAPUNT,DELTIME,FCSTYM,MAPST)
      IF(MAPST.NE.0) GO TO 980
C
C  IF THE STATUS CODE FROM MAPDVR IS 77 THEN THE POTENTIAL TEMPERATURE
C  MAP IS OVER 6 HOURS BEFORE OR AFTER THIS FRAME TIME.  IT IS CONSIDERED
C  A FATAL ERROR FOR THE FRAME AND ONCE THE POTENTIAL TEMPERATURE MAP
C  HAS BEEN UPDATED THIS ORBIT SHOULD BE REPROCESSED.
C
C  **NOTE** BY REMOVING THE 'GOTO 130' CLEAR RETRIEVALS CAN BE PROCESSED
C
      IF(MAPST .EQ. 77) THEN
         CALL SPGDV$(202, BADUNT, HOLDNAME, STATUS)
         IF(STATUS .EQ. 99) GOTO 1000
CCLW     GOTO 130
      ELSEIF(MAPST .EQ. 99) THEN
C
C   FATAL ERROR READING MAPPARM CDB WAS ENCOUNTERED.
C
         CALL SPGDV$(203,BADUNT,HOLDNAME,STATUS)
         GO TO 1000
      ENDIF
C
C  CHECK THE EARTH LOCATION TO ENSURE THE FRAME IS GOOD.  AS THIS
C  LOCATION CHECK USES THE AMSU-A, MAKE SURE THE AMSU-A IS AVAILABLE
C  FOR THE FRAME WITH A BTEST ON FRMCNF.  IF THE FRAME IS NOT GOOD,
C  SET RTHLOCERR TO TRUE, SO THAT SPGDVR WILL STOP WITH A CONDITION
C  CODE OF 0 RATHER THAN 99 - THIS WILL ALLOW ANY FRAME DATA UP TO THIS
C  POINT TO BE SAVED AND WRITTEN TO THE ROTATING ODF FILES UPON
C  SPGDVR COMPLETION.
C
      IF (BTEST(FRMCNF,30)) THEN
         CALL RTHLOC(ERSTAT)
         IF(ERSTAT .EQ. 99) THEN
           RTHLOCERR=.TRUE.
           GOTO 1000
         ENDIF
      ENDIF
C
C  INITIALIZE THE RETRIEVAL BUFFERS TO MISSING VALUES EACH FRAME.
C
      CALL CLRBUF(RETDAT,RNGRDP,RTOTLN,RDATEL)
      CALL CLRBUF(RETANC,RNGRDP,RTOTLN,RNANC)
      CALL CLRBUF(TOZDAT,RNGRDP,RTOTLN,2)
C
C  THE FIRST TIME THROUGH WE NEED TO PULL OUT A NUMBER OF COEFFICIENTS
C  FROM THE CDB.
C
      IF (FIRST) THEN
         FIRST = .FALSE.
         INSCFG = CONFIG
C
C  GET THE CDB COEFFICIENTS USED IN CLASSIFICATION.
C
         CALL CLCDBV(CDBST)
         IF (CDBST .NE. 0) GOTO 1000
C
C  GET THE CDB COEFFICIENTS USED IN LIMB ADJUSTMENTS.
C
         CALL LCCDBV(CDBST)
         IF (CDBST .NE. 0) GOTO 1000
C
C  GET OTHER CDB VALUES NEEDED FOR ADJUSTING DATA.
C
         CALL RCDBV(CHNCMB,CLDCHN,RETMET,CNFLIM,CLRHGT,LIMHGT,LIMHG2,
     1              SIMCHN,NRTHLM,STHLIM,CDBST)
         IF(CDBST .NE. 0) GOTO 1000
C
C  WE NEED TO OBTAIN THE COLLOCATED AMSU-A TO HIRS SPOT AND LINE
C  NUMBERS FOR PRECIP EDITING AND COLLOCATION DRIVER.
C
            DO 50 J = 1, HTOTLN
               DO 40 I = 1, HNSPOT
                  CALL COLX2Y(I,J,LSCMTL,HTOTBF,ATOTBF,HIRANG,AMAANG,
     1                   HIRTIM,AMATIM,ANSPOT,ATOTLN,AMASPT(I,J),
     2                    AMALNE(I,J),DELSPT(I,J),DELSCN(I,J))
   40          CONTINUE
   50       CONTINUE
      ENDIF

C
C     SAVE UNLIMB CORRECTED HIRS AND AMSU DATA. ALSO SAVE HIRS CHANNELS
C     8 AND 9 TO THE BUFFER ULCHIR89 FOR FUTURE USE IN THE OZONE CALCULATION
C     TO THE BUFFER ULCHIR FOR FUTURE USE IN THE OZONE CALCULATION
C
      DO I = 1,HNSPOT
         DO J = 1,HTOTLN
            DO K = 1,20
             IF(K .EQ. 20) THEN
               SAVECH20(I,J) = HIRDAT (I,J,K)
             ELSE
               OBSHIR(I,J,K) = HIRDAT(I,J,K)
               IF (K.EQ.8) ULCHIR89(I,J,1) = HIRDAT(I,J,K)
               IF (K.EQ.9) ULCHIR89(I,J,2) = HIRDAT(I,J,K)
             ENDIF
            END DO
         END DO
      END DO
      DO I =1,ANSPOT
         DO J = 1,ATOTLN
            DO K = 1,15
               OBSAMA(I,J,K) = AMADAT(I,J,K)
            END DO
         END DO
      END DO
C
C     SAVE UNCORRECTED AMSU-B DATA
C
      DO I = 1, BNSPOT
         DO J = 1, BTOTLN
            DO K = 1, 5
               OBSAMB(I,J,K) = AMBDAT(I,J,K)
            END DO
         END DO
      END DO
C
C     ***********************************************************
C     THE FOLLOWING CODE APPLIES TSAN MO'S ANTENNA
C     TEMPERATURE ADJUSTMENT  VALUES FROM THE RAW AMSU-A DATA (BEFORE
C     LIMB CORRECTION).  THIS STEP IS DONE FOR PRODUCT PROCESSING
C     AFTER THE LIMB CORR. DATA COLLECTION.
C     ***********************************************************

       IF(TSAN_ON.EQ.1) THEN
          DO I=1,ATOTLN
             DO J=1,ANSPOT
                DO K=1,15
                   IF(OBSAMA(J,I,K).NE.R4SPV .AND.
     1             (K .LE. 3 .OR. K .EQ. 15)) THEN
                      OBSAMA(J,I,K)=(OBSAMA(J,I,K)-
     1                      ETA(K)*TMOSTADJ(J,K,2)*290.0
     2                      - TMOSTADJ(J,K,3)*2.73)/
     3                      TMOSTADJ(J,K,1)
                   ENDIF
                ENDDO
             ENDDO
          ENDDO
       ENDIF
C
C
C  PERFORM GLOBAL LIMB ADJUSTMENT ON AMSU-A AND HIRS DATA.
C  IF THE LAND/SEA FLAG IS SET TO 1, THEN USE LAND/SEA LIMB
C  ADJUSTMENT COEFFICIENTS
C
      IF(LNDSEAFG .EQ. 0) THEN
        CALL GLCDVR
      ELSE
        CALL GLCDVR2
      ENDIF
C
C PLACE HIRS CH. 20 INTO HIRDAT FROM SAVECH20 SINCE IT'S
C ZEROED OUT IN THE LIMB CORRECTION PROCESS
C ALSO IF N16HIR IS SET TO ONE, MAKE SURE THAT THE FIRST
C BEAM POSITION IS SET TO MISSING. ASA (1/31/01)
C
      DO I = 1,HNSPOT
         DO J = 1,HTOTLN
          HIRDAT(I,J,20) = SAVECH20(I,J)
          IF(N16HIR .EQ. 1) THEN
           DO K=1,20
            HIRDAT(1,J,K) = R4SPV
           ENDDO
          ENDIF
         ENDDO
      ENDDO
C
C  CALCULATE THE AMSU-A SURFACE PRODUCTS
C
      CALL AMASFC(LIMBADJ,OBSAMA,MAXLMT,X2COEF,XCOEF,
     >            CH4THRES,CLWADJ,CLWZERO,Q4,Q5,Q4P,CLWADJ4,
     >            CH5THRES,CLWADJ5)
C
C
C  PERFORM CLOUD TESTS, PRECIP EDITING AND INTERPOLATION/
C  EXTRAPOLATION OF AMSU-A TO THE HIRS FOV.
C
CCFT ADDED N16HIR FLAG TO CALL TO CONDVR 9/04/2002.  FHT

      CALL CONDVR(CLOUDY,AMASPT,DELSPT,AMALNE,DELSCN,CNFLIM,CLWTHR,
     1            LPITHR,LATLMT,PRECIPFG,NDIM,CHKPREC,TERRPERC,
     2            CH4LIM,CH5LIM,AVLLIM,ALTERLIM,N16HIR,ERSTAT)
      IF (ERSTAT .EQ. 99) GOTO 1000

C
C  APPLY DATA MISLOCATION PROTECTION, PER M. CHALFANT
C  BASED ON AMSU-A CHANNEL 2.  DATA CHECKED IS ONLY BETWEEN
C  +/- 57 DEGREES LATITUDE, WITH TERRAIN HEIGHT LESS THAN
C  1500 METERS.
C  CHANGED FROM USING AMSU-A DATA AT AMSU-A FOV TO AMSU-A DATA
C  AT HIRS FOV. ASA (2/14/01)
C
      DO I =1,HNSPOT
         DO J = 1,HTOTLN
C
C     IF ANY INFORMATION(LONGITUDE,TERRAIN HT OR AMSU-A TEMPERATURE)
C     IS MISSING, SKIP THIS SPOT.
C
          IF(HIRANC(I,J,2).EQ.R4SPV .OR.
     1       HIRANC(I,J,8).EQ.R4SPV .OR.
     2       HIRDAT(I,J,22).EQ.R4SPV) GO TO 150
C
C     FIRST CHECK LATITUDE
C
          IF(HIRANC(I,J,1).LT.-57.0.OR.HIRANC(I,J,1).GT.57.0)
     1         GO TO 150
C
C     THEN CHECK TERRAIN HEIGHT
C
          IF(HIRANC(I,J,8).GE.1500.0) GO TO 150
C
C     THEN SET EXPECTED TERRAIN FLAG BASED ON MISLAND & MISSEA
C
          IF(HIRDAT(I,J,22).GT.MISLAND) THEN
              EXPTERRAIN = 1
          ELSEIF(HIRDAT(I,J,22).LT.MISSEA) THEN
               EXPTERRAIN = 0
          ELSE
               GO TO 150
          ENDIF

C     THEN COMPARE EXPECTED TERRAIN FLAG TO MAPPED TERRAIN FLAG.  IF THESE
C     DO NOT AGREE, SET A FLAG.  ALSO KEEP TRACK OF NUMBER OF FOVS CHECKED.
C
          ITERRAIN = NINT(HIRANC(I,J,9))
          MISLCHECK = MISLCHECK + 1
          IF(EXPTERRAIN.NE.ITERRAIN .AND. (ITERRAIN .EQ. 0 .OR.
     >       ITERRAIN .EQ.1)) MISLOC = MISLOC + 1
 150      CONTINUE
         END DO
      END DO




C
C  SAVE NON-LIMB CORRECTED, BUT COLLOCATED, HIRS, AMSU-A, AND AMSU-B DATA
C  TO BUFFERS FOR LATER OUTPUT TO RODF FILE.
C
      DO IHS=1,HNSPOT
        DO IHL=1,HTOTLN
           DO IHCH=1,20
              NLCHDAT(IHS,IHL,IHCH) = OBSHIR(IHS,IHL,IHCH)
           END DO
        END DO
      END DO
      DO IAS=1,ANSPOT
        DO IAL=1,ATOTLN
           DO IACH=1,15
              NLCADAT(IAS,IAL,IACH) = OBSAMA(IAS,IAL,IACH)
           END DO
        END DO
      END DO
ccft  DO IAS=1,BNSPOT
ccft     DO IAL=1,BTOTLN
ccft        DO IACH=1,5
ccft          NLCBDAT(IAS,IAL,IACH) = OBSAMB(IAS,IAL,IACH)
ccft        END DO
ccft     END DO
ccft  END DO

C
C  WRITE HIRS, AMSU-A, FRAMES TO ODF FILES.
C
      IF (BTEST(FRMCNF,31).AND.(.NOT.(NOH1BS))) THEN
         INSTR=1
C
C  SAVE LOCATION OF FRAME HEADER, AND INCREMENT HODREC TO SKIP WHERE
C  THE HEADER WILL GO.
C
         HIRFRMREC = HODREC
         CALL HIRODB(HODREC,OBSHIR,HIRCNT,HODF1ST,HLAST,LIMHREC,
     1              CLWTHR,LPITHR,GETLIMOBH,POLARTHR,MIDLATTR,TROPTHR,
     >              CHN4SEL,CHN5SEL,A5THRES,CH4THRES,PRECIPFG,CHN5LIM,
     >              C5THRES,ERSTAT)
         IF(ERSTAT .EQ. 99) GOTO 1000
         IF(DBUGPRT.EQ.1) THEN
            KMSG='HODF:        RECORDS WRITTEN BY HIRODB FOR FRAME   '
            WRITE(C6TEMP,'(I6)') HIRCNT
            KMSG(7:12) = C6TEMP
            WRITE(C2TEMP,'(I2)') FRAMNO
            KMSG(50:51) = C2TEMP
            CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
         ENDIF
C
C  NOW GO BACK AND WRITE FRAME HEADER BASED ON ACTUAL NUMBER OF
C  DATA RECORDS WRITTEN TO HODF.  IF NO RECORDS WERE WRITTEN,
C  DON'T WRITE A FRAME HEADER FOR THIS FRAME, AND DON'T INCREMENT
C  THE AODF RECORD NUMBER.
C
         IF(HIRCNT.NE.0) THEN
C
            CALL FRMHIR(HIRDAT,HIRANC,HIRFRMREC,HIRCNT,HODF1ST,
     1                  HLAST,ERSTAT)
            IF(ERSTAT .EQ. 99) GOTO 1000
C
C  AND INCREMENT HODF RECORD NUMBER IN READINESS FOR NEXT FRAME
C
            HODREC = HODREC + 1
         ENDIF
      ENDIF
C
      IF (BTEST(FRMCNF,30)) THEN
         INSTR=2
C
C  SAVE LOCATION OF FRAME HEADER, AND INCREMENT HODREC TO SKIP WHERE
C  THE HEADER WILL GO.
C
         AMAFRMREC = AODREC
         CALL AMAODB(AMADAT,AMAANC,AODREC,AMACNT,AODF1ST,ALAST,LIMAREC,
     1              GETLIMOBA,OBSAMA,MAXLMT,ERSTAT)
         IF(DBUGPRT.EQ.1) THEN
            KMSG='AODF:        RECORDS WRITTEN BY AMAODB FOR FRAME   '
            WRITE(C6TEMP,'(I6)') AMACNT
            KMSG(7:12) = C6TEMP
            WRITE(C2TEMP,'(I2)') FRAMNO
            KMSG(50:51) = C2TEMP
            CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
         ENDIF
         IF(ERSTAT .EQ. 99) GOTO 1000
C
C  NOW GO BACK AND WRITE FRAME HEADER BASED ON ACTUAL NUMBER OF
C  DATA RECORDS WRITTEN TO AODF.  IF NO RECORDS WERE WRITTEN,
C  DON'T WRITE A FRAME HEADER FOR THIS FRAME, AND DON'T INCREMENT
C  THE AODF RECORD NUMBER.
C
         IF(AMACNT.NE.0) THEN
            CALL FRMAMA(AMADAT,AMAANC,AMAFRMREC,AMACNT,AODF1ST,
     1                  ALAST,ERSTAT)
            IF(ERSTAT .EQ. 99) GOTO 1000
C
C  AND INCREMENT AODF RECORD NUMBER IN READINESS FOR NEXT FRAME
C
            AODREC = AODREC + 1
         ENDIF
      ENDIF
C
C AMSU-B ODF
C
      IF (BTEST(FRMCNF,29)) THEN
         INSTR=3
C
C  SAVE LOCATION OF FRAME HEADER, AND INCREMENT HODREC TO SKIP WHERE
C  THE HEADER WILL GO.
C
         AMBFRMREC = BODREC
         CALL AMBODB(AMBDAT,AMBANC,BODREC,AMBCNT,BODF1ST,BLAST,LIMBREC,
     1              GETLIMOBB,OBSAMB,ERSTAT)
         IF(DBUGPRT.EQ.1) THEN
            KMSG='BODF:        RECORDS WRITTEN BY AMBODB FOR FRAME   '
            WRITE(C6TEMP,'(I6)') AMBCNT
            KMSG(7:12) = C6TEMP
            WRITE(C2TEMP,'(I2)') FRAMNO
            KMSG(50:51) = C2TEMP
            CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
         ENDIF
         IF(ERSTAT .EQ. 99) GOTO 1000
C
C  NOW GO BACK AND WRITE FRAME HEADER BASED ON ACTUAL NUMBER OF
C  DATA RECORDS WRITTEN TO BODF.  IF NO RECORDS WERE WRITTEN,
C  DON'T WRITE A FRAME HEADER FOR THIS FRAME, AND DON'T INCREMENT
C  THE BODF RECORD NUMBER.
C
         IF(AMBCNT.NE.0) THEN
            CALL FRMAMB(AMBDAT,AMBANC,AMBFRMREC,AMBCNT,BODF1ST,
     1                  BLAST,ERSTAT)
            IF(ERSTAT .EQ. 99) GOTO 1000
C
C  AND INCREMENT AODF RECORD NUMBER IN READINESS FOR NEXT FRAME
C
            BODREC = BODREC + 1
         ENDIF
      ENDIF
C
C
C  ATTEMPT TO CLEAR CLOUD-CONTAMINATED FIELDS OF VIEW.
C
      CALL NSDVR(CLOUDY,CNFLIM,ERSTAT)
      IF (ERSTAT .EQ. 99) GOTO 1000
C
C  COLLOCATED AMSU-A CHANNEL VALUES WERE PLACED IN THE HIRS DATA BUFFER
C  FOR CLOUD TESTS AND CLOUD CLEARING IN CONDVR.  THESE SHOULD BE
C  RESET TO MISSING VALUES SINCE THEY MAY NOT BE REPLACED BY VALUES
C  IN COLDVR.
C
      DO 180 K = HNCHAN+1, HNCHAN+ANCHAN
         DO 170 J = 1, HTOTLN
            DO 160 I = 1, HNSPOT
               OBSHIR(I,J,K) = R4SPV
  160       CONTINUE
  170    CONTINUE
  180 CONTINUE
C
C  UPDATE CLOUD TEST AND CLOUD-CLEARING STATISTICS.
C
      TOTCLD = TOTCLD + NMCLDY(FRAMNO)
      TOTCLR = TOTCLR + NUMCLR(FRAMNO)
      TOTNS = TOTNS + NUMNS(FRAMNO)
      TOTNOT = TOTNOT + NOTEST(FRAMNO)

C
C  PERFORM COLLOCATION OF THE AMSU-A TO THE HIRS, THEN
C  COLLOCATE THE HIRS TO THE RETRIEVAL GRID.
C
      CALL COLDVR(RETMET,CHNCMB,CNFLIM,AMASPT,AMALNE,DELSPT,
     1            DELSCN,RUNFILTR,LATLMT,NOH1BS,ERSTAT)
ccft 1            DELSCN,RUNFILTR,LATLMT,ERSTAT)
      IF(ERSTAT .EQ. 99) GOTO 1000

C
C  ACCUMULATE RANGES OF STATS FOR THE ON-LINE STATS FILE.
C
      CALL CNFRNG(HIRANC,HNSPOT,HTOTLN,HNANC,HTOTBF,CNFLIM,1,HCNFRG)
      CALL CNFRNG(AMAANC,ANSPOT,ATOTLN,ANANC,ATOTBF,CNFLIM,2,ACNFRG)
C
C  DETERMINE THE OPERATOR COMPONENT BIN NUMBER FOR THE DATA FRAME.
C  THE BIN NUMBER IS PUT IN PLACE OF THE CLASS NUMBER IN THE
C  RETRIEVAL ANCILLARY BUFFER.
C
      CALL BINDVR
C
C
C  CHECK ALL DATA BUFFERS TO ENSURE ALL BRIGHTNESS TEMPERATURES ARE
C  STILL WITHIN LIMITS.
C
      IF (BTEST(FRMCNF,31).AND.(.NOT.(NOH1BS))) THEN
         CALL BTCHK1(HIRDAT,HNSPOT,HTOTLN,HDATEL,HMINBT,HMAXBT,
     1       100.0,HNANC,HIRANC,HNCHAN,ERSTAT)
         IF (ERSTAT .EQ. 99) GOTO 1000
      ENDIF
      IF (BTEST(FRMCNF,30)) THEN
         CALL BTCHK1(AMADAT,ANSPOT,ATOTLN,ADATEL,AMINBT,AMAXBT,
     1       100.0,ANANC,AMAANC,ANCHAN,ERSTAT)
         IF (ERSTAT .EQ. 99) GOTO 1000
      ENDIF
C
C     PRINT SUMMARY OF GROSS ERROR LIMITS FAILURES FOR HIRS AND AMSU-A
C     TO KIDS FILE, IF NON-ZERO.
C
      IF (.NOT.(NOH1BS)) THEN
         DO I=1,HNCHAN
            IF(NBBHIR(FRAMNO,I).GT.0) THEN
               KMSG='      HIRS CH.   FAILED GROSS LIMITS CHECK - FR.  '
               WRITE(C5TEMP,'(I5)') NBBHIR(FRAMNO,I)
               KMSG(1:5) = C5TEMP
               WRITE(C2TEMP,'(I2)') I
               KMSG(15:16) = C2TEMP
               WRITE(C2TEMP,'(I2)') FRAMNO
               KMSG(49:50) = C2TEMP
               KMSG(52:100) = '  (AFTER GLCDVR AND COLDVR)'
               PRINT *, KMSG
               CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
            ENDIF
         ENDDO
      END IF
      DO I=1,ANCHAN
         IF(NBBAMA(FRAMNO,I).GT.0) THEN
             KMSG='      AMAX CH.   FAILED GROSS LIMITS CHECK - FR.  '
             WRITE(C5TEMP,'(I5)') NBBAMA(FRAMNO,I)
             KMSG(1:5) = C5TEMP
             WRITE(C2TEMP,'(I2)') I
             KMSG(15:16) = C2TEMP
             WRITE(C2TEMP,'(I2)') FRAMNO
             KMSG(49:50) = C2TEMP
             KMSG(52:100) = '  (AFTER GLCDVR AND COLDVR)'
             PRINT *, KMSG
             CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         ENDIF
      ENDDO
C
      CALL RETDVR(CHNCMB,CLDCHN,ULCHIR89,CLRHGT,LIMHGT,LIMHG2,SIMCHN,
     1   NRTHLM,STHLIM,ERSTAT)
      IF (ERSTAT .EQ. 99) GOTO 1000

C
C  CLASSIFY ANY RETRIEVALS AS POLAR REDUNDANT.
C
      CALL POLRED(HIRANC,RETANC,HSCTIM,RNGRDP,RTOTLN,RNANC,
     1            HNSPOT,HTOTLN,HNANC,PRFLAG,ERSTAT)
      IF (ERSTAT .EQ. 99) GOTO 1000
C
C  SET THE WATER VAPOR CORRECTED HIRS-8 ARRAY, AND
C  COMPUTE THE CLOUD AMOUNT, CLOUD TOP PRESSURE AND CLOUD TOP
C  TEMPERATURE WITH A CALL TO CLDTOP.
C
      IF(AT8ONOFF.EQ.1) THEN
       DO H2=1,HTOTLN
         DO H1=1,HNSPOT
          IF(HIRDAT(H1,H2,8).NE.R4SPV .AND.
     +       HIRDAT(H1,H2,10).NE.R4SPV .AND.
     +       HIRDAT(H1,H2,11).NE.R4SPV .AND.
     +       HIRDAT(H1,H2,22).NE.R4SPV) THEN
               HIRANC(H1,H2,23)=H8WVC(1) +
     1                             HIRDAT(H1,H2,8)  * H8WVC(2) +
     2                             HIRDAT(H1,H2,10) * H8WVC(3) +
     3                             HIRDAT(H1,H2,11) * H8WVC(4) +
     4                             HIRDAT(H1,H2,22) * H8WVC(5) +
     5                             HIRDAT(H1,H2,8)
             ENDIF
          END DO
       END DO

      ELSE
          DO H2=1,HTOTLN
           DO H1=1,HNSPOT
            IF(HIRDAT(H1,H2,8).NE.R4SPV)
     1       HIRANC(H1,H2,23) = HIRDAT(H1,H2,8)
           END DO
          END DO
      ENDIF

      IF(CLDONOFF.EQ.1) THEN
C
         CALL CLDTOP(OBSHIR,SATID,OLR,LC1,LC2,LC3,
     2               LC4,R1T1ST,CLD1ST,PRFLAG,ERSTAT,
     +               aerocnt,extcnt,METOP,CTPEXIT,cpelt5pct,
     +               aeroqc2,clearcount,clearavg,failcount,countqc3)

C
C  A FAILURE IN CLDTOP IS NOT FATAL TO SPGDVR, SO JUST PRINT
C  A WARNING AND CONTINUE PROCESSING.
C
         IF(ERSTAT.NE.0) THEN
           KMSG(1:50)='* WARNING * CLDTOP SUBROUTINE RETURNED ERSTAT =   '
           KMSG(51:100)=' FOR FRAME   . CLOUD PARAMETERS MAY BE MISSING. '
           WRITE(C2TEMP,'(I2)') ERSTAT
           KMSG(49:50) = C2TEMP
           WRITE(C2TEMP,'(I2)') FRAMNO
           KMSG(62:63) = C2TEMP
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
         ENDIF
      ENDIF
C
C  WRITE TO THE U.K. FILE
C
      CALL WRTCOL(COLUNT,CRECL)
C
      CALL FRMRET(RETANC,HIRANC,FRMTIM,ODFREC,FRHREC,N16HIR,ERSTAT)
      if (FRMTIM(1).lt.0) then
         print *,'SPGDVR: invalid frame time from FRMRET ',
     +            FRMTIM(1),FRMTIM(2),FRMTIM(3)
         go to 200
      end if
      HIRTIP(1) = FRMTIM(1)
      HIRTIP(2) = FRMTIM(2)
      HIRTIP(3) = FRMTIM(3)
C
C  IF NO RETRIEVALS WERE MADE AN ERSTAT OF -2 IS RETURNED.
C
      IF(ERSTAT .EQ. -2) GOTO 200
      IF (ERSTAT .EQ. -3) GO TO 1000
      IF(ERSTAT .EQ. 99) GOTO 1000
C
C  IF THERE ARE RETRIEVALS, WRITE THE FRAME HEADER AND
C  OUTPUT THE RETRIEVAL GRID DATA TO THE ODF.
C

      CALL 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)

      IF(ERSTAT .EQ. 99) GOTO 1000

C
C  USE THE SCAN LINE START TIME OF THE FIRST HIRS SCAN PROCESSED 
C  IN THIS DATA FRAME TO UPDATE THE PREVIOUSLY PROCESSED FILE.
C
      OUTPPR(4)=FRYR
      OUTPPR(5)=FRDATE
      OUTPPR(6)=FRHR
      OUTPPR(7)=1
      FPROC(FRAMNO) = .TRUE.
C
C** END OF THIS FRAME, GO TO NEXT FRAME
C
  200 ERSTAT = 0
      SUBNAM='SPGDVR'
      CUMRET = CUMRET + NFRCYC(FRAMNO)
      ACTUALRET = ACTUALRET + NFRRET(FRAMNO)
C
C  WRITE SUMMARY OF ANY RODF RECORDS THAT DO NOT HAVE A RETRIEVAL FOR
C  THIS FRAME.
C
      IF(BINRET00(FRAMNO).NE.0) THEN
      KMSG = '# OF RETRIEVALS FLAGGED AS NO-HIRS BY BINDVR = '
      WRITE(C5TEMP,'(I5)') BINRET00(FRAMNO)
      KMSG(48:52) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(RETRETSP00(FRAMNO).NE.0) THEN
      KMSG(1:50)='# OF CLOUDY RETRIEVALS WITH NO FORECAST SURFACE PO'
      KMSG(51:100)='TENTIAL TEMP (FLAGGED BY RETDVR) =       '
      WRITE(C5TEMP,'(I5)') RETRETSP00(FRAMNO)
      KMSG(88:92) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(LBRETSPT0(FRAMNO).NE.0) THEN
      KMSG(1:50)='# OF CLOUDY RETRIEVALS WITH NO FORECAST SURFACE PO'
      KMSG(51:100)='TENTIAL TEMP (FLAGGED BY LBSRCH) =       '
      WRITE(C5TEMP,'(I5)') LBRETSPT0(FRAMNO)
      KMSG(88:92) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(LBRETPTH0(FRAMNO).NE.0) THEN
      KMSG = '# OF RETRIEVALS W/ NO-HIRS PATHID IN LBSRCH  = '
      WRITE(C5TEMP,'(I5)') LBRETPTH0(FRAMNO)
      KMSG(48:52) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(LBRETNO0(FRAMNO).NE.0) THEN
      KMSG = '# OF RETRIEVALS W/ NO FIRST GUESS IN LBSRCH  = '
      WRITE(C5TEMP,'(I5)') LBRETNO0(FRAMNO)
      KMSG(48:52) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(RETRETNG0(FRAMNO).NE.0) THEN
      KMSG = '# OF RETRIEVALS W/ NEGATIVE TEMPS IN LBSRCH  = '
      WRITE(C5TEMP,'(I5)') RETRETNG0(FRAMNO)
      KMSG(48:52) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
      KMSG(1:50)='----------FRAME    PROCESSED      RETRIEVALS (CUMU'
      KMSG(51:100)='LATIVE =       )'
      WRITE(C2TEMP,'(I2)') FRAMNO
      KMSG(17:18) = C2TEMP
      WRITE(C4TEMP,'(I4)') NFRRET(FRAMNO)
      KMSG(30:33) = C4TEMP
      WRITE(C6TEMP,'(I6)') ACTUALRET
      KMSG(60:65) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      KMSG(1:50)='----------FRAME    PROCESSED      RODF RECS  (CUMU'
      KMSG(51:100)='LATIVE =       )'
      WRITE(C2TEMP,'(I2)') FRAMNO
      KMSG(17:18) = C2TEMP
      WRITE(C4TEMP,'(I4)') NFRCYC(FRAMNO)
      KMSG(30:33) = C4TEMP
      WRITE(C6TEMP,'(I6)') CUMRET
      KMSG(60:65) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      PRINT *,'Frame ',FRAMNO,' processed ',NFRRET(FRAMNO),
     1        ' retrievals & ',NFRCYC(FRAMNO),' RODF recs'
      PRINT *,'------------------------------------------------------'
C
      GOTO 130
C
  800 KMSG = ' ERROR DURING OPEN/READ OF THE STAB INPUT FILE'
      SUBNAM='SPGDVR'
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      GO TO 1000
C
  810 KMSG = 'LEVEL     (VALUE:      ) IS NOT AT A RETRIEVAL LEVEL'
      SUBNAM='SPGDVR'
      WRITE(C3TEMP,'(I3)') I
      KMSG(7:9) = C3TEMP
      WRITE(C4TEMP,'(I4)') IPRESS(I)
      KMSG(19:22) = C4TEMP
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      GO TO 1000
C
  815 KMSG = 'ERROR WHILE READING HEADER OF R1T - UNIT '
      WRITE(C4TEMP,'(I4)') R1TUNT
      KMSG(42:45) = C4TEMP
      SUBNAM='SPGDVR'
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      GO TO 1000
C
  820 KMSG = 'ERROR DURING OPEN/READ OF NMC FILE - UNIT '
      WRITE(C4TEMP,'(I4)') IFIL(INMC)
      KMSG(43:46) = C4TEMP
      SUBNAM='SPGDVR'
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      GO TO 1000
C
  825 KMSG(1:50)='ERROR DURING OPEN/READ/WRITE OF PREVIOUSLY PROCESS'
      KMSG(51:100)='ED FILE - UNIT                                  '
      WRITE(C4TEMP,'(I4)') FPUNIT
      KMSG(66:69) = C4TEMP
      SUBNAM='SPGDVR'
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      GO TO 1030
C
  920 CALL SPGDV$( 102, BADUNT, HOLDNAME, STATUS )
      GOTO 1030
C
  950 CALL SPGDV$( 301, BADUNT ,HOLDNAME, STATUS )
      GOTO 1030
C
  960 CALL SPGDV$( 302, BADUNT, HOLDNAME, STATUS )
      GOTO 1030
C
  980 CALL SPGDV$( 201,BADUNT, HOLDNAME, STATUS )
C
C  SHUTDOWN.
C
 1000 IF(RUNFILTR.NE.0) THEN
         WRITE(6,300) LNGDCNT,OCGDCNT,GOODCNT,LNQUCNT,OCQUCNT,
     1                QUESCNT,LNBDCNT,OCBDCNT,BADCNT,TOTCONT
C
         IF(TOTCONT.NE.0) THEN
            PCLNGD=(LNGDCNT/TOTCONT)*100
            PCLNQU=(LNQUCNT/TOTCONT)*100
            PCLNBD=(LNBDCNT/TOTCONT)*100
            PCOCGD=(OCGDCNT/TOTCONT)*100
            PCOCQU=(OCQUCNT/TOTCONT)*100
            PCOCBD=(OCBDCNT/TOTCONT)*100
            PCGOOD=(GOODCNT/TOTCONT)*100
            PCQUES=(QUESCNT/TOTCONT)*100
            PCBAD=(BADCNT/TOTCONT)*100
         ELSE
            PCLNGD=0.0
            PCLNQU=0.0
            PCLNBD=0.0
            PCOCGD=0.0
            PCOCQU=0.0
            PCOCBD=0.0
            PCGOOD=0.0
            PCQUES=0.0
            PCBAD=0.0
         ENDIF
         WRITE(6,400) PCLNGD,PCOCGD,PCGOOD,PCLNQU,PCOCQU,PCQUES,
     1                PCLNBD,PCOCBD,PCBAD
      ELSE
         PRINT *, 'NO FILTER SUMMARY TABLE - FILTER (D3CON) TURNED OFF'
      ENDIF
C
C  WRITE SUMMARY OF ANY RODF RECORDS THAT DO NOT HAVE A RETRIEVAL.
C
      IF(BINRET0.NE.0) THEN
      KMSG = '# OF RETRIEVALS FLAGGED AS NO-HIRS BY BINDVR = '
      WRITE(C5TEMP,'(I5)') BINRET0
      KMSG(48:52) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(RETRETSPT.NE.0) THEN
      KMSG(1:50)='# OF CLOUDY RETRIEVALS WITH NO FORECAST SURFACE PO'
      KMSG(51:100)='TENTIAL TEMP (FLAGGED BY RETDVR) =       '
      WRITE(C5TEMP,'(I5)') RETRETSPT
      KMSG(88:92) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(LBRETSPT.NE.0) THEN
      KMSG(1:50)='# OF CLOUDY RETRIEVALS WITH NO FORECAST SURFACE PO'
      KMSG(51:100)='TENTIAL TEMP (FLAGGED BY LBSRCH) =       '
      WRITE(C5TEMP,'(I5)') LBRETSPT
      KMSG(88:92) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(LBRETPTH.NE.0) THEN
      KMSG = '# OF RETRIEVALS W/ NO-HIRS PATHID IN LBSRCH  = '
      WRITE(C5TEMP,'(I5)') LBRETPTH
      KMSG(48:52) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(LBRETNO.NE.0) THEN
      KMSG = '# OF RETRIEVALS W/ NO FIRST GUESS IN LBSRCH  = '
      WRITE(C5TEMP,'(I5)') LBRETNO
      KMSG(48:52) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
      IF(RETRETNEG.NE.0) THEN
      KMSG = '# OF RETRIEVALS W/ NEGATIVE TEMPS IN LBSRCH  = '
      WRITE(C5TEMP,'(I5)') RETRETNEG
      KMSG(48:52) = C5TEMP
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
      ENDIF
C
C  GET CURRENT SYSTEM TIME.
C
      CALL DATIM1(IDATIM)
      IDATIM(2) = IDATIM(5)/100
      IDATIM(5) = IDATIM(5)-(IDATIM(2)*100)
      IDATIM(6) = IDATIM(6)/100
      IF(IDATIM(1).EQ.98.OR.IDATIM(1).EQ.99) THEN
         IDATIM(1) = 1900 + IDATIM(1)
      ELSE
         IDATIM(1) = 2000 + IDATIM(1)
      ENDIF
C
C  STORE THE END DATE AND TIME FOR THE MONITORING STATS FILE.
C
      ENDATE = ((IDATIM(1)*10000)+(IDATIM(3)*100)+IDATIM(4))
      LASTIM = ((IDATIM(2)*10000)+(IDATIM(5)*100)+IDATIM(6))
C
C    FIGURE PERCENTAGE OF AMSU-A MISLOCATIONS PER FOVS CHECKED
C
      RDUMMY = 0.0
      IF(MISLCHECK.NE.0)
     1   RDUMMY = (REAL(MISLOC)/REAL(MISLCHECK))*100.0
      WRITE(6,450) MISLCHECK,MISLOC,RDUMMY
  450 FORMAT(2X,'******************************************',
     1      /2X,'AMSU-A DATA MISLOCATION PROTECTION SUMMARY',
     2      /2X,'OUT OF ',I6,' ELIGIBLE FOVS, ',I6,
     3         ' MAY BE MISLOCATED (',F6.2,'%).',
     4      /2X,'******************************************')
C
C   CHECK TO SEE IF THE PERCENTAGE OF MISLOCATIONS ARE GREATER THAN
C   THE LIMIT. NO MATTER WHAT THE OUTCOME, PLACE THE FLAG VALUE IN ALL
C   OF THE FRAME HEADERS TO INDICATE THAT THE ORBIT IS MISLOCATED AND
C   SHOULD NOT BE USED.
C
      IF(RDUMMY .GT. PRCNTML) THEN
         MISSTAT = 88
         MISLOCFG =1
      ENDIF
      FRAMNUM = FRAMNO -1
      DO J=1, FRAMNUM
         IF(NFRRET(J) .GT. 0 .AND. FRHREC(J).GT.0) THEN
            READ(UNIT=ODBUNT,REC=FRHREC(J),IOSTAT=STATUS,ERR=9001) RODFBUF
            RODFBUF(23) = MISLOCFG
            WRITE(UNIT=ODBUNT,REC=FRHREC(J),IOSTAT=STATUS,ERR=9002) RODFBUF
         ENDIF
      ENDDO
C
C CALCULATE THE PERCENTAGE OF RETRIEVALS WITH BAD OBS QUALITY FLAGS
C
      IF(OQCHEKD.NE.0)
     1   OQPCNT = (REAL(OQBAD)/REAL(OQCHEKD))*100.0
      WRITE(6,460) OQCHEKD,OQBAD,OQPCNT,OQLIMT
  460 FORMAT(2X,'******************************************',
     1      /2X,'RETRIEVAL OBSERVATION QUALITY SUMMARY',
     2      /2X,'OUT OF ',I6,' RETRIEVAL FOVS, ',I6,
     3         ' HAD BAD OBS QUALITY (',F6.2,'%).',
     4      /2X,'THE OBS QUALITY THRESHOLD IS ',F6.2,'%.'
     5      /2X,'******************************************')
C
C IF THE PERCENTAGE OF RETRIEVALS WITH BAD OBS QUALITY FLAGS
C EXCEEDS THE THRESHOLD, SET THE 'MISLOCATION' FLAG IN THE
C DATA FRAME HEADERS IN THE RODF TO PREVENT MATCHUPS FROM
C BEING MADE FOR THIS ORBIT.
C
      OQSTAT = 0
      IF (OQPCNT.GT.OQLIMT) THEN
         OQSTAT = 89
         DO J=1, FRAMNUM
            IF (FRHREC(J).GT.0) THEN
               READ(UNIT=ODBUNT,REC=FRHREC(J),IOSTAT=STATUS,ERR=9001)
     +            RODFBUF
               RODFBUF(23) = 1
               WRITE(UNIT=ODBUNT,REC=FRHREC(J),IOSTAT=STATUS,ERR=9002)
     +            RODFBUF
            END IF
         END DO
      END IF
C
C  UPDATE THE MONITORING STATS FILE.
C
      PRINT *,'TIME OF FIRST HIRS SCAN LINE PROCESSED ',ORBSTM(1),ORBSTM(2)
      PRINT *,'TIME OF LAST HIRS SCAN LINE PROCESSED  ',ORBETM(1),ORBETM(2)
      CALL STATS(SMSSTAT)
      IF(SMSSTAT.NE.0) THEN
        KMSG(1:50)='* WARNING! * ERROR WRITING TO MONITORING STATS FIL'
        KMSG(51:100)='E @ UNIT   .  STATS RETURNED STATUS OF       .  '
        WRITE(C2TEMP,'(I2)') SMSUNT
        KMSG(60:61) = C2TEMP
        WRITE(C6TEMP,'(I6)') SMSSTAT
        KMSG(90:95) = C6TEMP
      ENDIF

C  WRITE TO HEADER RECORD OF EACH ODF THE NUMBER OF
C  RECORDS IN THE FILE (INCLUDING BOTH HEADERS)
C  ALSO WRITE THE START AND END RETRIEVAL/RECORD
C  YEAR AS A 4-DIGIT CHARACTER TO THE HEADERS.
C
      SUBNAM='SPGDVR'
      HODFHD(1) = HODREC
      KMSG='HODF HAS        RECORDS.'
      WRITE(C6TEMP,'(I6)') HODREC
      KMSG(10:15) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
      WRITE(UNIT=HIDFU,REC=1,ERR=901) HODFHD
      GO TO 902
  901 KMSG='ERROR WRITING HEADER OF HODF - #RECS=      '
      KMSG(38:43) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
  902 AODFHD(1) = AODREC
      KMSG='AODF HAS        RECORDS.'
      WRITE(C6TEMP,'(I6)') AODREC
      KMSG(10:15) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
      WRITE(UNIT=AIDFU,REC=1,ERR=903) AODFHD
      AOSTAT = 0
      GO TO 904
  903 KMSG='ERROR WRITING HEADER OF AODF - #RECS='
      KMSG(38:43) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
  904 CONTINUE
      BODFHD(1) = BODREC 
      KMSG='BODF HAS        RECORDS.'
      WRITE(C6TEMP,'(I6)') BODREC
      KMSG(10:15) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
      WRITE(UNIT=BIDFU,REC=1,ERR=905) BODFHD
      GO TO 906
  905 KMSG='ERROR WRITING HEADER OF BODF = #RECS='
      KMSG(38:43) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
 
  906 RODFHD(1) = ODFREC
      KMSG='RODF HAS        RECORDS.'
      WRITE(C6TEMP,'(I6)') ODFREC
      KMSG(10:15) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
C
C  NOTE THAT THE SPGDVR NEVER WRITES TO RECORD 2 OF THE RODF.
C  THIS IS SCALING FACTORS FOR THE DATA RECORD THAT ARE SET
C  WHEN THE FILE IS INITIALIZED.
C
      FRAMNO = FRAMNO - 1
      WRITE(UNIT=ODBUNT,REC=1,ERR=1005) RODFHD
      PRINT *, 'Total retrievals = ',ACTUALRET,' over ',FRAMNO,
     1         ' frames (',CUMRET,' RODF recs)'
      IF (FRAMNO.LE.1) THEN
         KMSG='An unusually short orbit was processed.'
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         KMSG='There may be a Front End Processor problem.'
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         ERSTAT = 60
         GO TO 1006
      END IF

      IF(ACTUALRET.EQ.0) THEN
        KMSG='No retrievals were made for this orbit.'
        CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
        print *,'Of ',FRAMNO,' frames, ',PPFRAME,' were previously processed.'
        IF ((FRAMNO - PPFRAME).LE.3) THEN
           ERSTAT = 67
           KMSG='All frames were processed previously.'
        CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
        ELSE
           ERSTAT=55
        END IF
      END IF
      GO TO 1006
C
 1005 KMSG='ERROR WRITING HEADER OF RODF - #RECS='
      KMSG(38:43) = C6TEMP
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      GO TO 1006
9001  PRINT *, 'ERROR READING RODF FRAME HEADER RECORD'
      GO TO 1006
9002  PRINT *, 'ERROR WRITING OUT RODF FRAME HEADER RECORD'
C
 1006 CLOSE(ODBUNT)
      CLOSE(AIDFU)
      CLOSE(HIDFU)
 1010 CLOSE(CDBUNT)
      CLOSE(UNTOPC)
      CLOSE(MAPUNT)
 1020 CLOSE(HUNIT)
      CLOSE(AUNIT)
      CLOSE(BUNIT)
      CLOSE(GUNIT)
      IF(GETLIMOBH.EQ.1) CLOSE(HLMUNT)
      IF(GETLIMOBA.EQ.1) CLOSE(ALMUNT)
      IF(GETLIMOBB.EQ.1) CLOSE(BLMUNT)
C
C   UPDATE PREV. PROC. FILE AND CLOSE IT.
C
      IF(.NOT.PPR1ST.AND.ODFREC.GT.2) THEN
         IDUM=NXTPPROC+1
         IF(IDUM.GT.NPPROC) IDUM=2
         OPEN(UNIT=FPUNIT,ERR=825,ACCESS='SEQUENTIAL',FORM='FORMATTED')
         WRITE(FPUNIT,108,ERR=825) NPPROC,IDUM
         DO PPR=2,NPPROC
            IF(PPR.EQ.NXTPPROC) THEN
               WRITE(FPUNIT,111,ERR=825) OUTPPR
            ELSE
              WRITE(FPUNIT,111,ERR=825)STPPY(PPR),STPPJ(PPR),STPPT(PPR),
     1                                 ENPPY(PPR),ENPPJ(PPR),ENPPT(PPR),
     2                                 PPRFLG(PPR)
            ENDIF
         ENDDO
         CLOSE(FPUNIT)
      ENDIF
C
      CLOSE(COLUNT)
      CLOSE(IFIL(1))
      CLOSE(IFIL(2))
      CLOSE(IFIL(3))
      CLOSE(IFIL(4))
      CLOSE(IFIL(5))


C
C     UPDATE HOUSEKEEPING RECORD FOR THE DSDR1T FILE.
C     IF NO RECORDS WERE WRITTEN TO THE FILE, UPDATE
C     THE ORBITAL RECORD COUNT IN THE HEADER TO ZERO.
C
      IF (R1TORB.EQ.0) R1THKP(5) = R1TORB
C
      KMSG='       RECORDS WERE WRITTEN TO THE ROTATING 1T'
      WRITE(C6TEMP,'(I6)') R1TORB
      KMSG(1:6) = C6TEMP
      PRINT *, KMSG
      CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
      print *,R1TCLEAR,' of ',R1TSPOTS,' R1T spots were CLEAR'
      if (R1TSPOTS.gt.0) then
         pctclear = FLOAT(R1TCLEAR) / FLOAT(R1TSPOTS)
         pctclear = pctclear * 100.0
      else
         pctclear = 0.0
      end if
      print 1028,pctclear,'% of R1T spots were CLEAR'
      print *,QCFLAG7,' spots had QCFLAG set to 7'
      print *,aeroqc2,' aerosol spots had a QCFLAG of 2'
      print *,countqc3,' spots had a QCFLAG of 3'
      print *,' Of ',extcnt,' QCFLAG 6 spots ',aerocnt,' were ',
     +        'aerosols'
 1028 FORMAT(F6.2,A)
      print *,' '
      print 1029,'CTP EXIT  1 = ',CTPEXIT(1),' Original CPE from GETERR = 0.0'
      print 1029,'CTP EXIT  4 = ',CTPEXIT(4),' SAMOUNT > 3.0'
      print 1029,'CTP EXIT  5 = ',CTPEXIT(5),' ABS(DELPR) < 1 mb'
      print 1029,'CTP EXIT  6 = ',CTPEXIT(6),' QCFLAG Set to 7 in CTPADJUST'
      print 1029,'CTP EXIT  7 = ',CTPEXIT(7),' CPE increased on ITER 1'
      print 1029,'CTP EXIT  8 = ',CTPEXIT(8),' CTP < MINPRESS'
      print 1029,'CTP EXIT  9 = ',CTPEXIT(9),' CTP < TROPRS for aerosols'
      print 1029,'CTP EXIT 10 = ',CTPEXIT(10),' CPE increased after ITER 1'
      print 1029,'CTP EXIT 11 = ',CTPEXIT(11),' Interpolation performed'
      print 1029,'CTP EXIT 12 = ',CTPEXIT(12),' Max iterations reached'
      print 1029,'CTP EXIT 13 = ',CTPEXIT(13),' Total calls to CTPADJUST'
 1029 FORMAT(A,I5,A)
c
c     print 1027, 'Number of FLIPPED QCFLAG 0 Spots   = ',CTPEXIT(14)
c     print 1027, 'Number of unFLIPPED QCFLAG 0 Spots = ',CTPEXIT(15)
      print 1027, 'Number of Failed spots from CLDTOP = ',failcount

 1027 FORMAT(A,I5)
      CLDCOUNT = R1TSPOTS - R1TCLEAR
      if (CLDCOUNT.GT.0) then
         LT5PCT = FLOAT(cpelt5pct) / FLOAT(CLDCOUNT)
         LT5PCT = LT5PCT * 100.0
      else
         LT5PCT = 0.0
      end if
      print *,' '
      print 1031,' CPE <= 5 Percent = ',cpelt5pct,' of ',CLDCOUNT,' spots: ',
     +        LT5PCT,'%'
 1031 FORMAT(A,I5,A,I5,A,F6.2,A)


      if (clearcount(1) .gt. 0) then
         clearavg(1,1) = clearavg(1,1) / FLOAT(clearcount(1))
         clearavg(1,2) = clearavg(1,2) / FLOAT(clearcount(1))
         clearavg(1,3) = clearavg(1,3) / FLOAT(clearcount(1))
         clearavg(1,4) = clearavg(1,4) / FLOAT(clearcount(1))
      end if

      if (clearcount(2) .gt. 0) then
         clearavg(2,1) = clearavg(2,1) / FLOAT(clearcount(2))
         clearavg(2,2) = clearavg(2,2) / FLOAT(clearcount(2))
         clearavg(2,3) = clearavg(2,3) / FLOAT(clearcount(2))
         clearavg(2,4) = clearavg(2,4) / FLOAT(clearcount(2))
      end if

      if (clearcount(3) .gt. 0) then
         clearavg(3,1) = clearavg(3,1) / FLOAT(clearcount(3))
         clearavg(3,2) = clearavg(3,2) / FLOAT(clearcount(3))
         clearavg(3,3) = clearavg(3,3) / FLOAT(clearcount(3))
         clearavg(3,4) = clearavg(3,4) / FLOAT(clearcount(3))
      end if

      if (clearcount(4) .gt. 0) then
         clearavg(4,1) = clearavg(4,1) / FLOAT(clearcount(4))
         clearavg(4,2) = clearavg(4,2) / FLOAT(clearcount(4))
         clearavg(4,3) = clearavg(4,3) / FLOAT(clearcount(4))
         clearavg(4,4) = clearavg(4,4) / FLOAT(clearcount(4))
      end if

c     print *,' '
c     print *,'QCFLAG -1 count    ',clearcount(1)
c     print 1032,'HIRS 8 Temp     ',clearavg(1,1)
c     print 1032,'(RADT8C-RCLOUD) ',clearavg(1,2)
c     print 1032,'RADT8M          ',clearavg(1,3)
c     print 1032,'CTT             ',clearavg(1,4)
c     
c     print *,' '
c     print *,'QCFLAG 1 count     ',clearcount(2)
c     print 1032,'HIRS 8 Temp     ',clearavg(2,1)
c     print 1032,'(RADT8C-RCLOUD) ',clearavg(2,2)
c     print 1032,'RADT8M          ',clearavg(2,3)
c     print 1032,'CTT             ',clearavg(2,4)
c
c     print *,' '
c     print *,'QCFLAG 4 count     ',clearcount(3)
c     print 1032,'HIRS 8 Temp     ',clearavg(3,1)
c     print 1032,'(RADT8C-RCLOUD) ',clearavg(3,2)
c     print 1032,'RADT8M          ',clearavg(3,3)
c     print 1032,'CTT             ',clearavg(3,4)
c
c     print *,' '
c     print *,'QCFLAG 7 count     ',clearcount(4)
c     print 1032,'HIRS 8 Temp     ',clearavg(4,1)
c     print 1032,'(RADT8C-RCLOUD) ',clearavg(4,2)
c     print 1032,'RADT8M          ',clearavg(4,3)
c     print 1032,'CTT             ',clearavg(4,4)

c1032 FORMAT(A,F7.2)

      WRITE(R1TUNT,REC=1,IOSTAT=STAT1T) R1THKP
      CLOSE(R1TUNT)
      IF(STAT1T.NE.0) THEN
         KMSG='* ERROR * AT END OF SPGDVR WRITING R1T HEADER'
         PRINT *, KMSG
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
C
C  WRITE SUMMARY OF HIRS AND AMSU-A SCAN LINE FLAG FAILURES.
C
      IF(BADHFLAG(1).GT.0) THEN
         KMSG='DO_NOT_USE_SCAN FAILED        TIMES FOR THE HIRS'
         WRITE(C6TEMP,'(I6)') BADHFLAG(1)
         KMSG(24:29) = C6TEMP
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
      END IF
      IF(BADAFLAG(1).GT.0) THEN
         KMSG='DO_NOT_USE_SCAN FAILED        TIMES FOR THE AMSU-A'
         WRITE(C6TEMP,'(I6)') BADAFLAG(1)
         KMSG(24:29) = C6TEMP
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
      ENDIF
      IF(BADMFLAG(1).GT.0) THEN
         KMSG='DO_NOT_USE_SCAN FAILED        TIMES FOR THE MHS/AMSU-B'
         WRITE(C6TEMP,'(I6)') BADMFLAG(1)
         KMSG(24:29) = C6TEMP
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
      ENDIF
C
 1030 CONTINUE
C
C     DUMP THE CONTENTS OF THE 1B* ERROR MESSAGE BUFFER. A NON-ZERO
C     ERROR STATUS FROM B1SERR IS NOT FATAL.
C
      CALL B1SERR(ERRBUF,LINES,NOUT,STAT1B)
      IF (STAT1B.NE.0) THEN
         PRINT *,'ERROR STATUS FROM B1SERR = ',STAT1B
      ELSE IF (NOUT.GT.1) THEN
         DO I = 1, NOUT
            KMSG(1:60) = ERRBUF(I)
            PRINT *,KMSG
            CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
         END DO
      END IF
C

      KMSG(1:50)='***   E N D    O R B I T A L  P R O C E S S I N G '
      KMSG(51:100)=' FOR                                            '
      KMSG(56:88) = SHORTNAME
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
C
C     CLOSE KIDS FILE
C
      KMSG='***'
      CALL KIDS(KUNIT,SUBNAM,1,KMSG,2)
      IF(MISSTAT.EQ.88) THEN
         CALL EXIT_(88_4)
      ELSEIF(OQSTAT.EQ.89) THEN
         CALL EXIT_(89_4)
      ELSEIF(ERSTAT.EQ.55) THEN
         CALL EXIT_(55_4)
      ELSEIF(ERSTAT.EQ.60) THEN
         CALL EXIT_(60_4)
      ELSEIF(ERSTAT.EQ.67) THEN
         CALL EXIT_(67_4)
      ELSEIF(ERSTAT.EQ.99.AND..NOT.RTHLOCERR) THEN
         CALL EXIT_(99_4)
      ELSE
         CALL EXIT_(0_4)
      ENDIF
C
  107 FORMAT(I4,1X,I4)
  108 FORMAT(I4,'.',I4)
  109 FORMAT(I4,1X,I3,1X,I4,1X,I4,1X,I3,1X,I4,1X,I1)
  111 FORMAT(I4,'.',I3,'.',I4,'.',I4,'.',I3,'.',I4,'.',I1)
  300 FORMAT(/20X,'                FILTER SUMMARY',
     1   /80('*'),
     2   /'# OF GOOD SPOTS OVER LAND & OCEAN :        ',F6.0,',',F6.0,
     .    ' (ALL GOOD = ',F10.2,')',
     3   /'# QUESTIONABLE SPOTS OVER LAND & OCEAN :   ',F6.0,',',F6.0,
     .    ' (ALL QUES = ',F10.2,')',
     4   /'# OF BAD SPOTS OVER LAND & OCEAN :         ',F6.0,',',F6.0,
     .    ' (ALL BAD = ',F10.2,')',
     6   /16X,'          TOTAL # OF ALL TYPES OF SPOTS:', F7.0)
  400 FORMAT(80('-'),
     2   /'% OF GOOD SPOTS OVER LAND & OCEAN :        ',F6.2,',',F6.2,
     .    ' (ALL GOOD = ',F5.2,')',
     3   /'% QUESTIONABLE SPOTS OVER LAND & OCEAN :   ',F6.2,',',F6.2,
     .    ' (ALL QUES = ',F5.2,')',
     4   /'% OF BAD SPOTS OVER LAND & OCEAN :         ',F6.2,',',F6.2,
     .    ' (ALL BAD = ',F5.2,')',
     5   /80('*')/)
C
      END
C*********************************************************************
      INCLUDE 'BDRTPLVL'
      INCLUDE 'BDLEVELS'
      INCLUDE 'NMCDATA'
C*********************************************************************
C*********************************************************************
      SUBROUTINE SPGDV$(MSGCOD,BADUNT,FNAME,STAT)
C
C*********************************************************************
C   NAME- SPGDV$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 05/05/91    PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 3.0    DATE- 07/03/97    PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.1    DATE- 04/06/98    PROGRAMMER- LEAH WILSON (RSTX)
C   VERSION- 3.2    DATE- 05/28/98    PROGRAMMER- LEAH WILSON (RSTX)
C
C   FILES-
C     DSN                       I/O UNIT  FUNCTIONAL DESCRIPTION
C     ---                       --- ----  -----------------------
C     ...KIDS                    O  KUNIT KRAY INTERACTIVE DOCUMENTATION
C                                         FILE.
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE SPGDVR.
C   VERSION- 3.0  WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
C                 INCLUDE UNIT NUMBERS IN ERROR MESSAGES WHERE APPLICABLE.
C   VERSION- 3.1  ADD MESSAGE 303 FOR LIMB COLLECTION.
C   VERSION- 3.2  ADD FNAME FOR 102 MESSAGE.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE               TYPE  I/O  FUNCTION
C     --------               ----  ---  --------
C     MSGCOD                  INT   I   MESSAGE CODE USED TO
C                                       IDENTIFY THE ERROR
C                                       PATTERN
C     BADUNT                  INT   I   UNIT NUMBER OF ANY FILE THAT
C                                       CAUSED AN ERROR
C     FNAME                  CH*80  I   FILE NAME.
C     STAT                    INT   O   STATUS CODE: 99 = FATAL ERROR,
C                                       0 = NO OR NON-FATAL ERROR
C
      INTEGER   MSGCOD,STAT
C
C   SUBROUTINES CALLED- KIDS
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C1TEMP                  CH*1    HOLDING BUFFER FOR WRITING TO KIDS
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C
C*********************************************************************
C
      INTEGER       BADUNT
      CHARACTER*1   C1TEMP
      CHARACTER*4   C4TEMP
      CHARACTER*80  FNAME
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
      IF(MSGCOD.EQ.201) THEN
        KMSG=' * FATAL ERROR * CALLING MAPDVR FOR FRAME #   '
        WRITE(C4TEMP,'(I4)') FRMNBR
        KMSG(45:48) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=99
C
      ELSEIF(MSGCOD.EQ.202) THEN
        KMSG='NO TIMELY INFORMATION IN MAPPARM CDB FOR FRAME #    '
        WRITE(C4TEMP,'(I4)') FRMNBR
        KMSG(48:51) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=0
C
      ELSEIF(MSGCOD.EQ.203) THEN
        KMSG='* FATAL ERROR * MISSING COEFFICIENT IN MAPPARM CDB'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=99
C
      ELSEIF(MSGCOD.EQ.301) THEN
        KMSG=' * FATAL OPEN ERROR * UNIT     '
        WRITE(C4TEMP,'(I4)') BADUNT
        KMSG(28:31) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=99
C
      ELSEIF(MSGCOD.EQ.302) THEN
        KMSG=' * FATAL READ ERROR * OZONE FILE - UNIT        -  REC 1'
        WRITE(C4TEMP,'(I4)') BADUNT
        KMSG(41:44) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=99
C
      ELSEIF(MSGCOD.EQ.303) THEN
        KMSG(1:50)='* WARNING! * COULD NOT OPEN LIMB COLLECTION FILE O'
        KMSG(51:100)='N UNIT     .  NO SUB-1B* WILL BE CREATED.'
        WRITE(C4TEMP,'(I4)') BADUNT
        KMSG(58:61) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        STAT=99
C
      ELSEIF(MSGCOD.EQ.401) THEN
        KMSG=' FINAL FRAME (      ) PROCESSED '
        WRITE(C4TEMP,'(I4)') FRMNBR
        KMSG(16:19) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
        STAT=0
C
      ELSEIF(MSGCOD.EQ.102) THEN
        KMSG(1:50)='* FATAL ERROR * OPENING 1B* FILE - UNIT  .  1B* UT'
        KMSG(51:99)='ILITY RETURNED ERSTAT =      FOR THE FILE:       '
        WRITE(C1TEMP,'(I1)') BADUNT
        KMSG(41:41) = C1TEMP
        WRITE(C4TEMP,'(I4)') STAT
        KMSG(75:78) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        KMSG(1:80) = FNAME(1:80)
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        IF(STAT.EQ.-3) THEN
           KMSG = 'ERSTAT = -3 IMPLIES AN INCORRECT FILE NAME.'
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ENDIF
        STAT=99
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
