      SUBROUTINE XYPLT (XXX, M, N, INCTRL, IOCTRL)
      IMPLICIT NONE
C
C ***      GENERAL X-Y PLOTTING
C
      INTEGER M, N, INCTRL, IOCTRL
      REAL XXX(M,N)
C
      INCLUDE MATLAB$KOM:SIZEPARMS.INC
      INCLUDE MATLAB$KOM:IOP.KOM
      INCLUDE MATLAB$KOM:MATPLT.KOM
      INCLUDE MATLAB$KOM:PLTXY.KOM
C
      REAL X(LINES,VECSIZ), Y(LINES,VECSIZ), XPL(VECSIZ), YPL(VECSIZ),
     .     XLIM, YLIM, XMINH, XMAXH, YMINH, YMAXH, YYY,
     .     SYMNXY, SYMXXY, SYMMXY
      INTEGER IANS, I, J, K, IERR, IOPTNS
      CHARACTER*8 ICURS
      CHARACTER*4 ROWTAG
      COMMON /AREA/ X,Y,XPL,YPL
C
C ***      FUNCTIONS
      REAL GSXLCM, GSYLCM
C
      DATA ROWTAG / ' ROW' /
      DATA ICURS / 'XYPlot>>' /
C
C
C ***      FIND OUT WHO CALLED US
      IF (INCTRL.NE.0) GO TO 800
C
C ***      SET UP THE DEFAULTS
      ISECY = .FALSE.
      CHRSIZ = 0
      ISYOPT = 0
      IAXSPN = 1
      DO 1 I = 1, 80
        XLABXY(I) = ' '
        YLABXY(I) = ' '
        SYLBXY(I) = ' '
        LABXY(I) = ' '
1     CONTINUE
      XLABXY(2) = Z'00'
      YLABXY(2) = Z'00'
      SYLBXY(2) = Z'00'
      LABXY(2) = Z'00'
      IPLTP1 = 0
      IPLTP2 = 0
      IPLTP3 = 0
      NLINES = 0
      DO 2 I = 1, LINES
        LINSYL(I) = 1
        SYMSZ(I) = .2
        INTSYM(I) = 5
        ISYMNO(I) = 0
        FGRPXY(I) = I+1
2     CONTINUE
      XLIM = GSXLCM ()
      YLIM = GSYLCM ()
      XSTXY = 0.
      YSTXY = 0.
      XFRCXY = 100.
      YFRCXY = 95
C
C ***      CHOOSE THE PLOT MODE
      CALL CHKEND
7     CONTINUE
      CALL DSSTRN (' PLEASE CHOOSE THE X-Y PLOT MODE:', 1)
      CALL DSSTRN (' [1] IMPLICIT X, EXPLICIT Y', 1)
      CALL DSSTRN (' [2] EXPLICIT X, EXPLICIT Y', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, IDEFXY, 'I')
      GO TO (4, 4), IDEFXY
      CALL MENUER (2)
      GO TO 7
C
C ***      DO AN ERROR TEST
4     CONTINUE
      IF ( (M.EQ.1 .OR. N.EQ.1) .AND. IDEFXY.EQ.2) THEN
        CALL DSERR ('ERROR.  ARRAY IS ONE DIMENSIONAL.  INSUFFICIENT')
        CALL DSERR ('INFORMATION TO DO AN EXPLICIT X, EXPLICIT Y PLOT')
        GO TO 995
      ENDIF
C
C ***      DETERMINE WHETHER TO PLOT ALONG ROWS OR COLUMNS
      CALL PLTPRP (X, Y, M, N, XXX, YYY, ICURS, TAG, NLINES,
     .             LINXYX, LINXYY, NPTS, IDEFXY)
C
C ***      DETERMINE AXIS SCALING
      XMINXY = 9999
      YMINXY = 9999
      XMAXXY = -99999
      YMAXXY = -99999
      DO 150 J = 1, NLINES
        XMINH = 99999
        XMAXH = -99999
        YMINH = 99999
        YMAXH = -99999
        DO 130 K = 1, NPTS
          XMINH = AMIN1 (XMINH, X(J,K))
          XMAXH = AMAX1 (XMAXH, X(J,K))
          YMINH = AMIN1 (YMINH, Y(J,K))
          YMAXH = AMAX1 (YMAXH, Y(J,K))
130     CONTINUE
        XMINXY = AMIN1 (XMINH, XMINXY)
        XMAXXY = AMAX1 (XMAXH, XMAXXY)
        YMINXY = AMIN1 (YMINH, YMINXY)
        YMAXXY = AMAX1 (YMAXH, YMAXXY)
150   CONTINUE
      XLO = XMINXY
      XHI = XMAXXY
      YLO = YMINXY
      YHI = YMAXXY
      SYMNXY = YMINXY
      SYMXXY = YMAXXY
C
C ***      SELECT PLOT OPTIONS
160   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' PLEASE CHOOSE AN OPTION', 1)
      CALL DSSTRN (' [1]  SELECT THE TYPE OF PLOT AXES', 1)
      CALL DSSTRN (' [2]  SELECT THE X AND Y GRID MARKINGS', 1)
      CALL DSSTRN (' [3]  SELECT X AND Y AXIS TICK MARK OPTIONS', 1)
      CALL DSSTRN (' [4]  SELECT THE LINE OPTIONS', 1)
      CALL DSSTRN (' [5]  SELECT THE PLOT COLORS', 1)
      CALL DSSTRN (' [6]  SELECT THE BACKGROUND COLOR', 1)
      CALL DSSTRN (' [7]  SELECT THE AXIS SCALES', 1)
      CALL DSSTRN (' [8]  CHOOSE THE PLOT SIZE', 1)
      CALL DSSTRN (' [9]  CHOOSE THE PLOT LABELS', 1)
      CALL DSSTRN (' [10] DO THE PLOT', 1)
      CALL DSSTRN (' [11] END THE CURRENT PLOT', 1)
      CALL DSSTRN (' [12] EXIT XYPLOT', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, IANS, 'I')
      GO TO  (180, 300, 400, 620, 500, 750,
     .        550, 600, 700, 800, 899, 995), IANS
      CALL MENUER (12)
      GO TO 160
C
180   CONTINUE
      CALL SETAXS (ICURS, IPLTP1, ISYOPT, ISECY, SYMNXY, SYMMXY,
     .             XLO, YLO)
      GO TO 160
C
C ***      SELECT THE GRID LINES OPTIONS
300   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' PLEASE CHOOSE A GRID LINE OPTION', 1)
      CALL DSSTRN (' [1] NO GRID LINES', 1)
      CALL DSSTRN (' [2] GRID LINES ON X AXIS ONLY', 1)
      CALL DSSTRN (' [3] GRID LINES ON Y AXIS ONLY', 1)
      CALL DSSTRN (' [4] GRID LINES ON BOTH AXES', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, IPLTP2, 'I')
      GO TO  (320, 330, 340, 350), IPLTP2
      CALL MENUER (4)
      GO TO 300
C
320   CONTINUE
      IPLTP2 = 0
      GO TO 160
C
330   CONTINUE
      IPLTP2 = 4
      GO TO 160
C
340   CONTINUE
      IPLTP2 = 8
      GO TO 160
C
350   CONTINUE
      IPLTP2 = 12
      GO TO 160
C
C ***      SELECT THE TICK MARK OPTIONS
400   CONTINUE
      IPLTP3 = 0
405   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' PLEASE CHOOSE TICK MARK OPTIONS, ONE BY ONE.', 1)
      CALL DSSTRN ('   NOTE THAT TICK MARK OPTIONS ARE CUMULATIVE.', 1)
      CALL DSSTRN (' [1] CLEAR ALL TICK MARK OPTIONS', 1)
      CALL DSSTRN (' [2] ALLOW X AXIS TO END NOT ON A TICK MARK', 1)
      CALL DSSTRN (' [3] ALLOW Y AXIS TO END NOT ON A TICK MARK', 1)
      CALL DSSTRN (' [4] DO NOT PLOT X AXIS TICK MARKS', 1)
      CALL DSSTRN (' [5] DO NOT PLOT Y AXIS TICK MARKS', 1)
      CALL DSSTRN (' [6] EXTRA X-AXIS AND TICKS ON TOP OF PLOT', 1)
      CALL DSSTRN (' [7] EXTRA Y-AXIS AND TICKS TO RIGHT OF PLOT', 1)
      CALL DSSTRN (' [8] SELECT TICK MARK AND TICK CHARACTER SIZE', 1)
      CALL DSSTRN (' [9] RETURN TO XYPLOT MENU', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, IANS, 'I')
      GO TO  (420, 430, 440, 450, 460, 470, 480, 490, 160), IANS
      CALL MENUER (9)
      GO TO 405
C
420   CONTINUE
      GO TO 400
C
430   CONTINUE
      IPLTP3 = IPLTP3+16
      GO TO 405
C
440   CONTINUE
      IPLTP3 = IPLTP3+32
      GO TO 405
C
450   CONTINUE
      IPLTP3 = IPLTP3+512
      GO TO 405
C
460   CONTINUE
      IPLTP3 = IPLTP3+1024
      GO TO 405
C
470   CONTINUE
      IPLTP3 = IPLTP3+64
      GO TO 405
C
480   CONTINUE
      IPLTP3 = IPLTP3+128
      GO TO 405
C
490   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' PLEASE CHOOSE A TICK MARK SIZING OPTION', 1)
      CALL DSSTRN (' [1] USE AUTO SIZING', 1)
      CALL DSSTRN (' [2] CHOOSE THE SIZE', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, IANS, 'I')
      GO TO  (494, 496), IANS
      CALL MENUER (2)
      GO TO 490
C
494   CONTINUE
      CHRSIZ = 0
      GO TO 405
C
496   CONTINUE
      CALL DSSTRN (' ENTER THE TICK MARK CHARACTER SIZE (CM)', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (CHRSIZ, 0, 'F')
      GO TO 405
C
C ***      DEFINE THE PLOT COLORS
500   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' PLEASE CHOOSE A PLOT COLOR OPTION', 1)
      CALL DSSTRN (' [1] SELECT THE LINE COLORS', 1)
      CALL DSSTRN (' [2] SELECT THE AXIS COLORS', 1)
      CALL DSSTRN (' [3] RETURN TO XYPLOT MENU', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, IANS, 'I')
      GO TO  (520, 540, 160), IANS
      CALL MENUER (3)
      GO TO 500
C
520   CONTINUE
      DO 536 I = 1, NLINES
        CALL DSSTRN (' FOR LINE NUMBER ', 0)
        CALL DSWDI (I, 1)
        CALL SETFG (FGRPXY(I), ICURS)
536   CONTINUE
      GO TO 500
C
540   CONTINUE
      CALL DSSTRN (' FOR THE X-Y AXES', 1)
      CALL SETFG (IAXSPN, ICURS)
      GO TO 500
C
C ***      SET THE AXIS SCALING
550   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' PLEASE CHOOSE PLOT SCALE OPTIONS', 1)
      CALL DSSTRN (' [1] USE AUTO-SCALING', 1)
      CALL DSSTRN (' [2] SELECT X-AXIS SCALE', 1)
      CALL DSSTRN (' [3] SELECT Y-AXIS SCALE', 1)
      CALL DSSTRN (' [4] RETURN TO XYPLOT MENU', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, IANS, 'I')
      GO TO  (565, 570, 580, 160), IANS
      CALL MENUER (4)
C
565   CONTINUE
      XMINXY = XLO
      XMAXXY = XHI
      YMINXY = YLO
      YMAXXY = YHI
      GO TO 160
C
570   CONTINUE
      CALL DSSTRN (' CHOOSE MINIMUM X-AXIS VALUE', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (XMINXY, 0, 'F')
      CALL DSSTRN (' CHOOSE MAXIMUM X-AXIS VALUE', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (XMAXXY, 0, 'F')
      GO TO 550
C
580   CONTINUE
      CALL DSSTRN (' CHOOSE MINIMUM Y-AXIS VALUE', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (YMINXY, 0, 'F')
      CALL DSSTRN (' CHOOSE MAXIMUM Y-AXIS VALUE', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (YMAXXY, 0, 'F')
      GO TO 550
C
C ***      CHOOSE THE PLOT SIZE
600   CONTINUE
      CALL CHKEND
      CALL MAKSIZ (ICURS, XSTXY, XFRCXY, YSTXY, YFRCXY)
      XSTXY = XSTXY*100.
      YSTXY = YSTXY*100.
      XFRCXY = XFRCXY*100.+XSTXY
      YFRCXY = YFRCXY*100.+YSTXY
      GO TO 160
C
C ***      SET LINE OPTIONS
620   CONTINUE
      CALL CHKEND
      CALL LNOPTS (NLINES, SYMSZ, INTSYM, ISYMNO, LINSYL, ICURS)
      GO TO 160
C
C ***      SET THE PLOT LABELS
700   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' ENTER THE X-AXIS LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (XLABXY)
      CALL DSSTRN (' ENTER THE Y-AXIS LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (YLABXY)
      CALL DSSTRN (' ENTER THE PLOT LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (LABXY)
      IF (ISECY) THEN
        CALL DSSTRN (' ENTER THE SECOND Y-AXIS LABEL', 1)
        CALL DSCURS (ICURS)
        CALL GETLAB (SYLBXY)
      ENDIF
      GO TO 160
C
C ***      SET BACKGROUND COLOR
750    CONTINUE
       CALL CHKEND
       CALL CHBACK (ICURS)
       GO TO 160
C
C ***      END THE PLOT
899   CONTINUE
      IF (PLTST) THEN
        PLTST = .FALSE.
        CALL PLTFIN
      ENDIF
      GO TO 160
C
C ***      BEGIN PLOTTING
800   CONTINUE
      CALL CHKEND
C
C ***      SAVE THE SETTINGS UNLESS WE ARE PLOTTING FROM SAVED SETTINGS
C           IF SO, RESTORE THE VALUES TO BE PLOTTED.
      IF (INCTRL.EQ.0) THEN
        CALL SAVPLT (2)
      ELSE
        DO 840 J = 1, NLINES
          IF (TAG.EQ.ROWTAG) THEN
            DO 810 I = 1, N
              IF (IDEFXY.EQ.2)THEN
                X(J,I) = XXX(LINXYX(J),I)
              ELSE
                X(J,I) = FLOAT(I)
              ENDIF
810         CONTINUE
          ELSE
            DO 820 I = 1, M
              IF (IDEFXY.EQ.2)THEN
                X(J,I) = XXX(I,LINXYX(J))
              ELSE
                X(J,I) = FLOAT(I)
              ENDIF
820         CONTINUE
          ENDIF
          IF (TAG.EQ.ROWTAG) THEN
            DO 830 I = 1, N
              Y(J,I) = XXX(LINXYY(J),I)
830         CONTINUE
          ELSE
            DO 832 I = 1, M
              Y(J,I) = XXX(I,LINXYY(J))
832         CONTINUE
          ENDIF
840     CONTINUE
      ENDIF
C
C ***      IF MODE 1, SCALE THE X-AXES
      IF (IDEFXY.EQ.1) THEN
        DO 845 J = 1, NLINES
          IF (TAG.EQ.ROWTAG) THEN
            DO 843 I = 1, N
              X(J,I) = X(J,I)*XMAXXY/FLOAT(N)
843         CONTINUE
          ELSE
            DO 844 I = 1, M
              X(J,I) = X(J,I)*XMAXXY/FLOAT(M)
844         CONTINUE
          ENDIF
845     CONTINUE
      ENDIF
      IF ( (XMINXY.LE.0 .AND. (IPLTP1.EQ.1 .OR. IPLTP1.EQ.3)) .OR.
     .     (YMINXY.LE.0 .AND. (IPLTP1.EQ.2 .OR. IPLTP1.EQ.3))) THEN
        CALL DSERR ('ERROR. AXIS LIMITS INCOMPATIBLE WITH LOG PLOT')
        GO TO 160
      ENDIF
      IF (ISECY .AND. SYMNXY.LE.0 .AND. ISYOPT.EQ.2) THEN
        CALL DSERR (
     .      'ERROR. SECOND Y-AXIS LIMITS INCOMPATIBLE WITH LOG PLOT')
        GO TO 160
      ENDIF
C
      IF (.NOT.PLTST) THEN
        PLTST = .TRUE.
        CALL BGNPLT
      ENDIF
      IF (SETBG) THEN
        SETBG = .FALSE.
        CALL SETBAK (BGRP)
      ENDIF
      CALL GSCOLR (IAXSPN, IERR)
C
C ***      DEFINE PLOT SIZE
      IF (ISECY) THEN
        CALL MAPSZ2 (XSTXY, XFRCXY, YSTXY, YFRCXY, CHRSIZ)
      ELSE
        CALL MAPSIZ (XSTXY, XFRCXY, YSTXY, YFRCXY, CHRSIZ)
      ENDIF
C
C ***      GENERATE THE AXES
      IOPTNS = IPLTP1+IPLTP2+IPLTP3
      CALL GSLTYP (1)
      CALL MAPIT (XMINXY, XMAXXY, YMINXY, YMAXXY,
     .            XLABXY, YLABXY, LABXY, IOPTNS)

C
C ***      DO THE LINES ON THE PLOT
      DO 900 INDEX = 1, NLINES
        DO 882 I = 1, NPTS
          XPL(I) = X(INDEX,I)
          YPL(I) = Y(INDEX,I)
882     CONTINUE
        CALL GSCOLR (FGRPXY(INDEX), IERR)
        CALL GSLTYP (LINSYL(INDEX))
        CALL CURVE (XPL, YPL, NPTS, ISYMNO(INDEX), SYMSZ(INDEX),
     .              INTSYM(INDEX))
900   CONTINUE
C
C ***      DO THE SECOND Y AXIS
      IF (ISECY) THEN
        CALL GSCOLR (IAXSPN, IERR)
        CALL GSLTYP (1)
        CALL SYAXIS (SYMNXY, SYMXXY, SYLBXY, ISYOPT)
      ENDIF
      IF (INCTRL.EQ.0) GO TO 160
C
995   CONTINUE
      RETURN
      END
