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