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