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