...
As an exercise to put all of this together, we will write a new macro to compute the precipitation rate in mm per hour at a particular location for each time stepperiod.We'll do it step by step.
...
This is what we already did earlier, so it's done! Just make a copy the code to your new earlier macro and change , compute_precip, and call it precip_rate. Change the result variable name to precip_diff
to make it more generic. Remove the return
line, as we want to use this fieldset, not return it.
Construct a loop to go through the fields
FirstNow, create an empty list (dates = nil
) - we . 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 :(we should already have n
defined as the number of fields from the previous exercise):
Code Block | ||
---|---|---|
| ||
Code Block | ||
| ||
n = count(precip) # the number of fields in the fieldset
dates = nil
for i = 1 to n do
print(i) # we will put proper code here later!
end for |
...
Print the results of these to understand the numbers that are being returned.
Convert these numbers into a date variable
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. |
Convert these numbers into a date variable
You will need to divide the time variable (t
) to 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
). A time of 12:00 is returned as 1200, so we need to divide by 100 to get it into hours. The hours()
function then converts it into a fraction of a day. The result can be converted with the date()
function into a proper date, ; call the resulting variable dt
and print it to check that it is a full date variable.
Add the date to the list
Code Block | ||
---|---|---|
| ||
dt = date(d) + hour(t/100) |
Add the date to the list
If the date variable is dt
If the date variable is dt
, then we add it to the list like this (inside the loop):
Code Block | ||
---|---|---|
| ||
dates = dates & [dt] |
...
Compute the differences between consecutive dates
Very similar to
...
computing the precipitation data earlier (ok, we know it's 3 hours, but in theory it could be anything)
...
:
Code Block | ||
---|---|---|
| ||
date_diffs = dates[2, n] - dates[1, n-1] |
Now you have a list of time differences in days. You can multiply by 24 to get them in hours.
Here are some hints to help.
A list is built up like this:
Code Block | ||
---|---|---|
| ||
datesdate_diffs_in_hours = nil for i = .... do dt = ..... # construct a date/time variable dates = dates & [dt] end for |
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. |
date_diffs*24 |
Extract the point value for each field in precip_diff
Use the nearest_gridpoint()
function on the precip_diff
fieldset. It returns a list of values, one for each field. Choose a location with some high precipitationNow 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 result is a list of values, a value for each field. You can directly multiply a list variable by a number to obtain a new list where each element has been multiplied - do this to scale from metres to mm.
Compute precipitation rate in mm per hour
The final calculation requires converting the data values into mm per hour - divide this the list of precipitation values by the list of time differences, which should be are 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).
Print the result - it will be a list of numbers, one for each time period.
...
In Missing Values and Masks, we will see how we could do this sort of thing directly with the GRIB fields.