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