annotate insect_phenology_model.R @ 45:315c5e1bc44a draft

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