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;
|
10
|
567 for (life_stage in life_stages) {
|
|
568 if (life_stage=="Total") {
|
|
569 process_eggs = TRUE;
|
|
570 process_nymphs = TRUE;
|
|
571 process_adults = TRUE;
|
|
572 } else if (life_stage=="Egg") {
|
|
573 process_eggs = TRUE;
|
|
574 } else if (life_stage=="Nymph") {
|
|
575 process_nymphs = TRUE;
|
|
576 } else if (life_stage=="Adult") {
|
|
577 process_adults = TRUE;
|
|
578 }
|
|
579 }
|
20
|
580 if (process_nymphs) {
|
|
581 # Split life_stages_nymph into a list of strings for plots.
|
|
582 life_stages_nymph_str = as.character(opt$life_stages_nymph);
|
|
583 life_stages_nymph = strsplit(life_stages_nymph_str, ",")[[1]];
|
23
|
584 for (life_stage_nymph in life_stages_nymph) {
|
20
|
585 if (life_stage_nymph=="Young") {
|
|
586 process_young_nymphs = TRUE;
|
|
587 } else if (life_stage_nymph=="Old") {
|
|
588 process_old_nymphs = TRUE;
|
|
589 } else if (life_stage_nymph=="Total") {
|
|
590 process_total_nymphs = TRUE;
|
|
591 }
|
|
592 }
|
|
593 }
|
16
|
594 if (process_adults) {
|
|
595 # Split life_stages_adult into a list of strings for plots.
|
|
596 life_stages_adult_str = as.character(opt$life_stages_adult);
|
|
597 life_stages_adult = strsplit(life_stages_adult_str, ",")[[1]];
|
23
|
598 for (life_stage_adult in life_stages_adult) {
|
|
599 if (life_stage_adult=="Pre-vittelogenic") {
|
|
600 process_previttelogenic_adults = TRUE;
|
24
|
601 } else if (life_stage_adult=="Vittelogenic") {
|
23
|
602 process_vittelogenic_adults = TRUE;
|
20
|
603 } else if (life_stage_adult=="Diapausing") {
|
|
604 process_diapausing_adults = TRUE;
|
|
605 } else if (life_stage_adult=="Total") {
|
|
606 process_total_adults = TRUE;
|
|
607 }
|
|
608 }
|
16
|
609 }
|
6
|
610 # Initialize matrices.
|
49
|
611 total_days = dim(temperature_data_frame)[1];
|
10
|
612 if (process_eggs) {
|
38
|
613 Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
614 }
|
23
|
615 if (process_young_nymphs | process_total_nymphs) {
|
38
|
616 YoungNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
20
|
617 }
|
23
|
618 if (process_old_nymphs | process_total_nymphs) {
|
38
|
619 OldNymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
620 }
|
23
|
621 if (process_previttelogenic_adults | process_total_adults) {
|
38
|
622 Previttelogenic.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
23
|
623 }
|
|
624 if (process_vittelogenic_adults | process_total_adults) {
|
38
|
625 Vittelogenic.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
23
|
626 }
|
|
627 if (process_diapausing_adults | process_total_adults) {
|
38
|
628 Diapausing.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
629 }
|
38
|
630 newborn.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
631 adult.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
632 death.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
633 if (plot_generations_separately) {
|
|
634 # P is Parental, or overwintered adults.
|
38
|
635 P.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
636 # F1 is the first field-produced generation.
|
38
|
637 F1.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
638 # F2 is the second field-produced generation.
|
38
|
639 F2.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
640 if (process_eggs) {
|
38
|
641 P_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
642 F1_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
643 F2_eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
644 }
|
20
|
645 if (process_young_nymphs) {
|
38
|
646 P_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
647 F1_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
648 F2_young_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
20
|
649 }
|
|
650 if (process_old_nymphs) {
|
38
|
651 P_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
652 F1_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
653 F2_old_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
20
|
654 }
|
|
655 if (process_total_nymphs) {
|
38
|
656 P_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
657 F1_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
658 F2_total_nymphs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
659 }
|
23
|
660 if (process_previttelogenic_adults) {
|
38
|
661 P_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
662 F1_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
663 F2_previttelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
23
|
664 }
|
|
665 if (process_vittelogenic_adults) {
|
38
|
666 P_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
667 F1_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
668 F2_vittelogenic_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
23
|
669 }
|
|
670 if (process_diapausing_adults) {
|
38
|
671 P_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
672 F1_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
673 F2_diapausing_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
23
|
674 }
|
|
675 if (process_total_adults) {
|
38
|
676 P_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
677 F1_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
|
678 F2_total_adults.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
10
|
679 }
|
|
680 }
|
|
681 # Total population.
|
38
|
682 population.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
|
5
|
683
|
6
|
684 # Process replications.
|
18
|
685 for (current_replication in 1:opt$replications) {
|
6
|
686 # Start with the user-defined number of insects per replication.
|
8
|
687 num_insects = opt$insects_per_replication;
|
6
|
688 # Generation, Stage, degree-days, T, Diapause.
|
8
|
689 vector.ini = c(0, 3, 0, 0, 0);
|
10
|
690 # Replicate to create a matrix where the columns are
|
|
691 # Generation, Stage, degree-days, T, Diapause and the
|
|
692 # rows are the initial number of insects per replication.
|
8
|
693 vector.matrix = rep(vector.ini, num_insects);
|
10
|
694 # Complete transposed matrix for the population, so now
|
|
695 # the rows are Generation, Stage, degree-days, T, Diapause
|
8
|
696 vector.matrix = base::t(matrix(vector.matrix, nrow=5));
|
5
|
697 # Time series of population size.
|
10
|
698 if (process_eggs) {
|
38
|
699 Eggs = rep(0, total_days);
|
10
|
700 }
|
23
|
701 if (process_young_nymphs | process_total_nymphs) {
|
38
|
702 YoungNymphs = rep(0, total_days);
|
23
|
703 }
|
|
704 if (process_old_nymphs | process_total_nymphs) {
|
38
|
705 OldNymphs = rep(0, total_days);
|
10
|
706 }
|
23
|
707 if (process_previttelogenic_adults | process_total_adults) {
|
38
|
708 Previttelogenic = rep(0, total_days);
|
23
|
709 }
|
|
710 if (process_vittelogenic_adults | process_total_adults) {
|
38
|
711 Vittelogenic = rep(0, total_days);
|
23
|
712 }
|
|
713 if (process_diapausing_adults | process_total_adults) {
|
38
|
714 Diapausing = rep(0, total_days);
|
10
|
715 }
|
38
|
716 N.newborn = rep(0, total_days);
|
|
717 N.adult = rep(0, total_days);
|
|
718 N.death = rep(0, total_days);
|
|
719 overwintering_adult.population = rep(0, total_days);
|
|
720 first_generation.population = rep(0, total_days);
|
|
721 second_generation.population = rep(0, total_days);
|
10
|
722 if (plot_generations_separately) {
|
|
723 # P is Parental, or overwintered adults.
|
|
724 # F1 is the first field-produced generation.
|
|
725 # F2 is the second field-produced generation.
|
|
726 if (process_eggs) {
|
38
|
727 P.egg = rep(0, total_days);
|
|
728 F1.egg = rep(0, total_days);
|
|
729 F2.egg = rep(0, total_days);
|
10
|
730 }
|
20
|
731 if (process_young_nymphs) {
|
38
|
732 P.young_nymph = rep(0, total_days);
|
|
733 F1.young_nymph = rep(0, total_days);
|
|
734 F2.young_nymph = rep(0, total_days);
|
20
|
735 }
|
|
736 if (process_old_nymphs) {
|
38
|
737 P.old_nymph = rep(0, total_days);
|
|
738 F1.old_nymph = rep(0, total_days);
|
|
739 F2.old_nymph = rep(0, total_days);
|
20
|
740 }
|
|
741 if (process_total_nymphs) {
|
38
|
742 P.total_nymph = rep(0, total_days);
|
|
743 F1.total_nymph = rep(0, total_days);
|
|
744 F2.total_nymph = rep(0, total_days);
|
10
|
745 }
|
23
|
746 if (process_previttelogenic_adults) {
|
38
|
747 P.previttelogenic_adult = rep(0, total_days);
|
|
748 F1.previttelogenic_adult = rep(0, total_days);
|
|
749 F2.previttelogenic_adult = rep(0, total_days);
|
23
|
750 }
|
|
751 if (process_vittelogenic_adults) {
|
38
|
752 P.vittelogenic_adult = rep(0, total_days);
|
|
753 F1.vittelogenic_adult = rep(0, total_days);
|
|
754 F2.vittelogenic_adult = rep(0, total_days);
|
23
|
755 }
|
|
756 if (process_diapausing_adults) {
|
38
|
757 P.diapausing_adult = rep(0, total_days);
|
|
758 F1.diapausing_adult = rep(0, total_days);
|
|
759 F2.diapausing_adult = rep(0, total_days);
|
23
|
760 }
|
|
761 if (process_total_adults) {
|
38
|
762 P.total_adult = rep(0, total_days);
|
|
763 F1.total_adult = rep(0, total_days);
|
|
764 F2.total_adult = rep(0, total_days);
|
10
|
765 }
|
|
766 }
|
8
|
767 total.population = NULL;
|
38
|
768 averages.day = rep(0, total_days);
|
|
769 # All the days included in the input_ytd temperature dataset.
|
|
770 for (row in 1:total_days) {
|
5
|
771 # Get the integer day of the year for the current row.
|
8
|
772 doy = temperature_data_frame$DOY[row];
|
5
|
773 # Photoperiod in the day.
|
8
|
774 photoperiod = temperature_data_frame$DAYLEN[row];
|
49
|
775 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row);
|
8
|
776 mean.temp = temp.profile[1];
|
|
777 averages.temp = temp.profile[2];
|
|
778 averages.day[row] = averages.temp;
|
5
|
779 # Trash bin for death.
|
8
|
780 death.vector = NULL;
|
5
|
781 # Newborn.
|
8
|
782 birth.vector = NULL;
|
5
|
783 # All individuals.
|
6
|
784 for (i in 1:num_insects) {
|
|
785 # Individual record.
|
8
|
786 vector.individual = vector.matrix[i,];
|
6
|
787 # Adjustment for late season mortality rate (still alive?).
|
5
|
788 if (latitude < 40.0) {
|
8
|
789 post.mortality = 1;
|
|
790 day.kill = 300;
|
5
|
791 }
|
|
792 else {
|
8
|
793 post.mortality = 2;
|
|
794 day.kill = 250;
|
5
|
795 }
|
6
|
796 if (vector.individual[2] == 0) {
|
5
|
797 # Egg.
|
8
|
798 death.probability = opt$egg_mortality * mortality.egg(mean.temp);
|
5
|
799 }
|
6
|
800 else if (vector.individual[2] == 1 | vector.individual[2] == 2) {
|
18
|
801 # Nymph.
|
8
|
802 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp);
|
5
|
803 }
|
6
|
804 else if (vector.individual[2] == 3 | vector.individual[2] == 4 | vector.individual[2] == 5) {
|
|
805 # Adult.
|
5
|
806 if (doy < day.kill) {
|
8
|
807 death.probability = opt$adult_mortality * mortality.adult(mean.temp);
|
5
|
808 }
|
|
809 else {
|
|
810 # Increase adult mortality after fall equinox.
|
8
|
811 death.probability = opt$adult_mortality * post.mortality * mortality.adult(mean.temp);
|
5
|
812 }
|
|
813 }
|
6
|
814 # Dependent on temperature and life stage?
|
8
|
815 u.d = runif(1);
|
6
|
816 if (u.d < death.probability) {
|
8
|
817 death.vector = c(death.vector, i);
|
6
|
818 }
|
5
|
819 else {
|
6
|
820 # End of diapause.
|
|
821 if (vector.individual[1] == 0 && vector.individual[2] == 3) {
|
27
|
822 # Overwintering adult (pre-vittelogenic).
|
6
|
823 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) {
|
5
|
824 # Add 68C to become fully reproductively matured.
|
|
825 # Transfer to vittelogenic.
|
8
|
826 vector.individual = c(0, 4, 0, 0, 0);
|
|
827 vector.matrix[i,] = vector.individual;
|
5
|
828 }
|
|
829 else {
|
27
|
830 # Add average temperature for current day.
|
8
|
831 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
832 # Add 1 day in current stage.
|
8
|
833 vector.individual[4] = vector.individual[4] + 1;
|
|
834 vector.matrix[i,] = vector.individual;
|
5
|
835 }
|
|
836 }
|
6
|
837 if (vector.individual[1] != 0 && vector.individual[2] == 3) {
|
27
|
838 # Not overwintering adult (pre-vittelogenic).
|
8
|
839 current.gen = vector.individual[1];
|
6
|
840 if (vector.individual[3] > 68) {
|
5
|
841 # Add 68C to become fully reproductively matured.
|
|
842 # Transfer to vittelogenic.
|
8
|
843 vector.individual = c(current.gen, 4, 0, 0, 0);
|
|
844 vector.matrix[i,] = vector.individual;
|
5
|
845 }
|
|
846 else {
|
6
|
847 # Add average temperature for current day.
|
8
|
848 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
849 # Add 1 day in current stage.
|
8
|
850 vector.individual[4] = vector.individual[4] + 1;
|
|
851 vector.matrix[i,] = vector.individual;
|
5
|
852 }
|
|
853 }
|
6
|
854 # Oviposition -- where population dynamics comes from.
|
|
855 if (vector.individual[2] == 4 && vector.individual[1] == 0 && mean.temp > 10) {
|
5
|
856 # Vittelogenic stage, overwintering generation.
|
6
|
857 if (vector.individual[4] == 0) {
|
5
|
858 # Just turned in vittelogenic stage.
|
8
|
859 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size));
|
5
|
860 }
|
|
861 else {
|
|
862 # Daily probability of birth.
|
8
|
863 p.birth = opt$oviposition * 0.01;
|
|
864 u1 = runif(1);
|
5
|
865 if (u1 < p.birth) {
|
8
|
866 num_insects.birth = round(runif(1, 2, 8));
|
5
|
867 }
|
|
868 }
|
6
|
869 # Add average temperature for current day.
|
8
|
870 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
871 # Add 1 day in current stage.
|
8
|
872 vector.individual[4] = vector.individual[4] + 1;
|
|
873 vector.matrix[i,] = vector.individual;
|
6
|
874 if (num_insects.birth > 0) {
|
5
|
875 # Add new birth -- might be in different generations.
|
8
|
876 new.gen = vector.individual[1] + 1;
|
5
|
877 # Egg profile.
|
8
|
878 new.individual = c(new.gen, 0, 0, 0, 0);
|
|
879 new.vector = rep(new.individual, num_insects.birth);
|
5
|
880 # Update batch of egg profile.
|
8
|
881 new.vector = t(matrix(new.vector, nrow=5));
|
5
|
882 # Group with total eggs laid in that day.
|
8
|
883 birth.vector = rbind(birth.vector, new.vector);
|
5
|
884 }
|
|
885 }
|
6
|
886 # Oviposition -- for generation 1.
|
|
887 if (vector.individual[2] == 4 && vector.individual[1] == 1 && mean.temp > 12.5 && doy < 222) {
|
5
|
888 # Vittelogenic stage, 1st generation
|
6
|
889 if (vector.individual[4] == 0) {
|
5
|
890 # Just turned in vittelogenic stage.
|
8
|
891 num_insects.birth = round(runif(1, 2+opt$min_clutch_size, 8+opt$max_clutch_size));
|
5
|
892 }
|
|
893 else {
|
|
894 # Daily probability of birth.
|
8
|
895 p.birth = opt$oviposition * 0.01;
|
|
896 u1 = runif(1);
|
5
|
897 if (u1 < p.birth) {
|
8
|
898 num_insects.birth = round(runif(1, 2, 8));
|
5
|
899 }
|
|
900 }
|
6
|
901 # Add average temperature for current day.
|
8
|
902 vector.individual[3] = vector.individual[3] + averages.temp;
|
5
|
903 # Add 1 day in current stage.
|
8
|
904 vector.individual[4] = vector.individual[4] + 1;
|
|
905 vector.matrix[i,] = vector.individual;
|
6
|
906 if (num_insects.birth > 0) {
|
5
|
907 # Add new birth -- might be in different generations.
|
8
|
908 new.gen = vector.individual[1] + 1;
|
5
|
909 # Egg profile.
|
8
|
910 new.individual = c(new.gen, 0, 0, 0, 0);
|
|
911 new.vector = rep(new.individual, num_insects.birth);
|
5
|
912 # Update batch of egg profile.
|
8
|
913 new.vector = t(matrix(new.vector, nrow=5));
|
5
|
914 # Group with total eggs laid in that day.
|
8
|
915 birth.vector = rbind(birth.vector, new.vector);
|
5
|
916 }
|
|
917 }
|
6
|
918 # Egg to young nymph.
|
|
919 if (vector.individual[2] == 0) {
|
|
920 # Add average temperature for current day.
|
8
|
921 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
922 if (vector.individual[3] >= (68+opt$young_nymph_accumulation)) {
|
|
923 # From egg to young nymph, degree-days requirement met.
|
8
|
924 current.gen = vector.individual[1];
|
5
|
925 # Transfer to young nymph stage.
|
8
|
926 vector.individual = c(current.gen, 1, 0, 0, 0);
|
5
|
927 }
|
|
928 else {
|
|
929 # Add 1 day in current stage.
|
8
|
930 vector.individual[4] = vector.individual[4] + 1;
|
5
|
931 }
|
8
|
932 vector.matrix[i,] = vector.individual;
|
5
|
933 }
|
6
|
934 # Young nymph to old nymph.
|
|
935 if (vector.individual[2] == 1) {
|
|
936 # Add average temperature for current day.
|
8
|
937 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
938 if (vector.individual[3] >= (250+opt$old_nymph_accumulation)) {
|
|
939 # From young to old nymph, degree_days requirement met.
|
8
|
940 current.gen = vector.individual[1];
|
5
|
941 # Transfer to old nym stage.
|
8
|
942 vector.individual = c(current.gen, 2, 0, 0, 0);
|
5
|
943 if (photoperiod < opt$photoperiod && doy > 180) {
|
8
|
944 vector.individual[5] = 1;
|
5
|
945 } # Prepare for diapausing.
|
|
946 }
|
|
947 else {
|
|
948 # Add 1 day in current stage.
|
8
|
949 vector.individual[4] = vector.individual[4] + 1;
|
5
|
950 }
|
8
|
951 vector.matrix[i,] = vector.individual;
|
6
|
952 }
|
27
|
953 # Old nymph to adult: pre-vittelogenic or diapausing?
|
6
|
954 if (vector.individual[2] == 2) {
|
|
955 # Add average temperature for current day.
|
8
|
956 vector.individual[3] = vector.individual[3] + averages.temp;
|
6
|
957 if (vector.individual[3] >= (200+opt$adult_accumulation)) {
|
|
958 # From old to adult, degree_days requirement met.
|
8
|
959 current.gen = vector.individual[1];
|
6
|
960 if (vector.individual[5] == 0) {
|
|
961 # Previttelogenic.
|
8
|
962 vector.individual = c(current.gen, 3, 0, 0, 0);
|
5
|
963 }
|
|
964 else {
|
|
965 # Diapausing.
|
8
|
966 vector.individual = c(current.gen, 5, 0, 0, 1);
|
5
|
967 }
|
|
968 }
|
|
969 else {
|
|
970 # Add 1 day in current stage.
|
8
|
971 vector.individual[4] = vector.individual[4] + 1;
|
5
|
972 }
|
8
|
973 vector.matrix[i,] = vector.individual;
|
5
|
974 }
|
6
|
975 # Growing of diapausing adult (unimportant, but still necessary).
|
|
976 if (vector.individual[2] == 5) {
|
8
|
977 vector.individual[3] = vector.individual[3] + averages.temp;
|
|
978 vector.individual[4] = vector.individual[4] + 1;
|
|
979 vector.matrix[i,] = vector.individual;
|
5
|
980 }
|
|
981 } # Else if it is still alive.
|
|
982 } # End of the individual bug loop.
|
6
|
983
|
|
984 # Number of deaths.
|
8
|
985 num_insects.death = length(death.vector);
|
6
|
986 if (num_insects.death > 0) {
|
|
987 # Remove record of dead.
|
8
|
988 vector.matrix = vector.matrix[-death.vector,];
|
5
|
989 }
|
6
|
990 # Number of births.
|
8
|
991 num_insects.newborn = length(birth.vector[,1]);
|
|
992 vector.matrix = rbind(vector.matrix, birth.vector);
|
5
|
993 # Update population size for the next day.
|
8
|
994 num_insects = num_insects - num_insects.death + num_insects.newborn;
|
5
|
995
|
10
|
996 # Aggregate results by day. Due to multiple transpose calls
|
|
997 # on vector.matrix above, the columns of vector.matrix
|
|
998 # are now Generation, Stage, degree-days, T, Diapause,
|
|
999 if (process_eggs) {
|
|
1000 # For egg population size, column 2 (Stage), must be 0.
|
|
1001 Eggs[row] = sum(vector.matrix[,2]==0);
|
|
1002 }
|
23
|
1003 if (process_young_nymphs | process_total_nymphs) {
|
10
|
1004 # For young nymph population size, column 2 (Stage) must be 1.
|
|
1005 YoungNymphs[row] = sum(vector.matrix[,2]==1);
|
20
|
1006 }
|
23
|
1007 if (process_old_nymphs | process_total_nymphs) {
|
10
|
1008 # For old nymph population size, column 2 (Stage) must be 2.
|
|
1009 OldNymphs[row] = sum(vector.matrix[,2]==2);
|
|
1010 }
|
23
|
1011 if (process_previttelogenic_adults | process_total_adults) {
|
|
1012 # For pre-vittelogenic population size, column 2 (Stage) must be 3.
|
|
1013 Previttelogenic[row] = sum(vector.matrix[,2]==3);
|
|
1014 }
|
|
1015 if (process_vittelogenic_adults | process_total_adults) {
|
|
1016 # For vittelogenic population size, column 2 (Stage) must be 4.
|
24
|
1017 Vittelogenic[row] = sum(vector.matrix[,2]==4);
|
23
|
1018 }
|
|
1019 if (process_diapausing_adults | process_total_adults) {
|
10
|
1020 # For diapausing population size, column 2 (Stage) must be 5.
|
|
1021 Diapausing[row] = sum(vector.matrix[,2]==5);
|
|
1022 }
|
5
|
1023
|
6
|
1024 # Newborn population size.
|
8
|
1025 N.newborn[row] = num_insects.newborn;
|
6
|
1026 # Adult population size.
|
8
|
1027 N.adult[row] = sum(vector.matrix[,2]==3) + sum(vector.matrix[,2]==4) + sum(vector.matrix[,2]==5);
|
6
|
1028 # Dead population size.
|
8
|
1029 N.death[row] = num_insects.death;
|
6
|
1030
|
8
|
1031 total.population = c(total.population, num_insects);
|
6
|
1032
|
10
|
1033 # For overwintering adult (P) population
|
|
1034 # size, column 1 (Generation) must be 0.
|
8
|
1035 overwintering_adult.population[row] = sum(vector.matrix[,1]==0);
|
10
|
1036 # For first field generation (F1) population
|
|
1037 # size, column 1 (Generation) must be 1.
|
8
|
1038 first_generation.population[row] = sum(vector.matrix[,1]==1);
|
10
|
1039 # For second field generation (F2) population
|
|
1040 # size, column 1 (Generation) must be 2.
|
8
|
1041 second_generation.population[row] = sum(vector.matrix[,1]==2);
|
5
|
1042
|
10
|
1043 if (plot_generations_separately) {
|
|
1044 if (process_eggs) {
|
18
|
1045 # For egg life stage of generation P population size,
|
10
|
1046 # column 1 (generation) is 0 and column 2 (Stage) is 0.
|
|
1047 P.egg[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==0);
|
|
1048 # For egg life stage of generation F1 population size,
|
|
1049 # column 1 (generation) is 1 and column 2 (Stage) is 0.
|
|
1050 F1.egg[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==0);
|
|
1051 # For egg life stage of generation F2 population size,
|
|
1052 # column 1 (generation) is 2 and column 2 (Stage) is 0.
|
|
1053 F2.egg[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==0);
|
|
1054 }
|
20
|
1055 if (process_young_nymphs) {
|
|
1056 # For young nymph life stage of generation P population
|
|
1057 # size, the following combination is required:
|
|
1058 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph)
|
|
1059 P.young_nymph[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==1);
|
|
1060 # For young nymph life stage of generation F1 population
|
|
1061 # size, the following combination is required:
|
|
1062 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph)
|
|
1063 F1.young_nymph[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==1);
|
|
1064 # For young nymph life stage of generation F2 population
|
|
1065 # size, the following combination is required:
|
|
1066 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph)
|
|
1067 F2.young_nymph[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==1);
|
|
1068 }
|
|
1069 if (process_old_nymphs) {
|
|
1070 # For old nymph life stage of generation P population
|
|
1071 # size, the following combination is required:
|
|
1072 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph)
|
|
1073 P.old_nymph[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==2);
|
|
1074 # For old nymph life stage of generation F1 population
|
|
1075 # size, the following combination is required:
|
|
1076 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph)
|
|
1077 F1.old_nymph[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==2);
|
|
1078 # For old nymph life stage of generation F2 population
|
|
1079 # size, the following combination is required:
|
|
1080 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph)
|
|
1081 F2.old_nymph[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==2);
|
|
1082 }
|
|
1083 if (process_total_nymphs) {
|
|
1084 # For total nymph life stage of generation P population
|
10
|
1085 # size, one of the following combinations is required:
|
|
1086 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph)
|
|
1087 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph)
|
20
|
1088 P.total_nymph[row] = sum((vector.matrix[,1]==0 & vector.matrix[,2]==1) | (vector.matrix[,1]==0 & vector.matrix[,2]==2));
|
|
1089 # For total nymph life stage of generation F1 population
|
10
|
1090 # size, one of the following combinations is required:
|
|
1091 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph)
|
|
1092 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph)
|
20
|
1093 F1.total_nymph[row] = sum((vector.matrix[,1]==1 & vector.matrix[,2]==1) | (vector.matrix[,1]==1 & vector.matrix[,2]==2));
|
|
1094 # For total nymph life stage of generation F2 population
|
10
|
1095 # size, one of the following combinations is required:
|
|
1096 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph)
|
|
1097 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph)
|
20
|
1098 F2.total_nymph[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==1) | (vector.matrix[,1]==2 & vector.matrix[,2]==2));
|
10
|
1099 }
|
23
|
1100 if (process_previttelogenic_adults) {
|
|
1101 # For previttelogenic adult life stage of generation P population
|
|
1102 # size, the following combination is required:
|
|
1103 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1104 P.previttelogenic_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==3);
|
|
1105 # For previttelogenic adult life stage of generation F1 population
|
|
1106 # size, the following combination is required:
|
|
1107 # - column 1 (Generation) is 1 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1108 F1.previttelogenic_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==3);
|
|
1109 # For previttelogenic adult life stage of generation F2 population
|
|
1110 # size, the following combination is required:
|
|
1111 # - column 1 (Generation) is 2 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
|
1112 F2.previttelogenic_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==3);
|
|
1113 }
|
|
1114 if (process_vittelogenic_adults) {
|
|
1115 # For vittelogenic adult life stage of generation P population
|
|
1116 # size, the following combination is required:
|
24
|
1117 # - column 1 (Generation) is 0 and column 2 (Stage) is 4 (Vittelogenic)
|
23
|
1118 P.vittelogenic_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==4);
|
|
1119 # For vittelogenic adult life stage of generation F1 population
|
|
1120 # size, the following combination is required:
|
24
|
1121 # - column 1 (Generation) is 1 and column 2 (Stage) is 4 (Vittelogenic)
|
23
|
1122 F1.vittelogenic_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==4);
|
|
1123 # For vittelogenic adult life stage of generation F2 population
|
|
1124 # size, the following combination is required:
|
24
|
1125 # - column 1 (Generation) is 2 and column 2 (Stage) is 4 (Vittelogenic)
|
23
|
1126 F2.vittelogenic_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==4);
|
|
1127 }
|
|
1128 if (process_diapausing_adults) {
|
|
1129 # For diapausing adult life stage of generation P population
|
|
1130 # size, the following combination is required:
|
10
|
1131 # - column 1 (Generation) is 0 and column 2 (Stage) is 5 (Diapausing)
|
23
|
1132 P.diapausing_adult[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==5);
|
|
1133 # For diapausing adult life stage of generation F1 population
|
|
1134 # size, the following combination is required:
|
|
1135 # - column 1 (Generation) is 1 and column 2 (Stage) is 5 (Diapausing)
|
|
1136 F1.diapausing_adult[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==5);
|
|
1137 # For diapausing adult life stage of generation F2 population
|
|
1138 # size, the following combination is required:
|
|
1139 # - column 1 (Generation) is 2 and column 2 (Stage) is 5 (Diapausing)
|
|
1140 F2.diapausing_adult[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==5);
|
|
1141 }
|
|
1142 if (process_total_adults) {
|
|
1143 # For total adult life stage of generation P population
|
10
|
1144 # size, one of the following combinations is required:
|
23
|
1145 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
24
|
1146 # - column 1 (Generation) is 0 and column 2 (Stage) is 4 (Vittelogenic)
|
23
|
1147 # - column 1 (Generation) is 0 and column 2 (Stage) is 5 (Diapausing)
|
|
1148 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));
|
|
1149 # For total adult life stage of generation F1 population
|
|
1150 # size, one of the following combinations is required:
|
|
1151 # - column 1 (Generation) is 1 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
24
|
1152 # - column 1 (Generation) is 1 and column 2 (Stage) is 4 (Vittelogenic)
|
10
|
1153 # - column 1 (Generation) is 1 and column 2 (Stage) is 5 (Diapausing)
|
23
|
1154 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));
|
|
1155 # For total adult life stage of generation F2 population
|
10
|
1156 # size, one of the following combinations is required:
|
23
|
1157 # - column 1 (Generation) is 2 and column 2 (Stage) is 3 (Pre-vittelogenic)
|
24
|
1158 # - column 1 (Generation) is 2 and column 2 (Stage) is 4 (Vittelogenic)
|
10
|
1159 # - column 1 (Generation) is 2 and column 2 (Stage) is 5 (Diapausing)
|
23
|
1160 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
|
1161 }
|
|
1162 }
|
38
|
1163 } # End of days specified in the input_ytd temperature data.
|
5
|
1164
|
8
|
1165 averages.cum = cumsum(averages.day);
|
5
|
1166
|
6
|
1167 # Define the output values.
|
10
|
1168 if (process_eggs) {
|
18
|
1169 Eggs.replications[,current_replication] = Eggs;
|
10
|
1170 }
|
23
|
1171 if (process_young_nymphs | process_total_nymphs) {
|
18
|
1172 YoungNymphs.replications[,current_replication] = YoungNymphs;
|
20
|
1173 }
|
23
|
1174 if (process_old_nymphs | process_total_nymphs) {
|
18
|
1175 OldNymphs.replications[,current_replication] = OldNymphs;
|
10
|
1176 }
|
23
|
1177 if (process_previttelogenic_adults | process_total_adults) {
|
|
1178 Previttelogenic.replications[,current_replication] = Previttelogenic;
|
|
1179 }
|
|
1180 if (process_vittelogenic_adults | process_total_adults) {
|
24
|
1181 Vittelogenic.replications[,current_replication] = Vittelogenic;
|
23
|
1182 }
|
|
1183 if (process_diapausing_adults | process_total_adults) {
|
18
|
1184 Diapausing.replications[,current_replication] = Diapausing;
|
10
|
1185 }
|
18
|
1186 newborn.replications[,current_replication] = N.newborn;
|
|
1187 adult.replications[,current_replication] = N.adult;
|
|
1188 death.replications[,current_replication] = N.death;
|
10
|
1189 if (plot_generations_separately) {
|
|
1190 # P is Parental, or overwintered adults.
|
18
|
1191 P.replications[,current_replication] = overwintering_adult.population;
|
10
|
1192 # F1 is the first field-produced generation.
|
18
|
1193 F1.replications[,current_replication] = first_generation.population;
|
10
|
1194 # F2 is the second field-produced generation.
|
18
|
1195 F2.replications[,current_replication] = second_generation.population;
|
10
|
1196 if (process_eggs) {
|
18
|
1197 P_eggs.replications[,current_replication] = P.egg;
|
|
1198 F1_eggs.replications[,current_replication] = F1.egg;
|
|
1199 F2_eggs.replications[,current_replication] = F2.egg;
|
10
|
1200 }
|
20
|
1201 if (process_young_nymphs) {
|
|
1202 P_young_nymphs.replications[,current_replication] = P.young_nymph;
|
|
1203 F1_young_nymphs.replications[,current_replication] = F1.young_nymph;
|
|
1204 F2_young_nymphs.replications[,current_replication] = F2.young_nymph;
|
|
1205 }
|
|
1206 if (process_old_nymphs) {
|
|
1207 P_old_nymphs.replications[,current_replication] = P.old_nymph;
|
|
1208 F1_old_nymphs.replications[,current_replication] = F1.old_nymph;
|
|
1209 F2_old_nymphs.replications[,current_replication] = F2.old_nymph;
|
|
1210 }
|
|
1211 if (process_total_nymphs) {
|
|
1212 P_total_nymphs.replications[,current_replication] = P.total_nymph;
|
|
1213 F1_total_nymphs.replications[,current_replication] = F1.total_nymph;
|
|
1214 F2_total_nymphs.replications[,current_replication] = F2.total_nymph;
|
10
|
1215 }
|
23
|
1216 if (process_previttelogenic_adults) {
|
|
1217 P_previttelogenic_adults.replications[,current_replication] = P.previttelogenic_adult;
|
|
1218 F1_previttelogenic_adults.replications[,current_replication] = F1.previttelogenic_adult;
|
|
1219 F2_previttelogenic_adults.replications[,current_replication] = F2.previttelogenic_adult;
|
|
1220 }
|
|
1221 if (process_vittelogenic_adults) {
|
|
1222 P_vittelogenic_adults.replications[,current_replication] = P.vittelogenic_adult;
|
|
1223 F1_vittelogenic_adults.replications[,current_replication] = F1.vittelogenic_adult;
|
|
1224 F2_vittelogenic_adults.replications[,current_replication] = F2.vittelogenic_adult;
|
|
1225 }
|
|
1226 if (process_diapausing_adults) {
|
|
1227 P_diapausing_adults.replications[,current_replication] = P.diapausing_adult;
|
|
1228 F1_diapausing_adults.replications[,current_replication] = F1.diapausing_adult;
|
|
1229 F2_diapausing_adults.replications[,current_replication] = F2.diapausing_adult;
|
|
1230 }
|
|
1231 if (process_total_adults) {
|
|
1232 P_total_adults.replications[,current_replication] = P.total_adult;
|
|
1233 F1_total_adults.replications[,current_replication] = F1.total_adult;
|
|
1234 F2_total_adults.replications[,current_replication] = F2.total_adult;
|
10
|
1235 }
|
|
1236 }
|
18
|
1237 population.replications[,current_replication] = total.population;
|
|
1238 # End processing replications.
|
5
|
1239 }
|
|
1240
|
10
|
1241 if (process_eggs) {
|
|
1242 # Mean value for eggs.
|
|
1243 eggs = apply(Eggs.replications, 1, mean);
|
27
|
1244 temperature_data_frame = append_vector(temperature_data_frame, eggs, "EGG");
|
10
|
1245 # Standard error for eggs.
|
|
1246 eggs.std_error = apply(Eggs.replications, 1, sd) / sqrt(opt$replications);
|
27
|
1247 temperature_data_frame = append_vector(temperature_data_frame, eggs.std_error, "EGGSE");
|
10
|
1248 }
|
|
1249 if (process_nymphs) {
|
|
1250 # Calculate nymph populations for selected life stage.
|
16
|
1251 for (life_stage_nymph in life_stages_nymph) {
|
28
|
1252 if (life_stage_nymph=="Young") {
|
16
|
1253 # Mean value for young nymphs.
|
|
1254 young_nymphs = apply(YoungNymphs.replications, 1, mean);
|
27
|
1255 temperature_data_frame = append_vector(temperature_data_frame, young_nymphs, "YOUNGNYMPH");
|
16
|
1256 # Standard error for young nymphs.
|
|
1257 young_nymphs.std_error = apply(YoungNymphs.replications / sqrt(opt$replications), 1, sd);
|
27
|
1258 temperature_data_frame = append_vector(temperature_data_frame, young_nymphs.std_error, "YOUNGNYMPHSE");
|
18
|
1259 } else if (life_stage_nymph=="Old") {
|
16
|
1260 # Mean value for old nymphs.
|
|
1261 old_nymphs = apply(OldNymphs.replications, 1, mean);
|
27
|
1262 temperature_data_frame = append_vector(temperature_data_frame, old_nymphs, "OLDNYMPH");
|
16
|
1263 # Standard error for old nymphs.
|
|
1264 old_nymphs.std_error = apply(OldNymphs.replications / sqrt(opt$replications), 1, sd);
|
27
|
1265 temperature_data_frame = append_vector(temperature_data_frame, old_nymphs.std_error, "OLDNYMPHSE");
|
28
|
1266 } else if (life_stage_nymph=="Total") {
|
|
1267 # Mean value for all nymphs.
|
|
1268 total_nymphs = apply((YoungNymphs.replications+OldNymphs.replications), 1, mean);
|
|
1269 temperature_data_frame = append_vector(temperature_data_frame, total_nymphs, "TOTALNYMPH");
|
|
1270 # Standard error for all nymphs.
|
|
1271 total_nymphs.std_error = apply((YoungNymphs.replications+OldNymphs.replications) / sqrt(opt$replications), 1, sd);
|
|
1272 temperature_data_frame = append_vector(temperature_data_frame, total_nymphs.std_error, "TOTALNYMPHSE");
|
16
|
1273 }
|
10
|
1274 }
|
|
1275 }
|
|
1276 if (process_adults) {
|
|
1277 # Calculate adult populations for selected life stage.
|
16
|
1278 for (life_stage_adult in life_stages_adult) {
|
28
|
1279 if (life_stage_adult == "Pre-vittelogenic") {
|
23
|
1280 # Mean value for previttelogenic adults.
|
|
1281 previttelogenic_adults = apply(Previttelogenic.replications, 1, mean);
|
50
|
1282 temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults, "PRE.VITADULT");
|
23
|
1283 # Standard error for previttelogenic adults.
|
|
1284 previttelogenic_adults.std_error = apply(Previttelogenic.replications, 1, sd) / sqrt(opt$replications);
|
50
|
1285 temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults.std_error, "PRE.VITADULTSE");
|
18
|
1286 } else if (life_stage_adult == "Vittelogenic") {
|
23
|
1287 # Mean value for vittelogenic adults.
|
24
|
1288 vittelogenic_adults = apply(Vittelogenic.replications, 1, mean);
|
27
|
1289 temperature_data_frame = append_vector(temperature_data_frame, vittelogenic_adults, "VITADULT");
|
23
|
1290 # Standard error for vittelogenic adults.
|
24
|
1291 vittelogenic_adults.std_error = apply(Vittelogenic.replications, 1, sd) / sqrt(opt$replications);
|
27
|
1292 temperature_data_frame = append_vector(temperature_data_frame, vittelogenic_adults.std_error, "VITADULTSE");
|
18
|
1293 } else if (life_stage_adult == "Diapausing") {
|
23
|
1294 # Mean value for vittelogenic adults.
|
16
|
1295 diapausing_adults = apply(Diapausing.replications, 1, mean);
|
27
|
1296 temperature_data_frame = append_vector(temperature_data_frame, diapausing_adults, "DIAPAUSINGADULT");
|
23
|
1297 # Standard error for vittelogenic adults.
|
16
|
1298 diapausing_adults.std_error = apply(Diapausing.replications, 1, sd) / sqrt(opt$replications);
|
27
|
1299 temperature_data_frame = append_vector(temperature_data_frame, diapausing_adults.std_error, "DIAPAUSINGADULTSE");
|
28
|
1300 } else if (life_stage_adult=="Total") {
|
|
1301 # Mean value for all adults.
|
|
1302 total_adults = apply((Previttelogenic.replications+Vittelogenic.replications+Diapausing.replications), 1, mean);
|
|
1303 temperature_data_frame = append_vector(temperature_data_frame, total_adults, "TOTALADULT");
|
|
1304 # Standard error for all adults.
|
|
1305 total_adults.std_error = apply((Previttelogenic.replications+Vittelogenic.replications+Diapausing.replications), 1, sd) / sqrt(opt$replications);
|
|
1306 temperature_data_frame = append_vector(temperature_data_frame, total_adults.std_error, "TOTALADULTSE");
|
16
|
1307 }
|
10
|
1308 }
|
|
1309 }
|
5
|
1310
|
10
|
1311 if (plot_generations_separately) {
|
20
|
1312 m_se = get_mean_and_std_error(P.replications, F1.replications, F2.replications);
|
|
1313 P = m_se[[1]];
|
|
1314 P.std_error = m_se[[2]];
|
|
1315 F1 = m_se[[3]];
|
|
1316 F1.std_error = m_se[[4]];
|
|
1317 F2 = m_se[[5]];
|
|
1318 F2.std_error = m_se[[6]];
|
10
|
1319 if (process_eggs) {
|
20
|
1320 m_se = get_mean_and_std_error(P_eggs.replications, F1_eggs.replications, F2_eggs.replications);
|
|
1321 P_eggs = m_se[[1]];
|
|
1322 P_eggs.std_error = m_se[[2]];
|
50
|
1323 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs, "EGG.P");
|
|
1324 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs.std_error, "EGG.P.SE");
|
20
|
1325 F1_eggs = m_se[[3]];
|
|
1326 F1_eggs.std_error = m_se[[4]];
|
50
|
1327 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs, "EGG.F1");
|
|
1328 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs.std_error, "EGG.F1.SE");
|
20
|
1329 F2_eggs = m_se[[5]];
|
|
1330 F2_eggs.std_error = m_se[[6]];
|
50
|
1331 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs, "EGG.F2");
|
|
1332 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs.std_error, "EGG.F2.SE");
|
20
|
1333 }
|
|
1334 if (process_young_nymphs) {
|
|
1335 m_se = get_mean_and_std_error(P_young_nymphs.replications, F1_young_nymphs.replications, F2_young_nymphs.replications);
|
|
1336 P_young_nymphs = m_se[[1]];
|
|
1337 P_young_nymphs.std_error = m_se[[2]];
|
50
|
1338 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs, "YOUNGNYMPH.P");
|
|
1339 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs.std_error, "YOUNGNYMPH.P.SE");
|
20
|
1340 F1_young_nymphs = m_se[[3]];
|
|
1341 F1_young_nymphs.std_error = m_se[[4]];
|
50
|
1342 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs, "YOUNGNYMPH.F1");
|
|
1343 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs.std_error, "YOUNGNYMPH.F1.SE");
|
20
|
1344 F2_young_nymphs = m_se[[5]];
|
|
1345 F2_young_nymphs.std_error = m_se[[6]];
|
50
|
1346 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs, "YOUNGNYMPH.F2");
|
|
1347 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs.std_error, "YOUNGNYMPH.F2.SE");
|
10
|
1348 }
|
20
|
1349 if (process_old_nymphs) {
|
|
1350 m_se = get_mean_and_std_error(P_old_nymphs.replications, F1_old_nymphs.replications, F2_old_nymphs.replications);
|
|
1351 P_old_nymphs = m_se[[1]];
|
|
1352 P_old_nymphs.std_error = m_se[[2]];
|
50
|
1353 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs, "OLDNYMPH.P");
|
|
1354 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs.std_error, "OLDNYMPH.P.SE");
|
20
|
1355 F1_old_nymphs = m_se[[3]];
|
|
1356 F1_old_nymphs.std_error = m_se[[4]];
|
50
|
1357 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs, "OLDNYMPH.F1");
|
|
1358 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs.std_error, "OLDNYMPH.F1.SE");
|
20
|
1359 F2_old_nymphs = m_se[[5]];
|
|
1360 F2_old_nymphs.std_error = m_se[[6]];
|
50
|
1361 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs, "OLDNYMPH.F2");
|
|
1362 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs.std_error, "OLDNYMPH.F2.SE");
|
20
|
1363 }
|
|
1364 if (process_total_nymphs) {
|
|
1365 m_se = get_mean_and_std_error(P_total_nymphs.replications, F1_total_nymphs.replications, F2_total_nymphs.replications);
|
|
1366 P_total_nymphs = m_se[[1]];
|
|
1367 P_total_nymphs.std_error = m_se[[2]];
|
50
|
1368 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs, "TOTALNYMPH.P");
|
|
1369 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs.std_error, "TOTALNYMPH.P.SE");
|
20
|
1370 F1_total_nymphs = m_se[[3]];
|
|
1371 F1_total_nymphs.std_error = m_se[[4]];
|
50
|
1372 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs, "TOTALNYMPH.F1");
|
|
1373 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs.std_error, "TOTALNYMPH.F1.SE");
|
20
|
1374 F2_total_nymphs = m_se[[5]];
|
|
1375 F2_total_nymphs.std_error = m_se[[6]];
|
50
|
1376 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs, "TOTALNYMPH.F2");
|
|
1377 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs.std_error, "TOTALNYMPH.F2.SE");
|
10
|
1378 }
|
23
|
1379 if (process_previttelogenic_adults) {
|
|
1380 m_se = get_mean_and_std_error(P_previttelogenic_adults.replications, F1_previttelogenic_adults.replications, F2_previttelogenic_adults.replications);
|
|
1381 P_previttelogenic_adults = m_se[[1]];
|
|
1382 P_previttelogenic_adults.std_error = m_se[[2]];
|
50
|
1383 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults, "PRE.VITADULT.P");
|
|
1384 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults.std_error, "PRE.VITADULT.P.SE");
|
23
|
1385 F1_previttelogenic_adults = m_se[[3]];
|
|
1386 F1_previttelogenic_adults.std_error = m_se[[4]];
|
50
|
1387 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults, "PRE.VITADULT.F1");
|
|
1388 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults.std_error, "PRE.VITADULT.F1.SE");
|
23
|
1389 F2_previttelogenic_adults = m_se[[5]];
|
|
1390 F2_previttelogenic_adults.std_error = m_se[[6]];
|
50
|
1391 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults, "PRE.VITADULT.F2");
|
|
1392 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults.std_error, "PRE.VITADULT.F2.SE");
|
23
|
1393 }
|
|
1394 if (process_vittelogenic_adults) {
|
|
1395 m_se = get_mean_and_std_error(P_vittelogenic_adults.replications, F1_vittelogenic_adults.replications, F2_vittelogenic_adults.replications);
|
|
1396 P_vittelogenic_adults = m_se[[1]];
|
|
1397 P_vittelogenic_adults.std_error = m_se[[2]];
|
50
|
1398 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults, "VITADULT.P");
|
|
1399 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults.std_error, "VITADULT.P.SE");
|
23
|
1400 F1_vittelogenic_adults = m_se[[3]];
|
|
1401 F1_vittelogenic_adults.std_error = m_se[[4]];
|
50
|
1402 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults, "VITADULT.F1");
|
|
1403 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults.std_error, "VITADULT.F1.SE");
|
23
|
1404 F2_vittelogenic_adults = m_se[[5]];
|
|
1405 F2_vittelogenic_adults.std_error = m_se[[6]];
|
50
|
1406 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults, "VITADULT.F2");
|
|
1407 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults.std_error, "VITADULT.F2.SE");
|
23
|
1408 }
|
|
1409 if (process_diapausing_adults) {
|
|
1410 m_se = get_mean_and_std_error(P_diapausing_adults.replications, F1_diapausing_adults.replications, F2_diapausing_adults.replications);
|
|
1411 P_diapausing_adults = m_se[[1]];
|
|
1412 P_diapausing_adults.std_error = m_se[[2]];
|
50
|
1413 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults, "DIAPAUSINGADULT.P");
|
|
1414 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults.std_error, "DIAPAUSINGADULT.P.SE");
|
23
|
1415 F1_diapausing_adults = m_se[[3]];
|
|
1416 F1_diapausing_adults.std_error = m_se[[4]];
|
50
|
1417 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults, "DIAPAUSINGADULT.F1");
|
|
1418 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults.std_error, "DIAPAUSINGADULT.F1.SE");
|
23
|
1419 F2_diapausing_adults = m_se[[5]];
|
|
1420 F2_diapausing_adults.std_error = m_se[[6]];
|
50
|
1421 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults, "DIAPAUSINGADULT.F2");
|
|
1422 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults.std_error, "DIAPAUSINGADULT.F2.SE");
|
23
|
1423 }
|
|
1424 if (process_total_adults) {
|
|
1425 m_se = get_mean_and_std_error(P_total_adults.replications, F1_total_adults.replications, F2_total_adults.replications);
|
|
1426 P_total_adults = m_se[[1]];
|
|
1427 P_total_adults.std_error = m_se[[2]];
|
50
|
1428 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults, "TOTALADULT.P");
|
|
1429 temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults.std_error, "TOTALADULT.P.SE");
|
23
|
1430 F1_total_adults = m_se[[3]];
|
|
1431 F1_total_adults.std_error = m_se[[4]];
|
50
|
1432 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults, "TOTALADULT.F1");
|
|
1433 temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults.std_error, "TOTALADULT.F1.SE");
|
23
|
1434 F2_total_adults = m_se[[5]];
|
|
1435 F2_total_adults.std_error = m_se[[6]];
|
50
|
1436 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults, "TOTALADULT.F2");
|
|
1437 temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults.std_error, "TOTALADULT.F2.SE");
|
10
|
1438 }
|
|
1439 }
|
6
|
1440
|
31
|
1441 # Save the analyzed data for combined generations.
|
34
|
1442 file_path = paste("output_data_dir", "04_combined_generations.csv", sep="/");
|
|
1443 write.csv(temperature_data_frame, file=file_path, row.names=F);
|
31
|
1444 if (plot_generations_separately) {
|
|
1445 # Save the analyzed data for generation P.
|
34
|
1446 file_path = paste("output_data_dir", "01_generation_P.csv", sep="/");
|
|
1447 write.csv(temperature_data_frame_P, file=file_path, row.names=F);
|
31
|
1448 # Save the analyzed data for generation F1.
|
34
|
1449 file_path = paste("output_data_dir", "02_generation_F1.csv", sep="/");
|
|
1450 write.csv(temperature_data_frame_F1, file=file_path, row.names=F);
|
31
|
1451 # Save the analyzed data for generation F2.
|
34
|
1452 file_path = paste("output_data_dir", "03_generation_F2.csv", sep="/");
|
|
1453 write.csv(temperature_data_frame_F2, file=file_path, row.names=F);
|
31
|
1454 }
|
5
|
1455
|
49
|
1456 total_days_vector = c(1:dim(temperature_data_frame)[1]);
|
10
|
1457 if (plot_generations_separately) {
|
15
|
1458 for (life_stage in life_stages) {
|
10
|
1459 if (life_stage == "Egg") {
|
|
1460 # Start PDF device driver.
|
|
1461 dev.new(width=20, height=30);
|
19
|
1462 file_path = get_file_path(life_stage, "egg_pop_by_generation.pdf")
|
10
|
1463 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1464 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1465 # Egg population size by generation.
|
18
|
1466 maxval = max(P_eggs+F1_eggs+F2_eggs) + 100;
|
45
|
1467 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
|
38
|
1468 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=P_eggs, group_std_error=P_eggs.std_error,
|
|
1469 group2=F1_eggs, group2_std_error=F1_eggs.std_error, group3=F2_eggs, group3_std_error=F2_eggs.std_error);
|
10
|
1470 # Turn off device driver to flush output.
|
|
1471 dev.off();
|
|
1472 } else if (life_stage == "Nymph") {
|
16
|
1473 for (life_stage_nymph in life_stages_nymph) {
|
|
1474 # Start PDF device driver.
|
|
1475 dev.new(width=20, height=30);
|
50
|
1476 file_path = get_file_path(life_stage, "nymph_pop_by_generation.pdf", sub_life_stage=life_stage_nymph)
|
16
|
1477 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1478 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
20
|
1479 if (life_stage_nymph=="Young") {
|
|
1480 # Young nymph population size by generation.
|
|
1481 maxval = max(P_young_nymphs+F1_young_nymphs+F2_young_nymphs) + 100;
|
|
1482 group = P_young_nymphs;
|
|
1483 group_std_error = P_young_nymphs.std_error;
|
|
1484 group2 = F1_young_nymphs;
|
|
1485 group2_std_error = F1_young_nymphs.std_error;
|
|
1486 group3 = F2_young_nymphs;
|
|
1487 group3_std_error = F2_young_nymphs.std_error;
|
|
1488 } else if (life_stage_nymph=="Old") {
|
|
1489 # Total nymph population size by generation.
|
|
1490 maxval = max(P_old_nymphs+F1_old_nymphs+F2_old_nymphs) + 100;
|
|
1491 group = P_old_nymphs;
|
|
1492 group_std_error = P_old_nymphs.std_error;
|
|
1493 group2 = F1_old_nymphs;
|
|
1494 group2_std_error = F1_old_nymphs.std_error;
|
|
1495 group3 = F2_old_nymphs;
|
|
1496 group3_std_error = F2_old_nymphs.std_error;
|
|
1497 } else if (life_stage_nymph=="Total") {
|
|
1498 # Total nymph population size by generation.
|
|
1499 maxval = max(P_total_nymphs+F1_total_nymphs+F2_total_nymphs) + 100;
|
|
1500 group = P_total_nymphs;
|
|
1501 group_std_error = P_total_nymphs.std_error;
|
|
1502 group2 = F1_total_nymphs;
|
|
1503 group2_std_error = F1_total_nymphs.std_error;
|
|
1504 group3 = F2_total_nymphs;
|
|
1505 group3_std_error = F2_total_nymphs.std_error;
|
|
1506 }
|
45
|
1507 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
|
38
|
1508 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
|
50
|
1509 group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, sub_life_stage=life_stage_nymph);
|
16
|
1510 # Turn off device driver to flush output.
|
|
1511 dev.off();
|
|
1512 }
|
10
|
1513 } else if (life_stage == "Adult") {
|
16
|
1514 for (life_stage_adult in life_stages_adult) {
|
|
1515 # Start PDF device driver.
|
|
1516 dev.new(width=20, height=30);
|
50
|
1517 file_path = get_file_path(life_stage, "adult_pop_by_generation.pdf", sub_life_stage=life_stage_adult)
|
16
|
1518 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1519 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
23
|
1520 if (life_stage_adult=="Pre-vittelogenic") {
|
|
1521 # Pre-vittelogenic adult population size by generation.
|
|
1522 maxval = max(P_previttelogenic_adults+F1_previttelogenic_adults+F2_previttelogenic_adults) + 100;
|
|
1523 group = P_previttelogenic_adults;
|
|
1524 group_std_error = P_previttelogenic_adults.std_error;
|
|
1525 group2 = F1_previttelogenic_adults;
|
|
1526 group2_std_error = F1_previttelogenic_adults.std_error;
|
|
1527 group3 = F2_previttelogenic_adults;
|
|
1528 group3_std_error = F2_previttelogenic_adults.std_error;
|
|
1529 } else if (life_stage_adult=="Vittelogenic") {
|
|
1530 # Vittelogenic adult population size by generation.
|
|
1531 maxval = max(P_vittelogenic_adults+F1_vittelogenic_adults+F2_vittelogenic_adults) + 100;
|
|
1532 group = P_vittelogenic_adults;
|
|
1533 group_std_error = P_vittelogenic_adults.std_error;
|
|
1534 group2 = F1_vittelogenic_adults;
|
|
1535 group2_std_error = F1_vittelogenic_adults.std_error;
|
|
1536 group3 = F2_vittelogenic_adults;
|
|
1537 group3_std_error = F2_vittelogenic_adults.std_error;
|
|
1538 } else if (life_stage_adult=="Diapausing") {
|
|
1539 # Diapausing adult population size by generation.
|
|
1540 maxval = max(P_diapausing_adults+F1_diapausing_adults+F2_diapausing_adults) + 100;
|
|
1541 group = P_diapausing_adults;
|
|
1542 group_std_error = P_diapausing_adults.std_error;
|
|
1543 group2 = F1_diapausing_adults;
|
|
1544 group2_std_error = F1_diapausing_adults.std_error;
|
|
1545 group3 = F2_diapausing_adults;
|
|
1546 group3_std_error = F2_diapausing_adults.std_error;
|
|
1547 } else if (life_stage_adult=="Total") {
|
|
1548 # Total adult population size by generation.
|
|
1549 maxval = max(P_total_adults+F1_total_adults+F2_total_adults) + 100;
|
|
1550 group = P_total_adults;
|
|
1551 group_std_error = P_total_adults.std_error;
|
|
1552 group2 = F1_total_adults;
|
|
1553 group2_std_error = F1_total_adults.std_error;
|
|
1554 group3 = F2_total_adults;
|
|
1555 group3_std_error = F2_total_adults.std_error;
|
|
1556 }
|
45
|
1557 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
|
38
|
1558 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
|
50
|
1559 group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, sub_life_stage=life_stage_adult);
|
16
|
1560 # Turn off device driver to flush output.
|
|
1561 dev.off();
|
|
1562 }
|
10
|
1563 } else if (life_stage == "Total") {
|
|
1564 # Start PDF device driver.
|
18
|
1565 # Name collection elements so that they
|
|
1566 # are displayed in logical order.
|
10
|
1567 dev.new(width=20, height=30);
|
19
|
1568 file_path = get_file_path(life_stage, "total_pop_by_generation.pdf")
|
10
|
1569 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1570 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1571 # Total population size by generation.
|
18
|
1572 maxval = max(P+F1+F2) + 100;
|
45
|
1573 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
|
38
|
1574 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=P, group_std_error=P.std_error,
|
|
1575 group2=F1, group2_std_error=F1.std_error, group3=F2, group3_std_error=F2.std_error);
|
10
|
1576 # Turn off device driver to flush output.
|
|
1577 dev.off();
|
|
1578 }
|
15
|
1579 }
|
10
|
1580 } else {
|
|
1581 for (life_stage in life_stages) {
|
|
1582 if (life_stage == "Egg") {
|
|
1583 # Start PDF device driver.
|
|
1584 dev.new(width=20, height=30);
|
19
|
1585 file_path = get_file_path(life_stage, "egg_pop.pdf")
|
10
|
1586 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1587 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1588 # Egg population size.
|
18
|
1589 maxval = max(eggs+eggs.std_error) + 100;
|
45
|
1590 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
|
38
|
1591 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=eggs, group_std_error=eggs.std_error);
|
10
|
1592 # Turn off device driver to flush output.
|
|
1593 dev.off();
|
|
1594 } else if (life_stage == "Nymph") {
|
16
|
1595 for (life_stage_nymph in life_stages_nymph) {
|
|
1596 # Start PDF device driver.
|
|
1597 dev.new(width=20, height=30);
|
50
|
1598 file_path = get_file_path(life_stage, "nymph_pop.pdf", sub_life_stage=life_stage_nymph)
|
16
|
1599 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1600 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1601 if (life_stage_nymph=="Total") {
|
|
1602 # Total nymph population size.
|
|
1603 group = total_nymphs;
|
|
1604 group_std_error = total_nymphs.std_error;
|
|
1605 } else if (life_stage_nymph=="Young") {
|
|
1606 # Young nymph population size.
|
|
1607 group = young_nymphs;
|
|
1608 group_std_error = young_nymphs.std_error;
|
|
1609 } else if (life_stage_nymph=="Old") {
|
|
1610 # Old nymph population size.
|
|
1611 group = old_nymphs;
|
|
1612 group_std_error = old_nymphs.std_error;
|
|
1613 }
|
18
|
1614 maxval = max(group+group_std_error) + 100;
|
45
|
1615 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
|
38
|
1616 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
|
50
|
1617 sub_life_stage=life_stage_nymph);
|
16
|
1618 # Turn off device driver to flush output.
|
|
1619 dev.off();
|
|
1620 }
|
10
|
1621 } else if (life_stage == "Adult") {
|
16
|
1622 for (life_stage_adult in life_stages_adult) {
|
|
1623 # Start PDF device driver.
|
|
1624 dev.new(width=20, height=30);
|
50
|
1625 file_path = get_file_path(life_stage, "adult_pop.pdf", sub_life_stage=life_stage_adult)
|
16
|
1626 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1627 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1628 if (life_stage_adult=="Total") {
|
|
1629 # Total adult population size.
|
|
1630 group = total_adults;
|
|
1631 group_std_error = total_adults.std_error
|
|
1632 } else if (life_stage_adult=="Pre-vittelogenic") {
|
|
1633 # Pre-vittelogenic adult population size.
|
|
1634 group = previttelogenic_adults;
|
|
1635 group_std_error = previttelogenic_adults.std_error
|
|
1636 } else if (life_stage_adult=="Vittelogenic") {
|
|
1637 # Vittelogenic adult population size.
|
|
1638 group = vittelogenic_adults;
|
|
1639 group_std_error = vittelogenic_adults.std_error
|
|
1640 } else if (life_stage_adult=="Diapausing") {
|
|
1641 # Diapausing adult population size.
|
|
1642 group = diapausing_adults;
|
|
1643 group_std_error = diapausing_adults.std_error
|
|
1644 }
|
18
|
1645 maxval = max(group+group_std_error) + 100;
|
45
|
1646 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
|
38
|
1647 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
|
50
|
1648 sub_life_stage=life_stage_adult);
|
16
|
1649 # Turn off device driver to flush output.
|
|
1650 dev.off();
|
|
1651 }
|
10
|
1652 } else if (life_stage == "Total") {
|
|
1653 # Start PDF device driver.
|
|
1654 dev.new(width=20, height=30);
|
19
|
1655 file_path = get_file_path(life_stage, "total_pop.pdf")
|
10
|
1656 pdf(file=file_path, width=20, height=30, bg="white");
|
|
1657 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
|
|
1658 # Total population size.
|
18
|
1659 maxval = max(eggs+eggs.std_error, total_nymphs+total_nymphs.std_error, total_adults+total_adults.std_error) + 100;
|
45
|
1660 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
|
38
|
1661 start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=total_adults, group_std_error=total_adults.std_error,
|
|
1662 group2=total_nymphs, group2_std_error=total_nymphs.std_error, group3=eggs, group3_std_error=eggs.std_error);
|
10
|
1663 # Turn off device driver to flush output.
|
|
1664 dev.off();
|
|
1665 }
|
|
1666 }
|
|
1667 }
|