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
|
78
|
204 cat("Number of columns: ", opt$num_columns, "\n")
|
|
205
|
74
|
206 temperature_data_frame <- parse_input_data(opt$input, opt$num_days, opt$num_columns)
|
77
|
207 print(temperature_data_frame)
|
73
|
208 latitude <- temperature_data_frame[1, 1]
|
0
|
209
|
28
|
210 cat("Number of days: ", opt$num_days, "\n")
|
26
|
211
|
27
|
212 # Initialize matrix for results from all replications.
|
13
|
213 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol = opt$replications)
|
|
214 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
|
215
|
|
216 # loop through replications
|
|
217 for (N.rep in 1:opt$replications) {
|
26
|
218 # During each replication start with 1000 individuals.
|
|
219 # TODO: user definable as well?
|
0
|
220 n <- 1000
|
26
|
221 # Generation, Stage, DD, T, Diapause.
|
0
|
222 vec.ini <- c(0, 3, 0, 0, 0)
|
26
|
223 # Overwintering, previttelogenic, DD=0, T=0, no-diapause.
|
0
|
224 vec.mat <- rep(vec.ini, n)
|
26
|
225 # Complete matrix for the population.
|
|
226 vec.mat <- base::t(matrix(vec.mat, nrow=5))
|
|
227 # Time series of population size.
|
0
|
228 tot.pop <- NULL
|
13
|
229 gen0.pop <- rep(0, opt$num_days)
|
|
230 gen1.pop <- rep(0, opt$num_days)
|
|
231 gen2.pop <- rep(0, opt$num_days)
|
|
232 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, opt$num_days)
|
|
233 g0.adult <- g1.adult <- g2.adult <- rep(0, opt$num_days)
|
|
234 N.newborn <- N.death <- N.adult <- rep(0, opt$num_days)
|
|
235 dd.day <- rep(0, opt$num_days)
|
0
|
236
|
22
|
237 # All the days included in the input temperature dataset.
|
46
|
238 for (row in 1:opt$num_days) {
|
|
239 # Get the integer day of the year for the current row.
|
73
|
240 doy <- temperature_data_frame[row, 4]
|
26
|
241 # Photoperiod in the day.
|
73
|
242 photoperiod <- temperature_data_frame[row, 7]
|
|
243 temp.profile <- get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days)
|
0
|
244 mean.temp <- temp.profile[1]
|
|
245 dd.temp <- temp.profile[2]
|
47
|
246 dd.day[row] <- dd.temp
|
26
|
247 # Trash bin for death.
|
0
|
248 death.vec <- NULL
|
26
|
249 # Newborn.
|
0
|
250 birth.vec <- NULL
|
|
251
|
26
|
252 # All individuals.
|
0
|
253 for (i in 1:n) {
|
26
|
254 # Find individual record.
|
0
|
255 vec.ind <- vec.mat[i,]
|
26
|
256 # First of all, still alive?
|
|
257 # Adjustment for late season mortality rate.
|
22
|
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) {
|
26
|
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) {
|
26
|
274 # For adult.
|
47
|
275 if (doy < day.kill) {
|
0
|
276 death.prob = opt$adult_mort * mortality.adult(mean.temp)
|
|
277 }
|
|
278 else {
|
26
|
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 {
|
26
|
289 # Aggregrate index of dead bug.
|
|
290 # Event 1 end of diapause.
|
0
|
291 if (vec.ind[1] == 0 && vec.ind[2] == 3) {
|
26
|
292 # Overwintering adult (previttelogenic).
|
47
|
293 if (photoperiod > opt$photoperiod && vec.ind[3] > 68 && doy < 180) {
|
26
|
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 {
|
26
|
300 # Add to dd.
|
0
|
301 vec.ind[3] <- vec.ind[3] + dd.temp
|
26
|
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) {
|
26
|
308 # Not overwintering adult (previttelogenic).
|
0
|
309 current.gen <- vec.ind[1]
|
|
310 if (vec.ind[3] > 68) {
|
26
|
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 {
|
26
|
317 # Add to dd.
|
0
|
318 vec.ind[3] <- vec.ind[3] + dd.temp
|
26
|
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
|
26
|
325 # Event 2 oviposition -- where population dynamics comes from.
|
0
|
326 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) {
|
26
|
327 # Vittelogenic stage, overwintering generation.
|
0
|
328 if (vec.ind[4] == 0) {
|
26
|
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 {
|
26
|
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 }
|
26
|
340 # Add to dd.
|
0
|
341 vec.ind[3] <- vec.ind[3] + dd.temp
|
26
|
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) {
|
26
|
346 # Add new birth -- might be in different generations.
|
0
|
347 new.gen <- vec.ind[1] + 1
|
26
|
348 # Egg profile.
|
0
|
349 new.ind <- c(new.gen, 0, 0, 0, 0)
|
|
350 new.vec <- rep(new.ind, n.birth)
|
26
|
351 # Update batch of egg profile.
|
0
|
352 new.vec <- t(matrix(new.vec, nrow=5))
|
26
|
353 # Group with total eggs laid in that day.
|
0
|
354 birth.vec <- rbind(birth.vec, new.vec)
|
|
355 }
|
|
356 }
|
|
357
|
26
|
358 # Event 2 oviposition -- for gen 1.
|
47
|
359 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && doy < 222) {
|
26
|
360 # Vittelogenic stage, 1st generation
|
0
|
361 if (vec.ind[4] == 0) {
|
26
|
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 {
|
26
|
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 }
|
26
|
373 # Add to dd.
|
0
|
374 vec.ind[3] <- vec.ind[3] + dd.temp
|
26
|
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) {
|
26
|
379 # Add new birth -- might be in different generations.
|
0
|
380 new.gen <- vec.ind[1] + 1
|
26
|
381 # Egg profile.
|
0
|
382 new.ind <- c(new.gen, 0, 0, 0, 0)
|
|
383 new.vec <- rep(new.ind, n.birth)
|
26
|
384 # Update batch of egg profile.
|
0
|
385 new.vec <- t(matrix(new.vec, nrow=5))
|
26
|
386 # Group with total eggs laid in that day.
|
0
|
387 birth.vec <- rbind(birth.vec, new.vec)
|
|
388 }
|
|
389 }
|
|
390
|
26
|
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) {
|
26
|
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)) {
|
26
|
398 # From egg to young nymph, DD requirement met.
|
0
|
399 current.gen <- vec.ind[1]
|
26
|
400 # Transfer to young nymph stage.
|
0
|
401 vec.ind <- c(current.gen, 1, 0, 0, 0)
|
|
402 }
|
|
403 else {
|
26
|
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
|
26
|
410 # Event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause).
|
0
|
411 if (vec.ind[2] == 1) {
|
26
|
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)) {
|
26
|
416 # From young to old nymph, dd requirement met.
|
0
|
417 current.gen <- vec.ind[1]
|
26
|
418 # Transfer to old nym stage.
|
0
|
419 vec.ind <- c(current.gen, 2, 0, 0, 0)
|
47
|
420 if (photoperiod < opt$photoperiod && doy > 180) {
|
0
|
421 vec.ind[5] <- 1
|
26
|
422 } # Prepare for diapausing.
|
0
|
423 }
|
|
424 else {
|
26
|
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
|
26
|
431 # Event 3.3 old nymph to adult: previttelogenic or diapausing?
|
0
|
432 if (vec.ind[2] == 2) {
|
26
|
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)) {
|
26
|
437 # From old to adult, dd requirement met.
|
0
|
438 current.gen <- vec.ind[1]
|
|
439 if (vec.ind[5] == 0) {
|
26
|
440 # Non-diapausing adult -- previttelogenic.
|
0
|
441 vec.ind <- c(current.gen, 3, 0, 0, 0)
|
|
442 }
|
|
443 else {
|
26
|
444 # Diapausing.
|
0
|
445 vec.ind <- c(current.gen, 5, 0, 0, 1)
|
|
446 }
|
|
447 }
|
|
448 else {
|
26
|
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
|
26
|
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 }
|
26
|
461 } # Else if it is still alive.
|
|
462 } # End of the individual bug loop.
|
0
|
463
|
26
|
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 }
|
26
|
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)
|
26
|
473 # Update population size for the next day.
|
0
|
474 n <- n - n.death + n.newborn
|
|
475
|
26
|
476 # Aggregate results by day.
|
0
|
477 tot.pop <- c(tot.pop, n)
|
26
|
478 # Egg.
|
0
|
479 s0 <- sum(vec.mat[,2] == 0)
|
26
|
480 # Young nymph.
|
0
|
481 s1 <- sum(vec.mat[,2] == 1)
|
26
|
482 # Old nymph.
|
0
|
483 s2 <- sum(vec.mat[,2] == 2)
|
26
|
484 # Previtellogenic.
|
0
|
485 s3 <- sum(vec.mat[,2] == 3)
|
26
|
486 # Vitellogenic.
|
0
|
487 s4 <- sum(vec.mat[,2] == 4)
|
26
|
488 # Diapausing.
|
0
|
489 s5 <- sum(vec.mat[,2] == 5)
|
26
|
490 # Overwintering adult.
|
0
|
491 gen0 <- sum(vec.mat[,1] == 0)
|
26
|
492 # First generation.
|
0
|
493 gen1 <- sum(vec.mat[,1] == 1)
|
26
|
494 # Second generation.
|
0
|
495 gen2 <- sum(vec.mat[,1] == 2)
|
26
|
496 # Sum of all adults.
|
0
|
497 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5)
|
47
|
498
|
|
499 # Generation 0 pop size.
|
|
500 gen0.pop[row] <- gen0
|
|
501 gen1.pop[row] <- gen1
|
|
502 gen2.pop[row] <- gen2
|
0
|
503
|
47
|
504 S0[row] <- s0
|
|
505 S1[row] <- s1
|
|
506 S2[row] <- s2
|
|
507 S3[row] <- s3
|
|
508 S4[row] <- s4
|
|
509 S5[row] <- s5
|
|
510
|
|
511 g0.adult[row] <- sum(vec.mat[,1] == 0)
|
|
512 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))
|
|
513 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))
|
|
514
|
|
515 N.newborn[row] <- n.newborn
|
|
516 N.death[row] <- n.death
|
|
517 N.adult[row] <- n.adult
|
13
|
518 } # end of days specified in the input temperature data
|
0
|
519
|
|
520 dd.cum <- cumsum(dd.day)
|
47
|
521
|
26
|
522 # Collect all the outputs.
|
0
|
523 S0.rep[,N.rep] <- S0
|
|
524 S1.rep[,N.rep] <- S1
|
|
525 S2.rep[,N.rep] <- S2
|
|
526 S3.rep[,N.rep] <- S3
|
|
527 S4.rep[,N.rep] <- S4
|
|
528 S5.rep[,N.rep] <- S5
|
|
529 newborn.rep[,N.rep] <- N.newborn
|
|
530 death.rep[,N.rep] <- N.death
|
|
531 adult.rep[,N.rep] <- N.adult
|
|
532 pop.rep[,N.rep] <- tot.pop
|
|
533 g0.rep[,N.rep] <- gen0.pop
|
|
534 g1.rep[,N.rep] <- gen1.pop
|
|
535 g2.rep[,N.rep] <- gen2.pop
|
|
536 g0a.rep[,N.rep] <- g0.adult
|
|
537 g1a.rep[,N.rep] <- g1.adult
|
|
538 g2a.rep[,N.rep] <- g2.adult
|
|
539 }
|
|
540
|
44
|
541 # Data analysis and visualization can currently
|
46
|
542 # plot only within a single calendar year.
|
|
543 # TODO: enhance this to accomodate multiple calendar years.
|
73
|
544 start_date <- temperature_data_frame[1, 3]
|
|
545 end_date <- temperature_data_frame[opt$num_days, 3]
|
|
546
|
46
|
547 n.yr <- 1
|
|
548 day.all <- c(1:opt$num_days * n.yr)
|
0
|
549
|
|
550 # mean value for adults
|
|
551 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean)
|
|
552 # mean value for nymphs
|
|
553 sn <- apply((S1.rep + S2.rep), 1,mean)
|
|
554 # mean value for eggs
|
|
555 se <- apply(S0.rep, 1, mean)
|
|
556 # mean value for P
|
|
557 g0 <- apply(g0.rep, 1, mean)
|
|
558 # mean value for F1
|
|
559 g1 <- apply(g1.rep, 1, mean)
|
|
560 # mean value for F2
|
|
561 g2 <- apply(g2.rep, 1, mean)
|
|
562 # mean value for P adult
|
|
563 g0a <- apply(g0a.rep, 1, mean)
|
|
564 # mean value for F1 adult
|
|
565 g1a <- apply(g1a.rep, 1, mean)
|
|
566 # mean value for F2 adult
|
|
567 g2a <- apply(g2a.rep, 1, mean)
|
|
568
|
|
569 # SE for adults
|
|
570 sa.se <- apply((S3.rep + S4.rep + S5.rep), 1, sd) / sqrt(opt$replications)
|
|
571 # SE for nymphs
|
|
572 sn.se <- apply((S1.rep + S2.rep) / sqrt(opt$replications), 1, sd)
|
|
573 # SE for eggs
|
|
574 se.se <- apply(S0.rep, 1, sd) / sqrt(opt$replications)
|
|
575 # SE value for P
|
|
576 g0.se <- apply(g0.rep, 1, sd) / sqrt(opt$replications)
|
|
577 # SE for F1
|
|
578 g1.se <- apply(g1.rep, 1, sd) / sqrt(opt$replications)
|
|
579 # SE for F2
|
|
580 g2.se <- apply(g2.rep, 1, sd) / sqrt(opt$replications)
|
|
581 # SE for P adult
|
|
582 g0a.se <- apply(g0a.rep, 1, sd) / sqrt(opt$replications)
|
|
583 # SE for F1 adult
|
|
584 g1a.se <- apply(g1a.rep, 1, sd) / sqrt(opt$replications)
|
|
585 # SE for F2 adult
|
|
586 g2a.se <- apply(g2a.rep, 1, sd) / sqrt(opt$replications)
|
|
587
|
48
|
588 dev.new(width=20, height=30)
|
0
|
589
|
|
590 # Start PDF device driver to save charts to output.
|
48
|
591 pdf(file=opt$output, width=20, height=30, bg="white")
|
0
|
592
|
|
593 par(mar = c(5, 6, 4, 4), mfrow=c(3, 1))
|
|
594
|
54
|
595 # Subfigure 1: population size by life stage
|
|
596 title <- paste("BSMB total population by life stage :", opt$location, ": Lat:", latitude, ":", start_date, "to", end_date, sep=" ")
|
55
|
597 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
|
598 # Young and old nymphs.
|
|
599 lines(day.all, sn, lwd=2, lty=1, col=2)
|
0
|
600 # Eggs
|
31
|
601 lines(day.all, se, lwd=2, lty=1, col=4)
|
54
|
602 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"))
|
|
603 axis(2, cex.axis=3)
|
0
|
604 leg.text <- c("Egg", "Nymph", "Adult")
|
48
|
605 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(4, 2, 1), cex=3)
|
0
|
606 if (opt$se_plot == 1) {
|
48
|
607 # Add SE lines to plot
|
0
|
608 # SE for adults
|
31
|
609 lines (day.all, sa + sa.se, lty=2)
|
|
610 lines (day.all, sa - sa.se, lty=2)
|
0
|
611 # SE for nymphs
|
31
|
612 lines (day.all, sn + sn.se, col=2, lty=2)
|
48
|
613 lines (day.all, sn - sn.se, col=2, lty=2)
|
0
|
614 # SE for eggs
|
31
|
615 lines (day.all, se + se.se, col=4, lty=2)
|
48
|
616 lines (day.all, se - se.se, col=4, lty=2)
|
0
|
617 }
|
|
618
|
54
|
619 # Subfigure 2: population size by generation
|
|
620 title <- paste("BSMB total population by generation :", opt$location, ": Lat:", latitude, ":", start_date, "to", end_date, sep=" ")
|
55
|
621 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
|
622 lines(day.all, g1, lwd = 2, lty = 1, col=2)
|
|
623 lines(day.all, g2, lwd = 2, lty = 1, col=4)
|
54
|
624 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"))
|
|
625 axis(2, cex.axis=3)
|
0
|
626 leg.text <- c("P", "F1", "F2")
|
54
|
627 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(1, 2, 4), cex=3)
|
0
|
628 if (opt$se_plot == 1) {
|
48
|
629 # Add SE lines to plot
|
0
|
630 # SE for adults
|
56
|
631 lines (day.all, g0+g0.se, lty=2)
|
|
632 lines (day.all, g0-g0.se, lty=2)
|
0
|
633 # SE for nymphs
|
56
|
634 lines (day.all, g1+g1.se, col=2, lty=2)
|
|
635 lines (day.all, g1-g1.se, col=2, lty=2)
|
0
|
636 # SE for eggs
|
56
|
637 lines (day.all, g2+g2.se, col=4, lty=2)
|
|
638 lines (day.all, g2-g2.se, col=4, lty=2)
|
0
|
639 }
|
|
640
|
54
|
641 # Subfigure 3: adult population size by generation
|
|
642 title <- paste("BSMB adult population by generation :", opt$location, ": Lat:", latitude, ":", start_date, "to", end_date, sep=" ")
|
55
|
643 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
|
644 lines(day.all, g1a, lwd = 2, lty = 1, col=2)
|
|
645 lines(day.all, g2a, lwd = 2, lty = 1, col=4)
|
54
|
646 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"))
|
|
647 axis(2, cex.axis=3)
|
0
|
648 leg.text <- c("P", "F1", "F2")
|
54
|
649 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(1, 2, 4), cex=3)
|
0
|
650 if (opt$se_plot == 1) {
|
48
|
651 # Add SE lines to plot
|
0
|
652 # SE for adults
|
56
|
653 lines (day.all, g0a+g0a.se, lty=2)
|
|
654 lines (day.all, g0a-g0a.se, lty=2)
|
0
|
655 # SE for nymphs
|
56
|
656 lines (day.all, g1a+g1a.se, col=2, lty=2)
|
|
657 lines (day.all, g1a-g1a.se, col=2, lty=2)
|
0
|
658 # SE for eggs
|
56
|
659 lines (day.all, g2a+g2a.se, col=4, lty=2)
|
|
660 lines (day.all, g2a-g2a.se, col=4, lty=2)
|
0
|
661 }
|
|
662
|
|
663 # Turn off device driver to flush output.
|
|
664 dev.off()
|