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