C$Procedure INTBDS ( get next approximation interval bounds )

      SUBROUTINE INTBDS ( HANDLE, NUMINT, NUMREC, BEGSEG, TIMFLG,
     .                    INTFIL, IDSC, PSIZ, TIMMIN, TIMMAX, COUNT,
     .                    SCLKD1, SCLKD2, EOFTIM)

      IMPLICIT NONE

C$ Abstract
C
C     INTBDS is a utility subroutine for MAKECK4 programme. It reads a
C     line from approximation intervals input file to define interval
C     of approximation of pointing data. If no more lines are in file
C     then EOFTIM is set to .TRUE. If approximation interval file is
C     not defined in MAKECK4 setup file then time bounds of input
C     pointing file are returned ( TIMMIN&TIMMAX). Current INTBDS
C     version can read intervals from CK Type 3 binary files.
C
C$ Declarations

      INTEGER                 HANDLE
      INTEGER                 NUMINT
      INTEGER                 NUMREC
      INTEGER                 BEGSEG
      LOGICAL                 TIMFLG
      CHARACTER*(*)           INTFIL
      INTEGER                 IDSC
      INTEGER                 PSIZ
      DOUBLE PRECISION        TIMMIN
      DOUBLE PRECISION        TIMMAX
      INTEGER                 COUNT
      DOUBLE PRECISION        SCLKD1
      DOUBLE PRECISION        SCLKD2
      LOGICAL                 EOFTIM

C$ Copyright
C
C      Copyright (1997-1998), Jet Propulsion Laboratory
C
C$ Brief_I/O
C
C     Variable  I/O  Description
C     --------  ---  --------------------------------------------------
C     HANDLE     I   Handle for input DAF file containing pointing data
C                    and time interval bounds (for CK3 type only)
C     NUMINT     I   Number of approximation intervals in
C                    the input file (for CK3 type only)
C     NUMREC     I   Number of pointing records in the input file
C                    (for CK3 type only)
C     BEGSEG     I   Begin of the current segment in the input file
C                    (for CK3 type only)
C     TIMFLG     I   .TRUE. if TIME_INTERVALS_FILE is defined
C     INTFIL     I   Approximation intervals file name
C     IDSC       I   Spacecraft (platform) NAIF ID
C     PSIZ       I   Packet size ( number of values ) in Input 
C                    data file
C     TIMMIN     I   Start time of segment (file) pointing
C     TIMMAX     I   End time of segment (file) pointing
C     COUNT     I/O  Counter of approximation intervals
C                    in current segment ( for CK3 only )
C     SCLKD1     O   Start time of the next approximation interval
C     SCLKD2     O   End time of the next approximation interval
C     EOFTIM     O   End of input file flag
C
C$ Detailed_Input
C
C     HANDLE    Handle for input DAF file containing pointing data
C               and time interval bounds (for CK3 type only)
C
C     NUMINT    Number of approximation intervals in
C               the input file (for CK3 type only)
C
C     NUMREC    Number of pointing records in the input file
C               (for CK3 type only)
C
C     BEGSEG    Begin of the current segment in the input file
C               (for CK3 type only)
C
C     TIMFLG    This flag determines if approximation intervals are
C               defined in setup file of MAKECK4 programme
C               ( .TRUE. - if TIME_INTERVALS_FILE parameter present
C               .FALSE.- if TIME_INTERVALS_FILE parameter not present )
C
C     INTFIL    Approximation interval file name. This name is obtained
C               from TIME_INTERVALS_FILE parameter
C
C     IDSC      Spacecraft (platform) NAIF ID code which is used for
C               UTC time to on-board time transformation
C
C     PSIZ      Packet size ( number of values ) in Input data file
C
C     TIMMIN    Start time of segment (file) pointing
C
C     TIMMAX    End time of segment (file) pointing
C
C$ Detailed_Output
C
C     COUNT     Counter of approximation intervals
C               in current segment ( for CK3 only )
C
C     SCLKD1    Start time of the next approximation interval
C
C     SCLKD2    End time of the next approximation interval
C
C     EOFTIM    End of input file flag. This parameter returns
C               .TRUE. if no more lines there are in the input file
C
C$ Author_and_Institution
C
C       Y.K. Zaiko      (BERC)
C
C$ Version
C
C-    MAKECK4 Version 1.0.0, 1998-09-02 (YKZ)
C
C-&

C
C     Local parameters
C
      INTEGER                 BUFSIZ
      PARAMETER             ( BUFSIZ = 2   )

      INTEGER                 DIRSIZ
      PARAMETER             ( DIRSIZ = 100 )
C
C     Time bound lenght
C
      INTEGER                 TIMLEN
      PARAMETER             ( TIMLEN = 30 )

C
C     Input string lenght
C
      INTEGER                 STRLEN
      PARAMETER             ( STRLEN = 61 )

C
C     Delimiter character for input values
C
      CHARACTER*1             DELIM
      PARAMETER             ( DELIM = ' ' )

C
C     Maximum number of values in input string
C
      INTEGER                 NTIUTC
      PARAMETER             ( NTIUTC = 2  )


      DOUBLE PRECISION        TINET1
      DOUBLE PRECISION        TINET2

      INTEGER                 N
      INTEGER                 NRDIR
      INTEGER                 BEGLOC
      INTEGER                 BEGLC1
      DOUBLE PRECISION        BUFFER  ( BUFSIZ )

      CHARACTER*(STRLEN)      TIMSTR
      CHARACTER*(TIMLEN)      TIUTC ( NTIUTC )
      
C
C     These must be saved.
C

      SAVE                    NRDIR
      SAVE                    BEGLOC



      CALL CHKIN ( 'INTBDS' )

C
C     If Time_intervals_file is presented
C
      IF ( TIMFLG ) THEN

C
C        Read line from approximation intervals file
C
         CALL RDNBL ( INTFIL, TIMSTR, EOFTIM )

C
C        Extract times from input line
C
         IF ( .NOT. EOFTIM ) THEN

            CALL LPARSE ( TIMSTR, DELIM, NTIUTC, N, TIUTC )

            IF ( N .NE. NTIUTC ) THEN
               CALL SETMSG ( 'Input string in TIME_INTERVALS_FILE '//
     .                 '"#" does not contain all necessary values'  )
               CALL ERRCH  ( '#', TIMSTR                            )
               CALL SIGERR ( 'SPICE(INCOMPLETEINPUT)'               )
            END IF

C
C           Transform UTC times from input file to on-board clock times
C
            CALL UTC2ET ( TIUTC( 1 ), TINET1 )
            CALL UTC2ET ( TIUTC( 2 ), TINET2 )
            CALL SCE2T ( IDSC, TINET1, SCLKD1 )
            CALL SCE2T ( IDSC, TINET2, SCLKD2 )

         END IF

C
C     If Time_intervals_file not presented and input DAF-file
C     is CK3-type.
C
      ELSE

C
C        Check if number of intervals for current INTBDS call doesn't
C        exceed numbers of intervals in DAF file.
C
         IF ( COUNT .LE. NUMINT ) THEN

C
C           Count address of record BEGLOC, which contains start time
C           of the first segment. It has to be done only for the first
C           call of INTBDS for a new segment.
C
            IF ( COUNT .EQ. 1 ) THEN

               NRDIR = ( NUMREC - 1 ) / DIRSIZ
               BEGLOC = BEGSEG + ( PSIZ + 1 ) * NUMREC + NRDIR

            END IF

C
C           Special processing for a case then segment includes only
C           one interval.
C
            IF ( NUMINT .EQ. 1 ) THEN

               SCLKD1 = TIMMIN
               SCLKD2 = TIMMAX

C
C           It number of current interval strictly less then number
C           of intervals in current segment, we read start time of
C           current interval and start time of the next interval,
C           which is equal to end time of current interval.
C           (look CK Required Reading)
C
            ELSE IF ( COUNT .LT. NUMINT ) THEN

               BEGLC1 = BEGLOC + COUNT

               CALL DAFRDA ( HANDLE, BEGLC1 - 1, BEGLC1, BUFFER )

               SCLKD1 = BUFFER( 1 )
C
C              We have to subtract 1 tick from the beginning of the 
C              next interval to "estimate" the end of current interval 
C              (due to .LE. comparison in the main program we need to
C              prevent the first point of the next interval, if it's 
C              exactly at the start of the next interval, to be taken
C              into account when constract polinomials for the current 
C              interval.)
C
               SCLKD2 = BUFFER( 2 ) - 1.D0

C
C           Check if current interval is a last one in segment.
C
            ELSE IF ( COUNT .EQ. NUMINT ) THEN

               SCLKD1 = SCLKD2
               SCLKD2 = TIMMAX

            END IF


C
C        Branch for a case then number of intervals in current segment
C        exceeds maximum reserved number of intervals in a segment.
C
         ELSE

            EOFTIM = .TRUE.

         END IF

C
C        Increment current interval number ( for next INTBDS call )
C
         COUNT = COUNT + 1
         
C
C     End If ( TIMFLAG )
C
      END IF

      CALL CHKOUT ( 'INTBDS' )
      RETURN
      END
