-
Notifications
You must be signed in to change notification settings - Fork 0
/
one_day.R
144 lines (115 loc) · 5.49 KB
/
one_day.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#' Simulates Ascochyta spore dispersal for a single day increment
#'
#' @param i_date a character string or POSIXct formatted string indicating an
#' iteration date of the model. Preferably in \acronym{ISO8601} format (YYYY-MM-DD),
#' \emph{e.g.} \dQuote{2020-04-26}.
#'
#' @param daily_vals `list` of model variables which have been calculated for days prior to the `i_date`
#' @param weather_dat `data.table` of weather observations which includes the query date `i_date`
#' @param max_gp numeric double a function of max_gp_lim x (1 - exp(-0.138629 x seeding_rate))
#' @param spore_interception_parameter parameter indicating the probability of a spore
#' landing on a susceptible growing point
#' @param spores_per_gp_per_wet_hour Number of spores produced per sporulating growing point each wet hour.
#' Also known as the 'spore_rate'. Value is dependent on the susceptibility of the host genotype.
#'
#' @return a `list` detailing daily data for the day `i_date` generated by the model including:
#' a `paddock` an 'x' 'y' data.table, iteration day (i_day), cumulative daily weather data, such as:
#' cumulative degree days (cdd), cumulative wet hours (cwh) and cumulative rainfall in mm (cr);
#' standard growing points assuming growth is not impeeded by infection (gp_standard), new growing points
#' produced in the last 24 hours (new_gp), an 'xy' data.table of only sporulating growing points (infected_gp),
#' and new_infections 'xy' data.table indicating exposed growing points in the latent period phase of
#' infection.
#'
#' @keywords internal
#' @noRd
one_day <- function(i_date,
daily_vals,
weather_dat,
gp_rr,
max_gp,
spore_interception_parameter,
spores_per_gp_per_wet_hour) {
times <- temp <- wet_hours <- rain <- new_gp <- sporulating_gp <-
cdd_at_infection <- noninfected_gp <- NULL
# expand time to be hourly
i_time <- rep(i_date, 24) + lubridate::dhours(0:23)
# subset weather data by day
weather_day <-
weather_dat[times %in% i_time, ]
# obtain summary weather for i_day
i_mean_air_temp <- mean(weather_day[, temp])
i_wet_hours <- weather_day[, wet_hours]
i_rainfall <- sum(weather_day[, rain], na.rm = TRUE)
# Start building a list of values for 'i'
# NOTE: I may add this to after `make_some_infective`
daily_vals[["cdd"]] <- daily_vals[["cdd"]] + i_mean_air_temp
daily_vals[["cwh"]] <- daily_vals[["cwh"]] + i_wet_hours
daily_vals[["cr"]] <- daily_vals[["cr"]] + i_rainfall
max_interception_probability <-
interception_probability(target_density = 5 * max(daily_vals[["paddock"]][,new_gp]),
k = spore_interception_parameter)
# need to make a copy of the data.table otherwise it will modify all data.tables
# in the following functions
daily_vals[["paddock"]] <- copy(daily_vals[["paddock"]])
if(any(is.na(daily_vals[["paddock"]][,sporulating_gp]))){
stop("NA values in daily_vals[['paddock']][,sporulating_gp] ")
}
# Spread spores and infect plants
# Update growing points for paddock coordinates
if(i_rainfall > 0){
newly_infected_dt <-
rbindlist(
lapply(
seq_len(nrow(weather_day[rain >= 0, ])),
FUN = spores_each_wet_hour,
weather_hourly = weather_day[rain >= 0, ],
paddock = daily_vals[["paddock"]],
max_interception_probability = max_interception_probability,
spore_interception_parameter = spore_interception_parameter,
spores_per_gp_per_wet_hour = spores_per_gp_per_wet_hour
)
)
newly_infected_dt[, cdd_at_infection := daily_vals[["cdd"]]]
# #aggregate infections
# newly_infected_dt <- newly_infected_dt[ , .N, by = .(x,y, cdd_at_infection)]
# setnames(
# newly_infected_list,
# old = c("x", "y", "cdd_at_infection", "N"),
# new = c("x", "y", "cdd_at_infection", "spores_per_packet")
# )
daily_vals[["newly_infected"]] <- rbind(daily_vals[["newly_infected"]],
newly_infected_dt)
}
# exposed gps which have undergone latent period are moved to sporulating gps
daily_vals <- make_some_infective(daily_vals = daily_vals,
latent_period = 150)
# update infected coordinates
daily_vals[["infected_coords"]] <- daily_vals[["paddock"]][sporulating_gp > 0, c("x","y")]
# Grow Plants
# this code represents mathematica function `growth`; `updateRefUninfectiveGrowingPoints`
# `updateGrowingPointsAllInfectiveElements`
# Update Growing points for non-infected coords for time i
daily_vals[["new_gp"]] <-
calc_new_gp(current_growing_points = daily_vals[["gp_standard"]],
gp_rr = gp_rr,
max_gp = max_gp,
mean_air_temp = i_mean_air_temp)
# this might be quicker if there was no fifelse statement
daily_vals[["paddock"]][, new_gp := fcase(
noninfected_gp == 0, 0,
noninfected_gp == daily_vals[["gp_standard"]], daily_vals[["new_gp"]],
noninfected_gp < daily_vals[["gp_standard"]], calc_new_gp(
current_growing_points = noninfected_gp,
gp_rr = gp_rr,
max_gp = max_gp,
mean_air_temp = i_mean_air_temp
)
)]
daily_vals[["gp_standard"]] <-
daily_vals[["gp_standard"]] + daily_vals[["new_gp"]]
daily_vals[["paddock"]][, noninfected_gp := noninfected_gp + new_gp]
# This line is here due to https://github.com/Rdatatable/data.table/issues/869
#daily_vals[["paddock"]]
daily_vals
return(daily_vals)
}