!-----------------------------------------------------------
!version: 25 September 2003
!
!             READ ME FIRST
!     This beta-test code is provided by R. Grotjahn. It is a
!     shell fortran file to illustrate use of ncar graphics 
!     to plot a point symbol and a line.
!     It also illustrates some print and write commands.
!     The intention is to provide the outlines of a simple 
!     program in which you can insert your own calculation
!     It is recommended that you make a copy and modify the copy.
!
!----------------------------------------
!
!             READ ME SECOND
!      This is a script & fortran file to run a basic template of a
!       program to draw lines and plot a 1-d array of points.
!      Solid lines are illustrated
!     
!      This program also illustrates a simple written output procedure
!       as well line plotting.  See READ ME THIRD
!
!      Other notes:
!       to run your program, you make 2 unix commands. The first
!         compiles & links the files; the second runs the job.
!       Assume your file is called ptline.f then:
!         ncargf90 ptline.f
!         ./a.out
!
!       To see your printed output look for the output file.
!       To see your plotted output type:
!           idt gmeta
!
!        (default names are used above)
!ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
!
!         READ ME THIRD
!
!      your source code to generate the arrays goes between the 
!        first pair of lines that contain c repeated many times. To
!        change the calls to the plot generation, make mods between
!        the next pair of lines containing c repeated many times.
!       
!     For the purpose of this example, arrays F and X are created.
      PROGRAM PtLinePlot
!     highly recommended to declare implicit none, to avoid using 
!      undefined variables.
      implicit none
!
      PARAMETER (Nx=81)
!      Nx = # PTS IN F and  X, 
!
      real F(Nx),X(NX)
      real dx,pi,fmin,fmax,xmin,xmax,xw,xe,xmid,fabv
      integer nx,i,nxp
      CHARACTER*8 CHRS
      character*1 chrpt
!      all statements above are non-executable
      PI=acos(-1.)   ! define pi
      FMIN=1.E36
      FMAX=-1.E36
      dx=0.05
!      loop over the x points to calculate the corresponding f values
      DO I=1,NX
        X(I)=FLOAT(I-1)*dx-2.
        F(I)=COS(PI*X(I))
        IF(F(I)>FMAX) FMAX=F(I)
        IF(F(I)<FMIN) FMIN=F(I)
      end do
!      DEFINE x-axis plotting RANGES
      xmin = X(1)
      Xmax = X(NX)
!
!     printed output to the screen examples:
!     unformatted printing:
      print *,'xmin & xmax =',xmin,xmax
      print *,'fmin & fmax =',fmin,fmax
!     formatted printing
      print 11,xmin,xmax,nx
   11 format(1h ,' xmin=',f8.3,'  xmax=',e12.3,5x,'nx=',i4)
!
!    The next few lines
!         show an example set up for writing to a file called numric.dat
      open(9,file='./numric.dat',   
     &   form='formatted',status='unknown')
!
!      some example output forms
!     unformatted writing
      write(9,*)'This is a test program'
      write(9,*)'xmin & xmax =',xmin,xmax
      write(9,*)'fmin & fmax =',fmin,fmax
!     formatted printing
      write(9,11)xmin,xmax,nx
      close(9)
!
!
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
!
!      NOW PLOT things
!
!      start plot commands
      CALL OPNGKS
!      GSCLIP(0) call allows plotting OUTSIDE the SET CALL RANGEs
      CALL GSCLIP(0)

!      FIRST, HERE ARE SOME SIMPLE PLOT ROUTINES THAT ARE EASY TO CALL
!    
!     assign & scale a portion of the plotter screen for your plot
      call set(0.1,0.9,0.1,0.9,xmin,xmax,fmin,fmax,1)
!
!      NXP IS THE NUMBER OF POINTS TO BE PLOTTED: NXP IS <= NX
      NXP=NX
!      PLOT F VS X as # signs
!      encode a $ sign in character format into variable chrpt
      WRITE(CHRpt,'(1h$)')
      do i=1,nxp
      call plchhq(x(i),f(i),chrpt,8.,0.,0.)
!      plchhq plots chrpt at location x(i),f(i) with size 8.
      end do
!
!     now plot a square around the plot using the scaling
!     each line call has x,f starting location then x,f ending loc.
      call line(xmin,fmin,xmin,fmax)
      call line(xmin,fmin,xmax,fmin)
      call line(xmax,fmax,xmin,fmax)
      call line(xmax,fmax,xmax,fmin)
!
!     write a character string into chrs, then plot chrs above box
      xmid=(xmax+xmin)/2.
      fabv=fmax*1.05
      WRITE(CHRs,'(4hNxp=,i4)')Nxp
      call plchhq(xmid,fabv,chrs,12.,0.,0.)

!     flush the plot buffer and advance to the next frame
      call frame
!
!      end plot commands
      CALL CLSGKS
!
!cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      stop
      end
