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