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