85
|
1 #!/usr/bin/env Rscript
|
|
2
|
|
3 suppressPackageStartupMessages(library("optparse"))
|
|
4
|
|
5 option_list <- list(
|
109
|
6 make_option(c("--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"),
|
|
7 make_option(c("--adult_accumulation"), action="store", dest="adult_accumulation", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"),
|
|
8 make_option(c("--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"),
|
117
|
9 make_option(c("--end_date"), action="store", dest="end_date", default=NULL, help="End date for custom date interval"),
|
112
|
10 make_option(c("--input_norm"), action="store", dest="input_norm", help="30 year normals temperature data for selected station"),
|
|
11 make_option(c("--input_ytd"), action="store", dest="input_ytd", default=NULL, help="Year-to-date temperature data for selected location"),
|
109
|
12 make_option(c("--insect"), action="store", dest="insect", help="Insect name"),
|
|
13 make_option(c("--insects_per_replication"), action="store", dest="insects_per_replication", type="integer", help="Number of insects with which to start each replication"),
|
112
|
14 make_option(c("--life_stages"), action="store", dest="life_stages", help="Selected life stages for plotting"),
|
|
15 make_option(c("--life_stages_adult"), action="store", dest="life_stages_adult", default=NULL, help="Adult life stages for plotting"),
|
|
16 make_option(c("--life_stages_nymph"), action="store", dest="life_stages_nymph", default=NULL, help="Nymph life stages for plotting"),
|
|
17 make_option(c("--location"), action="store", dest="location", default=NULL, help="Selected location"),
|
109
|
18 make_option(c("--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
|
|
19 make_option(c("--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
|
112
|
20 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"),
|
109
|
21 make_option(c("--nymph_mortality"), action="store", dest="nymph_mortality", type="integer", help="Adjustment rate for nymph mortality"),
|
|
22 make_option(c("--old_nymph_accumulation"), action="store", dest="old_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"),
|
|
23 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
|
|
24 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
|
112
|
25 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"),
|
|
26 make_option(c("--plot_std_error"), action="store", dest="plot_std_error", help="Plot Standard error"),
|
109
|
27 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
|
117
|
28 make_option(c("--start_date"), action="store", dest="start_date", default=NULL, help="Start date for custom date interval"),
|
109
|
29 make_option(c("--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)")
|
85
|
30 )
|
|
31
|
111
|
32 parser <- OptionParser(usage="%prog [options] file", option_list=option_list);
|
|
33 args <- parse_args(parser, positional_arguments=TRUE);
|
|
34 opt <- args$options;
|
85
|
35
|
122
|
36 add_daylight_length = function(temperature_data_frame) {
|
|
37 # Return temperature_data_frame with an added column
|
|
38 # of daylight length (photoperido profile).
|
|
39 num_rows = dim(temperature_data_frame)[1];
|
|
40 # From Forsythe 1995.
|
111
|
41 p = 0.8333;
|
|
42 latitude = temperature_data_frame$LATITUDE[1];
|
|
43 daylight_length_vector = NULL;
|
85
|
44 for (i in 1:num_rows) {
|
|
45 # Get the day of the year from the current row
|
|
46 # of the temperature data for computation.
|
111
|
47 doy = temperature_data_frame$DOY[i];
|
|
48 theta = 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186)));
|
|
49 phi = asin(0.39795 * cos(theta));
|
85
|
50 # Compute the length of daylight for the day of the year.
|
111
|
51 darkness_length = 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)));
|
|
52 daylight_length_vector[i] = 24 - darkness_length;
|
85
|
53 }
|
|
54 # Append daylight_length_vector as a new column to temperature_data_frame.
|
112
|
55 temperature_data_frame = append_vector(temperature_data_frame, daylight_length_vector, "DAYLEN");
|
111
|
56 return(temperature_data_frame);
|
85
|
57 }
|
|
58
|
112
|
59 append_vector = function(data_frame, vec, new_column_name) {
|
|
60 num_columns = dim(data_frame)[2];
|
|
61 current_column_names = colnames(data_frame);
|
|
62 # Append vector vec as a new column to data_frame.
|
|
63 data_frame[,num_columns+1] = vec;
|
|
64 # Reset the column names with the additional column for later access.
|
|
65 colnames(data_frame) = append(current_column_names, new_column_name);
|
|
66 return(data_frame);
|
102
|
67 }
|
|
68
|
119
|
69 extract_date_interval_rows = function(df, start_date, end_date) {
|
120
|
70 date_interval_rows = df[df$DATE >= start_date & df$DATE <= end_date];
|
119
|
71 return(date_interval_rows);
|
|
72 }
|
|
73
|
122
|
74 from_30_year_normals = function(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy) {
|
|
75 # The data we want is fully contained within the 30 year normals data.
|
|
76 first_norm_row = which(norm_data_frame$DOY==start_date_doy);
|
|
77 last_norm_row = which(norm_data_frame$DOY==end_date_doy);
|
|
78 norm_data_frame_rows = last_norm_row - first_norm_row;
|
|
79 j = 0;
|
|
80 for (i in first_norm_row:last_norm_row) {
|
|
81 j = j + 1;
|
|
82 temperature_data_frame[j,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
|
|
83 }
|
|
84 return (temperature_data_frame);
|
|
85 }
|
|
86
|
112
|
87 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) {
|
|
88 if (!is.null(life_stage_nymph)) {
|
|
89 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph);
|
|
90 file_name = paste(lsi, tolower(life_stage_nymph), base_name, sep="_");
|
|
91 } else if (!is.null(life_stage_adult)) {
|
|
92 lsi = get_life_stage_index(life_stage, life_stage_adult=life_stage_adult);
|
|
93 file_name = paste(lsi, tolower(life_stage_adult), base_name, sep="_");
|
|
94 } else {
|
|
95 lsi = get_life_stage_index(life_stage);
|
|
96 file_name = paste(lsi, base_name, sep="_");
|
|
97 }
|
|
98 file_path = paste("output_plots_dir", file_name, sep="/");
|
|
99 return(file_path);
|
102
|
100 }
|
|
101
|
112
|
102 get_life_stage_index = function(life_stage, life_stage_nymph=NULL, life_stage_adult=NULL) {
|
|
103 # Name collection elements so that they
|
|
104 # are displayed in logical order.
|
|
105 if (life_stage=="Egg") {
|
|
106 lsi = "01";
|
|
107 } else if (life_stage=="Nymph") {
|
|
108 if (life_stage_nymph=="Young") {
|
|
109 lsi = "02";
|
|
110 } else if (life_stage_nymph=="Old") {
|
|
111 lsi = "03";
|
|
112 } else if (life_stage_nymph=="Total") {
|
|
113 lsi="04";
|
|
114 }
|
|
115 } else if (life_stage=="Adult") {
|
|
116 if (life_stage_adult=="Pre-vittelogenic") {
|
|
117 lsi = "05";
|
|
118 } else if (life_stage_adult=="Vittelogenic") {
|
|
119 lsi = "06";
|
|
120 } else if (life_stage_adult=="Diapausing") {
|
|
121 lsi = "07";
|
|
122 } else if (life_stage_adult=="Total") {
|
|
123 lsi = "08";
|
|
124 }
|
|
125 } else if (life_stage=="Total") {
|
|
126 lsi = "09";
|
|
127 }
|
|
128 return(lsi);
|
111
|
129 }
|
|
130
|
112
|
131 get_mean_and_std_error = function(p_replications, f1_replications, f2_replications) {
|
|
132 # P mean.
|
|
133 p_m = apply(p_replications, 1, mean);
|
|
134 # P standard error.
|
|
135 p_se = apply(p_replications, 1, sd) / sqrt(opt$replications);
|
|
136 # F1 mean.
|
|
137 f1_m = apply(f1_replications, 1, mean);
|
|
138 # F1 standard error.
|
|
139 f1_se = apply(f1_replications, 1, sd) / sqrt(opt$replications);
|
|
140 # F2 mean.
|
|
141 f2_m = apply(f2_replications, 1, mean);
|
|
142 # F2 standard error.
|
|
143 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications);
|
|
144 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))
|
|
145 }
|
111
|
146
|
112
|
147 get_next_normals_row = function(norm_data_frame, year, is_leap_year, index) {
|
|
148 # Return the next 30 year normals row formatted
|
|
149 # appropriately for the year-to-date data.
|
|
150 latitude = norm_data_frame[index,"LATITUDE"][1];
|
|
151 longitude = norm_data_frame[index,"LONGITUDE"][1];
|
|
152 # Format the date.
|
|
153 mmdd = norm_data_frame[index,"MMDD"][1];
|
|
154 date_str = paste(year, mmdd, sep="-");
|
|
155 doy = norm_data_frame[index,"DOY"][1];
|
|
156 if (!is_leap_year) {
|
|
157 # Since all normals data includes Feb 29, we have to
|
|
158 # subtract 1 from DOY if we're not in a leap year since
|
|
159 # we removed the Feb 29 row from the data frame above.
|
|
160 doy = as.integer(doy) - 1;
|
111
|
161 }
|
112
|
162 tmin = norm_data_frame[index,"TMIN"][1];
|
|
163 tmax = norm_data_frame[index,"TMAX"][1];
|
|
164 return(list(latitude, longitude, date_str, doy, tmin, tmax));
|
102
|
165 }
|
|
166
|
117
|
167 get_temperature_at_hour = function(latitude, temperature_data_frame, row) {
|
111
|
168 # Base development threshold for Brown Marmorated Stink Bug
|
85
|
169 # insect phenology model.
|
111
|
170 threshold = 14.17;
|
85
|
171 # Minimum temperature for current row.
|
111
|
172 curr_min_temp = temperature_data_frame$TMIN[row];
|
85
|
173 # Maximum temperature for current row.
|
111
|
174 curr_max_temp = temperature_data_frame$TMAX[row];
|
85
|
175 # Mean temperature for current row.
|
111
|
176 curr_mean_temp = 0.5 * (curr_min_temp + curr_max_temp);
|
85
|
177 # Initialize degree day accumulation
|
111
|
178 averages = 0;
|
102
|
179 if (curr_max_temp < threshold) {
|
111
|
180 averages = 0;
|
85
|
181 }
|
|
182 else {
|
|
183 # Initialize hourly temperature.
|
111
|
184 T = NULL;
|
85
|
185 # Initialize degree hour vector.
|
111
|
186 dh = NULL;
|
85
|
187 # Daylight length for current row.
|
111
|
188 y = temperature_data_frame$DAYLEN[row];
|
85
|
189 # Darkness length.
|
111
|
190 z = 24 - y;
|
85
|
191 # Lag coefficient.
|
111
|
192 a = 1.86;
|
85
|
193 # Darkness coefficient.
|
111
|
194 b = 2.20;
|
85
|
195 # Sunrise time.
|
111
|
196 risetime = 12 - y / 2;
|
85
|
197 # Sunset time.
|
111
|
198 settime = 12 + y / 2;
|
|
199 ts = (curr_max_temp - curr_min_temp) * sin(pi * (settime - 5) / (y + 2 * a)) + curr_min_temp;
|
85
|
200 for (i in 1:24) {
|
|
201 if (i > risetime && i < settime) {
|
|
202 # Number of hours after Tmin until sunset.
|
111
|
203 m = i - 5;
|
|
204 T[i] = (curr_max_temp - curr_min_temp) * sin(pi * m / (y + 2 * a)) + curr_min_temp;
|
85
|
205 if (T[i] < 8.4) {
|
111
|
206 dh[i] = 0;
|
85
|
207 }
|
|
208 else {
|
111
|
209 dh[i] = T[i] - 8.4;
|
85
|
210 }
|
|
211 }
|
96
|
212 else if (i > settime) {
|
111
|
213 n = i - settime;
|
|
214 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z);
|
85
|
215 if (T[i] < 8.4) {
|
111
|
216 dh[i] = 0;
|
85
|
217 }
|
|
218 else {
|
111
|
219 dh[i] = T[i] - 8.4;
|
85
|
220 }
|
|
221 }
|
|
222 else {
|
111
|
223 n = i + 24 - settime;
|
|
224 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z);
|
85
|
225 if (T[i] < 8.4) {
|
111
|
226 dh[i] = 0;
|
85
|
227 }
|
|
228 else {
|
111
|
229 dh[i] = T[i] - 8.4;
|
85
|
230 }
|
|
231 }
|
|
232 }
|
111
|
233 averages = sum(dh) / 24;
|
85
|
234 }
|
103
|
235 return(c(curr_mean_temp, averages))
|
85
|
236 }
|
|
237
|
112
|
238 get_tick_index = function(index, last_tick, ticks, month_labels) {
|
|
239 # The R code tries hard not to draw overlapping tick labels, and so
|
|
240 # will omit labels where they would abut or overlap previously drawn
|
|
241 # labels. This can result in, for example, every other tick being
|
|
242 # labelled. We'll keep track of the last tick to make sure all of
|
|
243 # the month labels are displayed, and missing ticks are restricted
|
|
244 # to Sundays which have no labels anyway.
|
|
245 if (last_tick==0) {
|
|
246 return(length(ticks)+1);
|
|
247 }
|
|
248 last_saved_tick = ticks[[length(ticks)]];
|
|
249 if (index-last_saved_tick<3) {
|
|
250 last_saved_month = month_labels[[length(month_labels)]];
|
|
251 if (last_saved_month=="") {
|
|
252 # We're safe overwriting a tick
|
|
253 # with no label (i.e., a Sunday tick).
|
|
254 return(length(ticks));
|
|
255 } else {
|
|
256 # Don't eliminate a Month label.
|
|
257 return(NULL);
|
|
258 }
|
|
259 }
|
|
260 return(length(ticks)+1);
|
|
261 }
|
|
262
|
|
263 get_total_days = function(is_leap_year) {
|
|
264 # Get the total number of days in the current year.
|
|
265 if (is_leap_year) {
|
|
266 return(366);
|
|
267 } else {
|
|
268 return(365);
|
|
269 }
|
|
270 }
|
|
271
|
122
|
272 get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval) {
|
|
273 # Generate a list of ticks and labels for plotting the
|
|
274 # x axis. There are several scenarios that affect this.
|
|
275 # 1. If restricted_date_interval is TRUE:
|
|
276 # a.
|
|
277 if (prepend_end_doy_norm > 0) {
|
|
278 prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm);
|
|
279 } else {
|
|
280 prepend_end_norm_row = 0;
|
|
281 }
|
|
282 if (append_start_doy_norm > 0) {
|
|
283 append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm);
|
|
284 } else {
|
|
285 append_start_norm_row = 0;
|
|
286 }
|
|
287 num_rows = dim(temperature_data_frame)[1];
|
112
|
288 month_labels = list();
|
|
289 ticks = list();
|
|
290 current_month_label = NULL;
|
|
291 last_tick = 0;
|
|
292 for (i in 1:num_rows) {
|
122
|
293 # We're plotting the entire year, so ticks will
|
|
294 # occur on Sundays and the first of each month.
|
|
295 if (i == prepend_end_norm_row) {
|
112
|
296 # Add a tick for the end of the 30 year normnals data
|
|
297 # that was prepended to the year-to-date data.
|
|
298 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
|
|
299 ticks[tick_index] = i;
|
|
300 month_labels[tick_index] = "End prepended 30 year normals";
|
|
301 last_tick = i;
|
122
|
302 } else if (i == append_start_doy_norm) {
|
112
|
303 # Add a tick for the start of the 30 year normnals data
|
|
304 # that was appended to the year-to-date data.
|
|
305 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
|
|
306 ticks[tick_index] = i;
|
|
307 month_labels[tick_index] = "Start appended 30 year normals";
|
|
308 last_tick = i;
|
|
309 } else if (i==num_rows) {
|
|
310 # Add a tick for the last day of the year.
|
|
311 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
|
|
312 ticks[tick_index] = i;
|
|
313 month_labels[tick_index] = "";
|
|
314 last_tick = i;
|
|
315 } else {
|
|
316 # Get the year and month from the date which
|
|
317 # has the format YYYY-MM-DD.
|
|
318 date = format(temperature_data_frame$DATE[i]);
|
|
319 # Get the month label.
|
|
320 items = strsplit(date, "-")[[1]];
|
|
321 month = items[2];
|
|
322 month_label = month.abb[as.integer(month)];
|
|
323 if (!identical(current_month_label, month_label)) {
|
122
|
324 # Add a tick for the month.
|
112
|
325 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
|
|
326 ticks[tick_index] = i;
|
|
327 month_labels[tick_index] = month_label;
|
|
328 current_month_label = month_label;
|
|
329 last_tick = i;
|
|
330 }
|
|
331 tick_index = get_tick_index(i, last_tick, ticks, month_labels)
|
|
332 if (!is.null(tick_index)) {
|
122
|
333 if (restricted_date_interval) {
|
|
334 # Add a tick for every day.
|
112
|
335 ticks[tick_index] = i;
|
|
336 # Add a blank month label so it is not displayed.
|
|
337 month_labels[tick_index] = "";
|
|
338 last_tick = i;
|
122
|
339 } else {
|
|
340 # Get the day.
|
|
341 day = weekdays(as.Date(date));
|
|
342 if (day=="Sunday") {
|
|
343 # Add a tick if we're on a Sunday.
|
|
344 ticks[tick_index] = i;
|
|
345 # Add a blank month label so it is not displayed.
|
|
346 month_labels[tick_index] = "";
|
|
347 last_tick = i;
|
|
348 }
|
112
|
349 }
|
|
350 }
|
|
351 }
|
|
352 }
|
|
353 return(list(ticks, month_labels));
|
|
354 }
|
|
355
|
|
356 is_leap_year = function(date_str) {
|
|
357 # Extract the year from the date_str.
|
|
358 date = format(date_str);
|
|
359 items = strsplit(date, "-")[[1]];
|
|
360 year = as.integer(items[1]);
|
|
361 if (((year %% 4 == 0) & (year %% 100 != 0)) | (year %% 400 == 0)) {
|
|
362 return(TRUE);
|
|
363 } else {
|
|
364 return(FALSE);
|
|
365 }
|
|
366 }
|
|
367
|
102
|
368 mortality.adult = function(temperature) {
|
|
369 if (temperature < 12.7) {
|
111
|
370 mortality.probability = 0.002;
|
102
|
371 }
|
|
372 else {
|
111
|
373 mortality.probability = temperature * 0.0005 + 0.02;
|
102
|
374 }
|
|
375 return(mortality.probability)
|
85
|
376 }
|
|
377
|
|
378 mortality.egg = function(temperature) {
|
|
379 if (temperature < 12.7) {
|
111
|
380 mortality.probability = 0.8;
|
85
|
381 }
|
|
382 else {
|
111
|
383 mortality.probability = 0.8 - temperature / 40.0;
|
102
|
384 if (mortality.probability < 0) {
|
111
|
385 mortality.probability = 0.01;
|
85
|
386 }
|
|
387 }
|
102
|
388 return(mortality.probability)
|
85
|
389 }
|
|
390
|
|
391 mortality.nymph = function(temperature) {
|
|
392 if (temperature < 12.7) {
|
111
|
393 mortality.probability = 0.03;
|
85
|
394 }
|
|
395 else {
|
111
|
396 mortality.probability = temperature * 0.0008 + 0.03;
|
85
|
397 }
|
111
|
398 return(mortality.probability);
|
102
|
399 }
|
|
400
|
122
|
401 parse_input_data = function(input_ytd, input_norm, location, start_date, end_date) {
|
|
402 # The end DOY for norm data prepended to ytd data.
|
|
403 prepend_end_doy_norm = 0;
|
|
404 # The start DOY for norm data appended to ytd data.
|
|
405 append_start_doy_norm = 0;
|
112
|
406 if (is.null(input_ytd)) {
|
122
|
407 # We're processing only the 30 year normals data.
|
|
408 processing_year_to_date_data = FALSE;
|
|
409 } else {
|
|
410 processing_year_to_date_data = TRUE;
|
|
411 }
|
|
412 if (is.null(start_date) && is.null(end_date)) {
|
|
413 # We're processing the entire year, possibly merging
|
|
414 # data from input_norm with data from input_ytd.
|
|
415 restricted_date_interval = FALSE;
|
|
416 } else {
|
|
417 restricted_date_interval = TRUE;
|
|
418 # Get the DOY for start_date and end_date.
|
|
419 start_date_doy = strftime(start_date, format="%j");
|
|
420 end_date_doy = strftime(end_date, format="%j");
|
|
421 }
|
|
422 # Read the input_norm temperature datafile into a data frame.
|
|
423 # The input_norm data has the following 10 columns:
|
|
424 # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX
|
|
425 norm_data_frame = read.csv(file=input_norm, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
|
|
426 # Set the norm_data_frame column names for access.
|
|
427 colnames(norm_data_frame) = c("STATIONID", "LATITUDE","LONGITUDE", "ELEV_M", "NAME", "ST", "MMDD", "DOY", "TMIN", "TMAX");
|
|
428 if (processing_year_to_date_data) {
|
|
429 # Read the input_ytd temperature data file into a data frame.
|
|
430 # The input_ytd data has the following 6 columns:
|
|
431 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
|
|
432 temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
|
|
433 # Set the temperature_data_frame column names for access.
|
|
434 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
|
|
435 if (restricted_date_interval) {
|
|
436 # We're plotting a date interval.
|
|
437 start_date_ytd_row = which(temperature_data_frame$DATE==start_date);
|
|
438 if (start_date_ytd_row > 0) {
|
|
439 # The start date is contained within the input_ytd data.
|
|
440 start_doy_ytd = as.integer(temperature_data_frame$DOY[start_date_ytd_row]);
|
|
441 } else {
|
|
442 # The start date is contained within the input_norm data.
|
|
443 start_date_norm_row = which(norm_data_frame$DATE==start_date);
|
|
444 }
|
|
445 end_date_ytd_row = which(temperature_data_frame$DATE==end_date);
|
|
446 if (end_date_ytd_row > 0) {
|
|
447 # The end date is contained within the input_ytd data.
|
|
448 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]);
|
|
449 }
|
|
450 date_str = start_date;
|
|
451 } else {
|
|
452 # We're plotting an entire year.
|
|
453 # Get the number of days contained in temperature_data_frame.
|
|
454 num_rows = dim(temperature_data_frame)[1];
|
|
455 # Get the start date and end date from temperature_data_frame.
|
|
456 start_date_ytd_row = 1;
|
|
457 start_date = temperature_data_frame$DATE[1];
|
|
458 end_date_ytd_row = num_rows;
|
|
459 end_date = temperature_data_frame$DATE[num_rows];
|
|
460 date_str = format(start_date);
|
|
461 # Save the first DOY to later check if start_date is Jan 1.
|
|
462 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]);
|
|
463 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_rows]);
|
|
464 }
|
|
465 # Extract the year from the start date.
|
|
466 date_str_items = strsplit(date_str, "-")[[1]];
|
|
467 year = date_str_items[1];
|
|
468 } else {
|
|
469 # We're processing only the 30 year normals data, so create an empty
|
112
|
470 # data frame for containing temperature data after it is converted
|
|
471 # from the 30 year normals format to the year-to-date format.
|
|
472 temperature_data_frame = data.frame(matrix(ncol=6, nrow=0));
|
|
473 colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
|
122
|
474 if (restricted_date_interval) {
|
|
475 # We're plotting a date interval.
|
|
476 # Extract the year, month and day from the start date.
|
117
|
477 start_date_str = format(start_date);
|
|
478 start_date_str_items = strsplit(start_date_str, "-")[[1]];
|
122
|
479 year = start_date_str_items[1];
|
117
|
480 start_date_month = start_date_str_items[2];
|
|
481 start_date_day = start_date_str_items[3];
|
|
482 start_date = paste(year, start_date_month, start_date_day, sep="-");
|
|
483 # Extract the month and day from the end date.
|
|
484 end_date_str = format(start_date);
|
|
485 end_date_str_items = strsplit(end_date_str, "-")[[1]];
|
|
486 end_date_month = end_date_str_items[2];
|
|
487 end_date_day = end_date_str_items[3];
|
|
488 end_date = paste(year, start_date_month, start_date_day, sep="-");
|
122
|
489 } else {
|
|
490 # We're plotting an entire year.
|
|
491 # Base all dates on the current date since 30 year
|
|
492 # normals data does not include any dates.
|
|
493 year = format(Sys.Date(), "%Y");
|
|
494 start_date = paste(year, "01", "01", sep="-");
|
|
495 end_date = paste(year, "12", "31", sep="-");
|
117
|
496 }
|
112
|
497 }
|
|
498 # See if we're in a leap year.
|
|
499 is_leap_year = is_leap_year(start_date);
|
|
500 # All normals data includes Feb 29 which is row 60 in
|
|
501 # the data, so delete that row if we're not in a leap year.
|
|
502 if (!is_leap_year) {
|
|
503 norm_data_frame = norm_data_frame[-c(60),];
|
102
|
504 }
|
122
|
505 # Set the location to be the station name if the user elected not to enter it.
|
|
506 if (is.null(location) | length(location) == 0) {
|
112
|
507 location = norm_data_frame$NAME[1];
|
|
508 }
|
122
|
509 if (processing_year_to_date_data) {
|
|
510 # Merge the year-to-date data with the 30 year normals data.
|
|
511 if (restricted_date_interval) {
|
|
512 # The values of start_date_ytd_row and end_date_ytd_row were set above.
|
|
513 if (start_date_ytd_row > 0 & end_date_ytd_row > 0) {
|
|
514 # The date interval is contained within the input_ytd
|
|
515 # data, so we don't need to merge the 30 year normals data.
|
|
516 temperature_data_frame = temperature_data_frame[start_date_ytd_row:end_date_ytd_row,];
|
|
517 } else if (start_date_ytd_row == 0 & end_date_ytd_row > 0) {
|
|
518 # The date interval starts in input_norm and ends in
|
|
519 # input_ytd, so prepend appropriate rows from input_norm
|
|
520 # to appropriate rows from input_ytd.
|
|
521 first_norm_row = which(norm_data_frame$DOY==start_date_doy);
|
|
522 # Get the first DOY from temperature_data_frame.
|
|
523 first_ytd_doy = temperature_data_frame$DOY[1];
|
|
524 # End DOY of input_norm data prepended to input_ytd.
|
|
525 prepend_end_doy_norm = first_ytd_doy - 1;
|
|
526 # Get the number of rows for the restricted date interval
|
|
527 # that are contained in temperature_data_frame.
|
|
528 temperature_data_frame_rows = end_date_ytd_row;
|
|
529 # Get the last row needed from the 30 year normals data.
|
|
530 last_norm_row = which(norm_data_frame$DOY==prepend_end_doy_norm);
|
|
531 # Get the number of rows for the restricted date interval
|
|
532 # that are contained in norm_data_frame.
|
|
533 norm_data_frame_rows = last_norm_row - first_norm_row;
|
|
534 # Create a temporary data frame to contain the 30 year normals
|
|
535 # data from the start date to the date immediately prior to the
|
|
536 # first row of the input_ytd data.
|
|
537 tmp_norm_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows));
|
|
538 for (i in first_norm_row:last_norm_row) {
|
|
539 # Populate the temp_data_frame row with
|
|
540 # values from norm_data_frame.
|
|
541 tmp_norm_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
|
|
542 }
|
|
543 # Create a second temporary data frame containing the
|
|
544 # appropriate rows from temperature_data_frame.
|
|
545 tmp_temperature_data_frame = temperature_data_frame[1:first_norm_row-1,];
|
|
546 # Merge the 2 temporary data frames.
|
|
547 temperature_data_frame = rbind(tmp_norm_data_frame, tmp_temperature_data_frame);
|
|
548 } else if (start_date_ytd_row > 0 & end_date_ytd_row == 0) {
|
|
549 # The date interval starts in input_ytd and ends in input_norm,
|
|
550 # so append appropriate rows from input_norm to appropriate rows
|
|
551 # from input_ytd.
|
|
552 num_rows = dim(temperature_data_frame)[1];
|
|
553 # Get the number of rows for the restricted date interval
|
|
554 # that are contained in temperature_data_frame.
|
|
555 temperature_data_frame_rows = num_rows - start_date_ytd_row
|
|
556 # Get the DOY of the last row in the input_ytd data.
|
|
557 last_ytd_doy = temperature_data_frame$DOY[num_rows];
|
|
558 # Get the DOYs for the first and last rows from norm_data_frame
|
|
559 # that will be appended to temperature_data_frame.
|
|
560 append_start_doy_norm = last_ytd_doy + 1;
|
|
561 # Get the row from norm_data_frame containing first_norm_doy.
|
|
562 first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm);
|
|
563 # Get the row from norm_data_frame containing end_date_doy.
|
|
564 last_norm_row = which(norm_data_frame$DOY == end_date_doy);
|
|
565 # Get the number of rows for the restricted date interval
|
|
566 # that are contained in norm_data_frame.
|
|
567 norm_data_frame_rows = last_norm_row - first_norm_row;
|
|
568 # Create a temporary data frame to contain the data
|
|
569 # taken from both temperateu_data_frame and norm_data_frame
|
|
570 # for the date interval.
|
|
571 tmp_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows));
|
|
572 # Populate tmp_data_frame with the appropriate rows from temperature_data_frame.
|
|
573 tmp_data_frame[temperature_data_frame_rows,] = temperature_data_frame[start_date_ytd_row:temperature_data_frame_rows,];
|
|
574 # Apppend the appropriate rows from norm_data_frame to tmp_data_frame.
|
|
575 for (i in first_norm_row:last_norm_row) {
|
|
576 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
|
|
577 }
|
|
578 temperature_data_frame = tmp_data_frame[,];
|
|
579 } else if (start_date_ytd_row == 0 & end_date_ytd_row == 0) {
|
|
580 # The date interval is contained witin input_norm.
|
|
581 temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy);
|
|
582 }
|
|
583 } else {
|
|
584 # We're plotting an entire year.
|
|
585 if (start_doy_ytd > 1) {
|
|
586 # The input_ytd data starts after Jan 1, so prepend
|
|
587 # appropriate rows from input_norm to temperature_data_frame.
|
|
588 prepend_end_doy_norm = start_doy_ytd - 1;
|
|
589 first_norm_row = 1;
|
|
590 last_norm_row = which(norm_data_frame$DOY == prepend_end_doy_norm);
|
|
591 # Create a temporary data frame to contain the input_norm data
|
|
592 # from Jan 1 to the date immediately prior to start_date.
|
|
593 tmp_data_frame = temperature_data_frame[FALSE,];
|
|
594 # Populate tmp_data_frame with appropriate rows from norm_data_frame.
|
|
595 for (i in first_norm_row:last_norm_row) {
|
|
596 tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
|
|
597 }
|
|
598 # Merge the temporary data frame with temperature_data_frame.
|
|
599 temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame);
|
|
600 }
|
|
601 # Set the value of total_days.
|
|
602 total_days = get_total_days(is_leap_year);
|
|
603 if (end_doy_ytd < total_days) {
|
|
604 # Define the next row for the year-to-date data from the 30 year normals data.
|
|
605 append_start_doy_norm = end_doy_ytd + 1;
|
|
606 first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm);
|
|
607 last_norm_row = which(norm_data_frame$DOY == total_days);
|
|
608 # Append the 30 year normals data to the year-to-date data.
|
|
609 for (i in first_norm_row:last_norm_row) {
|
|
610 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
|
|
611 }
|
|
612 }
|
112
|
613 }
|
|
614 } else {
|
122
|
615 # We're processing only the 30 year normals data.
|
|
616 if (restricted_date_interval) {
|
|
617 # Populate temperature_data_frame from norm_data_frame.
|
|
618 temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy);
|
|
619 } else {
|
|
620 total_days = get_total_days(is_leap_year);
|
|
621 for (i in 1:total_days) {
|
|
622 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
|
112
|
623 }
|
|
624 }
|
|
625 }
|
|
626 # Add a column containing the daylight length for each day.
|
122
|
627 temperature_data_frame = add_daylight_length(temperature_data_frame);
|
|
628 return(list(temperature_data_frame, start_date, end_date, prepend_end_doy_norm, append_start_doy_norm, is_leap_year, location));
|
85
|
629 }
|
|
630
|
112
|
631 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,
|
|
632 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,
|
|
633 life_stages_adult=NULL, life_stages_nymph=NULL) {
|
|
634 if (chart_type=="pop_size_by_life_stage") {
|
|
635 if (life_stage=="Total") {
|
|
636 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
637 legend_text = c("Egg", "Nymph", "Adult");
|
|
638 columns = c(4, 2, 1);
|
|
639 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);
|
|
640 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
|
|
641 lines(days, group2, lwd=2, lty=1, col=2);
|
|
642 lines(days, group3, lwd=2, lty=1, col=4);
|
|
643 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);
|
|
644 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
|
|
645 if (plot_std_error=="yes") {
|
|
646 # Standard error for group.
|
|
647 lines(days, group+group_std_error, lty=2);
|
|
648 lines(days, group-group_std_error, lty=2);
|
|
649 # Standard error for group2.
|
|
650 lines(days, group2+group2_std_error, col=2, lty=2);
|
|
651 lines(days, group2-group2_std_error, col=2, lty=2);
|
|
652 # Standard error for group3.
|
|
653 lines(days, group3+group3_std_error, col=4, lty=2);
|
|
654 lines(days, group3-group3_std_error, col=4, lty=2);
|
|
655 }
|
|
656 } else {
|
|
657 if (life_stage=="Egg") {
|
|
658 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
659 legend_text = c(life_stage);
|
|
660 columns = c(4);
|
|
661 } else if (life_stage=="Nymph") {
|
|
662 stage = paste(life_stages_nymph, "Nymph Pop :", sep=" ");
|
|
663 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
664 legend_text = c(paste(life_stages_nymph, life_stage, sep=" "));
|
|
665 columns = c(2);
|
|
666 } else if (life_stage=="Adult") {
|
|
667 stage = paste(life_stages_adult, "Adult Pop", sep=" ");
|
|
668 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
669 legend_text = c(paste(life_stages_adult, life_stage, sep=" "));
|
|
670 columns = c(1);
|
|
671 }
|
|
672 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);
|
|
673 legend("topleft", legend_text, lty=c(1), col="black", cex=3);
|
|
674 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);
|
|
675 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
|
|
676 if (plot_std_error=="yes") {
|
|
677 # Standard error for group.
|
|
678 lines(days, group+group_std_error, lty=2);
|
|
679 lines(days, group-group_std_error, lty=2);
|
|
680 }
|
|
681 }
|
|
682 } else if (chart_type=="pop_size_by_generation") {
|
|
683 if (life_stage=="Total") {
|
|
684 title_str = ": Total Pop by Gen :";
|
|
685 } else if (life_stage=="Egg") {
|
|
686 title_str = ": Egg Pop by Gen :";
|
|
687 } else if (life_stage=="Nymph") {
|
|
688 title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" ");
|
|
689 } else if (life_stage=="Adult") {
|
|
690 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" ");
|
|
691 }
|
|
692 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
111
|
693 legend_text = c("P", "F1", "F2");
|
|
694 columns = c(1, 2, 4);
|
112
|
695 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);
|
|
696 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
|
|
697 lines(days, group2, lwd=2, lty=1, col=2);
|
|
698 lines(days, group3, lwd=2, lty=1, col=4);
|
|
699 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);
|
|
700 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
|
|
701 if (plot_std_error=="yes") {
|
|
702 # Standard error for group.
|
|
703 lines(days, group+group_std_error, lty=2);
|
|
704 lines(days, group-group_std_error, lty=2);
|
|
705 # Standard error for group2.
|
|
706 lines(days, group2+group2_std_error, col=2, lty=2);
|
|
707 lines(days, group2-group2_std_error, col=2, lty=2);
|
|
708 # Standard error for group3.
|
|
709 lines(days, group3+group3_std_error, col=4, lty=2);
|
|
710 lines(days, group3-group3_std_error, col=4, lty=2);
|
|
711 }
|
85
|
712 }
|
|
713 }
|
|
714
|
117
|
715 stop_err = function(msg) {
|
|
716 cat(msg, file=stderr());
|
|
717 quit(save="no", status=1);
|
|
718 }
|
|
719
|
|
720 validate_date = function(date_str) {
|
|
721 valid_date = as.Date(date_str, format="%Y-%m-%d");
|
|
722 if( class(valid_date)=="try-error" || is.na(valid_date)) {
|
|
723 msg = paste("Invalid date: ", date_str, ", valid date format is yyyy-mm-dd.", sep="");
|
|
724 stop_err(msg);
|
|
725 }
|
|
726 return(valid_date);
|
|
727 }
|
|
728
|
122
|
729 # Parse the inputs.
|
|
730 data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$location, opt$start_date, opt$end_date);
|
|
731 temperature_data_frame = data_list[[1]];
|
|
732 # Information needed for plots, some of these values are
|
|
733 # being reset here since in some case they were set above.
|
|
734 start_date = data_list[[2]];
|
|
735 end_date = data_list[[3]];
|
|
736 prepend_end_doy_norm = data_list[[4]];
|
|
737 append_start_doy_norm = data_list[[5]];
|
|
738 is_leap_year = data_list[[6]];
|
|
739 location = data_list[[7]];
|
|
740
|
|
741 if (is.null(input_ytd)) {
|
|
742 processing_year_to_date_data = FALSE;
|
|
743 } else {
|
|
744 processing_year_to_date_data = TRUE;
|
|
745 }
|
112
|
746 # Determine if we're plotting generations separately.
|
|
747 if (opt$plot_generations_separately=="yes") {
|
|
748 plot_generations_separately = TRUE;
|
|
749 } else {
|
|
750 plot_generations_separately = FALSE;
|
|
751 }
|
117
|
752 if (is.null(opt$start_date) && is.null(opt$end_date)) {
|
122
|
753 # We're plotting an entire year.
|
|
754 restricted_date_interval = FALSE;
|
|
755 # Display the total number of days in the Galaxy history item blurb.
|
|
756 if (processing_year_to_date_data) {
|
|
757 cat("Number of days year-to-date: ", opt$num_days_ytd, "\n");
|
|
758 } else {
|
|
759 if (is_leap_year) {
|
|
760 num_days = 366;
|
|
761 } else {
|
|
762 num_days = 365;
|
|
763 }
|
|
764 cat("Number of days in year: ", num_days, "\n");
|
|
765 }
|
117
|
766 } else {
|
|
767 # FIXME: currently custom date fields are free text, but
|
|
768 # Galaxy should soon include support for a date selector
|
|
769 # at which point this tool should be enhanced to use it.
|
|
770 # Validate start_date.
|
122
|
771 restricted_date_interval = TRUE;
|
|
772 # Calaculate the number of days in the date interval rather
|
|
773 # than using the number of rows in the input temperature data.
|
|
774 start_date = validate_date(opt$start_date);
|
117
|
775 # Validate end_date.
|
|
776 end_date = validate_date(opt$end_date);
|
|
777 if (start_date >= end_date) {
|
122
|
778 stop_err("The start date must be between 1 and 50 days before the end date when setting date intervals for plots.");
|
117
|
779 }
|
122
|
780 # Calculate the number of days in the date interval.
|
117
|
781 num_days = difftime(start_date, end_date, units=c("days"));
|
|
782 if (num_days > 50) {
|
122
|
783 # We need to restrict date intervals since
|
117
|
784 # plots render tick marks for each day.
|
122
|
785 stop_err("Date intervals for plotting cannot exceed 50 days.");
|
117
|
786 }
|
122
|
787 # Display the total number of days in the Galaxy history item blurb.
|
|
788 cat("Number of days in date interval: ", num_days, "\n");
|
117
|
789 }
|
112
|
790
|
|
791 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately.
|
|
792 if (plot_generations_separately) {
|
|
793 temperature_data_frame_P = data.frame(temperature_data_frame);
|
|
794 temperature_data_frame_F1 = data.frame(temperature_data_frame);
|
|
795 temperature_data_frame_F2 = data.frame(temperature_data_frame);
|
|
796 }
|
|
797
|
|
798 # Get the ticks date labels for plots.
|
122
|
799 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval);
|
112
|
800 ticks = c(unlist(ticks_and_labels[1]));
|
|
801 date_labels = c(unlist(ticks_and_labels[2]));
|
|
802 # All latitude values are the same, so get the value for plots from the first row.
|
111
|
803 latitude = temperature_data_frame$LATITUDE[1];
|
112
|
804
|
|
805 # Determine the specified life stages for processing.
|
|
806 # Split life_stages into a list of strings for plots.
|
|
807 life_stages_str = as.character(opt$life_stages);
|
|
808 life_stages = strsplit(life_stages_str, ",")[[1]];
|
85
|
809
|
112
|
810 # Determine the data we need to generate for plotting.
|
|
811 process_eggs = FALSE;
|
|
812 process_nymphs = FALSE;
|
|
813 process_young_nymphs = FALSE;
|
|
814 process_old_nymphs = FALSE;
|
|
815 process_total_nymphs = FALSE;
|
|
816 process_adults = FALSE;
|
|
817 process_previttelogenic_adults = FALSE;
|
|
818 process_vittelogenic_adults = FALSE;
|
|
819 process_diapausing_adults = FALSE;
|
|
820 process_total_adults = FALSE;
|
|
821 for (life_stage in life_stages) {
|
|
822 if (life_stage=="Total") {
|
|
823 process_eggs = TRUE;
|
|
824 process_nymphs = TRUE;
|
|
825 process_adults = TRUE;
|
|
826 } else if (life_stage=="Egg") {
|
|
827 process_eggs = TRUE;
|
|
828 } else if (life_stage=="Nymph") {
|
|
829 process_nymphs = TRUE;
|
|
830 } else if (life_stage=="Adult") {
|
|
831 process_adults = TRUE;
|
|
832 }
|
|
833 }
|
|
834 if (process_nymphs) {
|
|
835 # Split life_stages_nymph into a list of strings for plots.
|
|
836 life_stages_nymph_str = as.character(opt$life_stages_nymph);
|
|
837 life_stages_nymph = strsplit(life_stages_nymph_str, ",")[[1]];
|
|
838 for (life_stage_nymph in life_stages_nymph) {
|
|
839 if (life_stage_nymph=="Young") {
|
|
840 process_young_nymphs = TRUE;
|
|
841 } else if (life_stage_nymph=="Old") {
|
|
842 process_old_nymphs = TRUE;
|
|
843 } else if (life_stage_nymph=="Total") {
|
|
844 process_total_nymphs = TRUE;
|
|
845 }
|
|
846 }
|
|
847 }
|
|
848 if (process_adults) {
|
|
849 # Split life_stages_adult into a list of strings for plots.
|
|
850 life_stages_adult_str = as.character(opt$life_stages_adult);
|
|
851 life_stages_adult = strsplit(life_stages_adult_str, ",")[[1]];
|
|
852 for (life_stage_adult in life_stages_adult) {
|
|
853 if (life_stage_adult=="Pre-vittelogenic") {
|
|
854 process_previttelogenic_adults = TRUE;
|
|
855 } else if (life_stage_adult=="Vittelogenic") {
|
|
856 process_vittelogenic_adults = TRUE;
|
|
857 } else if (life_stage_adult=="Diapausing") {
|
|
858 process_diapausing_adults = TRUE;
|
|
859 } else if (life_stage_adult=="Total") {
|
|
860 process_total_adults = TRUE;
|
|
861 }
|
|
862 }
|
|
863 }
|
97
|
864 # Initialize matrices.
|
122
|
865 total_days = dim(temperature_data_frame)[1];
|
112
|
866 if (process_eggs) {
|
|
867 Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
868 }
|
|
869 if (process_young_nymphs | process_total_nymphs) {
|
|
870 YoungNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
871 }
|
|
872 if (process_old_nymphs | process_total_nymphs) {
|
|
873 OldNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
874 }
|
|
875 if (process_previttelogenic_adults | process_total_adults) {
|
|
876 Previttelogenic.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
877 }
|
|
878 if (process_vittelogenic_adults | process_total_adults) {
|
|
879 Vittelogenic.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
880 }
|
|
881 if (process_diapausing_adults | process_total_adults) {
|
|
882 Diapausing.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
883 }
|
|
884 newborn.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
885 adult.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
886 death.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
887 if (plot_generations_separately) {
|
|
888 # P is Parental, or overwintered adults.
|
|
889 P.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
890 # F1 is the first field-produced generation.
|
|
891 F1.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
892 # F2 is the second field-produced generation.
|
|
893 F2.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
894 if (process_eggs) {
|
|
895 P_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
896 F1_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
897 F2_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
898 }
|
|
899 if (process_young_nymphs) {
|
|
900 P_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
901 F1_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
902 F2_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
903 }
|
|
904 if (process_old_nymphs) {
|
|
905 P_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
906 F1_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
907 F2_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
908 }
|
|
909 if (process_total_nymphs) {
|
|
910 P_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
911 F1_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
912 F2_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
913 }
|
|
914 if (process_previttelogenic_adults) {
|
|
915 P_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
916 F1_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
917 F2_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
918 }
|
|
919 if (process_vittelogenic_adults) {
|
|
920 P_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
921 F1_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
922 F2_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
923 }
|
|
924 if (process_diapausing_adults) {
|
|
925 P_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
926 F1_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
927 F2_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
928 }
|
|
929 if (process_total_adults) {
|
|
930 P_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
931 F1_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
932 F2_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
933 }
|
|
934 }
|
|
935 # Total population.
|
|
936 population.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
103
|
937
|
102
|
938 # Process replications.
|
112
|
939 for (current_replication in 1:opt$replications) {
|
109
|
940 # Start with the user-defined number of insects per replication.
|
111
|
941 num_insects = opt$insects_per_replication;
|
90
|
942 # Generation, Stage, degree-days, T, Diapause.
|
111
|
943 vector.ini = c(0, 3, 0, 0, 0);
|
112
|
944 # Replicate to create a matrix where the columns are
|
|
945 # Generation, Stage, degree-days, T, Diapause and the
|
|
946 # rows are the initial number of insects per replication.
|
111
|
947 vector.matrix = rep(vector.ini, num_insects);
|
112
|
948 # Complete transposed matrix for the population, so now
|
|
949 # the rows are Generation, Stage, degree-days, T, Diapause
|
111
|
950 vector.matrix = base::t(matrix(vector.matrix, nrow=5));
|
85
|
951 # Time series of population size.
|
112
|
952 if (process_eggs) {
|
|
953 Eggs = rep(0, total_days);
|
|
954 }
|
|
955 if (process_young_nymphs | process_total_nymphs) {
|
|
956 YoungNymphs = rep(0, total_days);
|
|
957 }
|
|
958 if (process_old_nymphs | process_total_nymphs) {
|
|
959 OldNymphs = rep(0, total_days);
|
|
960 }
|
|
961 if (process_previttelogenic_adults | process_total_adults) {
|
|
962 Previttelogenic = rep(0, total_days);
|
|
963 }
|
|
964 if (process_vittelogenic_adults | process_total_adults) {
|
|
965 Vittelogenic = rep(0, total_days);
|
|
966 }
|
|
967 if (process_diapausing_adults | process_total_adults) {
|
|
968 Diapausing = rep(0, total_days);
|
|
969 }
|
|
970 N.newborn = rep(0, total_days);
|
|
971 N.adult = rep(0, total_days);
|
|
972 N.death = rep(0, total_days);
|
|
973 overwintering_adult.population = rep(0, total_days);
|
|
974 first_generation.population = rep(0, total_days);
|
|
975 second_generation.population = rep(0, total_days);
|
|
976 if (plot_generations_separately) {
|
|
977 # P is Parental, or overwintered adults.
|
|
978 # F1 is the first field-produced generation.
|
|
979 # F2 is the second field-produced generation.
|
|
980 if (process_eggs) {
|
|
981 P.egg = rep(0, total_days);
|
|
982 F1.egg = rep(0, total_days);
|
|
983 F2.egg = rep(0, total_days);
|
|
984 }
|
|
985 if (process_young_nymphs) {
|
|
986 P.young_nymph = rep(0, total_days);
|
|
987 F1.young_nymph = rep(0, total_days);
|
|
988 F2.young_nymph = rep(0, total_days);
|
|
989 }
|
|
990 if (process_old_nymphs) {
|
|
991 P.old_nymph = rep(0, total_days);
|
|
992 F1.old_nymph = rep(0, total_days);
|
|
993 F2.old_nymph = rep(0, total_days);
|
|
994 }
|
|
995 if (process_total_nymphs) {
|
|
996 P.total_nymph = rep(0, total_days);
|
|
997 F1.total_nymph = rep(0, total_days);
|
|
998 F2.total_nymph = rep(0, total_days);
|
|
999 }
|
|
1000 if (process_previttelogenic_adults) {
|
|
1001 P.previttelogenic_adult = rep(0, total_days);
|
|
1002 F1.previttelogenic_adult = rep(0, total_days);
|
|
1003 F2.previttelogenic_adult = rep(0, total_days);
|
|
1004 }
|
|
1005 if (process_vittelogenic_adults) {
|
|
1006 P.vittelogenic_adult = rep(0, total_days);
|
|
1007 F1.vittelogenic_adult = rep(0, total_days);
|
|
1008 F2.vittelogenic_adult = rep(0, total_days);
|
|
1009 }
|
|
1010 if (process_diapausing_adults) {
|
|
1011 P.diapausing_adult = rep(0, total_days);
|
|
1012 F1.diapausing_adult = rep(0, total_days);
|
|
1013 F2.diapausing_adult = rep(0, total_days);
|
|
1014 }
|
|
1015 if (process_total_adults) {
|
|
1016 P.total_adult = rep(0, total_days);
|
|
1017 F1.total_adult = rep(0, total_days);
|
|
1018 F2.total_adult = rep(0, total_days);
|
|
1019 }
|
|
1020 }
|
111
|
1021 total.population = NULL;
|
112
|
1022 averages.day = rep(0, total_days);
|
|
1023 # All the days included in the input_ytd temperature dataset.
|
|
1024 for (row in 1:total_days) {
|
85
|
1025 # Get the integer day of the year for the current row.
|
111
|
1026 doy = temperature_data_frame$DOY[row];
|
85
|
1027 # Photoperiod in the day.
|
111
|
1028 photoperiod = temperature_data_frame$DAYLEN[row];
|
117
|
1029 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row);
|
111
|
1030 mean.temp = temp.profile[1];
|
|
1031 averages.temp = temp.profile[2];
|
|
1032 averages.day[row] = averages.temp;
|
85
|
1033 # Trash bin for death.
|
111
|
1034 death.vector = NULL;
|
85
|
1035 # Newborn.
|
111
|
1036 birth.vector = NULL;
|
85
|
1037 # All individuals.
|
92
|
1038 for (i in 1:num_insects) {
|
103
|
1039 # Individual record.
|
111
|
1040 vector.individual = vector.matrix[i,];
|
103
|
1041 # Adjustment for late season mortality rate (still alive?).
|
85
|
1042 if (latitude < 40.0) {
|
111
|
1043 post.mortality = 1;
|
|
1044 day.kill = 300;
|
85
|
1045 }
|
|
1046 else {
|
111
|
1047 post.mortality = 2;
|
|
1048 day.kill = 250;
|
85
|
1049 }
|
102
|
1050 if (vector.individual[2] == 0) {
|
85
|
1051 # Egg.
|
111
|
1052 death.probability = opt$egg_mortality * mortality.egg(mean.temp);
|
85
|
1053 }
|
102
|
1054 else if (vector.individual[2] == 1 | vector.individual[2] == 2) {
|
112
|
1055 # Nymph.
|
111
|
1056 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp);
|
85
|
1057 }
|
102
|
1058 else if (vector.individual[2] == 3 | vector.individual[2] == 4 | vector.individual[2] == 5) {
|
103
|
1059 # Adult.
|
85
|
1060 if (doy < day.kill) {
|
111
|
1061 death.probability = opt$adult_mortality * mortality.adult(mean.temp);
|
85
|
1062 }
|
|
1063 else {
|
|
1064 # Increase adult mortality after fall equinox.
|
111
|
1065 death.probability = opt$adult_mortality * post.mortality * mortality.adult(mean.temp);
|
85
|
1066 }
|
|
1067 }
|
103
|
1068 # Dependent on temperature and life stage?
|
111
|
1069 u.d = runif(1);
|
102
|
1070 if (u.d < death.probability) {
|
111
|
1071 death.vector = c(death.vector, i);
|
96
|
1072 }
|
85
|
1073 else {
|
103
|
1074 # End of diapause.
|
102
|
1075 if (vector.individual[1] == 0 && vector.individual[2] == 3) {
|
112
|
1076 # Overwintering adult (pre-vittelogenic).
|
102
|
1077 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) {
|
85
|
1078 # Add 68C to become fully reproductively matured.
|
|
1079 # Transfer to vittelogenic.
|
111
|
1080 vector.individual = c(0, 4, 0, 0, 0);
|
|
1081 vector.matrix[i,] = vector.individual;
|
85
|
1082 }
|
|
1083 else {
|
112
|
1084 # Add average temperature for current day.
|
111
|
1085 vector.individual[3] = vector.individual[3] + averages.temp;
|
85
|
1086 # Add 1 day in current stage.
|
111
|
1087 vector.individual[4] = vector.individual[4] + 1;
|
|
1088 vector.matrix[i,] = vector.individual;
|
85
|
1089 }
|
|
1090 }
|
102
|
1091 if (vector.individual[1] != 0 && vector.individual[2] == 3) {
|
112
|
1092 # Not overwintering adult (pre-vittelogenic).
|
111
|
1093 current.gen = vector.individual[1];
|
102
|
1094 if (vector.individual[3] > 68) {
|
85
|
1095 # Add 68C to become fully reproductively matured.
|
|
1096 # Transfer to vittelogenic.
|
111
|
1097 vector.individual = c(current.gen, 4, 0, 0, 0);
|
|
1098 vector.matrix[i,] = vector.individual;
|
85
|
1099 }
|
|
1100 else {
|
103
|
1101 # Add average temperature for current day.
|
111
|
1102 vector.individual[3] = vector.individual[3] + averages.temp;
|
85
|
1103 # Add 1 day in current stage.
|
111
|
1104 vector.individual[4] = vector.individual[4] + 1;
|
|
1105 vector.matrix[i,] = vector.individual;
|
85
|
1106 }
|
|
1107 }
|
109
|
1108 # Oviposition -- where population dynamics comes from.
|
102
|
1109 if (vector.individual[2] == 4 && vector.individual[1] == 0 && mean.temp > 10) {
|
85
|
1110 # Vittelogenic stage, overwintering generation.
|
102
|
1111 if (vector.individual[4] == 0) {
|
85
|
1112 # Just turned in vittelogenic stage.
|
111
|
1113 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size));
|
85
|
1114 }
|
|
1115 else {
|
|
1116 # Daily probability of birth.
|
111
|
1117 p.birth = opt$oviposition * 0.01;
|
|
1118 u1 = runif(1);
|
85
|
1119 if (u1 < p.birth) {
|
111
|
1120 num_insects.birth = round(runif(1, 2, 8));
|
85
|
1121 }
|
|
1122 }
|
103
|
1123 # Add average temperature for current day.
|
111
|
1124 vector.individual[3] = vector.individual[3] + averages.temp;
|
85
|
1125 # Add 1 day in current stage.
|
111
|
1126 vector.individual[4] = vector.individual[4] + 1;
|
|
1127 vector.matrix[i,] = vector.individual;
|
90
|
1128 if (num_insects.birth > 0) {
|
85
|
1129 # Add new birth -- might be in different generations.
|
111
|
1130 new.gen = vector.individual[1] + 1;
|
85
|
1131 # Egg profile.
|
111
|
1132 new.individual = c(new.gen, 0, 0, 0, 0);
|
|
1133 new.vector = rep(new.individual, num_insects.birth);
|
85
|
1134 # Update batch of egg profile.
|
111
|
1135 new.vector = t(matrix(new.vector, nrow=5));
|
85
|
1136 # Group with total eggs laid in that day.
|
111
|
1137 birth.vector = rbind(birth.vector, new.vector);
|
85
|
1138 }
|
|
1139 }
|
109
|
1140 # Oviposition -- for generation 1.
|
102
|
1141 if (vector.individual[2] == 4 && vector.individual[1] == 1 && mean.temp > 12.5 && doy < 222) {
|
85
|
1142 # Vittelogenic stage, 1st generation
|
102
|
1143 if (vector.individual[4] == 0) {
|
85
|
1144 # Just turned in vittelogenic stage.
|
111
|
1145 num_insects.birth = round(runif(1, 2+opt$min_clutch_size, 8+opt$max_clutch_size));
|
85
|
1146 }
|
|
1147 else {
|
|
1148 # Daily probability of birth.
|
111
|
1149 p.birth = opt$oviposition * 0.01;
|
|
1150 u1 = runif(1);
|
85
|
1151 if (u1 < p.birth) {
|
111
|
1152 num_insects.birth = round(runif(1, 2, 8));
|
85
|
1153 }
|
|
1154 }
|
103
|
1155 # Add average temperature for current day.
|
111
|
1156 vector.individual[3] = vector.individual[3] + averages.temp;
|
85
|
1157 # Add 1 day in current stage.
|
111
|
1158 vector.individual[4] = vector.individual[4] + 1;
|
|
1159 vector.matrix[i,] = vector.individual;
|
90
|
1160 if (num_insects.birth > 0) {
|
85
|
1161 # Add new birth -- might be in different generations.
|
111
|
1162 new.gen = vector.individual[1] + 1;
|
85
|
1163 # Egg profile.
|
111
|
1164 new.individual = c(new.gen, 0, 0, 0, 0);
|
|
1165 new.vector = rep(new.individual, num_insects.birth);
|
85
|
1166 # Update batch of egg profile.
|
111
|
1167 new.vector = t(matrix(new.vector, nrow=5));
|
85
|
1168 # Group with total eggs laid in that day.
|
111
|
1169 birth.vector = rbind(birth.vector, new.vector);
|
85
|
1170 }
|
|
1171 }
|
109
|
1172 # Egg to young nymph.
|
102
|
1173 if (vector.individual[2] == 0) {
|
103
|
1174 # Add average temperature for current day.
|
111
|
1175 vector.individual[3] = vector.individual[3] + averages.temp;
|
102
|
1176 if (vector.individual[3] >= (68+opt$young_nymph_accumulation)) {
|
90
|
1177 # From egg to young nymph, degree-days requirement met.
|
111
|
1178 current.gen = vector.individual[1];
|
85
|
1179 # Transfer to young nymph stage.
|
111
|
1180 vector.individual = c(current.gen, 1, 0, 0, 0);
|
85
|
1181 }
|
|
1182 else {
|
|
1183 # Add 1 day in current stage.
|
111
|
1184 vector.individual[4] = vector.individual[4] + 1;
|
85
|
1185 }
|
111
|
1186 vector.matrix[i,] = vector.individual;
|
85
|
1187 }
|
109
|
1188 # Young nymph to old nymph.
|
102
|
1189 if (vector.individual[2] == 1) {
|
103
|
1190 # Add average temperature for current day.
|
111
|
1191 vector.individual[3] = vector.individual[3] + averages.temp;
|
102
|
1192 if (vector.individual[3] >= (250+opt$old_nymph_accumulation)) {
|
90
|
1193 # From young to old nymph, degree_days requirement met.
|
111
|
1194 current.gen = vector.individual[1];
|
85
|
1195 # Transfer to old nym stage.
|
111
|
1196 vector.individual = c(current.gen, 2, 0, 0, 0);
|
85
|
1197 if (photoperiod < opt$photoperiod && doy > 180) {
|
111
|
1198 vector.individual[5] = 1;
|
85
|
1199 } # Prepare for diapausing.
|
|
1200 }
|
|
1201 else {
|
|
1202 # Add 1 day in current stage.
|
111
|
1203 vector.individual[4] = vector.individual[4] + 1;
|
85
|
1204 }
|
111
|
1205 vector.matrix[i,] = vector.individual;
|
96
|
1206 }
|
112
|
1207 # Old nymph to adult: pre-vittelogenic or diapausing?
|
102
|
1208 if (vector.individual[2] == 2) {
|
103
|
1209 # Add average temperature for current day.
|
111
|
1210 vector.individual[3] = vector.individual[3] + averages.temp;
|
102
|
1211 if (vector.individual[3] >= (200+opt$adult_accumulation)) {
|
90
|
1212 # From old to adult, degree_days requirement met.
|
111
|
1213 current.gen = vector.individual[1];
|
102
|
1214 if (vector.individual[5] == 0) {
|
109
|
1215 # Previttelogenic.
|
111
|
1216 vector.individual = c(current.gen, 3, 0, 0, 0);
|
85
|
1217 }
|
|
1218 else {
|
|
1219 # Diapausing.
|
111
|
1220 vector.individual = c(current.gen, 5, 0, 0, 1);
|
85
|
1221 }
|
|
1222 }
|
|
1223 else {
|
|
1224 # Add 1 day in current stage.
|
111
|
1225 vector.individual[4] = vector.individual[4] + 1;
|
85
|
1226 }
|
111
|
1227 vector.matrix[i,] = vector.individual;
|
85
|
1228 }
|
109
|
1229 # Growing of diapausing adult (unimportant, but still necessary).
|
102
|
1230 if (vector.individual[2] == 5) {
|
111
|
1231 vector.individual[3] = vector.individual[3] + averages.temp;
|
|
1232 vector.individual[4] = vector.individual[4] + 1;
|
|
1233 vector.matrix[i,] = vector.individual;
|
85
|
1234 }
|
|
1235 } # Else if it is still alive.
|
|
1236 } # End of the individual bug loop.
|
107
|
1237
|
|
1238 # Number of deaths.
|
111
|
1239 num_insects.death = length(death.vector);
|
90
|
1240 if (num_insects.death > 0) {
|
102
|
1241 # Remove record of dead.
|
111
|
1242 vector.matrix = vector.matrix[-death.vector,];
|
85
|
1243 }
|
107
|
1244 # Number of births.
|
111
|
1245 num_insects.newborn = length(birth.vector[,1]);
|
|
1246 vector.matrix = rbind(vector.matrix, birth.vector);
|
85
|
1247 # Update population size for the next day.
|
111
|
1248 num_insects = num_insects - num_insects.death + num_insects.newborn;
|
85
|
1249
|
112
|
1250 # Aggregate results by day. Due to multiple transpose calls
|
|
1251 # on vector.matrix above, the columns of vector.matrix
|
|
1252 # are now Generation, Stage, degree-days, T, Diapause,
|
|
1253 if (process_eggs) {
|
|
1254 # For egg population size, column 2 (Stage), must be 0.
|
|
1255 Eggs[row] = sum(vector.matrix[,2]==0);
|
|
1256 }
|
|
1257 if (process_young_nymphs | process_total_nymphs) {
|
|
1258 # For young nymph population size, column 2 (Stage) must be 1.
|
|
1259 YoungNymphs[row] = sum(vector.matrix[,2]==1);
|
|
1260 }
|
|
1261 if (process_old_nymphs | process_total_nymphs) {
|
|
1262 # For old nymph population size, column 2 (Stage) must be 2.
|
|
1263 OldNymphs[row] = sum(vector.matrix[,2]==2);
|
|
1264 }
|
|
1265 if (process_previttelogenic_adults | process_total_adults) {
|
|
1266 # For pre-vittelogenic population size, column 2 (Stage) must be 3.
|
|
1267 Previttelogenic[row] = sum(vector.matrix[,2]==3);
|
|
1268 }
|
|
1269 if (process_vittelogenic_adults | process_total_adults) {
|
|
1270 # For vittelogenic population size, column 2 (Stage) must be 4.
|
|
1271 Vittelogenic[row] = sum(vector.matrix[,2]==4);
|
|
1272 }
|
|
1273 if (process_diapausing_adults | process_total_adults) {
|
|
1274 # For diapausing population size, column 2 (Stage) must be 5.
|
|
1275 Diapausing[row] = sum(vector.matrix[,2]==5);
|
|
1276 }
|
107
|
1277
|
|
1278 # Newborn population size.
|
111
|
1279 N.newborn[row] = num_insects.newborn;
|
107
|
1280 # Adult population size.
|
111
|
1281 N.adult[row] = sum(vector.matrix[,2]==3) + sum(vector.matrix[,2]==4) + sum(vector.matrix[,2]==5);
|
107
|
1282 # Dead population size.
|
111
|
1283 N.death[row] = num_insects.death;
|
107
|
1284
|
111
|
1285 total.population = c(total.population, num_insects);
|
107
|
1286
|
112
|
1287 # For overwintering adult (P) population
|
|
1288 # size, column 1 (Generation) must be 0.
|
111
|
1289 overwintering_adult.population[row] = sum(vector.matrix[,1]==0);
|
112
|
1290 # For first field generation (F1) population
|
|
1291 # size, column 1 (Generation) must be 1.
|
111
|
1292 first_generation.population[row] = sum(vector.matrix[,1]==1);
|
112
|
1293 # For second field generation (F2) population
|
|
1294 # size, column 1 (Generation) must be 2.
|
111
|
1295 second_generation.population[row] = sum(vector.matrix[,1]==2);
|
107
|
1296
|
112
|
1297 if (plot_generations_separately) {
|
|
1298 if (process_eggs) {
|
|
1299 # For egg life stage of generation P population size,
|
|
1300 # column 1 (generation) is 0 and column 2 (Stage) is 0.
|
|
1301 P.egg[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==0);
|
|
1302 # For egg life stage of generation F1 population size,
|
|
1303 # column 1 (generation) is 1 and column 2 (Stage) is 0.
|
|
1304 F1.egg[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==0);
|
|
1305 # For egg life stage of generation F2 population size,
|
|
1306 # column 1 (generation) is 2 and column 2 (Stage) is 0.
|
|
1307 F2.egg[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==0);
|
|
1308 }
|
|
1309 if (process_young_nymphs) {
|
|
1310 # For young nymph life stage of generation P population
|
|
1311 # size, the following combination is required:
|
|
1312 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph)
|
|
1313 P.young_nymph[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==1);
|
|
1314 # For young nymph life stage of generation F1 population
|
|
1315 # size, the following combination is required:
|
|
1316 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph)
|
|
1317 F1.young_nymph[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==1);
|
|
1318 # For young nymph life stage of generation F2 population
|
|
1319 # size, the following combination is required:
|
|
1320 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph)
|
|
1321 F2.young_nymph[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==1);
|
|
1322 }
|
|
1323 if (process_old_nymphs) {
|
|
1324 # For old nymph life stage of generation P population
|
|
1325 # size, the following combination is required:
|
|
1326 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph)
|
|
1327 P.old_nymph[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==2);
|
|
1328 # For old nymph life stage of generation F1 population
|
|
1329 # size, the following combination is required:
|
|
1330 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph)
|
|
1331 F1.old_nymph[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==2);
|
|
1332 # For old nymph life stage of generation F2 population
|
|
1333 # size, the following combination is required:
|
|
1334 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph)
|
|
1335 F2.old_nymph[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==2);
|
|
1336 }
|
|
1337 if (process_total_nymphs) {
|
|
1338 # For total nymph life stage of generation P population
|
|
1339 # size, one of the following combinations is required:
|
|
1340 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph)
|
|
1341 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph)
|
|
1342 P.total_nymph[row] = sum((vector.matrix[,1]==0 & vector.matrix[,2]==1) | (vector.matrix[,1]==0 & vector.matrix[,2]==2));
|
|
1343 # For total nymph life stage of generation F1 population
|
|
1344 # size, one of the following combinations is required:
|
|
1345 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph)
|
|
1346 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph)
|
|
1347 F1.total_nymph[row] = sum((vector.matrix[,1]==1 & vector.matrix[,2]==1) | (vector.matrix[,1]==1 & vector.matrix[,2]==2));
|
|
1348 # For total nymph life stage of generation F2 population
|
|
1349 # size, one of the following combinations is required:
|
|
1350 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph)
|
|
1351 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph)
|
|
1352 F2.total_nymph[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==1) | (vector.matrix[,1]==2 & vector.matrix[,2]==2));
|
|
1353 }
|
|
1354 if (process_previttelogenic_adults) {
|
|
1355 # For previttelogenic adult life stage of generation P population
|
|
1356 # size, the following combination is required:
|
|
1357 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1358 P.previttelogenic_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==3);
|
|
1359 # For previttelogenic adult life stage of generation F1 population
|
|
1360 # size, the following combination is required:
|
|
1361 # - column 1 (Generation) is 1 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1362 F1.previttelogenic_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==3);
|
|
1363 # For previttelogenic adult life stage of generation F2 population
|
|
1364 # size, the following combination is required:
|
|
1365 # - column 1 (Generation) is 2 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1366 F2.previttelogenic_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==3);
|
|
1367 }
|
|
1368 if (process_vittelogenic_adults) {
|
|
1369 # For vittelogenic adult life stage of generation P population
|
|
1370 # size, the following combination is required:
|
|
1371 # - column 1 (Generation) is 0 and column 2 (Stage) is 4 (Vittelogenic)
|
|
1372 P.vittelogenic_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==4);
|
|
1373 # For vittelogenic adult life stage of generation F1 population
|
|
1374 # size, the following combination is required:
|
|
1375 # - column 1 (Generation) is 1 and column 2 (Stage) is 4 (Vittelogenic)
|
|
1376 F1.vittelogenic_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==4);
|
|
1377 # For vittelogenic adult life stage of generation F2 population
|
|
1378 # size, the following combination is required:
|
|
1379 # - column 1 (Generation) is 2 and column 2 (Stage) is 4 (Vittelogenic)
|
|
1380 F2.vittelogenic_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==4);
|
|
1381 }
|
|
1382 if (process_diapausing_adults) {
|
|
1383 # For diapausing adult life stage of generation P population
|
|
1384 # size, the following combination is required:
|
|
1385 # - column 1 (Generation) is 0 and column 2 (Stage) is 5 (Diapausing)
|
|
1386 P.diapausing_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==5);
|
|
1387 # For diapausing adult life stage of generation F1 population
|
|
1388 # size, the following combination is required:
|
|
1389 # - column 1 (Generation) is 1 and column 2 (Stage) is 5 (Diapausing)
|
|
1390 F1.diapausing_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==5);
|
|
1391 # For diapausing adult life stage of generation F2 population
|
|
1392 # size, the following combination is required:
|
|
1393 # - column 1 (Generation) is 2 and column 2 (Stage) is 5 (Diapausing)
|
|
1394 F2.diapausing_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==5);
|
|
1395 }
|
|
1396 if (process_total_adults) {
|
|
1397 # For total adult life stage of generation P population
|
|
1398 # size, one of the following combinations is required:
|
|
1399 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1400 # - column 1 (Generation) is 0 and column 2 (Stage) is 4 (Vittelogenic)
|
|
1401 # - column 1 (Generation) is 0 and column 2 (Stage) is 5 (Diapausing)
|
|
1402 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));
|
|
1403 # For total adult life stage of generation F1 population
|
|
1404 # size, one of the following combinations is required:
|
|
1405 # - column 1 (Generation) is 1 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1406 # - column 1 (Generation) is 1 and column 2 (Stage) is 4 (Vittelogenic)
|
|
1407 # - column 1 (Generation) is 1 and column 2 (Stage) is 5 (Diapausing)
|
|
1408 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));
|
|
1409 # For total adult life stage of generation F2 population
|
|
1410 # size, one of the following combinations is required:
|
|
1411 # - column 1 (Generation) is 2 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1412 # - column 1 (Generation) is 2 and column 2 (Stage) is 4 (Vittelogenic)
|
|
1413 # - column 1 (Generation) is 2 and column 2 (Stage) is 5 (Diapausing)
|
|
1414 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));
|
|
1415 }
|
|
1416 }
|
|
1417 } # End of days specified in the input_ytd temperature data.
|
85
|
1418
|
111
|
1419 averages.cum = cumsum(averages.day);
|
85
|
1420
|
102
|
1421 # Define the output values.
|
112
|
1422 if (process_eggs) {
|
|
1423 Eggs.replications[,current_replication] = Eggs;
|
|
1424 }
|
|
1425 if (process_young_nymphs | process_total_nymphs) {
|
|
1426 YoungNymphs.replications[,current_replication] = YoungNymphs;
|
|
1427 }
|
|
1428 if (process_old_nymphs | process_total_nymphs) {
|
|
1429 OldNymphs.replications[,current_replication] = OldNymphs;
|
|
1430 }
|
|
1431 if (process_previttelogenic_adults | process_total_adults) {
|
|
1432 Previttelogenic.replications[,current_replication] = Previttelogenic;
|
|
1433 }
|
|
1434 if (process_vittelogenic_adults | process_total_adults) {
|
|
1435 Vittelogenic.replications[,current_replication] = Vittelogenic;
|
|
1436 }
|
|
1437 if (process_diapausing_adults | process_total_adults) {
|
|
1438 Diapausing.replications[,current_replication] = Diapausing;
|
|
1439 }
|
|
1440 newborn.replications[,current_replication] = N.newborn;
|
|
1441 adult.replications[,current_replication] = N.adult;
|
|
1442 death.replications[,current_replication] = N.death;
|
|
1443 if (plot_generations_separately) {
|
|
1444 # P is Parental, or overwintered adults.
|
|
1445 P.replications[,current_replication] = overwintering_adult.population;
|
|
1446 # F1 is the first field-produced generation.
|
|
1447 F1.replications[,current_replication] = first_generation.population;
|
|
1448 # F2 is the second field-produced generation.
|
|
1449 F2.replications[,current_replication] = second_generation.population;
|
|
1450 if (process_eggs) {
|
|
1451 P_eggs.replications[,current_replication] = P.egg;
|
|
1452 F1_eggs.replications[,current_replication] = F1.egg;
|
|
1453 F2_eggs.replications[,current_replication] = F2.egg;
|
|
1454 }
|
|
1455 if (process_young_nymphs) {
|
|
1456 P_young_nymphs.replications[,current_replication] = P.young_nymph;
|
|
1457 F1_young_nymphs.replications[,current_replication] = F1.young_nymph;
|
|
1458 F2_young_nymphs.replications[,current_replication] = F2.young_nymph;
|
|
1459 }
|
|
1460 if (process_old_nymphs) {
|
|
1461 P_old_nymphs.replications[,current_replication] = P.old_nymph;
|
|
1462 F1_old_nymphs.replications[,current_replication] = F1.old_nymph;
|
|
1463 F2_old_nymphs.replications[,current_replication] = F2.old_nymph;
|
|
1464 }
|
|
1465 if (process_total_nymphs) {
|
|
1466 P_total_nymphs.replications[,current_replication] = P.total_nymph;
|
|
1467 F1_total_nymphs.replications[,current_replication] = F1.total_nymph;
|
|
1468 F2_total_nymphs.replications[,current_replication] = F2.total_nymph;
|
|
1469 }
|
|
1470 if (process_previttelogenic_adults) {
|
|
1471 P_previttelogenic_adults.replications[,current_replication] = P.previttelogenic_adult;
|
|
1472 F1_previttelogenic_adults.replications[,current_replication] = F1.previttelogenic_adult;
|
|
1473 F2_previttelogenic_adults.replications[,current_replication] = F2.previttelogenic_adult;
|
|
1474 }
|
|
1475 if (process_vittelogenic_adults) {
|
|
1476 P_vittelogenic_adults.replications[,current_replication] = P.vittelogenic_adult;
|
|
1477 F1_vittelogenic_adults.replications[,current_replication] = F1.vittelogenic_adult;
|
|
1478 F2_vittelogenic_adults.replications[,current_replication] = F2.vittelogenic_adult;
|
|
1479 }
|
|
1480 if (process_diapausing_adults) {
|
|
1481 P_diapausing_adults.replications[,current_replication] = P.diapausing_adult;
|
|
1482 F1_diapausing_adults.replications[,current_replication] = F1.diapausing_adult;
|
|
1483 F2_diapausing_adults.replications[,current_replication] = F2.diapausing_adult;
|
|
1484 }
|
|
1485 if (process_total_adults) {
|
|
1486 P_total_adults.replications[,current_replication] = P.total_adult;
|
|
1487 F1_total_adults.replications[,current_replication] = F1.total_adult;
|
|
1488 F2_total_adults.replications[,current_replication] = F2.total_adult;
|
|
1489 }
|
|
1490 }
|
|
1491 population.replications[,current_replication] = total.population;
|
|
1492 # End processing replications.
|
|
1493 }
|
107
|
1494
|
112
|
1495 if (process_eggs) {
|
|
1496 # Mean value for eggs.
|
|
1497 eggs = apply(Eggs.replications, 1, mean);
|
|
1498 temperature_data_frame = append_vector(temperature_data_frame, eggs, "EGG");
|
|
1499 # Standard error for eggs.
|
|
1500 eggs.std_error = apply(Eggs.replications, 1, sd) / sqrt(opt$replications);
|
|
1501 temperature_data_frame = append_vector(temperature_data_frame, eggs.std_error, "EGGSE");
|
|
1502 }
|
|
1503 if (process_nymphs) {
|
|
1504 # Calculate nymph populations for selected life stage.
|
|
1505 for (life_stage_nymph in life_stages_nymph) {
|
|
1506 if (life_stage_nymph=="Young") {
|
|
1507 # Mean value for young nymphs.
|
|
1508 young_nymphs = apply(YoungNymphs.replications, 1, mean);
|
|
1509 temperature_data_frame = append_vector(temperature_data_frame, young_nymphs, "YOUNGNYMPH");
|
|
1510 # Standard error for young nymphs.
|
|
1511 young_nymphs.std_error = apply(YoungNymphs.replications / sqrt(opt$replications), 1, sd);
|
|
1512 temperature_data_frame = append_vector(temperature_data_frame, young_nymphs.std_error, "YOUNGNYMPHSE");
|
|
1513 } else if (life_stage_nymph=="Old") {
|
|
1514 # Mean value for old nymphs.
|
|
1515 old_nymphs = apply(OldNymphs.replications, 1, mean);
|
|
1516 temperature_data_frame = append_vector(temperature_data_frame, old_nymphs, "OLDNYMPH");
|
|
1517 # Standard error for old nymphs.
|
|
1518 old_nymphs.std_error = apply(OldNymphs.replications / sqrt(opt$replications), 1, sd);
|
|
1519 temperature_data_frame = append_vector(temperature_data_frame, old_nymphs.std_error, "OLDNYMPHSE");
|
|
1520 } else if (life_stage_nymph=="Total") {
|
|
1521 # Mean value for all nymphs.
|
|
1522 total_nymphs = apply((YoungNymphs.replications+OldNymphs.replications), 1, mean);
|
|
1523 temperature_data_frame = append_vector(temperature_data_frame, total_nymphs, "TOTALNYMPH");
|
|
1524 # Standard error for all nymphs.
|
|
1525 total_nymphs.std_error = apply((YoungNymphs.replications+OldNymphs.replications) / sqrt(opt$replications), 1, sd);
|
|
1526 temperature_data_frame = append_vector(temperature_data_frame, total_nymphs.std_error, "TOTALNYMPHSE");
|
|
1527 }
|
|
1528 }
|
|
1529 }
|
|
1530 if (process_adults) {
|
|
1531 # Calculate adult populations for selected life stage.
|
|
1532 for (life_stage_adult in life_stages_adult) {
|
|
1533 if (life_stage_adult == "Pre-vittelogenic") {
|
|
1534 # Mean value for previttelogenic adults.
|
|
1535 previttelogenic_adults = apply(Previttelogenic.replications, 1, mean);
|
|
1536 temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults, "PRE-VITADULT");
|
|
1537 # Standard error for previttelogenic adults.
|
|
1538 previttelogenic_adults.std_error = apply(Previttelogenic.replications, 1, sd) / sqrt(opt$replications);
|
|
1539 temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults.std_error, "PRE-VITADULTSE");
|
|
1540 } else if (life_stage_adult == "Vittelogenic") {
|
|
1541 # Mean value for vittelogenic adults.
|
|
1542 vittelogenic_adults = apply(Vittelogenic.replications, 1, mean);
|
|
1543 temperature_data_frame = append_vector(temperature_data_frame, vittelogenic_adults, "VITADULT");
|
|
1544 # Standard error for vittelogenic adults.
|
|
1545 vittelogenic_adults.std_error = apply(Vittelogenic.replications, 1, sd) / sqrt(opt$replications);
|
|
1546 temperature_data_frame = append_vector(temperature_data_frame, vittelogenic_adults.std_error, "VITADULTSE");
|
|
1547 } else if (life_stage_adult == "Diapausing") {
|
|
1548 # Mean value for vittelogenic adults.
|
|
1549 diapausing_adults = apply(Diapausing.replications, 1, mean);
|
|
1550 temperature_data_frame = append_vector(temperature_data_frame, diapausing_adults, "DIAPAUSINGADULT");
|
|
1551 # Standard error for vittelogenic adults.
|
|
1552 diapausing_adults.std_error = apply(Diapausing.replications, 1, sd) / sqrt(opt$replications);
|
|
1553 temperature_data_frame = append_vector(temperature_data_frame, diapausing_adults.std_error, "DIAPAUSINGADULTSE");
|
|
1554 } else if (life_stage_adult=="Total") {
|
|
1555 # Mean value for all adults.
|
|
1556 total_adults = apply((Previttelogenic.replications+Vittelogenic.replications+Diapausing.replications), 1, mean);
|
|
1557 temperature_data_frame = append_vector(temperature_data_frame, total_adults, "TOTALADULT");
|
|
1558 # Standard error for all adults.
|
|
1559 total_adults.std_error = apply((Previttelogenic.replications+Vittelogenic.replications+Diapausing.replications), 1, sd) / sqrt(opt$replications);
|
|
1560 temperature_data_frame = append_vector(temperature_data_frame, total_adults.std_error, "TOTALADULTSE");
|
|
1561 }
|
|
1562 }
|
85
|
1563 }
|
|
1564
|
112
|
1565 if (plot_generations_separately) {
|
|
1566 m_se = get_mean_and_std_error(P.replications, F1.replications, F2.replications);
|
|
1567 P = m_se[[1]];
|
|
1568 P.std_error = m_se[[2]];
|
|
1569 F1 = m_se[[3]];
|
|
1570 F1.std_error = m_se[[4]];
|
|
1571 F2 = m_se[[5]];
|
|
1572 F2.std_error = m_se[[6]];
|
|
1573 if (process_eggs) {
|
|
1574 m_se = get_mean_and_std_error(P_eggs.replications, F1_eggs.replications, F2_eggs.replications);
|
|
1575 P_eggs = m_se[[1]];
|
|
1576 P_eggs.std_error = m_se[[2]];
|
|
1577 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs, "EGG-P");
|
|
1578 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs.std_error, "EGG-P-SE");
|
|
1579 F1_eggs = m_se[[3]];
|
|
1580 F1_eggs.std_error = m_se[[4]];
|
|
1581 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs, "EGG-F1");
|
|
1582 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs.std_error, "EGG-F1-SE");
|
|
1583 F2_eggs = m_se[[5]];
|
|
1584 F2_eggs.std_error = m_se[[6]];
|
|
1585 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs, "EGG-F2");
|
|
1586 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs.std_error, "EGG-F2-SE");
|
|
1587 }
|
|
1588 if (process_young_nymphs) {
|
|
1589 m_se = get_mean_and_std_error(P_young_nymphs.replications, F1_young_nymphs.replications, F2_young_nymphs.replications);
|
|
1590 P_young_nymphs = m_se[[1]];
|
|
1591 P_young_nymphs.std_error = m_se[[2]];
|
|
1592 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs, "YOUNGNYMPH-P");
|
|
1593 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs.std_error, "YOUNGNYMPH-P-SE");
|
|
1594 F1_young_nymphs = m_se[[3]];
|
|
1595 F1_young_nymphs.std_error = m_se[[4]];
|
|
1596 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs, "YOUNGNYMPH-F1");
|
|
1597 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs.std_error, "YOUNGNYMPH-F1-SE");
|
|
1598 F2_young_nymphs = m_se[[5]];
|
|
1599 F2_young_nymphs.std_error = m_se[[6]];
|
|
1600 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs, "YOUNGNYMPH-F2");
|
|
1601 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs.std_error, "YOUNGNYMPH-F2-SE");
|
|
1602 }
|
|
1603 if (process_old_nymphs) {
|
|
1604 m_se = get_mean_and_std_error(P_old_nymphs.replications, F1_old_nymphs.replications, F2_old_nymphs.replications);
|
|
1605 P_old_nymphs = m_se[[1]];
|
|
1606 P_old_nymphs.std_error = m_se[[2]];
|
|
1607 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs, "OLDNYMPH-P");
|
|
1608 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs.std_error, "OLDNYMPH-P-SE");
|
|
1609 F1_old_nymphs = m_se[[3]];
|
|
1610 F1_old_nymphs.std_error = m_se[[4]];
|
|
1611 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs, "OLDNYMPH-F1");
|
|
1612 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs.std_error, "OLDNYMPH-F1-SE");
|
|
1613 F2_old_nymphs = m_se[[5]];
|
|
1614 F2_old_nymphs.std_error = m_se[[6]];
|
|
1615 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs, "OLDNYMPH-F2");
|
|
1616 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs.std_error, "OLDNYMPH-F2-SE");
|
|
1617 }
|
|
1618 if (process_total_nymphs) {
|
|
1619 m_se = get_mean_and_std_error(P_total_nymphs.replications, F1_total_nymphs.replications, F2_total_nymphs.replications);
|
|
1620 P_total_nymphs = m_se[[1]];
|
|
1621 P_total_nymphs.std_error = m_se[[2]];
|
|
1622 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs, "TOTALNYMPH-P");
|
|
1623 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs.std_error, "TOTALNYMPH-P-SE");
|
|
1624 F1_total_nymphs = m_se[[3]];
|
|
1625 F1_total_nymphs.std_error = m_se[[4]];
|
|
1626 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs, "TOTALNYMPH-F1");
|
|
1627 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs.std_error, "TOTALNYMPH-F1-SE");
|
|
1628 F2_total_nymphs = m_se[[5]];
|
|
1629 F2_total_nymphs.std_error = m_se[[6]];
|
|
1630 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs, "TOTALNYMPH-F2");
|
|
1631 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs.std_error, "TOTALNYMPH-F2-SE");
|
|
1632 }
|
|
1633 if (process_previttelogenic_adults) {
|
|
1634 m_se = get_mean_and_std_error(P_previttelogenic_adults.replications, F1_previttelogenic_adults.replications, F2_previttelogenic_adults.replications);
|
|
1635 P_previttelogenic_adults = m_se[[1]];
|
|
1636 P_previttelogenic_adults.std_error = m_se[[2]];
|
|
1637 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults, "PRE-VITADULT-P");
|
|
1638 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults.std_error, "PRE-VITADULT-P-SE");
|
|
1639 F1_previttelogenic_adults = m_se[[3]];
|
|
1640 F1_previttelogenic_adults.std_error = m_se[[4]];
|
|
1641 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults, "PRE-VITADULT-F1");
|
|
1642 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults.std_error, "PRE-VITADULT-F1-SE");
|
|
1643 F2_previttelogenic_adults = m_se[[5]];
|
|
1644 F2_previttelogenic_adults.std_error = m_se[[6]];
|
|
1645 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults, "PRE-VITADULT-F2");
|
|
1646 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults.std_error, "PRE-VITADULT-F2-SE");
|
|
1647 }
|
|
1648 if (process_vittelogenic_adults) {
|
|
1649 m_se = get_mean_and_std_error(P_vittelogenic_adults.replications, F1_vittelogenic_adults.replications, F2_vittelogenic_adults.replications);
|
|
1650 P_vittelogenic_adults = m_se[[1]];
|
|
1651 P_vittelogenic_adults.std_error = m_se[[2]];
|
|
1652 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults, "VITADULT-P");
|
|
1653 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults.std_error, "VITADULT-P-SE");
|
|
1654 F1_vittelogenic_adults = m_se[[3]];
|
|
1655 F1_vittelogenic_adults.std_error = m_se[[4]];
|
|
1656 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults, "VITADULT-F1");
|
|
1657 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults.std_error, "VITADULT-F1-SE");
|
|
1658 F2_vittelogenic_adults = m_se[[5]];
|
|
1659 F2_vittelogenic_adults.std_error = m_se[[6]];
|
|
1660 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults, "VITADULT-F2");
|
|
1661 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults.std_error, "VITADULT-F2-SE");
|
|
1662 }
|
|
1663 if (process_diapausing_adults) {
|
|
1664 m_se = get_mean_and_std_error(P_diapausing_adults.replications, F1_diapausing_adults.replications, F2_diapausing_adults.replications);
|
|
1665 P_diapausing_adults = m_se[[1]];
|
|
1666 P_diapausing_adults.std_error = m_se[[2]];
|
|
1667 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults, "DIAPAUSINGADULT-P");
|
|
1668 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults.std_error, "DIAPAUSINGADULT-P-SE");
|
|
1669 F1_diapausing_adults = m_se[[3]];
|
|
1670 F1_diapausing_adults.std_error = m_se[[4]];
|
|
1671 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults, "DIAPAUSINGADULT-F1");
|
|
1672 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults.std_error, "DIAPAUSINGADULT-F1-SE");
|
|
1673 F2_diapausing_adults = m_se[[5]];
|
|
1674 F2_diapausing_adults.std_error = m_se[[6]];
|
|
1675 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults, "DIAPAUSINGADULT-F2");
|
|
1676 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults.std_error, "DIAPAUSINGADULT-F2-SE");
|
|
1677 }
|
|
1678 if (process_total_adults) {
|
|
1679 m_se = get_mean_and_std_error(P_total_adults.replications, F1_total_adults.replications, F2_total_adults.replications);
|
|
1680 P_total_adults = m_se[[1]];
|
|
1681 P_total_adults.std_error = m_se[[2]];
|
|
1682 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults, "TOTALADULT-P");
|
|
1683 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults.std_error, "TOTALADULT-P-SE");
|
|
1684 F1_total_adults = m_se[[3]];
|
|
1685 F1_total_adults.std_error = m_se[[4]];
|
|
1686 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults, "TOTALADULT-F1");
|
|
1687 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults.std_error, "TOTALADULT-F1-SE");
|
|
1688 F2_total_adults = m_se[[5]];
|
|
1689 F2_total_adults.std_error = m_se[[6]];
|
|
1690 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults, "TOTALADULT-F2");
|
|
1691 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults.std_error, "TOTALADULT-F2-SE");
|
|
1692 }
|
|
1693 }
|
103
|
1694
|
112
|
1695 # Save the analyzed data for combined generations.
|
|
1696 file_path = paste("output_data_dir", "04_combined_generations.csv", sep="/");
|
|
1697 write.csv(temperature_data_frame, file=file_path, row.names=F);
|
|
1698 if (plot_generations_separately) {
|
|
1699 # Save the analyzed data for generation P.
|
|
1700 file_path = paste("output_data_dir", "01_generation_P.csv", sep="/");
|
|
1701 write.csv(temperature_data_frame_P, file=file_path, row.names=F);
|
|
1702 # Save the analyzed data for generation F1.
|
|
1703 file_path = paste("output_data_dir", "02_generation_F1.csv", sep="/");
|
|
1704 write.csv(temperature_data_frame_F1, file=file_path, row.names=F);
|
|
1705 # Save the analyzed data for generation F2.
|
|
1706 file_path = paste("output_data_dir", "03_generation_F2.csv", sep="/");
|
|
1707 write.csv(temperature_data_frame_F2, file=file_path, row.names=F);
|
|
1708 }
|
103
|
1709
|
122
|
1710 total_days_vector = c(1:dim(temperature_data_frame)[1]);
|
112
|
1711 if (plot_generations_separately) {
|
|
1712 for (life_stage in life_stages) {
|
|
1713 if (life_stage == "Egg") {
|
|
1714 # Start PDF device driver.
|
|
1715 dev.new(width=20, height=30);
|
|
1716 file_path = get_file_path(life_stage, "egg_pop_by_generation.pdf")
|
|
1717 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1718 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1719 # Egg population size by generation.
|
|
1720 maxval = max(P_eggs+F1_eggs+F2_eggs) + 100;
|
|
1721 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
|
|
1722 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=P_eggs, group_std_error=P_eggs.std_error,
|
|
1723 group2=F1_eggs, group2_std_error=F1_eggs.std_error, group3=F2_eggs, group3_std_error=F2_eggs.std_error);
|
|
1724 # Turn off device driver to flush output.
|
|
1725 dev.off();
|
|
1726 } else if (life_stage == "Nymph") {
|
|
1727 for (life_stage_nymph in life_stages_nymph) {
|
|
1728 # Start PDF device driver.
|
|
1729 dev.new(width=20, height=30);
|
|
1730 file_path = get_file_path(life_stage, "nymph_pop_by_generation.pdf", life_stage_nymph=life_stage_nymph)
|
|
1731 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1732 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1733 if (life_stage_nymph=="Young") {
|
|
1734 # Young nymph population size by generation.
|
|
1735 maxval = max(P_young_nymphs+F1_young_nymphs+F2_young_nymphs) + 100;
|
|
1736 group = P_young_nymphs;
|
|
1737 group_std_error = P_young_nymphs.std_error;
|
|
1738 group2 = F1_young_nymphs;
|
|
1739 group2_std_error = F1_young_nymphs.std_error;
|
|
1740 group3 = F2_young_nymphs;
|
|
1741 group3_std_error = F2_young_nymphs.std_error;
|
|
1742 } else if (life_stage_nymph=="Old") {
|
|
1743 # Total nymph population size by generation.
|
|
1744 maxval = max(P_old_nymphs+F1_old_nymphs+F2_old_nymphs) + 100;
|
|
1745 group = P_old_nymphs;
|
|
1746 group_std_error = P_old_nymphs.std_error;
|
|
1747 group2 = F1_old_nymphs;
|
|
1748 group2_std_error = F1_old_nymphs.std_error;
|
|
1749 group3 = F2_old_nymphs;
|
|
1750 group3_std_error = F2_old_nymphs.std_error;
|
|
1751 } else if (life_stage_nymph=="Total") {
|
|
1752 # Total nymph population size by generation.
|
|
1753 maxval = max(P_total_nymphs+F1_total_nymphs+F2_total_nymphs) + 100;
|
|
1754 group = P_total_nymphs;
|
|
1755 group_std_error = P_total_nymphs.std_error;
|
|
1756 group2 = F1_total_nymphs;
|
|
1757 group2_std_error = F1_total_nymphs.std_error;
|
|
1758 group3 = F2_total_nymphs;
|
|
1759 group3_std_error = F2_total_nymphs.std_error;
|
|
1760 }
|
|
1761 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
|
|
1762 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
|
|
1763 group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, life_stages_nymph=life_stage_nymph);
|
|
1764 # Turn off device driver to flush output.
|
|
1765 dev.off();
|
|
1766 }
|
|
1767 } else if (life_stage == "Adult") {
|
|
1768 for (life_stage_adult in life_stages_adult) {
|
|
1769 # Start PDF device driver.
|
|
1770 dev.new(width=20, height=30);
|
|
1771 file_path = get_file_path(life_stage, "adult_pop_by_generation.pdf", life_stage_adult=life_stage_adult)
|
|
1772 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1773 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1774 if (life_stage_adult=="Pre-vittelogenic") {
|
|
1775 # Pre-vittelogenic adult population size by generation.
|
|
1776 maxval = max(P_previttelogenic_adults+F1_previttelogenic_adults+F2_previttelogenic_adults) + 100;
|
|
1777 group = P_previttelogenic_adults;
|
|
1778 group_std_error = P_previttelogenic_adults.std_error;
|
|
1779 group2 = F1_previttelogenic_adults;
|
|
1780 group2_std_error = F1_previttelogenic_adults.std_error;
|
|
1781 group3 = F2_previttelogenic_adults;
|
|
1782 group3_std_error = F2_previttelogenic_adults.std_error;
|
|
1783 } else if (life_stage_adult=="Vittelogenic") {
|
|
1784 # Vittelogenic adult population size by generation.
|
|
1785 maxval = max(P_vittelogenic_adults+F1_vittelogenic_adults+F2_vittelogenic_adults) + 100;
|
|
1786 group = P_vittelogenic_adults;
|
|
1787 group_std_error = P_vittelogenic_adults.std_error;
|
|
1788 group2 = F1_vittelogenic_adults;
|
|
1789 group2_std_error = F1_vittelogenic_adults.std_error;
|
|
1790 group3 = F2_vittelogenic_adults;
|
|
1791 group3_std_error = F2_vittelogenic_adults.std_error;
|
|
1792 } else if (life_stage_adult=="Diapausing") {
|
|
1793 # Diapausing adult population size by generation.
|
|
1794 maxval = max(P_diapausing_adults+F1_diapausing_adults+F2_diapausing_adults) + 100;
|
|
1795 group = P_diapausing_adults;
|
|
1796 group_std_error = P_diapausing_adults.std_error;
|
|
1797 group2 = F1_diapausing_adults;
|
|
1798 group2_std_error = F1_diapausing_adults.std_error;
|
|
1799 group3 = F2_diapausing_adults;
|
|
1800 group3_std_error = F2_diapausing_adults.std_error;
|
|
1801 } else if (life_stage_adult=="Total") {
|
|
1802 # Total adult population size by generation.
|
|
1803 maxval = max(P_total_adults+F1_total_adults+F2_total_adults) + 100;
|
|
1804 group = P_total_adults;
|
|
1805 group_std_error = P_total_adults.std_error;
|
|
1806 group2 = F1_total_adults;
|
|
1807 group2_std_error = F1_total_adults.std_error;
|
|
1808 group3 = F2_total_adults;
|
|
1809 group3_std_error = F2_total_adults.std_error;
|
|
1810 }
|
|
1811 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
|
|
1812 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
|
|
1813 group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, life_stages_adult=life_stage_adult);
|
|
1814 # Turn off device driver to flush output.
|
|
1815 dev.off();
|
|
1816 }
|
|
1817 } else if (life_stage == "Total") {
|
|
1818 # Start PDF device driver.
|
|
1819 # Name collection elements so that they
|
|
1820 # are displayed in logical order.
|
|
1821 dev.new(width=20, height=30);
|
|
1822 file_path = get_file_path(life_stage, "total_pop_by_generation.pdf")
|
|
1823 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1824 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1825 # Total population size by generation.
|
|
1826 maxval = max(P+F1+F2) + 100;
|
|
1827 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
|
|
1828 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=P, group_std_error=P.std_error,
|
|
1829 group2=F1, group2_std_error=F1.std_error, group3=F2, group3_std_error=F2.std_error);
|
|
1830 # Turn off device driver to flush output.
|
|
1831 dev.off();
|
|
1832 }
|
|
1833 }
|
|
1834 } else {
|
|
1835 for (life_stage in life_stages) {
|
|
1836 if (life_stage == "Egg") {
|
|
1837 # Start PDF device driver.
|
|
1838 dev.new(width=20, height=30);
|
|
1839 file_path = get_file_path(life_stage, "egg_pop.pdf")
|
|
1840 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1841 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1842 # Egg population size.
|
|
1843 maxval = max(eggs+eggs.std_error) + 100;
|
|
1844 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
|
|
1845 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=eggs, group_std_error=eggs.std_error);
|
|
1846 # Turn off device driver to flush output.
|
|
1847 dev.off();
|
|
1848 } else if (life_stage == "Nymph") {
|
|
1849 for (life_stage_nymph in life_stages_nymph) {
|
|
1850 # Start PDF device driver.
|
|
1851 dev.new(width=20, height=30);
|
|
1852 file_path = get_file_path(life_stage, "nymph_pop.pdf", life_stage_nymph=life_stage_nymph)
|
|
1853 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1854 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1855 if (life_stage_nymph=="Total") {
|
|
1856 # Total nymph population size.
|
|
1857 group = total_nymphs;
|
|
1858 group_std_error = total_nymphs.std_error;
|
|
1859 } else if (life_stage_nymph=="Young") {
|
|
1860 # Young nymph population size.
|
|
1861 group = young_nymphs;
|
|
1862 group_std_error = young_nymphs.std_error;
|
|
1863 } else if (life_stage_nymph=="Old") {
|
|
1864 # Old nymph population size.
|
|
1865 group = old_nymphs;
|
|
1866 group_std_error = old_nymphs.std_error;
|
|
1867 }
|
|
1868 maxval = max(group+group_std_error) + 100;
|
|
1869 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
|
|
1870 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
|
|
1871 life_stages_nymph=life_stage_nymph);
|
|
1872 # Turn off device driver to flush output.
|
|
1873 dev.off();
|
|
1874 }
|
|
1875 } else if (life_stage == "Adult") {
|
|
1876 for (life_stage_adult in life_stages_adult) {
|
|
1877 # Start PDF device driver.
|
|
1878 dev.new(width=20, height=30);
|
|
1879 file_path = get_file_path(life_stage, "adult_pop.pdf", life_stage_adult=life_stage_adult)
|
|
1880 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1881 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1882 if (life_stage_adult=="Total") {
|
|
1883 # Total adult population size.
|
|
1884 group = total_adults;
|
|
1885 group_std_error = total_adults.std_error
|
|
1886 } else if (life_stage_adult=="Pre-vittelogenic") {
|
|
1887 # Pre-vittelogenic adult population size.
|
|
1888 group = previttelogenic_adults;
|
|
1889 group_std_error = previttelogenic_adults.std_error
|
|
1890 } else if (life_stage_adult=="Vittelogenic") {
|
|
1891 # Vittelogenic adult population size.
|
|
1892 group = vittelogenic_adults;
|
|
1893 group_std_error = vittelogenic_adults.std_error
|
|
1894 } else if (life_stage_adult=="Diapausing") {
|
|
1895 # Diapausing adult population size.
|
|
1896 group = diapausing_adults;
|
|
1897 group_std_error = diapausing_adults.std_error
|
|
1898 }
|
|
1899 maxval = max(group+group_std_error) + 100;
|
|
1900 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
|
|
1901 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
|
|
1902 life_stages_adult=life_stage_adult);
|
|
1903 # Turn off device driver to flush output.
|
|
1904 dev.off();
|
|
1905 }
|
|
1906 } else if (life_stage == "Total") {
|
|
1907 # Start PDF device driver.
|
|
1908 dev.new(width=20, height=30);
|
|
1909 file_path = get_file_path(life_stage, "total_pop.pdf")
|
|
1910 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1911 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1912 # Total population size.
|
|
1913 maxval = max(eggs+eggs.std_error, total_nymphs+total_nymphs.std_error, total_adults+total_adults.std_error) + 100;
|
|
1914 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
|
|
1915 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=total_adults, group_std_error=total_adults.std_error,
|
|
1916 group2=total_nymphs, group2_std_error=total_nymphs.std_error, group3=eggs, group3_std_error=eggs.std_error);
|
|
1917 # Turn off device driver to flush output.
|
|
1918 dev.off();
|
|
1919 }
|
|
1920 }
|
|
1921 }
|