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