*--------------------------------------------------------------------*
*                            P L O T                                 *
* Subroutine PLOT -- Perform the actual plotting.  Coordinates are   *
*                    scaled and plotted. Axes are generated if CODE=1*
*                                                                    *
*     Variables Used:                                                *
*        CODE .... If CODE = 1 plot axes, otherwise do not plot axes *
*        GRAPH ... stores the image of the graph to be plotted       *
*        NROW .... number of row positions in graph                  *
*        NCOL .... number of column positions on graph               *
*        XARRAY .. storage for x coordinate of point to be plotted   *
*        YARRAY .. storage for y coordinate of point to be plotted   *
*        ZARRAY .. storage for character to be plotted               *
*        N ....... number of points submitted for plotting           *
*        INIT .... switch to insure proper initialization is done    *
*        LIMIT ... contains the number of points actually plotted    *
*        XLOW, XHIGH ... min and max of all x data                   *
*        YLOW, YHIGH ... min and max of all y data                   *
*        YORD ..... y coordinates for printing                       *
*        ABSCIS .. x coordinates for printing                        *
*        TITLE ... Title of the plot                                 *
*--------------------------------------------------------------------*
      SUBROUTINE PLOT( CODE )                                         
                                                                     
      CHARACTER GRAPH( 81, 81 ), ZARRAY( 5000 ), TITLE*72           
      COMMON /ZGRID2/ GRAPH, ZARRAY, TITLE                         
      common /io/ iounit                                                        
                                                                  
      INTEGER NROW, NCOL, N                                      
      REAL XARRAY( 5000 ), YARRAY( 5000 )                       
      LOGICAL INIT                                             
      COMMON /POINTZ/ NROW, NCOL, N, INIT, XARRAY, YARRAY     
                                                             
      CHARACTER VBAR/'|'/, SPACE/' '/, PLUS/'+'/, STAR/'*'/, BAR/'-'/  
      INTEGER I, J, CODE, IXX, IYY, IX, IY, LIMIT, FACTOR, YFACTR     
      REAL XLOW, XHIGH, YLOW, YHIGH, XUNIT, YUNIT, RANGE, SCALE, CORR,  
     1     XPOINT, YPOINT, XSIZE, YSIZE, YORD, ABSCIS(9)               
      CHARACTER C                                                     
      INTEGER POS                                                    
                                                                    
9000  FORMAT( 9X, 81A1 )                                           
9001  FORMAT( 1X, F8.5, 81A1 )                                    
*9002  FORMAT( <I+3>X, 12F10.4 )                                 
9002  FORMAT( 8X, 12F10.4 )                                     
9009  FORMAT(//,2X,'Scale Factors: X-axis 10**',I6,5X,' Y-axis 10**',I6,
     1        /,2X,'Number of points plotted:',I5,                              
     2        //,8X, A72)                                              
                                                                      
* Check that the plot routines have been initialized.                
      IF( .NOT. INIT ) THEN                                         
         PRINT*,' ERROR - NO INITIALIZATION.'                      
         GOTO 999                                                 
      ENDIF                                                      
                                                                
* Check the number of points to be plotted. If n > 1000        
* only 5000 points have been stored.                          
      IF( N .EQ. 0 ) THEN                                    
         PRINT*,'ERROR - ATTEMPT TO PLOT 0 POINTS.'         
         GOTO 999                                          
      ENDIF                                               
      LIMIT = N                                          
      LIMIT = MIN( 5000, LIMIT )                        
                                                       
* Determine the high and low values for both X and Y coordinates        
      XLOW = XARRAY(1)                                                 
      XHIGH = XARRAY(1)                                               
      YLOW = YARRAY(1)                                               
      YHIGH = YARRAY(1)                                             
      DO 500 I = 1, LIMIT                                          
         XPOINT = XARRAY(I)                                       
         YPOINT = YARRAY(I)                                      
         IF( XPOINT .LT. XLOW ) XLOW = XPOINT                   
         IF( XPOINT .GT. XHIGH ) XHIGH = XPOINT                
         IF( YPOINT .LT. YLOW ) YLOW = YPOINT                 
         IF( YPOINT .GT. YHIGH ) YHIGH = YPOINT              
500   CONTINUE                                              
                                                           
* If upper and lower limits are the same for either x or y axis         
* alter the high and low values so that (high - low) is not zero.      
      IF( XHIGH .EQ. XLOW ) THEN                                      
         XHIGH = XHIGH + 1.0                                         
         XLOW = XLOW - 1.0                                          
         PRINT *,'WARNING - ALL POINTS HAVE SAME X-VALUE.'         
      ENDIF                                                       
      IF( YHIGH .EQ. YLOW ) THEN                                 
         YHIGH = YHIGH + 1.0                                    
         YLOW = YLOW - 1.0                                     
         PRINT *, 'WARNING - ALL POINTS HAVE SAME Y-VALUE.'   
      ENDIF                                                  
                                                            
* Prepare location for axes:  If the origin would lie totally outside   
* the graph, determined by plus or minus 10%, then force the axes along
* the edge of the graph.                                              
      IF( LIMIT .LT. 5000 ) LIMIT = LIMIT + 1                        
      XARRAY(LIMIT) = XLOW                                          
      YARRAY(LIMIT) = YLOW                                         
      ZARRAY(LIMIT) = PLUS                                        
      IF( XLOW*XHIGH .LE. 0 ) THEN                               
*        Put y-axis inside.                                     
         XARRAY(LIMIT) = 0.0                                   
                                                              
      ELSEIF( XLOW .GE. 0 ) THEN                             
         IF( XLOW .LE. 0.1*XHIGH ) THEN                     
              XARRAY(LIMIT) = 0.0                          
              XLOW = 0.0                                  
         ENDIF                                           
      ELSE                                              
         XARRAY(LIMIT) = XHIGH                         
         IF( -XHIGH .LE. -0.1*XLOW ) THEN             
              XARRAY(LIMIT) = 0.0                    
              XHIGH = 0.0                           
         ENDIF                                     
      ENDIF                                       
                                                 
                                                
      IF( YLOW*YHIGH .LE. 0 ) THEN             
         YARRAY(LIMIT) = 0.0                  
      ELSEIF( YLOW .GE. 0 ) THEN             
         IF( YLOW .LE. 0.1*YHIGH ) THEN     
              YARRAY(LIMIT) = 0.0          
              YLOW = 0.0                  
         ENDIF                           
      ELSE                              
         YARRAY(LIMIT) = YHIGH         
         IF( -YHIGH .LE. -0.1*YLOW ) THEN                               
              YARRAY(LIMIT) = 0.0                                      
              YHIGH = 0.0                                             
         ENDIF                                                       
      ENDIF                                                         
                                                                   
* Mark the origin with a '+'if axes were requested and if the orig
* is within the plot region.                                     
      IF(XARRAY(LIMIT) .EQ. 0.0 .AND. YARRAY(LIMIT) .EQ. 0.0    
     1                          .AND. CODE .EQ. 1) ZARRAY(LIMIT) = PLUS 
                                                                       
* Compute the range of X and Y values, and the unit range for         
* each printer position.                                             
      XSIZE = XHIGH - XLOW                                          
      YSIZE = YHIGH - YLOW                                         
      XUNIT = XSIZE/(NCOL - 1)                                    
      YUNIT = YSIZE/(NROW - 1)                                   
      IXX = IFIX( (XARRAY(LIMIT) - XLOW)/XUNIT + 1.5 )          
      IYY = IFIX( (YARRAY(LIMIT) - YLOW)/YUNIT + 1.5 )         
                                                              
* If axes are requested, build up the characters in the array graph.    
      IF( CODE .EQ. 1 ) THEN                                           
*        Draw the x axis                                              
         DO 700 I = 1, NCOL                                          
              POS = NROW + 1 - IYY                                  
              GRAPH(POS, I) = BAR                                  
              IF(MOD(I-IXX,10) .EQ. 0) GRAPH(POS,I) = PLUS        
700      CONTINUE                                                
*        Draw th y axis                                         
         DO 800 I = 1, NROW                                    
              POS = NROW + 1 - I                              
              GRAPH(POS,IXX) = VBAR                          
              IF(MOD(NROW + IYY - I, 5) .EQ. 1) GRAPH(POS,IXX) = PLUS   
800      CONTINUE                                                      
      ENDIF                                                           
                                                                     
* Now generate the plot in the buffer (GRAPH).  If the character    
* position contains a character other than a space, "-", "|", "+",      
* or the current character, two curves have intersected - place a "*". 
      DO 950 I = 1, LIMIT                                             
         IX = IFIX((XARRAY(I) - XLOW)/XUNIT + 1.5 )                  
         IY = IFIX((YARRAY(I) - YLOW)/YUNIT + 1.5 )                 
         IY = NROW + 1 - IY                                        
         C = GRAPH(IY,IX)                                         
         IF( C.NE.SPACE .AND. C.NE.BAR .AND. C.NE.VBAR .AND. C.NE.PLUS  
     1                  .AND. C.NE.ZARRAY(I) ) THEN                    
              GRAPH(IY,IX) = STAR                                     
         ELSE                                                        
              GRAPH(IY,IX) = ZARRAY(I)                              
         ENDIF                                                     
950   CONTINUE                                                    
                                                                 
* Compute y - axis scale factor                                 
      YFACTR = 0                                               
      SCALE = 1.0                                             
      RANGE = ABS(YHIGH)                                     
      IF( ABS(YLOW) .GE. RANGE ) RANGE = ABS(YLOW)          
975   IF( RANGE .GE. 10 .OR. RANGE .LT. 1 ) THEN           
         IF( RANGE .LT. 1 ) THEN                          
              RANGE = RANGE*10.0                         
              SCALE = SCALE*10.0                        
              YFACTR = YFACTR - 1                      
         ELSE                                         
              RANGE = RANGE/10.0                     
              SCALE = SCALE/10.0                    
              YFACTR = YFACTR + 1                  
         ENDIF                                    
         GOTO 975                                
      ENDIF                                     
                                               
* Compute axis alignment correction           
      CORR = 0.0                             
      IF( YLOW*YHIGH .LT. 0.0 ) CORR = (YLOW + (IYY - 1)*YUNIT)*SCALE   
                                                                        PLO02030
* And print the plot                                                    PLO02040
      DO 980 I = 1, NROW                                                PLO02050
         IF( MOD(NROW+1-IYY-I,5) .NE. 0 ) THEN                          PLO02060
              write(iounit,9000) (GRAPH(I,J),J=1,NCOL)                  PLO02070
         ELSE                                                           PLO02080
              YORD = (YLOW+(NROW - I)*YUNIT)*SCALE - CORR               PLO02090
              write(iounit,9001) YORD, (GRAPH(I,J),J=1,NCOL)            PLO02100
         ENDIF                                                          PLO02110
980   CONTINUE                                                          PLO02120
                                                                        PLO02130
* Generate the abscissa coordinates                                     PLO02140
      FACTOR = 0                                                        PLO02150
      SCALE = 1.0                                                       PLO02160
      RANGE = ABS(XHIGH)                                                PLO02170
      IF(ABS(XLOW) .GE. RANGE) RANGE = ABS(XLOW)                        PLO02180
990   IF( RANGE .GE. 10 .OR. RANGE .LT. 1 ) THEN                        PLO02190
         IF( RANGE .LT. 1 ) THEN                                        PLO02200
              RANGE = RANGE*10.0                                        PLO02210
              SCALE = SCALE*10.0                                        PLO02220
              FACTOR = FACTOR - 1                                       PLO02230
         ELSE                                                           PLO02240
              RANGE = RANGE/10.0                                        PLO02250
              SCALE = SCALE/10.0                                        PLO02260
              FACTOR = FACTOR + 1                                       PLO02270
         ENDIF                                                          PLO02280
         GOTO 990                                                       PLO02290
      ENDIF                                                             PLO02300
                                                                        PLO02310
* Compute axis alignment correction                                     PLO02320
      CORR = 0.0                                                        PLO02330
      IF( XLOW*XHIGH .LT. 0.0 ) CORR = (XLOW + (IXX - 1)*XUNIT)*SCALE   PLO02340
      I = MOD(IXX,10)                                                   PLO02350
                                                                        PLO02360
* Label the abscissa axis                                               PLO02370
      K = 0                                                             PLO02380
      write(iounit,9099)                                                PLO02390
9099  format(/)                                                                 
      DO 1200 J = I, NCOL, 10                                           PLO02400
         K = K + 1                                                      PLO02410
         ABSCIS(K) = (XLOW+(J-1)*XUNIT)*SCALE - CORR                    PLO02420
1200  CONTINUE                                                          PLO02430
      write(iounit,9002) (ABSCIS(J), J=1,K)                             PLO02440
                                                                        PLO02450
* Output summary information - scale factors, number of points          PLO02460
* processed, and title:                                                 PLO02470
      write(iounit,9009) FACTOR, YFACTR, LIMIT, TITLE                   PLO02480
999   RETURN                                                            PLO02530
      END                                                               PLO02540
*--------------------------------------------------------------------*  SET00010
*     Subroutine SETPLT -- Initialize the plotting package, and      *  SET00020
*                          establish the actual number of rows and   *  SET00030
*                          columns in the printed plot.              *  SET00040
*                                                                    *  SET00050
*     Variables Used:                                                *  SET00060
*        XLIMIT ..... Maximum number of columns                      *  SET00070
*        YLIMIT ..... Maximum number of rows                         *  SET00080
*        I .......... loop counter                                   *  SET00090
*        BLANK ...... The blank character ' '                        *  SET00100
*--------------------------------------------------------------------*  SET00110
      SUBROUTINE SETPLT( NCHARX, NCHARY ,IOUNIT, TYTLE )                SET00120
                                                                        SET00130
      CHARACTER GRAPH(81, 81), ZARRAY(5000), TITLE*72                   SET00140
      COMMON /ZGRID2/ GRAPH, ZARRAY, TITLE                              SET00150
      integer iounit, unit                                                      
      common /io/ unit                                                          
                                                                        SET00160
      INTEGER NROW, NCOL, N                                             SET00170
      REAL XARRAY(5000), YARRAY(5000)                                   SET00180
      LOGICAL INIT                                                      SET00190
      COMMON /POINTZ/ NROW, NCOL, N, INIT, XARRAY, YARRAY               SET00200
                                                                        SET00210
      INTEGER I, J, XLIMIT/81/, YLIMIT/81/                              SET00220
      CHARACTER BLANK/' '/, TYTLE*(*)                                   SET00230
                                                                        SET00240
* Statement function to adjust M to 11, 21, 31, ...                     SET00250
                                                                        SET00260
      ADJUST( M ) = ((M + 8) / 10)*10 + 1                               SET00270
      TITLE = TYTLE                                                     SET00280
                                                                                
      If( iounit .lt. 1 .or. iounit .gt. 99 ) iounit = 6                        
      unit = iounit                                                             
* Check supplied number of rows and columns.  Adjust and set            SET00290
* to default values if out of range.                                    SET00300
                                                                        SET00310
      NCOL = ADJUST( NCHARX )                                           SET00320
      IF( NCOL .LE. 1 ) THEN                                            SET00330
         NCOL = 11                                                      SET00340
         PRINT*,'PLOT-WIDTH TOO SMALL, 11 CHARACTERS USED.'             SET00350
      ENDIF                                                             SET00360
      IF( NCOL .GT. XLIMIT ) THEN                                       SET00370
         NCOL = XLIMIT                                                  SET00380
         PRINT *, 'PLOT-WIDTH TOO LARGE, ',XLIMIT,' CHARACTERS USED.'   SET00390
      ENDIF                                                             SET00400
                                                                        SET00410
      NROW = ADJUST( NCHARY )                                           SET00420
      IF( NROW .LE. 1 ) THEN                                            SET00430
         NROW = 11                                                      SET00440
         PRINT *,'PLOT-HEIGHT TOO SMALL, 11 CHARACTERS USED.'           SET00450
      ENDIF                                                             SET00460
      IF( NROW .GT. YLIMIT ) THEN                                       SET00470
         NROW = YLIMIT                                                  SET00480
         PRINT *,'PLOT-HEIGHT TOO LARGE, ',YLIMIT,' CHARACTERS USED.'   SET00490
      ENDIF                                                             SET00500
                                                                        SET00510
* Initialize elements of the array GRAPH to blanks.  Zero the points    SET00520
* counter and set the initialization flag.                              SET00530
                                                                        SET00540
      DO 100 I = 1, YLIMIT                                              SET00550
         DO 100 J = 1, XLIMIT                                           SET00560
              GRAPH(I,J) = BLANK                                        SET00570
100   CONTINUE                                                          SET00580
                                                                        SET00590
      N = 0                                                             SET00600
      INIT = .TRUE.                                                     SET00610
      RETURN                                                            SET00620
      END                                                               SET00630
*--------------------------------------------------------------------*  STO00010
*                       S T O P N T                                  *  STO00020
*     Stores x,y coordinates for later plotting.                     *  STO00030
*                                                                    *  STO00040
*     Variables Used:                                                *  STO00050
*        X, Y ..... The coordinates of the point to be stored        *  STO00060
*        Z ........ The plotting character for this point            *  STO00070
*--------------------------------------------------------------------*  STO00080
      SUBROUTINE STOPNT( X, Y, Z )                                      STO00090
                                                                        STO00100
      CHARACTER GRAPH( 81, 81 ), ZARRAY( 5000 ), TITLE*72               STO00110
      COMMON /ZGRID2/ GRAPH, ZARRAY, TITLE                              STO00120
                                                                        STO00130
      INTEGER NROW, NCOL, N                                             STO00140
      REAL XARRAY( 5000 ), YARRAY( 5000 )                               STO00150
      LOGICAL INIT                                                      STO00160
      COMMON /POINTZ/ NROW, NCOL, N, INIT, XARRAY, YARRAY               STO00170
                                                                        STO00180
      REAL X, Y                                                         STO00190
      CHARACTER Z                                                       STO00200
                                                                        STO00210
* Check for initialization                                              STO00220
      IF( .NOT. INIT ) THEN                                             STO00230
         PRINT *,'WARNING - PLOT NOT INITIALIZED.'                      STO00240
         STOP                                                           STO00250
      ENDIF                                                             STO00260
                                                                        STO00270
      N = N + 1                                                         STO00280
      IF( N.LT.5001 ) THEN                                              STO00290
         XARRAY(N) = X                                                  STO00300
         YARRAY(N) = Y                                                  STO00310
         ZARRAY(N) = Z                                                  STO00320
      ELSE                                                              STO00330
         IF( N .EQ. 5001 ) THEN                                         STO00340
              PRINT*,'TOO MANY POINTS - FIRST 5000 USED.'               STO00350
         ENDIF                                                          STO00360
      ENDIF                                                             STO00370
                                                                        STO00380
      RETURN                                                            STO00390
      END                                                               STO00400
