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