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