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