      SUBROUTINE D3PLOT (XXX, M, N, INCTRL, IOCTRL, IZ)
      IMPLICIT NONE
C
C ***      3D PLOTTING
C
      INTEGER M, N, INCTRL, IOCTRL
      REAL XXX(M,N)
C
      INCLUDE MATLAB$KOM:SIZEPARMS.INC
C
      CHARACTER*1 IZ(VARSIZE)
C
      INCLUDE MATLAB$KOM:IOP.KOM
      INCLUDE MATLAB$KOM:MATPLT.KOM
      INCLUDE MATLAB$KOM:PLT3D.KOM
C
      INTEGER CHOICE, I, MN, IERR
      CHARACTER*8 ICURS
      REAL LABSIZ, XMAX, YMAX, ZMAX, ZMIN
C
C ***      FUNCTIONS
      REAL GSXLCM, GSYLCM
C
      DATA ICURS / '3DPlot>>' /
C
C
C ***      FIGURE OUT WHO CALLED US AND
C ***        IF SO INDICATED JUMP DIRECTLY TO THE PLOT
      IF (INCTRL.NE.0) GO TO 900
C
C ***      SET UP THE DEFAULTS
      DO 25 I = 1, 80
        XLAB3D(I) = Z'20'
        YLAB3D(I) = Z'20'
        ZLAB3D(I) = Z'20'
25    CONTINUE
      LABE3D = 0
      XST3D = 0
      YST3D = 0
      XFRC3D = 1
      YFRC3D = 1
      XYLIM(1,1) = 0
      XYLIM(2,1) = M
      XYLIM(1,2) = 0
      XYLIM(2,2) = N
      XYLIM(1,4) = 0.0
      XYLIM(2,4) = 0.0
      XYLIM(1,6) = XST3D
      XYLIM(2,6) = YST3D
      CAMLOC(1) = 2000.
      CAMLOC(2) = 45
      CAMLOC(3) = 30
      FGRP3D = 1
      IOCTRL = 0
      MARPLT = 0
      XMAX = GSXLCM ()
      YMAX = GSYLCM ()
      XYLIM(1,5) = XMAX
      XYLIM(2,5) = YMAX * .9
      MN = M*N
C      WRITE (WTE, 657) ((XXX(J,K), J = 1, M), K = 1, N)
C657   FORMAT (1X, F10.2)
      CALL MINMAX (XXX, MN, ZMIN, ZMAX)
      XYLIM(1,3) = ZMIN
      XYLIM(2,3) = ZMAX
C
C ***      DETERMINE THE CHOICES
C75    CONTINUE
      CALL CHKEND
C      CALL DSSTRN (' USE THE DEFAULT VALUES?', 1)
C      CALL DSCURS (ICURS)
C      READ (RTE, 80) ANS
C80    FORMAT (A1)
C      IF (ANS.EQ.'N' .OR. ANS.EQ.'n') THEN
C        GO TO 100
C      ELSE IF (ANS.EQ.'Y' .OR. ANS.EQ.'y') THEN
C        GO TO 900
C      ELSE
C        CALL DSERR ('ERROR.  PLEASE ANSWER YES (Y) OR NO (N)')
C        GO TO 75
C      ENDIF
100   CONTINUE
      CALL CHKEND
      CALL DSBLLN (2)
      CALL DSSTRN (' PLEASE CHOOSE AN OPTION', 1)
      CALL DSSTRN (' [1] CHOOSE PLOT LABELS', 1)
      CALL DSSTRN (' [2] CHOOSE PLOT VALUES', 1)
      CALL DSSTRN (' [3] CHOOSE VIEWER PERSPECTIVE', 1)
      CALL DSSTRN (' [4] CHOOSE HIDDEN LINES OPTIONS', 1)
      CALL DSSTRN (' [5] CHOOSE PLOT COLOR', 1)
      CALL DSSTRN (' [6] CHOOSE PLOT SIZE', 1)
      CALL DSSTRN (' [7] DO THE PLOT', 1)
      CALL DSSTRN (' [8] END THE CURRENT PLOT', 1)
      CALL DSSTRN (' [9] EXIT 3D PLOT', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, CHOICE, 'I')
      GO TO (200, 300, 400, 500, 700, 800, 900, 899, 990), CHOICE
      CALL MENUER (9)
      GO TO 100
C
C ***      SELECT THE LABELS
200   CONTINUE
      CALL CHKEND
      CALL DSBLLN (2)
      CALL DSSTRN (' ENTER THE X AXIS LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (XLAB3D)
      CALL DSBLLN (2)
      CALL DSSTRN (' ENTER THE Y AXIS LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (YLAB3D)
      CALL DSBLLN (2)
      CALL DSSTRN (' ENTER THE Z AXIS LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (ZLAB3D)
      CALL DSBLLN (2)
      CALL DSSTRN (' ENTER THE SIZE OF THE LABELS (CM)', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (LABSIZ, 0, 'F')
      IF (LABSIZ.NE.0) LABE3D = LABSIZ
      GO TO 100
C
C ***      SELECT THE DATA
300   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' ENTER THE MINIMUM VALUE OF X', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (XYLIM(1,1), 0, 'F')
      CALL DSSTRN (' ENTER THE MAXIMUM VALUE OF X', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (XYLIM(2,1), 0, 'F')
      CALL DSSTRN (' ENTER THE MINIMUM VALUE OF Y', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (XYLIM(1,2), 0, 'F')
      CALL DSSTRN (' ENTER THE MAXIMUM VALUE OF Y', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (XYLIM(2,2), 0, 'F')
      CALL DSSTRN (' ENTER THE X/Z LENGTH RATIO', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (XYLIM(1,4), 0, 'F')
      CALL DSSTRN (' ENTER THE Y/Z LENGTH RATIO', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (XYLIM(2,4), 0, 'F')
      GO TO 100
C
C ***      DEFINE THE VIEWER PERSPECTIVE
400   CONTINUE
      CALL CHKEND
      CALL DSSTRN (' ENTER THE DISTANCE OF THE OBSERVER FROM THE', 1)
      CALL DSSTRN ('  CENTER OF THE PICTURE (SAME UNITS AS Z)', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (CAMLOC(1), 0, 'F')
      CALL DSSTRN (
     .    ' ENTER THE ANGLE BETWEEN THE VIEWER AND THE X-AXIS', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (CAMLOC(2), 0, 'F')
      CALL DSSTRN (
     .    ' ENTER THE ANGLE BETWEEN THE VIEWER AND THE X-Z PLANE', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (CAMLOC(3), 0, 'F')
      GO TO 100
C
C ***      CHOOSE THE HIDDEN LINE DRAWING MODE
500   CONTINUE
      CALL CHKEND
507   CONTINUE

      CALL DSBLLN (2)
      CALL DSSTRN (' SELECT THE DRAWING MODE', 1)
      CALL DSSTRN (' [1] DRAW ALL LINES, HIDDEN OR NOT', 1)
      CALL DSSTRN (
     .    ' [2] SUPPRESS HIDDEN LINES, BUT DRAW TOP AND BOTTOM', 1)
      CALL DSSTRN ('      OF THE SURFACE', 1)
      CALL DSSTRN (
     .    ' [3] SUPPRESS HIDDEN LINES, AS WELL AS ALL LINES SHOWING', 1)
      CALL DSSTRN ('      THE BOTTOM OF THE SURFACE', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, MARPLT, 'I')
      GO TO (515, 515, 525) MARPLT
      CALL MENUER (3)
      GO TO 507
C
515   CONTINUE
      MARPLT = MARPLT - 1
525   CONTINUE
      GO TO 100
C
C ***      SELECT THE PLOT COLOR
700   CONTINUE
      CALL CHKEND
      CALL SETFG (FGRP3D, ICURS)
      GO TO 100
C
C ***      SELECT THE PLOT SIZE
800   CONTINUE
      CALL CHKEND
      CALL MAKSIZ (ICURS, XST3D, XFRC3D, YST3D, YFRC3D)
      XYLIM(1,6) = XST3D*XMAX
      XYLIM(2,6) = YST3D*YMAX
      XYLIM(1,5) = XFRC3D*XMAX
      XYLIM(2,5) = YFRC3D*YMAX
      GO TO 100
C
899   CONTINUE
      IF (PLTST) THEN
        PLTST = .FALSE.
        CALL PLTFIN
      ENDIF
      GO TO 100
C
C ***      DO THE PLOT
900   CONTINUE
      CALL CHKEND
C
C ***      SAVE THE PLOT SETTINGS, UNLESS WE ARE WORKING FROM A SAVED FILE
      IF (INCTRL.EQ.0) CALL SAVPLT (1)
      IF (.NOT.PLTST) THEN
        PLTST = .TRUE.
        CALL BGNPLT
      ENDIF
      IF (SETBG) THEN
        SETBG = .FALSE.
        CALL SETBAK (BGRP)
      ENDIF
      CALL GSCOLR (FGRP3D, IERR)
C      DO 940 I = 1, 6
C        WRITE (WTE, 950) XYLIM(1,I), XYLIM(2,I)
C950     FORMAT (1X, 2F10.3)
C940   CONTINUE
C      WRITE (WTE, 960) (CAMLOC(I), I = 1, 3)
C960   FORMAT (' CAMLOC:', 3F8.2)
C      WRITE (WTE, 962) M, N, MARPLT
C962   FORMAT (3I4)
      CALL PURJOY (XXX, M, IZ, M, N, CAMLOC, XYLIM, XLAB3D, YLAB3D,
     .             ZLAB3D, LABE3D, MARPLT)
      IF (INCTRL.EQ.0) GO TO 100
C
990   CONTINUE
      RETURN
      END
