Gri: Hints for Gri programming

- Programming Gri 1: Introduction 2: Simple example 3: Fancy example 4: Running Gri 5: Programming Gri 6: General Issues 7: X-Y Plots 8: Contour Plots 9: Image Plots 10: Examples 11: Handling Data 12: Gri Commands 13: Gri Extras 14: Evolution of Gri 15: Installing Gri 16: Gri Bugs 17: System Tools 18: Acknowledgments 19: License 20: Newsgroup 21: Concept Index | ## 5.11: 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 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
      ...
- Creat 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|y scale`' before each new panel, so you will start afresh. Clearly identify 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 optionally draw a curve
      open \f
      read columns x y
      close
      if ..num_col_data..
        draw curve
        draw label "\f" at \
            {rpn ..lastx.. xusertocm 0.5} \
            {rpn ..lasty.. 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`' loops later see Query. Use multiple ``draw title`' commands:
      draw title "Atlantic water entering Arctic Ocean"
      draw title "\.command_file. \.time."