annotate insect_phenology_model.R @ 6:fe3f86012394 draft

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