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