      PROGRAM PGDEM2
C-----------------------------------------------------------------------
C Demonstration program for PGPLOT. The main program opens the output
C device and calls a series of subroutines, one for each sample plot.
C-----------------------------------------------------------------------
C
C Call PGBEGIN to initiate PGPLOT and open the output device; PGBEGIN
C will prompt the user to supply the device name and type.
C
      CALL PGBEGIN(0,'?',1,1)
C
C Call the demonstration subroutines.
C
      CALL PGEX21
      CALL PGEX22
      CALL PGEX23
      CALL PGEX24
C
C Finally, call PGEND to terminate things properly.
C
      CALL PGEND
C-----------------------------------------------------------------------
      END

      SUBROUTINE PGEX21
C-----------------------------------------------------------------------
C Test subroutine for PGPLOT: screen alignment and color palette.
C-----------------------------------------------------------------------
      INTEGER I, L1, L2
      REAL X1, X2, Y1, Y2
      CHARACTER*80 GTYPE, GVER
C
C Get PGPLOT information.
C
      CALL PGQINF('VERSION', GVER, L1)
      CALL PGQINF('TYPE', GTYPE, L2)
      CALL PGBBUF
C
C Alignment test: clear the screen, and draw a box and grid using
C three monochrome intensities (color indices 1, 14, and 15).  The
C plot uses the largest available square viewport and and unit window.
C
      CALL PGPAGE
      CALL PGVPORT(0.0,1.0,0.0,1.0)
      CALL PGWNAD(0.0,1.0,0.0,1.0)
      CALL PGSCI(14)
      CALL PGBOX('g',0.02,1,'g',0.02,1)
      CALL PGSCI(15)
      CALL PGBOX('g',0.1,5,'g',0.1,5)
      CALL PGSCI(1)
      CALL PGBOX('bc',0.1,5,'bc',0.1,5)
C
C Color palette test.
C
      DO 20 I=0,15
          CALL PGSCI(I)
          X1 = 0.31 + MOD(I,4)*0.1
          Y1 = 0.61 - (I/4)*0.1
          X2 = X1 + 0.08
          Y2 = Y1 + 0.08
          CALL PGRECT(X1, X2, Y1, Y2)
   20 CONTINUE
C
C Write the device type on the plot.
C
      CALL PGSCI(0)
      CALL PGRECT(0.31, 1.0-0.31, 0.85, 0.97)
      CALL PGSCI(1)
      CALL PGSFS(2)
      CALL PGRECT(0.31, 1.0-0.31, 0.85, 0.97)
      CALL PGPTEXT(0.5, 0.91, 0.0, 0.5, 'PGPLOT '//GVER(1:L1))
      CALL PGPTEXT(0.5, 0.87, 0.0, 0.5, 'Device '//GTYPE(1:L2))
C
      CALL PGEBUF
C-----------------------------------------------------------------------
      END

      SUBROUTINE PGEX22
C-----------------------------------------------------------------------
C Demonstration program for the PGPLOT plotting package.
C Plot a table of the standard PGPLOT graph marker symbols. This
C program also illustrates how windows and viewports may be manipulated.
C-----------------------------------------------------------------------
      CHARACTER*2 LABEL
      INTEGER NX, NY, N, IX, JY, LW
      REAL X, X1, X2, XOFF, Y, Y1, Y2, YOFF, DX, DY
      REAL XPIX1, XPIX2, YPIX1, YPIX2, RES
C
C Determine size of view surface.
C Lower left corner is (X1,Y1), upper right (X2, Y2) [inches].
C
      CALL PGVPORT(0.0, 1.0, 0.0, 1.0)
      CALL PGQVP(1, X1, X2, Y1, Y2)
      X = X2-X1
      Y = Y2-Y1
C
C Determine device resolution (pixels/inch), and use it to choose
C line width.
C
      CALL PGQVP(3, XPIX1, XPIX2, YPIX1, YPIX2)
      RES = ABS(XPIX2-XPIX1)/ABS(X)
      LW = 1
      IF (RES.GT.166.0) LW = 2
C
C Choose horizontal or vertical arrangement depending on
C device aspect ratio.
C
      IF (X.GT.Y) THEN
          NX = 8
          NY = 4
      ELSE
          NX = 4
          NY = 8
      END IF
      DX = MIN(X/NX, 0.95*Y/NY)
      DY = DX
      IX = NX
      JY = 1
      XOFF = X1 + (X-NX*DX)*0.5
      YOFF = Y1 + (0.95*Y-NY*DY)*0.5
      CALL PGBBUF
C
C Each symbol will be drawn in a standard window; the window is moved
C by manipulating the viewport.
C
      CALL PGWINDOW(-1.,1.,-1.,1.)
C
C Loop through all known symbols (N=0-31). 
C
      DO 10 N=0,31
          WRITE (LABEL,'(I2)') N
C
C Define window and viewport. The loop allows the plot to extend over
C more than one page if necessary; each page is labelled at the top.
C
          IX = IX+1
          IF (IX.GT.NX) THEN
            IX = 1
            JY = JY-1
          END IF
          IF (JY.LT.1) THEN
            JY = NY
            CALL PGPAGE
            CALL PGSCH(1.2)
            CALL PGVSIZE(XOFF, XOFF+NX*DX, YOFF, YOFF+NY*DY)
            CALL PGSLW(LW)
            CALL PGMTEXT('T', 1.0, 0.5, 0.5,
     1                   '\fiPGPLOT \frMarker Symbols')
          END IF
          CALL PGVSIZE(XOFF+(IX-1)*DX, XOFF+IX*DX,
     1                 YOFF+(JY-1)*DY, YOFF+JY*DY)
C
C Call PGBOX to draw a box and PGMTEXT to label it.
C
          CALL PGSLW(1)
          CALL PGBOX('BC',10.0,0,'BC',10.0,0)
          CALL PGSCH(0.5)
          CALL PGMTEXT('T',-1.5,0.05,0.0,LABEL)
C
C Call PGPOINT to draw the symbol.
C
          CALL PGSLW(LW)
          CALL PGSCH(1.5)
          CALL PGPOINT(1,0.0,0.0,N)
   10 CONTINUE
C
      CALL PGEBUF
C-----------------------------------------------------------------------
      END

      SUBROUTINE PGEX23
C-----------------------------------------------------------------------
C Demonstration program for the PGPLOT plotting package. 
C-----------------------------------------------------------------------
      INTEGER I
      CHARACTER*80 SAMPLE(8)
      DATA SAMPLE(1)
     1 /'Normal:  \fnABCDQ efgh 1234 \ga\gb\gg\gd \gL\gH\gD\gW'/
      DATA SAMPLE(2)
     1 /'Roman:  \frABCDQ efgh 1234 \ga\gb\gg\gd \gL\gH\gD\gW'/
      DATA SAMPLE(3)
     1 /'Italic:  \fiABCDQ efgh 1234 \ga\gb\gg\gd \gL\gH\gD\gW'/
      DATA SAMPLE(4)
     1 /'Script:  \fsABCDQ efgh 1234 \ga\gb\gg\gd \gL\gH\gD\gW'/
      DATA SAMPLE(5)
     1 /'\fif\fr(\fix\fr) = \fix\fr\u2\dcos(2\gp\fix\fr)e\u\fix\fr\u2'/
      DATA SAMPLE(6)
     1 /'\fiH\d0\u \fr= 75 \(2233) 25 km s\u-1\d Mpc\u-1\d'/
      DATA SAMPLE(7)
     1 /'\fsL/L\d\(2281)\u\fr = 5.6 (\gl1216\A)'/
      DATA SAMPLE(8)
     1 /'Markers: 3=\m3, 8=\m8, 12=\m12, 28=\m28.'/
C
C Call PGENV to initialize the viewport and window.
C Call PGLABEL to label the graph.
C
      CALL PGENV(0.,20.,8.,0.,0,-2)
      CALL PGLABEL(' ',' ','\fiPGPLOT \frFonts')
C
C Use PGTEXT to write the sample character strings.
C
      CALL PGSCH(1.6)
      DO 10 I=1,8
          CALL PGTEXT(0.0,FLOAT(I),SAMPLE(I))
   10 CONTINUE
      CALL PGSCH(1.0)
C-----------------------------------------------------------------------
      END

      SUBROUTINE PGEX24
C-----------------------------------------------------------------------
      CHARACTER*128 DEVICE
      CHARACTER*80 GTYPE, GVER
      INTEGER I, J, L, L1, L2
      REAL X, X1, X2, Y, Y1, Y2, R, XI, XP, YP
      REAL PX(43), PY(43)
      DATA PX / 0.0,2.0,4.0,6.0,8.0,10.0,12.0,14.0,16.4,17.0,17.3,
     1          17.8, 18.5, 20.0, 22.0, 24.0, 26.0, 28.0, 29.0,
     2          28.8,27.2,25.0,23.0,21.5,21.1,21.5,22.8, 24.1, 25.1,
     3          25.2, 24.2, 22.1, 20.0, 18.0, 16.0, 14.0, 12.0,
     4          10.0,  8.0,  6.1,  4.2,  3.0,  1.3 /
      DATA PY / 8.8, 7.6, 7.1, 7.4, 8.0, 8.9, 9.6, 9.9, 9.4,
     1          9.7, 12.0, 14.0, 16.1, 17.0, 17.0, 16.0, 13.9,
     2          13.1, 13.2, 12.3, 11.5, 11.5, 11.5, 11.2, 10.5,
     3          9.0, 8.0, 7.0, 5.1, 3.6, 1.9, 1.1, 0.9, 0.7,
     4          0.8, 1.0, 1.0, 1.2, 1.8, 2.1, 2.9, 4.1, 6.0 /
C
      CALL PGQINF('DEV/TYPE', DEVICE, L)
      CALL PGQINF('VERSION', GVER, L1)
      CALL PGQINF('TYPE', GTYPE, L2)
      CALL PGBBUF
C
C Clear the screen; set background color.
C
      CALL PGPAGE
      CALL PGSCR(0,0.0,0.0,0.35)
C
C Draw a frame at the physical extremities of the plot.
C Dimensions are X by Y (inches).
C
      CALL PGVPORT(0.0, 1.0, 0.0, 1.0)
      CALL PGQVP(1, X1, X2, Y1, Y2)
      X = X2-X1
      Y = Y2-Y1
      CALL PGWINDOW(0.0, X, 0.0, Y)
      CALL PGSFS(2)
      CALL PGRECT(0.0, X, 0.0, Y)
      CALL PGMOVE(0.5*X, 0.0)
      CALL PGDRAW(0.5*X, Y)
      CALL PGMOVE(0.0, 0.5*Y)
      CALL PGDRAW(X, 0.5*Y)
C
C Draw a circle of diameter 0.5 x min(x,y)
C
      R = 0.25*MIN(X,Y)
      CALL PGCIRC(X*0.5, Y*0.5, R)
C
C Draw some more circles with different line-styles; this tests
C the dashing algorithm on curved lines.
C
      CALL PGSLS(2)
      CALL PGCIRC(X*0.5, Y*0.5, R*1.1)
      CALL PGSLS(3)
      CALL PGCIRC(X*0.5, Y*0.5, R*1.2)
      CALL PGSLS(2)
      CALL PGSLW(3)
      CALL PGCIRC(X*0.5, Y*0.5, R*1.3)
      CALL PGSLS(1)
      CALL PGSLW(1)      
C
C Demonstrate different line-styles
C
      DO 10 I=1,5
          CALL PGSLS(I)
          CALL PGMOVE(I*(X/20.0),0.0)
          CALL PGDRAW(I*(X/20.0),Y)
   10 CONTINUE
      CALL PGSLS(1)
C
C Demonstrate different line-widths
C
      DO 20 I=1,5
          CALL PGSLW(I)
          CALL PGMOVE(0.0, I*(Y/20.0))
          CALL PGDRAW(X, I*(Y/20.0))
   20 CONTINUE
      CALL PGSLW(1)
C
C Draw dots in different thicknesses.
C
      DO 30 I=1,21
          XP = 6*Y/20.0
          YP = I*Y/22.0
          CALL PGSLW(I)
          CALL PGPOINT(1,XP,YP,-1)
   30 CONTINUE
C
C Demonstrate different line-colors
C
      CALL PGSLW(4)
      DO 40 I=0,15
          CALL PGSCI(I)
          XI = (I+20)*(X/40.0)
          CALL PGMOVE(XI,0.0)
          CALL PGDRAW(XI,Y)
   40 CONTINUE
      CALL PGSCI(1)
      CALL PGSLW(1)
C
C Demonstrate fill area
C
      DO 50 J=1,43
         PX(J) = (PX(J)+50.0)/100.0*X
         PY(J) = (PY(J)+75.0)/100.0*Y
   50 CONTINUE
      DO 70 I=0,3
          CALL PGSCI(I)
          CALL PGSFS(1)
          CALL PGPOLY(43,PX,PY)
          CALL PGSCI(1)
          CALL PGSFS(2)
          CALL PGPOLY(43,PX,PY)
          DO 60 J=1,43
             PY(J) = PY(J)-0.25*Y
   60     CONTINUE
   70 CONTINUE
C
C Write the device type on the plot.
C
      CALL PGWINDOW(0.0, 1.0, 0.0, 1.0)
      CALL PGSFS(1)
      CALL PGSCI(0)
      CALL PGRECT(0.31, 1.0-0.31, 0.85, 0.97)
      CALL PGSCI(1)
      CALL PGSFS(2)
      CALL PGRECT(0.31, 1.0-0.31, 0.85, 0.97)
      CALL PGPTEXT(0.5, 0.91, 0.0, 0.5, 'PGPLOT '//GVER(1:L1))
      CALL PGPTEXT(0.5, 0.87, 0.0, 0.5, 'Device '//GTYPE(1:L2))
C
      CALL PGEBUF
C-----------------------------------------------------------------------
      END

      SUBROUTINE PGCIRC(X0, Y0, R)
      REAL X0, Y0, R
C
C Draw a circle of radius R, center (X0, Y0) [world coordinates].
C
      REAL A, XI, YI

      CALL PGMOVE(X0+R,Y0)
      DO 10 A=0.,360.,1.0
         XI = X0 + R*COS(A/57.29577951)
         YI = Y0 + R*SIN(A/57.29577951)
         CALL PGDRAW(XI,YI)
   10 CONTINUE
      END
