85
|
1 #!/usr/bin/env Rscript
|
|
2
|
|
3 suppressPackageStartupMessages(library("optparse"))
|
|
4
|
|
5 option_list <- list(
|
102
|
6 make_option(c("-a", "--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"),
|
|
7 make_option(c("-b", "--adult_accumulation"), action="store", dest="adult_accumulation", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"),
|
|
8 make_option(c("-c", "--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"),
|
85
|
9 make_option(c("-e", "--location"), action="store", dest="location", help="Selected location"),
|
|
10 make_option(c("-f", "--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
|
|
11 make_option(c("-i", "--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
|
102
|
12 make_option(c("-j", "--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"),
|
|
13 make_option(c("-k", "--old_nymph_accumulation"), action="store", dest="old_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"),
|
85
|
14 make_option(c("-n", "--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"),
|
|
15 make_option(c("-o", "--output"), action="store", dest="output", help="Output dataset"),
|
|
16 make_option(c("-p", "--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
|
|
17 make_option(c("-q", "--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
|
|
18 make_option(c("-s", "--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
|
90
|
19 make_option(c("-t", "--std_error_plot"), action="store", dest="std_error_plot", help="Plot Standard error"),
|
85
|
20 make_option(c("-v", "--input"), action="store", dest="input", help="Temperature data for selected location"),
|
102
|
21 make_option(c("-y", "--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)"),
|
87
|
22 make_option(c("-x", "--insect"), action="store", dest="insect", help="Insect name")
|
85
|
23 )
|
|
24
|
|
25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
|
|
26 args <- parse_args(parser, positional_arguments=TRUE)
|
|
27 opt <- args$options
|
|
28
|
|
29 add_daylight_length = function(temperature_data_frame, num_columns, num_rows) {
|
|
30 # Return a vector of daylight length (photoperido profile) for
|
|
31 # the number of days specified in the input temperature data
|
|
32 # (from Forsythe 1995).
|
|
33 p = 0.8333
|
|
34 latitude <- temperature_data_frame$LATITUDE[1]
|
|
35 daylight_length_vector <- NULL
|
|
36 for (i in 1:num_rows) {
|
|
37 # Get the day of the year from the current row
|
|
38 # of the temperature data for computation.
|
|
39 doy <- temperature_data_frame$DOY[i]
|
|
40 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186)))
|
|
41 phi <- asin(0.39795 * cos(theta))
|
|
42 # Compute the length of daylight for the day of the year.
|
102
|
43 darkness_length <- 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)))
|
|
44 daylight_length_vector[i] <- 24 - darkness_length
|
85
|
45 }
|
|
46 # Append daylight_length_vector as a new column to temperature_data_frame.
|
|
47 temperature_data_frame[, num_columns+1] <- daylight_length_vector
|
|
48 return(temperature_data_frame)
|
|
49 }
|
|
50
|
102
|
51 dev.egg = function(temperature) {
|
|
52 dev.rate = -0.9843 * temperature + 33.438
|
|
53 return(dev.rate)
|
|
54 }
|
|
55
|
|
56 dev.emerg = function(temperature) {
|
|
57 emerg.rate <- -0.5332 * temperature + 24.147
|
|
58 return(emerg.rate)
|
|
59 }
|
|
60
|
|
61 dev.old = function(temperature) {
|
|
62 n34 <- -0.6119 * temperature + 17.602
|
|
63 n45 <- -0.4408 * temperature + 19.036
|
|
64 dev.rate = mean(n34 + n45)
|
|
65 return(dev.rate)
|
|
66 }
|
|
67
|
|
68 dev.young = function(temperature) {
|
|
69 n12 <- -0.3728 * temperature + 14.68
|
|
70 n23 <- -0.6119 * temperature + 25.249
|
|
71 dev.rate = mean(n12 + n23)
|
|
72 return(dev.rate)
|
|
73 }
|
|
74
|
85
|
75 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) {
|
|
76 # Base development threshold for Brown Marmolated Stink Bug
|
|
77 # insect phenology model.
|
|
78 threshold <- 14.17
|
|
79 # Minimum temperature for current row.
|
102
|
80 curr_min_temp <- temperature_data_frame$TMIN[row]
|
85
|
81 # Maximum temperature for current row.
|
102
|
82 curr_max_temp <- temperature_data_frame$TMAX[row]
|
85
|
83 # Mean temperature for current row.
|
102
|
84 curr_mean_temp <- 0.5 * (curr_min_temp + curr_max_temp)
|
85
|
85 # Initialize degree day accumulation
|
103
|
86 averages <- 0
|
102
|
87 if (curr_max_temp < threshold) {
|
103
|
88 averages <- 0
|
85
|
89 }
|
|
90 else {
|
|
91 # Initialize hourly temperature.
|
|
92 T <- NULL
|
|
93 # Initialize degree hour vector.
|
|
94 dh <- NULL
|
|
95 # Daylight length for current row.
|
|
96 y <- temperature_data_frame$DAYLEN[row]
|
|
97 # Darkness length.
|
|
98 z <- 24 - y
|
|
99 # Lag coefficient.
|
|
100 a <- 1.86
|
|
101 # Darkness coefficient.
|
|
102 b <- 2.20
|
|
103 # Sunrise time.
|
|
104 risetime <- 12 - y / 2
|
|
105 # Sunset time.
|
|
106 settime <- 12 + y / 2
|
102
|
107 ts <- (curr_max_temp - curr_min_temp) * sin(pi * (settime - 5) / (y + 2 * a)) + curr_min_temp
|
85
|
108 for (i in 1:24) {
|
|
109 if (i > risetime && i < settime) {
|
|
110 # Number of hours after Tmin until sunset.
|
|
111 m <- i - 5
|
102
|
112 T[i] = (curr_max_temp - curr_min_temp) * sin(pi * m / (y + 2 * a)) + curr_min_temp
|
85
|
113 if (T[i] < 8.4) {
|
|
114 dh[i] <- 0
|
|
115 }
|
|
116 else {
|
|
117 dh[i] <- T[i] - 8.4
|
|
118 }
|
|
119 }
|
96
|
120 else if (i > settime) {
|
85
|
121 n <- i - settime
|
102
|
122 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z)
|
85
|
123 if (T[i] < 8.4) {
|
|
124 dh[i] <- 0
|
|
125 }
|
|
126 else {
|
|
127 dh[i] <- T[i] - 8.4
|
|
128 }
|
|
129 }
|
|
130 else {
|
|
131 n <- i + 24 - settime
|
102
|
132 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z)
|
85
|
133 if (T[i] < 8.4) {
|
|
134 dh[i] <- 0
|
|
135 }
|
|
136 else {
|
|
137 dh[i] <- T[i] - 8.4
|
|
138 }
|
|
139 }
|
|
140 }
|
103
|
141 averages <- sum(dh) / 24
|
85
|
142 }
|
103
|
143 return(c(curr_mean_temp, averages))
|
85
|
144 }
|
|
145
|
102
|
146 mortality.adult = function(temperature) {
|
|
147 if (temperature < 12.7) {
|
|
148 mortality.probability = 0.002
|
|
149 }
|
|
150 else {
|
|
151 mortality.probability = temperature * 0.0005 + 0.02
|
|
152 }
|
|
153 return(mortality.probability)
|
85
|
154 }
|
|
155
|
|
156 mortality.egg = function(temperature) {
|
|
157 if (temperature < 12.7) {
|
102
|
158 mortality.probability = 0.8
|
85
|
159 }
|
|
160 else {
|
102
|
161 mortality.probability = 0.8 - temperature / 40.0
|
|
162 if (mortality.probability < 0) {
|
|
163 mortality.probability = 0.01
|
85
|
164 }
|
|
165 }
|
102
|
166 return(mortality.probability)
|
85
|
167 }
|
|
168
|
|
169 mortality.nymph = function(temperature) {
|
|
170 if (temperature < 12.7) {
|
102
|
171 mortality.probability = 0.03
|
85
|
172 }
|
|
173 else {
|
102
|
174 mortality.probability = temperature * 0.0008 + 0.03
|
85
|
175 }
|
102
|
176 return(mortality.probability)
|
|
177 }
|
|
178
|
|
179 parse_input_data = function(input_file, num_rows) {
|
|
180 # Read in the input temperature datafile into a data frame.
|
|
181 temperature_data_frame <- read.csv(file=input_file, header=T, strip.white=TRUE, sep=",")
|
|
182 num_columns <- dim(temperature_data_frame)[2]
|
|
183 if (num_columns == 6) {
|
|
184 # The input data has the following 6 columns:
|
|
185 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
|
|
186 # Set the column names for access when adding daylight length..
|
|
187 colnames(temperature_data_frame) <- c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX")
|
|
188 # Add a column containing the daylight length for each day.
|
|
189 temperature_data_frame <- add_daylight_length(temperature_data_frame, num_columns, num_rows)
|
|
190 # Reset the column names with the additional column for later access.
|
|
191 colnames(temperature_data_frame) <- c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX", "DAYLEN")
|
|
192 }
|
|
193 return(temperature_data_frame)
|
85
|
194 }
|
|
195
|
102
|
196 render_chart = function(chart_type, insect, location, latitude, start_date, end_date, days, maxval, plot_std_error,
|
|
197 group1, group2, group3, group1_std_error, group2_std_error, group3_std_error) {
|
|
198 if (chart_type == "pop_size_by_life_stage") {
|
|
199 title <- paste(insect, ": Total pop. by life stage :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ")
|
103
|
200 legend_text <- c("Egg", "Nymph", "Adult")
|
|
201 columns <- c(4, 2, 1)
|
102
|
202 } else if (chart_type == "pop_size_by_generation") {
|
|
203 title <- paste(insect, ": Total pop. by generation :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ")
|
103
|
204 legend_text <- c("P", "F1", "F2")
|
|
205 columns <- col=c(1, 2, 4)
|
102
|
206 } else if (chart_type == "adult_pop_size_by_generation") {
|
|
207 title <- paste(insect, ": Adult pop. by generation :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ")
|
103
|
208 legend_text <- c("P", "F1", "F2")
|
|
209 columns <- col=c(1, 2, 4)
|
85
|
210 }
|
102
|
211 plot(days, group1, main=title, type="l", ylim=c(0, maxval), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3)
|
103
|
212 legend("topleft", legend_text, lty=c(1, 1, 1), columns, cex=3)
|
102
|
213 lines(days, group2, lwd=2, lty=1, col=2)
|
|
214 lines(days, group3, lwd=2, lty=1, col=4)
|
|
215 axis(1, at=c(1:12) * 30 - 15, cex.axis=3, labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
|
|
216 axis(2, cex.axis=3)
|
|
217 if (plot_std_error==1) {
|
|
218 # Standard error for group1.
|
|
219 lines(days, group1+group1_std_error, lty=2)
|
|
220 lines (days, group1-group1_std_error, lty=2)
|
|
221 # Standard error for group2.
|
|
222 lines(days, group2+group2_std_error, col=2, lty=2)
|
|
223 lines(days, group2-group2_std_error, col=2, lty=2)
|
|
224 # Standard error for group3.
|
|
225 lines(days, group3+group3_std_error, col=4, lty=2)
|
|
226 lines(days, group3-group3_std_error, col=4, lty=2)
|
85
|
227 }
|
|
228 }
|
|
229
|
|
230 temperature_data_frame <- parse_input_data(opt$input, opt$num_days)
|
|
231 # All latitude values are the same,
|
|
232 # so get the value from the first row.
|
|
233 latitude <- temperature_data_frame$LATITUDE[1]
|
|
234
|
|
235 cat("Number of days: ", opt$num_days, "\n")
|
|
236
|
97
|
237 # Initialize matrices.
|
102
|
238 Eggs.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
|
239 YoungNymphs.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
|
240 OldNymphs.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
|
241 Previtellogenic.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
|
242 Vitellogenic.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
|
243 Diapausing.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
103
|
244
|
97
|
245 newborn.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
103
|
246 adult.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
97
|
247 death.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
103
|
248
|
100
|
249 P.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
103
|
250 P_adults.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
100
|
251 F1.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
103
|
252 F1_adults.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
100
|
253 F2.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
99
|
254 F2_adults.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
85
|
255
|
103
|
256 population.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
|
|
257
|
102
|
258 # Process replications.
|
97
|
259 for (N.replications in 1:opt$replications) {
|
85
|
260 # During each replication start with 1000 individuals.
|
|
261 # TODO: user definable as well?
|
90
|
262 num_insects <- 1000
|
|
263 # Generation, Stage, degree-days, T, Diapause.
|
97
|
264 vector.ini <- c(0, 3, 0, 0, 0)
|
90
|
265 # Overwintering, previttelogenic, degree-days=0, T=0, no-diapause.
|
97
|
266 vector.matrix <- rep(vector.ini, num_insects)
|
85
|
267 # Complete matrix for the population.
|
97
|
268 vector.matrix <- base::t(matrix(vector.matrix, nrow=5))
|
85
|
269 # Time series of population size.
|
102
|
270 Eggs <- rep(0, opt$num_days)
|
|
271 YoungNymphs <- rep(0, opt$num_days)
|
|
272 OldNymphs <- rep(0, opt$num_days)
|
|
273 Previtellogenic <- rep(0, opt$num_days)
|
|
274 Vitellogenic <- rep(0, opt$num_days)
|
|
275 Diapausing <- rep(0, opt$num_days)
|
103
|
276
|
|
277 N.newborn <- rep(0, opt$num_days)
|
|
278 N.adult <- rep(0, opt$num_days)
|
|
279 N.death <- rep(0, opt$num_days)
|
|
280
|
|
281 overwintering_adult.population <- rep(0, opt$num_days)
|
|
282 first_generation.population <- rep(0, opt$num_days)
|
|
283 second_generation.population <- rep(0, opt$num_days)
|
|
284
|
100
|
285 P.adult <- rep(0, opt$num_days)
|
|
286 F1.adult <- rep(0, opt$num_days)
|
|
287 F2.adult <- rep(0, opt$num_days)
|
103
|
288
|
|
289 total.population <- NULL
|
|
290
|
|
291 averages.day <- rep(0, opt$num_days)
|
85
|
292 # All the days included in the input temperature dataset.
|
|
293 for (row in 1:opt$num_days) {
|
|
294 # Get the integer day of the year for the current row.
|
|
295 doy <- temperature_data_frame$DOY[row]
|
|
296 # Photoperiod in the day.
|
|
297 photoperiod <- temperature_data_frame$DAYLEN[row]
|
|
298 temp.profile <- get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days)
|
|
299 mean.temp <- temp.profile[1]
|
103
|
300 averages.temp <- temp.profile[2]
|
|
301 averages.day[row] <- averages.temp
|
85
|
302 # Trash bin for death.
|
97
|
303 death.vector <- NULL
|
85
|
304 # Newborn.
|
97
|
305 birth.vector <- NULL
|
85
|
306 # All individuals.
|
92
|
307 for (i in 1:num_insects) {
|
103
|
308 # Individual record.
|
102
|
309 vector.individual <- vector.matrix[i,]
|
103
|
310 # Adjustment for late season mortality rate (still alive?).
|
85
|
311 if (latitude < 40.0) {
|
102
|
312 post.mortality <- 1
|
85
|
313 day.kill <- 300
|
|
314 }
|
|
315 else {
|
102
|
316 post.mortality <- 2
|
85
|
317 day.kill <- 250
|
|
318 }
|
102
|
319 if (vector.individual[2] == 0) {
|
85
|
320 # Egg.
|
102
|
321 death.probability = opt$egg_mortality * mortality.egg(mean.temp)
|
85
|
322 }
|
102
|
323 else if (vector.individual[2] == 1 | vector.individual[2] == 2) {
|
|
324 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp)
|
85
|
325 }
|
102
|
326 else if (vector.individual[2] == 3 | vector.individual[2] == 4 | vector.individual[2] == 5) {
|
103
|
327 # Adult.
|
85
|
328 if (doy < day.kill) {
|
102
|
329 death.probability = opt$adult_mortality * mortality.adult(mean.temp)
|
85
|
330 }
|
|
331 else {
|
|
332 # Increase adult mortality after fall equinox.
|
102
|
333 death.probability = opt$adult_mortality * post.mortality * mortality.adult(mean.temp)
|
85
|
334 }
|
|
335 }
|
103
|
336 # Dependent on temperature and life stage?
|
85
|
337 u.d <- runif(1)
|
102
|
338 if (u.d < death.probability) {
|
97
|
339 death.vector <- c(death.vector, i)
|
96
|
340 }
|
85
|
341 else {
|
|
342 # Aggregrate index of dead bug.
|
103
|
343 # End of diapause.
|
102
|
344 if (vector.individual[1] == 0 && vector.individual[2] == 3) {
|
85
|
345 # Overwintering adult (previttelogenic).
|
102
|
346 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) {
|
85
|
347 # Add 68C to become fully reproductively matured.
|
|
348 # Transfer to vittelogenic.
|
102
|
349 vector.individual <- c(0, 4, 0, 0, 0)
|
|
350 vector.matrix[i,] <- vector.individual
|
85
|
351 }
|
|
352 else {
|
103
|
353 # Add to # Add average temperature for current day..
|
|
354 vector.individual[3] <- vector.individual[3] + averages.temp
|
85
|
355 # Add 1 day in current stage.
|
102
|
356 vector.individual[4] <- vector.individual[4] + 1
|
|
357 vector.matrix[i,] <- vector.individual
|
85
|
358 }
|
|
359 }
|
102
|
360 if (vector.individual[1] != 0 && vector.individual[2] == 3) {
|
85
|
361 # Not overwintering adult (previttelogenic).
|
102
|
362 current.gen <- vector.individual[1]
|
|
363 if (vector.individual[3] > 68) {
|
85
|
364 # Add 68C to become fully reproductively matured.
|
|
365 # Transfer to vittelogenic.
|
102
|
366 vector.individual <- c(current.gen, 4, 0, 0, 0)
|
|
367 vector.matrix[i,] <- vector.individual
|
85
|
368 }
|
|
369 else {
|
103
|
370 # Add average temperature for current day.
|
|
371 vector.individual[3] <- vector.individual[3] + averages.temp
|
85
|
372 # Add 1 day in current stage.
|
102
|
373 vector.individual[4] <- vector.individual[4] + 1
|
|
374 vector.matrix[i,] <- vector.individual
|
85
|
375 }
|
|
376 }
|
|
377 # Event 2 oviposition -- where population dynamics comes from.
|
102
|
378 if (vector.individual[2] == 4 && vector.individual[1] == 0 && mean.temp > 10) {
|
85
|
379 # Vittelogenic stage, overwintering generation.
|
102
|
380 if (vector.individual[4] == 0) {
|
85
|
381 # Just turned in vittelogenic stage.
|
90
|
382 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
|
85
|
383 }
|
|
384 else {
|
|
385 # Daily probability of birth.
|
|
386 p.birth = opt$oviposition * 0.01
|
|
387 u1 <- runif(1)
|
|
388 if (u1 < p.birth) {
|
90
|
389 num_insects.birth = round(runif(1, 2, 8))
|
85
|
390 }
|
|
391 }
|
103
|
392 # Add average temperature for current day.
|
|
393 vector.individual[3] <- vector.individual[3] + averages.temp
|
85
|
394 # Add 1 day in current stage.
|
102
|
395 vector.individual[4] <- vector.individual[4] + 1
|
|
396 vector.matrix[i,] <- vector.individual
|
90
|
397 if (num_insects.birth > 0) {
|
85
|
398 # Add new birth -- might be in different generations.
|
102
|
399 new.gen <- vector.individual[1] + 1
|
85
|
400 # Egg profile.
|
102
|
401 new.individual <- c(new.gen, 0, 0, 0, 0)
|
|
402 new.vector <- rep(new.individual, num_insects.birth)
|
85
|
403 # Update batch of egg profile.
|
97
|
404 new.vector <- t(matrix(new.vector, nrow=5))
|
85
|
405 # Group with total eggs laid in that day.
|
97
|
406 birth.vector <- rbind(birth.vector, new.vector)
|
85
|
407 }
|
|
408 }
|
|
409 # Event 2 oviposition -- for generation 1.
|
102
|
410 if (vector.individual[2] == 4 && vector.individual[1] == 1 && mean.temp > 12.5 && doy < 222) {
|
85
|
411 # Vittelogenic stage, 1st generation
|
102
|
412 if (vector.individual[4] == 0) {
|
85
|
413 # Just turned in vittelogenic stage.
|
102
|
414 num_insects.birth = round(runif(1, 2+opt$min_clutch_size, 8+opt$max_clutch_size))
|
85
|
415 }
|
|
416 else {
|
|
417 # Daily probability of birth.
|
|
418 p.birth = opt$oviposition * 0.01
|
|
419 u1 <- runif(1)
|
|
420 if (u1 < p.birth) {
|
90
|
421 num_insects.birth = round(runif(1, 2, 8))
|
85
|
422 }
|
|
423 }
|
103
|
424 # Add average temperature for current day.
|
|
425 vector.individual[3] <- vector.individual[3] + averages.temp
|
85
|
426 # Add 1 day in current stage.
|
102
|
427 vector.individual[4] <- vector.individual[4] + 1
|
|
428 vector.matrix[i,] <- vector.individual
|
90
|
429 if (num_insects.birth > 0) {
|
85
|
430 # Add new birth -- might be in different generations.
|
102
|
431 new.gen <- vector.individual[1] + 1
|
85
|
432 # Egg profile.
|
102
|
433 new.individual <- c(new.gen, 0, 0, 0, 0)
|
|
434 new.vector <- rep(new.individual, num_insects.birth)
|
85
|
435 # Update batch of egg profile.
|
97
|
436 new.vector <- t(matrix(new.vector, nrow=5))
|
85
|
437 # Group with total eggs laid in that day.
|
97
|
438 birth.vector <- rbind(birth.vector, new.vector)
|
85
|
439 }
|
|
440 }
|
|
441 # Event 3 development (with diapause determination).
|
102
|
442 # Event 3.1 egg development to young nymph (vector.individual[2]=0 -> egg).
|
|
443 if (vector.individual[2] == 0) {
|
85
|
444 # Egg stage.
|
103
|
445 # Add average temperature for current day.
|
|
446 vector.individual[3] <- vector.individual[3] + averages.temp
|
102
|
447 if (vector.individual[3] >= (68+opt$young_nymph_accumulation)) {
|
90
|
448 # From egg to young nymph, degree-days requirement met.
|
102
|
449 current.gen <- vector.individual[1]
|
85
|
450 # Transfer to young nymph stage.
|
102
|
451 vector.individual <- c(current.gen, 1, 0, 0, 0)
|
85
|
452 }
|
|
453 else {
|
|
454 # Add 1 day in current stage.
|
102
|
455 vector.individual[4] <- vector.individual[4] + 1
|
85
|
456 }
|
102
|
457 vector.matrix[i,] <- vector.individual
|
85
|
458 }
|
102
|
459 # Event 3.2 young nymph to old nymph (vector.individual[2]=1 -> young nymph: determines diapause).
|
|
460 if (vector.individual[2] == 1) {
|
90
|
461 # Young nymph stage.
|
103
|
462 # Add average temperature for current day.
|
|
463 vector.individual[3] <- vector.individual[3] + averages.temp
|
102
|
464 if (vector.individual[3] >= (250+opt$old_nymph_accumulation)) {
|
90
|
465 # From young to old nymph, degree_days requirement met.
|
102
|
466 current.gen <- vector.individual[1]
|
85
|
467 # Transfer to old nym stage.
|
102
|
468 vector.individual <- c(current.gen, 2, 0, 0, 0)
|
85
|
469 if (photoperiod < opt$photoperiod && doy > 180) {
|
102
|
470 vector.individual[5] <- 1
|
85
|
471 } # Prepare for diapausing.
|
|
472 }
|
|
473 else {
|
|
474 # Add 1 day in current stage.
|
102
|
475 vector.individual[4] <- vector.individual[4] + 1
|
85
|
476 }
|
102
|
477 vector.matrix[i,] <- vector.individual
|
96
|
478 }
|
85
|
479 # Event 3.3 old nymph to adult: previttelogenic or diapausing?
|
102
|
480 if (vector.individual[2] == 2) {
|
85
|
481 # Old nymph stage.
|
103
|
482 # Add average temperature for current day.
|
|
483 vector.individual[3] <- vector.individual[3] + averages.temp
|
102
|
484 if (vector.individual[3] >= (200+opt$adult_accumulation)) {
|
90
|
485 # From old to adult, degree_days requirement met.
|
102
|
486 current.gen <- vector.individual[1]
|
|
487 if (vector.individual[5] == 0) {
|
85
|
488 # Non-diapausing adult -- previttelogenic.
|
102
|
489 vector.individual <- c(current.gen, 3, 0, 0, 0)
|
85
|
490 }
|
|
491 else {
|
|
492 # Diapausing.
|
102
|
493 vector.individual <- c(current.gen, 5, 0, 0, 1)
|
85
|
494 }
|
|
495 }
|
|
496 else {
|
|
497 # Add 1 day in current stage.
|
102
|
498 vector.individual[4] <- vector.individual[4] + 1
|
85
|
499 }
|
102
|
500 vector.matrix[i,] <- vector.individual
|
85
|
501 }
|
|
502 # Event 4 growing of diapausing adult (unimportant, but still necessary).
|
102
|
503 if (vector.individual[2] == 5) {
|
103
|
504 vector.individual[3] <- vector.individual[3] + averages.temp
|
102
|
505 vector.individual[4] <- vector.individual[4] + 1
|
|
506 vector.matrix[i,] <- vector.individual
|
85
|
507 }
|
|
508 } # Else if it is still alive.
|
|
509 } # End of the individual bug loop.
|
99
|
510 # Find the number of deaths.
|
97
|
511 num_insects.death <- length(death.vector)
|
90
|
512 if (num_insects.death > 0) {
|
102
|
513 # Remove record of dead.
|
97
|
514 vector.matrix <- vector.matrix[-death.vector, ]
|
85
|
515 }
|
99
|
516 # Find the number of births.
|
97
|
517 num_insects.newborn <- length(birth.vector[,1])
|
|
518 vector.matrix <- rbind(vector.matrix, birth.vector)
|
85
|
519 # Update population size for the next day.
|
96
|
520 num_insects <- num_insects - num_insects.death + num_insects.newborn
|
85
|
521
|
|
522 # Aggregate results by day.
|
101
|
523 total.population <- c(total.population, num_insects)
|
99
|
524 # All adults population size.
|
102
|
525 num_insects.adult <- sum(vector.matrix[,2]==3)+sum(vector.matrix[,2]==4)+sum(vector.matrix[,2]==5)
|
99
|
526 # Overwintering adult population size.
|
102
|
527 overwintering_adult.population[row] <- sum(vector.matrix[,1]==0)
|
99
|
528 # First generation population size.
|
102
|
529 first_generation.population[row] <- sum(vector.matrix[,1]==1)
|
99
|
530 # Second generation population size.
|
102
|
531 second_generation.population[row] <- sum(vector.matrix[,1]==2)
|
99
|
532 # Egg population size.
|
102
|
533 Eggs[row] <- sum(vector.matrix[,2]==0)
|
99
|
534 # Young nymph population size.
|
102
|
535 YoungNymphs[row] <- sum(vector.matrix[,2]==1)
|
99
|
536 # Old nymph population size.
|
102
|
537 OldNymphs[row] <- sum(vector.matrix[,2]==2)
|
99
|
538 # Previtellogenic population size.
|
102
|
539 Previtellogenic[row] <- sum(vector.matrix[,2]==3)
|
99
|
540 # Vitellogenic population size.
|
102
|
541 Vitellogenic[row] <- sum(vector.matrix[,2]==4)
|
99
|
542 # Diapausing population size.
|
102
|
543 Diapausing[row] <- sum(vector.matrix[,2]==5)
|
|
544 # P adult population size.
|
101
|
545 P.adult[row] <- sum(vector.matrix[,1]==0)
|
102
|
546 # F1 adult population size.
|
101
|
547 F1.adult[row] <- sum((vector.matrix[,1]==1 & vector.matrix[,2]==3) | (vector.matrix[,1]==1 & vector.matrix[,2]==4) | (vector.matrix[,1]==1 & vector.matrix[,2]==5))
|
102
|
548 # F2 adult population size
|
101
|
549 F2.adult[row] <- sum((vector.matrix[,1]==2 & vector.matrix[,2]==3) | (vector.matrix[,1]==2 & vector.matrix[,2]==4) | (vector.matrix[,1]==2 & vector.matrix[,2]==5))
|
102
|
550 # Newborn population size.
|
90
|
551 N.newborn[row] <- num_insects.newborn
|
102
|
552 # Dead population size.
|
90
|
553 N.death[row] <- num_insects.death
|
102
|
554 # Adult population size.
|
90
|
555 N.adult[row] <- num_insects.adult
|
102
|
556 } # End of days specified in the input temperature data.
|
85
|
557
|
103
|
558 averages.cum <- cumsum(averages.day)
|
85
|
559
|
102
|
560 # Define the output values.
|
|
561 Eggs.replications[,N.replications] <- Eggs
|
|
562 YoungNymphs.replications[,N.replications] <- YoungNymphs
|
|
563 OldNymphs.replications[,N.replications] <- OldNymphs
|
|
564 Previtellogenic.replications[,N.replications] <- Previtellogenic
|
|
565 Vitellogenic.replications[,N.replications] <- Vitellogenic
|
|
566 Diapausing.replications[,N.replications] <- Diapausing
|
97
|
567 newborn.replications[,N.replications] <- N.newborn
|
|
568 death.replications[,N.replications] <- N.death
|
|
569 adult.replications[,N.replications] <- N.adult
|
102
|
570 population.replications[,N.replications] <- total.population
|
101
|
571 P.replications[,N.replications] <- overwintering_adult.population
|
|
572 F1.replications[,N.replications] <- first_generation.population
|
|
573 F2.replications[,N.replications] <- second_generation.population
|
100
|
574 P_adults.replications[,N.replications] <- P.adult
|
|
575 F1_adults.replications[,N.replications] <- F1.adult
|
|
576 F2_adults.replications[,N.replications] <- F2.adult
|
85
|
577 }
|
|
578
|
96
|
579 # Mean value for eggs.
|
102
|
580 eggs <- apply(Eggs.replications, 1, mean)
|
103
|
581 # Standard error for eggs.
|
|
582 eggs.std_error <- apply(Eggs.replications, 1, sd) / sqrt(opt$replications)
|
|
583
|
|
584 # Mean value for nymphs.
|
|
585 nymphs <- apply((YoungNymphs.replications+OldNymphs.replications), 1, mean)
|
|
586 # Standard error for nymphs.
|
|
587 nymphs.std_error <- apply((YoungNymphs.replications+OldNymphs.replications) / sqrt(opt$replications), 1, sd)
|
|
588
|
|
589 # Mean value for adults.
|
|
590 adults <- apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, mean)
|
|
591 # Standard error for adults.
|
|
592 adults.std_error <- apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, sd) / sqrt(opt$replications)
|
|
593
|
96
|
594 # Mean value for P.
|
100
|
595 P <- apply(P.replications, 1, mean)
|
103
|
596 # Standard error for P.
|
|
597 P.std_error <- apply(P.replications, 1, sd) / sqrt(opt$replications)
|
|
598
|
|
599 # Mean value for P adults.
|
|
600 P_adults <- apply(P_adults.replications, 1, mean)
|
|
601 # Standard error for P_adult.
|
|
602 P_adults.std_error <- apply(P_adults.replications, 1, sd) / sqrt(opt$replications)
|
|
603
|
96
|
604 # Mean value for F1.
|
100
|
605 F1 <- apply(F1.replications, 1, mean)
|
103
|
606 # Standard error for F1.
|
|
607 F1.std_error <- apply(F1.replications, 1, sd) / sqrt(opt$replications)
|
|
608
|
99
|
609 # Mean value for F1 adults.
|
100
|
610 F1_adults <- apply(F1_adults.replications, 1, mean)
|
103
|
611 # Standard error for F1 adult.
|
|
612 F1_adults.std_error <- apply(F1_adults.replications, 1, sd) / sqrt(opt$replications)
|
|
613
|
|
614 # Mean value for F2.
|
|
615 F2 <- apply(F2.replications, 1, mean)
|
|
616 # Standard error for F2.
|
|
617 F2.std_error <- apply(F2.replications, 1, sd) / sqrt(opt$replications)
|
|
618
|
99
|
619 # Mean value for F2 adults.
|
|
620 F2_adults <- apply(F2_adults.replications, 1, mean)
|
96
|
621 # Standard error for F2 adult.
|
99
|
622 F2_adults.std_error <- apply(F2_adults.replications, 1, sd) / sqrt(opt$replications)
|
85
|
623
|
102
|
624 dev.new(width=20, height=40)
|
85
|
625
|
|
626 # Start PDF device driver to save charts to output.
|
102
|
627 pdf(file=opt$output, width=20, height=40, bg="white")
|
90
|
628 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1))
|
|
629
|
|
630 # Data analysis and visualization plots
|
|
631 # only within a single calendar year.
|
104
|
632 days <- c(1:opt$num_days)
|
90
|
633 start_date <- temperature_data_frame$DATE[1]
|
|
634 end_date <- temperature_data_frame$DATE[opt$num_days]
|
85
|
635
|
|
636 # Subfigure 1: population size by life stage
|
102
|
637 maxval <- max(eggs+eggs.std_error, nymphs+nymphs.std_error, adults+adults.std_error)
|
104
|
638 render_chart("pop_size_by_life_stage", opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
102
|
639 opt$std_error_plot, adults, nymphs, eggs, adults.std_error, nymphs.std_error, eggs.std_error)
|
85
|
640 # Subfigure 2: population size by generation
|
102
|
641 maxval <- max(F2)
|
104
|
642 render_chart("pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
102
|
643 opt$std_error_plot, P, F1, F2, P.std_error, F1.std_error, F2.std_error)
|
85
|
644 # Subfigure 3: adult population size by generation
|
102
|
645 maxval <- max(F2_adults) + 100
|
104
|
646 render_chart("adult_pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
102
|
647 opt$std_error_plot, P_adults, F1_adults, F2_adults, P_adults.std_error, F1_adults.std_error, F2_adults2.std_error)
|
|
648
|
85
|
649 # Turn off device driver to flush output.
|
|
650 dev.off()
|