      SUBROUTINE phatim(phafile, sttime, endtime, qjd)
c **	subroutine to get start- and endtime from spectral buffer
c	phafile	c*	i: The PHA file to be used
c	sttime	r4	r: start time of accumulation in spectral buffer
c	endtime	r4	r: end time of accumulation in spectral buffer
c       qjd     l       i: flag for conversion into julian date
      INCLUDE '../inc/phasf.inc'
      INCLUDE '../inc/selectorsf.inc'
      CHARACTER*(*) phafile
      REAL*8 sttime, endtime, jdate
      LOGICAL qjd, qdone
      INTEGER*4 iparse, iflag, inunit, ierrsf, index, length, infoar(4)
      INTEGER*4 lenact, nhist, nsubs, i
      CHARACTER*80 header
      CHARACTER*1 tmplte
      RECORD /source_info/sinfo
      RECORD /selectors/sel
      RECORD /phase_selectors/phsel
 
      CALL getlun(inunit)
      CALL xtend(phafile, '.pha')
      ierrsf = 0
      CALL opnrsf(phafile, inunit, 'XSPEC data', header, tmplte, nhist,
     &            ierrsf)
      IF (ierrsf.NE.0) THEN
         WRITE (*, '(a,i4,2a)') ' ERROR ', ierrsf, ' opening ',
     &                          phafile(1:lenact(phafile))
         STOP
      ENDIF
      index = 0
      length = len_source_info
      CALL nxpksf(inunit, 'source info', index, nsubs, infoar, sinfo,
     &            length, .TRUE., ierrsf)
      IF (sinfo.start.gt.0.) THEN
         IF (qjd) THEN
            sttime = jdate(sinfo.start)
            endtime = jdate(sinfo.stop)
         ELSE
            sttime = sinfo.start
            endtime = sinfo.stop
         ENDIF
      ELSE
         WRITE (*, *) ' no times in PHA file, use times from previous'
      ENDIF
      CLOSE (inunit)
      CALL frelun(inunit)
      RETURN
 
      ENTRY phaphas(phafile, sttime, endtime)
c     entry point to get start- and endphase from selectors package in
c     spectral buffer
 
      CALL getlun(inunit)
      CALL xtend(phafile, '.pha')
      ierrsf = 0
      CALL opnrsf(phafile, inunit, 'XSPEC data', header, tmplte, nhist,
     &            ierrsf)
      IF (ierrsf.NE.0) THEN
         WRITE (*, '(a,i4,2a)') ' ERROR ', ierrsf, ' opening ',
     &                          phafile(1:lenact(phafile))
         STOP
      ENDIF
      index = 0
      length = len_selectors
      qdone = .FALSE.
      DO WHILE (.NOT.qdone)
         CALL nxpksf(inunit, 'selectors', index, nsubs, infoar, sel,
     &               length, .TRUE., ierrsf)
         IF (ierrsf.NE.0) THEN
            qdone = .TRUE.
            sttime = 0.
            endtime = 1.
         ELSE
            IF (sel.type.EQ.'phase') THEN
               qdone = .TRUE.
               DO i = 1, nsubs
                  length = len_phase_sel
                  CALL rsubsf(inunit, phsel, length, ierrsf)
               ENDDO
               sttime = phsel.PHMIN
               endtime = phsel.PHMAX
            ENDIF
         ENDIF
      ENDDO
      CLOSE (inunit)
      CALL frelun(inunit)
      RETURN
      END
