      SUBROUTINE resid(string, iparse, dset_new, dset)
c        residuals
c        fwj haberl may 1988
      INCLUDE 'xplot.inc'
      INCLUDE '../inc/writebuf.inc'
      CHARACTER*(*) string
      INTEGER*4 iparse, dset, iset, dset_new, jset, dset_tot
      INTEGER*4 nret, iflag, idelim, i, i1
      DATA iset/1/
 
      dset_tot = dset + dset_new
      iset = dset_tot
      CALL xgtint(string, iparse, 1, 'dataset', 1, iset, nret, iflag,
     &            idelim)
      IF (iflag.LT.0) THEN
         GOTO 900
      ENDIF
      IF (iset.LT.1 .OR. iset.GT.dset_tot) THEN
         WRITE (*, '(a,i3)') ' Data set no ', iset, ' not available'
         GOTO 900
      ELSE
         dset_new = dset_new + 1
         jset = dset_tot + 1
         nbins(jset) = nbine(dset_tot) + 1
         nbine(jset) = nbins(jset) + nbine(iset) - nbins(iset)
         i1 = nbins(iset)
         WRITE (wrtstr, '(2a,i3,a,i4,a)') indent, 'Data set ', jset,
     &           ';', nbine(iset) - nbins(iset) +1, ' bins'
         CALL xwrite(wrtstr, 10)
         DO i = nbins(jset), nbine(jset)
            datax(i) = datax(i1)
            errlx(i) = errlx(i1)
            errux(i) = errux(i1)
            datay(i) = datay(i1) - modely(i1)
            errly(i) = errly(i1) - modely(i1)
            erruy(i) = erruy(i1) - modely(i1)
            i1 = i1 + 1
            modely(i) = 0.
         ENDDO
         ylab(jset) = 'residuals ('//char(92)//'gx'//char(92)//'u2'
     &                     //char(92)//'d'//')'
         xlab(jset) = xlab(iset)
      ENDIF
 900  CONTINUE
      RETURN

      ENTRY chires(string, iparse, dset_new, dset)
c        chi**2 residuals
c        fwj haberl JUL 1993
 
      dset_tot = dset + dset_new
      iset = dset_tot
      CALL xgtint(string, iparse, 1, 'dataset', 1, iset, nret, iflag,
     &            idelim)
      IF (iflag.LT.0) THEN
         GOTO 901
      ENDIF
      IF (iset.LT.1 .OR. iset.GT.dset_tot) THEN
         WRITE (*, '(a,i3)') ' Data set no ', iset, ' not available'
         GOTO 901
      ELSE
         dset_new = dset_new + 1
         jset = dset_tot + 1
         nbins(jset) = nbine(dset_tot) + 1
         nbine(jset) = nbins(jset) + nbine(iset) - nbins(iset)
         i1 = nbins(iset)
         WRITE (wrtstr, '(2a,i3,a,i4,a)') indent, 'Data set ', jset,
     &           ';', nbine(iset) - nbins(iset) +1, ' bins'
         CALL xwrite(wrtstr, 10)
         DO i = nbins(jset), nbine(jset)
            datax(i) = datax(i1)
            errlx(i) = errlx(i1)
            errux(i) = errux(i1)
            datay(i) = 2.*(datay(i1) - modely(i1)) / 
     &                    (erruy(i1) - errly(i1))
            errly(i) = 2.*(errly(i1) - modely(i1)) / 
     &                    (erruy(i1) - errly(i1))
            erruy(i) = 2.*(erruy(i1) - modely(i1)) / 
     &                    (erruy(i1) - errly(i1))
            i1 = i1 + 1
            modely(i) = 0.
         ENDDO
         ylab(jset) = 'residuals ('//char(92)//'gx'//char(92)//
     &                'u2'//char(92)//'d'//')'
         xlab(jset) = xlab(iset)
      ENDIF
 901  CONTINUE
      RETURN
      END
