Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
rh = read("relative_humidity.grib")

field_name       = grib_get_string(rh[1], 'name')
field_valid_date = grib_get_string(rh[1], 'validityDate')
field_level      = grib_get_double(rh[1], 'level')

title_text = field_name & ' ' & field_valid_date & ' at level ' & field_level
print(title_text)

title = mtext(text_line_1: title_text)

plot(rh, title)

The print() command shows you what is going into the mtext() definition (which is the Macro function for the Text Plotting icon).

This is fine for a single field, but notice that if you move between the two fields in the relative_humidity dataplot, there are two different vertical levels - but we only have one title, and it was constructed using the meta-data from the first field. We cannot tell it to "use this title for the first field, but use another title for the second field". So in this case we should use a handy feature of the Text Plotting icon which is described in the next section.

Using Meta-data references in titles

In the previous example, Macro constructed a title string and passed it to the plot() command, which used it directly. However, the plot() command can be more clever than that. For a start, it knows to translate <magics_title/> into the automatic title. It also has some other tricks.

Instead of having Macro extract the meta-data, we can pass references to the meta-data, which will be replaced with their values at plot time, per field.

Create a copy of your macro and change just one line:

Code Block
field_level      = "<grib_info key='level'/>"

Now, we are no longer extracting the level ourselves, but we are asking the plotting module to extract it at plotting time. With this change, the title will show the correct level for each field.