Example R
syntax is provided for calculating daily mean radiation,
total rainfall, growing degree dates and the number of stress days
between two dates. Alternatively, a number of days before or after a
certain date may be specified.
Note that employing mapping functions to add agrometeorological
variables to large data frames can take a substantial amount of
computational time. We describe how to employ the furrr
package,
which provides a relatively simple way to apply mapping functions in
parallel, to speed up these calculations.
The growing_degree_days
function calculates the sum of degree days
for each day $i = 1 \ldots n$. The growing degree days $GDD$ summed
over $n$ days are
$$GDD = \sum_i^n (Tmax_{i} + Tmin_{i}) / 2 - T_{base}$$
during specified dates for a tibble/data frame of daily weather
data. For each day $i$, the maximum temperature is $Tmax_{i}$ and
minimum is $Tmin_{i}$. Note that the maximum temperature $Tmax$ is
capped at maxt_cap
degrees when calculating average temperature. The
defaults are $T_{base} = 5^{\circ}C$ and $Tmax$ is capped at
$Tmax_{cap} = 30^{\circ}C$. (See @mcmaster1997 or
https://farmwest.com/climate/calculator-information/gdd/ [@anonGDD])
The gdd functions in the pollen package [@pollen2019] and in agroclim [@agroclim2020] also calculate growing degree days. While these functions do not allow for a fixed number of days, and in the case of agroclim::gdd assume a more limited growing season since the function appears to be tailored to grapes, further variations on the formula above as outlined in @baskerville1969 are available.
To calculate the growing degree days at Boonah using weather data from
the boonah
object between flowering and harvest use:
## Growing Degree Days between two dates crop$flower_date[4] # flowering date for 4th field or farm in 'crop' crop$harvest_date[4] # harvest date for 4th field or farm in 'crop' growing_degree_days(boonah, startdate = crop$flower_date[4], enddate = crop$harvest_date[4]) #, monitor = TRUE)
stress_days_over
calculates the number of days when the maximum
temperature exceeded a base line stress_temp
during specified dates
for a tibble/data frame of daily weather data. The default
stress_temp
is set at $30^{\circ}C$.
To calculate the number of stress days at Boonah between flowering and harvest, use:
## Stress days between two dates stress_days_over(boonah, startdate = crop$flower_date[4], enddate = crop$harvest_date[4]) # , monitor = TRUE)
cumulative
calculates the sum total of daily values between two
dates from a tibble/data frame of daily weather data. Typically this
is used for solar radiation or rainfall.
To calculate the total rainfall at Boonah between flowering and harvest, use:
## cumulative rainfall between two dates (flowering and harvest) cumulative(boonah, var = rain, startdate = crop$flower_date[4], enddate = crop$harvest_date[4])
daily_mean
calculates the daily average of a variable between two
dates from a tibble/data frame of daily weather data. Typically this
would be for temperature, rainfall or solar radiation.
To calculate daily mean radiation in the 3 day period from day of flowering onwards (which also includes day of flowering), use:
## daily mean radiation for the three days ending on crop$flower_date[4] crop$flower_date[4] # a particular flowering date daily_mean(boonah, enddate = crop$flower_date[4], ndays = 3, monitor = TRUE)
To extract column(s) from a tibble/data frame of daily weather data
between two specified dates we use weather_extract
. Either specify
the start and end dates or specify one of these dates and also the
number of days after or before, respectively.
## Extract daily rainfall & maximum temperature data using %>% pipe operator boonah |> weather_extract(c(rain, maxt), date = date_met, startdate = ymd("2019-08-16"), enddate = ymd("2019-08-21"))
We can add agrometeorological variables to the crop
tibble using the
tidyverse
functions map_dbl
, map_dbl2
and pmap
to calculate
new columns employing the weather data from the boonah
object. Use
map_dbl
for one varying date and map_dbl2
for varying start and
end dates. For more than two varying parameters, which may be
necessary if for instance our weather object contained multiple
locations or sites, then we can use pmap
. These functions are from
the purrr
library. Alternatively, we could use functions from the
apply
family such as mapply
from the base
package.
To add growing degree days 7 days post sowing and the number of stress
days above $30^\circ C$ from flowering to harvest to the crop
tibble, then we employ the following mutate
syntax to extract the
appropriate weather data from the boonah
weather data object.
## Growing degree and stress days crop2 <- crop |> dplyr::mutate(gddays_post_sow_7d = purrr::map_dbl(sowing_date, function(x) growing_degree_days(boonah, startdate = x, ndays = 7)), stressdays_flower_harvest = purrr::map2_dbl(flower_date, harvest_date, function(x, y) stress_days_over(boonah, startdate = x, enddate = y))) print(crop2, n=5)
Similarly, to add total rainfall for the 7 days post sowing and the mean daily radiation from flowering to harvest we use:
## Totals and daily means crop3 <- crop |> dplyr::mutate(totalrain_post_sow_7d = purrr::map_dbl(sowing_date, function(x) cumulative(boonah, var = rain, startdate = x, ndays = 7)), meanrad_flower_harvest = purrr::map2_dbl(flower_date, harvest_date, function(x, y) daily_mean(boonah, var = radn, startdate = x, enddate = y))) print(crop3, n=5)
furrr
For large datasets these calculations can be time consuming. One
approach that may prove useful is to use the furrr
package which is
a bridge between purrr‘s family of mapping functions and future‘s
parallel processing capabilities. If speed is an issue, then it is
worth trying because it is simple to implement. While some tweaking
may prove useful, it seems that the defaults work pretty well (see
?future::plan
). After setting the number of workers, then simply
replace mapping functions by putting future_
at the front of the
name of the mapping function. For instance, map2_dbl
is replaced
with future_map2_dbl
. While the results are not shown here, to add
total rain and mean radiation as before, use something like:
ptm <- proc.time() # Start the clock! ## set number of 'furrr' workers library(furrr) plan(multisession, workers = 2) ## Totals and daily means crop3 <- crop |> dplyr::mutate(totalrain_post_sow_7d = future_map_dbl(sowing_date, function(x) cumulative(boonah, var = rain, startdate = x, ndays = 7)), meanrad_flower_harvest = future_map2_dbl(flower_date, harvest_date, function(x, y) daily_mean(boonah, var = radn, startdate = x, enddate = y))) print(crop3, n=5) proc.time() - ptm # Stop the clock!
For recent work, we have found that setting 4 workers was optimal but this will of course depend on your setup.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.