      SUBROUTINE FRMHIR (HIRDAT,HIRANC,HIRFRMREC,HIRCNT,HODF1ST,HLAST,
     1                   ERSTAT)
c
C**********************************************************************
C
C   NAME- FRMHIR
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 06/06/91   PROGRAMMER- DAVE MOREL  (SMSRC)
C   VERSION- 2.0    DATE- 11/07/94   PROGRAMMER-TERESA SIMMS (SMSRC)
C            XATOV-65   REPLACED FAU PACK ROUTINES WITH SPGFAU.
C            CHANGED COMPUTATION OF MILTIM AND ADDED CHECK FOR EXTENDING
C            INTO NEXT DAY.  REPLACED HARD-CODING WITH PARAMETERS.
C            ADDED INCLUDE /INSPARM/ AND REMOVED 4 CALLING ARGUMENTS.
C   VERSION- 2.1    DATE- 02/01/96   PROGRAMMER-DAVE MOREL (SMSRC)
C      ATOV-334    CHANGED THE CYCLEN TO 308 FROM 300. ALSO ADDED A COMMENT TO
C      THE DOC BLOCK.
C   VERSION- 3.0    DATE- 06/14/96   PROGRAMMER- LEAH WILSON (HSTX)
C                  MODIFICATIONS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 4.0    DATE- 08/29/97   PROGRAMMER- LEAH WILSON (HSTX)
C                  REPLACE IRDB WITH HODF.  REMOVE FRMHI$.
C   VERSION- 4.1    DATE- 02/20/98   PROGRAMMER- LEAH WILSON (R-STX)
C                  ADD HIRFRMREC,HIRCNT,HODF1ST,HLAST.
C   VERSION- 4.2    DATE- 02/26/99   PROGRAMMER- LEAH WILSON (RAYTHEON)
C                  ADD 4-DIGIT YEAR.
C   VERSION- 4.3    DATE- 11/14/01   PROGRAMMER- RICKEY ROLLINS (CSC)
C                  PORTED TO IBM SP
C   VERSION- 1.2.   DATE-02/13/02    PROGRAMMER- R. ROLLINS (CSC)
C        CHANGED I4SPV to I2SPV                                  
C        CORRECTED HODFBUF to I*2(200)                           
C
C   FUNCTION- THIS SUBROUTINE CREATES AN HODF FRAME HEADER FOR THE
C        HIRS INSTRUMENT, AND WRITES THE HEADER TO THE HIRS ORBITAL
C        DATA FILE (HODF).
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O  UNIT   FUNCTIONAL DESCRIPTION
C     ------------------------ ---  ----   ----------------------
C     .......HODF               O   HIDFU  HIRS INSTRUMENTAL DATA FILE.
C     .......KIDS               O   KUNIT  KIDS MESSAGE FILE.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     HIRDAT            REAL  I   ARRAY OF DIMENSION (HNSPOT,HTOTLN,
C                                 HDATEL) WHICH HOLDS ONE DATA FRAME
C                                 OF 256 SECONDS OF HIRS SPOTS
C                                 IN 20 CHANNELS AND 5 CHANNELS OF
C                                 AVHRR(GAC) AS BRIGHTNESS TEMPERATURES.
C     HIRANC            REAL I/O  ARRAY OF DIMENSION (HNSPOT,HTOTLN,
C                                 HNANC) WHICH HOLDS THE ANCILLARY
C                                 INFORMATION FROM THE HIRS INSTR.
C     HIRFRMREC         INT   I   HODF FRAME RECORD NUMBER.
C     HIRCNT            INT   I   NUMBER OF HIRS DATA RECORDS.
C     HODF1ST           INT   I   FIRST HODF DATA LINE (OF HTOTLN).
C     HLAST             INT   I   LAST HODF DATA LINE (OF HTOTLN).
C     ERSTAT            INT   O   ERROR STATUS CODE.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'MONSTA'
      INCLUDE 'INSPARM'
      INCLUDE 'FRMINF'
      INCLUDE 'INPARM'
      INCLUDE 'COLLOC'
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     FIRST         LOGICAL   SET TRUE THE FIRST TIME ONLY.
C     STATUS            INT   ERROR STATUS CODE RETURNED FROM A FMU.
C     DUMMY             INT   A VECTOR OF TWO USED AS A PLACE HOLDER.
C     LAT              REAL   A VECTOR OF 4 INDICATING THE LATITUDE OF
C                             FIRST AND LAST SPOTS OF THE FIRST AND
C                             LAST ACTIVE SCAN LINES.
C     LON              REAL   A VECTOR OF 4 INDICATING THE LONGITUDE
C                             CORRESPONDING TO THE SAME 4 SPOTS AT LAT.
C     DTYPE            CH*4   DATA TYPE OF THE DATA FRAME (I.E.HIRS).
C     CYCLEN            INT   CYCLE LENGTH OF THE DATA CYCLES(I.E.300
C                             FOR HIRS).
C     SYNCTI            INT   VECTOR OF 3 CONTAINING THE SYNC TIME OF
C                             THE FRAME.
C     ENDTIM            INT   VECTOR OF 3 CONTAINING THE ENDING TIME
C                             OF THE FRAME.
C     ORB               INT   VECTOR OF 2 CONTAINING THE STARTING
C                             AND ENDING ORBIT NUMBERS OF THE HIRS.
C     I,J,K             INT   LOOP CONTROL INDEX.
C     N                 INT   NUMBER OF OVERLAP SCAN LINES IN THE
C                             HIRANC BUFFER.
C     MILTIM            INT   TIME IN MILLISECONDS.
C     CYCID            CH*2   CYCLE (SPOT) ID IN IRDB.
C     TOTCHN            INT   TOTAL NUMBER OF HIRS, AMSU-A, AND AMSU-B
C                             CHANNELS.
C     C6TEMP           CH*6   TEMPORARY BUFFER FOR WRITING TO KIDS.
C     KMSG            CH*100  KIDS MESSAGE BUFFER.
C     HODFBUF          INT    HODF RECORD BUFFER.
C     C2HODF           CH*1   CHARACTER HODF RECORD BUFFER.
C
C   SUBPROGRAMS CALLED- ODBTIM, KIDS
C
C   EXIT STATES-
C      ERSTAT = 99 : ERROR ENCOUNTERED IN ACCESSING HODF FILE.
C
C*********************************************************************
C
      INTEGER ERSTAT, HIRFRMREC, IOERR, HODF1ST, HLAST
      REAL  HIRANC(HNSPOT,HTOTLN,HNANC)
      REAL  HIRDAT(HNSPOT,HTOTLN,HDATEL)
C
      CHARACTER*1 C2HODF(400)
      CHARACTER*4 DTYPE, C4TEMP
      CHARACTER*6 C6TEMP
      CHARACTER*100 KMSG
      INTEGER  CYCLEN, I, J, K, SYNCTI(3), ENDTIM(3)
      INTEGER  STATUS, DUMMY(2), N, MILTIM, ORB(2), TOTCHN
C     INTEGER  HODFBUF(200), IDX, HIRCNT
      INTEGER  IDX, HIRCNT
      INTEGER*2  HODFBUF(200)
      REAL     LAT(4), LON(4)
      LOGICAL  FIRST
C
      INCLUDE 'SPVAL'
C
C INITIALIZE LOCAL VARIABLES FOR THE HIRS ODF PARMS.
C
      IOERR=0
      FIRST = .TRUE.
      ORB(1) = SORB(1)
      ORB(2) = EORB(1)
      N = HTOTBF/2
      TOTCHN = HNCHAN + ANCHAN + BNCHAN
      DO IDX=1,200
         HODFBUF(IDX)=I2SPV
      END DO  
C
C  NOW WRITE THE INFORMATION TO THE HODF BUFFER.
C
      HODFBUF(1) = 1
C
C SET UP THE STARTING AND ENDING ORBIT NUMBERS.
C
      HODFBUF(2) = ORB(1)
      HODFBUF(3) = ORB(2)
C
C SET UP THE FRAME NUMBER FROM THE BEGINNING OF THE 1B DATA.
C
      HODFBUF(4) = FRAMNO
C
C SET UP TOTAL NUMBER OF HIRS DATA RECORDS TO FOLLOW.
C
      HODFBUF(5) = HIRCNT
C
C CONVERT THE DATE/TIME FROM CENDAY/MILLISECOND TIME FOR THE FIRST
C HODF DATA RECORD.
C
      CALL ODBTIM(HSCTIM(1,HODF1ST),HSCTIM(2,HODF1ST),SYNCTI)
C
C SET UP THE SYNC TIME OF THE FRAME YEAR/MONTH, DAY/HOUR, MIN/SEC.
C
      HODFBUF(6) = SYNCTI(1)
      HODFBUF(7) = SYNCTI(2)
      HODFBUF(8) = SYNCTI(3)
      IF(HODFBUF(6)/100.EQ.99) THEN
         HODFBUF(21) = 1999
      ELSE
         HODFBUF(21) = HODFBUF(6)/100 + 2000
      ENDIF
C
C COMPUTE AND CONVERT THE OBSERVATION TIME OF THE LAST DATA CYCLE.
C
C
C COMPUTE AND CONVERT THE OBSERVATION TIME OF THE LAST DATA CYCLE.
C THIS IS DONE BY TAKING THE SCAN LINE TIME FROM THE COMMON
C FRMINF:HSCTIM PLUS THE DIFFERENCE IN TIME TO THE LAST SPOT
C OF THE LAST ACTIVE SCAN LINE.
C
      MILTIM=HSCTIM(2,HLAST)+HIRTIM(1)+(HNSPOT-1)*HIRTIM(2)+HIRTIM(4)
C
      IF (MILTIM .GT. 86400000) THEN
         MILTIM=MILTIM - 86400000
         CALL ODBTIM(HSCTIM(1,HLAST)+1,MILTIM,ENDTIM)
      ELSE
         CALL ODBTIM(HSCTIM(1,HLAST),MILTIM,ENDTIM)
      ENDIF
C
C SET UP THE END_TIME OF THE LAST OBSERVED DATA CYCLE YYMM, DDHH, MMSS.
C
      HODFBUF(9) = ENDTIM(1)
      HODFBUF(10) = ENDTIM(2)
      HODFBUF(11) = ENDTIM(3)
      IF(HODFBUF(9)/100.EQ.99) THEN
         HODFBUF(22) = 1999
      ELSE
         HODFBUF(22) = HODFBUF(9)/100 + 2000
      ENDIF
C
C SET UP THE DATA FRAME LATITUDE VERTICES (FIRST SPOT AND LAST SPOT OF
C THE FIRST ACTIVE SCAN LINE AND THE LAST SPOT AND FIRST SPOT OF THE
C LAST ACTIVE SCAN LINE. THE ORDER OF THE VERTICES IS IN A COUNTER
C CLOCKWISE DIRECTION.
C
      LAT(1) = HIRANC(1,HODF1ST,1)
      LAT(2) = HIRANC(HNSPOT,HODF1ST,1)
      LAT(3) = HIRANC(HNSPOT,HLAST,1)
      LAT(4) = HIRANC(1,HLAST,1)
      HODFBUF(12) = LAT(1) * 128.0
      HODFBUF(13) = LAT(2) * 128.0
      HODFBUF(14) = LAT(3) * 128.0
      HODFBUF(15) = LAT(4) * 128.0
C
C SET UP THE DATA FRAME LONGITUDE VERTICES.
C
      LON(1) = HIRANC(1,HODF1ST,2)
      LON(2) = HIRANC(HNSPOT,HODF1ST,2)
      LON(3) = HIRANC(HNSPOT,HLAST,2)
      LON(4) = HIRANC(1,HLAST,2)
      HODFBUF(16) = LON(1) * 128.0
      HODFBUF(17) = LON(2) * 128.0
      HODFBUF(18) = LON(3) * 128.0
      HODFBUF(19) = LON(4) * 128.0
C
C SET UP THE OPERATIONAL FLAG - 1 = SUCCESSFULLY PROCESSED.
C
      HODFBUF(20) = 1
C
C WRITE THE FRAME HEADER TO THE HODF.
C THE HODF IS A 2-BYTE FILE, SO CONVERT THE ENTIRE 8-BYTE HODFBUF
C TO A 2-BYTE BUFFER VIA CRAY2IBM AND WRITE THAT.
C
C     IERR=CRAY2IBM(7,200,C2HODF,0,HODFBUF(1))
C     WRITE(UNIT=HIDFU,REC=HIRFRMREC,IOSTAT=IOERR,ERR=900) C2HODF 
      WRITE(UNIT=HIDFU,REC=HIRFRMREC,IOSTAT=IOERR,ERR=900) HODFBUF 
      IF(IOERR.NE.0) GO TO 900
      GO TO 1000
C
900   KMSG = '* FATAL ERROR * Writing Record        of HODF FILE'
      WRITE(C6TEMP,'(I6)') HIRFRMREC
      KMSG(32:37) =  C6TEMP
      SUBNAM='SPGDVR'
      IF(IOERR.NE.0) THEN
         KMSG(39:100) = 'with IOSTAT =     '
         WRITE(C4TEMP,'(I4)') IOERR
         KMSG(53:56) = C4TEMP
      ENDIF
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ERSTAT = 99
C
 1000 RETURN
      END
