Plot Layout

Pattern Fill


Pattern filling between contour lines is not available in grads. The only option available for filling is color shading. There are ways out, however, and here I present 3 that came up from a recent discussion in grads user list.

Layers

One can print two separate images from grads, one with the color shaded variable plotted, and the other with a second variable plotted using a solid contour line. Outside grads (or within but running shell command via !) you can use ImageMagick tools:

mogrify -transparent rgb(<red value>,<green value>,<blue value>) first_image.gif
mogrify -transparent rgb(<red value>,<green value>,<blue value>) second_image.gif
composite atop first_image.gif second_image.gif combined_image.gif

This would compose in one image the first and second images, which have their background been made transparent (actually whichever color defined by rgb(<red value>,<green value>,<blue value>)). Note that you can also use mogrify to automatically crop the large image border left by Grads.

All within grads

You can create a stippling effect for your top layer (area of freezing rain) like this:

set gxout grid
set grid off
set gridln off
set digsiz 0.02
d const(maskout(cfrzr,cfrzr-0.5),0)

This will draw little tiny 0's at the center of every grid point that has a cfrzr value > 0.5. You may have to play with the digsize to get it to look just right. And you can use 'set ccolor' to control the color of the stippling.

Automatic Script

The best option, however, is to use Kodama's script to do that. Quite a long and complicated
gs script, so I won't go through it. Here where you can find it:

http://wind.geophys.tohoku.ac.jp/~kodamail/wiki/wiki.cgi/GrADS/script/hatch.gs?lang=en

It allows you to choose the angle, the density and the precision of the hatched lines.
For example:

set gxout shaded
d sst
set gxout contour
d sst
hatch.gs sst -min 21 -max 24 -angle 45 -density 0.01 -int 0.1

The images bellow show the same plot done with the two latter methods:

jeni.pngkoda.png

Fig. 20th century mean SST in degree C. Left: using "gxout grid". Right:using Kodama's script

Axis


Getting rid of axis labels but keeping ticks

There are two solutions, both nice and clean.

Solution 1: This is based on 'set ylabs' command. The general syntax is:

set ylabs lab1 | lab2 | lab3 | ...

and it allows you to specify anything to be placed as labels. GrADS will then divide the axis range in equal spaces and draw the labels. So you don't want any labels? Just replace them by empty spaces:

ga-> set ylabs | | |

in this example GrADS will draw 4 tick marks because you give him 4 empty spaces (there are 3 separators)

Solution 2: This one is based on 'set ylab' command. The general syntax is:

set ylab on | off | auto | string

and we will set the ylab with the string option. The trick is:

ga-> set ylab `3 `0

setting ylab to a constant string which is empty. Note the accent grave for switching between the fonts.

Changing the axis title offset

There ain't a simple command to change the distance between an axis title and the axis itself.

What one can do is, instead of using the GrADS default title commands (eg draw title or draw ylab), set a string characteristics and draw the string explicitly. The disadvantage is that one has to use more than 1 line to accomplish drawing labels and the such:

set strsiz width <width> 
set string color <justification <thickness <rotation>>> 
draw string xpos ypos string

A more concret example:

ga-> set strsiz 0.14 
ga-> set string 1 r 2 0 
ga-> draw string 10.25 1.0 my_x_title 
ga-> set string 1 r 2 90 
ga-> draw string 0.2 7.25 my_y_title
  • Line1. Setting the string character size by changing the width
  • Line2. Choose a right justified white string with width 2 and no rotation
  • Line3. Draw X-axis title
  • Line4. The same as above, but rotate 90deg counterclockwise
  • Line5. Draw Y-axis title

Two graphs with different y-scale in the same plot

So how can we plot two 1-D curves in the same graph if they have different y-scales? For instance, that could be two time series: convective cloudiness and convective heating rate.

What we need is to draw a second y-axis on the right side of the graph with the scale of the second plot. That's done with:

'set ylpos offset side'

Bellow you will find a script that does the job, it is fully commented so you won't have any trouble to adapt it for your needs.

* reinit grads and turns off grid lines and grads labels 
'reinit' 
'set grid off' 
'set grads off' 
'open mydata.ctl' 
* set graph area to full window but 1in margin all around 
'set parea 1. 10. 1. 7.5' 
* set time range and fix lat/lon 
'set t 1 12' 
'set x 1' 
'set y 1' 
'set lev 500' 
**** First graph **** 
* no markers, solid line style, white color and thick line 
'set cmark 0'; 'set cstyle 1'; 'set ccolor 1'; 'set cthick 6' 
* fix y-range and y-step so that grads does what we want 
'set vrange 0 7' 
'set ylint 1' 
* y axis is drawn white, thick and labels have size 0.20 
'set ylopts 1 6 0.20' 
* x axis is drawn blue, thin and labels have size 0.15 
'set xlopts 4 1 0.15' 
'd tloop(aave(cvlh*86400,lon=-70,lon=-50,lat=-15,lat=0))' 
* draw first y label 
'draw ylab Convective Heating Rate (K/day)' 
**** Second graph **** 
* no markers, solid line style, red color and thick line 
'set cmark 0'; 'set cstyle 2'; 'set ccolor 2'; 'set cthick 6' 
* IMPORTANT: must fix y-range and y-step otherwise grads will 
* try to use the same as the first plot! 
'set vrange 0.0 0.05' 
'set ylint 0.01' 
* y axis is drawn red, thick and labels have size 0.20 
'set ylopts 2 4 0.20' 
* IMPORTANT: place second y-axis on the right 
'set ylpos 0 r' 
'd tloop(aave(cvcl,lon=-70,lon=-50,lat=-15,lat=0))' 
* IMPORTANT: can't use draw ylab for 2nd label 
'set strsiz 0.2' 
'set string 2 c 6 -90' 
'draw string 10.5 4.25 Convective Cloud [0-1]' 
* Graph title 
'draw title Convection at 500mb' *end

Some remarks about the second plot:

  1. You must fix y-range both for the first and second plots. If you don't do that GrADS will try to draw the second with the same y-scale as of the first.
  2. Remember to place the second axis on the right. That's done with 'set ylpos 0 r'.
  3. Unfortunatelly you won't be able to use 'draw ylab …' to draw the second y-axis label. You will need to do that by hand, i.e., with string primitives.

Some remarks about time series:

  1. It is necessary to fix lat/lon dimentions to plot a time series of an area average. You must also wrap the area average function with a tloop() function, otherwise GrADS will complain.

Here is the result:

Fonts


Using special fonts (subscript, symbol, etc…)

The backquote character ` can be imbedded in any grads text string to change to new fonts or modes within the string. This is valid for commands such as draw string, draw title, draw xlab, draw ylab, set clab, etc. Multiple escapes can be combined in a single string and the sintax is simple

Special Fonts
`b below
`n normal
`a above
`# change the font to 0-5
0 = helvetica
1 = roman
2 = italics roman
3 = symbols
4 = bold helvetica
5 = bold roman

An example of using a superscript:

draw title Watts/m`a2`n for Tuesday

Note that the `a sets the mode to "above" or superscript, and `n sets the mode back to "normal".

An example of using a subscript:

draw title Z`b500`n for Tuesday

where the `b sets the mode to "below" or subscript.

An example using the symbol fonts.

draw title Temperature in `3.`1C over Europe

This can also be used in contour labels, for example:

set clab %g`3.`1C
display ts-273.16

which would produce contour labels of the number followed by the degree symbol followed by the capital C character.

There are 2 GrADS scripts that will make your life easier: font.gs (by Brian Dott) and tfonts.gs (by Alan Robock). After getting these files, run

ga-> run tfont.gs

to show examples. Look at the text in tfont.gs to see how it was done. Use font.gs to display the available characters for a given font, e.g.:

ga-> run font.gs 3

will show the correspondence between the keyboard keys and the symbol fonts.

Geometry


Drawing a rectangle with lon/lat coordinates

GrADS has intrinsic functions to draw filled rectangles and unfilled rectangles. You probably know these functions:

draw rec xlo ylo xhi yhi
draw recf xlo ylo xhi yhi

The problem with these functions is that they use XY screen coordinates, and most of our plots are in world coordinates. Of course it is possible to convert between these two, but doing that every time you need to draw a rectangle is really annoying.

So I took sometime to write a script that does that:

drawrec lon_min lon_max lat_min lat_max <fill> <warn on|off>

It needs at least the four coordinates. There are optional parameters to draw a filled or unfilled rectangle, and to turn on or off warning messages.

Here's an example:

ga-> open vegetation.ctl
ga-> d mask
ga-> drawrec 285 310 -12 5
ga-> drawrec 180 240 -5 5 1
boxexample.gif

And here's the code:

*
* drawrec.gs
* 
* This function draws a rectangle over a map using World Coordinates,
* i.e., longitude and latitude.
*
* Usage: drawrec lon_min lon_max lat_min lat_max &#60;fill&#62; &#60;warn on|off&#62;
*        &#60;fill&#62; can be 0 or 1
*        &#60;warn on|off&#62; can be on or off
*
* Written by Henrique Barbosa July 2004
* Last update September 05
*
function drawrec(args)

* Get arguments
    Rxa = subwrd(args,1)
    Rxb = subwrd(args,2)
    Rya = subwrd(args,3)
    Ryb = subwrd(args,4)
    fil = subwrd(arqs,5)
    opt = subwrd(args,6)

* Test for parameters
  if (Rxa='' | Rxb='' | Rya='' | Ryb='') 
    say 'usage: drawrec lon_min lon_max lat_min lat_max <fill> <warn on|off>
    return 
  endif

* Find allowed dimensions
  'q dims'
  lonlin =sublin(result,2)
  Bxa = subwrd(lonlin,6)
  Bxb = subwrd(lonlin,8)
  latlin =sublin(result,3)
  Bya = subwrd(latlin,6)
  Byb = subwrd(latlin,8)

* Test if user input within allowed dimensions
  if (opt!='off')
    if (Rya<Bya | Rya>Byb); say 'warn: lat_min='Rya' is outside range ['Bya','Byb']'; endif
    if (Ryb<Bya | Ryb>Byb); say 'warn: lat_max='Ryb' is outside range ['Bya','Byb']'; endif

    if (Rxa<Bxa | Rxa>Bxb); say 'warn: lon_min='Rxa' is outside range ['Bxa','Bxb']'; endif
    if (Rxb<Bxa | Rxb>Bxb); say 'warn: lon_max='Rxb' is outside range ['Bxa','Bxb']'; endif
  endif 

* Transform world coordinates to xy
  'q w2xy 'Rxa' 'Rya
  x1=subwrd(result,3)
  y1=subwrd(result,6)

  'q w2xy 'Rxb' 'Ryb
  x2=subwrd(result,3)
  y2=subwrd(result,6)

* Draw a Filled/Empty rectangle
  if (fil=1)
    'draw recf 'x1' 'y1' 'x2' 'y2
  else
    'draw rec 'x1' 'y1' 'x2' 'y2''
  endif
return

GrADS pages you should read:
set strsiz
set string
draw string

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.