      SUBROUTINE TMLIMT(STIM,CONFIG,NUMOFI,TIMLIM,ERSTAT)
      IMPLICIT NONE
C*******************************************************************
C
C   NAME: TMLIMT
C
C   LANGUAGE- FORTRAN 77    TYPE- SUBROUTINE      COMPUTER- CRAY     
C
C   VERSION- 1.0 DATE- 04/20/91      PROGRAMMER- JEFF MANNING, SMSRC
C   VERSION- 2.0 DATE- 12/19/94      PROGRAMMER- SURYA JANDHYALA, SMSRC
C     XATOV-89 ** REMOVED USE OF HARD CODED DIMENSIONS.
C   VERSION- 3.0 DATE- 04/16/96      PROGRAMMER- ALEX POZDNYAK (FDC)   
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT AND
C      L*4 WITH LOG.
C  
C   FUNCTION- THIS ROUTINE IS DESIGNED TO CHECK THE INSTRUMENT START
C             TIMES FROM THE 1B DATA SETS FOR AN ALLOWABLE TIME
C             DIFFERENCE.  BECAUSE THE HIRS CAN BE PROCESSED BY ITSELF,
C             A TIME WINDOW OF AN APPROXIMATE ORBITAL LENGTH (110 MIN.)
C             IS CHECKED WHEN THE HIRS IS FOUND TO BE THE EARLIEST
C             HEADER TIME.  THIS ALLOWS PROCESSING TO CONTINUE UNTIL
C             THE OTHER INSTRUMENT DATA IS CAUGHT UP TO, WITHOUT
C             THROWING DATA AWAY.  IF THE HIRS IS NOT THE EARLIEST, A
C             TIME WINDOW OF APPROXIMATELY HALF AN ORBITAL LENGTH (55
C             MIN.) IS CHECKED.  ERROR HANDLING IS CONTAINED IN THIS
C             ROUTINE TO HANDLE INSTRUMENT SHUTDOWNS IF THE TIMES DO
C             NOT FALL WITHIN THE ALLOWABLE TIME DIFFERENCE.  THE
C             CONFIGURATION BITS FOR THOSE INSTRUMENTS TO BE SHUTDOWN
C             ARE ZEROED AND A MESSAGE IS WRITTEN TO THE KIDS FILE.
C
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE        TYPE      I/O     DESCRIPTION
C     --------        ----      ---     -----------
C     STIM             INT       I      INSTRUMENT HEADER START
C                                       TIMES IN CENTURY DAY,
C                                       MILLISECOND OF DAY.
C                                       DIMENSIONED 2 X NUMOFI.
C     CONFIG           INT      I/O     INSTRUMENT CONFIGURATION.
C     NUMOFI           INT       I      NUMBER OF INSTRUMENTS.
C     TIMLIM           INT       I      TIME LIMIT (IN MINUTES)
C                                       FOR ACCEPTABLE INSTRUMENT
C                                       TIME.
C     ERSTAT           INT       O      ERROR STATUS CODE (SET TO
C                                       99 WHEN AN ERROR FATAL TO
C                                       TO THE SYSTEM OCCURS).
C
C   LOCAL PARAMETERS-
C     VARIABLE        TYPE      DESCRIPTION
C     --------        ----      -----------
C     ORBLEN           INT     APPROXIMATE ORBITAL TIME LENGTH IN
C                              MINUTES.
C     JDIFF            INT     SET TO THE DIFFERENCE IN MILLISECONDS.
C     I,J              INT     DO LOOP COUNTER.
C     IHOLD            INT     HOLDS THE MILLISECONDS OF THE LATER TIME
C                              PLUS THE MILLISECONDS IN A DAY.
C     IDIFF            INT     SET TO THE DIFFERENCE IN DAYS.
C     HEARLY           LOG     FLAG DECLARING THE HIRS AS THE EARLIEST
C                              OF THE INSTRUMENT HEADER START TIMES.
C     BADTIM           LOG     FLAG SET TO TRUE IF THE INSTRUMENT TIME
C                              IS NOT WITHIN THE HALF THE ORBITAL TIME
C                              LENGTH.
C     AVAIL            INT     MAINTAINS COUNT OF AVAILABLE INSTRUMENTS.
C
C   SUBPROGRAMS CALLED- TMLIM$
C
C   FUNCTIONS USED- BTEST,IBCLR
C
C*******************************************************************
C
      INTEGER   NUMOFI,STIM(2,NUMOFI), TIMLIM, ORBLEN, JDIFF, AVAIL
      INTEGER   I, J, IHOLD, IDIFF, CONFIG, ERSTAT
      LOGICAL   HEARLY, BADTIM(10)
C
C     INITIALIZE VARIABLES.
C
      HEARLY=.FALSE.
      ORBLEN=2*TIMLIM*60000
      JDIFF=0
      IHOLD=0
      AVAIL=0
      IDIFF=0
      ERSTAT=0
C
      DO 10,I=1,NUMOFI
          BADTIM(I)=.TRUE.
 10    CONTINUE
C
C     CHECK INSTRUMENT BIT FOR THE HIRS TO SEE IF THE HIRS IS
C     AVAILABLE.
C
      IF(BTEST(CONFIG,31)) THEN
C
C     IF THE HIRS IS AVAILABLE THEN SET HIRS AS IF IT IS EARLIEST.
C
        BADTIM(1)= .FALSE.
        HEARLY= .TRUE.
C
C     LOOP THROUGH THE OTHER INSTRUMENTS.
C
        DO 20, I=2,NUMOFI
C
C     CHECK OTHER INSTRUMENT BITS.
C
          IF(BTEST(CONFIG,32-I)) THEN
C
C     IF OTHER INSTRUMENT IS AVAILABLE THEN CHECK TO SEE IF
C     THE HIRS IS THE EARLIEST.
C
            IF(STIM(1,1).LT.STIM(1,I)) THEN
              IDIFF=STIM(1,I) - STIM(1,1)
              IF(IDIFF.EQ.1) THEN
                IHOLD= STIM(2,I) + 86400000
                JDIFF= IHOLD - STIM(2,1)
                IF(JDIFF.LE.ORBLEN) THEN
                  BADTIM(I)=.FALSE.
                END IF
              END IF
C
            ELSE IF (STIM(1,1).EQ.STIM(1,I)) THEN
              IF(STIM(2,1).LE.STIM(2,I)) THEN
                JDIFF= STIM(2,I)-STIM(2,1)
                IF(JDIFF.LE.ORBLEN) THEN
                  BADTIM(I)=.FALSE.
                END IF
              ELSE
                BADTIM(1)=.TRUE.
                HEARLY=.FALSE.
              END IF
C
            ELSE
              BADTIM(1)=.TRUE.
              HEARLY= .FALSE.
            END IF
          END IF
  20    CONTINUE
      END IF
C
      IF((.NOT. HEARLY).OR.(.NOT. BTEST(CONFIG,31))) THEN
C
C     LOOP THROUGH INSTRUMENTS.
C
        DO 40, I=1,NUMOFI
C
            IF(BTEST(CONFIG,32-I)) THEN
C
C     COUNT AVAILABLE INSTRUMENTS.
C
               AVAIL=AVAIL+1
C
C     LOOP THROUGH INSTRUMENTS.
C
               DO 30, J=1,NUMOFI
                   IF(I.EQ.J) GO TO 30
                   IF(BTEST(CONFIG,32-J)) THEN
                     IF(STIM(1,J).LT.STIM(1,I)) THEN
                       IDIFF=STIM(1,I) - STIM(1,J)
                       IF(IDIFF.EQ.1) THEN
                          IHOLD= STIM(2,I) + 86400000
                          JDIFF= IHOLD-STIM(2,J)
                          IF(JDIFF.LE.ORBLEN/2) THEN
                             BADTIM(I)=.FALSE.
                             BADTIM(J)=.FALSE.
                          END IF
                       END IF
C
                     ELSE IF (STIM(1,J).EQ.STIM(1,I)) THEN
                       IF(STIM(2,J).LE.STIM(2,I)) THEN
                          JDIFF= STIM(2,I)-STIM(2,J)
                          IF(JDIFF.LE.ORBLEN/2) THEN
                             BADTIM(I)=.FALSE.
                             BADTIM(J)=.FALSE.
                          END IF
                       END IF
C
                     ELSE IF(STIM(1,I).LT.STIM(1,J)) THEN
                       IDIFF=STIM(1,J) - STIM(1,I)
                       IF(IDIFF.EQ.1) THEN
                          IHOLD= STIM(2,J) + 86400000
                          JDIFF= IHOLD-STIM(2,I)
                          IF(JDIFF.LE.ORBLEN/2) THEN
                             BADTIM(I)=.FALSE.
                             BADTIM(J)=.FALSE.
                          END IF
                       END IF
                     END IF
                   END IF
  30           CONTINUE
            END IF
  40    CONTINUE
      END IF
C
C      LOOP THROUGH INSTRUMENTS.
C
      IF (AVAIL.EQ.1) RETURN
C
      DO 60, I=1,NUMOFI
          IF (BTEST(CONFIG,32-I)) THEN
              IF(BADTIM(I)) THEN
                  CALL TMLIM$(306,STIM,NUMOFI,ERSTAT)
                  CONFIG = IBCLR(CONFIG,32-I)
                  DO 50 J = I, NUMOFI
                      IF(BADTIM(J)) THEN
                          CONFIG = IBCLR(CONFIG,32-J)
                      ENDIF
  50              CONTINUE
                  RETURN
               END IF
          END IF
  60  CONTINUE
      RETURN
      END
C*********************************************************************
      SUBROUTINE TMLIM$(MSGCOD,TIM,NUMOFI,ERSTAT)
C
C*********************************************************************
C   NAME- TMLIM$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 04/10/91   PROGRAMMER- JEFF MANNING, SMSRC
C   VERSION- 2.0    DATE- 12/19/94   PROGRAMMER- SURYA JANDHYALA, SMSRC
C     XATOV-89 ** REMOVED USE OF HARD CODED DIMENSIONS. REMOVED CALLING
C                 ARGUMENT BADTIM.
C   VERSION- 3.0  DATE- 10/07/97   PROGRAMMER- LEAH WILSON (HSTX)
C
C   FILES-
C     DSN                       I/O  UNIT  FUNCTIONAL DESCRIPTION
C     ---                       ---  ----  -----------------------
C     ...KIDS                    O   KUNIT KRAY INTERACTIVE DOCUMENTATION
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE TMLIMT
C   VERSION- 3.0 WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
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     TIM                     INT   I   STARTING HEADER TIME.
C                                       DIMENSIONED 2 X NUMOFI.
C     NUMOFI                  INT   I   NUMBER OF INSTRUMENTS.
C     ERSTAT                  INT   O   STATUS CODE IF AN ERROR
C                                       OCCURS DURING I/O.
C
      INTEGER  MSGCOD,ERSTAT,NUMOFI,TIM(2,NUMOFI)
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     STAT                     INT    STATUS CODE IF AN ERROR
C                                       OCCURS DURING I/O
C     I                        INT    LOOP COUNTER
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C     C8TEMP                  CH*8    HOLDING BUFFER FOR WRITING TO KIDS
C     C10TMP                  CH*10   HOLDING BUFFER FOR WRITING TO KIDS
C
C   FUNCTIONS USED- BTEST
C
C   SUBROUTINES CALLED- KIDS
C
C   EXIT STATES-
C       ERSTAT =  1; INDICATING NO OR A NON-FATAL ERROR WAS DETECTED
C       ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED
C
C*********************************************************************
C
      INTEGER  STAT, I
      CHARACTER*4   C4TEMP
      CHARACTER*8   C8TEMP
      CHARACTER*10  C10TMP
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
C   INITIALIZE VARIABLES
C
      ERSTAT=0
C
C  CHECK FOR VALID MSGCOD VALUES
C
      IF(MSGCOD.EQ.306) THEN
        KMSG(1:50)=' * TIME ERROR IN HEADER ** Header start times do n'
        KMSG(51:100)='ot coincide.                              '
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
        DO 20, I=1,NUMOFI
           IF(.NOT. BTEST(CONFIG,32-I)) GO TO 20
           KMSG='Instrument      starting time is           :        '
           WRITE(C4TEMP,'(I4)') I
           KMSG(12:15) = C4TEMP
           WRITE(C10TMP,'(I10)') TIM(1,I)
           KMSG(34:43) = C10TMP
           WRITE(C8TEMP,'(I8)') TIM(2,I)
           KMSG(45:52) = C8TEMP
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
   20   CONTINUE
C
C  NOT A VALID MSGCODE
C
      ELSE
        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
