#!/bin/csh
#-----------------------------------------------------------
#version: 3 January 1997
#
#             READ ME FIRST
#     This beta-test code is provided by R. Grotjahn. It is a
#     shell program to illustrate use of ncar graphics by
#       creating an executable file; hence the program can be 
#       as a UNIX command by typing the file name and hitting "return."
#     shell script changes are needed to make the program run.
#       Look for UNIX script changes between successive lines 
#       starting with: # chg
#     you will also want to modify the FORTRAN code, see
#       READ ME SECOND.
#
#----------------------------------------

cat >! template.for << 'EOF'
c
c             READ ME SECOND
c      This is a script & fortran file to run a basic template of a
c       program to make contour plots of 2-d arrays.
c      Users of this script need to modify the unix commands at the
c       end of this file:  several cp commands in order to
c       direct program input and output to their own directory under
c       a name of their preference. 
c     
c      This program also illustrates a simple written output procedure
c       as well contour plotting.  See READ ME THIRD
c
c      Other notes:
c       don't forget to make your file executable: chmod u+x contour.for
c
c
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c
c         READ ME THIRD
c
c      your source code to generate the 2-d arrays goes between the 
c        first pair of lines that contain c repeated many times. To
c        change the calls to the plot generation, make mods between
c        the next pair of lines containing c repeated many times.
c       
c      For the purpose of this shell example, arrays PSIP and PSI are created.
      PROGRAM Contour
c
      PARAMETER (NY=21,Nx=41)
C      NY=GRID PTS IN Y, NX GRID PTS IN X, 
      DIMENSION Y(NY),X(NX), PSIP(NX,Ny),PSI(NX,Ny)
C
      CHARACTER*8 CHRS
      COMMON /DOMAIN/ XWd,XEd,YSd,YNd
      PI=acos(-1.)
      DO 1 I=1,NX
      X(I)=FLOAT(I-1)*0.1-2.
      DO 1 J=1,NY
      Y(J)=FLOAT(J-1)*0.1-1.
      PSIP(I,J)=17.*EXP(-X(I)**2-Y(J)**2)
      PSI(I,J)=27.2*SIN(0.5*PI*X(I))*COS(PI*Y(J)/2.-X(I)/3.)
    1 CONTINUE
C      DEFINE RANGES
      XW=X(1)
      XE=X(NX)
      YS=Y(1)
      YN=Y(NY)
c
c
c      FYI:
c
c    The next few lines are commented out and can be ignored, these lines
c         show an example set up for writing to a file called numric.dat
c      open(9,file='./numric.dat',
c     1    form='formatted',status='unknown')
c
c      some example output forms
c      write(9,*)'This is a test program'
c      ba=2.
c      write(9,19)ba
19      format(//,'The value of ba is',f5.1)
c      close(9)
c
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
C
C      NOW PLOT PSIP and PSI
C
c      start plot commands
      CALL OPNGKS
      g=1.
c      input ranges, neg values may be needed due to constraints of the
c        call to subroutine set in subroutine ploty (see note below)
c      these ranges are also used to label axes.
c      left & right edges of "x" range
      XWd=Xw
      XEd=Xe
c      bottom & top edges of "y" range
      YSd=YS
      YNd=YN
c      note: the set call requires: xwd<xed, ysd<ynd.  If you have pressure
c        as a vertical coordinate, you may want to use negative values 
c        for ysd and ynd: i.e. ysd=-1000  ynd=-200 will put "-200" label
c        at the top of the axis and "-1000" at the bottom.  Of course...
c        you could always modify subroutine ploty to suit specific needs...
c
c      create a plot title 8 characters long
      WRITE(CHRs,'(8hPSI SB P)')
c      cint is the contour interval
      cint=1.5
c      create a plot and advance to the next page when done
      call ploty(PSIP,nx,nx,ny,g,chrs,cint)
c      start creation of second plot, etc.
      WRITE(CHRs,'(8h   PSI   )')
c      using cint=0. tells ncar graphics to choose the contour interval
      cint=0.
      call ploty(PSI,nx,nx,ny,g,chrs,cint)
c      end plot commands
      CALL CLSGKS
c
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
      stop
      end
c
c------------------------------------------------------------------
c
      SUBROUTINE PLOTY(PLTA,NDIM,NX,NY,G,CHRS,CINT)
c      While this sub can be used as is, you may wish to improve the
c       appearance of your plots by modifying it as you become 
c       familiar with ncar graphics.
c      Sub plots array plta.  
c      Plot the first nx columns of the array plta which
c       is dimensioned to have ndim columns.  plot the first ny rows
c       which must be less than or equal to the dimensioned size of
c       plta in the main program.
c      chrs contains a short label that is passed through
c      cint is the contour interval which is also passed through
c      g is an optional variable numeric label which could be encoded
c        using a write statement and plotted using a plchhq call.
c        (the latter is included, but commented out).
      DIMENSION PLTA(NDIM,NY)
      CHARACTER*8 CHRS
      CHARACTER*8 CHR
      COMMON /DOMAIN/ XW,XE,YS,YN
C      CHR: EXAMPLE FOR ENCODING A VARIABLE USED IN A GRAPH LABEL (OPT.)
      WRITE(CHR,'(5htime=,F3.1)')G
C      SET SPECIAL VALUE FOR CONTOURING TO AVOID GRID PTS WITH SPV VALUE
      CALL CPSETR('SPV',1.E36)
C      GSCLIP(0) CALL SO THAT WHAT IS OUTSIDE SET CALL RANGE IS ALLOWED
      CALL GSCLIP(0)
C      SET CALL USED TO DEFINE THE LOCATION OF PLOT ON MONITOR SCREEN
C      CALL SET(.2,.8,.25,.75,0.,1.,0.,1.,1)
C      NEXT 6 LINES NEEDED TO GET CNTR PLT TO FIT INSIDE SET RANGES
C      CALL SET(.2,.7,.25,.75,XW,XE,YS,YN,1)
      CALL SET(.1,.9,.35,.75,XW,XE,YS,YN,1)
      CALL CPSETI('SET',0) !1=CONPACK CALLS SET, =0 NO SET CALL
      CALL CPSETR('XC1',XW)
      CALL CPSETR('XCM',XE)
      CALL CPSETR('YC1',YS)
      CALL CPSETR('YCN',YN)
C
      CALL CPSETR('SPV',1.E36) ! SPECIAL VALS TO AVOID WITH CNTR LINE
      CALL CPSETR('CWM',1.8)  ! SCALE FACTR TO MULT ALL DEFAULT LABL SIZES
C
C      CALL CPSETI('LLP',-2)  !QUALITY OF LABL PLACEMNT 1=WORST 3=BEST
      CALL CPSETI('LIS',4)  !INTRVL BETWEEN LABLED LINES
C      SECOND TEST
      CALL CPSETI ('LLP - LINE LABEL POSITIONING',3)! USE FORTRAN FORMAT 
C     +   SCIENTIFIC NOTATION IF VALUE =3
C
      CALL CPSETI ('HLB - HIGH/LOW LABEL BOX FLAG',1) !ASK 4 BOX AROUND
C     +   HI/LO LABEL
C
      CALL CPSETI ('HLO - HIGH/LOW LABEL OVERLAP',4) !NO LABL OUTSIDE SET
C     +   RANGE
C
C      CALL CPSETR('HLS - HI/LO LABEL SIZ',.012)  ! DEFAULT IS .012
C     CALL CPSETI ('NCL - CONTOUR LEVEL SELECTION FLAG', NCL)
C
      CALL CPSETR ('PC1 - PENALTY SCHEME CONSTANT 1',1.5)
      CALL CPSETR ('T2D - TENSION ON THE 2D SMOOTHER',1.)
C      NEXT LINE FINALLY DRAWS THE CONTOUR LINES
      CALL CPCNRC(PLTA,NDIM,NX,NY,0.,0.,CINT,-1,0,-1)
C      CALL CPCNRC(PLTA,NDIM,NX,NY,0.,0.,0.,-1,0,-1)
C
C      NOW ADD VARIOUS LABELS AND TICK MARKS
C      AXES/PERIMETER LABELS
C      IF IX OR JY =1 USE RT EDGE OR TOP EDGE FOR LABELS
      IX=0
      JY=1
C      BORDER LABELS LETTER SIZE
      ISIZ=12
      JSIZ=12
C      IOR=0 FOR HORZ LABLS,  =1 FOR VERTICAL ORIENTED LABELS,
C      LABMOD ALSO SPECIFIES FORMAT OF LABELS, SIZE, ETC.
      CALL LABMOD('(F5.1)','(F5.1)',0,0,ISIZ,JSIZ,IX,JY,IOR)
c      CALL LABMOD('(F5.3)','(F5.3)',0,0,ISIZ,JSIZ,IX,JY,IOR)
C      NOW DETERMINE TYPE OF BORDER AROUND CONTOURS, E.G. A GRID, A BOX
C       WITH TICK MARKS, ETC.  SPECIFY IF TICK MARKS HAVE LABELS, ETC.
C      PLOT BORDER/LABELS,  1ST 4 ARGS OF GRIDAL LIKE OLD SUB PERIM
C      IG,JG =0 THEN LIKE PERIM, =1 THEN LIKE PERIML
      IG=0
      JG=0
      IF(G.GE.1.) THEN
      IG=1
      JG=1
      END IF
C      IGTYP =0 FOR GRID, =5 PERIMETR, =10 AXES, OTHER COMBS POSS.
      IGTYP=5
C      LAST 2 ARGS GRIDAL ARE INTERSECTN OF AXES IN X,Y VALUES
C      CALL GRIDAL(NX-1,0,NY-1,0,IG,JG,IGTYP,0.,0.)
      NX3=(NX-1)/3
      NY3=(NY-1)/3
C      CALL GRIDAL(NX3,3,NY3,3,IG,JG,IGTYP,0.,0.)
      XDIF=(XE-XW)/5.
      YDIF=(YN-YS)/5.
      NX3=IFIX(XDIF)+1
      NY3=IFIX(YDIF)+1
      CALL GRIDAL(NX3,5,NY3,5,IG,JG,IGTYP,0.,0.)
C      NOW ADD GRAPH TITLE AND OTHER LABELS
C      THE SET CALL IS FOR PLACEMENT OF THESE LABELS
      CALL SET(.05,.9,.05,.9,0.,1.,0.,1.,1)
C      GRAPH TITLE CHRS PASSED THROUGH 
      CALL PLCHHQ(.5,0.05,CHRS,12.,0.,0.)
C      PLOT ANOTHER GRAPH LABEL CHR (OPT.)
      CALL PLCHHQ(.5,0.1,CHR,12.,0.,0.)
      CALL FRAME
       RETURN
      END
c
c
'EOF'
#
ncargf77 -V -show xref template.for 
echo 'run job and create output file'
# nice sets a lower priority for background jobs
# adding a trailing (second) "&" says execute in a "background" type mode 
nice a.out >& output   # std out + std err to file output
# copy output to a specified file name
echo ' write output data to disk '
# chg: change next line to file name to your choice
cp output ./contour.out
# chg-------^^^^^^^^^^^----------------------------------------
# -V option creates compiled listing (.l file) which is copied (to a .lst file) next
cp template.l ./contour.lst
# chg-------^^^^^^^^^^^----------------------------------------
# write output data to a temp disk
echo ' write plot data to disk '
# next write any metafile that was created to a specified file
# chg: change next line to file name to your choice
cp gmeta  ./contour.plt
# chg-------^^^^^^^^^^^---------------------------------------
# chg: change file name to your choice (default: not implemented)
#cp ./numric.dat ./contour.dat
# chg--------------^^^^^^^^^^^---------------------------------
# remove exe file when done in case next time have compilatn errs
rm a.out
rm gmeta
# error exits to here
exit
# chg: change file name to your choice
cp log ./contour.log
# chg--^^^^^^^^^^^^^--------------------------------------------








