      SUBROUTINE HIRODB(HODREC,OBSHIR,HIRCNT,HODF1ST,HLAST,LIMREC,
     1                  CLWTHR,LPITHR,GETLIMOB,POLARTHR,MIDLATTR,
     >                  TROPTHR,CHN4SEL,CHN5SEL,A5THRES,CH4THRES,
     >                  PRECIPFG,CHN5LIM,C5THRES,ERSTAT)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- HIRODB
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE 
C
C   VERSION- 1.0    DATE- 05/09/91   PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 2.0    DATE- 11/07/94   PROGRAMMER-TERESA SIMMS (SMSRC)
C            XATOV-66  CHANGED FOR ATOVS FORMAT OF HIRS DATA CYCLE.
C                      INCLUDED NEW FILE ACCESS UTILITY ROUTINE.
C                      ADDED INCLUDE /INSPARM/ AND REMOVED CALLING
C                      ARGUMENTS.
C   VERSION- 2.1    DATE- 07/17/95   PROGRAMMER- LI LU (SMSRC)
C            ATOVS-142  CALLS TO SPGFAU WILL BE ADDED FOR HIR_SFCT
C	  	        AND HIR_WVH8.
C   VERSION- 2.2    DATE- 12/8/95    PROGRAMMER- CARLA NEIGH (SMSRC)
C           ATOVS-281:  REMOVED CALL TO SPGFAU FOR HIR_FOVQ.
C                       REMOVED ISCNQ AND SCNQ VARIABLES.
C   VERSION- 3.0    DATE- 06/19/96   PROGRAMMER- LEAH WILSON (HSTX)
C           MODIFICATIONS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 3.1    DATE- 07/30/97   PROGRAMMER- LEAH WILSON (HSTX)
C           ADD FAU NAME TO ERROR HANDLING CALL.
C   VERSION- 4.0    DATE- 08/30/97   PROGRAMMER- LEAH WILSON (HSTX)
C           REPLACE IRDB WITH HODF.  REMOVE HIROD$.  ADD SURFACE
C           TEMPERATURE ESTIMATES FOR HIRS CH. 8, 18, AND 19.
C           ADD OBSHIR TO GET NODE.
C   VERSION- 4.1    DATE- 02/20/98   PROGRAMMER- LEAH WILSON (R-STX)
C           ADD HIRCNT TO COUNT ACTUAL NUMBER OF RECS WRITTEN.
C           ADD HODF1ST,HLAST TO STORE ACTUAL LINE NUMBER OF FIRST
C           AND LAST DATA RECORDS.
C   VERSION- 4.2    DATE- 04/06/98   PROGRAMMER- AMERICO ALLEGRINO,
C                                                LEAH WILSON (R-STX)
C            ADD CODE TO EXTRACT THE NECESSARY OBSERVATIONS FOR THE
C            LIMB CORRECTION COEFFICIENT GENERATION PROCESS.
C   VERSION- 4.3    DATE- 08/18/98   PROGRAMMER- MICHAEL FERGUSON
C                                                   (RSTX)
C            SURFACE TEMPERATURE ESTIMATES TAKEN FROM COMMON REGCOE
C            INSTEAD OF BEING RE-CALCULATED.
C   VERSION- 4.4    DATE- 08/24/98   PROGRAMMER- MICHAEL FERGUSON
C                                                   (RSTX)
C            PREDICTED MSU AND AVHRR VALUES USED IN CLOUD DETECTION
C            TESTS ADDED TO HODF VIA MODIFIED REGCOE BUFFER.
C
C   VERSION- 4.5    DATE- 10/27/98   PROGRAMMER- LEAH WILSON, RAYTHEON
C
C            ADDED INTEGER CLOUD FLAG TO WORD 161.
C
C   VERSION- 4.6    DATE- 11/06/98   PROGRAMMER- A. ALLEGRINO, RAYTHEON
C
C            ADDED CODE TO OUTPUT THE LIMB ADJUSTED HIRS DATA TO WORDS
C            162 THROUGH 180 OF THE HIRS ODF RECORD.
C
C   VERSION- 4.7    DATE- 11/18/98   PROGRAMMER- MP FERGUSON, (RITSS)
C
C            ADDED CODE TO OUTPUT THE STANDARD DEVIATION OF THE AVHRR 4
C            TO WORD 181 OF THE HIRS ODF RECORD.
C
C   VERSION- 4.8    DATE- 11/20/98   PROGRAMMER- L. WILSON, RAYTHEON
C            ADDED /R1TDAT/ TO ALLOW HIRS CLOUD MASK TO BE SAVED FOR USE
C            IN CLDTOP.
C
C   VERSION- 4.9   DATE- 12/28/98    PROGRAMMER- L. WILSON, RAYTHEON
C            ADDED /AZIMUTH/ TO STORE LOCAL AZIMUTH ANGLE.  ADD CHECK OF
C            MONTH BEFORE DYOFMN IS ACCESSED, TO PREVENT OVER/UNDERFLOW 
C            ERRORS.  FOR THIS ERROR, KIDS MESSAGE IS PRINTED, AND LCDC IS
C            TURNED OFF IF IT WAS ON.
C
C   VERSION- 4.9   DATE-  4/14/99    PROGRAMMER- A. ALLEGRINO, RAYTHEON
C            REMOVED LCHIRS FROM CALLING LIST AND REPLACED ALL LCHIRS
C            WITH HIRDAT SINCE HIRDAT CONTAINS THE LIMB CORRECTED DATA.
C
C   VERSION- 4.10  DATE-  5/05/99    PROGRAMMER- A. ALLEGRINO, RAYTHEON
C            CHANGED THIRD DIMENSION OF OBSHIR FROM 20 TO 35
C
C   VERSION- 4.11  DATE-  7/13/99    PROGRAMMER- A. ALLEGRINO, RAYTHEON
C            ADDED AMSU-A SURFACE PRODUCTS AND AMSU-A CHS 4, 5, AND 6
C            CLOUD CLEARED TO THE HODF
C   VERSION- 4.12  DATE-  7/13/99    PROGRAMMER- M. FERGUSON, RITSS
C            CORRECTED AVHRR 3A-3B SWITCHING ALGORITHM TO PLACE MISSING
C            FLAG IN CORRECT 3A LOCATIONS WHEN 3A IS NOT PRESENT.
C   VERSION- 4.13  DATE-  8/16/99    PROGRAMMER- A. ALLEGRINO, RITSS
C            PLACED THE VALUE OF HIRS CHANNEL 20 (THE ALBEDO) INTO ITS
C            POSITION ON THE HODF VIA THE HIRDAT BUFFER.
C   VERSION- 4.14  DATE-  3/29/00    PROGRAMMER- A. ALLEGRINO, RITSS
C            ADDED THE LOGICAL ARRAY PRECIPFG TO INDICATE AREAS OF 
C            PRECIPITATION AS DETERMINED BY THE MEDIAN FILTER AND
C            THE CLOUD LIQUID WATER TEST. THIS FLAG IS PLACED ONTO
C            THE HODF.
C   VERSION- 4.15  DATE- 04/19/00    PROGRAMMER- LEAH WILSON, RAYTHEON
C            CHANGE LOOPING OVER J FROM "J=1+N,HTOTLN-N" (J=6,45) TO
C            "J=HTOTBF,HTOTLN-1" (J=10,49) TO REFLECT NEW HIRS FRAME
C            BOUNDARIES. SEE INHIRS FOR FRAME DESCRIPTION.
C   VERSION- 4.16  DATE- 06/13/00    PROGRAMMER- M.P.FERGUSON, RITSS
C            SETTINGS FOR HODFBUF ELEMENT 185 CHANGED, ELEMENT 196 IS
C            ADDED.
C   VERSION- 4.17  DATE- 06/13/00    PROGRAMMER- A. ALLEGRINO, RAYTHEON,
C                                                L. WILSON, RAYTHEON
C            MODIFY SOFTWARE THAT CONTROLS THE LIMB CORRECTION DATA 
C            COLLECTION IN ORDER TO SKIP SCAN LINES FOR THE FIRST
C            FRAME BASED ON SKIPHIR FLAG. ADD /FRAME1/ AND
C            INTRODUCE THE BADLINE VARIABLE.
C   VERSION- 4.18  DATE- 08/02/00   PROGRAMMER- M.P.FERGUSON, (RITSS)
C            MODIFIED TO WRITE MISSING VALUES IN THE CASE WHERE
C            DUMMIED HIRS IS BEING USED AS FILL TO ALLOW PROCESSING 
C            OF RETRIEVALS IN A NON-HIRS FORMAT, AS INDICATED BY
C            FLAG BADHIR (WR # 105).
C   VERSION- 4.19  DATE- 08/09/00   PROGRAMMER- R. KELLEY/MP FERGUSON
C            ADD COMMON HIRSTAT
C   VERSION- 4.20  DATE- 08/29/00   PROGRAMMER- A. ALLEGRINO, (RITSS)
C            SET CLW TO ZERO IF LESS THAN ZERO (FOR THE 2ND TIME).
C   VERSION- 4.21  DATE- 09/18/00   PROGRAMMER- A. ALLEGRINO, (RITSS)
C            ADDED TO HIRS ODF RECORD (SLOTS 197 & 198) ALL OF THE DATA FROM 
C            AMSUA CHANNELS 4 AND 5 WITH WATER VAPOR REMOVED. (WR #102)
C   VERSION- 4.22  DATE- 02/27/02   PROGRAMMER- A. ALLEGRINO, (RITSS)
C            ADDED THE LOGICAL ARRAY PRECPFG2 TO BE BASED ON
C            CLOUD LIQUID WATER VALUE NOT THE COMBINED CLD/MEDIAN
C            THRESHOLD VALUE.  THIS FLAG WILL ALSO BE AFFECTED BY THE 
C            AMSUA CH4 TEMPERATURE (SEE A4THRES) AND THE LATITUDE (SEE INLAT
C            AND ISLAT).  (WR#158 AND 191)
C   VERSION- 4.23  DATE- 03/05/02   PROGRAMMER- A. ALLEGRINO, (RITSS)
C            ADDED A CHECK OF SEA SURFACE TEMPERATURE IN ORDER TO CONVERT
C            ALL VALUES LESS THAN 150K TO MISSING VALUES. THIS SETS ALL LAND
C            AREAS TO MISSING SINCE LAND AREAS HAVE A ZERO VALUE. (WR#186)
C   VERSION- 4.24  DATE- 06/20/02   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C            Added calling arguments chn4sel, chn5sel, a5thres, ch4thres,
C            chn5lim, and c5thres and replaced latitude-dependent 
C            tropics/mid-latitude boundary with channel brightness
C            temperature thresholds.
C            Changed local LOGICAL array PRECPFG2 to a single
C            integer value with three meanings:
C              0 = No CLW contamination
C              1 = CLW contamination of AMSU-A channel 4
C              2 = CLW contamination of AMSU-A channels 4 and 5
C            Removed calling argument A4THRES. 
C            Set CLW and TPW to missing if the value is less than 0.
C            (WR #196)
C   VERSION- 4.19.1 DATE 11/14/01  PROGRAMMER- RD ROLLINS
C            PORTED TO IBM SP
C            REMOVED CRAY2IBM 
C   VERSION- 5.0    DATE-02/13/02  PROGRAMMER- R. ROLLINS (CSC)
C            CHANGED I4SPV to I2SPV
C   VERSION- 5.1    DATE- 02/27/03 PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C   VERSION- 5.2    DATE- 02/15/06 PROGRAMMER- FRANK TILLEY (ERT)
C            REPLACED COMMON /R1TDAT/ WITH COMMON /CLOUDMSK/ UNDER
C            WR #296.
C   VERSION- 5.3    DATE- 03/09/10 PROGRAMMER- FRANK TILLEY (IMSG)
C            MOVED INITIALIZATION OF THE OUTPUT BUFFER TO EXECUTE
C            INSIDE THE "SPOTS" LOOP SO THE RECORD IS INITIALIZED
C            FOR EACH SPOT.
C
C   FUNCTION-** THIS SUBROUTINE WRITES OUT A FRAME OF HIRS DATA
C            TO THE HIRS INSTRUMENTAL DATA FILE A CYCLE(SPOT)
C            AT A TIME.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O  UNIT   FUNCTIONAL DESCRIPTION
C     ------------------------ ---  ----   ----------------------
C     .../HODF                  O  HIDFU   HIRS INSTRUMENAL DATA FILE
C     .../KIDS                  O  KUNIT   KIDS MESSAGE FILE.
C     .../*HIRX*.LIMOBS         O  HLMUNT  HIRS LIMB DATA FILE.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     HODREC            INT    I  HODF RECORD NUMBER.
C     OBSHIR            REAL   I  ARRAY OF DIMENSION (HNSPOT,HTOTLN,
C                                 HDATEL) SIMILAR TO HIRDAT, EXCEPT
C                                 THE DATA IS NOT ADJUSTED IN ANY
C                                 WAY, AND NODE IS STORED IN THE 20TH
C                                 ELEMENT FOR EACH SPOT.    
C     HIRCNT            INT    O  NUMBER OF RECORDS WRITTEN TO HODF.
C     HODF1ST           INT    O  LINE NUMBER OF FIRST HODF RECORD.
C     HLAST             INT    O  LINE NUMBER OF LAST HODF RECORD.
C     LIMREC            INT   I/O COUNTS NUMBER OF RECORDS IN HLMUNT.  
C     GETLIMOB          INT    I  =1 IF LIMB COLLECTION IS TO BE DONE.
C     POLARTHR          REAL   I  POLAR THRESHOLD FOR 90N-60N AND 60S-90S
C     MIDLATTR          REAL   I  MID LATITUDE THRESHOLD FOR 30N-60N AND 
C                                 30S-60S
C     TROPTHR           REAL   I  TROPICAL THRESHOLD FOR 30N-30S
C     CHN4SEL           REAL   I  AMSU-A CHANNEL 4 BT THRESHOLD BELOW
C                                 WHICH POLAR CLW THRESHOLDS ARE USED.
C     CHN5SEL           REAL   I  AMSU-A CHANNEL 5 BT THRESHOLD ABOVE
C                                 WHICH AMSU-A CLW SCREENING IS DONE
C                                 IN THE TROPICS.
C     A5THRES           REAL   I  CLW VALUE IN THE TROPICS ABOVE WHICH
C                                 BOTH AMSU-A CHANNELS 4 AND 5 ARE
C                                 DEEMED TO BE CLW CONTAMINATED.
C     PRECIPFG          LOG    I  LOGICALLY ARRAY DENOTING PRECIP AREAS
C                                 AT THE HIRS FOV FROM CLW/MEDIAN FILTER.
C     CHN5LIM           REAL   I  CHANNEL 5 BRIGHTNESS TEMPERATURE 
C                                 THRESHOLD USED WITH C5THRES FOR CLW
C                                 SCREENING OF AMSU-A.
C     C5THRES           REAL   I  CLW THRESHOLD USED WITH CHN5LIM FOR
C                                 CLW SCREENING OF AMSU-A.
C     ERSTAT            INT    O  NORMALLY SET TO 0 BUT IF AN ERROR
C                                 OCCURS AN ERROR STATUS CODE IS SET.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'FRMINF'
      INCLUDE 'REGCOE'
      INCLUDE 'HIRSTAT'
      INCLUDE 'SPVAL'
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'MONSTA'
      INCLUDE 'CLOUDMSK'
      INCLUDE 'AZIMUTH'
      INCLUDE 'AVRCON'
      INCLUDE 'FRAME1'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  DESCRIPTION
C     --------          ----  --------------------------------------
C     I,J,K             INT   LOOP CONTROL INDICES.
C     STAT              INT   ERROR STATUS RETURNED BY FMU'S.
C     DUMMY             INT   VECTOR OF DIMENSION 2 HOLDING THE PLACE
C                             OF THE RECORD AND BYTE POINTERS.
C     RDATCY            REAL  VECTOR OF DIMENSION INPARM:HNCHAN WITH
C                             BRIGHTNESS TEMPERATURE HIRS DATA.
C     SPOTNR            INT   SPOT NUMBER WITHIN THE DATA FRAME.
C     M                 INT   SCAN LINE NUMBER USED IN COMPUTING
C                             THE SPOTNR.
C     N                 INT   AMOUNT OF OVERFLOW SCAN LINES IN HIRDAT.
C     TERTYP            INT   INTEGER TO HOLD THE TERRAIN TYPE
C                             VALUE IN THE REAL HIRANC BUFFER.
C     TOTCHN            INT   TOTAL NUMBER OF CHANNELS IN THE HIRS,
C                             AMSU-A, AND AMSU-B BUFFERS.
C     SPTCON            INT   INTEGER TO HOLD THE SPOT CONFIDENCE
C                             VALUE IN THE REAL HIRANC BUFFER.
C     DUMOBS            INT   DUMMY FOR WRITING TO HODFBUF.
C     C6TEMP           CH*6   TEMPOARARY FOR WRITING TO KIDS.
C     KMSG            CH*100  KIDS MESSAGE BUFFER.
C     HODFBUF           INT   HODF RECORD BUFFER.
C     H1ST              LOG   TRUE IF THIS IS THE FIRST DATA RECORD,
C                             ELSE FALSE.
C     LIMHOBS          INT    ARRAY TO HOLD OBSERVATIONS FOR LIMB CORREC
C     YEAR             INT    SCAN YEAR
C     MONTH            INT    SCAN MONTH OF THE YEAR
C     DAY              INT    SCAN DAY OF THE MONTH
C     HOUR             INT    SCAN HOUR OF THE DAY
C     MIN              INT    SCAN MINUTES
C     SEC              INT    SCAN SECONDS
C     SCNTIME          INT    ARRAY HOLDING THE SCAN DATE AND TIME
C     TOTELEM          INT    TOTAL NUMBER OF ELEMENTS IN THE LIMB OBS
C                             ARRAY (NUMBER OF SPOTS * ( NUMBER OF CHANN
C                             LATITUDE + LONGITUDE + NODE)) + 4 TIME ELE
C     DYOFMN           INT    BEGINNING JULIAN DAY OF EACH MONTH -1
C     JDAY             INT    JULIAN DAY
C     I4LIMHREC        INT    I*4 VERSION OF THE LIMB OBSERVATION ARRAY
C     BADLINE          INT    BAD SCAN LINE INDICATOR FOR LIMB COLLECTION
C                             DATA COLLECTION
C     INLAT            INT    NORTHERN LATITUDE LIMITS
C     ISLAT            INT    SOUTHERN LATITUDE LIMITS
C     PRECPFG2         INT    THREE PART FLAG FOR CLW PRECIP SCREENING.
C                               0 = NO PRECIP CONTAMINATION
C                               1 = AMSU-A CHANNEL 4 IS CONTAMINATED
C                               2 = AMSU-A CHANNELS 4 & 5 ARE
C                                   CONTAMINATED
C
C   SUBPROGRAMS CALLED-  SFCEST, KIDS 
C
C   EXIT STATES-
C       ERSTAT =  0; INDICATING NO ERRORS FOUND.
C       ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
C
C*********************************************************************
C
      INTEGER ERSTAT, HODREC, HODF1ST, HLAST, HIRCNT
      REAL OBSHIR(HNSPOT,HTOTLN,35),CLWTHR,LPITHR,CH4THRES
      REAL POLARTHR,MIDLATTR,TROPTHR,CHN4SEL,CHN5SEL,A5THRES
      REAL CHN5LIM,C5THRES
      LOGICAL PRECIPFG(HNSPOT,HTOTLN)
C
      CHARACTER*1 I4LIMHOBS(5168)
      CHARACTER*2 C2TEMP
      CHARACTER*4 C4TEMP
      CHARACTER*6 C6TEMP
      CHARACTER*100 KMSG
      INTEGER SPOTNR, STAT, HODFBUF(200), IOERR,BADLINE
      INTEGER I, J, K, M, N, TERTYP, SPTCON, TOTCHN, LIMLEN
      INTEGER DUMMY(2), INDEX, DUMOBS, IC, DAYOFMN(12), JDAY
      INTEGER TOTELEM, SCNTIM(3),YEAR,MONTH,DAY,HOUR,MIN
      INTEGER SEC,FORDGTYR
      INTEGER*4 LIMHOBS(HNSPOT*(HNCHAN+3)+4)
      INTEGER*2 I2HODF(200)
      INTEGER LIMREC,GETLIMOB,DYOFMN(12),SCNTIME(3),ITEMP
      INTEGER LIMIDX,JJJ,IERR,LIMERR, GETLIMB
      INTEGER INLAT(12),ISLAT(12), PRECPFG2 
      EQUIVALENCE(I4LIMHOBS, LIMHOBS)
      REAL RDATCY(HNCHAN),RTEMP,RDUM
      LOGICAL H1ST
      LOGICAL ISR4SPV

      EQUIVALENCE(RTEMP,ITEMP)

C     DATA INLAT/10,10,12,14,16,18,18,18,16,14,12,10/
C     DATA ISLAT/-18,-18,-18,-16,-14,-12,-10,-10,-10,-12,-14,-16/
      DATA INLAT/18,20,22,24,26,28,30,28,26,24,22,20/
      DATA ISLAT/-30,-28,-26,-24,-22,-20,-18,-20,-22,-24,-26,-28/
      DATA DYOFMN/0,31,59,90,120,151,181,212,243,273,304,334/
C
      GETLIMB = GETLIMOB
      HIRCNT = 0
      HODF1ST = 0
      HLAST = 0
      H1ST = .TRUE.
      BADLINE = 0
C
C INITIALIZE N TO REPRESENT HALF OF THE BUFFER OVERFLOW SCAN LINES
C BECAUSE HALF OF THE OVERFLOW LINES ARE AT THE BEGINNING AND THE END.
C
      IOERR = 0
      SPOTNR = 0
      N = HTOTBF/2
      M = N + 1
      LIMLEN = HNSPOT*(HNCHAN+3)+4
      TOTCHN = HNCHAN + ANCHAN + BNCHAN
      PRINT *, 'AVAUSN = ',AVAUSN
C
C THE HIRS BUFFERS, PASSED IN THE CALL, CONTAIN A 256 SECOND DATA FRAME.
C LOOP FROM LINE 10 TO 49 - SEE FRAME DESCRIPTION IN INHIRS.
C
C BEGIN LOOP OF ALL SCAN LINES.  IF THE GETLIMOB FLAG IS SET TO 1, THEN
C EXTRACT THE DATA NECESSARY FOR THE LIMB CORRECTION COEFFICIENT GENERATION.
C
      DO 50 J = HTOTBF,HTOTLN-1
C
C SKIP ANY LINES WHERE THE CENTURY DAY OF THE SCAN LINE START TIME IS
C ZERO OR LESS (USUALLY MEANS THE LINE IS MISSING, AS AT THE BEGINNING
C OR END OF AN ORBIT).

         IF (HSCTIM(1,J).LE.0) GO TO 50
C
         CALL ODBTIM(HSCTIM(1,J),HSCTIM(2,J),SCNTIME)
         MONTH = SCNTIME(1) - ((SCNTIME(1)/100)*100)
C        
         IF(GETLIMB .EQ. 1) THEN
C
C INITIALIZE THE LIMB DATA ARRAY
C
            TOTELEM = HNSPOT*(HNCHAN+3) + 4
            DO  M=1,TOTELEM
               LIMHOBS(M)= 0.0
            ENDDO
CCLW
            IF(FRMNBR.EQ.1.AND.J.LT.SKIPHIR) THEN
               BADLINE = 1
            ELSE
CCLW
C
C  LOAD ORBIT NUMBER INTO LIMB DATA ARRAY & GET SCAN TIME IN YEAR/MONTH
C  , DAY/HOUR AND MIN/SEC AND THEN CONVERT TIME TO JULIAN DAY AND TENTHS
C  SECONDS.
C
            LIMHOBS(1)=SORB(2)
            CALL ODBTIM(HSCTIM(1,J),HSCTIM(2,J),SCNTIME)
            YEAR = SCNTIME(1)/100
            MONTH = SCNTIME(1) - ((SCNTIME(1)/100)*100)
            DAY = SCNTIME(2)/100
            HOUR = SCNTIME(2) - ((SCNTIME(2)/100)*100)
            MIN = SCNTIME(3)/100
            SEC = SCNTIME(3) - ((SCNTIME(3)/100)*100)
C
C CHECK TO SEE IF YEAR IS 2000 OR ABOVE.
C
            FORDGTYR = YEAR
            IF(YEAR .LT. 98) FORDGTYR=2000 + YEAR
C
C CALCULATE THE JULIAN DAY
C
            IF(MONTH.GE.1.AND.MONTH.LE.12) THEN
               JDAY = DYOFMN(MONTH) + DAY
               IF(MOD(YEAR,4).EQ.0 .OR. MOD(FORDGTYR,400).EQ.0) THEN
                  IF(MONTH.GT.2) JDAY = JDAY + 1
               ENDIF
               LIMHOBS(2) = YEAR
               LIMHOBS(3) = JDAY
C
C CALCULATE THE YEARTIME IN DECISECONDS
C
               LIMHOBS(4)= SEC* 10 + MIN*600 + HOUR*36000 +
     1                  (JDAY-1)*864000
C
            ELSE
               KMSG(1:44)='* WARNING * MONTH    FOUND FOR HIRS LINE   .'
               WRITE(C2TEMP,'(I2)') MONTH 
               KMSG(19:20) =  C2TEMP
               WRITE(C2TEMP,'(I2)') J       
               KMSG(42:43) =  C2TEMP
CASA           KMSG(45:100) = '  FURTHER HIRS LCDC TURNED OFF.'
               KMSG(45:100) = '  LCDC THE NEXT LINE IN THE FRAME.'
               CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
               GETLIMB = 0
               BADLINE =1
            ENDIF
CCLW
            ENDIF
CCLW
         ENDIF

C
C LOOP THROUGH ALL SPOTS.
C
         DO 40 I = 1,HNSPOT
C
C INITIALIZE THE OUTPUT BUFFER FOR EACH HIRS SPOT
C
            DO INDEX=1,200
               HODFBUF(INDEX) = I2SPV
            END DO    
C
C PACK THE HIRS BRIGHTNESS TEMPERATURES FOR CHANNELS 1-19 AND
C THE % ALBEDO FOR CHANNEL 20, IF PRESENT.  
C
C START FILLING HODF RECORD BUFFER.
C
   10       HODFBUF(1) = 2
            HODFBUF(2) = SATNM
            DUMOBS = 3
            DO 20 K = 1,HDATEL
               IF(K .LE. HNCHAN) THEN
                  RDATCY(K)=OBSHIR(I,J,K)
                  IF(K .EQ. 20) RDATCY(K) = HIRDAT(I,J,K)
                  IF(ISR4SPV(RDATCY(K)) .OR. BADHIR(I,J,K).EQ.1) THEN
                     HODFBUF(DUMOBS) = I2SPV
                     IF(K .LT. 20) HODFBUF(DUMOBS+159) = I2SPV
                     IF(GETLIMB.EQ.1) LIMHOBS(K+7+(23*(I-1)))=0
                  ELSE
                     HODFBUF(DUMOBS) = RDATCY(K) * 64.0
                     IF(K .LT. 20) HODFBUF(DUMOBS+159) = 
     1                             HIRDAT(I,J,K) * 64.0
                     IF(GETLIMB.EQ.1) THEN
                        LIMHOBS(K+7+(23*(I-1)))= 
     1                             NINT(RDATCY(K)*100.0)
                        IF(I.GE.HNSPOT-5) LIMIDX= K+7+(23*(I-1))
                     ENDIF
                  ENDIF
                  DUMOBS = DUMOBS + 1
                ENDIF
   20       CONTINUE
C
C PACK THE SOLAR ZENITH ANGLE OF THE HIRS SPOT.
C
            HODFBUF(23) = NINT(HIRANC(I,J,3)*128.0)
C
C PACK THE SATELLITE ZENITH ANGLE OF THE HIRS SPOT.
C
            HODFBUF(24) = NINT(HIRANC(I,J,5)*128.0)
C
C PACK THE SPOT NUMBER, IN REFERENCE TO THE DATA FRAME.
C
            IF(J .GT. M) THEN
               SPOTNR = ((J-(N+1))*HNSPOT)
                M = J
            ENDIF
            SPOTNR = SPOTNR + 1
            HODFBUF(25) = SPOTNR
C
C PACK THE LATITUDE OF THE SPOT.
C
            HODFBUF(26) = NINT(HIRANC(I,J,1)*128.0)
C
C PACK THE LONGITUDE OF THE SPOT.
C
            HODFBUF(27) = NINT(HIRANC(I,J,2)*128.0)
C
C PACK THE SPOT CONFIDENCE VALUE.
C
            SPTCON = NINT(HIRANC(I,J,6))
            IF(ISR4SPV(HIRANC(I,J,6))) SPTCON = I2SPV
            HODFBUF(28) = SPTCON
C
C PACK THE CHANNEL AVAILABILTY.
C
            HODFBUF(29) = NINT(HIRANC(I,J,7))
C
C PACK THE TERRAIN HEIGHT (METERS).
C
            HODFBUF(30) = NINT(HIRANC(I,J,8))
C
C PACK THE TERRAIN TYPE.
C
            HODFBUF(31) = NINT(HIRANC(I,J,9))
C
C PACK THE SOLAR AZIMUTH.
C
            HODFBUF(32) = NINT(HIRANC(I,J,4)*128.0)
C
C PACK THE SEA SURFACE TEMPERATURE (K).
C
            IF(HIRANC(I,J,10) .GT. 0) THEN
             HODFBUF(33) = NINT(HIRANC(I,J,10)*64.0)
            ELSE
             HODFBUF(33) = I2SPV
            ENDIF
C
C PACK THE FORECAST POTENTIAL TEMPERATURE (K).
C
            HODFBUF(34) = NINT(HIRANC(I,J,11)*64.0)
C
C PACK THE FORECAST SURFACE PRESSURE (MB).
C
            HODFBUF(35) = NINT(HIRANC(I,J,12)*10.0)
C
C PACK THE FORECAST RELATIVE HUMIDITY.
C 
            HODFBUF(36) = NINT(HIRANC(I,J,20)*64.0)
C
C PACK THE CLOUD TEST ONE RESULTS.
C
            HODFBUF(37) = NINT(HIRANC(I,J,13))
C
CCLW - NOTE THAT THE CLOUD TEST RESULTS ARE TOO BIG TO FIT IN
CCLW - AN I*2 - THEY MUST SOMEHOW BE REPACKED TO BE UNPACKED
CCLW - PROPERLY
C
C PACK THE CLOUD TEST TWO RESULTS.
C
            HODFBUF(38) = NINT(HIRANC(I,J,14))
C
C PACK THE N* VALUE FROM THE AVHRR.
C
            HODFBUF(39) = NINT(HIRANC(I,J,17)*1024.0)
C
C PACK THE N* VALUE FROM THE IR/MW.
C
            HODFBUF(40) = NINT(HIRANC(I,J,21)*1024.0)
C
C PACK THE ENVELOPE TOPOGRAPHY VALUE.
C
            HODFBUF(41) = NINT(HIRANC(I,J,16))
C
C PACK THE FORECAST SURFACE TEMPERATURE.
C
            HODFBUF(42) = NINT(HIRANC(I,J,22)*64.0)
C
C PACK THE SURFACE TEMPERATURE ESTIMATE FOR HIRS CH. 8
C
CCMPF       RDUM=SFCEST(HIRDAT,HNSPOT,HTOTLN,HDATEL,TSE8,I,J,8)
            RDUM=ESTH8(I,J)
            HODFBUF(43) = NINT(RDUM*64.0)
C
C PACK THE AVHRR DATA (STORED IN HIRDAT WORDS 36-143).
C
            IC = 44
            DO INDEX=36,143 
               IF(.NOT. ISR4SPV(HIRDAT(I,J,INDEX)))
     1            HODFBUF(IC) = NINT(HIRDAT(I,J,INDEX)*64.0) 
               IC = IC + 1  
            END DO    
C
C SET APPROPRIATE 3A OR 3B DATA BUFFERS TO MISSING.
C
            IF(AVAUSN(J))THEN
               DO JJJ = 98,115
                 HODFBUF(JJJ) = I2SPV
               END DO
            ELSE
               DO JJJ = 80,97  
                 HODFBUF(JJJ) = I2SPV
               END DO
            ENDIF
C
C PACK THE ORBITAL NODE (1=ASCENDING,2=DESCENDING).
C
            HODFBUF(152) = NINT(OBSHIR(I,J,20))
C
C PACK THE SURFACE TEMPERATURE ESTIMATE FOR HIRS CH. 18
C
CCMPF       RDUM=SFCEST(HIRDAT,HNSPOT,HTOTLN,HDATEL,TSE18,I,J,18)
            RDUM=ESTH18(I,J)
            HODFBUF(153) = NINT(RDUM*64.0)
C
C PACK THE SURFACE TEMPERATURE ESTIMATE FOR HIRS CH. 19
C
CCMPF       RDUM=SFCEST(HIRDAT,HNSPOT,HTOTLN,HDATEL,TSE19,I,J,19)
            RDUM=ESTH19(I,J)
            HODFBUF(154) = NINT(RDUM*64.0)
C
C WRITE THE PREDICTED VALUES FOR SHORT & LONG WAVE MSU PRED AND AVHRR 3
C AND AVHRR 4 PREDICTED, AS WELL AS THE STANDARD DEVIATION OF AVHRR 4.
C
            RDUM=PSMSU2(I,J)
            HODFBUF(155) = NINT(RDUM*64.0)
            RDUM=PSMSU3(I,J)
            HODFBUF(156) = NINT(RDUM*64.0)
            RDUM=PLMSU2(I,J)
            HODFBUF(157) = NINT(RDUM*64.0)
            RDUM=PLMSU3(I,J)
            HODFBUF(158) = NINT(RDUM*64.0)
            RDUM=PAVHR3(I,J)
            HODFBUF(159) = NINT(RDUM*64.0)
            RDUM=PAVHR4(I,J)
            HODFBUF(160) = NINT(RDUM*64.0)
            RDUM=AVR4SD(I,J)
            HODFBUF(181) = NINT(RDUM*64.0)
C
C SINCE THE CLOUD TESTS ARE TOO LARGE TO BE STORED IN AN I*2, STORE
C THE NET RESULT, I.E. 0=CLEAR, 1=CLOUDY, 2=NSTAR (NOT YET SET), 4=
C NO-HIRS (NOT YET SET) IN WORD 161 OF THE HODF BUFFER.
C
            RTEMP = HIRANC(I,J,14)
            IF(.NOT.BTEST(ITEMP,0)) THEN
               HODFBUF(161) = 0
            ELSE
               HODFBUF(161) = 1
            ENDIF
            CLDMSK(I,J) = HODFBUF(161)
C
C STORE THE LOCAL AZIMUTH ANGLE
C
            HODFBUF(182) = NINT(LOCLAZM(I,J)*128.0)
C
C STORE THE AMSU-A SURFACE PRODUCTS & THE CLOUD CLEARED AMSU-A CHANNELS
C 4 AND 5 AT THE HIRS FOV.
C  HODFBUF(183) = CLOUD LIQUID WATER
C  HODFBUF(184) = TOTAL PRECIPITABLE WATER
C  HODFBUF(185) = SEA ICE CONCENTRATION
C  HODFBUF(186) = RAIN/NORAIN FLAG   
C  HODFBUF(187) = SNOW COVER        
C  HODFBUF(188) = CLOUD CLEARED AMSU-A CH 4
C  HODFBUF(189) = CLOUD CLEARED AMSU-A CH 5
C
C  ADDED CLOUD CLEARED AMSU-A CH 6 AND THE CLW/LPI PRECIPITATION
C  FLAG TO THE HODF AND THE NON CLOUD CLEARED AMSU-A CH 6
C
C  HODFBUF(190) = CLOUD CLEARED AMSU-A CH 6
C  HODFBUF(191) = AMSU-A CH 4
C  HODFBUF(192) = AMSU-A CH 5
C  HODFBUF(193) = AMSU-A CH 6
C  HODFBUF(194) = AMSU-A CH 7
C  HODFBUF(195) = CLW/LPI PRECIPITATION FLAG
C
C  HODFBUF(195) NOW REFLECTS THE MEDIAN FILTER/CLW PRECIPITATION
C  DETERMINATION. ASA (3/29/00)
C
C  HODFBUF(199) AND PRECPFG2 ARE BASED ON THE CLOUD LIQUID WATER , AMSUA CH4,
C  AND LATITUDE AND TIME OF YEAR. ASA (2/27/02)
C
C  TROPICAL/MID-LATITUDE/POLAR AREA DETERMINATION WAS CHANGED TO RELY
C  ON BRIGHTNESS TEMPERATURE RATHER THAN MONTHLY CHANGING VALUES
C  (4/24/02,FHT - WR #196)
C
            PRECPFG2 = 0
C
C  Polar cases - use corrected AMSU-A channel 4
C
            IF ((HIRDAT(I,J,154).LT.CH4THRES).AND.
     >          (HIRDAT(I,J,149).GT.POLARTHR)) PRECPFG2 = 1
C
C  Mid-latitude cases - use corrected AMSU-A channel 4
C
            IF ((HIRDAT(I,J,154).GE.CH4THRES).AND.
     >          (HIRDAT(I,J,154).LT.CHN4SEL).AND.
     )          (HIRDAT(I,J,149).GT.MIDLATTR)) PRECPFG2 = 1
C
C  Non-polar cases - use corrected AMSU-A channel 4, uncorrected
C                   AMSU-A channel 5, and the CLW
C
            IF ((HIRDAT(I,J,154).GE.CHN4SEL).AND.
     >          (HIRDAT(I,J,149).GT.TROPTHR)) PRECPFG2 = 1
            IF ((HIRDAT(I,J,25).GE.CHN5SEL).AND.
     >          (HIRDAT(I,J,149).GT.A5THRES)) PRECPFG2 = 2
C         
            IF ((HIRDAT(I,J,25).GE.CHN5LIM).AND.
     >          (HIRDAT(I,J,149).GT.C5THRES)) PRECPFG2 = 2
         
            IF (HIRDAT(I,J,149).GE.0.) THEN
               HODFBUF(183) = NINT(HIRDAT(I,J,149)*64.0)
            ELSE
               HODFBUF(183) = I2SPV
            END IF
            IF (HIRDAT(I,J,150).GE.0.) THEN
               HODFBUF(184) = NINT(HIRDAT(I,J,150)*64.0)
            ELSE
               HODFBUF(184) = I2SPV
            END IF
            IF(HIRDAT(I,J,151).GE. 0.)THEN
               HODFBUF(185) = NINT(HIRDAT(I,J,151)*64.0)
            ELSE
               HODFBUF(185) = I2SPV
            ENDIF
c           IF(HIRDAT(I,J,152) .GT. 0.95) HIRDAT(I,J,152)=1.0
C HIRDAT(I,J,152) was changed from a rain/no-rain flag to
C Surface Emissivity in 2002.
C
C           HODFBUF(186) = HIRDAT(I,J,152) 
            HODFBUF(186) = NINT(HIRDAT(I,J,152) * 64.0)
            HODFBUF(187) = HIRDAT(I,J,153)
            IF(PRECPFG2.NE.0) THEN
               HODFBUF(188) = I2SPV               
               IF (PRECPFG2.EQ.2) THEN
                  HODFBUF(189) = I2SPV
               ELSE
                  IF (.NOT. ISR4SPV(HIRDAT(I,J,155))) THEN
                     HODFBUF(189) = NINT(HIRDAT(I,J,155)*64.0)
                  ELSE
                     HODFBUF(189) = I2SPV
                  END IF
               END IF
            ELSE                  
               IF (.NOT. ISR4SPV(HIRDAT(I,J,154))) THEN
                  HODFBUF(188) = NINT(HIRDAT(I,J,154)*64.0)
               ELSE
                  HODFBUF(188) = I2SPV
               END IF
               IF (.NOT. ISR4SPV(HIRDAT(I,J,155))) THEN
                  HODFBUF(189) = NINT(HIRDAT(I,J,155)*64.0)
               ELSE
                  HODFBUF(189) = I2SPV
               END IF
            ENDIF
C
C PLACE ALL OF THE CLOUD CLEARED AMSU-A CH4 AND CH5 DATA
C AT OTHER LOCATIONS ON THE HIRS ODF.
C
            IF (.NOT. ISR4SPV(HIRDAT(I,J,154))) THEN
               HODFBUF(197) = NINT(HIRDAT(I,J,154)*64.0)
            ELSE
               HODFBUF(197) = I2SPV
            END IF
            IF (.NOT. ISR4SPV(HIRDAT(I,J,155))) THEN
               HODFBUF(198) = NINT(HIRDAT(I,J,155)*64.0)
            ELSE
               HODFBUF(198) = I2SPV
            END IF 
            HODFBUF(190) = NINT(HIRDAT(I,J,156)*64.0)
            IF(PRECIPFG(I,J)) THEN
               HODFBUF(191) = I2SPV               
               HODFBUF(192) = I2SPV               
            ELSE                  
               IF (.NOT. ISR4SPV(HIRDAT(I,J,24))) THEN
                  HODFBUF(191) = NINT(HIRDAT(I,J,24)*64.0)
               ELSE
                  HODFBUF(191) = I2SPV
               END IF
               IF (.NOT. ISR4SPV(HIRDAT(I,J,25))) THEN
                  HODFBUF(192) = NINT(HIRDAT(I,J,25)*64.0)
               ELSE
                  HODFBUF(192) = I2SPV
               END IF
            ENDIF
            HODFBUF(193) = NINT(HIRDAT(I,J,26)*64.0)
            HODFBUF(194) = NINT(HIRDAT(I,J,27)*64.0)
            IF(PRECIPFG(I,J)) HODFBUF(195) = 1
            IF(.NOT. PRECIPFG(I,J)) HODFBUF(195) = 0
C ------------------------------------------
C REPLACED WITH 3-part PRECPFG2 FLAG UNDER WR #196)
C           IF(PRECPFG2(I,J)) HODFBUF(199) = 1
C           IF(.NOT. PRECPFG2(I,J)) HODFBUF(199) = 0
C ------------------------------------------
ccft        HODFBUF(199) = PRECPFG2
            hodfbuf(199) = j
            hodfbuf(200) = i

            IF(.NOT. ISR4SPV(HIRDAT(I,J,158)))THEN 
               HODFBUF(196) = I2SPV
            ELSE
               HODFBUF(196) = NINT(HIRDAT(I,J,158) * 64.0)
            ENDIF
C           IF(.NOT. ISR4SPV(HIRDAT(I,J,162))) THEN
C              HODFBUF(196) = NINT(HIRDAT(I,J,162))
C           ELSE
C              HODFBUF(196) = I2SPV
C           ENDIF
C
C PLACE THE LATITUDE, LONGITUDE, AND NODE INTO THE OBSERVATION
C RECORD FOR THE LIMB CORRECTION COEFFICIENT CREATION.
C           
            IF(GETLIMB.EQ.1) THEN
               LIMHOBS(5+(23*(I-1))) = NINT(HIRANC(I,J,1)*100.0)
               LIMHOBS(6+(23*(I-1))) = NINT(HIRANC(I,J,2)*100.0)
               LIMHOBS(7+(23*(I-1))) = HODFBUF(152)
            ENDIF
C
C INCREMENT THE RECORD COUNTER & WRITE THE SPOT TO THE HODF.
C THE HODF IS A 2-BYTE FILE, SO USE CRAY2IBM TO CONVERT THE
C 8-BYTE BUFFER TO A 2-BYTE BUFFER WHICH IS WRITTEN.
C
            HODREC = HODREC + 1
            DO K=1, 200
               I2HODF(K) = HODFBUF(K)
            END DO
            WRITE(UNIT=HIDFU,REC=HODREC,IOSTAT=IOERR,ERR=900) I2HODF
            IF(IOERR.NE.0) GO TO 900
C
C INCREMENT COUNTERS AND SET LINE STORAGE VALUES FOR USE IN HEADER.
C
            HIRCNT = HIRCNT + 1
            IF(H1ST) THEN 
               HODF1ST = J
               H1ST = .FALSE.
            ENDIF
            HLAST = J
   40     CONTINUE
 
          TOTHLN = TOTHLN + 1
          AVAUSN(J) = .TRUE.
C
          IF(GETLIMB.EQ.1) THEN
             LIMREC = LIMREC + 1
             WRITE(UNIT=HLMUNT,REC=LIMREC,IOSTAT=LIMERR,ERR=901)
     1             I4LIMHOBS
             IF(LIMERR.NE.0) GO TO 901
          ENDIF
C
C  IF A BAD SCAN LINE WAS FOUND RESET BADLINE AND GETLIMB SO
C  THAT THE NEXT LINE CAN BE PROCESSED IF CORRECT.
C 
          IF(BADLINE .EQ. 1) THEN
            GETLIMB = 1
            BADLINE = 0
          ENDIF
C
   50 CONTINUE
      GO TO 1000
C
  900 KMSG = '* FATAL ERROR * WRITING RECORD        OF HODF FILE'
      WRITE(C6TEMP,'(I6)') HODREC
      KMSG(32:37) =  C6TEMP
      IF(IOERR.NE.0) THEN
         KMSG(39:100) = 'WITH IOSTAT =     '
         WRITE(C4TEMP,'(I4)') IOERR
         KMSG(53:56) = C4TEMP
      ENDIF
      SUBNAM='SPGDVR'
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
C  SINCE SOMETHING WAS WRONG WITH THIS RECORD, DECREMENT THE RECORD
C  NUMBER COUNTER FOR THIS FILE
C
      HODREC = HODREC - 1
      ERSTAT = 99
      GO TO 1000
C
  901 KMSG(1:50)='* FATAL ERROR * WRITING RECORD        OF LIMB HIRS'
      KMSG(51:100)='OBSERVATIONS FILE.'
      WRITE(C6TEMP,'(I6)') LIMREC
      KMSG(32:37) = C6TEMP
      IF(IOERR.NE.0) THEN
         KMSG(68:100) = ' WITH IOSTAT =     '
         WRITE(C4TEMP,'(I4)') IOERR
         KMSG(83:86) = C4TEMP
      ENDIF
      SUBNAM='SPGDVR'
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
C  SINCE SOMETHING WAS WRONG WITH THIS RECORD, DECREMENT THE RECORD
C  NUMBER COUNTER FOR THIS FILE
C
      LIMREC = LIMREC - 1
      ERSTAT = 99
C
 1000 RETURN
      END
