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