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