comparison insect_phenology_model.R @ 112:bcb12b7e8563 draft

Uploaded
author greg
date Tue, 29 May 2018 09:00:25 -0400
parents 37ac68b6ff10
children 9c998fd06628
comparison
equal deleted inserted replaced
111:37ac68b6ff10 112:bcb12b7e8563
4 4
5 option_list <- list( 5 option_list <- list(
6 make_option(c("--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"), 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)"), 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"), 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"), 9 make_option(c("--input_norm"), action="store", dest="input_norm", help="30 year normals temperature data for selected station"),
10 make_option(c("--input_ytd"), action="store", dest="input_ytd", default=NULL, help="Year-to-date temperature data for selected location"),
10 make_option(c("--insect"), action="store", dest="insect", help="Insect name"), 11 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"), 12 make_option(c("--insects_per_replication"), action="store", dest="insects_per_replication", type="integer", help="Number of insects with which to start each replication"),
12 make_option(c("--location"), action="store", dest="location", help="Selected location"), 13 make_option(c("--life_stages"), action="store", dest="life_stages", help="Selected life stages for plotting"),
14 make_option(c("--life_stages_adult"), action="store", dest="life_stages_adult", default=NULL, help="Adult life stages for plotting"),
15 make_option(c("--life_stages_nymph"), action="store", dest="life_stages_nymph", default=NULL, help="Nymph life stages for plotting"),
16 make_option(c("--location"), action="store", dest="location", default=NULL, help="Selected location"),
13 make_option(c("--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"), 17 make_option(c("--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
14 make_option(c("--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"), 18 make_option(c("--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
19 make_option(c("--num_days_ytd"), action="store", dest="num_days_ytd", default=NULL, type="integer", help="Total number of days in the year-to-date temperature dataset"),
15 make_option(c("--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"), 20 make_option(c("--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"),
16 make_option(c("--old_nymph_accumulation"), action="store", dest="old_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"), 21 make_option(c("--old_nymph_accumulation"), action="store", dest="old_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"),
17 make_option(c("--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"),
18 make_option(c("--output"), action="store", dest="output", help="Output dataset"),
19 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"), 22 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
20 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"), 23 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
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"),
21 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"), 26 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
22 make_option(c("--std_error_plot"), action="store", dest="std_error_plot", help="Plot Standard error"),
23 make_option(c("--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)") 27 make_option(c("--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)")
24 ) 28 )
25 29
26 parser <- OptionParser(usage="%prog [options] file", option_list=option_list); 30 parser <- OptionParser(usage="%prog [options] file", option_list=option_list);
27 args <- parse_args(parser, positional_arguments=TRUE); 31 args <- parse_args(parser, positional_arguments=TRUE);
28 opt <- args$options; 32 opt <- args$options;
29 33
30 add_daylight_length = function(temperature_data_frame, num_columns, num_rows) { 34 add_daylight_length = function(temperature_data_frame, num_rows) {
31 # Return a vector of daylight length (photoperido profile) for 35 # Return a vector of daylight length (photoperido profile) for
32 # the number of days specified in the input temperature data 36 # the number of days specified in the input_ytd temperature data
33 # (from Forsythe 1995). 37 # (from Forsythe 1995).
34 p = 0.8333; 38 p = 0.8333;
35 latitude = temperature_data_frame$LATITUDE[1]; 39 latitude = temperature_data_frame$LATITUDE[1];
36 daylight_length_vector = NULL; 40 daylight_length_vector = NULL;
37 for (i in 1:num_rows) { 41 for (i in 1:num_rows) {
43 # Compute the length of daylight for the day of the year. 47 # Compute the length of daylight for the day of the year.
44 darkness_length = 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi))); 48 darkness_length = 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)));
45 daylight_length_vector[i] = 24 - darkness_length; 49 daylight_length_vector[i] = 24 - darkness_length;
46 } 50 }
47 # Append daylight_length_vector as a new column to temperature_data_frame. 51 # Append daylight_length_vector as a new column to temperature_data_frame.
48 temperature_data_frame[, num_columns+1] = daylight_length_vector; 52 temperature_data_frame = append_vector(temperature_data_frame, daylight_length_vector, "DAYLEN");
49 return(temperature_data_frame); 53 return(temperature_data_frame);
50 } 54 }
51 55
52 dev.egg = function(temperature) { 56 append_vector = function(data_frame, vec, new_column_name) {
53 dev.rate = -0.9843 * temperature + 33.438; 57 num_columns = dim(data_frame)[2];
54 return(dev.rate); 58 current_column_names = colnames(data_frame);
55 } 59 # Append vector vec as a new column to data_frame.
56 60 data_frame[,num_columns+1] = vec;
57 dev.emerg = function(temperature) { 61 # Reset the column names with the additional column for later access.
58 emerg.rate = -0.5332 * temperature + 24.147; 62 colnames(data_frame) = append(current_column_names, new_column_name);
59 return(emerg.rate); 63 return(data_frame);
60 } 64 }
61 65
62 dev.old = function(temperature) { 66 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) {
63 n34 = -0.6119 * temperature + 17.602; 67 if (!is.null(life_stage_nymph)) {
64 n45 = -0.4408 * temperature + 19.036; 68 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph);
65 dev.rate = mean(n34 + n45); 69 file_name = paste(lsi, tolower(life_stage_nymph), base_name, sep="_");
66 return(dev.rate); 70 } else if (!is.null(life_stage_adult)) {
67 } 71 lsi = get_life_stage_index(life_stage, life_stage_adult=life_stage_adult);
68 72 file_name = paste(lsi, tolower(life_stage_adult), base_name, sep="_");
69 dev.young = function(temperature) { 73 } else {
70 n12 = -0.3728 * temperature + 14.68; 74 lsi = get_life_stage_index(life_stage);
71 n23 = -0.6119 * temperature + 25.249; 75 file_name = paste(lsi, base_name, sep="_");
72 dev.rate = mean(n12 + n23); 76 }
73 return(dev.rate); 77 file_path = paste("output_plots_dir", file_name, sep="/");
74 } 78 return(file_path);
75 79 }
76 80
77 get_date_labels = function(temperature_data_frame, num_rows) { 81 get_life_stage_index = function(life_stage, life_stage_nymph=NULL, life_stage_adult=NULL) {
78 # Keep track of the years to see if spanning years. 82 # Name collection elements so that they
79 month_labels = list(); 83 # are displayed in logical order.
80 current_month_label = NULL; 84 if (life_stage=="Egg") {
81 for (i in 1:num_rows) { 85 lsi = "01";
82 # Get the year and month from the date which 86 } else if (life_stage=="Nymph") {
83 # has the format YYYY-MM-DD. 87 if (life_stage_nymph=="Young") {
84 date = format(temperature_data_frame$DATE[i]); 88 lsi = "02";
85 items = strsplit(date, "-")[[1]]; 89 } else if (life_stage_nymph=="Old") {
86 month = items[2]; 90 lsi = "03";
87 month_label = month.abb[as.integer(month)]; 91 } else if (life_stage_nymph=="Total") {
88 if (!identical(current_month_label, month_label)) { 92 lsi="04";
89 month_labels[length(month_labels)+1] = month_label; 93 }
90 current_month_label = month_label; 94 } else if (life_stage=="Adult") {
91 } 95 if (life_stage_adult=="Pre-vittelogenic") {
92 } 96 lsi = "05";
93 return(c(unlist(month_labels))); 97 } else if (life_stage_adult=="Vittelogenic") {
98 lsi = "06";
99 } else if (life_stage_adult=="Diapausing") {
100 lsi = "07";
101 } else if (life_stage_adult=="Total") {
102 lsi = "08";
103 }
104 } else if (life_stage=="Total") {
105 lsi = "09";
106 }
107 return(lsi);
108 }
109
110 get_mean_and_std_error = function(p_replications, f1_replications, f2_replications) {
111 # P mean.
112 p_m = apply(p_replications, 1, mean);
113 # P standard error.
114 p_se = apply(p_replications, 1, sd) / sqrt(opt$replications);
115 # F1 mean.
116 f1_m = apply(f1_replications, 1, mean);
117 # F1 standard error.
118 f1_se = apply(f1_replications, 1, sd) / sqrt(opt$replications);
119 # F2 mean.
120 f2_m = apply(f2_replications, 1, mean);
121 # F2 standard error.
122 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications);
123 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))
124 }
125
126 get_next_normals_row = function(norm_data_frame, year, is_leap_year, index) {
127 # Return the next 30 year normals row formatted
128 # appropriately for the year-to-date data.
129 latitude = norm_data_frame[index,"LATITUDE"][1];
130 longitude = norm_data_frame[index,"LONGITUDE"][1];
131 # Format the date.
132 mmdd = norm_data_frame[index,"MMDD"][1];
133 date_str = paste(year, mmdd, sep="-");
134 doy = norm_data_frame[index,"DOY"][1];
135 if (!is_leap_year) {
136 # Since all normals data includes Feb 29, we have to
137 # subtract 1 from DOY if we're not in a leap year since
138 # we removed the Feb 29 row from the data frame above.
139 doy = as.integer(doy) - 1;
140 }
141 tmin = norm_data_frame[index,"TMIN"][1];
142 tmax = norm_data_frame[index,"TMAX"][1];
143 return(list(latitude, longitude, date_str, doy, tmin, tmax));
94 } 144 }
95 145
96 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) { 146 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) {
97 # Base development threshold for Brown Marmorated Stink Bug 147 # Base development threshold for Brown Marmorated Stink Bug
98 # insect phenology model. 148 # insect phenology model.
162 averages = sum(dh) / 24; 212 averages = sum(dh) / 24;
163 } 213 }
164 return(c(curr_mean_temp, averages)) 214 return(c(curr_mean_temp, averages))
165 } 215 }
166 216
217 get_tick_index = function(index, last_tick, ticks, month_labels) {
218 # The R code tries hard not to draw overlapping tick labels, and so
219 # will omit labels where they would abut or overlap previously drawn
220 # labels. This can result in, for example, every other tick being
221 # labelled. We'll keep track of the last tick to make sure all of
222 # the month labels are displayed, and missing ticks are restricted
223 # to Sundays which have no labels anyway.
224 if (last_tick==0) {
225 return(length(ticks)+1);
226 }
227 last_saved_tick = ticks[[length(ticks)]];
228 if (index-last_saved_tick<3) {
229 last_saved_month = month_labels[[length(month_labels)]];
230 if (last_saved_month=="") {
231 # We're safe overwriting a tick
232 # with no label (i.e., a Sunday tick).
233 return(length(ticks));
234 } else {
235 # Don't eliminate a Month label.
236 return(NULL);
237 }
238 }
239 return(length(ticks)+1);
240 }
241
242 get_total_days = function(is_leap_year) {
243 # Get the total number of days in the current year.
244 if (is_leap_year) {
245 return(366);
246 } else {
247 return(365);
248 }
249 }
250
251 get_x_axis_ticks_and_labels = function(temperature_data_frame, num_rows, start_doy_ytd, end_doy_ytd) {
252 # Keep track of the years to see if spanning years.
253 month_labels = list();
254 ticks = list();
255 current_month_label = NULL;
256 last_tick = 0;
257 for (i in 1:num_rows) {
258 if (start_doy_ytd > 1 & i==start_doy_ytd-1) {
259 # Add a tick for the end of the 30 year normnals data
260 # that was prepended to the year-to-date data.
261 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
262 ticks[tick_index] = i;
263 month_labels[tick_index] = "End prepended 30 year normals";
264 last_tick = i;
265 } else if (end_doy_ytd > 0 & i==end_doy_ytd+1) {
266 # Add a tick for the start of the 30 year normnals data
267 # that was appended to the year-to-date data.
268 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
269 ticks[tick_index] = i;
270 month_labels[tick_index] = "Start appended 30 year normals";
271 last_tick = i;
272 } else if (i==num_rows) {
273 # Add a tick for the last day of the year.
274 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
275 ticks[tick_index] = i;
276 month_labels[tick_index] = "";
277 last_tick = i;
278 } else {
279 # Get the year and month from the date which
280 # has the format YYYY-MM-DD.
281 date = format(temperature_data_frame$DATE[i]);
282 # Get the month label.
283 items = strsplit(date, "-")[[1]];
284 month = items[2];
285 month_label = month.abb[as.integer(month)];
286 if (!identical(current_month_label, month_label)) {
287 # Add an x-axis tick for the month.
288 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
289 ticks[tick_index] = i;
290 month_labels[tick_index] = month_label;
291 current_month_label = month_label;
292 last_tick = i;
293 }
294 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
295 if (!is.null(tick_index)) {
296 # Get the day.
297 day = weekdays(as.Date(date));
298 if (day=="Sunday") {
299 # Add an x-axis tick if we're on a Sunday.
300 ticks[tick_index] = i;
301 # Add a blank month label so it is not displayed.
302 month_labels[tick_index] = "";
303 last_tick = i;
304 }
305 }
306 }
307 }
308 return(list(ticks, month_labels));
309 }
310
311 is_leap_year = function(date_str) {
312 # Extract the year from the date_str.
313 date = format(date_str);
314 items = strsplit(date, "-")[[1]];
315 year = as.integer(items[1]);
316 if (((year %% 4 == 0) & (year %% 100 != 0)) | (year %% 400 == 0)) {
317 return(TRUE);
318 } else {
319 return(FALSE);
320 }
321 }
322
167 mortality.adult = function(temperature) { 323 mortality.adult = function(temperature) {
168 if (temperature < 12.7) { 324 if (temperature < 12.7) {
169 mortality.probability = 0.002; 325 mortality.probability = 0.002;
170 } 326 }
171 else { 327 else {
195 mortality.probability = temperature * 0.0008 + 0.03; 351 mortality.probability = temperature * 0.0008 + 0.03;
196 } 352 }
197 return(mortality.probability); 353 return(mortality.probability);
198 } 354 }
199 355
200 parse_input_data = function(input_file, num_rows) { 356 parse_input_data = function(input_ytd, input_norm, num_days_ytd, location) {
201 # Read in the input temperature datafile into a data frame. 357 if (is.null(input_ytd)) {
202 temperature_data_frame = read.csv(file=input_file, header=T, strip.white=TRUE, sep=","); 358 # We're analysing only the 30 year normals data, so create an empty
203 num_columns = dim(temperature_data_frame)[2]; 359 # data frame for containing temperature data after it is converted
204 if (num_columns == 6) { 360 # from the 30 year normals format to the year-to-date format.
205 # The input data has the following 6 columns: 361 temperature_data_frame = data.frame(matrix(ncol=6, nrow=0));
362 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
363 # Base all dates on the current date since 30 year
364 # normals data does not include any dates.
365 year = format(Sys.Date(), "%Y");
366 start_date = paste(year, "01", "01", sep="-");
367 end_date = paste(year, "12", "31", sep="-");
368 # Set invalid start and end DOY.
369 start_doy_ytd = 0;
370 end_doy_ytd = 0;
371 } else {
372 # Read the input_ytd temperature datafile into a data frame.
373 # The input_ytd data has the following 6 columns:
206 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX 374 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
207 # Set the column names for access when adding daylight length.. 375 temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
208 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); 376 # Set the temperature_data_frame column names for access.
209 # Add a column containing the daylight length for each day. 377 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
210 temperature_data_frame = add_daylight_length(temperature_data_frame, num_columns, num_rows); 378 # Get the start date.
211 # Reset the column names with the additional column for later access. 379 start_date = temperature_data_frame$DATE[1];
212 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX", "DAYLEN"); 380 end_date = temperature_data_frame$DATE[num_days_ytd];
213 } 381 # Extract the year from the start date.
214 return(temperature_data_frame); 382 date_str = format(start_date);
215 } 383 date_str_items = strsplit(date_str, "-")[[1]];
216 384 year = date_str_items[1];
217 385 # Save the first DOY to later check if start_date is Jan 1.
218 render_chart = function(chart_type, insect, location, latitude, start_date, end_date, days, maxval, plot_std_error, 386 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]);
219 group1, group2, group3, group1_std_error, group2_std_error, group3_std_error, date_labels) { 387 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_days_ytd]);
220 if (chart_type == "pop_size_by_life_stage") { 388 }
221 title = paste(insect, ": Total pop. by life stage :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" "); 389 # See if we're in a leap year.
222 legend_text = c("Egg", "Nymph", "Adult"); 390 is_leap_year = is_leap_year(start_date);
223 columns = c(4, 2, 1); 391 # Get the number of days in the year.
224 } else if (chart_type == "pop_size_by_generation") { 392 total_days = get_total_days(is_leap_year);
225 title = paste(insect, ": Total pop. by generation :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" "); 393 # Read the input_norm temperature datafile into a data frame.
394 # The input_norm data has the following 10 columns:
395 # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX
396 norm_data_frame = read.csv(file=input_norm, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
397 # Set the norm_data_frame column names for access.
398 colnames(norm_data_frame) = c("STATIONID", "LATITUDE","LONGITUDE", "ELEV_M", "NAME", "ST", "MMDD", "DOY", "TMIN", "TMAX");
399 # All normals data includes Feb 29 which is row 60 in
400 # the data, so delete that row if we're not in a leap year.
401 if (!is_leap_year) {
402 norm_data_frame = norm_data_frame[-c(60),];
403 }
404 # Set the location to be the station name if the user elected no to enter it.
405 if (is.null(location) | length(location)==0) {
406 location = norm_data_frame$NAME[1];
407 }
408 if (is.null(input_ytd)) {
409 # Convert the 30 year normals data to the year-to-date format.
410 for (i in 1:total_days) {
411 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
412 }
413 } else {
414 # Merge the year-to-date data with the 30 year normals data.
415 if (start_doy_ytd > 1) {
416 # The year-to-date data starts after Jan 1, so create a
417 # temporary data frame to contain the 30 year normals data
418 # from Jan 1 to the date immediately prior to start_date.
419 tmp_data_frame = temperature_data_frame[FALSE,];
420 for (i in 1:start_doy_ytd-1) {
421 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
422 }
423 # Next merge the temporary data frame with the year-to-date data frame.
424 temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame);
425 }
426 # Define the next row for the year-to-date data from the 30 year normals data.
427 first_normals_append_row = end_doy_ytd + 1;
428 # Append the 30 year normals data to the year-to-date data.
429 for (i in first_normals_append_row:total_days) {
430 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
431 }
432 }
433 # Add a column containing the daylight length for each day.
434 temperature_data_frame = add_daylight_length(temperature_data_frame, total_days);
435 return(list(temperature_data_frame, start_date, end_date, start_doy_ytd, end_doy_ytd, is_leap_year, total_days, location));
436 }
437
438 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,
439 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,
440 life_stages_adult=NULL, life_stages_nymph=NULL) {
441 if (chart_type=="pop_size_by_life_stage") {
442 if (life_stage=="Total") {
443 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
444 legend_text = c("Egg", "Nymph", "Adult");
445 columns = c(4, 2, 1);
446 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
447 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
448 lines(days, group2, lwd=2, lty=1, col=2);
449 lines(days, group3, lwd=2, lty=1, col=4);
450 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
451 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
452 if (plot_std_error=="yes") {
453 # Standard error for group.
454 lines(days, group+group_std_error, lty=2);
455 lines(days, group-group_std_error, lty=2);
456 # Standard error for group2.
457 lines(days, group2+group2_std_error, col=2, lty=2);
458 lines(days, group2-group2_std_error, col=2, lty=2);
459 # Standard error for group3.
460 lines(days, group3+group3_std_error, col=4, lty=2);
461 lines(days, group3-group3_std_error, col=4, lty=2);
462 }
463 } else {
464 if (life_stage=="Egg") {
465 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
466 legend_text = c(life_stage);
467 columns = c(4);
468 } else if (life_stage=="Nymph") {
469 stage = paste(life_stages_nymph, "Nymph Pop :", sep=" ");
470 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
471 legend_text = c(paste(life_stages_nymph, life_stage, sep=" "));
472 columns = c(2);
473 } else if (life_stage=="Adult") {
474 stage = paste(life_stages_adult, "Adult Pop", sep=" ");
475 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
476 legend_text = c(paste(life_stages_adult, life_stage, sep=" "));
477 columns = c(1);
478 }
479 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
480 legend("topleft", legend_text, lty=c(1), col="black", cex=3);
481 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
482 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
483 if (plot_std_error=="yes") {
484 # Standard error for group.
485 lines(days, group+group_std_error, lty=2);
486 lines(days, group-group_std_error, lty=2);
487 }
488 }
489 } else if (chart_type=="pop_size_by_generation") {
490 if (life_stage=="Total") {
491 title_str = ": Total Pop by Gen :";
492 } else if (life_stage=="Egg") {
493 title_str = ": Egg Pop by Gen :";
494 } else if (life_stage=="Nymph") {
495 title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" ");
496 } else if (life_stage=="Adult") {
497 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" ");
498 }
499 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
226 legend_text = c("P", "F1", "F2"); 500 legend_text = c("P", "F1", "F2");
227 columns = c(1, 2, 4); 501 columns = c(1, 2, 4);
228 } else if (chart_type == "adult_pop_size_by_generation") { 502 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
229 title = paste(insect, ": Adult pop. by generation :", location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" "); 503 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
230 legend_text = c("P", "F1", "F2"); 504 lines(days, group2, lwd=2, lty=1, col=2);
231 columns = c(1, 2, 4); 505 lines(days, group3, lwd=2, lty=1, col=4);
232 } 506 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
233 plot(days, group1, 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); 507 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
234 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3); 508 if (plot_std_error=="yes") {
235 lines(days, group2, lwd=2, lty=1, col=2); 509 # Standard error for group.
236 lines(days, group3, lwd=2, lty=1, col=4); 510 lines(days, group+group_std_error, lty=2);
237 axis(1, at=c(1:length(date_labels)) * 30 - 15, cex.axis=3, labels=date_labels); 511 lines(days, group-group_std_error, lty=2);
238 axis(2, cex.axis=3); 512 # Standard error for group2.
239 if (plot_std_error==1) { 513 lines(days, group2+group2_std_error, col=2, lty=2);
240 # Standard error for group1. 514 lines(days, group2-group2_std_error, col=2, lty=2);
241 lines(days, group1+group1_std_error, lty=2); 515 # Standard error for group3.
242 lines(days, group1-group1_std_error, lty=2); 516 lines(days, group3+group3_std_error, col=4, lty=2);
243 # Standard error for group2. 517 lines(days, group3-group3_std_error, col=4, lty=2);
244 lines(days, group2+group2_std_error, col=2, lty=2); 518 }
245 lines(days, group2-group2_std_error, col=2, lty=2); 519 }
246 # Standard error for group3. 520 }
247 lines(days, group3+group3_std_error, col=4, lty=2); 521
248 lines(days, group3-group3_std_error, col=4, lty=2); 522 # Determine if we're plotting generations separately.
249 } 523 if (opt$plot_generations_separately=="yes") {
250 } 524 plot_generations_separately = TRUE;
251 525 } else {
252 temperature_data_frame = parse_input_data(opt$input, opt$num_days); 526 plot_generations_separately = FALSE;
253 # All latitude values are the same, so get the value from the first row. 527 }
528 # Display the total number of days in the Galaxy history item blurb.
529 cat("Year-to-date number of days: ", opt$num_days_ytd, "\n");
530
531 # Parse the inputs.
532 data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$num_days_ytd, opt$location);
533 temperature_data_frame = data_list[[1]];
534 # Information needed for plots.
535 start_date = data_list[[2]];
536 end_date = data_list[[3]];
537 start_doy_ytd = data_list[[4]];
538 end_doy_ytd = data_list[[5]];
539 is_leap_year = data_list[[6]];
540 total_days = data_list[[7]];
541 total_days_vector = c(1:total_days);
542 location = data_list[[8]];
543
544 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately.
545 if (plot_generations_separately) {
546 temperature_data_frame_P = data.frame(temperature_data_frame);
547 temperature_data_frame_F1 = data.frame(temperature_data_frame);
548 temperature_data_frame_F2 = data.frame(temperature_data_frame);
549 }
550
551 # Get the ticks date labels for plots.
552 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, total_days, start_doy_ytd, end_doy_ytd);
553 ticks = c(unlist(ticks_and_labels[1]));
554 date_labels = c(unlist(ticks_and_labels[2]));
555 # All latitude values are the same, so get the value for plots from the first row.
254 latitude = temperature_data_frame$LATITUDE[1]; 556 latitude = temperature_data_frame$LATITUDE[1];
255 num_columns = dim(temperature_data_frame)[2]; 557
256 date_labels = get_date_labels(temperature_data_frame, opt$num_days); 558 # Determine the specified life stages for processing.
257 559 # Split life_stages into a list of strings for plots.
560 life_stages_str = as.character(opt$life_stages);
561 life_stages = strsplit(life_stages_str, ",")[[1]];
562
563 # Determine the data we need to generate for plotting.
564 process_eggs = FALSE;
565 process_nymphs = FALSE;
566 process_young_nymphs = FALSE;
567 process_old_nymphs = FALSE;
568 process_total_nymphs = FALSE;
569 process_adults = FALSE;
570 process_previttelogenic_adults = FALSE;
571 process_vittelogenic_adults = FALSE;
572 process_diapausing_adults = FALSE;
573 process_total_adults = FALSE;
574 for (life_stage in life_stages) {
575 if (life_stage=="Total") {
576 process_eggs = TRUE;
577 process_nymphs = TRUE;
578 process_adults = TRUE;
579 } else if (life_stage=="Egg") {
580 process_eggs = TRUE;
581 } else if (life_stage=="Nymph") {
582 process_nymphs = TRUE;
583 } else if (life_stage=="Adult") {
584 process_adults = TRUE;
585 }
586 }
587 if (process_nymphs) {
588 # Split life_stages_nymph into a list of strings for plots.
589 life_stages_nymph_str = as.character(opt$life_stages_nymph);
590 life_stages_nymph = strsplit(life_stages_nymph_str, ",")[[1]];
591 for (life_stage_nymph in life_stages_nymph) {
592 if (life_stage_nymph=="Young") {
593 process_young_nymphs = TRUE;
594 } else if (life_stage_nymph=="Old") {
595 process_old_nymphs = TRUE;
596 } else if (life_stage_nymph=="Total") {
597 process_total_nymphs = TRUE;
598 }
599 }
600 }
601 if (process_adults) {
602 # Split life_stages_adult into a list of strings for plots.
603 life_stages_adult_str = as.character(opt$life_stages_adult);
604 life_stages_adult = strsplit(life_stages_adult_str, ",")[[1]];
605 for (life_stage_adult in life_stages_adult) {
606 if (life_stage_adult=="Pre-vittelogenic") {
607 process_previttelogenic_adults = TRUE;
608 } else if (life_stage_adult=="Vittelogenic") {
609 process_vittelogenic_adults = TRUE;
610 } else if (life_stage_adult=="Diapausing") {
611 process_diapausing_adults = TRUE;
612 } else if (life_stage_adult=="Total") {
613 process_total_adults = TRUE;
614 }
615 }
616 }
258 # Initialize matrices. 617 # Initialize matrices.
259 Eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 618 if (process_eggs) {
260 YoungNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 619 Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
261 OldNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 620 }
262 Previtellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 621 if (process_young_nymphs | process_total_nymphs) {
263 Vitellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 622 YoungNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
264 Diapausing.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 623 }
265 624 if (process_old_nymphs | process_total_nymphs) {
266 newborn.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 625 OldNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
267 adult.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 626 }
268 death.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 627 if (process_previttelogenic_adults | process_total_adults) {
269 628 Previttelogenic.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
270 P.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 629 }
271 P_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 630 if (process_vittelogenic_adults | process_total_adults) {
272 F1.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 631 Vittelogenic.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
273 F1_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 632 }
274 F2.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 633 if (process_diapausing_adults | process_total_adults) {
275 F2_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 634 Diapausing.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
276 635 }
277 population.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); 636 newborn.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
637 adult.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
638 death.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
639 if (plot_generations_separately) {
640 # P is Parental, or overwintered adults.
641 P.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
642 # F1 is the first field-produced generation.
643 F1.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
644 # F2 is the second field-produced generation.
645 F2.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
646 if (process_eggs) {
647 P_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
648 F1_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
649 F2_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
650 }
651 if (process_young_nymphs) {
652 P_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
653 F1_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
654 F2_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
655 }
656 if (process_old_nymphs) {
657 P_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
658 F1_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
659 F2_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
660 }
661 if (process_total_nymphs) {
662 P_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
663 F1_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
664 F2_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
665 }
666 if (process_previttelogenic_adults) {
667 P_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
668 F1_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
669 F2_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
670 }
671 if (process_vittelogenic_adults) {
672 P_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
673 F1_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
674 F2_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
675 }
676 if (process_diapausing_adults) {
677 P_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
678 F1_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
679 F2_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
680 }
681 if (process_total_adults) {
682 P_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
683 F1_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
684 F2_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
685 }
686 }
687 # Total population.
688 population.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
278 689
279 # Process replications. 690 # Process replications.
280 for (N.replications in 1:opt$replications) { 691 for (current_replication in 1:opt$replications) {
281 # Start with the user-defined number of insects per replication. 692 # Start with the user-defined number of insects per replication.
282 num_insects = opt$insects_per_replication; 693 num_insects = opt$insects_per_replication;
283 # Generation, Stage, degree-days, T, Diapause. 694 # Generation, Stage, degree-days, T, Diapause.
284 vector.ini = c(0, 3, 0, 0, 0); 695 vector.ini = c(0, 3, 0, 0, 0);
285 # Overwintering, previttelogenic, degree-days=0, T=0, no-diapause. 696 # Replicate to create a matrix where the columns are
697 # Generation, Stage, degree-days, T, Diapause and the
698 # rows are the initial number of insects per replication.
286 vector.matrix = rep(vector.ini, num_insects); 699 vector.matrix = rep(vector.ini, num_insects);
287 # Complete matrix for the population. 700 # Complete transposed matrix for the population, so now
701 # the rows are Generation, Stage, degree-days, T, Diapause
288 vector.matrix = base::t(matrix(vector.matrix, nrow=5)); 702 vector.matrix = base::t(matrix(vector.matrix, nrow=5));
289 # Time series of population size. 703 # Time series of population size.
290 Eggs = rep(0, opt$num_days); 704 if (process_eggs) {
291 YoungNymphs = rep(0, opt$num_days); 705 Eggs = rep(0, total_days);
292 OldNymphs = rep(0, opt$num_days); 706 }
293 Previtellogenic = rep(0, opt$num_days); 707 if (process_young_nymphs | process_total_nymphs) {
294 Vitellogenic = rep(0, opt$num_days); 708 YoungNymphs = rep(0, total_days);
295 Diapausing = rep(0, opt$num_days); 709 }
296 710 if (process_old_nymphs | process_total_nymphs) {
297 N.newborn = rep(0, opt$num_days); 711 OldNymphs = rep(0, total_days);
298 N.adult = rep(0, opt$num_days); 712 }
299 N.death = rep(0, opt$num_days); 713 if (process_previttelogenic_adults | process_total_adults) {
300 714 Previttelogenic = rep(0, total_days);
301 overwintering_adult.population = rep(0, opt$num_days); 715 }
302 first_generation.population = rep(0, opt$num_days); 716 if (process_vittelogenic_adults | process_total_adults) {
303 second_generation.population = rep(0, opt$num_days); 717 Vittelogenic = rep(0, total_days);
304 718 }
305 P.adult = rep(0, opt$num_days); 719 if (process_diapausing_adults | process_total_adults) {
306 F1.adult = rep(0, opt$num_days); 720 Diapausing = rep(0, total_days);
307 F2.adult = rep(0, opt$num_days); 721 }
308 722 N.newborn = rep(0, total_days);
723 N.adult = rep(0, total_days);
724 N.death = rep(0, total_days);
725 overwintering_adult.population = rep(0, total_days);
726 first_generation.population = rep(0, total_days);
727 second_generation.population = rep(0, total_days);
728 if (plot_generations_separately) {
729 # P is Parental, or overwintered adults.
730 # F1 is the first field-produced generation.
731 # F2 is the second field-produced generation.
732 if (process_eggs) {
733 P.egg = rep(0, total_days);
734 F1.egg = rep(0, total_days);
735 F2.egg = rep(0, total_days);
736 }
737 if (process_young_nymphs) {
738 P.young_nymph = rep(0, total_days);
739 F1.young_nymph = rep(0, total_days);
740 F2.young_nymph = rep(0, total_days);
741 }
742 if (process_old_nymphs) {
743 P.old_nymph = rep(0, total_days);
744 F1.old_nymph = rep(0, total_days);
745 F2.old_nymph = rep(0, total_days);
746 }
747 if (process_total_nymphs) {
748 P.total_nymph = rep(0, total_days);
749 F1.total_nymph = rep(0, total_days);
750 F2.total_nymph = rep(0, total_days);
751 }
752 if (process_previttelogenic_adults) {
753 P.previttelogenic_adult = rep(0, total_days);
754 F1.previttelogenic_adult = rep(0, total_days);
755 F2.previttelogenic_adult = rep(0, total_days);
756 }
757 if (process_vittelogenic_adults) {
758 P.vittelogenic_adult = rep(0, total_days);
759 F1.vittelogenic_adult = rep(0, total_days);
760 F2.vittelogenic_adult = rep(0, total_days);
761 }
762 if (process_diapausing_adults) {
763 P.diapausing_adult = rep(0, total_days);
764 F1.diapausing_adult = rep(0, total_days);
765 F2.diapausing_adult = rep(0, total_days);
766 }
767 if (process_total_adults) {
768 P.total_adult = rep(0, total_days);
769 F1.total_adult = rep(0, total_days);
770 F2.total_adult = rep(0, total_days);
771 }
772 }
309 total.population = NULL; 773 total.population = NULL;
310 774 averages.day = rep(0, total_days);
311 averages.day = rep(0, opt$num_days); 775 # All the days included in the input_ytd temperature dataset.
312 # All the days included in the input temperature dataset. 776 for (row in 1:total_days) {
313 for (row in 1:opt$num_days) {
314 # Get the integer day of the year for the current row. 777 # Get the integer day of the year for the current row.
315 doy = temperature_data_frame$DOY[row]; 778 doy = temperature_data_frame$DOY[row];
316 # Photoperiod in the day. 779 # Photoperiod in the day.
317 photoperiod = temperature_data_frame$DAYLEN[row]; 780 photoperiod = temperature_data_frame$DAYLEN[row];
318 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days); 781 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row, total_days);
319 mean.temp = temp.profile[1]; 782 mean.temp = temp.profile[1];
320 averages.temp = temp.profile[2]; 783 averages.temp = temp.profile[2];
321 averages.day[row] = averages.temp; 784 averages.day[row] = averages.temp;
322 # Trash bin for death. 785 # Trash bin for death.
323 death.vector = NULL; 786 death.vector = NULL;
339 if (vector.individual[2] == 0) { 802 if (vector.individual[2] == 0) {
340 # Egg. 803 # Egg.
341 death.probability = opt$egg_mortality * mortality.egg(mean.temp); 804 death.probability = opt$egg_mortality * mortality.egg(mean.temp);
342 } 805 }
343 else if (vector.individual[2] == 1 | vector.individual[2] == 2) { 806 else if (vector.individual[2] == 1 | vector.individual[2] == 2) {
807 # Nymph.
344 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp); 808 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp);
345 } 809 }
346 else if (vector.individual[2] == 3 | vector.individual[2] == 4 | vector.individual[2] == 5) { 810 else if (vector.individual[2] == 3 | vector.individual[2] == 4 | vector.individual[2] == 5) {
347 # Adult. 811 # Adult.
348 if (doy < day.kill) { 812 if (doy < day.kill) {
359 death.vector = c(death.vector, i); 823 death.vector = c(death.vector, i);
360 } 824 }
361 else { 825 else {
362 # End of diapause. 826 # End of diapause.
363 if (vector.individual[1] == 0 && vector.individual[2] == 3) { 827 if (vector.individual[1] == 0 && vector.individual[2] == 3) {
364 # Overwintering adult (previttelogenic). 828 # Overwintering adult (pre-vittelogenic).
365 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) { 829 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) {
366 # Add 68C to become fully reproductively matured. 830 # Add 68C to become fully reproductively matured.
367 # Transfer to vittelogenic. 831 # Transfer to vittelogenic.
368 vector.individual = c(0, 4, 0, 0, 0); 832 vector.individual = c(0, 4, 0, 0, 0);
369 vector.matrix[i,] = vector.individual; 833 vector.matrix[i,] = vector.individual;
370 } 834 }
371 else { 835 else {
372 # Add to # Add average temperature for current day. 836 # Add average temperature for current day.
373 vector.individual[3] = vector.individual[3] + averages.temp; 837 vector.individual[3] = vector.individual[3] + averages.temp;
374 # Add 1 day in current stage. 838 # Add 1 day in current stage.
375 vector.individual[4] = vector.individual[4] + 1; 839 vector.individual[4] = vector.individual[4] + 1;
376 vector.matrix[i,] = vector.individual; 840 vector.matrix[i,] = vector.individual;
377 } 841 }
378 } 842 }
379 if (vector.individual[1] != 0 && vector.individual[2] == 3) { 843 if (vector.individual[1] != 0 && vector.individual[2] == 3) {
380 # Not overwintering adult (previttelogenic). 844 # Not overwintering adult (pre-vittelogenic).
381 current.gen = vector.individual[1]; 845 current.gen = vector.individual[1];
382 if (vector.individual[3] > 68) { 846 if (vector.individual[3] > 68) {
383 # Add 68C to become fully reproductively matured. 847 # Add 68C to become fully reproductively matured.
384 # Transfer to vittelogenic. 848 # Transfer to vittelogenic.
385 vector.individual = c(current.gen, 4, 0, 0, 0); 849 vector.individual = c(current.gen, 4, 0, 0, 0);
490 # Add 1 day in current stage. 954 # Add 1 day in current stage.
491 vector.individual[4] = vector.individual[4] + 1; 955 vector.individual[4] = vector.individual[4] + 1;
492 } 956 }
493 vector.matrix[i,] = vector.individual; 957 vector.matrix[i,] = vector.individual;
494 } 958 }
495 # Old nymph to adult: previttelogenic or diapausing? 959 # Old nymph to adult: pre-vittelogenic or diapausing?
496 if (vector.individual[2] == 2) { 960 if (vector.individual[2] == 2) {
497 # Add average temperature for current day. 961 # Add average temperature for current day.
498 vector.individual[3] = vector.individual[3] + averages.temp; 962 vector.individual[3] = vector.individual[3] + averages.temp;
499 if (vector.individual[3] >= (200+opt$adult_accumulation)) { 963 if (vector.individual[3] >= (200+opt$adult_accumulation)) {
500 # From old to adult, degree_days requirement met. 964 # From old to adult, degree_days requirement met.
533 num_insects.newborn = length(birth.vector[,1]); 997 num_insects.newborn = length(birth.vector[,1]);
534 vector.matrix = rbind(vector.matrix, birth.vector); 998 vector.matrix = rbind(vector.matrix, birth.vector);
535 # Update population size for the next day. 999 # Update population size for the next day.
536 num_insects = num_insects - num_insects.death + num_insects.newborn; 1000 num_insects = num_insects - num_insects.death + num_insects.newborn;
537 1001
538 # Aggregate results by day. 1002 # Aggregate results by day. Due to multiple transpose calls
539 # Egg population size. 1003 # on vector.matrix above, the columns of vector.matrix
540 Eggs[row] = sum(vector.matrix[,2]==0); 1004 # are now Generation, Stage, degree-days, T, Diapause,
541 # Young nymph population size. 1005 if (process_eggs) {
542 YoungNymphs[row] = sum(vector.matrix[,2]==1); 1006 # For egg population size, column 2 (Stage), must be 0.
543 # Old nymph population size. 1007 Eggs[row] = sum(vector.matrix[,2]==0);
544 OldNymphs[row] = sum(vector.matrix[,2]==2); 1008 }
545 # Previtellogenic population size. 1009 if (process_young_nymphs | process_total_nymphs) {
546 Previtellogenic[row] = sum(vector.matrix[,2]==3); 1010 # For young nymph population size, column 2 (Stage) must be 1.
547 # Vitellogenic population size. 1011 YoungNymphs[row] = sum(vector.matrix[,2]==1);
548 Vitellogenic[row] = sum(vector.matrix[,2]==4); 1012 }
549 # Diapausing population size. 1013 if (process_old_nymphs | process_total_nymphs) {
550 Diapausing[row] = sum(vector.matrix[,2]==5); 1014 # For old nymph population size, column 2 (Stage) must be 2.
1015 OldNymphs[row] = sum(vector.matrix[,2]==2);
1016 }
1017 if (process_previttelogenic_adults | process_total_adults) {
1018 # For pre-vittelogenic population size, column 2 (Stage) must be 3.
1019 Previttelogenic[row] = sum(vector.matrix[,2]==3);
1020 }
1021 if (process_vittelogenic_adults | process_total_adults) {
1022 # For vittelogenic population size, column 2 (Stage) must be 4.
1023 Vittelogenic[row] = sum(vector.matrix[,2]==4);
1024 }
1025 if (process_diapausing_adults | process_total_adults) {
1026 # For diapausing population size, column 2 (Stage) must be 5.
1027 Diapausing[row] = sum(vector.matrix[,2]==5);
1028 }
551 1029
552 # Newborn population size. 1030 # Newborn population size.
553 N.newborn[row] = num_insects.newborn; 1031 N.newborn[row] = num_insects.newborn;
554 # Adult population size. 1032 # Adult population size.
555 N.adult[row] = sum(vector.matrix[,2]==3) + sum(vector.matrix[,2]==4) + sum(vector.matrix[,2]==5); 1033 N.adult[row] = sum(vector.matrix[,2]==3) + sum(vector.matrix[,2]==4) + sum(vector.matrix[,2]==5);
556 # Dead population size. 1034 # Dead population size.
557 N.death[row] = num_insects.death; 1035 N.death[row] = num_insects.death;
558 1036
559 total.population = c(total.population, num_insects); 1037 total.population = c(total.population, num_insects);
560 1038
561 # Overwintering adult population size. 1039 # For overwintering adult (P) population
1040 # size, column 1 (Generation) must be 0.
562 overwintering_adult.population[row] = sum(vector.matrix[,1]==0); 1041 overwintering_adult.population[row] = sum(vector.matrix[,1]==0);
563 # First generation population size. 1042 # For first field generation (F1) population
1043 # size, column 1 (Generation) must be 1.
564 first_generation.population[row] = sum(vector.matrix[,1]==1); 1044 first_generation.population[row] = sum(vector.matrix[,1]==1);
565 # Second generation population size. 1045 # For second field generation (F2) population
1046 # size, column 1 (Generation) must be 2.
566 second_generation.population[row] = sum(vector.matrix[,1]==2); 1047 second_generation.population[row] = sum(vector.matrix[,1]==2);
567 1048
568 # P adult population size. 1049 if (plot_generations_separately) {
569 P.adult[row] = sum(vector.matrix[,1]==0); 1050 if (process_eggs) {
570 # F1 adult population size. 1051 # For egg life stage of generation P population size,
571 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)); 1052 # column 1 (generation) is 0 and column 2 (Stage) is 0.
572 # F2 adult population size 1053 P.egg[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==0);
573 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)); 1054 # For egg life stage of generation F1 population size,
574 } # End of days specified in the input temperature data. 1055 # column 1 (generation) is 1 and column 2 (Stage) is 0.
1056 F1.egg[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==0);
1057 # For egg life stage of generation F2 population size,
1058 # column 1 (generation) is 2 and column 2 (Stage) is 0.
1059 F2.egg[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==0);
1060 }
1061 if (process_young_nymphs) {
1062 # For young nymph life stage of generation P population
1063 # size, the following combination is required:
1064 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph)
1065 P.young_nymph[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==1);
1066 # For young nymph life stage of generation F1 population
1067 # size, the following combination is required:
1068 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph)
1069 F1.young_nymph[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==1);
1070 # For young nymph life stage of generation F2 population
1071 # size, the following combination is required:
1072 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph)
1073 F2.young_nymph[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==1);
1074 }
1075 if (process_old_nymphs) {
1076 # For old nymph life stage of generation P population
1077 # size, the following combination is required:
1078 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph)
1079 P.old_nymph[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==2);
1080 # For old nymph life stage of generation F1 population
1081 # size, the following combination is required:
1082 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph)
1083 F1.old_nymph[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==2);
1084 # For old nymph life stage of generation F2 population
1085 # size, the following combination is required:
1086 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph)
1087 F2.old_nymph[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==2);
1088 }
1089 if (process_total_nymphs) {
1090 # For total nymph life stage of generation P population
1091 # size, one of the following combinations is required:
1092 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph)
1093 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph)
1094 P.total_nymph[row] = sum((vector.matrix[,1]==0 & vector.matrix[,2]==1) | (vector.matrix[,1]==0 & vector.matrix[,2]==2));
1095 # For total nymph life stage of generation F1 population
1096 # size, one of the following combinations is required:
1097 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph)
1098 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph)
1099 F1.total_nymph[row] = sum((vector.matrix[,1]==1 & vector.matrix[,2]==1) | (vector.matrix[,1]==1 & vector.matrix[,2]==2));
1100 # For total nymph life stage of generation F2 population
1101 # size, one of the following combinations is required:
1102 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph)
1103 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph)
1104 F2.total_nymph[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==1) | (vector.matrix[,1]==2 & vector.matrix[,2]==2));
1105 }
1106 if (process_previttelogenic_adults) {
1107 # For previttelogenic adult life stage of generation P population
1108 # size, the following combination is required:
1109 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vittelogenic)
1110 P.previttelogenic_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==3);
1111 # For previttelogenic adult life stage of generation F1 population
1112 # size, the following combination is required:
1113 # - column 1 (Generation) is 1 and column 2 (Stage) is 3 (Pre-vittelogenic)
1114 F1.previttelogenic_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==3);
1115 # For previttelogenic adult life stage of generation F2 population
1116 # size, the following combination is required:
1117 # - column 1 (Generation) is 2 and column 2 (Stage) is 3 (Pre-vittelogenic)
1118 F2.previttelogenic_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==3);
1119 }
1120 if (process_vittelogenic_adults) {
1121 # For vittelogenic adult life stage of generation P population
1122 # size, the following combination is required:
1123 # - column 1 (Generation) is 0 and column 2 (Stage) is 4 (Vittelogenic)
1124 P.vittelogenic_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==4);
1125 # For vittelogenic adult life stage of generation F1 population
1126 # size, the following combination is required:
1127 # - column 1 (Generation) is 1 and column 2 (Stage) is 4 (Vittelogenic)
1128 F1.vittelogenic_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==4);
1129 # For vittelogenic adult life stage of generation F2 population
1130 # size, the following combination is required:
1131 # - column 1 (Generation) is 2 and column 2 (Stage) is 4 (Vittelogenic)
1132 F2.vittelogenic_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==4);
1133 }
1134 if (process_diapausing_adults) {
1135 # For diapausing adult life stage of generation P population
1136 # size, the following combination is required:
1137 # - column 1 (Generation) is 0 and column 2 (Stage) is 5 (Diapausing)
1138 P.diapausing_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==5);
1139 # For diapausing adult life stage of generation F1 population
1140 # size, the following combination is required:
1141 # - column 1 (Generation) is 1 and column 2 (Stage) is 5 (Diapausing)
1142 F1.diapausing_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==5);
1143 # For diapausing adult life stage of generation F2 population
1144 # size, the following combination is required:
1145 # - column 1 (Generation) is 2 and column 2 (Stage) is 5 (Diapausing)
1146 F2.diapausing_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==5);
1147 }
1148 if (process_total_adults) {
1149 # For total adult life stage of generation P population
1150 # size, one of the following combinations is required:
1151 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vittelogenic)
1152 # - column 1 (Generation) is 0 and column 2 (Stage) is 4 (Vittelogenic)
1153 # - column 1 (Generation) is 0 and column 2 (Stage) is 5 (Diapausing)
1154 P.total_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));
1155 # For total adult life stage of generation F1 population
1156 # size, one of the following combinations is required:
1157 # - column 1 (Generation) is 1 and column 2 (Stage) is 3 (Pre-vittelogenic)
1158 # - column 1 (Generation) is 1 and column 2 (Stage) is 4 (Vittelogenic)
1159 # - column 1 (Generation) is 1 and column 2 (Stage) is 5 (Diapausing)
1160 F1.total_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));
1161 # For total adult life stage of generation F2 population
1162 # size, one of the following combinations is required:
1163 # - column 1 (Generation) is 2 and column 2 (Stage) is 3 (Pre-vittelogenic)
1164 # - column 1 (Generation) is 2 and column 2 (Stage) is 4 (Vittelogenic)
1165 # - column 1 (Generation) is 2 and column 2 (Stage) is 5 (Diapausing)
1166 F2.total_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));
1167 }
1168 }
1169 } # End of days specified in the input_ytd temperature data.
575 1170
576 averages.cum = cumsum(averages.day); 1171 averages.cum = cumsum(averages.day);
577 1172
578 # Define the output values. 1173 # Define the output values.
579 Eggs.replications[,N.replications] = Eggs; 1174 if (process_eggs) {
580 YoungNymphs.replications[,N.replications] = YoungNymphs; 1175 Eggs.replications[,current_replication] = Eggs;
581 OldNymphs.replications[,N.replications] = OldNymphs; 1176 }
582 Previtellogenic.replications[,N.replications] = Previtellogenic; 1177 if (process_young_nymphs | process_total_nymphs) {
583 Vitellogenic.replications[,N.replications] = Vitellogenic; 1178 YoungNymphs.replications[,current_replication] = YoungNymphs;
584 Diapausing.replications[,N.replications] = Diapausing; 1179 }
585 1180 if (process_old_nymphs | process_total_nymphs) {
586 newborn.replications[,N.replications] = N.newborn; 1181 OldNymphs.replications[,current_replication] = OldNymphs;
587 adult.replications[,N.replications] = N.adult; 1182 }
588 death.replications[,N.replications] = N.death; 1183 if (process_previttelogenic_adults | process_total_adults) {
589 1184 Previttelogenic.replications[,current_replication] = Previttelogenic;
590 P.replications[,N.replications] = overwintering_adult.population; 1185 }
591 P_adults.replications[,N.replications] = P.adult; 1186 if (process_vittelogenic_adults | process_total_adults) {
592 F1.replications[,N.replications] = first_generation.population; 1187 Vittelogenic.replications[,current_replication] = Vittelogenic;
593 F1_adults.replications[,N.replications] = F1.adult; 1188 }
594 F2.replications[,N.replications] = second_generation.population; 1189 if (process_diapausing_adults | process_total_adults) {
595 F2_adults.replications[,N.replications] = F2.adult; 1190 Diapausing.replications[,current_replication] = Diapausing;
596 1191 }
597 population.replications[,N.replications] = total.population; 1192 newborn.replications[,current_replication] = N.newborn;
598 } 1193 adult.replications[,current_replication] = N.adult;
599 1194 death.replications[,current_replication] = N.death;
600 # Mean value for eggs. 1195 if (plot_generations_separately) {
601 eggs = apply(Eggs.replications, 1, mean); 1196 # P is Parental, or overwintered adults.
602 # Standard error for eggs. 1197 P.replications[,current_replication] = overwintering_adult.population;
603 eggs.std_error = apply(Eggs.replications, 1, sd) / sqrt(opt$replications); 1198 # F1 is the first field-produced generation.
604 1199 F1.replications[,current_replication] = first_generation.population;
605 # Mean value for nymphs. 1200 # F2 is the second field-produced generation.
606 nymphs = apply((YoungNymphs.replications+OldNymphs.replications), 1, mean); 1201 F2.replications[,current_replication] = second_generation.population;
607 # Standard error for nymphs. 1202 if (process_eggs) {
608 nymphs.std_error = apply((YoungNymphs.replications+OldNymphs.replications) / sqrt(opt$replications), 1, sd); 1203 P_eggs.replications[,current_replication] = P.egg;
609 1204 F1_eggs.replications[,current_replication] = F1.egg;
610 # Mean value for adults. 1205 F2_eggs.replications[,current_replication] = F2.egg;
611 adults = apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, mean); 1206 }
612 # Standard error for adults. 1207 if (process_young_nymphs) {
613 adults.std_error = apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, sd) / sqrt(opt$replications); 1208 P_young_nymphs.replications[,current_replication] = P.young_nymph;
614 1209 F1_young_nymphs.replications[,current_replication] = F1.young_nymph;
615 # Mean value for P. 1210 F2_young_nymphs.replications[,current_replication] = F2.young_nymph;
616 P = apply(P.replications, 1, mean); 1211 }
617 # Standard error for P. 1212 if (process_old_nymphs) {
618 P.std_error = apply(P.replications, 1, sd) / sqrt(opt$replications); 1213 P_old_nymphs.replications[,current_replication] = P.old_nymph;
619 1214 F1_old_nymphs.replications[,current_replication] = F1.old_nymph;
620 # Mean value for P adults. 1215 F2_old_nymphs.replications[,current_replication] = F2.old_nymph;
621 P_adults = apply(P_adults.replications, 1, mean); 1216 }
622 # Standard error for P_adult. 1217 if (process_total_nymphs) {
623 P_adults.std_error = apply(P_adults.replications, 1, sd) / sqrt(opt$replications); 1218 P_total_nymphs.replications[,current_replication] = P.total_nymph;
624 1219 F1_total_nymphs.replications[,current_replication] = F1.total_nymph;
625 # Mean value for F1. 1220 F2_total_nymphs.replications[,current_replication] = F2.total_nymph;
626 F1 = apply(F1.replications, 1, mean); 1221 }
627 # Standard error for F1. 1222 if (process_previttelogenic_adults) {
628 F1.std_error = apply(F1.replications, 1, sd) / sqrt(opt$replications); 1223 P_previttelogenic_adults.replications[,current_replication] = P.previttelogenic_adult;
629 1224 F1_previttelogenic_adults.replications[,current_replication] = F1.previttelogenic_adult;
630 # Mean value for F1 adults. 1225 F2_previttelogenic_adults.replications[,current_replication] = F2.previttelogenic_adult;
631 F1_adults = apply(F1_adults.replications, 1, mean); 1226 }
632 # Standard error for F1 adult. 1227 if (process_vittelogenic_adults) {
633 F1_adults.std_error = apply(F1_adults.replications, 1, sd) / sqrt(opt$replications); 1228 P_vittelogenic_adults.replications[,current_replication] = P.vittelogenic_adult;
634 1229 F1_vittelogenic_adults.replications[,current_replication] = F1.vittelogenic_adult;
635 # Mean value for F2. 1230 F2_vittelogenic_adults.replications[,current_replication] = F2.vittelogenic_adult;
636 F2 = apply(F2.replications, 1, mean); 1231 }
637 # Standard error for F2. 1232 if (process_diapausing_adults) {
638 F2.std_error = apply(F2.replications, 1, sd) / sqrt(opt$replications); 1233 P_diapausing_adults.replications[,current_replication] = P.diapausing_adult;
639 1234 F1_diapausing_adults.replications[,current_replication] = F1.diapausing_adult;
640 # Mean value for F2 adults. 1235 F2_diapausing_adults.replications[,current_replication] = F2.diapausing_adult;
641 F2_adults = apply(F2_adults.replications, 1, mean); 1236 }
642 # Standard error for F2 adult. 1237 if (process_total_adults) {
643 F2_adults.std_error = apply(F2_adults.replications, 1, sd) / sqrt(opt$replications); 1238 P_total_adults.replications[,current_replication] = P.total_adult;
644 1239 F1_total_adults.replications[,current_replication] = F1.total_adult;
645 # Display the total number of days in the Galaxy history item blurb. 1240 F2_total_adults.replications[,current_replication] = F2.total_adult;
646 cat("Number of days: ", opt$num_days, "\n"); 1241 }
647 1242 }
648 dev.new(width=20, height=30); 1243 population.replications[,current_replication] = total.population;
649 1244 # End processing replications.
650 # Start PDF device driver to save charts to output. 1245 }
651 pdf(file=opt$output, width=20, height=30, bg="white"); 1246
652 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1)); 1247 if (process_eggs) {
653 1248 # Mean value for eggs.
654 # Data analysis and visualization plots only within a single calendar year. 1249 eggs = apply(Eggs.replications, 1, mean);
655 days = c(1:opt$num_days); 1250 temperature_data_frame = append_vector(temperature_data_frame, eggs, "EGG");
656 start_date = temperature_data_frame$DATE[1]; 1251 # Standard error for eggs.
657 end_date = temperature_data_frame$DATE[opt$num_days]; 1252 eggs.std_error = apply(Eggs.replications, 1, sd) / sqrt(opt$replications);
658 1253 temperature_data_frame = append_vector(temperature_data_frame, eggs.std_error, "EGGSE");
659 # Subfigure 1: population size by life stage. 1254 }
660 maxval = max(eggs+eggs.std_error, nymphs+nymphs.std_error, adults+adults.std_error); 1255 if (process_nymphs) {
661 render_chart("pop_size_by_life_stage", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, 1256 # Calculate nymph populations for selected life stage.
662 opt$std_error_plot, adults, nymphs, eggs, adults.std_error, nymphs.std_error, eggs.std_error, date_labels); 1257 for (life_stage_nymph in life_stages_nymph) {
663 # Subfigure 2: population size by generation. 1258 if (life_stage_nymph=="Young") {
664 maxval = max(F2); 1259 # Mean value for young nymphs.
665 render_chart("pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, 1260 young_nymphs = apply(YoungNymphs.replications, 1, mean);
666 opt$std_error_plot, P, F1, F2, P.std_error, F1.std_error, F2.std_error, date_labels); 1261 temperature_data_frame = append_vector(temperature_data_frame, young_nymphs, "YOUNGNYMPH");
667 # Subfigure 3: adult population size by generation. 1262 # Standard error for young nymphs.
668 maxval = max(F2_adults) + 100; 1263 young_nymphs.std_error = apply(YoungNymphs.replications / sqrt(opt$replications), 1, sd);
669 render_chart("adult_pop_size_by_generation", opt$insect, opt$location, latitude, start_date, end_date, days, maxval, 1264 temperature_data_frame = append_vector(temperature_data_frame, young_nymphs.std_error, "YOUNGNYMPHSE");
670 opt$std_error_plot, P_adults, F1_adults, F2_adults, P_adults.std_error, F1_adults.std_error, F2_adults.std_error, 1265 } else if (life_stage_nymph=="Old") {
671 date_labels); 1266 # Mean value for old nymphs.
672 1267 old_nymphs = apply(OldNymphs.replications, 1, mean);
673 # Turn off device driver to flush output. 1268 temperature_data_frame = append_vector(temperature_data_frame, old_nymphs, "OLDNYMPH");
674 dev.off(); 1269 # Standard error for old nymphs.
1270 old_nymphs.std_error = apply(OldNymphs.replications / sqrt(opt$replications), 1, sd);
1271 temperature_data_frame = append_vector(temperature_data_frame, old_nymphs.std_error, "OLDNYMPHSE");
1272 } else if (life_stage_nymph=="Total") {
1273 # Mean value for all nymphs.
1274 total_nymphs = apply((YoungNymphs.replications+OldNymphs.replications), 1, mean);
1275 temperature_data_frame = append_vector(temperature_data_frame, total_nymphs, "TOTALNYMPH");
1276 # Standard error for all nymphs.
1277 total_nymphs.std_error = apply((YoungNymphs.replications+OldNymphs.replications) / sqrt(opt$replications), 1, sd);
1278 temperature_data_frame = append_vector(temperature_data_frame, total_nymphs.std_error, "TOTALNYMPHSE");
1279 }
1280 }
1281 }
1282 if (process_adults) {
1283 # Calculate adult populations for selected life stage.
1284 for (life_stage_adult in life_stages_adult) {
1285 if (life_stage_adult == "Pre-vittelogenic") {
1286 # Mean value for previttelogenic adults.
1287 previttelogenic_adults = apply(Previttelogenic.replications, 1, mean);
1288 temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults, "PRE-VITADULT");
1289 # Standard error for previttelogenic adults.
1290 previttelogenic_adults.std_error = apply(Previttelogenic.replications, 1, sd) / sqrt(opt$replications);
1291 temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults.std_error, "PRE-VITADULTSE");
1292 } else if (life_stage_adult == "Vittelogenic") {
1293 # Mean value for vittelogenic adults.
1294 vittelogenic_adults = apply(Vittelogenic.replications, 1, mean);
1295 temperature_data_frame = append_vector(temperature_data_frame, vittelogenic_adults, "VITADULT");
1296 # Standard error for vittelogenic adults.
1297 vittelogenic_adults.std_error = apply(Vittelogenic.replications, 1, sd) / sqrt(opt$replications);
1298 temperature_data_frame = append_vector(temperature_data_frame, vittelogenic_adults.std_error, "VITADULTSE");
1299 } else if (life_stage_adult == "Diapausing") {
1300 # Mean value for vittelogenic adults.
1301 diapausing_adults = apply(Diapausing.replications, 1, mean);
1302 temperature_data_frame = append_vector(temperature_data_frame, diapausing_adults, "DIAPAUSINGADULT");
1303 # Standard error for vittelogenic adults.
1304 diapausing_adults.std_error = apply(Diapausing.replications, 1, sd) / sqrt(opt$replications);
1305 temperature_data_frame = append_vector(temperature_data_frame, diapausing_adults.std_error, "DIAPAUSINGADULTSE");
1306 } else if (life_stage_adult=="Total") {
1307 # Mean value for all adults.
1308 total_adults = apply((Previttelogenic.replications+Vittelogenic.replications+Diapausing.replications), 1, mean);
1309 temperature_data_frame = append_vector(temperature_data_frame, total_adults, "TOTALADULT");
1310 # Standard error for all adults.
1311 total_adults.std_error = apply((Previttelogenic.replications+Vittelogenic.replications+Diapausing.replications), 1, sd) / sqrt(opt$replications);
1312 temperature_data_frame = append_vector(temperature_data_frame, total_adults.std_error, "TOTALADULTSE");
1313 }
1314 }
1315 }
1316
1317 if (plot_generations_separately) {
1318 m_se = get_mean_and_std_error(P.replications, F1.replications, F2.replications);
1319 P = m_se[[1]];
1320 P.std_error = m_se[[2]];
1321 F1 = m_se[[3]];
1322 F1.std_error = m_se[[4]];
1323 F2 = m_se[[5]];
1324 F2.std_error = m_se[[6]];
1325 if (process_eggs) {
1326 m_se = get_mean_and_std_error(P_eggs.replications, F1_eggs.replications, F2_eggs.replications);
1327 P_eggs = m_se[[1]];
1328 P_eggs.std_error = m_se[[2]];
1329 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs, "EGG-P");
1330 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs.std_error, "EGG-P-SE");
1331 F1_eggs = m_se[[3]];
1332 F1_eggs.std_error = m_se[[4]];
1333 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs, "EGG-F1");
1334 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs.std_error, "EGG-F1-SE");
1335 F2_eggs = m_se[[5]];
1336 F2_eggs.std_error = m_se[[6]];
1337 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs, "EGG-F2");
1338 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs.std_error, "EGG-F2-SE");
1339 }
1340 if (process_young_nymphs) {
1341 m_se = get_mean_and_std_error(P_young_nymphs.replications, F1_young_nymphs.replications, F2_young_nymphs.replications);
1342 P_young_nymphs = m_se[[1]];
1343 P_young_nymphs.std_error = m_se[[2]];
1344 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs, "YOUNGNYMPH-P");
1345 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs.std_error, "YOUNGNYMPH-P-SE");
1346 F1_young_nymphs = m_se[[3]];
1347 F1_young_nymphs.std_error = m_se[[4]];
1348 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs, "YOUNGNYMPH-F1");
1349 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs.std_error, "YOUNGNYMPH-F1-SE");
1350 F2_young_nymphs = m_se[[5]];
1351 F2_young_nymphs.std_error = m_se[[6]];
1352 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs, "YOUNGNYMPH-F2");
1353 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs.std_error, "YOUNGNYMPH-F2-SE");
1354 }
1355 if (process_old_nymphs) {
1356 m_se = get_mean_and_std_error(P_old_nymphs.replications, F1_old_nymphs.replications, F2_old_nymphs.replications);
1357 P_old_nymphs = m_se[[1]];
1358 P_old_nymphs.std_error = m_se[[2]];
1359 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs, "OLDNYMPH-P");
1360 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs.std_error, "OLDNYMPH-P-SE");
1361 F1_old_nymphs = m_se[[3]];
1362 F1_old_nymphs.std_error = m_se[[4]];
1363 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs, "OLDNYMPH-F1");
1364 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs.std_error, "OLDNYMPH-F1-SE");
1365 F2_old_nymphs = m_se[[5]];
1366 F2_old_nymphs.std_error = m_se[[6]];
1367 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs, "OLDNYMPH-F2");
1368 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs.std_error, "OLDNYMPH-F2-SE");
1369 }
1370 if (process_total_nymphs) {
1371 m_se = get_mean_and_std_error(P_total_nymphs.replications, F1_total_nymphs.replications, F2_total_nymphs.replications);
1372 P_total_nymphs = m_se[[1]];
1373 P_total_nymphs.std_error = m_se[[2]];
1374 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs, "TOTALNYMPH-P");
1375 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs.std_error, "TOTALNYMPH-P-SE");
1376 F1_total_nymphs = m_se[[3]];
1377 F1_total_nymphs.std_error = m_se[[4]];
1378 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs, "TOTALNYMPH-F1");
1379 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs.std_error, "TOTALNYMPH-F1-SE");
1380 F2_total_nymphs = m_se[[5]];
1381 F2_total_nymphs.std_error = m_se[[6]];
1382 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs, "TOTALNYMPH-F2");
1383 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs.std_error, "TOTALNYMPH-F2-SE");
1384 }
1385 if (process_previttelogenic_adults) {
1386 m_se = get_mean_and_std_error(P_previttelogenic_adults.replications, F1_previttelogenic_adults.replications, F2_previttelogenic_adults.replications);
1387 P_previttelogenic_adults = m_se[[1]];
1388 P_previttelogenic_adults.std_error = m_se[[2]];
1389 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults, "PRE-VITADULT-P");
1390 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults.std_error, "PRE-VITADULT-P-SE");
1391 F1_previttelogenic_adults = m_se[[3]];
1392 F1_previttelogenic_adults.std_error = m_se[[4]];
1393 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults, "PRE-VITADULT-F1");
1394 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults.std_error, "PRE-VITADULT-F1-SE");
1395 F2_previttelogenic_adults = m_se[[5]];
1396 F2_previttelogenic_adults.std_error = m_se[[6]];
1397 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults, "PRE-VITADULT-F2");
1398 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults.std_error, "PRE-VITADULT-F2-SE");
1399 }
1400 if (process_vittelogenic_adults) {
1401 m_se = get_mean_and_std_error(P_vittelogenic_adults.replications, F1_vittelogenic_adults.replications, F2_vittelogenic_adults.replications);
1402 P_vittelogenic_adults = m_se[[1]];
1403 P_vittelogenic_adults.std_error = m_se[[2]];
1404 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults, "VITADULT-P");
1405 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults.std_error, "VITADULT-P-SE");
1406 F1_vittelogenic_adults = m_se[[3]];
1407 F1_vittelogenic_adults.std_error = m_se[[4]];
1408 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults, "VITADULT-F1");
1409 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults.std_error, "VITADULT-F1-SE");
1410 F2_vittelogenic_adults = m_se[[5]];
1411 F2_vittelogenic_adults.std_error = m_se[[6]];
1412 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults, "VITADULT-F2");
1413 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults.std_error, "VITADULT-F2-SE");
1414 }
1415 if (process_diapausing_adults) {
1416 m_se = get_mean_and_std_error(P_diapausing_adults.replications, F1_diapausing_adults.replications, F2_diapausing_adults.replications);
1417 P_diapausing_adults = m_se[[1]];
1418 P_diapausing_adults.std_error = m_se[[2]];
1419 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults, "DIAPAUSINGADULT-P");
1420 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults.std_error, "DIAPAUSINGADULT-P-SE");
1421 F1_diapausing_adults = m_se[[3]];
1422 F1_diapausing_adults.std_error = m_se[[4]];
1423 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults, "DIAPAUSINGADULT-F1");
1424 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults.std_error, "DIAPAUSINGADULT-F1-SE");
1425 F2_diapausing_adults = m_se[[5]];
1426 F2_diapausing_adults.std_error = m_se[[6]];
1427 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults, "DIAPAUSINGADULT-F2");
1428 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults.std_error, "DIAPAUSINGADULT-F2-SE");
1429 }
1430 if (process_total_adults) {
1431 m_se = get_mean_and_std_error(P_total_adults.replications, F1_total_adults.replications, F2_total_adults.replications);
1432 P_total_adults = m_se[[1]];
1433 P_total_adults.std_error = m_se[[2]];
1434 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults, "TOTALADULT-P");
1435 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults.std_error, "TOTALADULT-P-SE");
1436 F1_total_adults = m_se[[3]];
1437 F1_total_adults.std_error = m_se[[4]];
1438 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults, "TOTALADULT-F1");
1439 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults.std_error, "TOTALADULT-F1-SE");
1440 F2_total_adults = m_se[[5]];
1441 F2_total_adults.std_error = m_se[[6]];
1442 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults, "TOTALADULT-F2");
1443 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults.std_error, "TOTALADULT-F2-SE");
1444 }
1445 }
1446
1447 # Save the analyzed data for combined generations.
1448 file_path = paste("output_data_dir", "04_combined_generations.csv", sep="/");
1449 write.csv(temperature_data_frame, file=file_path, row.names=F);
1450 if (plot_generations_separately) {
1451 # Save the analyzed data for generation P.
1452 file_path = paste("output_data_dir", "01_generation_P.csv", sep="/");
1453 write.csv(temperature_data_frame_P, file=file_path, row.names=F);
1454 # Save the analyzed data for generation F1.
1455 file_path = paste("output_data_dir", "02_generation_F1.csv", sep="/");
1456 write.csv(temperature_data_frame_F1, file=file_path, row.names=F);
1457 # Save the analyzed data for generation F2.
1458 file_path = paste("output_data_dir", "03_generation_F2.csv", sep="/");
1459 write.csv(temperature_data_frame_F2, file=file_path, row.names=F);
1460 }
1461
1462 if (plot_generations_separately) {
1463 for (life_stage in life_stages) {
1464 if (life_stage == "Egg") {
1465 # Start PDF device driver.
1466 dev.new(width=20, height=30);
1467 file_path = get_file_path(life_stage, "egg_pop_by_generation.pdf")
1468 pdf(file=file_path, width=20, height=30, bg="white");
1469 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
1470 # Egg population size by generation.
1471 maxval = max(P_eggs+F1_eggs+F2_eggs) + 100;
1472 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
1473 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=P_eggs, group_std_error=P_eggs.std_error,
1474 group2=F1_eggs, group2_std_error=F1_eggs.std_error, group3=F2_eggs, group3_std_error=F2_eggs.std_error);
1475 # Turn off device driver to flush output.
1476 dev.off();
1477 } else if (life_stage == "Nymph") {
1478 for (life_stage_nymph in life_stages_nymph) {
1479 # Start PDF device driver.
1480 dev.new(width=20, height=30);
1481 file_path = get_file_path(life_stage, "nymph_pop_by_generation.pdf", life_stage_nymph=life_stage_nymph)
1482 pdf(file=file_path, width=20, height=30, bg="white");
1483 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
1484 if (life_stage_nymph=="Young") {
1485 # Young nymph population size by generation.
1486 maxval = max(P_young_nymphs+F1_young_nymphs+F2_young_nymphs) + 100;
1487 group = P_young_nymphs;
1488 group_std_error = P_young_nymphs.std_error;
1489 group2 = F1_young_nymphs;
1490 group2_std_error = F1_young_nymphs.std_error;
1491 group3 = F2_young_nymphs;
1492 group3_std_error = F2_young_nymphs.std_error;
1493 } else if (life_stage_nymph=="Old") {
1494 # Total nymph population size by generation.
1495 maxval = max(P_old_nymphs+F1_old_nymphs+F2_old_nymphs) + 100;
1496 group = P_old_nymphs;
1497 group_std_error = P_old_nymphs.std_error;
1498 group2 = F1_old_nymphs;
1499 group2_std_error = F1_old_nymphs.std_error;
1500 group3 = F2_old_nymphs;
1501 group3_std_error = F2_old_nymphs.std_error;
1502 } else if (life_stage_nymph=="Total") {
1503 # Total nymph population size by generation.
1504 maxval = max(P_total_nymphs+F1_total_nymphs+F2_total_nymphs) + 100;
1505 group = P_total_nymphs;
1506 group_std_error = P_total_nymphs.std_error;
1507 group2 = F1_total_nymphs;
1508 group2_std_error = F1_total_nymphs.std_error;
1509 group3 = F2_total_nymphs;
1510 group3_std_error = F2_total_nymphs.std_error;
1511 }
1512 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
1513 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
1514 group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, life_stages_nymph=life_stage_nymph);
1515 # Turn off device driver to flush output.
1516 dev.off();
1517 }
1518 } else if (life_stage == "Adult") {
1519 for (life_stage_adult in life_stages_adult) {
1520 # Start PDF device driver.
1521 dev.new(width=20, height=30);
1522 file_path = get_file_path(life_stage, "adult_pop_by_generation.pdf", life_stage_adult=life_stage_adult)
1523 pdf(file=file_path, width=20, height=30, bg="white");
1524 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
1525 if (life_stage_adult=="Pre-vittelogenic") {
1526 # Pre-vittelogenic adult population size by generation.
1527 maxval = max(P_previttelogenic_adults+F1_previttelogenic_adults+F2_previttelogenic_adults) + 100;
1528 group = P_previttelogenic_adults;
1529 group_std_error = P_previttelogenic_adults.std_error;
1530 group2 = F1_previttelogenic_adults;
1531 group2_std_error = F1_previttelogenic_adults.std_error;
1532 group3 = F2_previttelogenic_adults;
1533 group3_std_error = F2_previttelogenic_adults.std_error;
1534 } else if (life_stage_adult=="Vittelogenic") {
1535 # Vittelogenic adult population size by generation.
1536 maxval = max(P_vittelogenic_adults+F1_vittelogenic_adults+F2_vittelogenic_adults) + 100;
1537 group = P_vittelogenic_adults;
1538 group_std_error = P_vittelogenic_adults.std_error;
1539 group2 = F1_vittelogenic_adults;
1540 group2_std_error = F1_vittelogenic_adults.std_error;
1541 group3 = F2_vittelogenic_adults;
1542 group3_std_error = F2_vittelogenic_adults.std_error;
1543 } else if (life_stage_adult=="Diapausing") {
1544 # Diapausing adult population size by generation.
1545 maxval = max(P_diapausing_adults+F1_diapausing_adults+F2_diapausing_adults) + 100;
1546 group = P_diapausing_adults;
1547 group_std_error = P_diapausing_adults.std_error;
1548 group2 = F1_diapausing_adults;
1549 group2_std_error = F1_diapausing_adults.std_error;
1550 group3 = F2_diapausing_adults;
1551 group3_std_error = F2_diapausing_adults.std_error;
1552 } else if (life_stage_adult=="Total") {
1553 # Total adult population size by generation.
1554 maxval = max(P_total_adults+F1_total_adults+F2_total_adults) + 100;
1555 group = P_total_adults;
1556 group_std_error = P_total_adults.std_error;
1557 group2 = F1_total_adults;
1558 group2_std_error = F1_total_adults.std_error;
1559 group3 = F2_total_adults;
1560 group3_std_error = F2_total_adults.std_error;
1561 }
1562 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
1563 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
1564 group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, life_stages_adult=life_stage_adult);
1565 # Turn off device driver to flush output.
1566 dev.off();
1567 }
1568 } else if (life_stage == "Total") {
1569 # Start PDF device driver.
1570 # Name collection elements so that they
1571 # are displayed in logical order.
1572 dev.new(width=20, height=30);
1573 file_path = get_file_path(life_stage, "total_pop_by_generation.pdf")
1574 pdf(file=file_path, width=20, height=30, bg="white");
1575 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
1576 # Total population size by generation.
1577 maxval = max(P+F1+F2) + 100;
1578 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
1579 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=P, group_std_error=P.std_error,
1580 group2=F1, group2_std_error=F1.std_error, group3=F2, group3_std_error=F2.std_error);
1581 # Turn off device driver to flush output.
1582 dev.off();
1583 }
1584 }
1585 } else {
1586 for (life_stage in life_stages) {
1587 if (life_stage == "Egg") {
1588 # Start PDF device driver.
1589 dev.new(width=20, height=30);
1590 file_path = get_file_path(life_stage, "egg_pop.pdf")
1591 pdf(file=file_path, width=20, height=30, bg="white");
1592 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
1593 # Egg population size.
1594 maxval = max(eggs+eggs.std_error) + 100;
1595 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
1596 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=eggs, group_std_error=eggs.std_error);
1597 # Turn off device driver to flush output.
1598 dev.off();
1599 } else if (life_stage == "Nymph") {
1600 for (life_stage_nymph in life_stages_nymph) {
1601 # Start PDF device driver.
1602 dev.new(width=20, height=30);
1603 file_path = get_file_path(life_stage, "nymph_pop.pdf", life_stage_nymph=life_stage_nymph)
1604 pdf(file=file_path, width=20, height=30, bg="white");
1605 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
1606 if (life_stage_nymph=="Total") {
1607 # Total nymph population size.
1608 group = total_nymphs;
1609 group_std_error = total_nymphs.std_error;
1610 } else if (life_stage_nymph=="Young") {
1611 # Young nymph population size.
1612 group = young_nymphs;
1613 group_std_error = young_nymphs.std_error;
1614 } else if (life_stage_nymph=="Old") {
1615 # Old nymph population size.
1616 group = old_nymphs;
1617 group_std_error = old_nymphs.std_error;
1618 }
1619 maxval = max(group+group_std_error) + 100;
1620 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
1621 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
1622 life_stages_nymph=life_stage_nymph);
1623 # Turn off device driver to flush output.
1624 dev.off();
1625 }
1626 } else if (life_stage == "Adult") {
1627 for (life_stage_adult in life_stages_adult) {
1628 # Start PDF device driver.
1629 dev.new(width=20, height=30);
1630 file_path = get_file_path(life_stage, "adult_pop.pdf", life_stage_adult=life_stage_adult)
1631 pdf(file=file_path, width=20, height=30, bg="white");
1632 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
1633 if (life_stage_adult=="Total") {
1634 # Total adult population size.
1635 group = total_adults;
1636 group_std_error = total_adults.std_error
1637 } else if (life_stage_adult=="Pre-vittelogenic") {
1638 # Pre-vittelogenic adult population size.
1639 group = previttelogenic_adults;
1640 group_std_error = previttelogenic_adults.std_error
1641 } else if (life_stage_adult=="Vittelogenic") {
1642 # Vittelogenic adult population size.
1643 group = vittelogenic_adults;
1644 group_std_error = vittelogenic_adults.std_error
1645 } else if (life_stage_adult=="Diapausing") {
1646 # Diapausing adult population size.
1647 group = diapausing_adults;
1648 group_std_error = diapausing_adults.std_error
1649 }
1650 maxval = max(group+group_std_error) + 100;
1651 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
1652 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
1653 life_stages_adult=life_stage_adult);
1654 # Turn off device driver to flush output.
1655 dev.off();
1656 }
1657 } else if (life_stage == "Total") {
1658 # Start PDF device driver.
1659 dev.new(width=20, height=30);
1660 file_path = get_file_path(life_stage, "total_pop.pdf")
1661 pdf(file=file_path, width=20, height=30, bg="white");
1662 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
1663 # Total population size.
1664 maxval = max(eggs+eggs.std_error, total_nymphs+total_nymphs.std_error, total_adults+total_adults.std_error) + 100;
1665 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
1666 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=total_adults, group_std_error=total_adults.std_error,
1667 group2=total_nymphs, group2_std_error=total_nymphs.std_error, group3=eggs, group3_std_error=eggs.std_error);
1668 # Turn off device driver to flush output.
1669 dev.off();
1670 }
1671 }
1672 }