      SUBROUTINE CURVE(NP, X, Y, IC)
C======================================================================*
C curve -- generic plotting routine (with self initialization)
C
C  This subroutine plots a series of NP points whose coordinates
C  are given in the arrays X and Y.  The colour of the line is given
C  by IC.  
C
C  The first time this routine is called it initializes the graphics,
C  opens a window, and sets the scale of the plotting region so that
C  all of the data in X and Y will fit.  Subsequent calls simply plot
C  curves in this window, using the same scale.
C
C  If NP is less than zero the routine pauses and waits for the user
C  to press the RETURN key, and then terminates the graphics display.  
C
C  If IC is negative then no color is used, and so the drawing is
C  done in the "current" color, whatever that is.
C
C  This version of the subroutine is designed for the Silicon Graphics
C  GL graphics library, or may be used with the VOGL library, which
C  emulates GL.  I've tried to make the calling parameters match some
C  of the 'standard' plotting packages rather than forcing the user
C  to use the begin/end line structure.  
C      
C  This routine was patterned on Claudio Rebbi's PLOT subroutine.
C
C  Eric Myers, ICTP College on Computational Physics,  18 May 1993  
C  $Revision: 1.6+X1 $  :  $Date: 94/07/11 13:30:27 $  :  $Author: myers $
C======================================================================*
        IMPLICIT NONE

C Arguments:
        
        INTEGER NP, IC
        REAL X(NP), Y(NP)

C The "state" of the graphics system is kept in a common block here,
C and all GL/VOGL includes are done here:

        INCLUDE 'CVstate.f'

C Local variables:

        INTEGER I
        REAL XY(2), XMAX, XMIN, YMAX, YMIN, DX, DY

        DATA XMAX, XMIN, YMAX, YMIN / 0.0, 0.0, 0.0, 0.0/
C======================================================================*
C BEGIN:


C If the number of points is negative and the window is already
C open then this is the termination call, so wait for user to 
C press RETURN, then shut down graphics  

        IF( NP.LT.0 .AND. WISOPEN ) THEN
          CALL CVEXIT
          RETURN
        ENDIF

C If this is the first call to the subroutine then we need to open
C a window and do whatever else is required to initialize the graphics,
C including setting the scale for further plotting operations
              
        IF(.NOT.WISOPEN) THEN

C Initialize the graphics

          CALL CVINIT

C Find max/min values for X and Y and set scale to be 10% more

          DO 100 I=1,NP
            IF( X(I).LT.XMIN) XMIN = X(I)
            IF( X(I).GT.XMAX) XMAX = X(I)
            IF( Y(I).LT.YMIN) YMIN = Y(I)
            IF( Y(I).GT.YMAX) YMAX = Y(I)
 100      CONTINUE
          DX = XMAX-XMIN
          DY = YMAX-YMIN
          XMAX = XMAX + 0.10*DX
          XMIN = XMIN - 0.10*DX
          YMAX = YMAX + 0.10*DY
          YMIN = YMIN - 0.10*DY
          CALL ORTHO2( XMIN, XMAX, YMIN, YMAX )
        ENDIF
        
C Set the line colour to IC

        IF(IC.GE.0) CALL COLOR(IC)

C Draw the curve as pairs of points

        CALL BGNLIN
          DO 200 I=1,NP
            XY(1)=X(I)
            XY(2)=Y(I)
            CALL  V2F(XY)
 200      CONTINUE  
        CALL ENDLIN 
        
        RETURN
        END


        SUBROUTINE CVINIT
C======================================================================*
C CVINIT -- initialize GL/VOGL for simple drawing with CURVE or CURVE3
C
C
C Eric Myers,  ICTP College on Computational Physics, Trieste, 30 May 1993
C $Revision: 1.6 $  :  $Date: 94/07/11 13:30:27 $  :  $Author: myers $
C======================================================================*
C GL parameters, like the names of colors, are defined here:
        
        INCLUDE 'CVstate.f'

C==================================================*
C BEGIN:

C FOREGR is required if your program is going to interact with the
C user through the keyboard/display (not just the mouse).  It keeps
C the program in foreground.  Otherwise GL puts the process into the
C background.  This is simply ignored by VOGL, which always runs in
C foreground.

        CALL FOREGR

C PREFSI(x-width, y-width) specifies the prefered size of the window, in
C pixels.  The positioning of the window depends on how your window
C manager does such things [either you have to position it, or it puts
C it randomly somewhere (see my .twmrc)].  If you omit this the default
C seems to be 100x100 in GL, and about 712x614 in VOGL

        CALL PREFSI(600,600)

C To be more specific use PREFPO(x-lower, x-upper, y-lower, y-upper)
C instead of PREFSI to specify the exact position where the window is to
C be placed on the screen.  N.B.: the y position in screen coordinates
C in X11 runs *down* the screen.
c$$$
c$$$
c$$$       CALL PREFPO(WX1, WX2, WY1, WY2)
c$$$

C Now we can open a window with WINOPE(title, length) where the title
C is a character string and the length is the length of the string.
C There seems to be a limit of 10 characters in the title in VOGL.

        CALL WINOPE('CURVE Graphics', 14)

C To clear the screen in GL you set the current color to what you want
C it to be and then call CLEAR.  The symbol BLACK is defined in fgl.h or
C fvogl.f; if you don't include them then use a ZERO instead.

        CALL COLOR(BLACK)
        CALL CLEAR 

C The COLOR subroutine takes a single integer index into the color
C table.  The default color table contains the following (the names
C in capital letters are the names defined in fgl.h):
C
C       0 = BLACK     1 = RED   
C       2 = GREEN     3 = YELLOW      
C       4 = BLUE      5 = MAGENT (magenta)
C       6 = CYAN      7 = WHITE
C
C All drawing operations are performed in the "current" color, so
C to draw a line we need to change the color to something other
C than BLACK.

        CALL COLOR(WHITE)

C Graphics is now initialized, so set the flag.

        WISOPEN = .TRUE.


        RETURN
        END



        SUBROUTINE CVEXIT
C======================================================================*
C CVEXIT -- exit curve plotting
C
C  This subroutine exits curve drawing mode by waiting for the user
C  to signal that they are done with the window, either by clicking
C  the mouse or typing 'q' or 'X', then calling GEXIT.
C
C Eric Myers,  ICTP College on Computational Physics, Trieste, 30 May 1993
C $Revision: 1.6 $  :  $Date: 94/07/11 13:30:27 $  :  $Author: myers $
C======================================================================*
C GL parameters and internal plotter state are kept in CVstate.f:

        CHARACTER*80 LINE

        INCLUDE 'CVstate.f'

C==================================================*
C BEGIN:

C Wait for RETURN key to be pressed

        PRINT *,'Press RETURN to continue...'
        READ(*,'(A80)') LINE

        IF(LINE.EQ.'wait') CALL SLEEP(5)

C Reset the state flag and then exit GL/VOGL

        WISOPEN=.FALSE.
        CALL GEXIT
        RETURN
        END

