annotate insect_phenology_model.R @ 17:6f31ade9a0f4 draft

Uploaded
author greg
date Tue, 06 Mar 2018 08:26:50 -0500
parents 309954bbe999
children f5ecff4800f2
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"),
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
9 make_option(c("--input"), action="store", dest="input", help="Temperature data for selected location"),
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
10 make_option(c("--insect"), action="store", dest="insect", help="Insect name"),
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
11 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
12 make_option(c("--life_stages"), action="store", dest="life_stages", help="Selected life stages for plotting"),
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
13 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
14 make_option(c("--life_stages_nymph"), action="store", dest="life_stages_nymph", default=NULL, help="Nymph life stages for plotting"),
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
15 make_option(c("--location"), action="store", dest="location", help="Selected location"),
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
16 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
17 make_option(c("--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
18 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
19 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
20 make_option(c("--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"),
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
21 make_option(c("--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
22 make_option(c("--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
23 make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
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"),
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
26 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
27 )
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
28
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
29 parser <- OptionParser(usage="%prog [options] file", option_list=option_list);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
30 args <- parse_args(parser, positional_arguments=TRUE);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
31 opt <- args$options;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
32
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
33 add_daylight_length = function(temperature_data_frame, num_columns, num_rows) {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
34 # Return a vector of daylight length (photoperido profile) for
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
35 # the number of days specified in the input temperature data
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
36 # (from Forsythe 1995).
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
37 p = 0.8333;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
38 latitude = temperature_data_frame$LATITUDE[1];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
39 daylight_length_vector = NULL;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
40 for (i in 1:num_rows) {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
41 # Get the day of the year from the current row
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
42 # of the temperature data for computation.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
43 doy = temperature_data_frame$DOY[i];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
44 theta = 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (doy - 186)));
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
45 phi = asin(0.39795 * cos(theta));
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
46 # Compute the length of daylight for the day of the year.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
47 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
48 daylight_length_vector[i] = 24 - darkness_length;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
49 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
50 # Append daylight_length_vector as a new column to temperature_data_frame.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
51 temperature_data_frame[, num_columns+1] = daylight_length_vector;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
52 return(temperature_data_frame);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
53 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
54
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
55 dev.egg = function(temperature) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
56 dev.rate = -0.9843 * temperature + 33.438;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
57 return(dev.rate);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
58 }
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
59
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
60 dev.emerg = function(temperature) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
61 emerg.rate = -0.5332 * temperature + 24.147;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
62 return(emerg.rate);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
63 }
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
64
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
65 dev.old = function(temperature) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
66 n34 = -0.6119 * temperature + 17.602;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
67 n45 = -0.4408 * temperature + 19.036;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
68 dev.rate = mean(n34 + n45);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
69 return(dev.rate);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
70 }
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
71
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
72 dev.young = function(temperature) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
73 n12 = -0.3728 * temperature + 14.68;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
74 n23 = -0.6119 * temperature + 25.249;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
75 dev.rate = mean(n12 + n23);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
76 return(dev.rate);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
77 }
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
78
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
79 get_date_labels = function(temperature_data_frame, num_rows) {
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
80 # Keep track of the years to see if spanning years.
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
81 month_labels = list();
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
82 current_month_label = NULL;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
83 for (i in 1:num_rows) {
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
84 # Get the year and month from the date which
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
85 # has the format YYYY-MM-DD.
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
86 date = format(temperature_data_frame$DATE[i]);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
87 items = strsplit(date, "-")[[1]];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
88 month = items[2];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
89 month_label = month.abb[as.integer(month)];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
90 if (!identical(current_month_label, month_label)) {
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
91 month_labels[length(month_labels)+1] = month_label;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
92 current_month_label = month_label;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
93 }
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
94 }
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
95 return(c(unlist(month_labels)));
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
96 }
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
97
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
98 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
99 # Base development threshold for Brown Marmorated Stink Bug
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
100 # insect phenology model.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
101 threshold = 14.17;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
102 # Minimum temperature for current row.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
103 curr_min_temp = temperature_data_frame$TMIN[row];
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
104 # Maximum temperature for current row.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
105 curr_max_temp = temperature_data_frame$TMAX[row];
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
106 # Mean temperature for current row.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
107 curr_mean_temp = 0.5 * (curr_min_temp + curr_max_temp);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
108 # Initialize degree day accumulation
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
109 averages = 0;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
110 if (curr_max_temp < threshold) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
111 averages = 0;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
112 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
113 else {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
114 # Initialize hourly temperature.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
115 T = NULL;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
116 # Initialize degree hour vector.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
117 dh = NULL;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
118 # Daylight length for current row.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
119 y = temperature_data_frame$DAYLEN[row];
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
120 # Darkness length.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
121 z = 24 - y;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
122 # Lag coefficient.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
123 a = 1.86;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
124 # Darkness coefficient.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
125 b = 2.20;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
126 # Sunrise time.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
127 risetime = 12 - y / 2;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
128 # Sunset time.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
129 settime = 12 + y / 2;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
130 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
131 for (i in 1:24) {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
132 if (i > risetime && i < settime) {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
133 # Number of hours after Tmin until sunset.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
134 m = i - 5;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
135 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
136 if (T[i] < 8.4) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
137 dh[i] = 0;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
138 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
139 else {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
140 dh[i] = T[i] - 8.4;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
141 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
142 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
143 else if (i > settime) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
144 n = i - settime;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
145 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
146 if (T[i] < 8.4) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
147 dh[i] = 0;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
148 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
149 else {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
150 dh[i] = T[i] - 8.4;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
151 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
152 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
153 else {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
154 n = i + 24 - settime;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
155 T[i] = curr_min_temp + (ts - curr_min_temp) * exp( - b * n / z);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
156 if (T[i] < 8.4) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
157 dh[i] = 0;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
158 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
159 else {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
160 dh[i] = T[i] - 8.4;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
161 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
162 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
163 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
164 averages = sum(dh) / 24;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
165 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
166 return(c(curr_mean_temp, averages))
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
167 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
168
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
169 mortality.adult = function(temperature) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
170 if (temperature < 12.7) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
171 mortality.probability = 0.002;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
172 }
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
173 else {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
174 mortality.probability = temperature * 0.0005 + 0.02;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
175 }
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
176 return(mortality.probability)
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
177 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
178
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
179 mortality.egg = function(temperature) {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
180 if (temperature < 12.7) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
181 mortality.probability = 0.8;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
182 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
183 else {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
184 mortality.probability = 0.8 - temperature / 40.0;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
185 if (mortality.probability < 0) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
186 mortality.probability = 0.01;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
187 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
188 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
189 return(mortality.probability)
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
190 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
191
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
192 mortality.nymph = function(temperature) {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
193 if (temperature < 12.7) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
194 mortality.probability = 0.03;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
195 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
196 else {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
197 mortality.probability = temperature * 0.0008 + 0.03;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
198 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
199 return(mortality.probability);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
200 }
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
201
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
202 parse_input_data = function(input_file, num_rows) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
203 # Read in the input temperature datafile into a data frame.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
204 temperature_data_frame = read.csv(file=input_file, header=T, strip.white=TRUE, sep=",");
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
205 num_columns = dim(temperature_data_frame)[2];
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
206 if (num_columns == 6) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
207 # The input data has the following 6 columns:
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
208 # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
209 # Set the column names for access when adding daylight length..
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
210 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
211 # Add a column containing the daylight length for each day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
212 temperature_data_frame = add_daylight_length(temperature_data_frame, num_columns, num_rows);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
213 # Reset the column names with the additional column for later access.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
214 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX", "DAYLEN");
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
215 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
216 return(temperature_data_frame);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
217 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
218
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
219
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
220 render_chart = function(date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
221 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
222 life_stages_adult=NULL, life_stages_nymph=NULL) {
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
223 if (chart_type=="pop_size_by_life_stage") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
224 if (life_stage=="Total") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
225 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
226 legend_text = c("Egg", "Nymph", "Adult");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
227 columns = c(4, 2, 1);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
228 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
229 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
230 lines(days, group2, lwd=2, lty=1, col=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
231 lines(days, group3, lwd=2, lty=1, col=4);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
232 axis(1, at=c(1:length(date_labels)) * 30 - 15, cex.axis=3, labels=date_labels);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
233 axis(2, cex.axis=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
234 if (plot_std_error=="yes") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
235 # Standard error for group.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
236 lines(days, group+group_std_error, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
237 lines(days, group-group_std_error, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
238 # Standard error for group2.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
239 lines(days, group2+group2_std_error, col=2, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
240 lines(days, group2-group2_std_error, col=2, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
241 # Standard error for group3.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
242 lines(days, group3+group3_std_error, col=4, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
243 lines(days, group3-group3_std_error, col=4, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
244 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
245 } else {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
246 if (life_stage=="Egg") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
247 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
248 legend_text = c(life_stage);
15
dd86ee185113 Uploaded
greg
parents: 11
diff changeset
249 columns = c(4);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
250 } else if (life_stage=="Nymph") {
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
251 stage = paste(life_stages_nymph, "Nymph Pop :", sep=" ");
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
252 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
253 legend_text = c(paste(life_stages_nymph, life_stage, sep=" "));
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
254 columns = c(2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
255 } else if (life_stage=="Adult") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
256 stage = paste(life_stages_adult, "Adult Pop", sep=" ");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
257 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
258 legend_text = c(paste(life_stages_adult, life_stage, sep=" "));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
259 columns = c(1);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
260 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
261 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
262 legend("topleft", legend_text, lty=c(1), col="black", cex=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
263 axis(1, at=c(1:length(date_labels)) * 30 - 15, cex.axis=3, labels=date_labels);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
264 axis(2, cex.axis=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
265 if (plot_std_error=="yes") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
266 # Standard error for group.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
267 lines(days, group+group_std_error, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
268 lines(days, group-group_std_error, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
269 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
270 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
271 } else if (chart_type=="pop_size_by_generation") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
272 if (life_stage=="Total") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
273 title_str = ": Total Pop by Gen :";
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
274 } else if (life_stage=="Egg") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
275 title_str = ": Egg Pop by Gen :";
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
276 } else if (life_stage=="Nymph") {
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
277 title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" ");
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
278 } else if (life_stage=="Adult") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
279 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" ");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
280 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
281 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
282 legend_text = c("P", "F1", "F2");
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
283 columns = c(1, 2, 4);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
284 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
285 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
286 lines(days, group2, lwd=2, lty=1, col=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
287 lines(days, group3, lwd=2, lty=1, col=4);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
288 axis(1, at=c(1:length(date_labels)) * 30 - 15, cex.axis=3, labels=date_labels);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
289 axis(2, cex.axis=3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
290 if (plot_std_error=="yes") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
291 # Standard error for group.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
292 lines(days, group+group_std_error, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
293 lines(days, group-group_std_error, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
294 # Standard error for group2.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
295 lines(days, group2+group2_std_error, col=2, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
296 lines(days, group2-group2_std_error, col=2, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
297 # Standard error for group3.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
298 lines(days, group3+group3_std_error, col=4, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
299 lines(days, group3-group3_std_error, col=4, lty=2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
300 }
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
301 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
302 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
303
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
304 # Determine if we're plotting generations separately.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
305 if (opt$plot_generations_separately=="yes") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
306 plot_generations_separately = TRUE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
307 } else {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
308 plot_generations_separately = FALSE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
309 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
310 # Read the temperature data into a data frame.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
311 temperature_data_frame = parse_input_data(opt$input, opt$num_days);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
312 output_dir = "output_dir";
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
313 # Get the date labels for plots.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
314 date_labels = get_date_labels(temperature_data_frame, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
315 # All latitude values are the same, so get the value for plots from the first row.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
316 latitude = temperature_data_frame$LATITUDE[1];
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
317 # Get the number of days for plots.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
318 num_columns = dim(temperature_data_frame)[2];
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
319 # Split life_stages into a list of strings for plots.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
320 life_stages_str = as.character(opt$life_stages);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
321 life_stages = strsplit(life_stages_str, ",")[[1]];
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
322 # Determine the data we need to generate for plotting.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
323 process_eggs = FALSE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
324 process_nymphs = FALSE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
325 process_adults = FALSE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
326 for (life_stage in life_stages) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
327 if (life_stage=="Total") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
328 process_eggs = TRUE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
329 process_nymphs = TRUE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
330 process_adults = TRUE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
331 } else if (life_stage=="Egg") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
332 process_eggs = TRUE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
333 } else if (life_stage=="Nymph") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
334 process_nymphs = TRUE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
335 } else if (life_stage=="Adult") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
336 process_adults = TRUE;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
337 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
338 }
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
339 if (process_adults) {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
340 # Split life_stages_adult into a list of strings for plots.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
341 life_stages_adult_str = as.character(opt$life_stages_adult);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
342 life_stages_adult = strsplit(life_stages_adult_str, ",")[[1]];
309954bbe999 Uploaded
greg
parents: 15
diff changeset
343 }
309954bbe999 Uploaded
greg
parents: 15
diff changeset
344 if (process_nymphs) {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
345 # Split life_stages_nymph into a list of strings for plots.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
346 life_stages_nymph_str = as.character(opt$life_stages_nymph);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
347 life_stages_nymph = strsplit(life_stages_nymph_str, ",")[[1]];
309954bbe999 Uploaded
greg
parents: 15
diff changeset
348 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
349 # Initialize matrices.
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
350 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
351 Eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
352 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
353 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
354 YoungNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
355 OldNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
356 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
357 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
358 Previtellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
359 Vitellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
360 Diapausing.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
361 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
362 newborn.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
363 adult.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
364 death.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
365 if (plot_generations_separately) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
366 # P is Parental, or overwintered adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
367 P.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
368 # F1 is the first field-produced generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
369 F1.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
370 # F2 is the second field-produced generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
371 F2.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
372 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
373 P_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
374 F1_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
375 F2_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
376 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
377 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
378 P_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
379 F1_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
380 F2_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
381 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
382 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
383 P_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
384 F1_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
385 F2_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
386 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
387 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
388 # Total population.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
389 population.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
390
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
391 # Process replications.
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
392 for (N.replications in 1:opt$replications) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
393 # Start with the user-defined number of insects per replication.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
394 num_insects = opt$insects_per_replication;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
395 # Generation, Stage, degree-days, T, Diapause.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
396 vector.ini = c(0, 3, 0, 0, 0);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
397 # Replicate to create a matrix where the columns are
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
398 # Generation, Stage, degree-days, T, Diapause and the
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
399 # rows are the initial number of insects per replication.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
400 vector.matrix = rep(vector.ini, num_insects);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
401 # Complete transposed matrix for the population, so now
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
402 # the rows are Generation, Stage, degree-days, T, Diapause
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
403 vector.matrix = base::t(matrix(vector.matrix, nrow=5));
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
404 # Time series of population size.
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
405 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
406 Eggs = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
407 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
408 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
409 YoungNymphs = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
410 OldNymphs = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
411 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
412 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
413 Previtellogenic = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
414 Vitellogenic = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
415 Diapausing = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
416 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
417 N.newborn = rep(0, opt$num_days);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
418 N.adult = rep(0, opt$num_days);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
419 N.death = rep(0, opt$num_days);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
420 overwintering_adult.population = rep(0, opt$num_days);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
421 first_generation.population = rep(0, opt$num_days);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
422 second_generation.population = rep(0, opt$num_days);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
423 if (plot_generations_separately) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
424 # P is Parental, or overwintered adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
425 # F1 is the first field-produced generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
426 # F2 is the second field-produced generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
427 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
428 P.egg = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
429 F1.egg = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
430 F2.egg = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
431 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
432 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
433 P.nymph = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
434 F1.nymph = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
435 F2.nymph = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
436 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
437 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
438 P.adult = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
439 F1.adult = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
440 F2.adult = rep(0, opt$num_days);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
441 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
442 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
443 total.population = NULL;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
444 averages.day = rep(0, opt$num_days);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
445 # All the days included in the input temperature dataset.
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
446 for (row in 1:opt$num_days) {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
447 # Get the integer day of the year for the current row.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
448 doy = temperature_data_frame$DOY[row];
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
449 # Photoperiod in the day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
450 photoperiod = temperature_data_frame$DAYLEN[row];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
451 temp.profile = get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
452 mean.temp = temp.profile[1];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
453 averages.temp = temp.profile[2];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
454 averages.day[row] = averages.temp;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
455 # Trash bin for death.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
456 death.vector = NULL;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
457 # Newborn.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
458 birth.vector = NULL;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
459 # All individuals.
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
460 for (i in 1:num_insects) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
461 # Individual record.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
462 vector.individual = vector.matrix[i,];
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
463 # Adjustment for late season mortality rate (still alive?).
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
464 if (latitude < 40.0) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
465 post.mortality = 1;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
466 day.kill = 300;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
467 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
468 else {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
469 post.mortality = 2;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
470 day.kill = 250;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
471 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
472 if (vector.individual[2] == 0) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
473 # Egg.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
474 death.probability = opt$egg_mortality * mortality.egg(mean.temp);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
475 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
476 else if (vector.individual[2] == 1 | vector.individual[2] == 2) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
477 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
478 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
479 else if (vector.individual[2] == 3 | vector.individual[2] == 4 | vector.individual[2] == 5) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
480 # Adult.
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
481 if (doy < day.kill) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
482 death.probability = opt$adult_mortality * mortality.adult(mean.temp);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
483 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
484 else {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
485 # Increase adult mortality after fall equinox.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
486 death.probability = opt$adult_mortality * post.mortality * mortality.adult(mean.temp);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
487 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
488 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
489 # Dependent on temperature and life stage?
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
490 u.d = runif(1);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
491 if (u.d < death.probability) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
492 death.vector = c(death.vector, i);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
493 }
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
494 else {
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
495 # End of diapause.
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
496 if (vector.individual[1] == 0 && vector.individual[2] == 3) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
497 # Overwintering adult (previttelogenic).
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
498 if (photoperiod > opt$photoperiod && vector.individual[3] > 68 && doy < 180) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
499 # Add 68C to become fully reproductively matured.
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
500 # Transfer to vittelogenic.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
501 vector.individual = c(0, 4, 0, 0, 0);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
502 vector.matrix[i,] = vector.individual;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
503 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
504 else {
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
505 # Add to # Add average temperature for current day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
506 vector.individual[3] = vector.individual[3] + averages.temp;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
507 # Add 1 day in current stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
508 vector.individual[4] = vector.individual[4] + 1;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
509 vector.matrix[i,] = vector.individual;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
510 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
511 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
512 if (vector.individual[1] != 0 && vector.individual[2] == 3) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
513 # Not overwintering adult (previttelogenic).
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
514 current.gen = vector.individual[1];
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
515 if (vector.individual[3] > 68) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
516 # Add 68C to become fully reproductively matured.
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
517 # Transfer to vittelogenic.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
518 vector.individual = c(current.gen, 4, 0, 0, 0);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
519 vector.matrix[i,] = vector.individual;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
520 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
521 else {
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
522 # Add average temperature for current day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
523 vector.individual[3] = vector.individual[3] + averages.temp;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
524 # Add 1 day in current stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
525 vector.individual[4] = vector.individual[4] + 1;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
526 vector.matrix[i,] = vector.individual;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
527 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
528 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
529 # Oviposition -- where population dynamics comes from.
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
530 if (vector.individual[2] == 4 && vector.individual[1] == 0 && mean.temp > 10) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
531 # Vittelogenic stage, overwintering generation.
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
532 if (vector.individual[4] == 0) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
533 # Just turned in vittelogenic stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
534 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size));
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
535 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
536 else {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
537 # Daily probability of birth.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
538 p.birth = opt$oviposition * 0.01;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
539 u1 = runif(1);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
540 if (u1 < p.birth) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
541 num_insects.birth = round(runif(1, 2, 8));
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
542 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
543 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
544 # Add average temperature for current day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
545 vector.individual[3] = vector.individual[3] + averages.temp;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
546 # Add 1 day in current stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
547 vector.individual[4] = vector.individual[4] + 1;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
548 vector.matrix[i,] = vector.individual;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
549 if (num_insects.birth > 0) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
550 # Add new birth -- might be in different generations.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
551 new.gen = vector.individual[1] + 1;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
552 # Egg profile.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
553 new.individual = c(new.gen, 0, 0, 0, 0);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
554 new.vector = rep(new.individual, num_insects.birth);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
555 # Update batch of egg profile.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
556 new.vector = t(matrix(new.vector, nrow=5));
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
557 # Group with total eggs laid in that day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
558 birth.vector = rbind(birth.vector, new.vector);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
559 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
560 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
561 # Oviposition -- for generation 1.
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
562 if (vector.individual[2] == 4 && vector.individual[1] == 1 && mean.temp > 12.5 && doy < 222) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
563 # Vittelogenic stage, 1st generation
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
564 if (vector.individual[4] == 0) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
565 # Just turned in vittelogenic stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
566 num_insects.birth = round(runif(1, 2+opt$min_clutch_size, 8+opt$max_clutch_size));
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
567 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
568 else {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
569 # Daily probability of birth.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
570 p.birth = opt$oviposition * 0.01;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
571 u1 = runif(1);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
572 if (u1 < p.birth) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
573 num_insects.birth = round(runif(1, 2, 8));
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
574 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
575 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
576 # Add average temperature for current day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
577 vector.individual[3] = vector.individual[3] + averages.temp;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
578 # Add 1 day in current stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
579 vector.individual[4] = vector.individual[4] + 1;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
580 vector.matrix[i,] = vector.individual;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
581 if (num_insects.birth > 0) {
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
582 # Add new birth -- might be in different generations.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
583 new.gen = vector.individual[1] + 1;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
584 # Egg profile.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
585 new.individual = c(new.gen, 0, 0, 0, 0);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
586 new.vector = rep(new.individual, num_insects.birth);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
587 # Update batch of egg profile.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
588 new.vector = t(matrix(new.vector, nrow=5));
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
589 # Group with total eggs laid in that day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
590 birth.vector = rbind(birth.vector, new.vector);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
591 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
592 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
593 # Egg to young nymph.
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
594 if (vector.individual[2] == 0) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
595 # Add average temperature for current day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
596 vector.individual[3] = vector.individual[3] + averages.temp;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
597 if (vector.individual[3] >= (68+opt$young_nymph_accumulation)) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
598 # From egg to young nymph, degree-days requirement met.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
599 current.gen = vector.individual[1];
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
600 # Transfer to young nymph stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
601 vector.individual = c(current.gen, 1, 0, 0, 0);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
602 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
603 else {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
604 # Add 1 day in current stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
605 vector.individual[4] = vector.individual[4] + 1;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
606 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
607 vector.matrix[i,] = vector.individual;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
608 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
609 # Young nymph to old nymph.
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
610 if (vector.individual[2] == 1) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
611 # Add average temperature for current day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
612 vector.individual[3] = vector.individual[3] + averages.temp;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
613 if (vector.individual[3] >= (250+opt$old_nymph_accumulation)) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
614 # From young to old nymph, degree_days requirement met.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
615 current.gen = vector.individual[1];
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
616 # Transfer to old nym stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
617 vector.individual = c(current.gen, 2, 0, 0, 0);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
618 if (photoperiod < opt$photoperiod && doy > 180) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
619 vector.individual[5] = 1;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
620 } # Prepare for diapausing.
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
621 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
622 else {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
623 # Add 1 day in current stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
624 vector.individual[4] = vector.individual[4] + 1;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
625 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
626 vector.matrix[i,] = vector.individual;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
627 }
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
628 # Old nymph to adult: previttelogenic or diapausing?
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
629 if (vector.individual[2] == 2) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
630 # Add average temperature for current day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
631 vector.individual[3] = vector.individual[3] + averages.temp;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
632 if (vector.individual[3] >= (200+opt$adult_accumulation)) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
633 # From old to adult, degree_days requirement met.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
634 current.gen = vector.individual[1];
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
635 if (vector.individual[5] == 0) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
636 # Previttelogenic.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
637 vector.individual = c(current.gen, 3, 0, 0, 0);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
638 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
639 else {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
640 # Diapausing.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
641 vector.individual = c(current.gen, 5, 0, 0, 1);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
642 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
643 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
644 else {
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
645 # Add 1 day in current stage.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
646 vector.individual[4] = vector.individual[4] + 1;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
647 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
648 vector.matrix[i,] = vector.individual;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
649 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
650 # Growing of diapausing adult (unimportant, but still necessary).
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
651 if (vector.individual[2] == 5) {
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
652 vector.individual[3] = vector.individual[3] + averages.temp;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
653 vector.individual[4] = vector.individual[4] + 1;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
654 vector.matrix[i,] = vector.individual;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
655 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
656 } # Else if it is still alive.
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
657 } # End of the individual bug loop.
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
658
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
659 # Number of deaths.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
660 num_insects.death = length(death.vector);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
661 if (num_insects.death > 0) {
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
662 # Remove record of dead.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
663 vector.matrix = vector.matrix[-death.vector,];
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
664 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
665 # Number of births.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
666 num_insects.newborn = length(birth.vector[,1]);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
667 vector.matrix = rbind(vector.matrix, birth.vector);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
668 # Update population size for the next day.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
669 num_insects = num_insects - num_insects.death + num_insects.newborn;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
670
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
671 # Aggregate results by day. Due to multiple transpose calls
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
672 # on vector.matrix above, the columns of vector.matrix
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
673 # are now Generation, Stage, degree-days, T, Diapause,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
674 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
675 # For egg population size, column 2 (Stage), must be 0.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
676 Eggs[row] = sum(vector.matrix[,2]==0);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
677 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
678 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
679 # For young nymph population size, column 2 (Stage) must be 1.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
680 YoungNymphs[row] = sum(vector.matrix[,2]==1);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
681 # For old nymph population size, column 2 (Stage) must be 2.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
682 OldNymphs[row] = sum(vector.matrix[,2]==2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
683 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
684 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
685 # For pre-vitellogenic population size, column 2 (Stage) must be 3.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
686 Previtellogenic[row] = sum(vector.matrix[,2]==3);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
687 # For vitellogenic population size, column 2 (Stage) must be 4.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
688 Vitellogenic[row] = sum(vector.matrix[,2]==4);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
689 # For diapausing population size, column 2 (Stage) must be 5.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
690 Diapausing[row] = sum(vector.matrix[,2]==5);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
691 }
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
692
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
693 # Newborn population size.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
694 N.newborn[row] = num_insects.newborn;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
695 # Adult population size.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
696 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
697 # Dead population size.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
698 N.death[row] = num_insects.death;
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
699
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
700 total.population = c(total.population, num_insects);
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
701
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
702 # For overwintering adult (P) population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
703 # size, column 1 (Generation) must be 0.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
704 overwintering_adult.population[row] = sum(vector.matrix[,1]==0);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
705 # For first field generation (F1) population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
706 # size, column 1 (Generation) must be 1.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
707 first_generation.population[row] = sum(vector.matrix[,1]==1);
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
708 # For second field generation (F2) population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
709 # size, column 1 (Generation) must be 2.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
710 second_generation.population[row] = sum(vector.matrix[,1]==2);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
711
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
712 if (plot_generations_separately) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
713 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
714 # For egg life stage of generation F1 population size,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
715 # column 1 (generation) is 0 and column 2 (Stage) is 0.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
716 P.egg[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==0);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
717 # For egg life stage of generation F1 population size,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
718 # column 1 (generation) is 1 and column 2 (Stage) is 0.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
719 F1.egg[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==0);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
720 # For egg life stage of generation F2 population size,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
721 # column 1 (generation) is 2 and column 2 (Stage) is 0.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
722 F2.egg[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==0);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
723 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
724 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
725 # For nymph life stage of generation F1 population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
726 # size, one of the following combinations is required:
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
727 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
728 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
729 P.nymph[row] = sum((vector.matrix[,1]==0 & vector.matrix[,2]==1) | (vector.matrix[,1]==0 & vector.matrix[,2]==2));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
730 # For nymph life stage of generation F1 population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
731 # size, one of the following combinations is required:
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
732 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
733 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
734 F1.nymph[row] = sum((vector.matrix[,1]==1 & vector.matrix[,2]==1) | (vector.matrix[,1]==1 & vector.matrix[,2]==2));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
735 # For nymph life stage of generation F2 population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
736 # size, one of the following combinations is required:
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
737 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
738 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
739 F2.nymph[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==1) | (vector.matrix[,1]==2 & vector.matrix[,2]==2));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
740 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
741 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
742 # For adult life stage of generation P population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
743 # size, one of the following combinations is required:
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
744 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vitellogenic)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
745 # - column 1 (Generation) is 0 and column 2 (Stage) is 4 (Vitellogenic)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
746 # - column 1 (Generation) is 0 and column 2 (Stage) is 5 (Diapausing)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
747 P.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));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
748 # For adult life stage of generation F1 population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
749 # size, one of the following combinations is required:
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
750 # - column 1 (Generation) is 1 and column 2 (Stage) is 3 (Pre-vitellogenic)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
751 # - column 1 (Generation) is 1 and column 2 (Stage) is 4 (Vitellogenic)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
752 # - column 1 (Generation) is 1 and column 2 (Stage) is 5 (Diapausing)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
753 F1.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));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
754 # For adult life stage of generation F2 population
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
755 # size, one of the following combinations is required:
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
756 # - column 1 (Generation) is 2 and column 2 (Stage) is 3 (Pre-vitellogenic)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
757 # - column 1 (Generation) is 2 and column 2 (Stage) is 4 (Vitellogenic)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
758 # - column 1 (Generation) is 2 and column 2 (Stage) is 5 (Diapausing)
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
759 F2.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));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
760 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
761 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
762 } # End of days specified in the input temperature data.
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
763
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
764 averages.cum = cumsum(averages.day);
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
765
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
766 # Define the output values.
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
767 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
768 Eggs.replications[,N.replications] = Eggs;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
769 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
770 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
771 YoungNymphs.replications[,N.replications] = YoungNymphs;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
772 OldNymphs.replications[,N.replications] = OldNymphs;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
773 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
774 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
775 Previtellogenic.replications[,N.replications] = Previtellogenic;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
776 Vitellogenic.replications[,N.replications] = Vitellogenic;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
777 Diapausing.replications[,N.replications] = Diapausing;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
778 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
779 newborn.replications[,N.replications] = N.newborn;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
780 adult.replications[,N.replications] = N.adult;
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
781 death.replications[,N.replications] = N.death;
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
782 if (plot_generations_separately) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
783 # P is Parental, or overwintered adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
784 P.replications[,N.replications] = overwintering_adult.population;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
785 # F1 is the first field-produced generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
786 F1.replications[,N.replications] = first_generation.population;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
787 # F2 is the second field-produced generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
788 F2.replications[,N.replications] = second_generation.population;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
789 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
790 P_eggs.replications[,N.replications] = P.egg;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
791 F1_eggs.replications[,N.replications] = F1.egg;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
792 F2_eggs.replications[,N.replications] = F2.egg;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
793 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
794 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
795 P_nymphs.replications[,N.replications] = P.nymph;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
796 F1_nymphs.replications[,N.replications] = F1.nymph;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
797 F2_nymphs.replications[,N.replications] = F2.nymph;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
798 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
799 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
800 P_adults.replications[,N.replications] = P.adult;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
801 F1_adults.replications[,N.replications] = F1.adult;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
802 F2_adults.replications[,N.replications] = F2.adult;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
803 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
804 }
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
805 population.replications[,N.replications] = total.population;
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
806 }
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
807
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
808 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
809 # Mean value for eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
810 eggs = apply(Eggs.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
811 # Standard error for eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
812 eggs.std_error = apply(Eggs.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
813 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
814 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
815 # Calculate nymph populations for selected life stage.
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
816 for (life_stage_nymph in life_stages_nymph) {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
817 if (life_stages_nymph=="Total") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
818 # Mean value for all nymphs.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
819 total_nymphs = apply((YoungNymphs.replications+OldNymphs.replications), 1, mean);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
820 # Standard error for all nymphs.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
821 total_nymphs.std_error = apply((YoungNymphs.replications+OldNymphs.replications) / sqrt(opt$replications), 1, sd);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
822 } else if (life_stages_nymph=="Young") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
823 # Mean value for young nymphs.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
824 young_nymphs = apply(YoungNymphs.replications, 1, mean);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
825 # Standard error for young nymphs.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
826 young_nymphs.std_error = apply(YoungNymphs.replications / sqrt(opt$replications), 1, sd);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
827 } else if (life_stages_nymph=="Old") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
828 # Mean value for old nymphs.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
829 old_nymphs = apply(OldNymphs.replications, 1, mean);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
830 # Standard error for old nymphs.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
831 old_nymphs.std_error = apply(OldNymphs.replications / sqrt(opt$replications), 1, sd);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
832 }
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
833 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
834 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
835 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
836 # Calculate adult populations for selected life stage.
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
837 for (life_stage_adult in life_stages_adult) {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
838 if (life_stages_adult=="Total") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
839 # Mean value for all adults.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
840 total_adults = apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, mean);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
841 # Standard error for all adults.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
842 total_adults.std_error = apply((Previtellogenic.replications+Vitellogenic.replications+Diapausing.replications), 1, sd) / sqrt(opt$replications);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
843 } else if (life_stages_adult == "Pre-vittelogenic") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
844 # Mean value for previtellogenic adults.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
845 previttelogenic_adults = apply(Previtellogenic.replications, 1, mean);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
846 # Standard error for previtellogenic adults.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
847 previttelogenic_adults.std_error = apply(Previtellogenic.replications, 1, sd) / sqrt(opt$replications);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
848 } else if (life_stages_adult == "Vittelogenic") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
849 # Mean value for vitellogenic adults.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
850 vittelogenic_adults = apply(Vitellogenic.replications, 1, mean);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
851 # Standard error for vitellogenic adults.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
852 vittelogenic_adults.std_error = apply(Vitellogenic.replications, 1, sd) / sqrt(opt$replications);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
853 } else if (life_stages_adult == "Diapausing") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
854 # Mean value for vitellogenic adults.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
855 diapausing_adults = apply(Diapausing.replications, 1, mean);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
856 # Standard error for vitellogenic adults.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
857 diapausing_adults.std_error = apply(Diapausing.replications, 1, sd) / sqrt(opt$replications);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
858 }
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
859 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
860 }
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
861
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
862 if (plot_generations_separately) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
863 # Mean value for P which is Parental, or overwintered adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
864 P = apply(P.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
865 # Standard error for P.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
866 P.std_error = apply(P.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
867 # Mean value for F1, which is the first field-produced generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
868 F1 = apply(F1.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
869 # Standard error for F1.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
870 F1.std_error = apply(F1.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
871 # Mean value for F2, which is the second field-produced generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
872 F2 = apply(F2.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
873 # Standard error for F2.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
874 F2.std_error = apply(F2.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
875 if (process_eggs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
876 # Mean value for P eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
877 P_eggs = apply(P_eggs.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
878 # Standard error for P_eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
879 P_eggs.std_error = apply(P_eggs.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
880 # Mean value for F1 eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
881 F1_eggs = apply(F1_eggs.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
882 # Standard error for F1 eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
883 F1_eggs.std_error = apply(F1_eggs.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
884 # Mean value for F2 eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
885 F2_eggs = apply(F2_eggs.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
886 # Standard error for F2 eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
887 F2_eggs.std_error = apply(F2_eggs.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
888 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
889 if (process_nymphs) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
890 # Mean value for P nymphs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
891 P_nymphs = apply(P_nymphs.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
892 # Standard error for P_nymphs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
893 P_nymphs.std_error = apply(P_nymphs.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
894 # Mean value for F1 nymphs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
895 F1_nymphs = apply(F1_nymphs.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
896 # Standard error for F1 nymphs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
897 F1_nymphs.std_error = apply(F1_nymphs.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
898 # Mean value for F2 nymphs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
899 F2_nymphs = apply(F2_nymphs.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
900 # Standard error for F2 eggs.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
901 F2_nymphs.std_error = apply(F2_nymphs.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
902 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
903 if (process_adults) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
904 # Mean value for P adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
905 P_adults = apply(P_adults.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
906 # Standard error for P_adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
907 P_adults.std_error = apply(P_adults.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
908 # Mean value for F1 adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
909 F1_adults = apply(F1_adults.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
910 # Standard error for F1 adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
911 F1_adults.std_error = apply(F1_adults.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
912 # Mean value for F2 adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
913 F2_adults = apply(F2_adults.replications, 1, mean);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
914 # Standard error for F2 adults.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
915 F2_adults.std_error = apply(F2_adults.replications, 1, sd) / sqrt(opt$replications);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
916 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
917 }
6
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
918
fe3f86012394 Uploaded
greg
parents: 5
diff changeset
919 # Display the total number of days in the Galaxy history item blurb.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
920 cat("Number of days: ", opt$num_days, "\n");
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
921
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
922 # Information needed for plots plots.
8
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
923 days = c(1:opt$num_days);
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
924 start_date = temperature_data_frame$DATE[1];
37f1ad91a949 Uploaded
greg
parents: 6
diff changeset
925 end_date = temperature_data_frame$DATE[opt$num_days];
5
1878a03f9c9f Uploaded
greg
parents: 4
diff changeset
926
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
927 if (plot_generations_separately) {
15
dd86ee185113 Uploaded
greg
parents: 11
diff changeset
928 for (life_stage in life_stages) {
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
929 if (life_stage == "Egg") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
930 # Start PDF device driver.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
931 dev.new(width=20, height=30);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
932 file_path = paste(output_dir, "egg_pop_by_generation.pdf", sep="/");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
933 pdf(file=file_path, width=20, height=30, bg="white");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
934 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
935 # Egg population size by generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
936 maxval = max(F2_eggs) + 100;
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
937 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
938 opt$replications, life_stage, group=P_eggs, group_std_error=P_eggs.std_error, group2=F1_eggs, group2_std_error=F1_eggs.std_error, group3=F2_eggs,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
939 group3_std_error=F2_eggs.std_error);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
940 # Turn off device driver to flush output.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
941 dev.off();
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
942 } else if (life_stage == "Nymph") {
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
943 for (life_stage_nymph in life_stages_nymph) {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
944 # Start PDF device driver.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
945 dev.new(width=20, height=30);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
946 file_name = paste(tolower(life_stage_nymph), "nymph_pop_by_generation.pdf", sep="_");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
947 file_path = paste(output_dir, file_name, sep="/");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
948 pdf(file=file_path, width=20, height=30, bg="white");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
949 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
309954bbe999 Uploaded
greg
parents: 15
diff changeset
950 # Nymph population size by generation.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
951 maxval = max(F2_nymphs) + 100;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
952 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
309954bbe999 Uploaded
greg
parents: 15
diff changeset
953 opt$replications, life_stage, group=P_nymphs, group_std_error=P_nymphs.std_error, group2=F1_nymphs, group2_std_error=F1_nymphs.std_error,
309954bbe999 Uploaded
greg
parents: 15
diff changeset
954 group3=F2_nymphs, group3_std_error=F2_nymphs.std_error, life_stages_nymph=life_stage_nymph);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
955 # Turn off device driver to flush output.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
956 dev.off();
309954bbe999 Uploaded
greg
parents: 15
diff changeset
957 }
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
958 } else if (life_stage == "Adult") {
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
959 for (life_stage_adult in life_stages_adult) {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
960 # Start PDF device driver.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
961 dev.new(width=20, height=30);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
962 file_name = paste(tolower(life_stage_adult), "adult_pop_by_generation.pdf", sep="_");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
963 file_path = paste(output_dir, file_name, sep="/");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
964 pdf(file=file_path, width=20, height=30, bg="white");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
965 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
309954bbe999 Uploaded
greg
parents: 15
diff changeset
966 # Adult population size by generation.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
967 maxval = max(F2_adults) + 100;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
968 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
309954bbe999 Uploaded
greg
parents: 15
diff changeset
969 opt$replications, life_stage, group=P_adults, group_std_error=P_adults.std_error, group2=F1_adults, group2_std_error=F1_adults.std_error,
309954bbe999 Uploaded
greg
parents: 15
diff changeset
970 group3=F2_adults, group3_std_error=F2_adults.std_error, life_stages_adult=life_stage_adult);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
971 # Turn off device driver to flush output.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
972 dev.off();
309954bbe999 Uploaded
greg
parents: 15
diff changeset
973 }
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
974 } else if (life_stage == "Total") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
975 # Start PDF device driver.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
976 dev.new(width=20, height=30);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
977 file_path = paste(output_dir, "total_pop_by_generation.pdf", sep="/");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
978 pdf(file=file_path, width=20, height=30, bg="white");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
979 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
980 # Total population size by generation.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
981 maxval = max(F2);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
982 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
983 opt$replications, life_stage, group=P, group_std_error=P.std_error, group2=F1, group2_std_error=F1.std_error, group3=F2, group3_std_error=F2.std_error);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
984 # Turn off device driver to flush output.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
985 dev.off();
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
986 }
15
dd86ee185113 Uploaded
greg
parents: 11
diff changeset
987 }
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
988 } else {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
989 for (life_stage in life_stages) {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
990 if (life_stage == "Egg") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
991 # Start PDF device driver.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
992 dev.new(width=20, height=30);
11
d1f2c0634354 Uploaded
greg
parents: 10
diff changeset
993 file_path = paste(output_dir, "egg_pop.pdf", sep="/");
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
994 pdf(file=file_path, width=20, height=30, bg="white");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
995 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
996 # Egg population size.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
997 maxval = max(eggs+eggs.std_error);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
998 render_chart(date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
999 opt$replications, life_stage, group=eggs, group_std_error=eggs.std_error);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1000 # Turn off device driver to flush output.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1001 dev.off();
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1002 } else if (life_stage == "Nymph") {
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1003 for (life_stage_nymph in life_stages_nymph) {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1004 # Start PDF device driver.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1005 dev.new(width=20, height=30);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1006 file_name = paste(tolower(life_stage_nymph), "nymph_pop.pdf", sep="_");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1007 file_path = paste(output_dir, file_name, sep="/");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1008 pdf(file=file_path, width=20, height=30, bg="white");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1009 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1010 if (life_stage_nymph=="Total") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1011 # Total nymph population size.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1012 group = total_nymphs;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1013 group_std_error = total_nymphs.std_error;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1014 } else if (life_stage_nymph=="Young") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1015 # Young nymph population size.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1016 group = young_nymphs;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1017 group_std_error = young_nymphs.std_error;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1018 } else if (life_stage_nymph=="Old") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1019 # Old nymph population size.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1020 group = old_nymphs;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1021 group_std_error = old_nymphs.std_error;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1022 }
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1023 maxval = max(group+group.std_error);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1024 render_chart(date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1025 opt$replications, life_stage, group=group, group_std_error=group_std_error, life_stages_nymph=life_stage_nymph);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1026 # Turn off device driver to flush output.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1027 dev.off();
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1028 }
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1029 } else if (life_stage == "Adult") {
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1030 for (life_stage_adult in life_stages_adult) {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1031 # Start PDF device driver.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1032 dev.new(width=20, height=30);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1033 file_name = paste(tolower(life_stages_adult), "adult_pop.pdf", sep="_");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1034 file_path = paste(output_dir, file_name, sep="/");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1035 pdf(file=file_path, width=20, height=30, bg="white");
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1036 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1037 if (life_stage_adult=="Total") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1038 # Total adult population size.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1039 group = total_adults;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1040 group_std_error = total_adults.std_error
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1041 } else if (life_stage_adult=="Pre-vittelogenic") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1042 # Pre-vittelogenic adult population size.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1043 group = previttelogenic_adults;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1044 group_std_error = previttelogenic_adults.std_error
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1045 } else if (life_stage_adult=="Vittelogenic") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1046 # Vittelogenic adult population size.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1047 group = vittelogenic_adults;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1048 group_std_error = vittelogenic_adults.std_error
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1049 } else if (life_stage_adult=="Diapausing") {
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1050 # Diapausing adult population size.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1051 group = diapausing_adults;
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1052 group_std_error = diapausing_adults.std_error
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1053 }
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1054 maxval = max(group+group_std_error);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1055 render_chart(date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1056 opt$replications, life_stage, group=group, group_std_error=group_std_error, life_stages_adult=life_stage_adult);
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1057 # Turn off device driver to flush output.
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1058 dev.off();
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1059 }
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1060 } else if (life_stage == "Total") {
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1061 # Start PDF device driver.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1062 dev.new(width=20, height=30);
11
d1f2c0634354 Uploaded
greg
parents: 10
diff changeset
1063 file_path = paste(output_dir, "total_pop.pdf", sep="/");
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1064 pdf(file=file_path, width=20, height=30, bg="white");
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1065 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1066 # Total population size.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1067 maxval = max(eggs+eggs.std_error, nymphs+nymphs.std_error, adults+adults.std_error);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1068 render_chart(date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval,
16
309954bbe999 Uploaded
greg
parents: 15
diff changeset
1069 opt$replications, life_stage, group=total_adults, group_std_error=total_adults.std_error, group2=total_nymphs, group2_std_error=total_nymphs.std_error, group3=eggs,
10
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1070 group3_std_error=eggs.std_error);
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1071 # Turn off device driver to flush output.
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1072 dev.off();
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1073 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1074 }
61bc6bd8807d Uploaded
greg
parents: 8
diff changeset
1075 }