      SUBROUTINE tfold(string, iparse, dset, dset_new, qmodel, *)
c		fwj haberl april 1988
c		subroutine for XPLOT to fold data sets
      INCLUDE 'xplot.inc'
      CHARACTER*(*) string
      INTEGER*4 iparse, iflag, idelim, nret, ix, iy, is, id, idum
      INTEGER*4 dset, dset_new, iset(dset_max)
      LOGICAL*4 qmodel
      REAL*4 rdum, T0, fold
      DATA fold/1./
 
      IF (dset.EQ.0) THEN
         WRITE (*, '(a)') ' No data set yet'
         GOTO 900
      ENDIF
      CALL xgtrl4(string, iparse, 1, 'start time', 1, T0, nret, iflag,
     &            idelim)
      IF (iflag.LT.0) THEN
         GOTO 900
      ENDIF
      CALL xgtrl4(string, iparse, 1, 'fold time', 1, fold, nret, iflag,
     &            idelim)
      IF (iflag.LT.0) THEN
         GOTO 900
      ENDIF
      ix = 1
      iy = 1
      CALL xgtint(string, iparse, 1, 'viewport x', 1, ix, nret, iflag,
     &            idelim)
      IF (iflag.LT.0) THEN
         GOTO 900
      ENDIF
      CALL xgtint(string, iparse, 1, 'viewport y', 1, iy, nret, iflag,
     &            idelim)
      IF (iflag.LT.0) THEN
         GOTO 900
      ENDIF
      limitx(1, ix) = 0.
      limitx(2, ix) = 0.
      DO is = 1, dset
         IF (pos(1,is).EQ.ix .AND. pos(2,is).EQ.iy) THEN
            xlab(is) = 'Phase'
            DO id = nbins(is), nbine(is)
               rdum = (datax(id)-T0)/fold
               idum = rdum
               datax(id) = rdum - idum
               rdum = (errlx(id)-T0)/fold
               idum = rdum
               errlx(id) = rdum - idum
               IF (errlx(id).GT.datax(id)) errlx(id) = errlx(id) - 1.
               rdum = (errux(id)-T0)/fold
               idum = rdum
               errux(id) = rdum - idum
               IF (errux(id).LT.datax(id)) errux(id) = errux(id) + 1.
            ENDDO
            CALL fndlim(ix, iy, is, qmodel)
         ENDIF
      ENDDO
      RETURN
 900  CONTINUE
c  error case
      RETURN 1
      END
