      SUBROUTINE BARPLT (XXX, M, N, INCTRL, IERR)
      IMPLICIT NONE
C
C ***      DO HISTOGRAMS AND BAR PLOTS
      INTEGER M, N, INCTRL, IERR
      REAL XXX(M,N)
C
      INCLUDE MATLAB$KOM:IOP.KOM
      INCLUDE MATLAB$KOM:MATPLT.KOM
      INCLUDE MATLAB$KOM:BAR.KOM
C
      INTEGER I, J, IANS, KK
      REAL FIMX(8), COUNT(512,8), STEP, X0, Y0, VX0, VX1, VY0, VY1
      CHARACTER*8 ICURS
C
      DATA ICURS / 'BARPlt>>' /
C
C
C ***      SEE WHO CALLED US
      IF (INCTRL.NE.0) GO TO 90
C
C ***      SET UP THE DEFAULTS
      DO 9 I = 1, 8
        FGRPBP(I) = I
9     CONTINUE
      CALL MINMAX (XXX, M*N, XLOWBP, XHIBP)
      IMXC = 512
      NOBARS = M
      YLOWBP = 0.0
      YHIBP = 1.0
      FBAR = FLOAT(NOBARS)
      DO 1 I = 1, 80
        XLABBP(I) = ' '
        YLABBP(I) = ' '
        LABBP(I) = ' '
1     CONTINUE
      XLABBP(2) = Z'00'
      YLABBP(2) = Z'00'
      LABBP(2) = Z'00'
      IMXPTS = M
      IMYPTS = N
      XSTBP = 0
      YSTBP = 0
      XFRCBP = 100
      YFRCBP = 95
      CHSZBP = 0
      ISCYBP = 0
      IPLBP1 = 0
      STATBP = .TRUE.
      IXCLBP = 1
C
C ***      Main Menu
4     CONTINUE
      CALL CHKEND
      CALL DSSTRN (' PLEASE SELECT AN OPTION', 1)
      CALL DSSTRN (' [1]  CHOOSE THE BAR GRAPH TYPE', 1)
      CALL DSSTRN (' [2]  CHOOSE BAR GRAPH AXIS TYPES', 1)
      CALL DSSTRN (' [3]  CHOOSE BAR GRAPH AXIS COLORS', 1)
      CALL DSSTRN (' [4]  CHOOSE THE BAR GRAPH LABELS', 1)
      CALL DSSTRN (' [5]  SET THE BAR GRAPH COLORS', 1)
      CALL DSSTRN (' [6]  SET THE BACKGROUND COLOR', 1)
      CALL DSSTRN (' [7]  SET THE PLOT SIZE', 1)
      CALL DSSTRN (' [8]  DO THE PLOT', 1)
      CALL DSSTRN (' [9]  END THE CURRENT PLOT', 1)
      CALL DSSTRN (' [10] EXIT BARGRAPH', 1)
      CALL DSCURS (ICURS)
C
      CALL VALGET (0, IANS, 'I')
      GO TO  (15, 10, 20, 60, 30, 40, 50, 90, 899, 9999), IANS
      CALL MENUER (10)
      GO TO 4
C
C ***      DEFINE THE BAR GRAPH AXIS TYPES
10    CONTINUE
      CALL CHKEND
      CALL SETAXS (ICURS, IPLBP1, SYOPBP, ISCYBP, SYLOBP, SYHIBP,
     .             XLOWBP, YLOWBP)
      GO TO 4
C
C ***      DETERMINE WHETHER TO COUNT OCCURRENCES OR
C ***        DO STATISTICAL DISTRIBUTION
15    CONTINUE
      CALL CHKEND
      CALL DSSTRN (' PLEASE CHOOSE THE DESIRED Y-AXIS TYPE:', 1)
      CALL DSSTRN (' [1] STATISTICAL (MAXIMUM Y VALUE IS 1.0)', 1)
      CALL DSSTRN (' [2] OCCURRENCE COUNT', 1)
      CALL DSCURS (ICURS)
      CALL VALGET (0, IANS, 'I')
      GO TO  (17, 18), IANS
C
      CALL MENUER (2)
      GO TO 15
C
17    CONTINUE
      STATBP = .TRUE.
      GO TO 4
C
18    CONTINUE
      STATBP = .FALSE.
      GO TO 4
C
C ***      DO THE PLOT LABELS
20    CONTINUE
      CALL DSSTRN (' ENTER THE X-AXIS LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (XLABBP)
      CALL DSSTRN (' ENTER THE Y-AXIS LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (YLABBP)
      CALL DSSTRN (' ENTER THE PLOT LABEL', 1)
      CALL DSCURS (ICURS)
      CALL GETLAB (LABBP)
      GO TO 4
C
C ***      SET THE BAR GRAPH COLORS
30    CONTINUE
      CALL CHKEND
      KK = IMYPTS
      IF (KK.GT.8) KK = 8
      DO 35 I = 1, KK
        CALL DSSTRN (' FOR BAR GRAPH NUMBER ', 0)
        CALL DSWDI (I, 1)
        CALL SETFG (FGRPBP(I), ICURS)
35    CONTINUE
      GO TO 4
C
C ***      CHANGE THE BACKGROUND PEN COLOR
40    CONTINUE
      CALL CHKEND
      CALL CHBACK (ICURS)
      GO TO 4
C
C ***      SET THE PLOT SIZE
50    CONTINUE
      CALL CHKEND
      CALL MAKSIZ (ICURS, XSTBP, XFRCBP, YSTBP, YFRCBP)
      XSTBP = XSTBP*100.
      YSTBP = YSTBP*100.
      XFRCBP = XFRCBP*100.+XSTBP
      YFRCBP = YFRCBP*100.+YSTBP
      GO TO 4
C
C ***      SET THE AXIS COLORS
60    CONTINUE
      CALL CHKEND
      CALL SETFG (IXCLBP, IERR)
      GO TO 4
C
C ***      DO THE PLOT
90    CONTINUE
      CALL CHKEND
C
C ***      SAVE THE PLOT UNLESS THIS IS ALREADY A SAVED PLOT
      IF (INCTRL.EQ.0) CALL SAVPLT (3)
C
      IF ( (XLOWBP.LE.0 .AND.  (IPLBP1.EQ.1 .OR. IPLBP1.EQ.3)) .OR.
     .     (YLOWBP.LE.0 .AND.  (IPLBP1.EQ.2 .OR. IPLBP1.EQ.3)) ) THEN
        CALL DSERR ('ERROR. AXIS LIMITS INCOMPATIBLE WITH LOG PLOT')
        GO TO 4
      ENDIF
      IF (ISCYBP .AND. (SYLOBP.LE.0) .AND. (SYOPBP.EQ.2)) THEN
        CALL DSERR (
     .       'ERROR. SECOND Y-AXIS LIMITS INCOMPATIBLE WITH LOG PLOT')
        GO TO 4
      ENDIF
C
      IF  (XLOWBP.GT.XHIBP) THEN
        CALL DSERR ('INTERNAL ERROR.  XMIN GREATER THAN XMAX')
        CALL DSERR ('CALLED FROM BARPLT.  THIS CAN''T HAPPEN,')
        CALL DSERR ('SO IF IT HAS, YOU''RE SCREWED!  SORRY.')
        GO TO 9999
      ENDIF
C
      IF  (NOBARS.GT.IMXC) THEN
        CALL DSERR ('ERROR.  TOO MANY BINS.  THE MAXIMUM IS 512')
        GO TO 9999
      ENDIF
C
      IF (IMYPTS.GT.8) THEN
        CALL DSERR ('WARNING.  TOO MANY BARGRAPHS SPECIFIED.')
        CALL DSERR ('ONLY THE FIRST 8 ROWS WILL BE GRAPHED')
        IMYPTS = 8
      ENDIF
C
      STEP = (XHIBP - XLOWBP) / FBAR
C
      DO 100 I = 1, 512
      DO 100 J = 1, 8
        COUNT(I, J) = 0.0
100   CONTINUE
C
      DO 350 KK = 1, IMYPTS
        DO 200 I = 1, IMXPTS
          J = INT ( (XXX(I,KK)-XLOWBP)/STEP) + 1
          IF (J.GT.NOBARS) J = NOBARS
          COUNT(J,KK) = COUNT(J,KK) + 1.0
200     CONTINUE
C
        IF (STATBP) THEN
          FIMX(KK) = FLOAT(IMXPTS) * STEP
          DO 300 I = 1, NOBARS
            COUNT(I,KK) = COUNT(I,KK) / FIMX(KK)
300       CONTINUE
        ENDIF
350   CONTINUE
C
      CALL MINMAX (COUNT, 4096, YLOWBP, YHIBP)
      YLOWBP = 0.0
      YHIBP = YHIBP + 0.1 * YHIBP
C
      IF (.NOT.PLTST) THEN
        PLTST = .TRUE.
        CALL BGNPLT
      ENDIF
C
C ***      SET THE BACKGROUND COLOR
      IF (SETBG) THEN
        SETBG = .FALSE.
        CALL SETBAK (BGRP)
      ENDIF
C
C ***      DEFINE PLOT SIZE
C      WRITE (WTE, 1324) XSTBP, XFRCBP, YSTBP, YFRCBP, CHSZBP
C1324  FORMAT (' MAPSIZE', 5F10.3)
      IF (ISCYBP) THEN
        CALL MAPSZ2 (XSTBP, XFRCBP, YSTBP, YFRCBP, CHSZBP)
      ELSE
        CALL MAPSIZ (XSTBP, XFRCBP, YSTBP, YFRCBP, CHSZBP)
      ENDIF
      CALL GSCOLR (IXCLBP, IERR)
C      WRITE (WTE, 1789) XLOWBP, XHIBP, YLOWBP, YHIBP
C1789  FORMAT (' MAPIT', 4F10.3)
      CALL MAPIT (XLOWBP, XHIBP, YLOWBP, YHIBP, XLABBP, YLABBP, LABBP,
     .            IPLBP1)
C
      DO 500 KK = 1, IMYPTS
        CALL GSCOLR (FGRPBP(KK), IERR)
        X0 = XLOWBP
        Y0 = 0.0
        CALL SCALE (X0, Y0, VX0, VY0)
        CALL GSMOVE (VX0, VY0)
C
        DO 400 I = 1, NOBARS
          X0 = XLOWBP + I * STEP
          Y0 = COUNT(I,KK)
          CALL SCALE (X0, Y0, VX1, VY1)
          CALL GSDRAW (VX0, VY1)
          CALL GSDRAW (VX1, VY1)
          CALL GSDRAW (VX1, VY0)
          VX0 = VX1
400     CONTINUE
500   CONTINUE
C
C ***      DO THE SECOND Y AXIS
      IF (ISCYBP) THEN
C        CALL GSCOLR (IXCLBP, IERR)
        CALL GSLTYP (1)
        CALL SYAXIS (SYLOBP, SYHIBP, SYLBBP, SYOPBP)
      ENDIF
      IF (INCTRL.NE.0) GO TO 9999
      GO TO 4
C
899   IF (PLTST) THEN
        PLTST = .FALSE.
        CALL PLTFIN
      ENDIF
      GO TO 4
C
9999  CONTINUE
      RETURN
      END
