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