      SUBROUTINE CHKHDR(NUMOFI,STIM,DSN,DTYPE,SCID,ERSTAT)
      IMPLICIT NONE
C
C*********************************************************************
C
C   NAME- CHKHDR
C
C   LANGUAGE- FORTRAN 77    TYPE- SUBROUTINE      COMPUTER- CRAY
C
C   VERSION- 1.0    DATE- 01/11/91   PROGRAMMER- JEFF MANNING (SMSRC)
C   VERSION- 2.0    DATE- 02/05/95   PROGRAMMER- ALEX POZDNYAK(SMSRC)
C      XATOV-97:   "STIM" CHANGED FROM DIMENSION (NUMOFI X 2) TO
C                  DIMENSION (2 X NUMOFI). LOCAL VARIABLES "BIT"
C                  AND "MOVE" WERE REMOVED. BEFORE CALLS TO 'CHKAGE'
C                  AND 'CHKTYP', "IF" BLOCK WAS ADDED. ARGUMENT "INSTR"
C                  WAS REMOVED FROM CALL TO 'CHKAGE' AND ARGUMENT "STIM"
C                  CHANGED TO "STIM(I,INSTR)".
C   VERSION- 2.1    DATE- 05/18/95   PROGRAMMER- ALEX POZDNYAK(SMSRC)
C      ATOVS-118:  ARGUMENT DSN CHANGED FROM CH*42 TO CH*80.
C                  COMMENT FROM DESCRIPTION OF DTYPE THAT GIVES THE
C                  DATA TYPE CODES FOR HIRS, MSU, AND SSU WAS REMOVED.
C                  DTYPE DIMENSION CHANGED FROM (3X2) TO (3).
C   VERSION- 3.0    DATE- 04/15/96   PROGRAMMER- ALEX POZDNYAK(FDC)  
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT AND
C      L*4 WITH LOG.  
C   VERSION- 3.1    DATE- 12/29/97   PROGRAMMER- LEAH WILSON (HSTX)
C      REMOVE CALL TO CHKORB: DUE TO NEW WAY ORBIT NUMBERS ARE 
C      DETERMINED, NO CHECK IS NEEDED.  ALSO, DO NOT CHECK SPACECRAFT ID.
C   VERSION- 3.2    DATE- 05/19/98   PROGRAMMER- LEAH WILSON (HSTX)
C      FOR AMSU-A ONLY MODE, ALLOW AMSU-A DATE TO BE USED IN START-UP
C      MESSAGE INSTEAD OF DEFAULTING TO HIRS TIME/DATE.
C   VERSION- 3.3    DATE- 08/04/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      FOR WR #100 ADDED COMMONS /INPARM/, /INSPARM/, AND /NOHIRSCF/ TO
C      PICK UP THE NOH1BS FLAG THAT IS SET WHEN NO HIRS 1B* IS AVAILABLE.
C   VERSION- 3.4    DATE- 01/09/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      MADE COSMETIC CHANGES TO USE LEAD ZEROS IN THE DATE/TIME DATA
C      IN THE KIDS MESSAGE.
C   VERSION- 4.0    DATE- 05/12/06   PROGRAMMER- FRANK TILLEY (IMSG)
C      UNDER WR #303 (PIPELINE PROCESSING) REMOVED CALLS TO CHKAGE AND
C      TMLIMT. IF THE INTERNAL 1B STAR NAMES MATCH, ASSUME THAT THE
C      DATA ARE FROM THE SAME ORBIT FOR ALL INSTRUMENTS. REMOVED THE
C      CALL TO GMTIME BECAUSE THE SYSTEM TIME IS ASSUMED TO BE GMT.
C      REMOVED CALLING ARGUMENT "AGELIM".
C
C   FUNCTION- THIS ROUTINE CHECKS THE INSTRUMENT 1B DATA SET HEADER
C             RECORDS FOR CONSISTENCY VALUES. THE HEADER DATA CHECKED
C             INCLUDES: ORBIT NAMES, SATELLITE ID'S, AND DATA TYPES.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE            TYPE      I/O      DEFINITION
C     --------            ----      ---      ----------
C      NUMOFI              INT       I       NUMBER OF INSTRUMENTS.
C      STIM                INT       I       2-D ARRAY OF HEADER START
C                                            TIMES.  TIME IS KEPT IN
C                                            GMT AS CENTURY DAY,
C                                            MILLISECOND OF DAY.
C      DSN               CH*80       I       ARRAY CONTAINING, FOR EACH
C                                            INSTRUMENT, THE 1B DATA
C                                            SOURCE. DIMENSIONED NUMOFI.
C      DTYPE               INT       I       ARRAY CONTAINING, FOR EACH
C                                            INSTRUMENT, THE INSTRUMENT
C                                            DATA TYPE. DIMENSIONED
C                                            NUMOFI.
C      SCID                INT       I       ARRAY CONTAINING, FOR EACH
C                                            INSTRUMENT, THE SPACECRAFT
C                                            ID NUMBERS.
C      ERSTAT              INT       O       ERROR CODE.
C
C   LOCAL PARAMETERS-
C     VARIABLE            TYPE      DEFINITION
C     --------            ----      ----------
C      NINSTR              INT      INSTRUMENT NUMBER.
C      DATIM(2)            INT      SYSTEM TIME: CENTURY DAY,
C                                   MILLISECOND OF DAY.
C      STRTIM(3)           INT      STARTING DATE/TIME OF ORBIT.
C      FIRST               LOG      LOGICAL SET TRUE THE FIRST TIME.
C      START               INT      LOOP INDEX START VARIABLE, USED
C                                   TO BYPASS HIRS CHECKS WHEN NO
C                                    HIRS 1B* IS AVAILABLE. 
C
C   SUBPROGRAMS CALLED- CHKDSN, CHKTYP, ODBTIM, CHKHD$
C
C   INTRINSIC FUNCTIONS USED- BTEST
C
C   EXIT STATES- ERSTAT=99 FOR AN ERROR FROM ONE OF THE CALLED
C                SUBROUTINES $-ROUTINE.
C
C   COMMENTS-  THIS ROUTINE CHECKS THE HEADER. IT WILL OUTPUT MESSAGES
C              TO THE LOG FOR NONFATAL ERRORS AND WILL SET CONFIG
C              TO UNAVAILABLE FOR THE PARTICULAR INSTRUMENTS.
C
C*********************************************************************
C
      INTEGER   NUMOFI,ERSTAT
      INTEGER   DTYPE(NUMOFI)
      INTEGER   STIM(2,NUMOFI),SCID(NUMOFI),NINSTR
      INTEGER   DATIM(2),STRTIM(3),START
      LOGICAL   FIRST
      CHARACTER*80 DSN(NUMOFI)
C
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPGUNT'
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'NOHIRSCF'
C
      SAVE FIRST
C
C     INITIALIZE VARIABLES.
C
      FIRST=.TRUE. 
      ERSTAT=0
C
C     THE FIRST TIME PER RUN THAT CHKHDR IS CALLED WE WRITE OUT
C     THE STARTING AND ENDING ORBIT NUMBERS ALONG WITH THE START
C     TIME OF THE FIRST 1B DATA SET.  THIS IS FOR INFORMATION
C     IN THE KIDS SO WE CAN ASSOCIATE ANY PROBLEMS ENCOUNTERED
C     WITH THE PROPER PROCESSING RUN.
C
      IF(FIRST) THEN
         FIRST = .FALSE.
         IF(STIM(1,1).NE.0.AND.STIM(2,1).NE.0) THEN
            CALL ODBTIM(STIM(1,1),STIM(2,1),STRTIM)
            CALL CHKHD$(100,STRTIM,ERSTAT)
         ELSEIF(STIM(1,2).NE.0.AND.STIM(2,2).NE.0) THEN
            CALL ODBTIM(STIM(1,2),STIM(2,2),STRTIM)
            CALL CHKHD$(100,STRTIM,ERSTAT)
         ENDIF
         IF(ERSTAT .EQ. 99) RETURN
      ENDIF
C
C     COMPARE DATA SOURCES.
C
          CALL CHKDSN(DSN,CONFIG,NUMOFI,ERSTAT)
          IF(ERSTAT.EQ.99) THEN
             RETURN
          END IF
C
      START = 1
      IF (NOH1BS) START = 2
      DO NINSTR=START,NUMOFI
C
C     CHECK THE INSTRUMENT CONFIGURATION BIT TO SEE IF THE
C     INSTRUMENT IS USABLE, THEN CHECK THE DATA TYPE.
C
          IF (BTEST(CONFIG,32-NINSTR)) THEN
            CALL CHKTYP(DTYPE,NINSTR,NUMOFI,ERSTAT)
            IF(ERSTAT.EQ.99) THEN
               RETURN
            END IF
          END IF
C
      END DO
C
      RETURN
C
   20 CALL CHKHD$(201,STIM,ERSTAT)
      RETURN
      END
C*********************************************************************
      SUBROUTINE CHKHD$(MSGCOD,STIME,ERSTAT)
C
C*********************************************************************
C   NAME- CHKHD$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 05/11/93    PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 3.0    DATE- 07/08/97    PROGRAMMER- LEAH WILSON (HSTX)
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 CHKHDR.
C   VERSION- 3.0 CONVERT TO KIDS FILE CONVENTION.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE               TYPE  I/O  FUNCTION
C     --------               ----  ---  --------
C     MSGCOD                  INT   I   MESSAGE CODE USED TO
C                                       IDENTIFY THE ERROR PATTERN.
C     STIME(3)                INT   I   STARTING TIME OF THE ORBIT.
C     ERSTAT                  INT   O   99 IF FATAL ERROR, ELSE 0.
C
      INTEGER   MSGCOD,ERSTAT,STIME(3)
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE  'ERPARM'
      INCLUDE  'ERINFO'
      INCLUDE  'SPGUNT'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     C5TEMP                  CH*5    HOLDING BUFFER FOR WRITING TO KIDS FILE.
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C
C   SUBPROGRAMS CALLED- KIDS
C
C   EXIT STATES-
C      ERSTAT = 0 ;INDICATING NO ERRORS FOUND, OR NON-FATAL ERROR FOUND .
C      ERSTAT = 99;INDICATING A FATAL ERROR WAS DETECTED.
C
C   ASSUMPTIONS- THIS ROUTINE ASSUMES THAT KIDS HAS BEEN CALLED PREVIOUSLY
C                TO OPEN THE KIDS FILE.
C
C*********************************************************************
C
      INTEGER     I
      CHARACTER*4 C4TEMP
      CHARACTER*5 C5TEMP
      CHARACTER*6 NAME
      CHARACTER*100 KMSG
C
C  SET SUBNAM TO MAIN MODULE NAME, SINCE KIDS DETERMINES SUBROUTINE NAME.
C
      SUBNAM = 'SPGDVR'
C
C  INITIALIZE VARIABLES.
C
      ERSTAT=0
C
C  CHECK FOR VALID MSGCOD VALUE.
C
      IF(MSGCOD.EQ.100) THEN
        KMSG='Orbit #      -      at date/time =               ' 
        WRITE(C5TEMP,'(I5)') SORB(1)
        KMSG(9:13) = C5TEMP
        WRITE(C5TEMP,'(I5)') EORB(1)
        KMSG(15:19) = C5TEMP
        WRITE(C4TEMP,'(I4.4)') STIME(1)
        KMSG(36:39) = C4TEMP
        WRITE(C4TEMP,'(I4.4)') STIME(2)
        KMSG(41:44) = C4TEMP
        WRITE(C4TEMP,'(I4.4)') STIME(3)
        KMSG(46:49) = C4TEMP
        print *,KMSG
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0) 
        ERSTAT=0
C
      ELSEIF(MSGCOD.EQ.201) THEN
        KMSG='* FATAL ERROR * Unreasonable value in GMTIME'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0) 
        ERSTAT=99
C
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = 'Invalid message code      '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(22:25) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0) 
        ERSTAT=0
      ENDIF
      RETURN
      END
