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