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