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