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