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