annotate utils.R @ 60:393085589438 draft

Uploaded
author greg
date Thu, 20 Dec 2018 09:15:52 -0500
parents 892cf703be62
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50
927321ed0322 Uploaded
greg
parents:
diff changeset
1 #!/usr/bin/env Rscript
927321ed0322 Uploaded
greg
parents:
diff changeset
2
927321ed0322 Uploaded
greg
parents:
diff changeset
3 get_file_path = function(life_stage, base_name, sub_life_stage=NULL) {
927321ed0322 Uploaded
greg
parents:
diff changeset
4 if (is.null(sub_life_stage)) {
927321ed0322 Uploaded
greg
parents:
diff changeset
5 lsi = get_life_stage_index(life_stage);
927321ed0322 Uploaded
greg
parents:
diff changeset
6 file_name = paste(lsi, base_name, sep="_");
927321ed0322 Uploaded
greg
parents:
diff changeset
7 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
8 lsi = get_life_stage_index(life_stage, sub_life_stage=sub_life_stage);
927321ed0322 Uploaded
greg
parents:
diff changeset
9 file_name = paste(lsi, tolower(sub_life_stage), base_name, sep="_");
927321ed0322 Uploaded
greg
parents:
diff changeset
10 }
927321ed0322 Uploaded
greg
parents:
diff changeset
11 file_path = paste("output_plots_dir", file_name, sep="/");
927321ed0322 Uploaded
greg
parents:
diff changeset
12 return(file_path);
927321ed0322 Uploaded
greg
parents:
diff changeset
13 }
927321ed0322 Uploaded
greg
parents:
diff changeset
14
927321ed0322 Uploaded
greg
parents:
diff changeset
15 get_year_from_date = function(date_str) {
927321ed0322 Uploaded
greg
parents:
diff changeset
16 date_str_items = strsplit(date_str, "-")[[1]];
927321ed0322 Uploaded
greg
parents:
diff changeset
17 return (date_str_items[1]);
927321ed0322 Uploaded
greg
parents:
diff changeset
18 }
927321ed0322 Uploaded
greg
parents:
diff changeset
19
927321ed0322 Uploaded
greg
parents:
diff changeset
20 get_life_stage_index = function(life_stage, sub_life_stage=NULL) {
927321ed0322 Uploaded
greg
parents:
diff changeset
21 # Name collection elements so that they
927321ed0322 Uploaded
greg
parents:
diff changeset
22 # are displayed in logical order.
927321ed0322 Uploaded
greg
parents:
diff changeset
23 if (life_stage=="Egg") {
927321ed0322 Uploaded
greg
parents:
diff changeset
24 lsi = "01";
927321ed0322 Uploaded
greg
parents:
diff changeset
25 } else if (life_stage=="Nymph") {
927321ed0322 Uploaded
greg
parents:
diff changeset
26 if (sub_life_stage=="Young") {
927321ed0322 Uploaded
greg
parents:
diff changeset
27 lsi = "02";
927321ed0322 Uploaded
greg
parents:
diff changeset
28 } else if (sub_life_stage=="Old") {
927321ed0322 Uploaded
greg
parents:
diff changeset
29 lsi = "03";
927321ed0322 Uploaded
greg
parents:
diff changeset
30 } else if (sub_life_stage=="Total") {
927321ed0322 Uploaded
greg
parents:
diff changeset
31 lsi="04";
927321ed0322 Uploaded
greg
parents:
diff changeset
32 }
927321ed0322 Uploaded
greg
parents:
diff changeset
33 } else if (life_stage=="Adult") {
927321ed0322 Uploaded
greg
parents:
diff changeset
34 if (sub_life_stage=="Pre-vittelogenic") {
927321ed0322 Uploaded
greg
parents:
diff changeset
35 lsi = "05";
927321ed0322 Uploaded
greg
parents:
diff changeset
36 } else if (sub_life_stage=="Vittelogenic") {
927321ed0322 Uploaded
greg
parents:
diff changeset
37 lsi = "06";
927321ed0322 Uploaded
greg
parents:
diff changeset
38 } else if (sub_life_stage=="Diapausing") {
927321ed0322 Uploaded
greg
parents:
diff changeset
39 lsi = "07";
927321ed0322 Uploaded
greg
parents:
diff changeset
40 } else if (sub_life_stage=="Total") {
927321ed0322 Uploaded
greg
parents:
diff changeset
41 lsi = "08";
927321ed0322 Uploaded
greg
parents:
diff changeset
42 }
927321ed0322 Uploaded
greg
parents:
diff changeset
43 } else if (life_stage=="Total") {
927321ed0322 Uploaded
greg
parents:
diff changeset
44 lsi = "09";
927321ed0322 Uploaded
greg
parents:
diff changeset
45 }
927321ed0322 Uploaded
greg
parents:
diff changeset
46 return(lsi);
927321ed0322 Uploaded
greg
parents:
diff changeset
47 }
927321ed0322 Uploaded
greg
parents:
diff changeset
48
927321ed0322 Uploaded
greg
parents:
diff changeset
49 get_mean_and_std_error = function(p_replications, f1_replications, f2_replications) {
927321ed0322 Uploaded
greg
parents:
diff changeset
50 # P mean.
927321ed0322 Uploaded
greg
parents:
diff changeset
51 p_m = apply(p_replications, 1, mean);
927321ed0322 Uploaded
greg
parents:
diff changeset
52 # P standard error.
927321ed0322 Uploaded
greg
parents:
diff changeset
53 p_se = apply(p_replications, 1, sd) / sqrt(opt$replications);
927321ed0322 Uploaded
greg
parents:
diff changeset
54 # F1 mean.
927321ed0322 Uploaded
greg
parents:
diff changeset
55 f1_m = apply(f1_replications, 1, mean);
927321ed0322 Uploaded
greg
parents:
diff changeset
56 # F1 standard error.
927321ed0322 Uploaded
greg
parents:
diff changeset
57 f1_se = apply(f1_replications, 1, sd) / sqrt(opt$replications);
927321ed0322 Uploaded
greg
parents:
diff changeset
58 # F2 mean.
927321ed0322 Uploaded
greg
parents:
diff changeset
59 f2_m = apply(f2_replications, 1, mean);
927321ed0322 Uploaded
greg
parents:
diff changeset
60 # F2 standard error.
927321ed0322 Uploaded
greg
parents:
diff changeset
61 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications);
927321ed0322 Uploaded
greg
parents:
diff changeset
62 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))
927321ed0322 Uploaded
greg
parents:
diff changeset
63 }
927321ed0322 Uploaded
greg
parents:
diff changeset
64
927321ed0322 Uploaded
greg
parents:
diff changeset
65 get_tick_index = function(index, last_tick, ticks, tick_labels, tick_sep) {
927321ed0322 Uploaded
greg
parents:
diff changeset
66 # The R code tries hard not to draw overlapping tick labels, and so
927321ed0322 Uploaded
greg
parents:
diff changeset
67 # will omit labels where they would abut or overlap previously drawn
927321ed0322 Uploaded
greg
parents:
diff changeset
68 # labels. This can result in, for example, every other tick being
927321ed0322 Uploaded
greg
parents:
diff changeset
69 # labelled. We'll keep track of the last tick to make sure all of
927321ed0322 Uploaded
greg
parents:
diff changeset
70 # the month labels are displayed, and missing ticks are restricted
927321ed0322 Uploaded
greg
parents:
diff changeset
71 # to Sundays which have no labels anyway.
927321ed0322 Uploaded
greg
parents:
diff changeset
72 if (last_tick==0) {
927321ed0322 Uploaded
greg
parents:
diff changeset
73 return(length(ticks)+1);
927321ed0322 Uploaded
greg
parents:
diff changeset
74 }
927321ed0322 Uploaded
greg
parents:
diff changeset
75 last_saved_tick = ticks[[length(ticks)]];
927321ed0322 Uploaded
greg
parents:
diff changeset
76 if (index-last_saved_tick<tick_sep) {
927321ed0322 Uploaded
greg
parents:
diff changeset
77 last_saved_month = tick_labels[[length(tick_labels)]];
927321ed0322 Uploaded
greg
parents:
diff changeset
78 if (last_saved_month=="") {
927321ed0322 Uploaded
greg
parents:
diff changeset
79 # We're safe overwriting a tick
927321ed0322 Uploaded
greg
parents:
diff changeset
80 # with no label (i.e., a Sunday tick).
927321ed0322 Uploaded
greg
parents:
diff changeset
81 return(length(ticks));
927321ed0322 Uploaded
greg
parents:
diff changeset
82 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
83 # Don't eliminate a Month label.
927321ed0322 Uploaded
greg
parents:
diff changeset
84 return(NULL);
927321ed0322 Uploaded
greg
parents:
diff changeset
85 }
927321ed0322 Uploaded
greg
parents:
diff changeset
86 }
927321ed0322 Uploaded
greg
parents:
diff changeset
87 return(length(ticks)+1);
927321ed0322 Uploaded
greg
parents:
diff changeset
88 }
927321ed0322 Uploaded
greg
parents:
diff changeset
89
927321ed0322 Uploaded
greg
parents:
diff changeset
90 get_total_days = function(is_leap_year) {
927321ed0322 Uploaded
greg
parents:
diff changeset
91 # Get the total number of days in the current year.
927321ed0322 Uploaded
greg
parents:
diff changeset
92 if (is_leap_year) {
927321ed0322 Uploaded
greg
parents:
diff changeset
93 return(366);
927321ed0322 Uploaded
greg
parents:
diff changeset
94 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
95 return(365);
927321ed0322 Uploaded
greg
parents:
diff changeset
96 }
927321ed0322 Uploaded
greg
parents:
diff changeset
97 }
927321ed0322 Uploaded
greg
parents:
diff changeset
98
59
892cf703be62 Uploaded
greg
parents: 50
diff changeset
99 get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm=0, append_start_doy_norm=0, date_interval=FALSE, doy_zero_insects=NULL) {
50
927321ed0322 Uploaded
greg
parents:
diff changeset
100 # Generate a list of ticks and labels for plotting the x axis.
927321ed0322 Uploaded
greg
parents:
diff changeset
101 if (prepend_end_doy_norm > 0) {
927321ed0322 Uploaded
greg
parents:
diff changeset
102 prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm);
927321ed0322 Uploaded
greg
parents:
diff changeset
103 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
104 prepend_end_norm_row = 0;
927321ed0322 Uploaded
greg
parents:
diff changeset
105 }
927321ed0322 Uploaded
greg
parents:
diff changeset
106 if (append_start_doy_norm > 0) {
927321ed0322 Uploaded
greg
parents:
diff changeset
107 append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm);
927321ed0322 Uploaded
greg
parents:
diff changeset
108 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
109 append_start_norm_row = 0;
927321ed0322 Uploaded
greg
parents:
diff changeset
110 }
59
892cf703be62 Uploaded
greg
parents: 50
diff changeset
111 if (is.null(doy_zero_insects)) {
892cf703be62 Uploaded
greg
parents: 50
diff changeset
112 zero_insects_row = 0;
892cf703be62 Uploaded
greg
parents: 50
diff changeset
113 } else {
892cf703be62 Uploaded
greg
parents: 50
diff changeset
114 zero_insects_row = which(temperature_data_frame$DOY==doy_zero_insects);
892cf703be62 Uploaded
greg
parents: 50
diff changeset
115 }
50
927321ed0322 Uploaded
greg
parents:
diff changeset
116 num_rows = dim(temperature_data_frame)[1];
927321ed0322 Uploaded
greg
parents:
diff changeset
117 tick_labels = list();
927321ed0322 Uploaded
greg
parents:
diff changeset
118 ticks = list();
927321ed0322 Uploaded
greg
parents:
diff changeset
119 current_month_label = NULL;
927321ed0322 Uploaded
greg
parents:
diff changeset
120 last_tick = 0;
927321ed0322 Uploaded
greg
parents:
diff changeset
121 if (date_interval) {
927321ed0322 Uploaded
greg
parents:
diff changeset
122 tick_sep = 0;
927321ed0322 Uploaded
greg
parents:
diff changeset
123 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
124 tick_sep = 3;
927321ed0322 Uploaded
greg
parents:
diff changeset
125 }
927321ed0322 Uploaded
greg
parents:
diff changeset
126 for (i in 1:num_rows) {
927321ed0322 Uploaded
greg
parents:
diff changeset
127 # Get the year and month from the date which
927321ed0322 Uploaded
greg
parents:
diff changeset
128 # has the format YYYY-MM-DD.
927321ed0322 Uploaded
greg
parents:
diff changeset
129 date = format(temperature_data_frame$DATE[i]);
927321ed0322 Uploaded
greg
parents:
diff changeset
130 # Get the month label.
927321ed0322 Uploaded
greg
parents:
diff changeset
131 items = strsplit(date, "-")[[1]];
927321ed0322 Uploaded
greg
parents:
diff changeset
132 month = items[2];
927321ed0322 Uploaded
greg
parents:
diff changeset
133 month_label = month.abb[as.integer(month)];
927321ed0322 Uploaded
greg
parents:
diff changeset
134 day = as.integer(items[3]);
927321ed0322 Uploaded
greg
parents:
diff changeset
135 doy = as.integer(temperature_data_frame$DOY[i]);
927321ed0322 Uploaded
greg
parents:
diff changeset
136 # We're plotting the entire year, so ticks will
927321ed0322 Uploaded
greg
parents:
diff changeset
137 # occur on Sundays and the first of each month.
59
892cf703be62 Uploaded
greg
parents: 50
diff changeset
138 if (i == zero_insects_row) {
892cf703be62 Uploaded
greg
parents: 50
diff changeset
139 # Add a tick for the day on which the number of insects
892cf703be62 Uploaded
greg
parents: 50
diff changeset
140 # per replication is 0.
892cf703be62 Uploaded
greg
parents: 50
diff changeset
141 label_str = "Number insects is 0";
892cf703be62 Uploaded
greg
parents: 50
diff changeset
142 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
892cf703be62 Uploaded
greg
parents: 50
diff changeset
143 ticks[tick_index] = i;
892cf703be62 Uploaded
greg
parents: 50
diff changeset
144 if (date_interval) {
892cf703be62 Uploaded
greg
parents: 50
diff changeset
145 # Append the day to label_str
892cf703be62 Uploaded
greg
parents: 50
diff changeset
146 tick_labels[tick_index] = paste(label_str, day, sep=" ");
892cf703be62 Uploaded
greg
parents: 50
diff changeset
147 } else {
892cf703be62 Uploaded
greg
parents: 50
diff changeset
148 tick_labels[tick_index] = label_str;
892cf703be62 Uploaded
greg
parents: 50
diff changeset
149 }
892cf703be62 Uploaded
greg
parents: 50
diff changeset
150 last_tick = i;
892cf703be62 Uploaded
greg
parents: 50
diff changeset
151 } else if (i == prepend_end_norm_row) {
50
927321ed0322 Uploaded
greg
parents:
diff changeset
152 # Add a tick for the end of the 30 year normnals data
927321ed0322 Uploaded
greg
parents:
diff changeset
153 # that was prepended to the year-to-date data.
927321ed0322 Uploaded
greg
parents:
diff changeset
154 label_str = "End prepended 30 year normals";
59
892cf703be62 Uploaded
greg
parents: 50
diff changeset
155 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
50
927321ed0322 Uploaded
greg
parents:
diff changeset
156 ticks[tick_index] = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
157 if (date_interval) {
927321ed0322 Uploaded
greg
parents:
diff changeset
158 # Append the day to label_str
927321ed0322 Uploaded
greg
parents:
diff changeset
159 tick_labels[tick_index] = paste(label_str, day, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
160 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
161 tick_labels[tick_index] = label_str;
927321ed0322 Uploaded
greg
parents:
diff changeset
162 }
927321ed0322 Uploaded
greg
parents:
diff changeset
163 last_tick = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
164 } else if (doy == append_start_doy_norm) {
927321ed0322 Uploaded
greg
parents:
diff changeset
165 # Add a tick for the start of the 30 year normnals data
927321ed0322 Uploaded
greg
parents:
diff changeset
166 # that was appended to the year-to-date data.
927321ed0322 Uploaded
greg
parents:
diff changeset
167 label_str = "Start appended 30 year normals";
59
892cf703be62 Uploaded
greg
parents: 50
diff changeset
168 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
50
927321ed0322 Uploaded
greg
parents:
diff changeset
169 ticks[tick_index] = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
170 if (!identical(current_month_label, month_label)) {
927321ed0322 Uploaded
greg
parents:
diff changeset
171 # Append the month to label_str.
927321ed0322 Uploaded
greg
parents:
diff changeset
172 label_str = paste(label_str, month_label, spe=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
173 current_month_label = month_label;
927321ed0322 Uploaded
greg
parents:
diff changeset
174 }
927321ed0322 Uploaded
greg
parents:
diff changeset
175 if (date_interval) {
927321ed0322 Uploaded
greg
parents:
diff changeset
176 # Append the day to label_str
927321ed0322 Uploaded
greg
parents:
diff changeset
177 label_str = paste(label_str, day, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
178 }
927321ed0322 Uploaded
greg
parents:
diff changeset
179 tick_labels[tick_index] = label_str;
927321ed0322 Uploaded
greg
parents:
diff changeset
180 last_tick = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
181 } else if (i==num_rows) {
927321ed0322 Uploaded
greg
parents:
diff changeset
182 # Add a tick for the last day of the year.
927321ed0322 Uploaded
greg
parents:
diff changeset
183 label_str = "";
59
892cf703be62 Uploaded
greg
parents: 50
diff changeset
184 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
50
927321ed0322 Uploaded
greg
parents:
diff changeset
185 ticks[tick_index] = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
186 if (!identical(current_month_label, month_label)) {
927321ed0322 Uploaded
greg
parents:
diff changeset
187 # Append the month to label_str.
927321ed0322 Uploaded
greg
parents:
diff changeset
188 label_str = month_label;
927321ed0322 Uploaded
greg
parents:
diff changeset
189 current_month_label = month_label;
927321ed0322 Uploaded
greg
parents:
diff changeset
190 }
927321ed0322 Uploaded
greg
parents:
diff changeset
191 if (date_interval) {
927321ed0322 Uploaded
greg
parents:
diff changeset
192 # Append the day to label_str
927321ed0322 Uploaded
greg
parents:
diff changeset
193 label_str = paste(label_str, day, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
194 }
927321ed0322 Uploaded
greg
parents:
diff changeset
195 tick_labels[tick_index] = label_str;
927321ed0322 Uploaded
greg
parents:
diff changeset
196 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
197 if (!identical(current_month_label, month_label)) {
927321ed0322 Uploaded
greg
parents:
diff changeset
198 # Add a tick for the month.
59
892cf703be62 Uploaded
greg
parents: 50
diff changeset
199 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep);
50
927321ed0322 Uploaded
greg
parents:
diff changeset
200 ticks[tick_index] = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
201 if (date_interval) {
927321ed0322 Uploaded
greg
parents:
diff changeset
202 # Append the day to the month.
927321ed0322 Uploaded
greg
parents:
diff changeset
203 tick_labels[tick_index] = paste(month_label, day, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
204 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
205 tick_labels[tick_index] = month_label;
927321ed0322 Uploaded
greg
parents:
diff changeset
206 }
927321ed0322 Uploaded
greg
parents:
diff changeset
207 current_month_label = month_label;
927321ed0322 Uploaded
greg
parents:
diff changeset
208 last_tick = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
209 }
927321ed0322 Uploaded
greg
parents:
diff changeset
210 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
927321ed0322 Uploaded
greg
parents:
diff changeset
211 if (!is.null(tick_index)) {
927321ed0322 Uploaded
greg
parents:
diff changeset
212 if (date_interval) {
927321ed0322 Uploaded
greg
parents:
diff changeset
213 # Add a tick for every day. The first tick is the
927321ed0322 Uploaded
greg
parents:
diff changeset
214 # month label, so add a tick only if i is not 1
927321ed0322 Uploaded
greg
parents:
diff changeset
215 if (i>1 & day>1) {
927321ed0322 Uploaded
greg
parents:
diff changeset
216 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
927321ed0322 Uploaded
greg
parents:
diff changeset
217 ticks[tick_index] = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
218 # Add the day as the label.
927321ed0322 Uploaded
greg
parents:
diff changeset
219 tick_labels[tick_index] = day;
927321ed0322 Uploaded
greg
parents:
diff changeset
220 last_tick = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
221 }
927321ed0322 Uploaded
greg
parents:
diff changeset
222 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
223 # Get the day.
927321ed0322 Uploaded
greg
parents:
diff changeset
224 day = weekdays(as.Date(date));
927321ed0322 Uploaded
greg
parents:
diff changeset
225 if (day=="Sunday") {
927321ed0322 Uploaded
greg
parents:
diff changeset
226 # Add a tick if we're on a Sunday.
927321ed0322 Uploaded
greg
parents:
diff changeset
227 ticks[tick_index] = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
228 # Add a blank month label so it is not displayed.
927321ed0322 Uploaded
greg
parents:
diff changeset
229 tick_labels[tick_index] = "";
927321ed0322 Uploaded
greg
parents:
diff changeset
230 last_tick = i;
927321ed0322 Uploaded
greg
parents:
diff changeset
231 }
927321ed0322 Uploaded
greg
parents:
diff changeset
232 }
927321ed0322 Uploaded
greg
parents:
diff changeset
233 }
927321ed0322 Uploaded
greg
parents:
diff changeset
234 }
927321ed0322 Uploaded
greg
parents:
diff changeset
235 }
927321ed0322 Uploaded
greg
parents:
diff changeset
236 return(list(ticks, tick_labels));
927321ed0322 Uploaded
greg
parents:
diff changeset
237 }
927321ed0322 Uploaded
greg
parents:
diff changeset
238
927321ed0322 Uploaded
greg
parents:
diff changeset
239 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,
927321ed0322 Uploaded
greg
parents:
diff changeset
240 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,
927321ed0322 Uploaded
greg
parents:
diff changeset
241 sub_life_stage=NULL) {
927321ed0322 Uploaded
greg
parents:
diff changeset
242 if (chart_type=="pop_size_by_life_stage") {
927321ed0322 Uploaded
greg
parents:
diff changeset
243 if (life_stage=="Total") {
927321ed0322 Uploaded
greg
parents:
diff changeset
244 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
245 legend_text = c("Egg", "Nymph", "Adult");
927321ed0322 Uploaded
greg
parents:
diff changeset
246 columns = c(4, 2, 1);
927321ed0322 Uploaded
greg
parents:
diff changeset
247 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
248 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
249 lines(days, group2, lwd=2, lty=1, col=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
250 lines(days, group3, lwd=2, lty=1, col=4);
927321ed0322 Uploaded
greg
parents:
diff changeset
251 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
252 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
253 if (plot_std_error=="yes") {
927321ed0322 Uploaded
greg
parents:
diff changeset
254 # Standard error for group.
927321ed0322 Uploaded
greg
parents:
diff changeset
255 lines(days, group+group_std_error, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
256 lines(days, group-group_std_error, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
257 # Standard error for group2.
927321ed0322 Uploaded
greg
parents:
diff changeset
258 lines(days, group2+group2_std_error, col=2, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
259 lines(days, group2-group2_std_error, col=2, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
260 # Standard error for group3.
927321ed0322 Uploaded
greg
parents:
diff changeset
261 lines(days, group3+group3_std_error, col=4, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
262 lines(days, group3-group3_std_error, col=4, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
263 }
927321ed0322 Uploaded
greg
parents:
diff changeset
264 } else {
927321ed0322 Uploaded
greg
parents:
diff changeset
265 if (life_stage=="Egg") {
927321ed0322 Uploaded
greg
parents:
diff changeset
266 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
267 legend_text = c(life_stage);
927321ed0322 Uploaded
greg
parents:
diff changeset
268 columns = c(4);
927321ed0322 Uploaded
greg
parents:
diff changeset
269 } else if (life_stage=="Nymph") {
927321ed0322 Uploaded
greg
parents:
diff changeset
270 stage = paste(sub_life_stage, "Nymph Pop :", sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
271 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
272 legend_text = c(paste(sub_life_stage, life_stage, sep=" "));
927321ed0322 Uploaded
greg
parents:
diff changeset
273 columns = c(2);
927321ed0322 Uploaded
greg
parents:
diff changeset
274 } else if (life_stage=="Adult") {
927321ed0322 Uploaded
greg
parents:
diff changeset
275 stage = paste(sub_life_stage, "Adult Pop", sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
276 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
277 legend_text = c(paste(sub_life_stage, life_stage, sep=" "));
927321ed0322 Uploaded
greg
parents:
diff changeset
278 columns = c(1);
927321ed0322 Uploaded
greg
parents:
diff changeset
279 }
927321ed0322 Uploaded
greg
parents:
diff changeset
280 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
281 legend("topleft", legend_text, lty=c(1), col="black", cex=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
282 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
283 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
284 if (plot_std_error=="yes") {
927321ed0322 Uploaded
greg
parents:
diff changeset
285 # Standard error for group.
927321ed0322 Uploaded
greg
parents:
diff changeset
286 lines(days, group+group_std_error, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
287 lines(days, group-group_std_error, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
288 }
927321ed0322 Uploaded
greg
parents:
diff changeset
289 }
927321ed0322 Uploaded
greg
parents:
diff changeset
290 } else if (chart_type=="pop_size_by_generation") {
927321ed0322 Uploaded
greg
parents:
diff changeset
291 if (life_stage=="Total") {
927321ed0322 Uploaded
greg
parents:
diff changeset
292 title_str = ": Total Pop by Gen :";
927321ed0322 Uploaded
greg
parents:
diff changeset
293 } else if (life_stage=="Egg") {
927321ed0322 Uploaded
greg
parents:
diff changeset
294 title_str = ": Egg Pop by Gen :";
927321ed0322 Uploaded
greg
parents:
diff changeset
295 } else if (life_stage=="Nymph") {
927321ed0322 Uploaded
greg
parents:
diff changeset
296 title_str = paste(":", sub_life_stage, "Nymph Pop by Gen", ":", sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
297 } else if (life_stage=="Adult") {
927321ed0322 Uploaded
greg
parents:
diff changeset
298 title_str = paste(":", sub_life_stage, "Adult Pop by Gen", ":", sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
299 }
927321ed0322 Uploaded
greg
parents:
diff changeset
300 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
927321ed0322 Uploaded
greg
parents:
diff changeset
301 legend_text = c("P", "F1", "F2");
927321ed0322 Uploaded
greg
parents:
diff changeset
302 columns = c(1, 2, 4);
927321ed0322 Uploaded
greg
parents:
diff changeset
303 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
304 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
305 lines(days, group2, lwd=2, lty=1, col=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
306 lines(days, group3, lwd=2, lty=1, col=4);
927321ed0322 Uploaded
greg
parents:
diff changeset
307 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
308 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
927321ed0322 Uploaded
greg
parents:
diff changeset
309 if (plot_std_error=="yes") {
927321ed0322 Uploaded
greg
parents:
diff changeset
310 # Standard error for group.
927321ed0322 Uploaded
greg
parents:
diff changeset
311 lines(days, group+group_std_error, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
312 lines(days, group-group_std_error, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
313 # Standard error for group2.
927321ed0322 Uploaded
greg
parents:
diff changeset
314 lines(days, group2+group2_std_error, col=2, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
315 lines(days, group2-group2_std_error, col=2, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
316 # Standard error for group3.
927321ed0322 Uploaded
greg
parents:
diff changeset
317 lines(days, group3+group3_std_error, col=4, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
318 lines(days, group3-group3_std_error, col=4, lty=2);
927321ed0322 Uploaded
greg
parents:
diff changeset
319 }
927321ed0322 Uploaded
greg
parents:
diff changeset
320 }
927321ed0322 Uploaded
greg
parents:
diff changeset
321 }
927321ed0322 Uploaded
greg
parents:
diff changeset
322
927321ed0322 Uploaded
greg
parents:
diff changeset
323 stop_err = function(msg) {
927321ed0322 Uploaded
greg
parents:
diff changeset
324 cat(msg, file=stderr());
927321ed0322 Uploaded
greg
parents:
diff changeset
325 quit(save="no", status=1);
927321ed0322 Uploaded
greg
parents:
diff changeset
326 }
927321ed0322 Uploaded
greg
parents:
diff changeset
327
927321ed0322 Uploaded
greg
parents:
diff changeset
328 validate_date = function(date_str) {
927321ed0322 Uploaded
greg
parents:
diff changeset
329 valid_date = as.Date(date_str, format="%Y-%m-%d");
927321ed0322 Uploaded
greg
parents:
diff changeset
330 if( class(valid_date)=="try-error" || is.na(valid_date)) {
927321ed0322 Uploaded
greg
parents:
diff changeset
331 msg = paste("Invalid date: ", date_str, ", valid date format is yyyy-mm-dd.", sep="");
927321ed0322 Uploaded
greg
parents:
diff changeset
332 stop_err(msg);
927321ed0322 Uploaded
greg
parents:
diff changeset
333 }
927321ed0322 Uploaded
greg
parents:
diff changeset
334 return(valid_date);
927321ed0322 Uploaded
greg
parents:
diff changeset
335 }
60
393085589438 Uploaded
greg
parents: 59
diff changeset
336
393085589438 Uploaded
greg
parents: 59
diff changeset
337 validate_doys = function(temperature_data_frame) {
393085589438 Uploaded
greg
parents: 59
diff changeset
338 # Ensure all DOY values are consecutive integers.
393085589438 Uploaded
greg
parents: 59
diff changeset
339 last_doy = 0;
393085589438 Uploaded
greg
parents: 59
diff changeset
340 num_rows = dim(temperature_data_frame)[1];
393085589438 Uploaded
greg
parents: 59
diff changeset
341 for (i in 1:num_rows) {
393085589438 Uploaded
greg
parents: 59
diff changeset
342 doy = as.integer(temperature_data_frame$DOY[i]);
393085589438 Uploaded
greg
parents: 59
diff changeset
343 if (last_doy == 0) {
393085589438 Uploaded
greg
parents: 59
diff changeset
344 last_doy = doy;
393085589438 Uploaded
greg
parents: 59
diff changeset
345 } else {
393085589438 Uploaded
greg
parents: 59
diff changeset
346 if (doy != last_doy + 1) {
393085589438 Uploaded
greg
parents: 59
diff changeset
347 msg = paste("DOY ", doy, " is not consecutive (previous DOY is ", last_doy, ".", sep="");
393085589438 Uploaded
greg
parents: 59
diff changeset
348 stop_err(msg);
393085589438 Uploaded
greg
parents: 59
diff changeset
349 }
393085589438 Uploaded
greg
parents: 59
diff changeset
350 last_doy = doy;
393085589438 Uploaded
greg
parents: 59
diff changeset
351 }
393085589438 Uploaded
greg
parents: 59
diff changeset
352 }
393085589438 Uploaded
greg
parents: 59
diff changeset
353 }