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