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