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