C Demonstration – the XYPLOT Program

 C.1 Fortran source
 C.2 Input file
 C.3 The plot

The following pages contain a listing of the Fortran source for a program called XYPLOT, plus a sample input file and the plot it produces. As well as demonstrating several of the facilities described in this document, XYPLOT may be useful as a template for developing other simple graphics utilities, and can be used in its own right as a convenient way of producing a presentable plot of a list of (x,y) data. The Fortran source may be found in the file NCAR_DIR:XYPLOT.FOR on VAX/VMS and in the archive /star/starlink/lib/snx/examples/snx-examples_source.tar on UNIX. On VAX/VMS, this program may be compiled, linked and run using the command sequence

  $ FORTRAN NCAR_DIR:XYPLOT
  $ LINK XYPLOT, NCAR_DIR:SNX_LINK/OPT, STAR_LINK/OPT
  $ RUN XYPLOT

On UNIX, this program may already be installed in directory /star/bin/examples/snx. If it has been deinstalled and removed to save space, you can copy the entire source directory to a scratch dircetory, and with the SYSTEM environment variable set appropriately, build and run it thus:

  % setenv SYSTEM alpha_OSF/1
  % ./mk xyplot
  % ./xyplot

XYPLOT reads an input file and plots a graph. The input file consists of a sequence of alphanumeric records, the first three of which are the labels (title, x-axis, y-axis) with the rest each containing an (x,y) point (in free format). The example data may be found in the file NCAR_DIR:XYPLOT.DAT on VAX/VMS and /star/bin/examples/snx/xyplot.dat on UNIX (if installed). The resulting graph consists of a line joining the points in the order in which they are given.

C.1 Fortran source

        PROGRAM XYPLOT
  *+
  *     - - - - - - -
  *      X Y P L O T
  *     - - - - - - -
  *
  *   Plot xy data from a file using NCAR package.
  *
  *   Input is from LU 1.  The format is:
  *
  *           graph label text
  *           x label text
  *           y label text
  *           x y
  *           x y
  *           x y
  *            :
  *            :
  *
  *   P T Wallace   Starlink   May 1987
  *
  *+
        IMPLICIT NONE
  
        INTEGER N,NP,NPMAX
        PARAMETER (NPMAX=10000)
        CHARACTER FNAME*80,GLAB*80,XLAB*80,YLAB*80
        REAL X(NPMAX),Y(NPMAX)
  
  
  *  Open input file
        PRINT *,’Filename?’
        READ (*,’(Q,A)’) N,FNAME
        OPEN (UNIT=1,STATUS=’OLD’,FILE=FNAME(:N),READONLY)
  
  *  Read label text
        READ (1,’(A)’,END=100) GLAB
        READ (1,’(A)’,END=100) XLAB
        READ (1,’(A)’,END=100) YLAB
  
  *  Read x,y data
        DO NP=1,NPMAX
           READ (1,*,END=100) X(NP),Y(NP)
        END DO
        NP=NPMAX+1
  
  *  Adjust number of points
   100  CONTINUE
        NP=NP-1
  
  *  Plot the graph
        IF (NP.GE.2) THEN
           CALL SNX_AGOP
           CALL SNX_EZRXY(X,Y,NP,XLAB,YLAB,GLAB)
           CALL SGS_CLOSE
        ELSE
           PRINT *,’*** Insufficient Data ***’
        END IF
  
        END

Notice that most of the program is devoted to reading in the data, and that the plotting itself requires just three calls – to open, plot and close respectively.

The reasons SNX_EZRXY expects you to do the SGS/GKS open and close rather than providing truly one-call plotting by doing them itself are:

The trade-off is fractionally less convenience versus greatly increased versatility.

C.2 Input file

The beginning of the file which was used to produce the graph on the next page is given below. Note the use of PWRITX function codes in the three label records; this assumes the program has been linked with the special version of the AGPWRT routine (see §3 and 7). Deliberately complicated example labels have been contrived; most real applications would require just plain text without PWRITX codes.

  A Bogus Star ’V:-50IGL’Y’H:-80KRU’C  DLT 1986
  Wavelength / ’.A’
  F’GLB’K’NRU’ / 10’S’-26’N’ erg cm’S’-2’N’ s’S’-1’N’ ’.AS’-1
     4004.000      0.1888006
     4008.000      0.0000000E+00
     4012.000      0.0000000E+00
     4016.000      0.3046547
     4020.000      0.3328641
     4024.000      3.8437031E-02
     4028.000      0.2980890
               :
               :
               :
               :

XYPLOT is a convenient tool for quickly debugging complicated label strings. With a text editor, set up a dummy input file consisting of the label string you want to try out, then two more arbitrary labels and two arbitrary data points. Run the XYPLOT program and inspect the x-axis label; if wrong, edit the data file and try again.

C.3 The plot

XYPLOT produced the following graph when the example input file given in the previous section was used. Further copies can be obtained by running XYPLOT and replying with the name of the example input file to the ‘Filename?’ prompt.


pdfpict