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