!++
!
! ---------------------- DEMO OF GPLOT CONTOUR ROUTINES -----------------------
!
! FUNCTIONAL DESCRIPTION
!   
!    This program constructs a 2-dimensional array of values and then uses 
!    the GPlot contouring routines to produce a contour may based on those 
!    values.
!
! -----------------------------------------------------------------------------
!
! DECLARATION SECTION
!
      PARAMETER Nx = 41        ! Number of data values in x direction (#rows)
      PARAMETER Ny = 51        ! Number of data values in y direction (#columns)

      PARAMETER xRange = 3.0   ! x Varies between -xRange and +xRange
      PARAMETER yRange = 4.0   ! y Varies between -yRange and +yRange

      DIMENSION DataArray(Nx,Ny)              ! The array of values to contour
!
! -----------------------------------------------------------------------------
!
!  MAIN PROGRAM
!  
!  First, create the array of values to contour in array DataArray(,)
!
      CALL BuildArray( DataArray, Nx, Ny, xRange, yRange )
!
!  Next initialize GPLOT and select output device 
!
      call GSTART
!
!  Use GPlot calls to set the page size, plotting area, x and y axis names,
!  and calibrations, and provide a title for the plot.
!  
      call page(11.0,8.5)
	CALL PHYSOR(2.0,0.5)
      call area2d(7.0,7.0)
      call xname('X-AXIS$',100)
      call yname('Y-AXIS$',100)
      call graf(-xRange, 'SCALE', xRange, -yRange, 'SCALE', yRange)
      call headin('GPLOT Example 13, Contour Plot$', 100,1.5,1)
!
!  The first contouring step is to create the contours using a call to the
!  GPlot procedure CONMAK.  The parameters to CONMAK are:
!
!     DataArray  -- The array of z values to contour
!     Nx, Ny     -- The dimensions of the DataArray
!     zIncrement -- The real value giving the increment between successive
!                   contour lines.  This may be 'SCALE' if you want GPlot to
!                   to select the increment automatically.
!
      zIncrement = 1.                 
      call CONMAK(DataArray, Nx, Ny, zIncrement)
!
!  Next set up the line styles to be used for sucessive levels of the contour
!  map.  This is done using the GPlot procedure CONLIN. The parameters to 
!  CONLIN are:
!
!     LineNumber -- Beginning with line 0, this index gives the number of the
!                   contour line whose characteristics are being set.
!   
!     LineType   -- Specifies the line type to use.  This must be one of 
!                   'SOLID', 'DOT', 'DASH', 'CHNDOT' or 'MYCON'.  The value
!                   'MYCON' allows the user to dynamically alter the
!                   characteristics of specific contour lines as they are
!                   drawn.  The user written routine MYCON will be called before
!                   the corresponding line is drawn.
!                   
!     LabelFlag  -- Must be 'NOLABELS' since labeling of the contour lines is
!                   not currently supported.
!
!     iThick     -- Integer thickness factor for the contour line.
!
!     iPriority  -- Integer line priority.  This is not used but should be 
!                   included for compatibility with Disspla.
!
      iThick = 1
      call CONLIN(0, 'MYCON' , 'NOLABELS', iThick, 1)
      call CONLIN(1, 'DASH'  , 'NOLABELS', iThick, 1)
      call CONLIN(2, 'DOT'   , 'NOLABELS', iThick, 1)
!
!  To actually draw the contours, we call the routine CONTUR. The parameters 
!  are:
!
!     nLineTypes -- The number of line types set up by calls to CONLIN
!
!     LabelFlag  -- Must be 'NOLABELS' since labeling is not supported in GPlot
!
!     DrawFlag   -- Must be 'DRAW' for compatibility with DISSPLA, ignored by
!                   GPlot
!
      nLineTypes = 3
      call CONTUR(nLineTypes, 'NOLABELS', 'DRAW')
!
!  Now end the plot and shut down GPlot
!
      call endpl(0)
      call GSTOP

      end

!++
!
! ------------------------------- SUBROUTINE MYCON ----------------------------
!
!  FUNCTIONAL DESCRIPTION
!
!    This routine is a simple example of the dynamic modification of contour
!    line modification.  It is called by the GPlot CONTUR routine as CONTUR is
!    about to draw a contour line with line type specified as 'MYCON'
!
!  FORMAL ARGUMENTS
!
!   rAray            INPUT     Real Array(2).  Only rAray(1) is used in GPlot
!                              This contains the z-level of the current contour
!                              line to be drawn.  Based on the level being
!                              drawn, MyCon can change curve styles, thickness
!                              parameters, and/or suppress drawing the contour.
!
!   iAray            IN/OUT    Integer Array(9).  Only iAray(1) and iAray(9) are
!                              used in GPlot.  iAray(1) contains the line 
!                              thickness value set by CONLIN.  Mycon may change
!                              this value for the current line, if desired.
!                              iAray(9) contains a Draw/NoDraw flag that may be
!                              changed by MyCon.  If MyCon sets iAray(9) = 0,
!                              the current line will not be drawn.
!  IMPLICIT INPUTS
!
!    NONE
!
!  IMPLICIT OUTPUTS
!
!    The current curve type is set to SOLID.
!
!  SIDE EFFECTS
!
!    The current curve type is set to SOLID.
!--
      SUBROUTINE MYCON( RArray, IArray )
      DIMENSION RArray(2), IArray(9)
      call solid
      if ( ABS(RArray(1)) .lt. .05) then       ! level 0, so make it three times
        IArray(1) = 3                          ! as thick
      endif
      end


!++
!
! -------------------------- SUBROUTINE BuildArray ---------------------------
!
!  FUNCTIONAL DESCRIPTION
!
!    This routine constructs a sample data set for use with contouring and 
!    surface routines.
!
!  FORMAL ARGUMENTS
!
!    DataArray       OUTPUT     Real Array(Nx,Ny) that will contain the sample
!                               data set.
!
!    Nx, Ny          INPUT      The dimensions of DataArray
!
!    xRange,         INPUT      Range of x and y values to be used when 
!    yRange                     fabricating the sample data set.
!
!  IMPLICIT INPUTS
!
!    NONE
!
!  IMPLICIT OUTPUTS
!
!    NONE
!
!  SIDE EFFECTS
!
!    NONE
!
!--
      SUBROUTINE BuildArray( DataArray, Nx, Ny, xRange, yRange )
      REAL DataArray( Nx, Ny )
      xstep = 2.0 * xRange/(Nx-1)
      ystep = 2.0 * yRange/(Ny-1)
      xi = -xRange
      do 10 i = 1,Nx
        yj = -yRange
        do 5 j = 1,Ny
          temp1 = max(xi+yj-2 , .2)
          temp2 = max( (xi+3.)**2 + (yj+2.)**2, .2)
          DataArray(i,j) = xi**2 - yj**2 + 1./temp1 + 1./temp2
          yj = yj + ystep
5       continue
        xi = xi + xstep
10    continue

!
!  Now put in a "peak"
!
      iPeak = Nx/3 + 1
      jPeak = Ny/5 + 1

      DataArray( iPeak-1 , jPeak-1 ) =  4.0
      DataArray( iPeak-1 , jPeak   ) =  6.0
      DataArray( iPeak-1 , jPeak+1 ) =  4.0
      DataArray( iPeak   , jPeak-1 ) =  6.0
      DataArray( iPeak   , jPeak   ) = 10.0
      DataArray( iPeak   , jPeak+1 ) =  6.0
      DataArray( iPeak+1 , jPeak-1 ) =  4.0
      DataArray( iPeak+1 , jPeak   ) =  6.0
      DataArray( iPeak+1 , jPeak+1 ) =  4.0

      END
