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