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"),
|
10
|
12 make_option(c("--life_stages"), action="store", dest="life_stages", help="Selected life stages for plotting"),
|
|
13 make_option(c("--life_stages_adult"), action="store", dest="life_stages_adult", default=NULL, help="Adult life stages for plotting"),
|
|
14 make_option(c("--life_stage_nymph"), action="store", dest="life_stage_nymph", default=NULL, help="Nymph life stages for plotting"),
|
6
|
15 make_option(c("--location"), action="store", dest="location", help="Selected location"),
|
|
16 make_option(c("--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
|
|
17 make_option(c("--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
|
|
18 make_option(c("--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"),
|
|
19 make_option(c("--old_nymph_accumulation"), action="store", dest="old_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"),
|
|
20 make_option(c("--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"),
|
|
21 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
|
|
22 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
|
|
23 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
|
10
|
24 make_option(c("--plot_generations_separately"), action="store", dest="plot_generations_separately", help="Plot Plot P, F1 and F2 as separate lines or pool across them"),
|
|
25 make_option(c("--plot_std_error"), action="store", dest="plot_std_error", help="Plot Standard error"),
|
6
|
26 make_option(c("--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)")
|
5
|
27 )
|
|
28
|
8
|
29 parser <- OptionParser(usage="%prog [options] file", option_list=option_list);
|
|
30 args <- parse_args(parser, positional_arguments=TRUE);
|
|
31 opt <- args$options;
|
5
|
32
|
|
33 add_daylight_length = function(temperature_data_frame, num_columns, num_rows) {
|
|
34 # Return a vector of daylight length (photoperido profile) for
|
|
35 # the number of days specified in the input temperature data
|
|
36 # (from Forsythe 1995).
|
8
|
37 p = 0.8333;
|
|
38 latitude = temperature_data_frame$LATITUDE[1];
|
|
39 daylight_length_vector = NULL;
|
5
|
40 for (i in 1:num_rows) {
|
|
41 # Get the day of the year from the current row
|
|
42 # of the temperature data for computation.
|
8
|
43 doy = temperature_data_frame$DOY[i];
|
|
44 theta = 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186)));
|
|
45 phi = asin(0.39795 * cos(theta));
|
5
|
46 # Compute the length of daylight for the day of the year.
|
8
|
47 darkness_length = 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)));
|
|
48 daylight_length_vector[i] = 24 - darkness_length;
|
5
|
49 }
|
|
50 # Append daylight_length_vector as a new column to temperature_data_frame.
|
8
|
51 temperature_data_frame[, num_columns+1] = daylight_length_vector;
|
|
52 return(temperature_data_frame);
|
5
|
53 }
|
|
54
|
6
|
55 dev.egg = function(temperature) {
|
8
|
56 dev.rate = -0.9843 * temperature + 33.438;
|
|
57 return(dev.rate);
|
6
|
58 }
|
|
59
|
|
60 dev.emerg = function(temperature) {
|
8
|
61 emerg.rate = -0.5332 * temperature + 24.147;
|
|
62 return(emerg.rate);
|
6
|
63 }
|
|
64
|
|
65 dev.old = function(temperature) {
|
8
|
66 n34 = -0.6119 * temperature + 17.602;
|
|
67 n45 = -0.4408 * temperature + 19.036;
|
|
68 dev.rate = mean(n34 + n45);
|
|
69 return(dev.rate);
|
6
|
70 }
|
|
71
|
|
72 dev.young = function(temperature) {
|
8
|
73 n12 = -0.3728 * temperature + 14.68;
|
|
74 n23 = -0.6119 * temperature + 25.249;
|
|
75 dev.rate = mean(n12 + n23);
|
|
76 return(dev.rate);
|
|
77 }
|
|
78
|
|
79 get_date_labels = function(temperature_data_frame, num_rows) {
|
|
80 # Keep track of the years to see if spanning years.
|
|
81 month_labels = list();
|
|
82 current_month_label = NULL;
|
|
83 for (i in 1:num_rows) {
|
|
84 # Get the year and month from the date which
|
|
85 # has the format YYYY-MM-DD.
|
|
86 date = format(temperature_data_frame$DATE[i]);
|
|
87 items = strsplit(date, "-")[[1]];
|
|
88 month = items[2];
|
|
89 month_label = month.abb[as.integer(month)];
|
|
90 if (!identical(current_month_label, month_label)) {
|
|
91 month_labels[length(month_labels)+1] = month_label;
|
|
92 current_month_label = month_label;
|
|
93 }
|
|
94 }
|
|
95 return(c(unlist(month_labels)));
|
6
|
96 }
|
|
97
|
5
|
98 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) {
|
8
|
99 # Base development threshold for Brown Marmorated Stink Bug
|
5
|
100 # insect phenology model.
|
8
|
101 threshold = 14.17;
|
5
|
102 # Minimum temperature for current row.
|
8
|
103 curr_min_temp = temperature_data_frame$TMIN[row];
|
5
|
104 # Maximum temperature for current row.
|
8
|
105 curr_max_temp = temperature_data_frame$TMAX[row];
|
5
|
106 # Mean temperature for current row.
|
8
|
107 curr_mean_temp = 0.5 * (curr_min_temp + curr_max_temp);
|
5
|
108 # Initialize degree day accumulation
|
8
|
109 averages = 0;
|
6
|
110 if (curr_max_temp < threshold) {
|
8
|
111 averages = 0;
|
5
|
112 }
|
|
113 else {
|
|
114 # Initialize hourly temperature.
|
8
|
115 T = NULL;
|
5
|
116 # Initialize degree hour vector.
|
8
|
117 dh = NULL;
|
5
|
118 # Daylight length for current row.
|
8
|
119 y = temperature_data_frame$DAYLEN[row];
|
5
|
120 # Darkness length.
|
8
|
121 z = 24 - y;
|
5
|
122 # Lag coefficient.
|
8
|
123 a = 1.86;
|
5
|
124 # Darkness coefficient.
|
8
|
125 b = 2.20;
|
5
|
126 # Sunrise time.
|
8
|
127 risetime = 12 - y / 2;
|
5
|
128 # Sunset time.
|
8
|
129 settime = 12 + y / 2;
|
|
130 ts = (curr_max_temp - curr_min_temp) * sin(pi * (settime - 5) / (y + 2 * a)) + curr_min_temp;
|
5
|
131 for (i in 1:24) {
|
|
132 if (i > risetime && i < settime) {
|
|
133 # Number of hours after Tmin until sunset.
|
8
|
134 m = i - 5;
|
|
135 T[i] = (curr_max_temp - curr_min_temp) * sin(pi * m / (y + 2 * a)) + curr_min_temp;
|
5
|
136 if (T[i] < 8.4) {
|
8
|
137 dh[i] = 0;
|
5
|
138 }
|
|
139 else {
|
8
|
140 dh[i] = T[i] - 8.4;
|
5
|
141 }
|
|
142 }
|
6
|
143 else if (i > settime) {
|
8
|
144 n = i - settime;
|
|
145 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z);
|
5
|
146 if (T[i] < 8.4) {
|
8
|
147 dh[i] = 0;
|
5
|
148 }
|
|
149 else {
|
8
|
150 dh[i] = T[i] - 8.4;
|
5
|
151 }
|
|
152 }
|
|
153 else {
|
8
|
154 n = i + 24 - settime;
|
|
155 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z);
|
5
|
156 if (T[i] < 8.4) {
|
8
|
157 dh[i] = 0;
|
5
|
158 }
|
|
159 else {
|
8
|
160 dh[i] = T[i] - 8.4;
|
5
|
161 }
|
|
162 }
|
|
163 }
|
8
|
164 averages = sum(dh) / 24;
|
5
|
165 }
|
6
|
166 return(c(curr_mean_temp, averages))
|
5
|
167 }
|
|
168
|
6
|
169 mortality.adult = function(temperature) {
|
|
170 if (temperature < 12.7) {
|
8
|
171 mortality.probability = 0.002;
|
6
|
172 }
|
|
173 else {
|
8
|
174 mortality.probability = temperature * 0.0005 + 0.02;
|
6
|
175 }
|
|
176 return(mortality.probability)
|
5
|
177 }
|
|
178
|
|
179 mortality.egg = function(temperature) {
|
|
180 if (temperature < 12.7) {
|
8
|
181 mortality.probability = 0.8;
|
5
|
182 }
|
|
183 else {
|
8
|
184 mortality.probability = 0.8 - temperature / 40.0;
|
6
|
185 if (mortality.probability < 0) {
|
8
|
186 mortality.probability = 0.01;
|
5
|
187 }
|
|
188 }
|
6
|
189 return(mortality.probability)
|
5
|
190 }
|
|
191
|
|
192 mortality.nymph = function(temperature) {
|
|
193 if (temperature < 12.7) {
|
8
|
194 mortality.probability = 0.03;
|
5
|
195 }
|
|
196 else {
|
8
|
197 mortality.probability = temperature * 0.0008 + 0.03;
|
5
|
198 }
|
8
|
199 return(mortality.probability);
|
6
|
200 }
|
|
201
|
|
202 parse_input_data = function(input_file, num_rows) {
|
|
203 # Read in the input temperature datafile into a data frame.
|
8
|
204 temperature_data_frame = read.csv(file=input_file, header=T, strip.white=TRUE, sep=",");
|
|
205 num_columns = dim(temperature_data_frame)[2];
|
6
|
206 if (num_columns == 6) {
|
|
207 # The input data has the following 6 columns:
|
|
208 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
|
|
209 # Set the column names for access when adding daylight length..
|
8
|
210 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
|
6
|
211 # Add a column containing the daylight length for each day.
|
8
|
212 temperature_data_frame = add_daylight_length(temperature_data_frame, num_columns, num_rows);
|
6
|
213 # Reset the column names with the additional column for later access.
|
8
|
214 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX", "DAYLEN");
|
6
|
215 }
|
8
|
216 return(temperature_data_frame);
|
5
|
217 }
|
|
218
|
8
|
219
|
10
|
220 render_chart = function(date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,
|
|
221 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,
|
|
222 life_stages_adult=NULL, life_stage_nymph=NULL) {
|
|
223 if (chart_type=="pop_size_by_life_stage") {
|
|
224 if (life_stage=="Total") {
|
|
225 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
226 legend_text = c("Egg", "Nymph", "Adult");
|
|
227 columns = c(4, 2, 1);
|
|
228 plot(days, group, 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);
|
|
229 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
|
|
230 lines(days, group2, lwd=2, lty=1, col=2);
|
|
231 lines(days, group3, lwd=2, lty=1, col=4);
|
|
232 axis(1, at=c(1:length(date_labels)) * 30 - 15, cex.axis=3, labels=date_labels);
|
|
233 axis(2, cex.axis=3);
|
|
234 if (plot_std_error=="yes") {
|
|
235 # Standard error for group.
|
|
236 lines(days, group+group_std_error, lty=2);
|
|
237 lines(days, group-group_std_error, lty=2);
|
|
238 # Standard error for group2.
|
|
239 lines(days, group2+group2_std_error, col=2, lty=2);
|
|
240 lines(days, group2-group2_std_error, col=2, lty=2);
|
|
241 # Standard error for group3.
|
|
242 lines(days, group3+group3_std_error, col=4, lty=2);
|
|
243 lines(days, group3-group3_std_error, col=4, lty=2);
|
|
244 }
|
|
245 } else {
|
|
246 if (life_stage=="Egg") {
|
|
247 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
248 legend_text = c(life_stage);
|
|
249 columns = c(4);
|
|
250 } else if (life_stage=="Nymph") {
|
|
251 stage = paste(life_stage_nymph, "Nymph Pop :", sep=" ");
|
|
252 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
253 legend_text = c(paste(life_stage_nymph, life_stage, sep=" "));
|
|
254 columns = c(2);
|
|
255 } else if (life_stage=="Adult") {
|
|
256 stage = paste(life_stages_adult, "Adult Pop", sep=" ");
|
|
257 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
258 legend_text = c(paste(life_stages_adult, life_stage, sep=" "));
|
|
259 columns = c(1);
|
|
260 }
|
|
261 plot(days, group, 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);
|
|
262 legend("topleft", legend_text, lty=c(1), col="black", cex=3);
|
|
263 axis(1, at=c(1:length(date_labels)) * 30 - 15, cex.axis=3, labels=date_labels);
|
|
264 axis(2, cex.axis=3);
|
|
265 if (plot_std_error=="yes") {
|
|
266 # Standard error for group.
|
|
267 lines(days, group+group_std_error, lty=2);
|
|
268 lines(days, group-group_std_error, lty=2);
|
|
269 }
|
|
270 }
|
|
271 } else if (chart_type=="pop_size_by_generation") {
|
|
272 if (life_stage=="Total") {
|
|
273 title_str = ": Total Pop by Gen :";
|
|
274 } else if (life_stage=="Egg") {
|
|
275 title_str = ": Egg Pop by Gen :";
|
|
276 } else if (life_stage=="Nymph") {
|
|
277 title_str = paste(":", life_stage_nymph, "Nymph Pop by Gen", ":", sep=" ");
|
|
278 } else if (life_stage=="Adult") {
|
|
279 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" ");
|
|
280 }
|
|
281 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
8
|
282 legend_text = c("P", "F1", "F2");
|
|
283 columns = c(1, 2, 4);
|
10
|
284 plot(days, group, 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);
|
|
285 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
|
|
286 lines(days, group2, lwd=2, lty=1, col=2);
|
|
287 lines(days, group3, lwd=2, lty=1, col=4);
|
|
288 axis(1, at=c(1:length(date_labels)) * 30 - 15, cex.axis=3, labels=date_labels);
|
|
289 axis(2, cex.axis=3);
|
|
290 if (plot_std_error=="yes") {
|
|
291 # Standard error for group.
|
|
292 lines(days, group+group_std_error, lty=2);
|
|
293 lines(days, group-group_std_error, lty=2);
|
|
294 # Standard error for group2.
|
|
295 lines(days, group2+group2_std_error, col=2, lty=2);
|
|
296 lines(days, group2-group2_std_error, col=2, lty=2);
|
|
297 # Standard error for group3.
|
|
298 lines(days, group3+group3_std_error, col=4, lty=2);
|
|
299 lines(days, group3-group3_std_error, col=4, lty=2);
|
|
300 }
|
5
|
301 }
|
|
302 }
|
|
303
|
10
|
304 # Determine if we're plotting generations separately.
|
|
305 if (opt$plot_generations_separately=="yes") {
|
|
306 plot_generations_separately = TRUE;
|
|
307 } else {
|
|
308 plot_generations_separately = FALSE;
|
|
309 }
|
|
310 # Read the temperature data into a data frame.
|
8
|
311 temperature_data_frame = parse_input_data(opt$input, opt$num_days);
|
10
|
312 output_dir = "output_dir";
|
|
313 # Get the date labels for plots.
|
|
314 date_labels = get_date_labels(temperature_data_frame, opt$num_days);
|
|
315 # All latitude values are the same, so get the value for plots from the first row.
|
8
|
316 latitude = temperature_data_frame$LATITUDE[1];
|
10
|
317 # Get the number of days for plots.
|
8
|
318 num_columns = dim(temperature_data_frame)[2];
|
10
|
319 # Split life_stages into a list of strings for plots.
|
|
320 life_stages_str = as.character(opt$life_stages);
|
|
321 life_stages = strsplit(life_stages_str, ",")[[1]];
|
|
322 # Determine the data we need to generate for plotting.
|
|
323 process_eggs = FALSE;
|
|
324 process_nymphs = FALSE;
|
|
325 process_adults = FALSE;
|
|
326 for (life_stage in life_stages) {
|
|
327 if (life_stage=="Total") {
|
|
328 process_eggs = TRUE;
|
|
329 process_nymphs = TRUE;
|
|
330 life_stage_nymph = "Total";
|
|
331 process_adults = TRUE;
|
|
332 life_stages_adult = "Total";
|
|
333 } else if (life_stage=="Egg") {
|
|
334 process_eggs = TRUE;
|
|
335 } else if (life_stage=="Nymph") {
|
|
336 process_nymphs = TRUE;
|
|
337 life_stage_nymph = opt$life_stage_nymph;
|
|
338 } else if (life_stage=="Adult") {
|
|
339 process_adults = TRUE;
|
|
340 life_stages_adult = opt$life_stages_adult;
|
|
341 }
|
|
342 }
|
5
|
343
|
6
|
344 # Initialize matrices.
|
10
|
345 if (process_eggs) {
|
|
346 Eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
347 }
|
|
348 if (process_nymphs) {
|
|
349 YoungNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
350 OldNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
351 }
|
|
352 if (process_adults) {
|
|
353 Previtellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
354 Vitellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
355 Diapausing.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
356 }
|
8
|
357 newborn.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
358 adult.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
359 death.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
10
|
360 if (plot_generations_separately) {
|
|
361 # P is Parental, or overwintered adults.
|
|
362 P.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
363 # F1 is the first field-produced generation.
|
|
364 F1.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
365 # F2 is the second field-produced generation.
|
|
366 F2.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
367 if (process_eggs) {
|
|
368 P_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
369 F1_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
370 F2_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
371 }
|
|
372 if (process_nymphs) {
|
|
373 P_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
374 F1_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
375 F2_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
376 }
|
|
377 if (process_adults) {
|
|
378 P_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
379 F1_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
380 F2_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
|
381 }
|
|
382 }
|
|
383 # Total population.
|
8
|
384 population.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
|
5
|
385
|
6
|
386 # Process replications.
|
|
387 for (N.replications in 1:opt$replications) {
|
|
388 # Start with the user-defined number of insects per replication.
|
8
|
389 num_insects = opt$insects_per_replication;
|
6
|
390 # Generation, Stage, degree-days, T, Diapause.
|
8
|
391 vector.ini = c(0, 3, 0, 0, 0);
|
10
|
392 # Replicate to create a matrix where the columns are
|
|
393 # Generation, Stage, degree-days, T, Diapause and the
|
|
394 # rows are the initial number of insects per replication.
|
8
|
395 vector.matrix = rep(vector.ini, num_insects);
|
10
|
396 # Complete transposed matrix for the population, so now
|
|
397 # the rows are Generation, Stage, degree-days, T, Diapause
|
8
|
398 vector.matrix = base::t(matrix(vector.matrix, nrow=5));
|
5
|
399 # Time series of population size.
|
10
|
400 if (process_eggs) {
|
|
401 Eggs = rep(0, opt$num_days);
|
|
402 }
|
|
403 if (process_nymphs) {
|
|
404 YoungNymphs = rep(0, opt$num_days);
|
|
405 OldNymphs = rep(0, opt$num_days);
|
|
406 }
|
|
407 if (process_adults) {
|
|
408 Previtellogenic = rep(0, opt$num_days);
|
|
409 Vitellogenic = rep(0, opt$num_days);
|
|
410 Diapausing = rep(0, opt$num_days);
|
|
411 }
|
8
|
412 N.newborn = rep(0, opt$num_days);
|
|
413 N.adult = rep(0, opt$num_days);
|
|
414 N.death = rep(0, opt$num_days);
|
|
415 overwintering_adult.population = rep(0, opt$num_days);
|
|
416 first_generation.population = rep(0, opt$num_days);
|
|
417 second_generation.population = rep(0, opt$num_days);
|
10
|
418 if (plot_generations_separately) {
|
|
419 # P is Parental, or overwintered adults.
|
|
420 # F1 is the first field-produced generation.
|
|
421 # F2 is the second field-produced generation.
|
|
422 if (process_eggs) {
|
|
423 P.egg = rep(0, opt$num_days);
|
|
424 F1.egg = rep(0, opt$num_days);
|
|
425 F2.egg = rep(0, opt$num_days);
|
|
426 }
|
|
427 if (process_nymphs) {
|
|
428 P.nymph = rep(0, opt$num_days);
|
|
429 F1.nymph = rep(0, opt$num_days);
|
|
430 F2.nymph = rep(0, opt$num_days);
|
|
431 }
|
|
432 if (process_adults) {
|
|
433 P.adult = rep(0, opt$num_days);
|
|
434 F1.adult = rep(0, opt$num_days);
|
|
435 F2.adult = rep(0, opt$num_days);
|
|
436 }
|
|
437 }
|
8
|
438 total.population = NULL;
|
|
439 averages.day = rep(0, opt$num_days);
|
5
|
440 # All the days included in the input temperature dataset.
|
|
441 for (row in 1:opt$num_days) {
|
|
442 # Get the integer day of the year for the current row.
|
8
|
443 doy = temperature_data_frame$DOY[row];
|
5
|
444 # Photoperiod in the day.
|
8
|
445 photoperiod = temperature_data_frame$DAYLEN[row];
|
|
446 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days);
|
|
447 mean.temp = temp.profile[1];
|
|
448 averages.temp = temp.profile[2];
|
|
449 averages.day[row] = averages.temp;
|
5
|
450 # Trash bin for death.
|
8
|
451 death.vector = NULL;
|
5
|
452 # Newborn.
|
8
|
453 birth.vector = NULL;
|
5
|
454 # All individuals.
|
6
|
455 for (i in 1:num_insects) {
|
|
456 # Individual record.
|
8
|
457 vector.individual = vector.matrix[i,];
|
6
|
458 # Adjustment for late season mortality rate (still alive?).
|
5
|
459 if (latitude < 40.0) {
|
8
|
460 post.mortality = 1;
|
|
461 day.kill = 300;
|
5
|
462 }
|
|
463 else {
|
8
|
464 post.mortality = 2;
|
|
465 day.kill = 250;
|
5
|
466 }
|
6
|
467 if (vector.individual[2] == 0) {
|
5
|
468 # Egg.
|
8
|
469 death.probability = opt$egg_mortality * mortality.egg(mean.temp);
|
5
|
470 }
|
6
|
471 else if (vector.individual[2] == 1 | vector.individual[2] == 2) {
|
8
|
472 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp);
|
5
|
473 }
|
6
|
474 else if (vector.individual[2] == 3 | vector.individual[2] == 4 | vector.individual[2] == 5) {
|
|
475 # Adult.
|
5
|
476 if (doy < day.kill) {
|
8
|
477 death.probability = opt$adult_mortality * mortality.adult(mean.temp);
|
5
|
478 }
|
|
479 else {
|
|
480 # Increase adult mortality after fall equinox.
|
8
|
481 death.probability = opt$adult_mortality * post.mortality * mortality.adult(mean.temp);
|
5
|
482 }
|
|
483 }
|
6
|
484 # Dependent on temperature and life stage?
|
8
|
485 u.d = runif(1);
|
6
|
486 if (u.d < death.probability) {
|
8
|
487 death.vector = c(death.vector, i);
|
6
|
488 }
|
5
|
489 else {
|
6
|
490 # End of diapause.
|
|
491 if (vector.individual[1] == 0 && vector.individual[2] == 3) {
|
5
|
492 # Overwintering adult (previttelogenic).
|
6
|
493 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) {
|
5
|
494 # Add 68C to become fully reproductively matured.
|
|
495 # Transfer to vittelogenic.
|
8
|
496 vector.individual = c(0, 4, 0, 0, 0);
|
|
497 vector.matrix[i,] = vector.individual;
|
5
|
498 }
|
|
499 else {
|
6
|
500 # Add to # Add average temperature for current day.
|
8
|
501 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
502 # Add 1 day in current stage.
|
8
|
503 vector.individual[4] = vector.individual[4] + 1;
|
|
504 vector.matrix[i,] = vector.individual;
|
5
|
505 }
|
|
506 }
|
6
|
507 if (vector.individual[1] != 0 && vector.individual[2] == 3) {
|
5
|
508 # Not overwintering adult (previttelogenic).
|
8
|
509 current.gen = vector.individual[1];
|
6
|
510 if (vector.individual[3] > 68) {
|
5
|
511 # Add 68C to become fully reproductively matured.
|
|
512 # Transfer to vittelogenic.
|
8
|
513 vector.individual = c(current.gen, 4, 0, 0, 0);
|
|
514 vector.matrix[i,] = vector.individual;
|
5
|
515 }
|
|
516 else {
|
6
|
517 # Add average temperature for current day.
|
8
|
518 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
519 # Add 1 day in current stage.
|
8
|
520 vector.individual[4] = vector.individual[4] + 1;
|
|
521 vector.matrix[i,] = vector.individual;
|
5
|
522 }
|
|
523 }
|
6
|
524 # Oviposition -- where population dynamics comes from.
|
|
525 if (vector.individual[2] == 4 && vector.individual[1] == 0 && mean.temp > 10) {
|
5
|
526 # Vittelogenic stage, overwintering generation.
|
6
|
527 if (vector.individual[4] == 0) {
|
5
|
528 # Just turned in vittelogenic stage.
|
8
|
529 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size));
|
5
|
530 }
|
|
531 else {
|
|
532 # Daily probability of birth.
|
8
|
533 p.birth = opt$oviposition * 0.01;
|
|
534 u1 = runif(1);
|
5
|
535 if (u1 < p.birth) {
|
8
|
536 num_insects.birth = round(runif(1, 2, 8));
|
5
|
537 }
|
|
538 }
|
6
|
539 # Add average temperature for current day.
|
8
|
540 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
541 # Add 1 day in current stage.
|
8
|
542 vector.individual[4] = vector.individual[4] + 1;
|
|
543 vector.matrix[i,] = vector.individual;
|
6
|
544 if (num_insects.birth > 0) {
|
5
|
545 # Add new birth -- might be in different generations.
|
8
|
546 new.gen = vector.individual[1] + 1;
|
5
|
547 # Egg profile.
|
8
|
548 new.individual = c(new.gen, 0, 0, 0, 0);
|
|
549 new.vector = rep(new.individual, num_insects.birth);
|
5
|
550 # Update batch of egg profile.
|
8
|
551 new.vector = t(matrix(new.vector, nrow=5));
|
5
|
552 # Group with total eggs laid in that day.
|
8
|
553 birth.vector = rbind(birth.vector, new.vector);
|
5
|
554 }
|
|
555 }
|
6
|
556 # Oviposition -- for generation 1.
|
|
557 if (vector.individual[2] == 4 && vector.individual[1] == 1 && mean.temp > 12.5 && doy < 222) {
|
5
|
558 # Vittelogenic stage, 1st generation
|
6
|
559 if (vector.individual[4] == 0) {
|
5
|
560 # Just turned in vittelogenic stage.
|
8
|
561 num_insects.birth = round(runif(1, 2+opt$min_clutch_size, 8+opt$max_clutch_size));
|
5
|
562 }
|
|
563 else {
|
|
564 # Daily probability of birth.
|
8
|
565 p.birth = opt$oviposition * 0.01;
|
|
566 u1 = runif(1);
|
5
|
567 if (u1 < p.birth) {
|
8
|
568 num_insects.birth = round(runif(1, 2, 8));
|
5
|
569 }
|
|
570 }
|
6
|
571 # Add average temperature for current day.
|
8
|
572 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
573 # Add 1 day in current stage.
|
8
|
574 vector.individual[4] = vector.individual[4] + 1;
|
|
575 vector.matrix[i,] = vector.individual;
|
6
|
576 if (num_insects.birth > 0) {
|
5
|
577 # Add new birth -- might be in different generations.
|
8
|
578 new.gen = vector.individual[1] + 1;
|
5
|
579 # Egg profile.
|
8
|
580 new.individual = c(new.gen, 0, 0, 0, 0);
|
|
581 new.vector = rep(new.individual, num_insects.birth);
|
5
|
582 # Update batch of egg profile.
|
8
|
583 new.vector = t(matrix(new.vector, nrow=5));
|
5
|
584 # Group with total eggs laid in that day.
|
8
|
585 birth.vector = rbind(birth.vector, new.vector);
|
5
|
586 }
|
|
587 }
|
6
|
588 # Egg to young nymph.
|
|
589 if (vector.individual[2] == 0) {
|
|
590 # Add average temperature for current day.
|
8
|
591 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
592 if (vector.individual[3] >= (68+opt$young_nymph_accumulation)) {
|
|
593 # From egg to young nymph, degree-days requirement met.
|
8
|
594 current.gen = vector.individual[1];
|
5
|
595 # Transfer to young nymph stage.
|
8
|
596 vector.individual = c(current.gen, 1, 0, 0, 0);
|
5
|
597 }
|
|
598 else {
|
|
599 # Add 1 day in current stage.
|
8
|
600 vector.individual[4] = vector.individual[4] + 1;
|
5
|
601 }
|
8
|
602 vector.matrix[i,] = vector.individual;
|
5
|
603 }
|
6
|
604 # Young nymph to old nymph.
|
|
605 if (vector.individual[2] == 1) {
|
|
606 # Add average temperature for current day.
|
8
|
607 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
608 if (vector.individual[3] >= (250+opt$old_nymph_accumulation)) {
|
|
609 # From young to old nymph, degree_days requirement met.
|
8
|
610 current.gen = vector.individual[1];
|
5
|
611 # Transfer to old nym stage.
|
8
|
612 vector.individual = c(current.gen, 2, 0, 0, 0);
|
5
|
613 if (photoperiod < opt$photoperiod && doy > 180) {
|
8
|
614 vector.individual[5] = 1;
|
5
|
615 } # Prepare for diapausing.
|
|
616 }
|
|
617 else {
|
|
618 # Add 1 day in current stage.
|
8
|
619 vector.individual[4] = vector.individual[4] + 1;
|
5
|
620 }
|
8
|
621 vector.matrix[i,] = vector.individual;
|
6
|
622 }
|
|
623 # Old nymph to adult: previttelogenic or diapausing?
|
|
624 if (vector.individual[2] == 2) {
|
|
625 # Add average temperature for current day.
|
8
|
626 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
627 if (vector.individual[3] >= (200+opt$adult_accumulation)) {
|
|
628 # From old to adult, degree_days requirement met.
|
8
|
629 current.gen = vector.individual[1];
|
6
|
630 if (vector.individual[5] == 0) {
|
|
631 # Previttelogenic.
|
8
|
632 vector.individual = c(current.gen, 3, 0, 0, 0);
|
5
|
633 }
|
|
634 else {
|
|
635 # Diapausing.
|
8
|
636 vector.individual = c(current.gen, 5, 0, 0, 1);
|
5
|
637 }
|
|
638 }
|
|
639 else {
|
|
640 # Add 1 day in current stage.
|
8
|
641 vector.individual[4] = vector.individual[4] + 1;
|
5
|
642 }
|
8
|
643 vector.matrix[i,] = vector.individual;
|
5
|
644 }
|
6
|
645 # Growing of diapausing adult (unimportant, but still necessary).
|
|
646 if (vector.individual[2] == 5) {
|
8
|
647 vector.individual[3] = vector.individual[3] + averages.temp;
|
|
648 vector.individual[4] = vector.individual[4] + 1;
|
|
649 vector.matrix[i,] = vector.individual;
|
5
|
650 }
|
|
651 } # Else if it is still alive.
|
|
652 } # End of the individual bug loop.
|
6
|
653
|
|
654 # Number of deaths.
|
8
|
655 num_insects.death = length(death.vector);
|
6
|
656 if (num_insects.death > 0) {
|
|
657 # Remove record of dead.
|
8
|
658 vector.matrix = vector.matrix[-death.vector,];
|
5
|
659 }
|
6
|
660 # Number of births.
|
8
|
661 num_insects.newborn = length(birth.vector[,1]);
|
|
662 vector.matrix = rbind(vector.matrix, birth.vector);
|
5
|
663 # Update population size for the next day.
|
8
|
664 num_insects = num_insects - num_insects.death + num_insects.newborn;
|
5
|
665
|
10
|
666 # Aggregate results by day. Due to multiple transpose calls
|
|
667 # on vector.matrix above, the columns of vector.matrix
|
|
668 # are now Generation, Stage, degree-days, T, Diapause,
|
|
669 if (process_eggs) {
|
|
670 # For egg population size, column 2 (Stage), must be 0.
|
|
671 Eggs[row] = sum(vector.matrix[,2]==0);
|
|
672 }
|
|
673 if (process_nymphs) {
|
|
674 # For young nymph population size, column 2 (Stage) must be 1.
|
|
675 YoungNymphs[row] = sum(vector.matrix[,2]==1);
|
|
676 # For old nymph population size, column 2 (Stage) must be 2.
|
|
677 OldNymphs[row] = sum(vector.matrix[,2]==2);
|
|
678 }
|
|
679 if (process_adults) {
|
|
680 # For pre-vitellogenic population size, column 2 (Stage) must be 3.
|
|
681 Previtellogenic[row] = sum(vector.matrix[,2]==3);
|
|
682 # For vitellogenic population size, column 2 (Stage) must be 4.
|
|
683 Vitellogenic[row] = sum(vector.matrix[,2]==4);
|
|
684 # For diapausing population size, column 2 (Stage) must be 5.
|
|
685 Diapausing[row] = sum(vector.matrix[,2]==5);
|
|
686 }
|
5
|
687
|
6
|
688 # Newborn population size.
|
8
|
689 N.newborn[row] = num_insects.newborn;
|
6
|
690 # Adult population size.
|
8
|
691 N.adult[row] = sum(vector.matrix[,2]==3) + sum(vector.matrix[,2]==4) + sum(vector.matrix[,2]==5);
|
6
|
692 # Dead population size.
|
8
|
693 N.death[row] = num_insects.death;
|
6
|
694
|
8
|
695 total.population = c(total.population, num_insects);
|
6
|
696
|
10
|
697 # For overwintering adult (P) population
|
|
698 # size, column 1 (Generation) must be 0.
|
8
|
699 overwintering_adult.population[row] = sum(vector.matrix[,1]==0);
|
10
|
700 # For first field generation (F1) population
|
|
701 # size, column 1 (Generation) must be 1.
|
8
|
702 first_generation.population[row] = sum(vector.matrix[,1]==1);
|
10
|
703 # For second field generation (F2) population
|
|
704 # size, column 1 (Generation) must be 2.
|
8
|
705 second_generation.population[row] = sum(vector.matrix[,1]==2);
|
5
|
706
|
10
|
707 if (plot_generations_separately) {
|
|
708 if (process_eggs) {
|
|
709 # For egg life stage of generation F1 population size,
|
|
710 # column 1 (generation) is 0 and column 2 (Stage) is 0.
|
|
711 P.egg[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==0);
|
|
712 # For egg life stage of generation F1 population size,
|
|
713 # column 1 (generation) is 1 and column 2 (Stage) is 0.
|
|
714 F1.egg[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==0);
|
|
715 # For egg life stage of generation F2 population size,
|
|
716 # column 1 (generation) is 2 and column 2 (Stage) is 0.
|
|
717 F2.egg[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==0);
|
|
718 }
|
|
719 if (process_nymphs) {
|
|
720 # For nymph life stage of generation F1 population
|
|
721 # size, one of the following combinations is required:
|
|
722 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph)
|
|
723 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph)
|
|
724 P.nymph[row] = sum((vector.matrix[,1]==0 & vector.matrix[,2]==1) | (vector.matrix[,1]==0 & vector.matrix[,2]==2));
|
|
725 # For nymph life stage of generation F1 population
|
|
726 # size, one of the following combinations is required:
|
|
727 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph)
|
|
728 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph)
|
|
729 F1.nymph[row] = sum((vector.matrix[,1]==1 & vector.matrix[,2]==1) | (vector.matrix[,1]==1 & vector.matrix[,2]==2));
|
|
730 # For nymph life stage of generation F2 population
|
|
731 # size, one of the following combinations is required:
|
|
732 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph)
|
|
733 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph)
|
|
734 F2.nymph[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==1) | (vector.matrix[,1]==2 & vector.matrix[,2]==2));
|
|
735 }
|
|
736 if (process_adults) {
|
|
737 # For adult life stage of generation P population
|
|
738 # size, one of the following combinations is required:
|
|
739 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vitellogenic)
|
|
740 # - column 1 (Generation) is 0 and column 2 (Stage) is 4 (Vitellogenic)
|
|
741 # - column 1 (Generation) is 0 and column 2 (Stage) is 5 (Diapausing)
|
|
742 P.adult[row] = sum((vector.matrix[,1]==0 & vector.matrix[,2]==3) | (vector.matrix[,1]==0 & vector.matrix[,2]==4) | (vector.matrix[,1]==0 & vector.matrix[,2]==5));
|
|
743 # For adult life stage of generation F1 population
|
|
744 # size, one of the following combinations is required:
|
|
745 # - column 1 (Generation) is 1 and column 2 (Stage) is 3 (Pre-vitellogenic)
|
|
746 # - column 1 (Generation) is 1 and column 2 (Stage) is 4 (Vitellogenic)
|
|
747 # - column 1 (Generation) is 1 and column 2 (Stage) is 5 (Diapausing)
|
|
748 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));
|
|
749 # For adult life stage of generation F2 population
|
|
750 # size, one of the following combinations is required:
|
|
751 # - column 1 (Generation) is 2 and column 2 (Stage) is 3 (Pre-vitellogenic)
|
|
752 # - column 1 (Generation) is 2 and column 2 (Stage) is 4 (Vitellogenic)
|
|
753 # - column 1 (Generation) is 2 and column 2 (Stage) is 5 (Diapausing)
|
|
754 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));
|
|
755 }
|
|
756 }
|
6
|
757 } # End of days specified in the input temperature data.
|
5
|
758
|
8
|
759 averages.cum = cumsum(averages.day);
|
5
|
760
|
6
|
761 # Define the output values.
|
10
|
762 if (process_eggs) {
|
|
763 Eggs.replications[,N.replications] = Eggs;
|
|
764 }
|
|
765 if (process_nymphs) {
|
|
766 YoungNymphs.replications[,N.replications] = YoungNymphs;
|
|
767 OldNymphs.replications[,N.replications] = OldNymphs;
|
|
768 }
|
|
769 if (process_adults) {
|
|
770 Previtellogenic.replications[,N.replications] = Previtellogenic;
|
|
771 Vitellogenic.replications[,N.replications] = Vitellogenic;
|
|
772 Diapausing.replications[,N.replications] = Diapausing;
|
|
773 }
|
8
|
774 newborn.replications[,N.replications] = N.newborn;
|
|
775 adult.replications[,N.replications] = N.adult;
|
|
776 death.replications[,N.replications] = N.death;
|
10
|
777 if (plot_generations_separately) {
|
|
778 # P is Parental, or overwintered adults.
|
|
779 P.replications[,N.replications] = overwintering_adult.population;
|
|
780 # F1 is the first field-produced generation.
|
|
781 F1.replications[,N.replications] = first_generation.population;
|
|
782 # F2 is the second field-produced generation.
|
|
783 F2.replications[,N.replications] = second_generation.population;
|
|
784 if (process_eggs) {
|
|
785 P_eggs.replications[,N.replications] = P.egg;
|
|
786 F1_eggs.replications[,N.replications] = F1.egg;
|
|
787 F2_eggs.replications[,N.replications] = F2.egg;
|
|
788 }
|
|
789 if (process_nymphs) {
|
|
790 P_nymphs.replications[,N.replications] = P.nymph;
|
|
791 F1_nymphs.replications[,N.replications] = F1.nymph;
|
|
792 F2_nymphs.replications[,N.replications] = F2.nymph;
|
|
793 }
|
|
794 if (process_adults) {
|
|
795 P_adults.replications[,N.replications] = P.adult;
|
|
796 F1_adults.replications[,N.replications] = F1.adult;
|
|
797 F2_adults.replications[,N.replications] = F2.adult;
|
|
798 }
|
|
799 }
|
8
|
800 population.replications[,N.replications] = total.population;
|
5
|
801 }
|
|
802
|
10
|
803 if (process_eggs) {
|
|
804 # Mean value for eggs.
|
|
805 eggs = apply(Eggs.replications, 1, mean);
|
|
806 # Standard error for eggs.
|
|
807 eggs.std_error = apply(Eggs.replications, 1, sd) / sqrt(opt$replications);
|
|
808 }
|
|
809 if (process_nymphs) {
|
|
810 # Calculate nymph populations for selected life stage.
|
|
811 if (life_stage_nymph=="Total") {
|
|
812 # Mean value for all nymphs.
|
|
813 nymphs = apply((YoungNymphs.replications+OldNymphs.replications), 1, mean);
|
|
814 # Standard error for all nymphs.
|
|
815 nymphs.std_error = apply((YoungNymphs.replications+OldNymphs.replications) / sqrt(opt$replications), 1, sd);
|
|
816 } else if (life_stage_nymph=="Young") {
|
|
817 # Mean value for young nymphs.
|
|
818 nymphs = apply(YoungNymphs.replications, 1, mean);
|
|
819 # Standard error for young nymphs.
|
|
820 nymphs.std_error = apply(YoungNymphs.replications / sqrt(opt$replications), 1, sd);
|
|
821 } else if (life_stage_nymph=="Old") {
|
|
822 # Mean value for old nymphs.
|
|
823 nymphs = apply(OldNymphs.replications, 1, mean);
|
|
824 # Standard error for old nymphs.
|
|
825 nymphs.std_error = apply(OldNymphs.replications / sqrt(opt$replications), 1, sd);
|
|
826 }
|
|
827 }
|
|
828 if (process_adults) {
|
|
829 # Calculate adult populations for selected life stage.
|
|
830 if (life_stages_adult=="Total") {
|
|
831 # Mean value for all adults.
|
|
832 adults = apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, mean);
|
|
833 # Standard error for all adults.
|
|
834 adults.std_error = apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, sd) / sqrt(opt$replications);
|
|
835 } else if (life_stages_adult == "Pre-vittelogenic") {
|
|
836 # Mean value for previtellogenic adults.
|
|
837 adults = apply(Previtellogenic.replications, 1, mean);
|
|
838 # Standard error for previtellogenic adults.
|
|
839 adults.std_error = apply(Previtellogenic.replications, 1, sd) / sqrt(opt$replications);
|
|
840 } else if (life_stages_adult == "Vittelogenic") {
|
|
841 # Mean value for vitellogenic adults.
|
|
842 adults = apply(Vitellogenic.replications, 1, mean);
|
|
843 # Standard error for vitellogenic adults.
|
|
844 adults.std_error = apply(Vitellogenic.replications, 1, sd) / sqrt(opt$replications);
|
|
845 } else if (life_stages_adult == "Diapausing") {
|
|
846 # Mean value for vitellogenic adults.
|
|
847 adults = apply(Diapausing.replications, 1, mean);
|
|
848 # Standard error for vitellogenic adults.
|
|
849 adults.std_error = apply(Diapausing.replications, 1, sd) / sqrt(opt$replications);
|
|
850 }
|
|
851 }
|
5
|
852
|
10
|
853 if (plot_generations_separately) {
|
|
854 # Mean value for P which is Parental, or overwintered adults.
|
|
855 P = apply(P.replications, 1, mean);
|
|
856 # Standard error for P.
|
|
857 P.std_error = apply(P.replications, 1, sd) / sqrt(opt$replications);
|
|
858 # Mean value for F1, which is the first field-produced generation.
|
|
859 F1 = apply(F1.replications, 1, mean);
|
|
860 # Standard error for F1.
|
|
861 F1.std_error = apply(F1.replications, 1, sd) / sqrt(opt$replications);
|
|
862 # Mean value for F2, which is the second field-produced generation.
|
|
863 F2 = apply(F2.replications, 1, mean);
|
|
864 # Standard error for F2.
|
|
865 F2.std_error = apply(F2.replications, 1, sd) / sqrt(opt$replications);
|
|
866 if (process_eggs) {
|
|
867 # Mean value for P eggs.
|
|
868 P_eggs = apply(P_eggs.replications, 1, mean);
|
|
869 # Standard error for P_eggs.
|
|
870 P_eggs.std_error = apply(P_eggs.replications, 1, sd) / sqrt(opt$replications);
|
|
871 # Mean value for F1 eggs.
|
|
872 F1_eggs = apply(F1_eggs.replications, 1, mean);
|
|
873 # Standard error for F1 eggs.
|
|
874 F1_eggs.std_error = apply(F1_eggs.replications, 1, sd) / sqrt(opt$replications);
|
|
875 # Mean value for F2 eggs.
|
|
876 F2_eggs = apply(F2_eggs.replications, 1, mean);
|
|
877 # Standard error for F2 eggs.
|
|
878 F2_eggs.std_error = apply(F2_eggs.replications, 1, sd) / sqrt(opt$replications);
|
|
879 }
|
|
880 if (process_nymphs) {
|
|
881 # Mean value for P nymphs.
|
|
882 P_nymphs = apply(P_nymphs.replications, 1, mean);
|
|
883 # Standard error for P_nymphs.
|
|
884 P_nymphs.std_error = apply(P_nymphs.replications, 1, sd) / sqrt(opt$replications);
|
|
885 # Mean value for F1 nymphs.
|
|
886 F1_nymphs = apply(F1_nymphs.replications, 1, mean);
|
|
887 # Standard error for F1 nymphs.
|
|
888 F1_nymphs.std_error = apply(F1_nymphs.replications, 1, sd) / sqrt(opt$replications);
|
|
889 # Mean value for F2 nymphs.
|
|
890 F2_nymphs = apply(F2_nymphs.replications, 1, mean);
|
|
891 # Standard error for F2 eggs.
|
|
892 F2_nymphs.std_error = apply(F2_nymphs.replications, 1, sd) / sqrt(opt$replications);
|
|
893 }
|
|
894 if (process_adults) {
|
|
895 # Mean value for P adults.
|
|
896 P_adults = apply(P_adults.replications, 1, mean);
|
|
897 # Standard error for P_adults.
|
|
898 P_adults.std_error = apply(P_adults.replications, 1, sd) / sqrt(opt$replications);
|
|
899 # Mean value for F1 adults.
|
|
900 F1_adults = apply(F1_adults.replications, 1, mean);
|
|
901 # Standard error for F1 adults.
|
|
902 F1_adults.std_error = apply(F1_adults.replications, 1, sd) / sqrt(opt$replications);
|
|
903 # Mean value for F2 adults.
|
|
904 F2_adults = apply(F2_adults.replications, 1, mean);
|
|
905 # Standard error for F2 adults.
|
|
906 F2_adults.std_error = apply(F2_adults.replications, 1, sd) / sqrt(opt$replications);
|
|
907 }
|
|
908 }
|
6
|
909
|
|
910 # Display the total number of days in the Galaxy history item blurb.
|
8
|
911 cat("Number of days: ", opt$num_days, "\n");
|
5
|
912
|
10
|
913 # Information needed for plots plots.
|
8
|
914 days = c(1:opt$num_days);
|
|
915 start_date = temperature_data_frame$DATE[1];
|
|
916 end_date = temperature_data_frame$DATE[opt$num_days];
|
5
|
917
|
10
|
918 if (plot_generations_separately) {
|
|
919 for (life_stage in life_stages) {}
|
|
920 if (life_stage == "Egg") {
|
|
921 # Start PDF device driver.
|
|
922 dev.new(width=20, height=30);
|
|
923 file_path = paste(output_dir, "egg_pop_by_generation.pdf", sep="/");
|
|
924 pdf(file=file_path, width=20, height=30, bg="white");
|
|
925 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
926 # Egg population size by generation.
|
|
927 maxval = max(F2_eggs) + 100;
|
|
928 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
|
929 opt$replications, life_stage, group=P_eggs, group_std_error=P_eggs.std_error, group2=F1_eggs, group2_std_error=F1_eggs.std_error, group3=F2_eggs,
|
|
930 group3_std_error=F2_eggs.std_error);
|
|
931 # Turn off device driver to flush output.
|
|
932 dev.off();
|
|
933 } else if (life_stage == "Nymph") {
|
|
934 # Start PDF device driver.
|
|
935 dev.new(width=20, height=30);
|
|
936 file_name = paste(tolower(life_stage_nymph), "nymph_pop_by_generation.pdf", sep="_");
|
|
937 file_path = paste(output_dir, file_name, sep="/");
|
|
938 pdf(file=file_path, width=20, height=30, bg="white");
|
|
939 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
940 # Nymph population size by generation.
|
|
941 maxval = max(F2_nymphs) + 100;
|
|
942 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
|
943 opt$replications, life_stage, group=P_nymphs, group_std_error=P_nymphs.std_error, group2=F1_nymphs, group2_std_error=F1_nymphs.std_error,
|
|
944 group3=F2_nymphs, group3_std_error=F2_nymphs.std_error, life_stage_nymph=life_stage_nymph);
|
|
945 # Turn off device driver to flush output.
|
|
946 dev.off();
|
|
947 } else if (life_stage == "Adult") {
|
|
948 # Start PDF device driver.
|
|
949 dev.new(width=20, height=30);
|
|
950 file_name = paste(tolower(life_stages_adult), "adult_pop_by_generation.pdf", sep="_");
|
|
951 file_path = paste(output_dir, file_name, sep="/");
|
|
952 pdf(file=file_path, width=20, height=30, bg="white");
|
|
953 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
954 # Adult population size by generation.
|
|
955 maxval = max(F2_adults) + 100;
|
|
956 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
|
957 opt$replications, life_stage, group=P_adults, group_std_error=P_adults.std_error, group2=F1_adults, group2_std_error=F1_adults.std_error,
|
|
958 group3=F2_adults, group3_std_error=F2_adults.std_error, life_stages_adult=life_stages_adult);
|
|
959 # Turn off device driver to flush output.
|
|
960 dev.off();
|
|
961 } else if (life_stage == "Total") {
|
|
962 # Start PDF device driver.
|
|
963 dev.new(width=20, height=30);
|
|
964 file_path = paste(output_dir, "total_pop_by_generation.pdf", sep="/");
|
|
965 pdf(file=file_path, width=20, height=30, bg="white");
|
|
966 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
967 # Total population size by generation.
|
|
968 maxval = max(F2);
|
|
969 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
|
970 opt$replications, life_stage, group=P, group_std_error=P.std_error, group2=F1, group2_std_error=F1.std_error, group3=F2, group3_std_error=F2.std_error);
|
|
971 # Turn off device driver to flush output.
|
|
972 dev.off();
|
|
973 }
|
|
974 } else {
|
|
975 for (life_stage in life_stages) {
|
|
976 if (life_stage == "Egg") {
|
|
977 # Start PDF device driver.
|
|
978 dev.new(width=20, height=30);
|
|
979 file_path = paste(output_dir, "egg_pop_size.pdf", sep="/");
|
|
980 pdf(file=file_path, width=20, height=30, bg="white");
|
|
981 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
982 # Egg population size.
|
|
983 maxval = max(eggs+eggs.std_error);
|
|
984 render_chart(date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
|
985 opt$replications, life_stage, group=eggs, group_std_error=eggs.std_error);
|
|
986 # Turn off device driver to flush output.
|
|
987 dev.off();
|
|
988 } else if (life_stage == "Nymph") {
|
|
989 # Start PDF device driver.
|
|
990 dev.new(width=20, height=30);
|
|
991 file_name = paste(tolower(life_stage_nymph), "nymph_pop_size.pdf", sep="_");
|
|
992 file_path = paste(output_dir, file_name, sep="/");
|
|
993 pdf(file=file_path, width=20, height=30, bg="white");
|
|
994 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
995 # Nymph population size.
|
|
996 maxval = max(nymphs+nymphs.std_error);
|
|
997 render_chart(date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
|
998 opt$replications, life_stage, group=nymphs, group_std_error=nymphs.std_error, life_stage_nymph=life_stage_nymph);
|
|
999 # Turn off device driver to flush output.
|
|
1000 dev.off();
|
|
1001 } else if (life_stage == "Adult") {
|
|
1002 # Start PDF device driver.
|
|
1003 dev.new(width=20, height=30);
|
|
1004 file_name = paste(tolower(life_stages_adult), "adult_pop_size.pdf", sep="_");
|
|
1005 file_path = paste(output_dir, file_name, sep="/");
|
|
1006 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1007 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1008 # Adult population size.
|
|
1009 maxval = max(adults+adults.std_error);
|
|
1010 render_chart(date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
|
1011 opt$replications, life_stage, group=adults, group_std_error=adults.std_error, life_stages_adult=life_stages_adult);
|
|
1012 # Turn off device driver to flush output.
|
|
1013 dev.off();
|
|
1014 } else if (life_stage == "Total") {
|
|
1015 # Start PDF device driver.
|
|
1016 dev.new(width=20, height=30);
|
|
1017 file_path = paste(output_dir, "total_pop_size.pdf", sep="/");
|
|
1018 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1019 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1020 # Total population size.
|
|
1021 maxval = max(eggs+eggs.std_error, nymphs+nymphs.std_error, adults+adults.std_error);
|
|
1022 render_chart(date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
|
|
1023 opt$replications, life_stage, group=adults, group_std_error=adults.std_error, group2=nymphs, group2_std_error=nymphs.std_error, group3=eggs,
|
|
1024 group3_std_error=eggs.std_error);
|
|
1025 # Turn off device driver to flush output.
|
|
1026 dev.off();
|
|
1027 }
|
|
1028 }
|
|
1029 }
|