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