Versions Compared

Key

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

...

  • compute the 'period' precipitation from precip.grib - this is what we already did earlier, so it's done! Just copy the code to your new macro and change the result variable name to precip_diff
  • loop through the fields in the original fieldset and for each:
    • get the date and time of the forecast step (see the hint later)
    • combine these into a Metview date variable
    • add it to a list (which was initialised to nil before the loop)
  • use syntax similar to the line of code used to compute the 'period' precipitation to find the differences between the times of adjacent fields (ok, we know it's 3 hours, but in theory it could be anything)
  • extract the point value for each field in precip_diff (use the nearest_gridpoint() function). Choose a location with some high precipitation
  • scale up from metres (as the data are stored) to mm by multiplying by 1000
  • convert this into a rate, mm per hour, using the time differences computed earlier

...

You can get the date and time of a field as numbers like this, inside the loop, where i is the field index:

Code Block
languagepy
d = grib_get_long(precip_diff[i], 'validityDate')
t = grib_get_long(precip_diff[i], 'validityTime')

then combine those numbers into proper date variables. Print the results of these function calls to check exactly what they are returning before you try to convert them into date variables. You will need to divide the time variable (t) to convert it into a fraction of a day before adding it to the date variable (d). The result can be converted with the date() function into a proper date.

Info
The grib_get() functions are general-purpose functions to get pieces of meta-data from a GRIB field, specified by keys such as 'validityDate'. The Grib Examiner can help you find the available keys.

Now that you have a list of proper date variables, you can find the difference between consecutive dates using a single line of code which subtracts one set of list elements from another (very similar to computing the 'period precipitation' we did earlier). Now you have a list of time differences in days. You can multiply by 24 to get them in hours.

The nearest_gridpoint() function is now used to get the precipitation values. It can be called in a number of ways, but we will use it like this:

...

The final calculation requires converting the data values into mm per hour - divide this list of precipitation values by the list of time differences, which should be in hours (e.g. if the time difference between two steps is 7 hours, then the rate of precip per hour is the mean precip value divided by 7).

...