Gri: Hints for Gri programming

- Chapters: 1: Introduction 2: Simple example 3: Invocation 4: Finer Control 5: X-Y Plots 6: Contour Plots 7: Image Plots 8: Examples 9: Gri Commands 10: Programming 11: Environment 12: Emacs Mode 13: History 14: Installation 15: Gri Bugs 16: Test Suite 17: Gri in Press 18: Acknowledgments 19: License Indices: Concepts Commands Variables | ## 10.12: Hints for Gri Programming Here are some hints for good Gri programs: Whenever working with grids (for contouring) or images, make use of the ``show grid`' or ``show image`' commands. They will give you useful information about the statistics (min/max/histogram) of the items. Use the operating system, not Gri, to manipulate your data. For example, if you have a file whose first column is x times 100, and third is the arcsin of y, you could do:
      open "gawk '{print $1/100, sin($3)}' |"
      read columns x y
- If you have x and y in a non-decimal geographical format (e.g. hour.minute-second format), use the operating system to convert for you (see Open). Use the ``pstack`' operator liberally in your rpn expressions to see what is going on (see rpn Mathematics). While developing programs, put a ``show columns statistics`' command after every ``read column`' command, to check that the data have been read correctly. Development time can be minimized by limiting the number of data being processed. For example, in a multi-panel plot, it is often necessary to try various alternatives before aesthetic scales and page layout is achieved. The process can be speeded up by limiting the number of data being processed, as shown below. (If Gri finds fewer data in the file than specified, it will simply use the data that it found; so when the program works, just change ``.n.`' into something large.)
      .n. = 100 # 10000 for later
      ...
      # Panel 1
      read columns .n. x y
      ...
      # Panel 2
      read columns .n. x y
      ...
- Create new commands to do repetitive work. Use ``draw time stamp`' on all plots except for publication versions:
      if !..publication..
        draw time stamp
      end if
- For multiple panels on one page, do ``delete x scale`' or ``delete y scale`' before each new panel, so you will start afresh. Clearly identify code for particular panels with comments. This reduces errors you might get if you shuffle things later. Use the ``..num_col_data..`' built-in variable to see how many data have passed the ``set input data window`' data window in the last ``read columns`' command. The following example shows how to avoid drawing an unwanted curve:
      open \f
      read columns x y
      close
      if ..num_col_data..
        draw curve
        draw label "\f" at \
            {rpn ..xlast.. xusertocm 0.5} \
            {rpn ..ylast.. yusertocm 0.2} cm
      end if
- Use synonyms and ``query`' for filenames. This makes programs much more flexible. Note that you can string synonyms together:
      \dir = "~/EOS/iso0/"
      query \file "Give file in directory \dir" ("1.dat")
      open \dir/\file
- It is also a good idea to give a restrictive list of possibilities in your ``query`' command, to avoid complicated ``if`' commands later (see Query). Use multiple ``draw title`' commands:
      draw title "Atlantic water entering Arctic Ocean"
      draw title "\.command_file. \.time."
Use the ``query`' command to interact with the user (see Query). The results can be stored in variables and synonyms.