      SUBROUTINE AutoAxes (datasetx, datasety, numdat, AxFlag)
      INCLUDE 'GRAFTYPE.FOR'
      INCLUDE 'STDHDR.FOR'
      INTEGER cwin,numdat, AxFlag, dirx, diry, lsx, lsy
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL datasetx(0:maxv), datasety(0:maxv), VerySmall
      REAL xx1, yy1, xx2, yy2, ts1, ts2, xi, yi
      PARAMETER ( VerySmall = 1E-10)

      dirx = 0
      diry = 0
      CALL FindMinMax(datasetx, numdat, xx1, xx2)
      CALL FindMinMax(datasety, numdat, yy1, yy2)
      IF (.NOT. grstat(cwin).LogX)  CALL RoundAxes(xx1, xx2, ts1)
      IF (.NOT. grstat(cwin).LogY)  CALL RoundAxes(yy1, yy2, ts2)
      IF (AxFlag .EQ. 0) THEN
        IF (.NOT. grstat(cwin).LogX) THEN
          IF (xx2 .GE. 0.0 .AND. xx1 .GE. 0.0) xi = xx1
          IF (xx2 .GE. 0.0 .AND. xx1 .LT. 0.0) xi = 0.0
          IF (xx2 .LE. 0.0 .AND. xx1 .LE. 0.0) THEN
            xi = xx2
            diry = 1
          END IF
        END IF
        IF (.NOT. grstat(cwin).LogY) THEN
          IF (yy2 .GE. 0.0 .AND. yy1 .GE. 0.0)  yi = yy1
          IF (yy2 .GE. 0.0 .AND. yy1 .LT. 0.0)  yi = 0.0
          IF (yy2 .LE. 0.0 .AND. yy1 .LE. 0.0) THEN
            yi = yy2
            dirx = 1
          END IF
        END IF
      ELSE
        xi = xx1
        yi = yy1
      END IF
      grstat(cwin).yint = yi
      grstat(cwin).xint = xi
      CALL ScalePlotArea(xx1, yy1, xx2, yy2)
      IF (grstat(cwin).LogX)
     +    grstat(cwin).xint = grstat(cwin).plotclip.left
      IF (grstat(cwin).LogY)
     +   grstat(cwin).yint = grstat(cwin).plotclip.bottom
      CALL SetXYIntercepts(grstat(cwin).xint, grstat(cwin).yint)
      CALL DrawYAxis(ts2, diry)
      CALL DrawXAxis(ts1, dirx)
      IF (grstat(cwin).plotrect.right -
     +      grstat(cwin).plotrect.left .GT. 350) THEN
        lsx = 10
      ELSE
        lsx = 20
      END IF
      IF (ABS(grstat(cwin).plotrect.top -
     +          grstat(cwin).plotrect.bottom) .LT. 75) THEN
        lsy = 20
      ELSE
        lsy = 10
      END IF
      CALL LabelYAxis(lsy, diry)
      CALL LabelXAxis(lsx, dirx)
      END !SUBROUTINE





      SUBROUTINE BarGraphData (datasetx, datasety, numdat,
     +                         barwid, newcolor, Hatchstyle)
      INCLUDE 'GRAFTYPE.FOR'
      INCLUDE 'STDHDR.FOR'
      INTEGER cwin, numdat, newcolor, hatchstyle, i, OldColor, iErr
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL datasetx(0:maxv), datasety(0:maxv), barwid
      REAL xx1, yy1, yy2
      REAL tx[ALLOCATABLE](:),ty[ALLOCATABLE](:)

      ALLOCATE(tx(0:maxV),ty(0:maxV),STAT=iErr)
      CALL CheckMem(iErr)

      CALL GetColXX(OldColor)
      CALL SetFillStyleXX(Hatchstyle, newcolor)
      CALL CopyVectors(datasetx, tx, numdat)
      CALL CopyVectors(datasety, ty, numdat)
      CALL PrePlot(tx, ty, numdat)
      CALL moveworldabs(tx(0), ty(0))
      IF (grstat(cwin).plotclip.top .GE. 0.0 .AND.
     +   grstat(cwin).plotclip.bottom .GE. 0.0)
     +    yy1 = grstat(cwin).plotclip.bottom
      IF (grstat(cwin).plotclip.top .GE. 0.0 .AND.
     +    grstat(cwin).plotclip.bottom .LT. 0.0)    yy1 = 0.0
      IF (grstat(cwin).plotclip.top .LE. 0.0 .AND.
     +    grstat(cwin).plotclip.bottom .LE. 0.0 )
     +    yy1 = grstat(cwin).plotclip.top
      IF (grstat(cwin).LogX)   barwid =
     +    (log10(grstat(cwin).plotclip.right) -
     +    log10(grstat(cwin).plotclip.left)) / (numdat * 1.1)
      IF (grstat(cwin).LogY)  yy1 = log10(yy1)
      DO i = 0, numdat - 1
        yy2 = ty(i)
        xx1 = tx(i)
        CALL BarWorld(xx1,yy1, yy2 - yy1, barwid, newcolor, Hatchstyle)
      END DO
      CALL PostPlot
      CALL SelectColor(OldColor)
      DEALLOCATE(tx,ty,STAT=iErr)
      CALL CheckDealloc(iErr)
      END !SUBROUTINE





      SUBROUTINE BorderCurrentWindow (c)
      INTEGER c
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      CALL SelectColor(c)
      CALL RectangleXX(1.0, 1.0,
     +     grstat(cwin).drawingRect.right -
     +     grstat(cwin).drawingRect.left - 1.0,
     +     grstat(cwin).drawingRect.bottom -
     +     grstat(cwin).drawingRect.top - 1.0)
      END !SUBROUTINE



      SUBROUTINE CheckForContour (cm, j1, i1, j2, i2,
     +            minX, xSpace, minY, ySpace, contourZ, x, y, found)
      INCLUDE 'GRAFTYPE.FOR'
      REAL cm(0:maxContourX,0:maxContourY)
      REAL minX, xSpace, minY, ySpace, contourZ, x, y
      INTEGER j1, i1, j2, i2
      LOGICAL found
      REAL deltaX, deltaY, deltaZ

      IF ((cm(i1, j1) .GE. contourZ .AND.
     +    cm(i2, j2) .LE. contourZ) .OR.
     +   (cm(i1, j1) .LE. contourZ .AND.
     +    cm(i2, j2) .GE. contourZ)) THEN
        found = .TRUE.
      ELSE
        found = .FALSE.
      END IF

      IF (found) THEN
        IF (j2 - j1 .EQ. 1) THEN
          deltaX = xSpace
        ELSEIF (j2 - j1 .EQ. -1) THEN
          deltaX = -xSpace
        ELSE
          deltaX = 0.0
        END IF

        IF (i2 - i1 .EQ. 1) THEN
          deltaY = ySpace
        ELSEIF (i2 - i1 .EQ. -1) THEN
          deltaY = -ySpace
        ELSE
          deltaY = 0.0
        END IF
        deltaZ = cm(i2, j2) - cm(i1, j1)
        x = minX + xSpace * j1 +
     +      (contourZ - cm(i1, j1)) * (deltaX / deltaZ)
        y = minY + ySpace * i1 +
     +      (contourZ - cm(i1, j1)) * (deltaY / deltaZ)
      END IF
      END !FUNCTION





      SUBROUTINE ClearGraph
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetGraphViewport(grstat(cwin).plotrect.left + 1,
     +    grstat(cwin).plotrect.top + 1,
     +    grstat(cwin).plotrect.right - 1,
     +    grstat(cwin).plotrect.bottom - 1)
      CALL ClearViewportXX
      CALL SetGraphViewport(grstat(cwin).drawingRect.left,
     +    grstat(cwin).drawingRect.top,
     +    grstat(cwin).drawingRect.right,
     +    grstat(cwin).drawingRect.bottom)
      END !SUBROUTINE




      SUBROUTINE ClearWindow

      CALL ClearViewportXX
      END !SUBROUTINE



      SUBROUTINE CloseSEGraphics

      CALL CloseGraphics
      END !SUBROUTINE




      SUBROUTINE ContourPlot (cm, rows, columns, contourInc,
     +                        colorMap)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      REAL cm(0:maxContourX,0:maxContourY), contourInc
      RECORD /GroupInfoRec/ colorMap(0:maxLegends)
      INTEGER rows, columns
      INTEGER i, j, k, contourCounter
      RECORD /WorldRect/ wr
      REAL minX, xSpace, minY, ySpace, contourValue
      REAL minZ, maxZ, x(0: 3), y(0: 3)
      LOGICAL contourFound(0: 3)

      minX = grstat(cwin).plotclip.left
      xSpace = (grstat(cwin).plotclip.right -
     +          grstat(cwin).plotclip.left) / REAL(columns - 1)
      minY = grstat(cwin).plotclip.bottom
      ySpace = (grstat(cwin).plotclip.top -
     +          grstat(cwin).plotclip.bottom) /REAL (rows - 1)

      CALL SetWorldRect(wr, grstat(cwin).plotclip.left,
     +    grstat(cwin).plotclip.bottom, grstat(cwin).plotclip.right,
     +    grstat(cwin).plotclip.top)
      CALL SetGraphViewport(grstat(cwin).plotrect.left,
     +    grstat(cwin).plotrect.top, grstat(cwin).plotrect.right,
     +    grstat(cwin).plotrect.bottom)
      CALL SetWorldCoordinates(wr)
      contourCounter = 0
      CALL FindCMMInMax(cm, rows, columns, minZ, maxZ)
      contourValue = minZ + contourInc
      DO  WHILE  (contourValue .LT. maxZ)
        CALL SelectColor(colorMap(contourCounter).GroupColor)
        CALL SetLineStyleXX(colorMap(contourCounter).GroupHatch,1)
        DO i = 0, rows - 2
          DO j = 0, columns - 2
      !!!top
         CALL CheckForContour(cm, j, i, j + 1,i, minX, xSpace, minY,
     +      ySpace, contourValue, x(0), y(0), contourFound(0))
      !!!right
         CALL CheckForContour(cm, j+1, i, j+1,i+1, minX, xSpace,minY,
     +       ySpace, contourValue, x(1), y(1),contourFound(1))
      !!!bottom
           CALL CheckForContour(cm, j+1, i+1, j,i+1, minX, xSpace,minY,
     +       ySpace, contourValue, x(2), y(2), contourFound(2))
      !!!left
           CALL CheckForContour(cm, j, i+1, j, i,minX, xSpace, minY,
     +       ySpace, contourValue, x(3), y(3), contourFound(3))
            numCountours = 0
            DO k = 0, 3
              IF (contourFound(k)) THEN
                x(numCountours) = x(k)
                y(numCountours) = y(k)
                numCountours = numCountours + 1
              END IF
            END DO
            SELECT CASE (numCountours)
              CASE (2)
                   CALL moveworldabs(x(0), y(0))
                   CALL lineworldabs(x(1), y(1))
              CASE (3)
                   CALL moveworldabs(x(0), y(0))
                   CALL lineworldabs(x(2), y(2))
              CASE (4)
                   CALL moveworldabs(x(0), y(0))
                   CALL lineworldabs(x(2), y(2))
                   CALL moveworldabs(x(1), y(1))
                   CALL lineworldabs(x(3), y(3))
              CASE DEFAULT
                  CALL moveworldabs(x(0), y(0))
                  CALL lineworldabs(x(1), y(1))
            END SELECT
          END DO
        END DO
        contourValue = contourValue + contourInc
        contourCounter = contourCounter + 1
      END DO
      CALL SetGraphViewport(grstat(cwin).drawingRect.left,
     +      grstat(cwin).drawingRect.top,
     +      grstat(cwin).drawingRect.right,
     +      grstat(cwin).drawingRect.bottom)
      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      END !SUBROUTINE





      SUBROUTINE ContourPlotLegends (cm, rows, columns, contourInc,
     +                               colorMap )
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL cm(0:maxcontourX, 0:maxcontourY),  contourInc
      RECORD /GroupInfoRec/ colorMap(0:MaxLegends)
      INTEGER rows, columns
      REAL contourValue, minZ, maxZ,contourValues(0 : maxLegends)
      INTEGER  i

      i = 0
      CALL FindCMMInMax(cm, rows, columns, minZ, maxZ)
      contourValue = minZ + contourInc
      DO WHILE (contourValue .LT. maxZ)
        contourValues(i) = contourValue
        contourValue = contourValue + contourInc
        i = i + 1
      END DO
      CALL RealLegends(contourValues, colorMap, i, 0)
      END !SUBROUTINE





      SUBROUTINE CopyVectors (xin, xout, n)
      INCLUDE 'STDHDR.FOR'
      REAL xin(0:maxv), xout(0:maxv)
      INTEGER n, i

      DO i = 0, n - 1
        xout(i) = xin(i)
      END DO
      END !SUBROUTINE


      SUBROUTINE Copy1to2D (xout, xin, numdat, numgroup)
      INCLUDE 'STDHDR.FOR'
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER  i
      REAL xin(0:maxv)
      REAL xout(0:maxgroup,0:maxv)

      DO i = 0, numdat - 1
        xout(numgroup,i) = xin(i)
      END DO
      END !SUBROUTINE

      SUBROUTINE Copy2DVectors (xin, xout, numdat, numgroup)
      INCLUDE 'STDHDR.FOR'
      INCLUDE 'GRAFTYPE.FOR'
      REAL xin(0:maxgroup, 0: maxv), xout(0:maxv)
      INTEGER numdat, numgroup, i

       DO i = 0, numdat - 1
          xout(i) = xin(numgroup,i)
       END DO
      END !SUBROUTINE





      SUBROUTINE DefGraphWindow (xx1, yy1, xx2, yy2, win)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER xx1, yy1, xx2, yy2, win, cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      cwin = win
      CALL SetAxesType(0, 0)
      CALL SetRect(grstat(cwin).drawingRect, xx1, yy1, xx2, yy2)
      grstat(cwin).plotrect.left = grstat(cwin).drawingRect.left +
     +                    NINT((grstat(cwin).drawingRect.right -
     +                          grstat(cwin).drawingRect.left) *
     +                          grstat(cwin).win2plotratio.left)
      grstat(cwin).plotrect.bottom = grstat(cwin).drawingRect.bottom
     +                   - NINT((grstat(cwin).drawingRect.bottom -
     +                   grstat(cwin).drawingRect.top) *
     +                   grstat(cwin).win2plotratio.bottom)
      grstat(cwin).plotrect.right = grstat(cwin).drawingRect.right -
     +                  NINT((grstat(cwin).drawingRect.right -
     +                  grstat(cwin).drawingRect.left) *
     +                  grstat(cwin).win2plotratio.right)
      grstat(cwin).plotrect.top = grstat(cwin).drawingRect.top +
     +                  NINT((grstat(cwin).drawingRect.bottom -
     +                  grstat(cwin).drawingRect.top) *
     +                  grstat(cwin).win2plotratio.top)
      CALL SetWorldRect(grstat(cwin).plotclip,0.0, 0.0,1000.0,1000.0)
      CALL SetWorldRect(grstat(cwin).plotworld,0.0, 0.0,1000.0,1000.0)
      CALL SetGraphAreaWorld(0.0, 0.0, 1000.0, 1000.0)
      CALL SetGraphViewport(xx1, yy1, xx2, yy2)
      END !SUBROUTINE




      SUBROUTINE DrawGrid (NthTic)
      INTEGER NthTic
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      IF (grstat(cwin).LogX) THEN
        CALL DrXLogGrid (NthTic)
      ELSE
        CALL DrXLinGrid (NthTic)
      END IF
      IF (grstat(cwin).LogY) THEN
        CALL DrYLogGrid (NthTic)
      ELSE
        CALL DrYLinGrid (NthTic)
      END IF
      END !SUBROUTINE



      SUBROUTINE DrawGridX (NthTic)
      INTEGER NthTic
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      IF (grstat(cwin).LogX) THEN
        CALL DrXLogGrid(NthTic)
      ELSE
        CALL DrXLinGrid(NthTic)
      END IF
      END !SUBROUTINE



      SUBROUTINE DrawGridY (NthTic)
      INTEGER NthTic
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      IF (grstat(cwin).LogY) THEN
        CALL DrYLogGrid(NthTic)
      ELSE
        CALL DrYLinGrid(NthTic)
      END IF
      END !SUBROUTINE




      SUBROUTINE DrawXAxis (TicSpace, dir)
      REAL TicSpace
      INTEGER dir
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      IF (grstat(cwin).LogX) THEN
        CALL DrLogXAx(dir)
      ELSE
        CALL DrLinXAx(TicSpace, dir)
      END IF
      END !SUBROUTINE

      SUBROUTINE DrawYAxis (TicSpace, dir)
      REAL TicSpace
      INTEGER dir
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      IF (grstat(cwin).LogY) THEN
        CALL DrLogYAx(dir)
      ELSE
        CALL DrLinYAx(TicSpace, dir)
      END IF
      END !SUBROUTINE


      SUBROUTINE FindCMMInMax (cm, rows, columns, minzval, maxzval)
      INCLUDE 'GRAFTYPE.FOR'
      REAL cm(0:maxcontourX, 0:maxcontourY)
      INTEGER rows, columns
      REAL minzval, maxzval
      INTEGER i, j

      minzval = cm(0, 0)
      maxzval = cm(0, 0)
      DO i = 0, columns - 1
        DO j = 0, rows - 1
          IF (cm(i, j) .LT. minzval)  minzval = cm(i, j)
          IF (cm(i, j) .GT. maxzval)  maxzval = cm(i, j)
        END DO
      END DO
      END !SUBROUTINE



      SUBROUTINE FindMinMax (dataset, numdat, minval, maxval)
      INCLUDE 'STDHDR.FOR'
      REAL dataset(0:maxv), minval, maxval
      INTEGER numdat, i

      minval = dataset(0)
      maxval = dataset(0)
      DO i = 0, numdat - 1
        IF (dataset(i) .LT. minval)  minval = dataset(i)
        IF (dataset(i) .GT. maxval)  maxval = dataset(i)
      END DO
      END !SUBROUTINE





      SUBROUTINE GroupPlotData (datasetx, GroupData,
     +   numdat, numgroup, GraphType, barwid, fill)
      INCLUDE 'GRAFTYPE.FOR'
      INCLUDE 'STDHDR.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL datasetx(0:maxv), GroupData(0:maxgroup,0:maxv)
      RECORD /GroupInfoRec/ fill( 0:maxgroup)
      INTEGER numdat, numgroup, GraphType, i, j, OldColor, iErr
      REAL barwid, xx1, yy1, xx2, yy2
      REAL tx[ALLOCATABLE](:),SumVector[ALLOCATABLE](:)
      REAL tempg[ALLOCATABLE](:), txg[ALLOCATABLE](:,:)

      ALLOCATE(tx(0:maxV),SumVector(0:maxV),tempg(0:maxv), STAT=iErr)
      CALL CheckMem(iErr)
      ALLOCATE(txg(0:maxgroup,0:maxv), STAT = iErr)
      CALL CheckMem(iErr)

      CALL GetColXX(OldColor)
      CALL CopyVectors(datasetx, tx, numdat)
      IF (GraphType .LT. 0 .OR. GraphType .GT. 2) GraphType = 1
      DO i = 0, numdat - 1
        SumVector(i) = 0.0
      END DO
      DO i = 0, numgroup - 1
        CALL Copy2DVectors(GroupData, tempg, numdat,i)
        CALL PrePlot(tx, tempg, numdat)
        Call Copy1to2D(txg,tempg,numdat,i)
      END DO
      SELECT CASE (GraphType)
         CASE (0)
          DO i = 0, numgroup - 1
            DO j = 0, numdat - 1
              SumVector(j) = SumVector(j) + txg(i,j)
            END DO
            CALL LinePlotData(tx, SumVector, numdat,
     +            fill(i).GroupColor, fill(i).GroupHatch)
          END DO
         CASE (1)
           DO j = 0, numdat - 1
             xx1 = tx(j) - barwid / 2.0
             xx2 = xx1 + barwid
             DO i = 0, numgroup - 1
              IF (grstat(cwin).plotclip.bottom .GE. SumVector(j)) THEN
                yy1 = grstat(cwin).plotclip.bottom
              ELSE
                yy1 = SumVector(j)
              END IF
              SumVector(j) = SumVector(j) + txg(i,j)
              yy2 = SumVector(j)
              CALL SetFillStyleXX(fill(i).GroupHatch,
     +                            fill(i).GroupColor)
              CALL BarWorld(xx1, yy1, yy2 - yy1, xx2 - xx1,
     +              fill(i).GroupColor, fill(i).GroupHatch)
            END DO
          END DO
         CASE (2)
          DO j = 0, numdat - 1
            DO i = 0, numgroup - 1
              xx1 = (tx(j) - barwid / 2.0) + i *
     +              (barwid /REAL( numgroup))
              xx2 = xx1 + barwid /REAL( numgroup)
              IF (grstat(cwin).plotclip.top .GE. 0.0 .AND.
     +           grstat(cwin).plotclip.bottom .GE. 0.0)
     +            yy1 = grstat(cwin).plotclip.bottom
              IF (grstat(cwin).plotclip.top .GE. 0.0 .AND.
     +           grstat(cwin).plotclip.bottom .LT. 0.0)  yy1 = 0.0
              IF (grstat(cwin).plotclip.top .LE. 0.0 .AND.
     +           grstat(cwin).plotclip.bottom .LE. 0.0)
     +              yy1 = grstat(cwin).plotclip.top
              yy2 = txg(i,j)
              CALL SetFillStyleXX(fill(i).GroupHatch,
     +                            fill(i).GroupColor)
              CALL BarWorld(xx1, yy1, yy2 - yy1, xx2 - xx1,
     +               fill(i).GroupColor, fill(i).GroupHatch)
            END DO
          END DO
      END SELECT
      CALL PostPlot

      CALL SelectColor(OldColor)
      DEALLOCATE(tx,SumVector,tempg,STAT=iErr)
      CALL CheckDealloc(iErr)
      DEALLOCATE(txg,STAT=iErr)
      CALL CheckDealloc(iErr)
      END !SUBROUTINE

      SUBROUTINE InitString( s)
      INTEGER i
      CHARACTER * 80 s
      DO i = 1, 80
        s(i:i) =  ' '
      END DO
      END !FUNCTION


      SUBROUTINE InitSEGraphics (mode, fontpath )
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, mode
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL LogSCFactor(1:10)
      CHARACTER * (*) fontpath
      COMMON /LogSC/ LogSCFactor

      LogSCFactor(1) = 0.0
      LogSCFactor(2) = 0.301
      LogSCFactor(3) = 0.4771
      LogSCFactor(4) = 0.6021
      LogSCFactor(5) = 0.699
      LogSCFactor(6) = 0.7782
      LogSCFactor(7) = 0.8451
      LogSCFactor(8) = 0.9031
      LogSCFactor(9) = 0.9542
      LogSCFactor(10) = 1.0
      CALL SetTextJustifyXX(0, 0)
      CALL OneTimeInit (mode, fontpath)
      DO i = 1, 10
        CALL SetWorldRect(grstat(i).win2plotratio,
     +                    0.166, 0.166, 0.166, 0.166)
      END DO

      CALL SetPercentWindow(0.1, 0.1, 0.9, 0.9, 1)
      CALL SetWin2PlotRatio(1, 0.19, 0.12, 0.05, 0.14)

      CALL SetPercentWindow(0.02, 0.02, 0.98, 0.98, 2)
      CALL SetWin2PlotRatio(2, 0.19, 0.12, 0.05, 0.14)

      CALL SetPercentWindow(0.02, 0.02, 0.98, 0.49, 3)
      CALL SetWin2PlotRatio(3, 0.19, 0.14, 0.05, 0.2)

      CALL SetPercentWindow(0.02, 0.51, 0.98, 0.98, 4)
      CALL SetWin2PlotRatio(4, 0.19, 0.14, 0.05, 0.2)

      CALL SetPercentWindow(0.02, 0.02, 0.49, 0.98, 5)
      CALL SetWin2PlotRatio(5, 0.23, 0.14, 0.06, 0.14)

      CALL SetPercentWindow(0.51, 0.02, 0.98, 0.98, 6)
      CALL SetWin2PlotRatio(6, 0.23, 0.14, 0.06, 0.14)

      CALL SetPercentWindow(0.02, 0.02, 0.49, 0.49, 7)
      CALL SetWin2PlotRatio(7, 0.23, 0.19, 0.08, 0.2)

      CALL SetPercentWindow(0.51, 0.02, 0.98, 0.49, 8)
      CALL SetWin2PlotRatio(8, 0.23, 0.19, 0.08, 0.2)

      CALL SetPercentWindow(0.02, 0.51, 0.49, 0.98, 9)
      CALL SetWin2PlotRatio(9, 0.23, 0.19, 0.08, 0.2)

      CALL SetPercentWindow(0.51, 0.51, 0.98, 0.98, 10)
      CALL SetWin2PlotRatio(10, 0.23, 0.19, 0.08, 0.2)


      CALL ScalePlotArea(0.0, 0.0, 100.0, 100.0)

      END !SUBROUTINE



      SUBROUTINE LabelGraphWindow (x, y, GrLabel, xjust, yjust)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin,xjust, yjust
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL x,y
      CHARACTER * (*) GrLabel
      CHARACTER * 80 TempLabel
      RECORD /WorldRect/ d

      CALL InitString(TempLabel )
      TempLabel = GrLabel
      CALL SetWorldRect(d, 0.0, 0.0, 1000.0, 1000.0)
      CALL SetWorldCoordinates(d)
      CALL SetTextJustifyXX(xjust, yjust)
      CALL moveworldabs(x, y)
      CALL OutTextXX( TempLabel)
      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      CALL SetTextJustifyXX(lefttext, centertext)
      END !SUBROUTINE



      SUBROUTINE LabelPlotArea (x, y, GrLabel, xjust, yjust)
      REAL x, y, xx1, xx2, yy1, yy2
      CHARACTER * (*) GrLabel
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, xjust, yjust, i
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      RECORD /WorldRect/ w1, w2, w3
      CHARACTER * 80 TempLabel

      DO i = 1, 80
        TempLabel(i:i) = ' '
      END DO
      TempLabel = GrLabel
      w3 = grstat(cwin).plotworld
      w2 = grstat(cwin).plotclip
      IF (grstat(cwin).LogX) THEN
        xx1 = log10(grstat(cwin).plotclip.left)
        xx2 = log10(grstat(cwin).plotclip.right)
        x = log10(x)
      ELSE
        xx1 = grstat(cwin).plotclip.left
        xx2 = grstat(cwin).plotclip.right
      END IF
      IF (grstat(cwin).LogY) THEN
        yy1 = log10(grstat(cwin).plotclip.bottom)
        yy2 = log10(grstat(cwin).plotclip.top)
        y = log10(y)
      ELSE
        yy1 = grstat(cwin).plotclip.bottom
        yy2 = grstat(cwin).plotclip.top
      END IF
      CALL SetGraphViewport(grstat(cwin).plotrect.left,
     +     grstat(cwin).plotrect.top, grstat(cwin).plotrect.right,
     +     grstat(cwin).plotrect.bottom)
      CALL SetWorldRect(w1, xx1, yy1, xx2, yy2)
      CALL SetWorldCoordinates(w1)
      CALL SetTextJustifyXX(xjust, yjust)
      CALL moveworldabs(x, y)
      CALL OutTextXX(TempLabel)
      grstat(cwin).plotworld = w3
      grstat(cwin).plotclip = w2
      CALL SetGraphViewport(grstat(cwin).drawingRect.left,
     +  grstat(cwin).drawingRect.top, grstat(cwin).drawingRect.right,
     +  grstat(cwin).drawingRect.bottom)
      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      CALL SetTextJustifyXX(0, 0)
      END !SUBROUTINE

      SUBROUTINE LabelXAxis (NthTic, dir)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, NthTic, dir
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      IF (dir .EQ. 0) THEN
        CALL SetTextJustifyXX(1, 2)
      ELSE
        CALL SetTextJustifyXX(1, 0)
      END IF
      IF (grstat(cwin).LogX) THEN
        CALL LabLogXAx(dir)
      ELSE
        CALL LabLinXAx(NthTic, dir)
      END IF
      CALL SetTextJustifyXX(0, 0)
      END !SUBROUTINE

      SUBROUTINE LabelXAxWithStrings (NthTic,TicStrings,NumStrings,dir)
      INCLUDE 'GRAFTYPE.FOR'
      REAL xx1
      CHARACTER* 80 TicStrings(0:20)
      INTEGER i, NthTic, NumStrings, dir, cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      IF (dir .EQ. 0) THEN
        CALL SetTextJustifyXX(1, 2)
      ELSE
        CALL SetTextJustifyXX(1, 0)
      END IF
      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      grstat(cwin).tsx = grstat(cwin).ticspacex * NthTic
      xx1 = grstat(cwin).plotclip.left + grstat(cwin).tsx
      DO i = 0, NumStrings - 1
       CALL LabelTicXString(xx1, grstat(cwin).yint, TicStrings(i), dir)
        xx1 = xx1 + grstat(cwin).tsx
      END DO
      CALL SetTextJustifyXX(0, 0)
      END !SUBROUTINE

      SUBROUTINE LabelYAxis (NthTic, dir)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, NthTic, dir
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      IF (dir .EQ. 0) THEN
        CALL SetTextJustifyXX(2, 1)
      ELSE
        CALL SetTextJustifyXX(0, 1)
      END IF
      IF (grstat(cwin).LogY) THEN
        CALL LabLogYAx(dir)
      ELSE
        CALL LabLinYAx(NthTic, dir)
      END IF
      CALL SetTextJustifyXX(0, 0)
      END !SUBROUTINE



      SUBROUTINE LinePlotData (datasetx, datasety,
     +               numdat, newcolor, linestyle)
      INCLUDE 'GRAFTYPE.FOR'
      INCLUDE 'STDHDR.FOR'
      REAL datasetx(0:maxv), datasety(0:maxv)
      INTEGER cwin,numdat, newcolor, linestyle, OldColor, iErr
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL tx[ALLOCATABLE](:),ty[ALLOCATABLE](:)


      ALLOCATE(tx(0:maxV),ty(0:maxV),STAT=iErr)
      CALL CheckMem(iErr)

      CALL GetColXX(OldColor)
      CALL SelectColor(newcolor)
      CALL SetLineStyleXX(linestyle,  1)

      CALL CopyVectors(datasetx, tx, numdat)
      CALL CopyVectors(datasety, ty, numdat)

      CALL PrePlot(tx, ty, numdat)
      CALL polyLineWorldAbs(tx, ty, numdat)
      CALL PostPlot

      CALL SetLineStyleXX(0, 1)
      CALL SelectColor(OldColor)
      DEALLOCATE(tx,ty,STAT=iErr)
      CALL CheckDealloc(iErr)
      END !SUBROUTINE



      SUBROUTINE PieChart (xdata, numgroup, gcolors,titles, pietype,
     +                    PieVal, per, explode)
      INCLUDE 'GRAFTYPE.FOR'
      INCLUDE 'STDHDR.FOR'
      REAL xw, yw, xdata(0:maxr),radius,Twopi360, xc, yc, sum, AspectR
      REAL xcenter, ycenter, xtitle, ytitle, radius1, mid2pi360,mul
      REAL startangle, endangle, midangle, startcenter, percent, xplode
      CHARACTER * 80 tempstr,numstr, percentstr, titles(0:maxgroup)
      INTEGER  gmx , gmy, numgroup, pietype, cwin, j
      LOGICAL pieval, per
      RECORD /GroupInfoRec/ gcolors(0:maxgroup)
      RECORD /explodeRec/ explode(0:maxgroup)
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin


      sum = 0.0
      startangle = 0.0
      endangle = 0.0
      percent = 0.0
      Twopi360 = (2.0 * pi) / 360.0
      CALL GetMaxCoords(gmx, gmy)
      AspectR = (gmy * 1.3) /REAL( gmx)
      CALL SetTextJustifyXX(0, 1)
      IF (pietype .EQ. 0) THEN
        startcenter = 2.0
      ELSE
        startcenter = 3.0
      END IF
      xw = ABS(grstat(cwin).plotrect.right -
     +         grstat(cwin).plotrect.left)
      yw = ABS(grstat(cwin).plotrect.bottom -
     +          grstat(cwin).plotrect.top)
      xc = (grstat(cwin).plotrect.left -
     +      grstat(cwin).drawingrect.left) + xw/ startcenter
      yc = (grstat(cwin).drawingrect.bottom -
     +      grstat(cwin).plotrect.bottom) + yw/ 2.0
      IF (xc .GT. yc) THEN
        radius = 0.17 * xw
      ELSE
        radius = 0.17 * yw
      END IF
      DO i = 0, numgroup - 1
        sum = sum + xdata(i)
      END DO
      DO i = 0, numgroup - 1
        xcenter = xc
        ycenter = yc
        DO j = 1, 80
          numstr(j:j) = '  '
          percentstr(j:j) = ' '
          tempstr(j:j) = ' '
        END DO
        CALL SelectColor(gcolors(i).GroupColor)
        CALL SetFillStyleXX(gcolors(i).GroupHatch,
     +                      gcolors(i).GroupColor)
        startangle = endangle + 1.0
        IF (i .EQ. 0)   startangle = 0.0
        endangle = (endangle + ((xdata(i) / sum) * 360.0))
        midangle = (startangle + endangle) / 2.0
        percent = (xdata(i) / sum) * 100.0

        IF (per) THEN
           CALL RealToString( percent, 2, 1, percentstr)
        END IF
        IF (PieVal) THEN
           CALL ConvertNum(xdata(i), 0, sum,
     +                     sum / 180.0, .FALSE., numstr)
           IF (per)  CALL Combine( numstr, '%')
        END IF
        tempstr = numstr
        CALL Combine(tempstr,percentstr)
        percentstr = tempstr

        IF (startangle .GT. 359.0)  startangle = 359.0
        IF (endangle .GT. 360.0)    endangle = 360.0
        mid2pi360 = Twopi360 * midangle
        IF (explode(i).explodetrue) THEN
          xplode = explode(i).percent * radius
          xcenter = xc + (xplode * COS(mid2pi360))
          ycenter = yc + (xplode * AspectR * SIN(mid2pi360))
        END IF
        CALL PieXX(xcenter, ycenter, startangle, endangle,
     +             radius, AspectR)
        radius1 = radius * 1.3
        xtitle = xcenter + (radius1 * COS(mid2pi360))
        ytitle = ycenter + (AspectR * radius1 * SIN(mid2pi360))
        IF (midangle .GT. 90.0 .AND. midangle .LE. 270.0) THEN
          CALL SetTextJustifyXX(2, 1)
        ELSE
          CALL SetTextJustifyXX(0, 1)
        END IF
        IF (pietype .EQ. 0) THEN
          IF (midangle .GT. 30 .AND. midangle .LT. 150) THEN
             mul = 1
          ELSE
             mul = 0
          END IF
          ytitle = ytitle + 9 * mul
          CALL OutTextPie(xtitle, ytitle, titles(i))
          CALL OutTextPie(xtitle, ytitle - 8, percentstr)
        ELSE
          CALL OutTextPie(xtitle, ytitle, percentstr)
        END IF
        CALL SetTextJustifyXX(0, centertext)

      END DO
      IF (pietype .EQ. 1) CALL PieLegend(titles, numgroup, gcolors)

      END !SUBROUTINE


      SUBROUTINE PieLegend (titles, numgroup, gcolors)
      INCLUDE 'GRAFTYPE.FOR'
      CHARACTER * 80 titles(0:20)
      INTEGER  numgroup, i
      REAL textstart, boxsize, boxstart
      RECORD /GroupInfoRec/ gcolors(0:maxgroup)
      RECORD /WorldRect/ d
      REAL ypos

      textstart = 800.0
      boxsize = 40.0
      boxstart = 725.0

      CALL SetWorldRect(d, 0.0, 0.0, 1000.0, 1000.0)
      CALL SetWorldCoordinates(d)
      CALL SetTextJustifyXX(lefttext, 0)
      CALL SelectColor(15)
      CALL moveworldabs(700.0, 200.0)
      CALL lineworldabs(700.0, 800.0)
      CALL lineworldabs(999.0, 800.0)
      CALL lineworldabs(999.0, 200.0)
      CALL lineworldabs(700.0, 200.0)
      ypos = 725
      DO i = 0, numgroup - 1
        CALL SelectColor(15)
        CALL moveworldabs(textstart, ypos)
        CALL OutTextXX(titles(i))
        CALL SetFillStyleXX(gcolors(i).GroupHatch,
     +                      gcolors(i).GroupColor)
        CALL BarWorld(boxstart, ypos, boxsize, boxsize,
     +         gcolors(i).GroupColor, gcolors(i).GroupHatch)
        ypos = ypos - (500.0 /REAL( numgroup))
      END DO
      CALL SetTextJustifyXX(lefttext, 0)

      END !SUBROUTINE

      SUBROUTINE PlotErrorBars (datasetx, datasety1, datasety2,
     +                          numdat, barwid, newcolor)
      INCLUDE 'GRAFTYPE.FOR'
      INCLUDE 'STDHDR.FOR'
      INTEGER cwin, numdat, newcolor, i, OldColor, iErr
      REAL datasetx(0:maxv), datasety1(0:maxv), datasety2(0:maxv)

      REAL barwid, xx1, yy1, xx2, yy2
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL tx[ALLOCATABLE](:),ty1[ALLOCATABLE](:),ty2[ALLOCATABLE](:)

      ALLOCATE(tx(0:maxV),ty1(0:maxV),ty2(0:maxV),STAT=iErr)
      CALL CheckMem(iErr)
      CALL GetColXX(OldColor)
      CALL SelectColor(newcolor)

      CALL CopyVectors(datasetx, tx, numdat)
      CALL CopyVectors(datasety1, ty1, numdat)
      CALL CopyVectors(datasety2, ty2, numdat)
      CALL PrePlot(tx, ty1, numdat)

      IF (grstat(cwin).LogY) THEN
        DO i = 0, numdat - 1
           ty2(i) = log10(ty2(i))
        END DO
      END IF
      IF (grstat(cwin).LogX)
     +    barwid = (log10(grstat(cwin).plotclip.right) -
     +              log10(grstat(cwin).plotclip.left)) / (numdat*1.1)
      DO i = 0, numdat - 1
        yy1 = ty1(i)
        yy2 = ty2(i)
        xx1 = tx(i) - barwid / 2.0
        xx2 = xx1 + barwid
        CALL moveworldabs(xx1, yy1)
        CALL lineworldabs(xx2, yy1)
        CALL moveworldabs(xx1, yy2)
        CALL lineworldabs(xx2, yy2)
      END DO
      CALL PostPlot
      CALL SelectColor(OldColor)
      DEALLOCATE(tx,ty1,ty2,STAT=iErr)
      CALL CheckDealloc(iErr)
      END !SUBROUTINE




      SUBROUTINE PostPlot
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      RECORD /WorldRect/ Worlda, Worldb
      COMMON /GrAttr/ grstat, cwin
      COMMON /GlobalWR/ Worlda, Worldb

      grstat(cwin).plotworld = Worlda
      grstat(cwin).plotclip = Worldb
      CALL SetGraphViewport(grstat(cwin).drawingRect.left,
     +  grstat(cwin).drawingRect.top, grstat(cwin).drawingRect.right,
     +  grstat(cwin).drawingRect.bottom)
      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      END !SUBROUTINE






      SUBROUTINE PrePlot (datasetx, datasety, numdat)
      INCLUDE 'GRAFTYPE.FOR'
      INCLUDE 'STDHDR.FOR'
      REAL datasetx(0:maxv), datasety(0:maxv), xx1, yy1, xx2, yy2
      INTEGER cwin, numdat,i
      RECORD /grstype/ grstat(1:10)
      RECORD /WorldRect/ Wr, Worlda, Worldb
      COMMON /GrAttr/ grstat, cwin
      COMMON /GlobalWR/ Worlda, Worldb

      Worlda = grstat(cwin).plotworld
      Worldb = grstat(cwin).plotclip
      IF (grstat(cwin).LogX) THEN
        xx1 = log10(grstat(cwin).plotclip.left)
        xx2 = log10(grstat(cwin).plotclip.right)
        DO i = 0, numdat - 1
          datasetx(i) = log10(ABS(datasetx(i)))
        END DO
      ELSE
        xx1 = grstat(cwin).plotclip.left
        xx2 = grstat(cwin).plotclip.right
      END IF
      IF (grstat(cwin).LogY) THEN
        yy1 = log10(grstat(cwin).plotclip.bottom)
        yy2 = log10(grstat(cwin).plotclip.top)
        DO i = 0, numdat - 1
          datasety(i) = log10(ABS(datasety(i)))
        END DO
      ELSE
        yy1 = grstat(cwin).plotclip.bottom
        yy2 = grstat(cwin).plotclip.top
      END IF
      CALL SetWorldRect(wr, xx1, yy1, xx2, yy2)
      CALL SetGraphViewport(grstat(cwin).plotrect.left,
     +   grstat(cwin).plotrect.top, grstat(cwin).plotrect.right,
     +   grstat(cwin).plotrect.bottom)
      CALL SetWorldCoordinates(wr)
      END !SUBROUTINE

      SUBROUTINE RealLegends (rv, colorMap, n, barLine)
      INCLUDE 'GRAFTYPE.FOR'
      REAL rv(0:MaxLegends)
      RECORD /GroupInfoRec/ ColorMap(0:MaxLegends)
      INTEGER  i, n, barline
      CHARACTER * 80 sv(0:20)

      DO i = 0, n - 1
           CALL RealToString(rv(i), 1, 1, sv(i))
      END DO
      CALL StringLegends(sv, colorMap, n, barLine)
      END !SUBROUTINE


      SUBROUTINE RoundAxes (a1, a2, tics)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL a1, a2, tics, dr1, dr2, px2, pc2
      INTEGER di1, di2, digits, a1neg, a2neg


      IF (a1 .LT. 0 ) THEN
        a1neg = 1
      ELSE
        a1neg = 0
      END IF
      IF (a2 .GT. 0) THEN
        a2neg = 1
      ELSE
        a2neg = 0
      END IF

      IF (NumExp(a2) .GT. NumExp(a1)) THEN
        px2 = REAL(NumExp(a2)) - 1.0
      ELSE
        px2 = REAL(NumExp(a1)) - 1.0
      END IF
      pc2 = PowerCalc(10.0, px2)
      dr2 = a2 / pc2
      dr1 = a1 / pc2

      di2 = AINT(dr2 + (dr2 - dr1) * .05)
      IF (di2 .GT. 0 .AND.  dr2 .LE. 0.0)  di2 = 0

      di1 = AINT(dr1 - (dr2 - dr1) * .05)
      IF (di1 .LT. 0  .AND. dr1 .GE. 0.0)  di1 = 0

      IF (ABS(di2) .LT. 5) THEN
        di2 = di2 + a2neg
      ELSEIF (ABS(di2) .LT. 60) THEN
        di2 = ((di2 / 5) + a2neg) * 5
      ELSE
        di2 = ((di2 / 10) + a2neg) * 10
      END IF
      IF (ABS(di1) .LT. 5) THEN
        di1 = di1 - a1neg
      ELSEIF (ABS(di1) .LT. 60) THEN
        di1 = ((di1 / 5) - a1neg) * 5
      ELSE
        di1 = ((di1 / 10) - a1neg) * 10
      END IF
      a1 = REAL(di1) * pc2
      a2 = REAL(di2) * pc2
      digits = ABS(di2 - di1)
      IF (digits .LT. 10) THEN
        tics = 1.0
      ELSEIF (digits .LT. 20)  THEN
        tics = 2.0
      ELSEIF (digits .LT. 50)  THEN
        tics = 5.0
      ELSEIF (digits .LT. 80)  THEN
        tics = 10.0
      ELSEIF (digits .LT. 100) THEN
        tics = 20.0
      ELSEIF (digits .LT. 151) THEN
        tics = 20.0
      ELSEIF (digits .LT. 200) THEN
        tics = 50.0
      ELSE
        tics = 100.0
      END IF
      tics = tics * (pc2) / 10.0
      END !SUBROUTINE

      SUBROUTINE ScaleLinX (xx1, xx2)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      REAL xx1, xx2
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetGraphAreaWorld(xx1, grstat(cwin).plotclip.bottom,
     +                       xx2, grstat(cwin).plotclip.top)
      grstat(cwin).LogX = .FALSE.
      END !SUBROUTINE


      SUBROUTINE ScalePlotArea (xx1, yy1, xx2, yy2)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      REAL xx1, yy1, xx2, yy2
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

       IF (grstat(cwin).LogX) THEN
         CALL ScaleLogX(xx1, xx2)
       ELSE
         CALL ScaleLinX(xx1, xx2)
       END IF
       IF (grstat(cwin).LogY) THEN
         CALL ScaleLogY(yy1, yy2)
       ELSE
         CALL ScaleLinY(yy1, yy2)
       END IF

      END !SUBROUTINE

      SUBROUTINE ScatterPlotData (datasetx, datasety,
     +                           numdat, newcolor, markType)
      INCLUDE 'STDHDR.FOR'
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, numdat,newcolor,markType, i, k, OldColor, iErr
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      REAL MarkerX(0: 2, 0: 6),MarkerY(0: 2, 0: 6)
      REAL datasetx(0:maxv), datasety(0:maxv)
      REAL pfsfx, pfsfy, pfx(0: 6), pfy(0: 6)
      REAL tx[ALLOCATABLE](:),ty[ALLOCATABLE](:)

      ALLOCATE(tx(0:maxV),ty(0:maxV),STAT=iErr)
      CALL CheckMem(iErr)

      MarkerX(0, 0) = -.5
      MarkerX(0, 1) = 0.0
      MarkerX(0, 2) = 1.0
      MarkerX(0, 3) = 0.0
      MarkerX(0, 4) = -1.0
      MarkerX(0, 5) = 0.0
      MarkerX(0, 6) = 0.0

      MarkerX(1, 0) = -.5
      MarkerX(1, 1) = .5
      MarkerX(1, 2) = .5
      MarkerX(1, 3) = -1.0
      MarkerX(1, 4) = 0.0
      MarkerX(1, 5) = 0.0
      MarkerX(1, 6) = 0.0

      MarkerX(2, 0) = 0.0
      MarkerX(2, 1) = -.5
      MarkerX(2, 2) = .5
      MarkerX(2, 3) = .5
      MarkerX(2, 4) = -.5
      MarkerX(2, 5) = 0.0
      MarkerX(2, 6) = 0.0

      MarkerY(0, 0) = -.5
      MarkerY(0, 1) = 1.0
      MarkerY(0, 2) = 0.0
      MarkerY(0, 3) = -1.0
      MarkerY(0, 4) = 0.0
      MarkerY(0, 5) = 0.0
      MarkerY(0, 6) = 0.0

      MarkerY(1, 0) = -.5
      MarkerY(1, 1) = 1.0
      MarkerY(1, 2) = -1.0
      MarkerY(1, 3) = 0.0
      MarkerY(1, 4) = 0.0
      MarkerY(1, 5) = 0.0
      MarkerY(1, 6) = 0.0

      MarkerY(2, 0) = -.5
      MarkerY(2, 1) = .5
      MarkerY(2, 2) = .5
      MarkerY(2, 3) = -.5
      MarkerY(2, 4) = -.5
      MarkerY(2, 5) = 0.0
      MarkerY(2, 6) = 0.0


      CALL GetColXX(OldColor)
      CALL SelectColor(newcolor)
      CALL CopyVectors(datasetx, tx, numdat)
      CALL CopyVectors(datasety, ty, numdat)

      IF (markType .GT. 2) THEN markType = 0
      CALL PrePlot(tx, ty, numdat)
      IF (grstat(cwin).LogX) THEN
        pfsfx = (log10(grstat(cwin).plotclip.right) -
     +           log10(grstat(cwin).plotclip.left)) * .02
      ELSE
        pfsfx = (grstat(cwin).plotclip.right -
     +           grstat(cwin).plotclip.left) * .02
      END IF
      IF (grstat(cwin).LogY) THEN
        pfsfy = (log10(grstat(cwin).plotclip.top) -
     +           log10(grstat(cwin).plotclip.bottom)) * .02
      ELSE
        pfsfy = (grstat(cwin).plotclip.top -
     +           grstat(cwin).plotclip.bottom) * .02
      END IF

      DO i = 0, 6
        pfx(i) = MarkerX(markType, i) * pfsfx
        pfy(i) = MarkerY(markType, i) * pfsfy
      END DO
      k = 5
      DO i = 0, numdat - 1
        CALL moveworldabs(tx(i), ty(i))
        CALL polyLineWorldRel(pfx, pfy, k)
      END DO
      CALL PostPlot
      CALL SelectColor(OldColor)
      DEALLOCATE(tx,ty,STAT=iErr)
      CALL CheckDealloc(iErr)
      END !SUBROUTINE

      SUBROUTINE SetAxesType (PlotTypeX, PlotTypeY)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, PlotTypeX, PlotTypeY
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      IF (PlotTypeX .EQ. 1) THEN
        grstat(cwin).LogX = .TRUE.
      ELSE
        grstat(cwin).LogX = .FALSE.
      END IF
      IF (PlotTypeY .EQ. 1) THEN
        grstat(cwin).LogY = .TRUE.
      ELSE
        grstat(cwin).LogY = .FALSE.
      END IF
      END !SUBROUTINE

      SUBROUTINE SetCurrentWindow (win)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, win
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      cwin = win
      CALL SetGraphWindow
      END !SUBROUTINE


      SUBROUTINE SetGraphWindow
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetGraphViewport(grstat(cwin).drawingRect.left,
     +  grstat(cwin).drawingRect.top, grstat(cwin).drawingRect.right,
     +  grstat(cwin).drawingRect.bottom)
      END !SUBROUTINE

      SUBROUTINE SetPercentWindow (x1, y1, x2, y2, win)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, win, maxX, maxY
      REAL x1, y1, x2, y2
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL GetMaxCoords(maxX, maxY)
      CALL DefGraphWindow(NINT(x1 * maxX), NINT(y1 * maxY),
     +                    NINT(x2 * maxX), NINT(y2 * maxY), win)
      END !SUBROUTINE

      SUBROUTINE SetPlotBackground (c)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, c
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetFillStyleXX(1, c)
      CALL SetGraphViewport(grstat(cwin).plotrect.left,
     +  grstat(cwin).plotrect.top, grstat(cwin).plotrect.right,
     +  grstat(cwin).plotrect.bottom)
      CALL BarXX(0.0, 0.0, REAL(grstat(cwin).plotrect.right -
     +   grstat(cwin).plotrect.left),
     +    REAL( grstat(cwin).plotrect.bottom -
     +   grstat(cwin).plotrect.top))
      CALL SetGraphViewport(grstat(cwin).drawingRect.left,
     + grstat(cwin).drawingRect.top, grstat(cwin).drawingRect.right,
     + grstat(cwin).drawingRect.bottom)
      END !SUBROUTINE

      SUBROUTINE SetRect (Worldr, xx1, yy1, xx2, yy2)
      INCLUDE 'GRAFTYPE.FOR'
      RECORD /Rect/ Worldr
      INTEGER xx1, yy1, xx2, yy2

      WorldR.left = xx1
      WorldR.top = yy1
      WorldR.right = xx2
      WorldR.bottom = yy2
      END !SUBROUTINE

      SUBROUTINE SetViewBackground (c)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin,c
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      CALL SetGlobalView(c)
      CALL SetFillStyleXX(2, c)
      CALL BarXX(0.0, 0.0,REAL(grstat(cwin).drawingRect.right -
     + grstat(cwin).drawingRect.left),
     + REAL(grstat(cwin).drawingRect.bottom-
     + grstat(cwin).drawingRect.top))
      END !SUBROUTINE

      SUBROUTINE SetWin2PlotRatio (win, l, t, r, b)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER win, cwin
      REAL l,t,r,b
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      grstat(win).win2plotratio.left = l
      grstat(win).win2plotratio.right = r
      grstat(win).win2plotratio.top = t
      grstat(win).win2plotratio.bottom = b
      grstat(win).plotrect.left = grstat(win).drawingRect.left+
     + NINT((grstat(win).drawingRect.right -
     + grstat(win).drawingRect.left) *grstat(win).win2plotratio.left)
      grstat(win).plotrect.bottom = grstat(win).drawingRect.bottom -
     +  NINT((grstat(win).drawingRect.bottom -
     +  grstat(win).drawingRect.top) *grstat(win).win2plotratio.bottom)
      grstat(win).plotrect.right = grstat(win).drawingRect.right -
     +  NINT((grstat(win).drawingRect.right -
     +  grstat(win).drawingRect.left) *grstat(win).win2plotratio.right)
      grstat(win).plotrect.top = grstat(win).drawingRect.top +
     +  NINT((grstat(win).drawingRect.bottom -
     +  grstat(win).drawingRect.top) *grstat(win).win2plotratio.top)
      END !SUBROUTINE

      SUBROUTINE SetXYIntercepts (xx1, yy1)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin
      REAL xx1, yy1
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin

      grstat(cwin).xint = xx1
      grstat(cwin).yint = yy1
      END !SUBROUTINE

      SUBROUTINE SortData (x, y, n, d)
      INCLUDE 'STDHDR.FOR'
      INTEGER j, k, n, d, iErr
      LOGICAL abort
      REAL TempX, TempY, x(0:maxv), y(0:maxv)
      REAL TempArray[ALLOCATABLE](:)

      ALLOCATE(TempArray(0:maxV),STAT=iErr)
      CALL CheckMem(iErr)

      IF (n .GT. 1) THEN
        DO j = 0, n - 1
          abort = .FALSE.
          TempX = x(j)
          TempY = y(j)
          k = j - 1
          DO WHILE ((.NOT. abort) .AND. (k .GE. 0))
            IF (TempX .LT. x(k)) THEN
              x(k + 1) = x(k)
              y(k + 1) = y(k)
              k = k - 1
            ELSE
              abort = .TRUE.
            END IF
          END DO
          x(k + 1) = TempX
          y(k + 1) = TempY
        END DO

        IF (d .EQ. 0) THEN
          DO j = 0, n - 1
            TempArray(j) = x(j)
          END DO
          DO j = n - 1, 0 , -1
            x(j) = TempArray(n - 1 - j)
          END DO
          DO j = 0, n - 1
            TempArray(j) = y(j)
          END DO
          DO j = n - 1, 0 , -1
            y(j) = TempArray(n - 1 - j)
          END DO
        END IF
      END IF
      DEALLOCATE(TempArray,STAT=iErr)
      CALL CheckDealloc(iErr)
      END !SUBROUTINE

      SUBROUTINE SortDataX (x, y, n, d)
      INCLUDE 'STDHDR.FOR'
      INTEGER  n, d
      REAL x(0:maxv), y(0:maxv)

      CALL SortData(x, y, n, d)
      END !SUBROUTINE

      SUBROUTINE SortDataY (x, y, n, d)
      INCLUDE 'STDHDR.FOR'
      INTEGER  n, d
      REAL x(0:maxv), y(0:maxv)

      CALL SortData(y, x, n, d)
      END !SUBROUTINE



      SUBROUTINE StringLegends (sv, colorMap, n, barLine)
      INCLUDE 'GRAFTYPE.FOR'
      INTEGER cwin, i,n, barline
      CHARACTER * 80 sv(0:MaxLegends)
      RECORD /GroupInfoRec/ colorMap(0:MaxLegends)
      RECORD /grstype/ grstat(1:10)
      COMMON /GrAttr/ grstat, cwin
      RECORD /WorldRect/ StrRect
      REAL charSizeX, charSizeY, xpos, ypos

      CALL SetTextJustifyXX(0, 1)
      charSizeX = 1000.0 /REAL((grstat(cwin).drawingRect.right -
     +    grstat(cwin).drawingRect.left) / 8)
      charSizeY = 1000.0 /REAL(ABS(grstat(cwin).drawingRect.top -
     +     grstat(cwin).drawingRect.bottom) / 8)
      CALL SetWorldRect(StrRect, 0.0, 0.0, 1000.0, 1000.0)
      CALL SetWorldCoordinates(StrRect)
      xpos = charSizeX + 10.0
      ypos = 950.0 - charSizeY
      DO i = 0, n - 1
        CALL SelectColor(colorMap(i).GroupColor)
        IF (barLine .EQ. 0) THEN
          CALL moveworldabs(xpos, ypos)
          CALL SetLineStyleXX(colorMap(i).GroupHatch,  1)
          CALL lineworldRel(2.0 * charSizeX, 0.0)
        ELSE
          CALL SetFillStyleXX(colorMap(i).GroupHatch,
     +                        colorMap(i).GroupColor)
          CALL BarWorld(xpos, ypos - 0.75 * charSizeY, 1.5*charSizeY,
     +      2.0 * charSizeX, colorMap(i).GroupColor,
     +      colorMap(i).GroupHatch)
        END IF
        CALL moveworldabs(xpos + 3.0 * charSizeX, ypos)
        CALL OutTextXX(sv(i))
        xpos = xpos + 5.0 * charSizeX + LEN_TRIM(sv(i)) * charSizeX
        IF (i .LT. n - 1  .AND. xpos + 5.0 * charSizeX +
     +       LEN_TRIM(sv(i + 1)) * charSizeX .GT. 980.0) THEN
          xpos = charSizeX + 10.0
          ypos = ypos - 2.0 * charSizeY
        END IF
      END DO
      CALL SetWorldCoordinates(grstat(cwin).plotworld)
      CALL SetTextJustifyXX(0, 0)
      CALL SetLineStyleXX(0,1)
      END !SUBROUTINE




      SUBROUTINE TitleWindow (GTitle)
      CHARACTER* (*) GTitle
      REAL x, y
      INTEGER font, dir, Hsize, VSize

      CALL GetTextStyleXX(font, dir, Hsize, VSize)
      CALL SetTextStyleXX(font, 0, Hsize,Vsize)
      x = 500.0
      y = 980.0
      CALL LabelGraphWindow(x, y, GTitle, 1, 2)
      CALL SetTextStyleXX(font, dir, Hsize, VSize)
      END !SUBROUTINE





      SUBROUTINE TitleXAxis (xtitle)
      CHARACTER* (*) xtitle
      REAL x, y
      INTEGER font, dir, Hsize, VSize

      CALL GetTextStyleXX(font, dir, Hsize, VSize)
      CALL SetTextStyleXX(font, 0, Hsize, VSize)
      x = 500.0
      y = 15.0
      CALL LabelGraphWindow(x, y, xtitle, 1, 0)
      CALL SetTextStyleXX(font, dir, Hsize, VSize)
      END !SUBROUTINE





      SUBROUTINE TitleYAxis (ytitle)
      CHARACTER* (*) ytitle
      REAL x, y
      INTEGER font, dir, Hsize, VSize

      CALL GetTextStyleXX(font, dir, Hsize, VSize)
      CALL SetTextStyleXX(font, 1, Hsize, VSize)
      x = 35.0
      y = 500.0
      CALL LabelGraphWindow(x, y, ytitle, 0, 1)
      CALL SetTextStyleXX(font, dir, Hsize, VSize)
      END !SUBROUTINE

!
! 1/19/90  Edited GroupplotData to reflect maxgroup, maxv changes
!          updated Copy1&2 vectors for these changes
!