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