Versions Compared

Key

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

...

Construct a loop to go through the fields

First, create an empty list (dates = nil) - we will add each date variable to it when we loop through the fields.

We will obtain the date for each field of the original precipitation fieldset and add it to the list. We need to loop through the fields:

Code Block
languagepy
n = count(precip) # the number of fields in the fieldset

#dates extract= thenil
for datesi and= times1 fromto then originaldo
 fields and put into a list
dates = nil
for i = 1 to n do
   print print(i) # we will put proper code here later!
end for

 

The skeleton of the loop should

...

Extract the date and time from each field

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')

Print the results of these to understand the numbers that are being returned.

Convert these numbers into a date variable

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, call the variable dt and print it to check that it is a full date variable.

Add the date to the  list

If the date variable is dt, then we add it to the list like this (inside the loop)

Here are some hints to help.

A list is built up like this:

Code Block
languagepy
dates = nil
for i = .... do
    dt = .....  # construct a date/time variable
    dates = dates & [dt]
end for

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')
dates & [dt]

 

 

  • 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

Here are some hints to help.

A list is built up like this:

Code Block
languagepy
dates = nil
for i = .... do
    dt = .....  # construct a date/time variable
    dates = dates & [dt]
end for

 

 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.

...