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