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