      SUBROUTINE stplot(string, iparse, qini, dset, dset_new)
c		fwj haberl march 1988
c		subroutine for XPLOT
c     args:
c     string	c*	i/r: input string to be parsed
c     iparse	i4	i/r: current position
c     qini      l4      i/r: flag for open plot device
c     dset      i4      i: no of data sets with defined viewport
c     dset_new  i4      i: no of data sets without defined viewport
      INCLUDE 'xplot.inc'
      CHARACTER*(*) string
      INTEGER*4 iparse
      LOGICAL*4 qini
      INTEGER*4 dset, dset_new
      INTEGER*4 icom, iflag, idelim, nret, ierr
      INTEGER*4 ix, iy, divxy(2), idel, nucmd
      INTEGER*4 maxscm
      PARAMETER (maxscm=6)
      INTEGER*4 iset, ipos(2)

      REAL*4 plen1, plen2
      CHARACTER*(10) subcom(maxscm)
      DATA subcom/'device', 'division', 'command', 'delete', 'list',
     &            'data_set'/
      DATA colind/1, 2, 3, 4, 7/
 
      CALL xgtmch(string, iparse, subcom, maxscm,
     &            '''setplot'' subcommand', icom, iflag, idelim, *900)
      IF (iflag.NE.0) THEN
         GOTO 900
      ENDIF
      GOTO (300, 350, 100, 150, 200, 400), icom
      WRITE (*, *)
     &     ' *Unbelievable ERROR*:SETPLT: subcommand index out of range'
      RETURN
C ***** Command
 100  CALL addCMD(string, iparse,nucmd)
      GOTO 900
C ***** DELete
 150  continue
c     by default delete last user set command
      idel = nucmd
      CALL xgtint(string, iparse, 1, 'Command number', 1, idel, nret,
     &            iflag, -1)
      CALL delCMD(idel,nucmd)
      GOTO 900
C ***** List
 200  CALL lisCMD
      GOTO 900
c *****	device handler
 300  CALL xgtstr(string, iparse, 1, 'device', 1, device, nret, iflag,
     &            -1)
      IF (nret.LT.1 .OR. iflag.NE.0) THEN
         CALL xnxtrd('type ? for list of possible devices: ', string,
     &               iparse, ierr, *300)
         GOTO 300
      ENDIF
      qini = .TRUE.
      cmd(1) = 'device '//device
      GOTO 900
c ***** plot division handler
 350  CONTINUE
      plen1 = framex2(divx) - framex1(1)
      plen2 = framey2(divy) - framey1(1)
      CALL xgtint(string, iparse, 2, 'xy division', 1, divxy, nret,
     &            iflag, idelim)
      IF (nret.EQ.1) THEN
         divx = divxy(1)
      ELSEIF (nret.EQ.2) THEN
         divx = divxy(1)
         divy = divxy(2)
      ENDIF
      IF (iflag.NE.0) THEN
         WRITE (*, '(a)') '*ERROR* reading plot divisions'
         GOTO 900
      ENDIF
      IF (divx.GT.xplotmax) THEN
         WRITE (*, '(a,i2)')
     &                 ' Warning: no of x divisions reset to maximum of'
     &                 , xplotmax
         divx = xplotmax
      ENDIF
      IF (divy.GT.yplotmax) THEN
         WRITE (*, '(a,i2)')
     &                 ' Warning: no of y divisions reset to maximum of'
     &                 , yplotmax
         divy = yplotmax
      ENDIF
      plen1 = plen1/divx
      plen2 = plen2/divy
      DO ix = 1, divx
         plenx(ix) = plen1
      ENDDO
      DO iy = 1, divy
         pleny(iy) = plen2
      ENDDO
      IF (divx*divy.GT.1) qabc = .TRUE.
      CALL frame
      GOTO 900
C ***** data_set
 400  continue
      CALL xgtint(string, iparse, 1, 'data set', 1, iset, nret,
     &            iflag, -1)
      CALL xgtint(string, iparse, 2, 'plot position', 1, ipos, nret,
     &            iflag, -1)
      IF (ipos(1).gt.divx) THEN
         write (*,'(2(a,i2))')' x-viewport ', ipos(1),
     &                        ' exceeds current setting ', divx
         return
      ENDIF
      IF (ipos(2).gt.divy) THEN
         write (*,'(2(a,i2))')' y-viewport ', ipos(2),
     &                        ' exceeds current setting ', divy
         return
      ENDIF
      pos(1, iset) = ipos(1)
      pos(2, iset) = ipos(2)
      GOTO 900
 
 900  RETURN
      END
