      SUBROUTINE ufspec(string, iparse, dset, dset_new)
c        to 'unfold' PHA spectrum using photon model spectrum.
c        fwj haberl   15-APR-1989
      INCLUDE 'xplot.inc'
      INCLUDE '../inc/phasf.inc'
      INCLUDE '../inc/historysf.inc'
      INTEGER*4 dset, dset_new
      CHARACTER*(*) string
      INTEGER*4 iparse, nret, iflag, idelim, i, i1, i2
      INTEGER*4 nset, iset, iset_pha, iset_phot, nfe, ne
      integer*4 maxchan
      parameter (maxchan=4096)
      INTEGER*4 start(maxchan), end(maxchan)
      REAL*4 z, fstart(maxchan), fend(maxchan)
      REAL*4 delta, corr, efar(0:maxchan), ear(0:maxchan)
 
      nset = dset+dset_new
      CALL xgtint(string, iparse, 1, 'data set PHA', 1, iset_pha, nret,
     &            iflag, idelim)
      IF (iset_pha.GT.nset .OR. iset_pha.LT.1) THEN
         WRITE (*, *) ' No such data set: ',iset_pha
         GOTO 999
      ENDIF
      CALL xgtint(string, iparse, 1, 'data set PHA', 1, iset_phot, nret,
     &            iflag, idelim)
      IF (iset_phot.GT.nset .OR. iset_phot.LT.1) THEN
         WRITE (*, *) ' No such data set: ',iset_phot
         GOTO 999
      ENDIF
c ***** rebin photon model spectrum
      nfe = nbine(iset_phot) - nbins(iset_phot) + 1
      ne = nbine(iset_pha) - nbins(iset_pha) + 1
      if(nfe.gt.maxchan)then
         write(*,*)' UFSPEC ERROR: maximum of ',maxchan,
     &                  ' energies exceeded'
         GOTO 999
      ENDIF
      IF (nfe.gt.maxchan)then
         write(*,*)' UFSPEC ERROR: maximum of ',maxchan,
     &                  ' channels exceeded'
         GOTO 999
      ENDIF
      iset = nset + 1
      xlab(iset) = xlab(iset_phot)
      ylab(iset) = ylab(iset_phot)
      nbins(iset) = nbine(nset) + 1
      nbine(iset) = nbine(nset) + ne
      i1 = nbins(iset_phot)
      efar(0) = errlx(i1)
      DO i = 1, nfe
         efar(i) = errux(i1)
         i1 = i1 + 1
      ENDDO
      i1 = nbins(iset_pha)
      ear(0) = errlx(i1)
      DO i = 1, ne
         ear(i) = errux(i1)
         i1 = i1 + 1
      ENDDO
      z = 0.
      i1 = nbins(iset)
      i2 = nbins(iset_phot)
      DO i = 1, nfe
         delta = efar(i) - efar(i-1)
         modely(i1) = modely(i2)*delta
         i1 = i1 + 1
         i2 = i2 + 1
      ENDDO
      CALL inibin(nfe, efar(0), ne, ear(0), start, end, fstart, fend, z)
      CALL erebin(nfe, modely(nbins(iset)), ne, start, end, fstart,
     &            fend, modely(nbins(iset)))
      i1 = nbins(iset)
      i2 = nbins(iset_pha)
      DO i = 1, ne
         delta = ear(i) - ear(i-1)
         modely(i1) = modely(i1)/delta
         corr = datay(i2)/modely(i2)
         datay(i1) = modely(i1)*corr
         corr = errly(i2)/modely(i2)
         errly(i1) = modely(i1)*corr
         corr = erruy(i2)/modely(i2)
         erruy(i1) = modely(i1)*corr
         errlx(i1) = errlx(i2)
         errux(i1) = errux(i2)
         datax(i1) = datax(i2)
         i1 = i1 + 1
         i2 = i2 + 1
      ENDDO
      dset_new = dset_new + 1
 999  CONTINUE
      RETURN
      END
