      SUBROUTINE CHKAGE(STIM,DATIM,CONFIG,INSTR,AGELIM,ERSTAT)
C*********************************************************************
C
C   NAME- CHKAGE
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY    
C
C   VERSION- 1.0    DATE- 01/10/91   PROGRAMMER- JEFF MANNING (SMSRC)
C   VERSION- 2.0    DATE- 12/22/94   PROGRAMMER- ALEX POZDNYAK(SMSRC)
C       XATOV-90:   STIM CHANGED FROM DIMENSION (NUMOFI X 2) TO
C                   DIMENSION (2). CALLING ARGUMENTS NUMOFI REMOVED.
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      R*4 WITH REAL IN CALLING ARGUMENTS.
C
C   FUNCTION- THIS ROUTINE CHECKS WHETHER THE INSTRUMENT TIME HAS
C             SURPASSED THE ALLOWABLE AGE LIMIT.  IF THE TIME FALLS
C             WITHIN THE AGE LIMIT, THEN THE INSTRUMENT IS
C             CONSIDERED USABLE, OTHERWISE IT IS CONSIDERED BAD
C             AND THE INSTRUMENT CONFIGURATION IS SET TO ZERO
C             FOR THAT INSTRUMENT.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE            TYPE       I/O     DEFINITION
C     --------            ----       ---     ----------
C      STIM(2)             INT        I      INPUT TIMES (CENTURY DAY,
C                                            MILLISECOND OF DAY)
C      DATIM(2)            INT        I      CURRENT SYSTEM TIME (GMT)
C      CONFIG              INT       I/O     INSTRUMENT CONFIGURATION
C                                            BIT PATTERN.  EACH
C                                            INSTRUMENT HAS A
C                                            CORRESPONDING BIT.  IF THE
C                                            BIT IS SET TO ONE, THE
C                                            INSTRUMENT IS AVAILABLE, IF
C                                            THE BIT IS SET TO ZERO, THE
C                                            INSTRUMENT IS UNAVAILABLE.
C      INSTR               INT        I      INSTRUMENT NUMBER
C      AGELIM              INT        I      AGE LIMIT (IN HOURS)
C      ERSTAT              INT        O      ERROR STATUS, SET TO 99
C                                            FOR A WRITE ERROR TO THE
C                                            KIDS FROM THE $-ROUTINE
C
C   LOCAL PARAMETERS-
C
C     VARIABLE            TYPE       DEFINITION
C     --------            ----       ----------
C      DIFF               REAL       DIFFERENCE BETWEEN INSTRUMENT
C                                    TIME MILLISECONDS AND SYSTEM
C                                    TIME MILISECONDS (IN HOURS).
C      TOTLHR             REAL       TOTAL DIFFERENCE BETWEEN SYSTEM
C                                    TIME AND INSTRUMENT TIME (IN HOURS)
C      BSHFT               INT       USED TO SHIFT BITS OF
C                                    CONFIG.
C      IHOLD               INT       INSTR. TIME PLUS THE
C                                    MILLISECONDS IN A DAY.
C      DAYS                INT       TEMPORARY VARIABLE CONTAINING THE
C                                    NUMBER OF DAYS BETWEEN THE
C                                    INSTRUMENT CENTURY DAY AND THE
C                                    SYSTEM CENTURY DAY.
C      HOURS               INT       TEMPORARY VARIABLE CONTAINING THE
C                                    CONVERSION OF DAYS INTO HOURS.
C
C   SUBPROGRAMS CALLED- CHKAG$
C
C   FUNCTIONS USED- IBCLR, REAL
C
C*********************************************************************
C
      INTEGER   STIM(2),DATIM(2),ERSTAT
      INTEGER   BSHFT,CONFIG,DAYS,HOURS,AGELIM,INSTR
C
      REAL      DIFF, TOTLHR
C
      DAYS=0
      HOURS=0
C
C     GET THE DIFFERENCE BETWEEN THE SYSTEM MILLISECONDS AND THE
C     INSTRUMENT MILLISECONDS.
C
      DIFF = REAL(DATIM(2)-STIM(2))/3600000.0
C
C     IF THE INSTRUMENT TIME CENTURY DAY IS GREATER THAN THE SYSTEM
C     TIME CENTURY DAY, AN ERROR IS ENCOUNTERED.  CALL THE $-ROUTINE.
C
      IF (STIM(1).GT.DATIM(1)) THEN
C
          CALL CHKAG$(308,STIM(1),STIM(2),DATIM,ERSTAT)
             BSHFT=64-INSTR
             CONFIG=IBCLR(CONFIG,BSHFT)
C
C     IF CENTURY DAYS ARE EQUAL THEN CHECK THE DIFFERENCE BETWEEN
C     MILLISECONDS.
C
      ELSE IF (STIM(1).EQ.DATIM(1)) THEN
C
C     IF THE DIFFERENCE IS LESS THAN ZERO, THE SYSTEM TIME OCCURS
C     BEFORE THE INSTRUMENT TIME AN ERROR IS ENCOUNTERED, CALL THE
C     $-ROUTINE.
C
          IF(DIFF.LE.0) THEN
             CALL CHKAG$(308,STIM(1),STIM(2),DATIM,ERSTAT)
             BSHFT=64-INSTR
             CONFIG=IBCLR(CONFIG,BSHFT)
C
C     IF THE DIFFERENCE BETWEEN MILLISECONDS IS GREATER THAN THE
C     AGE LIMIT, CALL THE $-ROUTINE.
C
          ELSE IF(DIFF.GT.AGELIM) THEN
             CALL CHKAG$(309,STIM(1),STIM(2),DATIM,ERSTAT)
             BSHFT=32-INSTR
             CONFIG=IBCLR(CONFIG,BSHFT)
          END IF
C
C     IF THE INSTRUMENT CENTURY DAY IS LESS THAN THE SYSTEM TIME
C     CENTURY DAY, COMPUTE THE NUMBER OF DAYS BETWEEN THEM.
C
      ELSE IF (STIM(1).LT.DATIM(1)) THEN
          DAYS = DATIM(1) - STIM(1)
C
C     CONVERT DAYS INTO HOURS.
C
          HOURS = DAYS * 24
C
C    ADD UP THE TOTAL NUMBER OF HOURS.
C
          TOTLHR = HOURS + DIFF
C
C    IF THE TOTAL NUMBER OF HOURS IS GREATER THAN THE AGE LIMIT, THEN
C    CALL THE $-ROUTINE.
C
          IF(TOTLHR.GT.AGELIM) THEN
             CALL CHKAG$(309,STIM(1),STIM(2),DATIM,ERSTAT)
             BSHFT=32-INSTR
             CONFIG=IBCLR(CONFIG,BSHFT)
          END IF
      END IF
      RETURN
      END
C*********************************************************************
      SUBROUTINE CHKAG$(MSGCOD,TIM1,TIM2,SYSTIM,ERSTAT)
C*********************************************************************
C   NAME- CHKAG$
C
C   LANGUAGE- FORTRAN 77      TYPE- SUBROUTINE        COMPUTER- CRAY    
C
C   VERSION- 1.0  DATE- 03/27/91    PROGRAMMER- JEFF MANNING (SMSRC)
C   VERSION- 3.0  DATE- 10/07/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.0  DATE- 04/24/98   PROGRAMMER- LEAH WILSON (RSTX)
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 CHKAGE
C   VERSION- 3.0 WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
C   VERSION- 3.1 ADD CALL TO ODBTIM TO CONVERT CENTURY DAY AND MILLISECS 
C                TO YYMM, DDHH, MMSS.
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     TIM1                    INT   I   HEADER START TIME CENTURY DAY
C     TIM2                    INT   I   HEADER START TIME MILLISECOND
C     SYSTIM(2)               INT   I   CURRENT SYSTEM TIME
C     ERSTAT                  INT   O   STATUS CODE IF AN ERROR
C                                       OCCURS DURING I/O
C
      INTEGER  MSGCOD,ERSTAT,TIM1,TIM2,SYSTIM(2)
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     C1TEMP                  CH*1    HOLDING BUFFER FOR WRITING TO KIDS
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C     TIME                     INT    ARRAY (DIM 3) HOLDING TIME AS YYMM,
C                                     DDHH, MMSS
C
C   SUBROUTINES CALLED- KIDS, ODBTIM
C
C*********************************************************************
C
      INTEGER  STAT,I, TIME(3)
      CHARACTER*1   C1TEMP
      CHARACTER*4   C4TEMP
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
C
C  INITIALIZE VARIABLES
C
      ERSTAT=0
      STAT=0
      CALL ODBTIM(TIM1,TIM2,TIME)
C
C  CHECK FOR VALID MSGCOD VALUES
C
      IF(MSGCOD.EQ.309) THEN
        KMSG(1:50)=' * HEADER AGE LIMIT ERROR * Instr.   exceeds age l'
        KMSG(51:100)='imit :               /      /      .'
        WRITE(C1TEMP,'(I1)') INSTR
        KMSG(36:36) = C1TEMP
        WRITE(C4TEMP,'(I4)') TIME(1)
        KMSG(67:70) = C4TEMP
        WRITE(C4TEMP,'(I4)') TIME(2)
        KMSG(74:77) = C4TEMP
        WRITE(C4TEMP,'(I4)') TIME(3)
        KMSG(81:84) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE IF(MSGCOD.EQ.308) THEN
        KMSG(1:50)=' * HEADER AGE LIMIT ERROR * Instr.   header time b'
        KMSG(51:100)='4 system time :      /      /      .'
        WRITE(C1TEMP,'(I1)') INSTR
        KMSG(36:36) = C1TEMP
        WRITE(C4TEMP,'(I4)') TIME(1)
        KMSG(67:70) = C4TEMP
        WRITE(C4TEMP,'(I4)') TIME(2)
        KMSG(74:77) = C4TEMP
        WRITE(C4TEMP,'(I4)') TIME(3)
        KMSG(81:84) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      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

