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