changeset 50:927321ed0322 draft

Uploaded
author greg
date Tue, 07 Aug 2018 12:59:06 -0400
parents 1b6864c5b50a
children 3f827fe31756
files insect_phenology_model.R insect_phenology_model.xml test-data/output_combined1.csv test-data/output_combined6.csv test-data/output_combined7.csv test-data/output_f1_3.csv test-data/output_f1_4.csv test-data/output_f2_3.csv test-data/output_f2_4.csv test-data/output_p_3.csv test-data/output_p_4.csv utils.R
diffstat 12 files changed, 431 insertions(+), 451 deletions(-) [+]
line wrap: on
line diff
--- a/insect_phenology_model.R	Tue Jun 05 07:52:59 2018 -0400
+++ b/insect_phenology_model.R	Tue Aug 07 12:59:06 2018 -0400
@@ -6,7 +6,6 @@
     make_option(c("--adult_mortality"), action="store", dest="adult_mortality", type="integer", help="Adjustment rate for adult mortality"),
     make_option(c("--adult_accumulation"), action="store", dest="adult_accumulation", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"),
     make_option(c("--egg_mortality"), action="store", dest="egg_mortality", type="integer", help="Adjustment rate for egg mortality"),
-    make_option(c("--end_date"), action="store", dest="end_date", default=NULL, help="End date for custom date interval"),
     make_option(c("--input_norm"), action="store", dest="input_norm", help="30 year normals temperature data for selected station"),
     make_option(c("--input_ytd"), action="store", dest="input_ytd", default=NULL, help="Year-to-date temperature data for selected location"),
     make_option(c("--insect"), action="store", dest="insect", help="Insect name"),
@@ -25,7 +24,7 @@
     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"),
     make_option(c("--plot_std_error"), action="store", dest="plot_std_error", help="Plot Standard error"),
     make_option(c("--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
-    make_option(c("--start_date"), action="store", dest="start_date", default=NULL, help="Start date for custom date interval"),
+    make_option(c("--script_dir"), action="store", dest="script_dir", help="R script source directory"),
     make_option(c("--young_nymph_accumulation"), action="store", dest="young_nymph_accumulation", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)")
 )
 
@@ -35,7 +34,7 @@
 
 add_daylight_length = function(temperature_data_frame) {
     # Return temperature_data_frame with an added column
-    # of daylight length (photoperido profile).
+    # of daylight length (photoperiod profile).
     num_rows = dim(temperature_data_frame)[1];
     # From Forsythe 1995.
     p = 0.8333;
@@ -66,11 +65,6 @@
     return(data_frame);
 }
 
-extract_date_interval_rows = function(df, start_date, end_date) {
-    date_interval_rows = df[df$DATE >= start_date & df$DATE <= end_date];
-    return(date_interval_rows);
-}
-
 from_30_year_normals = function(norm_data_frame, start_date_doy, end_date_doy, year) {
     # The data we want is fully contained within the 30 year normals data.
     first_norm_row = which(norm_data_frame$DOY==start_date_doy);
@@ -86,66 +80,6 @@
     return (tmp_data_frame);
 }
 
-get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) {
-    if (!is.null(life_stage_nymph)) {
-        lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph);
-        file_name = paste(lsi, tolower(life_stage_nymph), base_name, sep="_");
-    } else if (!is.null(life_stage_adult)) {
-        lsi = get_life_stage_index(life_stage, life_stage_adult=life_stage_adult);
-        file_name = paste(lsi, tolower(life_stage_adult), base_name, sep="_");
-    } else {
-        lsi = get_life_stage_index(life_stage);
-        file_name = paste(lsi, base_name, sep="_");
-    }
-    file_path = paste("output_plots_dir", file_name, sep="/");
-    return(file_path);
-}
-
-get_life_stage_index = function(life_stage, life_stage_nymph=NULL, life_stage_adult=NULL) {
-    # Name collection elements so that they
-    # are displayed in logical order.
-    if (life_stage=="Egg") {
-        lsi = "01";
-    } else if (life_stage=="Nymph") {
-        if (life_stage_nymph=="Young") {
-            lsi = "02";
-        } else if (life_stage_nymph=="Old") {
-            lsi = "03";
-        } else if (life_stage_nymph=="Total") {
-            lsi="04";
-        }
-    } else if (life_stage=="Adult") {
-        if (life_stage_adult=="Pre-vittelogenic") {
-            lsi = "05";
-        } else if (life_stage_adult=="Vittelogenic") {
-            lsi = "06";
-        } else if (life_stage_adult=="Diapausing") {
-            lsi = "07";
-        } else if (life_stage_adult=="Total") {
-            lsi = "08";
-        }
-    } else if (life_stage=="Total") {
-        lsi = "09";
-    }
-    return(lsi);
-}
-
-get_mean_and_std_error = function(p_replications, f1_replications, f2_replications) {
-    # P mean.
-    p_m = apply(p_replications, 1, mean);
-    # P standard error.
-    p_se = apply(p_replications, 1, sd) / sqrt(opt$replications);
-    # F1 mean.
-    f1_m = apply(f1_replications, 1, mean);
-    # F1 standard error.
-    f1_se = apply(f1_replications, 1, sd) / sqrt(opt$replications);
-    # F2 mean.
-    f2_m = apply(f2_replications, 1, mean);
-    # F2 standard error.
-    f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications);
-    return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))
-}
-
 get_new_norm_data_frame = function(is_leap_year, input_norm=NULL, nrow=0) {
     # The input_norm data has the following 10 columns:
     # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX
@@ -273,167 +207,6 @@
     return(c(curr_mean_temp, averages))
 }
 
-get_tick_index = function(index, last_tick, ticks, tick_labels, tick_sep) {
-    # The R code tries hard not to draw overlapping tick labels, and so
-    # will omit labels where they would abut or overlap previously drawn
-    # labels. This can result in, for example, every other tick being
-    # labelled.  We'll keep track of the last tick to make sure all of
-    # the month labels are displayed, and missing ticks are restricted
-    # to Sundays which have no labels anyway.
-    if (last_tick==0) {
-        return(length(ticks)+1);
-    }
-    last_saved_tick = ticks[[length(ticks)]];
-    if (index-last_saved_tick<tick_sep) {
-        last_saved_month = tick_labels[[length(tick_labels)]];
-        if (last_saved_month=="") {
-            # We're safe overwriting a tick
-            # with no label (i.e., a Sunday tick).
-            return(length(ticks));
-        } else {
-            # Don't eliminate a Month label.
-            return(NULL);
-        }
-    }
-    return(length(ticks)+1);
-}
-
-get_total_days = function(is_leap_year) {
-    # Get the total number of days in the current year.
-    if (is_leap_year) {
-        return(366);
-    } else {
-        return(365);
-    }
-}
-
-get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, date_interval) {
-    # Generate a list of ticks and labels for plotting the x axis.
-    if (prepend_end_doy_norm > 0) {
-        prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm);
-    } else {
-        prepend_end_norm_row = 0;
-    }
-    if (append_start_doy_norm > 0) {
-        append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm);
-    } else {
-        append_start_norm_row = 0;
-    }
-    num_rows = dim(temperature_data_frame)[1];
-    tick_labels = list();
-    ticks = list();
-    current_month_label = NULL;
-    last_tick = 0;
-    if (date_interval) {
-        tick_sep = 0;
-    } else {
-        tick_sep = 3;
-    }
-    for (i in 1:num_rows) {
-        # Get the year and month from the date which
-        # has the format YYYY-MM-DD.
-        date = format(temperature_data_frame$DATE[i]);
-        # Get the month label.
-        items = strsplit(date, "-")[[1]];
-        month = items[2];
-        month_label = month.abb[as.integer(month)];
-        day = as.integer(items[3]);
-        doy = as.integer(temperature_data_frame$DOY[i]);
-        # We're plotting the entire year, so ticks will
-        # occur on Sundays and the first of each month.
-        if (i == prepend_end_norm_row) {
-            # Add a tick for the end of the 30 year normnals data
-            # that was prepended to the year-to-date data.
-            label_str = "End prepended 30 year normals";
-            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
-            ticks[tick_index] = i;
-            if (date_interval) {
-                # Append the day to label_str
-                tick_labels[tick_index] = paste(label_str, day, sep=" ");
-            } else {
-                tick_labels[tick_index] = label_str;
-            }
-            last_tick = i;
-        } else if (doy == append_start_doy_norm) {
-            # Add a tick for the start of the 30 year normnals data
-            # that was appended to the year-to-date data.
-            label_str = "Start appended 30 year normals";
-            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
-            ticks[tick_index] = i;
-            if (!identical(current_month_label, month_label)) {
-                # Append the month to label_str.
-                label_str = paste(label_str, month_label, spe=" ");
-                current_month_label = month_label;
-            }
-            if (date_interval) {
-                # Append the day to label_str
-                label_str = paste(label_str, day, sep=" ");
-            }
-            tick_labels[tick_index] = label_str;
-            last_tick = i;
-        } else if (i==num_rows) {
-            # Add a tick for the last day of the year.
-            label_str = "";
-            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
-            ticks[tick_index] = i;
-            if (!identical(current_month_label, month_label)) {
-                # Append the month to label_str.
-                label_str = month_label;
-                current_month_label = month_label;
-            }
-            if (date_interval) {
-                # Append the day to label_str
-                label_str = paste(label_str, day, sep=" ");
-            }
-            tick_labels[tick_index] = label_str;
-        } else {
-            if (!identical(current_month_label, month_label)) {
-                # Add a tick for the month.
-                tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
-                ticks[tick_index] = i;
-                if (date_interval) {
-                    # Append the day to the month.
-                    tick_labels[tick_index] = paste(month_label, day, sep=" ");
-                } else {
-                    tick_labels[tick_index] = month_label;
-                }
-                current_month_label = month_label;
-                last_tick = i;
-            }
-            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
-            if (!is.null(tick_index)) {
-                if (date_interval) {
-                    # Add a tick for every day. The first tick is the
-                    # month label, so add a tick only if i is not 1
-                    if (i>1 & day>1) {
-                        tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
-                        ticks[tick_index] = i;
-                        # Add the day as the label.
-                        tick_labels[tick_index] = day;
-                        last_tick = i;
-                     }
-                } else {
-                    # Get the day.
-                    day = weekdays(as.Date(date));
-                    if (day=="Sunday") {
-                        # Add a tick if we're on a Sunday.
-                        ticks[tick_index] = i;
-                        # Add a blank month label so it is not displayed.
-                        tick_labels[tick_index] = "";
-                        last_tick = i;
-                    }
-                }
-            }
-        }
-    }
-    return(list(ticks, tick_labels));
-}
-
-get_year_from_date = function(date_str) {
-    date_str_items = strsplit(date_str, "-")[[1]];
-    return (date_str_items[1]);
-}
-
 is_leap_year = function(date_str) {
     # Extract the year from the date_str.
     date = format(date_str);
@@ -721,103 +494,9 @@
     return(list(temperature_data_frame, start_date, end_date, prepend_end_doy_norm, append_start_doy_norm, is_leap_year, location));
 }
 
-render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,
-    replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,
-    life_stages_adult=NULL, life_stages_nymph=NULL) {
-    if (chart_type=="pop_size_by_life_stage") {
-        if (life_stage=="Total") {
-            title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
-            legend_text = c("Egg", "Nymph", "Adult");
-            columns = c(4, 2, 1);
-            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);
-            legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
-            lines(days, group2, lwd=2, lty=1, col=2);
-            lines(days, group3, lwd=2, lty=1, col=4);
-            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);
-            axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
-            if (plot_std_error=="yes") {
-                # Standard error for group.
-                lines(days, group+group_std_error, lty=2);
-                lines(days, group-group_std_error, lty=2);
-                # Standard error for group2.
-                lines(days, group2+group2_std_error, col=2, lty=2);
-                lines(days, group2-group2_std_error, col=2, lty=2);
-                # Standard error for group3.
-                lines(days, group3+group3_std_error, col=4, lty=2);
-                lines(days, group3-group3_std_error, col=4, lty=2);
-            }
-        } else {
-            if (life_stage=="Egg") {
-                title = paste(insect,  ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
-                legend_text = c(life_stage);
-                columns = c(4);
-            } else if (life_stage=="Nymph") {
-                stage = paste(life_stages_nymph, "Nymph Pop :", sep=" ");
-                title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
-                legend_text = c(paste(life_stages_nymph, life_stage, sep=" "));
-                columns = c(2);
-            } else if (life_stage=="Adult") {
-                stage = paste(life_stages_adult, "Adult Pop", sep=" ");
-                title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
-                legend_text = c(paste(life_stages_adult, life_stage, sep=" "));
-                columns = c(1);
-            }
-            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);
-            legend("topleft", legend_text, lty=c(1), col="black", cex=3);
-            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);
-            axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
-            if (plot_std_error=="yes") {
-                # Standard error for group.
-                lines(days, group+group_std_error, lty=2);
-                lines(days, group-group_std_error, lty=2);
-            }
-        }
-    } else if (chart_type=="pop_size_by_generation") {
-        if (life_stage=="Total") {
-            title_str = ": Total Pop by Gen :";
-        } else if (life_stage=="Egg") {
-            title_str = ": Egg Pop by Gen :";
-        } else if (life_stage=="Nymph") {
-            title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" ");
-        } else if (life_stage=="Adult") {
-            title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" ");
-        }
-        title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
-        legend_text = c("P", "F1", "F2");
-        columns = c(1, 2, 4);
-        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);
-        legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
-        lines(days, group2, lwd=2, lty=1, col=2);
-        lines(days, group3, lwd=2, lty=1, col=4);
-        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);
-        axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
-        if (plot_std_error=="yes") {
-            # Standard error for group.
-            lines(days, group+group_std_error, lty=2);
-            lines(days, group-group_std_error, lty=2);
-            # Standard error for group2.
-            lines(days, group2+group2_std_error, col=2, lty=2);
-            lines(days, group2-group2_std_error, col=2, lty=2);
-            # Standard error for group3.
-            lines(days, group3+group3_std_error, col=4, lty=2);
-            lines(days, group3-group3_std_error, col=4, lty=2);
-        }
-    }
-}
-
-stop_err = function(msg) {
-    cat(msg, file=stderr());
-    quit(save="no", status=1);
-}
-
-validate_date = function(date_str) {
-    valid_date = as.Date(date_str, format="%Y-%m-%d");
-    if( class(valid_date)=="try-error" || is.na(valid_date)) {
-        msg = paste("Invalid date: ", date_str, ", valid date format is yyyy-mm-dd.", sep="");
-        stop_err(msg);
-    }
-    return(valid_date);
-}
+# Import the shared utility functions.
+utils_path <- paste(opt$script_dir, "utils.R", sep="/");
+source(utils_path);
 
 if (is.null(opt$input_ytd)) {
     processing_year_to_date_data = FALSE;
@@ -842,48 +521,19 @@
 is_leap_year = data_list[[6]];
 location = data_list[[7]];
 
-if (is.null(opt$start_date) && is.null(opt$end_date)) {
-    # We're plotting an entire year.
-    date_interval = FALSE;
-    # Display the total number of days in the Galaxy history item blurb.
-    if (processing_year_to_date_data) {
-        cat("Number of days year-to-date: ", opt$num_days_ytd, "\n");
-    } else {
-        if (is_leap_year) {
-            num_days = 366;
-        } else {
-            num_days = 365;
-        }
-        cat("Number of days in year: ", num_days, "\n");
-    }
+# We're plotting an entire year.
+# Display the total number of days in the Galaxy history item blurb.
+if (processing_year_to_date_data) {
+    cat("Number of days year-to-date: ", opt$num_days_ytd, "\n");
 } else {
-    # FIXME: currently custom date fields are free text, but
-    # Galaxy should soon include support for a date selector
-    # at which point this tool should be enhanced to use it.
-    # Validate start_date.
-    date_interval = TRUE;
-    # Calaculate the number of days in the date interval rather
-    # than using the number of rows in the input temperature data.
-    start_date = validate_date(opt$start_date);
-    # Validate end_date.
-    end_date = validate_date(opt$end_date);
-    if (start_date >= end_date) {
-        stop_err("The start date must be between 1 and 50 days before the end date when setting date intervals for plots.");
+    if (is_leap_year) {
+        num_days = 366;
+    } else {
+        num_days = 365;
     }
-    # Calculate the number of days in the date interval.
-    num_days = difftime(end_date, start_date, units=c("days"));
-    # Add 1 to the number of days to make the dates inclusive.  For
-    # example, if the user enters a date range of 2018-01-01 to
-    # 2018-01-31, they likely expect the end date to be included.
-    num_days = num_days + 1;
-    if (num_days > 50) {
-        # We need to restrict date intervals since
-        # plots render tick marks for each day.
-        stop_err("Date intervals for plotting cannot exceed 50 days.");
-    }
-    # Display the total number of days in the Galaxy history item blurb.
-    cat("Number of days in date interval: ", num_days, "\n");
+    cat("Number of days in year: ", num_days, "\n");
 }
+
 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately.
 if (plot_generations_separately) {
     temperature_data_frame_P = data.frame(temperature_data_frame);
@@ -892,7 +542,7 @@
 }
 
 # Get the ticks date labels for plots.
-ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, date_interval);
+ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm);
 ticks = c(unlist(ticks_and_labels[1]));
 date_labels = c(unlist(ticks_and_labels[2]));
 # All latitude values are the same, so get the value for plots from the first row.
@@ -1629,10 +1279,10 @@
         if (life_stage_adult == "Pre-vittelogenic") {
             # Mean value for previttelogenic adults.
             previttelogenic_adults = apply(Previttelogenic.replications, 1, mean);
-            temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults, "PRE-VITADULT");
+            temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults, "PRE.VITADULT");
             # Standard error for previttelogenic adults.
             previttelogenic_adults.std_error = apply(Previttelogenic.replications, 1, sd) / sqrt(opt$replications);
-            temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults.std_error, "PRE-VITADULTSE");
+            temperature_data_frame = append_vector(temperature_data_frame, previttelogenic_adults.std_error, "PRE.VITADULTSE");
         } else if (life_stage_adult == "Vittelogenic") {
             # Mean value for vittelogenic adults.
             vittelogenic_adults = apply(Vittelogenic.replications, 1, mean);
@@ -1670,121 +1320,121 @@
         m_se = get_mean_and_std_error(P_eggs.replications, F1_eggs.replications, F2_eggs.replications);
         P_eggs = m_se[[1]];
         P_eggs.std_error = m_se[[2]];
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs, "EGG-P");
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs.std_error, "EGG-P-SE");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs, "EGG.P");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_eggs.std_error, "EGG.P.SE");
         F1_eggs = m_se[[3]];
         F1_eggs.std_error = m_se[[4]];
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs, "EGG-F1");
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs.std_error, "EGG-F1-SE");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs, "EGG.F1");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_eggs.std_error, "EGG.F1.SE");
         F2_eggs = m_se[[5]];
         F2_eggs.std_error = m_se[[6]];
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs, "EGG-F2");
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs.std_error, "EGG-F2-SE");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs, "EGG.F2");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_eggs.std_error, "EGG.F2.SE");
     }
     if (process_young_nymphs) {
         m_se = get_mean_and_std_error(P_young_nymphs.replications, F1_young_nymphs.replications, F2_young_nymphs.replications);
         P_young_nymphs = m_se[[1]];
         P_young_nymphs.std_error = m_se[[2]];
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs, "YOUNGNYMPH-P");
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs.std_error, "YOUNGNYMPH-P-SE");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs, "YOUNGNYMPH.P");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_young_nymphs.std_error, "YOUNGNYMPH.P.SE");
         F1_young_nymphs = m_se[[3]];
         F1_young_nymphs.std_error = m_se[[4]];
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs, "YOUNGNYMPH-F1");
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs.std_error, "YOUNGNYMPH-F1-SE");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs, "YOUNGNYMPH.F1");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_young_nymphs.std_error, "YOUNGNYMPH.F1.SE");
         F2_young_nymphs = m_se[[5]];
         F2_young_nymphs.std_error = m_se[[6]];
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs, "YOUNGNYMPH-F2");
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs.std_error, "YOUNGNYMPH-F2-SE");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs, "YOUNGNYMPH.F2");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_young_nymphs.std_error, "YOUNGNYMPH.F2.SE");
     }
     if (process_old_nymphs) {
         m_se = get_mean_and_std_error(P_old_nymphs.replications, F1_old_nymphs.replications, F2_old_nymphs.replications);
         P_old_nymphs = m_se[[1]];
         P_old_nymphs.std_error = m_se[[2]];
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs, "OLDNYMPH-P");
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs.std_error, "OLDNYMPH-P-SE");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs, "OLDNYMPH.P");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_old_nymphs.std_error, "OLDNYMPH.P.SE");
         F1_old_nymphs = m_se[[3]];
         F1_old_nymphs.std_error = m_se[[4]];
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs, "OLDNYMPH-F1");
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs.std_error, "OLDNYMPH-F1-SE");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs, "OLDNYMPH.F1");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_old_nymphs.std_error, "OLDNYMPH.F1.SE");
         F2_old_nymphs = m_se[[5]];
         F2_old_nymphs.std_error = m_se[[6]];
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs, "OLDNYMPH-F2");
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs.std_error, "OLDNYMPH-F2-SE");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs, "OLDNYMPH.F2");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_old_nymphs.std_error, "OLDNYMPH.F2.SE");
     }
     if (process_total_nymphs) {
         m_se = get_mean_and_std_error(P_total_nymphs.replications, F1_total_nymphs.replications, F2_total_nymphs.replications);
         P_total_nymphs = m_se[[1]];
         P_total_nymphs.std_error = m_se[[2]];
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs, "TOTALNYMPH-P");
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs.std_error, "TOTALNYMPH-P-SE");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs, "TOTALNYMPH.P");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_nymphs.std_error, "TOTALNYMPH.P.SE");
         F1_total_nymphs = m_se[[3]];
         F1_total_nymphs.std_error = m_se[[4]];
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs, "TOTALNYMPH-F1");
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs.std_error, "TOTALNYMPH-F1-SE");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs, "TOTALNYMPH.F1");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_nymphs.std_error, "TOTALNYMPH.F1.SE");
         F2_total_nymphs = m_se[[5]];
         F2_total_nymphs.std_error = m_se[[6]];
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs, "TOTALNYMPH-F2");
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs.std_error, "TOTALNYMPH-F2-SE");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs, "TOTALNYMPH.F2");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_nymphs.std_error, "TOTALNYMPH.F2.SE");
     }
     if (process_previttelogenic_adults) {
         m_se = get_mean_and_std_error(P_previttelogenic_adults.replications, F1_previttelogenic_adults.replications, F2_previttelogenic_adults.replications);
         P_previttelogenic_adults = m_se[[1]];
         P_previttelogenic_adults.std_error = m_se[[2]];
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults, "PRE-VITADULT-P");
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults.std_error, "PRE-VITADULT-P-SE");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults, "PRE.VITADULT.P");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_previttelogenic_adults.std_error, "PRE.VITADULT.P.SE");
         F1_previttelogenic_adults = m_se[[3]];
         F1_previttelogenic_adults.std_error = m_se[[4]];
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults, "PRE-VITADULT-F1");
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults.std_error, "PRE-VITADULT-F1-SE");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults, "PRE.VITADULT.F1");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_previttelogenic_adults.std_error, "PRE.VITADULT.F1.SE");
         F2_previttelogenic_adults = m_se[[5]];
         F2_previttelogenic_adults.std_error = m_se[[6]];
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults, "PRE-VITADULT-F2");
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults.std_error, "PRE-VITADULT-F2-SE");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults, "PRE.VITADULT.F2");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_previttelogenic_adults.std_error, "PRE.VITADULT.F2.SE");
     }
     if (process_vittelogenic_adults) {
         m_se = get_mean_and_std_error(P_vittelogenic_adults.replications, F1_vittelogenic_adults.replications, F2_vittelogenic_adults.replications);
         P_vittelogenic_adults = m_se[[1]];
         P_vittelogenic_adults.std_error = m_se[[2]];
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults, "VITADULT-P");
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults.std_error, "VITADULT-P-SE");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults, "VITADULT.P");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_vittelogenic_adults.std_error, "VITADULT.P.SE");
         F1_vittelogenic_adults = m_se[[3]];
         F1_vittelogenic_adults.std_error = m_se[[4]];
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults, "VITADULT-F1");
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults.std_error, "VITADULT-F1-SE");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults, "VITADULT.F1");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_vittelogenic_adults.std_error, "VITADULT.F1.SE");
         F2_vittelogenic_adults = m_se[[5]];
         F2_vittelogenic_adults.std_error = m_se[[6]];
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults, "VITADULT-F2");
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults.std_error, "VITADULT-F2-SE");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults, "VITADULT.F2");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_vittelogenic_adults.std_error, "VITADULT.F2.SE");
     }
     if (process_diapausing_adults) {
         m_se = get_mean_and_std_error(P_diapausing_adults.replications, F1_diapausing_adults.replications, F2_diapausing_adults.replications);
         P_diapausing_adults = m_se[[1]];
         P_diapausing_adults.std_error = m_se[[2]];
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults, "DIAPAUSINGADULT-P");
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults.std_error, "DIAPAUSINGADULT-P-SE");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults, "DIAPAUSINGADULT.P");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_diapausing_adults.std_error, "DIAPAUSINGADULT.P.SE");
         F1_diapausing_adults = m_se[[3]];
         F1_diapausing_adults.std_error = m_se[[4]];
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults, "DIAPAUSINGADULT-F1");
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults.std_error, "DIAPAUSINGADULT-F1-SE");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults, "DIAPAUSINGADULT.F1");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_diapausing_adults.std_error, "DIAPAUSINGADULT.F1.SE");
         F2_diapausing_adults = m_se[[5]];
         F2_diapausing_adults.std_error = m_se[[6]];
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults, "DIAPAUSINGADULT-F2");
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults.std_error, "DIAPAUSINGADULT-F2-SE");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults, "DIAPAUSINGADULT.F2");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_diapausing_adults.std_error, "DIAPAUSINGADULT.F2.SE");
     }
     if (process_total_adults) {
         m_se = get_mean_and_std_error(P_total_adults.replications, F1_total_adults.replications, F2_total_adults.replications);
         P_total_adults = m_se[[1]];
         P_total_adults.std_error = m_se[[2]];
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults, "TOTALADULT-P");
-        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults.std_error, "TOTALADULT-P-SE");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults, "TOTALADULT.P");
+        temperature_data_frame_P = append_vector(temperature_data_frame_P, P_total_adults.std_error, "TOTALADULT.P.SE");
         F1_total_adults = m_se[[3]];
         F1_total_adults.std_error = m_se[[4]];
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults, "TOTALADULT-F1");
-        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults.std_error, "TOTALADULT-F1-SE");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults, "TOTALADULT.F1");
+        temperature_data_frame_F1 = append_vector(temperature_data_frame_F1, F1_total_adults.std_error, "TOTALADULT.F1.SE");
         F2_total_adults = m_se[[5]];
         F2_total_adults.std_error = m_se[[6]];
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults, "TOTALADULT-F2");
-        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults.std_error, "TOTALADULT-F2-SE");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults, "TOTALADULT.F2");
+        temperature_data_frame_F2 = append_vector(temperature_data_frame_F2, F2_total_adults.std_error, "TOTALADULT.F2.SE");
     }
 }
 
@@ -1823,7 +1473,7 @@
             for (life_stage_nymph in life_stages_nymph) {
                 # Start PDF device driver.
                 dev.new(width=20, height=30);
-                file_path = get_file_path(life_stage, "nymph_pop_by_generation.pdf", life_stage_nymph=life_stage_nymph)
+                file_path = get_file_path(life_stage, "nymph_pop_by_generation.pdf", sub_life_stage=life_stage_nymph)
                 pdf(file=file_path, width=20, height=30, bg="white");
                 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
                 if (life_stage_nymph=="Young") {
@@ -1856,7 +1506,7 @@
                 }
                 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
                     start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
-                    group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, life_stages_nymph=life_stage_nymph);
+                    group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, sub_life_stage=life_stage_nymph);
                 # Turn off device driver to flush output.
                 dev.off();
             }
@@ -1864,7 +1514,7 @@
             for (life_stage_adult in life_stages_adult) {
                 # Start PDF device driver.
                 dev.new(width=20, height=30);
-                file_path = get_file_path(life_stage, "adult_pop_by_generation.pdf", life_stage_adult=life_stage_adult)
+                file_path = get_file_path(life_stage, "adult_pop_by_generation.pdf", sub_life_stage=life_stage_adult)
                 pdf(file=file_path, width=20, height=30, bg="white");
                 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
                 if (life_stage_adult=="Pre-vittelogenic") {
@@ -1906,7 +1556,7 @@
                 }
                 render_chart(ticks, date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, location, latitude,
                     start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
-                    group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, life_stages_adult=life_stage_adult);
+                    group2=group2, group2_std_error=group2_std_error, group3=group3, group3_std_error=group3_std_error, sub_life_stage=life_stage_adult);
                 # Turn off device driver to flush output.
                 dev.off();
             }
@@ -1945,7 +1595,7 @@
             for (life_stage_nymph in life_stages_nymph) {
                 # Start PDF device driver.
                 dev.new(width=20, height=30);
-                file_path = get_file_path(life_stage, "nymph_pop.pdf", life_stage_nymph=life_stage_nymph)
+                file_path = get_file_path(life_stage, "nymph_pop.pdf", sub_life_stage=life_stage_nymph)
                 pdf(file=file_path, width=20, height=30, bg="white");
                 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
                 if (life_stage_nymph=="Total") {
@@ -1964,7 +1614,7 @@
                 maxval = max(group+group_std_error) + 100;
                 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
                     start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
-                    life_stages_nymph=life_stage_nymph);
+                    sub_life_stage=life_stage_nymph);
                 # Turn off device driver to flush output.
                 dev.off();
             }
@@ -1972,7 +1622,7 @@
             for (life_stage_adult in life_stages_adult) {
                 # Start PDF device driver.
                 dev.new(width=20, height=30);
-                file_path = get_file_path(life_stage, "adult_pop.pdf", life_stage_adult=life_stage_adult)
+                file_path = get_file_path(life_stage, "adult_pop.pdf", sub_life_stage=life_stage_adult)
                 pdf(file=file_path, width=20, height=30, bg="white");
                 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1));
                 if (life_stage_adult=="Total") {
@@ -1995,7 +1645,7 @@
                 maxval = max(group+group_std_error) + 100;
                 render_chart(ticks, date_labels, "pop_size_by_life_stage", opt$plot_std_error, opt$insect, location, latitude,
                     start_date, end_date, total_days_vector, maxval, opt$replications, life_stage, group=group, group_std_error=group_std_error,
-                    life_stages_adult=life_stage_adult);
+                    sub_life_stage=life_stage_adult);
                 # Turn off device driver to flush output.
                 dev.off();
             }
--- a/insect_phenology_model.xml	Tue Jun 05 07:52:59 2018 -0400
+++ b/insect_phenology_model.xml	Tue Aug 07 12:59:06 2018 -0400
@@ -8,7 +8,6 @@
 #set output_data_dir = "output_data_dir"
 #set output_plots_dir = "output_plots_dir"
 #set error_file = $os.path.join($output_data_dir, "04_combined_generations.csv")
-#set custom_date_interval = $custom_date_interval_cond.custom_date_interval
 #set life_stages = list()
 #set plot_adult_life_stage = $plot_adult_life_stage_cond.plot_adult_life_stage
 #set plot_nymph_life_stage = $plot_nymph_life_stage_cond.plot_nymph_life_stage
@@ -55,10 +54,7 @@
 --plot_generations_separately $plot_generations_separately
 --plot_std_error $plot_std_error
 --replications $replications
-#if str($custom_date_interval) == "yes":
-    --start_date '$custom_date_interval_cond.start_date'
-    --end_date '$custom_date_interval_cond.end_date'
-#end if
+--script_dir '$__tool_directory__'
 --young_nymph_accumulation $young_nymph_accumulation
 &>ipm_log.txt;
 if [[ $? -ne 0 ]]; then
@@ -97,21 +93,6 @@
         <param name="young_nymph_accumulation" type="integer" value="0" min="0" label="Adjustment of degree-days accumulation (egg->young nymph)"/>
         <param name="old_nymph_accumulation" type="integer" value="0" min="0" label="Adjustment of degree-days accumulation (young nymph->old nymph)"/>
         <param name="adult_accumulation" type="integer" value="0" min="0" label="Adjustment of degree-days accumulation (old nymph->adult)"/>
-        <conditional name="custom_date_interval_cond">
-            <param name="custom_date_interval" type="select" label="Set date interval for plots?" help="Scales the x axis in plots from weeks to days">
-                <option value="no" selected="true">No</option>
-                <option value="yes">Yes</option>
-            </param>
-            <when value="no"/>
-            <when value="yes">
-                <param name="start_date" type="text" value="" label="Start date" help="Format must be yyyy-mm-dd">
-                    <validator type="expression" message="Date must have the format yyyy-mm-dd">len(value.split('-')[0])==4 and int(value.split('-')[0]) and len(value.split('-')[1])==2 and int(value.split('-')[1]) and len(value.split('-')[2])==2 and int(value.split('-')[2])</validator>
-                </param>
-                <param name="end_date" type="text" value="" label="End date" help="Format must be yyyy-mm-dd">
-                    <validator type="expression" message="Date must have the format yyyy-mm-dd">len(value.split('-')[0])==4 and int(value.split('-')[0]) and len(value.split('-')[1])==2 and int(value.split('-')[1]) and len(value.split('-')[2])==2 and int(value.split('-')[2])</validator>
-                </param>
-            </when>
-        </conditional>
         <param name="plot_generations_separately" type="select" label="Plot generations separately?">
             <option value="yes" selected="True">Yes</option>
             <option value="no">No</option>
--- a/test-data/output_combined1.csv	Tue Jun 05 07:52:59 2018 -0400
+++ b/test-data/output_combined1.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -1,1 +1,1 @@
-"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","YOUNGNYMPH","YOUNGNYMPHSE","PRE-VITADULT","PRE-VITADULTSE"
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","YOUNGNYMPH","YOUNGNYMPHSE","PRE.VITADULT","PRE.VITADULTSE"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/output_combined6.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -0,0 +1,16 @@
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","YOUNGNYMPH","YOUNGNYMPHSE","PRE.VITADULT","PRE.VITADULTSE"
+40.81849,-77.84637,"2017-04-01",91,3.3,7.66,12.6361192995361
+40.81849,-77.84637,"2017-04-02",92,1.69,15.19,12.681126343138
+40.81849,-77.84637,"2017-04-03",93,3.48,18.81,12.7260513149927
+40.81849,-77.84637,"2017-04-04",94,9.28,18.75,12.7708878775247
+40.81849,-77.84637,"2017-04-05",95,8.14,19.01,12.8156295592851
+40.81849,-77.84637,"2017-04-06",96,4.59,12.02,12.8602697436611
+40.81849,-77.84637,"2017-04-07",97,1.19,3.66,12.9048016577475
+40.81849,-77.84637,"2017-04-08",98,-0.56,13.85,12.9492183613912
+40.81849,-77.84637,"2017-04-09",99,-2.32,21.96,12.9935127364246
+40.81849,-77.84637,"2017-04-10",100,7.9,26.25,13.037677476103
+40.81849,-77.84637,"2017-04-11",101,9.25,26.48,13.081705074763
+40.81849,-77.84637,"2017-04-12",102,8.95,19.78,13.1255878177204
+40.81849,-77.84637,"2017-04-13",103,4,15.47,13.1693177714296
+40.81849,-77.84637,"2017-04-14",104,5.09,20.1,13.2128867739251
+40.81849,-77.84637,"2017-04-15",105,8.44,25.02,13.2562864255699
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/output_combined7.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -0,0 +1,16 @@
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","YOUNGNYMPH","YOUNGNYMPHSE","PRE.VITADULT","PRE.VITADULTSE"
+40.3844,-76.0339,"2017-01-01",1,-6.8,2.8,9.33687304211315
+40.3844,-76.0339,"2017-01-02",2,-6.8,2.7,9.34822021765528
+40.3844,-76.0339,"2017-01-03",3,-6.9,2.6,9.36062371263695
+40.3844,-76.0339,"2017-01-04",4,-7,2.6,9.3740716338922
+40.3844,-76.0339,"2017-01-05",5,-7.1,2.5,9.38855118408927
+40.3844,-76.0339,"2017-01-06",6,-7.1,2.5,9.40404870530151
+40.3844,-76.0339,"2017-01-07",7,-7.2,2.4,9.42054972475204
+40.3844,-76.0339,"2017-01-08",8,-7.2,2.4,9.43803900243173
+40.3844,-76.0339,"2017-01-09",9,-7.3,2.4,9.45650058028574
+40.3844,-76.0339,"2017-01-10",10,-7.3,2.3,9.4759178326624
+40.3844,-76.0339,"2017-01-11",11,-7.4,2.3,9.4962735177201
+40.3844,-76.0339,"2017-01-12",12,-7.4,2.3,9.51754982949216
+40.3844,-76.0339,"2017-01-13",13,-7.4,2.3,9.53972845031694
+40.3844,-76.0339,"2017-01-14",14,-7.5,2.3,9.56279060334963
+40.3844,-76.0339,"2017-01-15",15,-7.5,2.3,9.58671710488405
--- a/test-data/output_f1_3.csv	Tue Jun 05 07:52:59 2018 -0400
+++ b/test-data/output_f1_3.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -1,1 +1,1 @@
-"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","OLDNYMPH-F1","OLDNYMPH-F1-SE"
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","OLDNYMPH.F1","OLDNYMPH.F1.SE"
--- a/test-data/output_f1_4.csv	Tue Jun 05 07:52:59 2018 -0400
+++ b/test-data/output_f1_4.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -1,1 +1,1 @@
-"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","EGG-F1","EGG-F1-SE","TOTALNYMPH-F1","TOTALNYMPH-F1-SE","TOTALADULT-F1","TOTALADULT-F1-SE"
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","EGG.F1","EGG.F1.SE","TOTALNYMPH.F1","TOTALNYMPH.F1.SE","TOTALADULT.F1","TOTALADULT.F1.SE"
--- a/test-data/output_f2_3.csv	Tue Jun 05 07:52:59 2018 -0400
+++ b/test-data/output_f2_3.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -1,1 +1,1 @@
-"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","OLDNYMPH-F2","OLDNYMPH-F2-SE"
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","OLDNYMPH.F2","OLDNYMPH.F2.SE"
--- a/test-data/output_f2_4.csv	Tue Jun 05 07:52:59 2018 -0400
+++ b/test-data/output_f2_4.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -1,1 +1,1 @@
-"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","EGG-F2","EGG-F2-SE","TOTALNYMPH-F2","TOTALNYMPH-F2-SE","TOTALADULT-F2","TOTALADULT-F2-SE"
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","EGG.F2","EGG.F2.SE","TOTALNYMPH.F2","TOTALNYMPH.F2.SE","TOTALADULT.F2","TOTALADULT.F2.SE"
--- a/test-data/output_p_3.csv	Tue Jun 05 07:52:59 2018 -0400
+++ b/test-data/output_p_3.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -1,1 +1,1 @@
-"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","OLDNYMPH-P","OLDNYMPH-P-SE"
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","OLDNYMPH.P","OLDNYMPH.P.SE"
--- a/test-data/output_p_4.csv	Tue Jun 05 07:52:59 2018 -0400
+++ b/test-data/output_p_4.csv	Tue Aug 07 12:59:06 2018 -0400
@@ -1,1 +1,1 @@
-"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","EGG-P","EGG-P-SE","TOTALNYMPH-P","TOTALNYMPH-P-SE","TOTALADULT-P","TOTALADULT-P-SE"
+"LATITUDE","LONGITUDE","DATE","DOY","TMIN","TMAX","DAYLEN","EGG.P","EGG.P.SE","TOTALNYMPH.P","TOTALNYMPH.P.SE","TOTALADULT.P","TOTALADULT.P.SE"
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils.R	Tue Aug 07 12:59:06 2018 -0400
@@ -0,0 +1,317 @@
+#!/usr/bin/env Rscript
+
+get_file_path = function(life_stage, base_name, sub_life_stage=NULL) {
+    if (is.null(sub_life_stage)) {
+        lsi = get_life_stage_index(life_stage);
+        file_name = paste(lsi, base_name, sep="_");
+    } else {
+        lsi = get_life_stage_index(life_stage, sub_life_stage=sub_life_stage);
+        file_name = paste(lsi, tolower(sub_life_stage), base_name, sep="_");
+    }
+    file_path = paste("output_plots_dir", file_name, sep="/");
+    return(file_path);
+}
+
+get_year_from_date = function(date_str) {
+    date_str_items = strsplit(date_str, "-")[[1]];
+    return (date_str_items[1]);
+}
+
+get_life_stage_index = function(life_stage, sub_life_stage=NULL) {
+    # Name collection elements so that they
+    # are displayed in logical order.
+    if (life_stage=="Egg") {
+        lsi = "01";
+    } else if (life_stage=="Nymph") {
+        if (sub_life_stage=="Young") {
+            lsi = "02";
+        } else if (sub_life_stage=="Old") {
+            lsi = "03";
+        } else if (sub_life_stage=="Total") {
+            lsi="04";
+        }
+    } else if (life_stage=="Adult") {
+        if (sub_life_stage=="Pre-vittelogenic") {
+            lsi = "05";
+        } else if (sub_life_stage=="Vittelogenic") {
+            lsi = "06";
+        } else if (sub_life_stage=="Diapausing") {
+            lsi = "07";
+        } else if (sub_life_stage=="Total") {
+            lsi = "08";
+        }
+    } else if (life_stage=="Total") {
+        lsi = "09";
+    }
+    return(lsi);
+}
+
+get_mean_and_std_error = function(p_replications, f1_replications, f2_replications) {
+    # P mean.
+    p_m = apply(p_replications, 1, mean);
+    # P standard error.
+    p_se = apply(p_replications, 1, sd) / sqrt(opt$replications);
+    # F1 mean.
+    f1_m = apply(f1_replications, 1, mean);
+    # F1 standard error.
+    f1_se = apply(f1_replications, 1, sd) / sqrt(opt$replications);
+    # F2 mean.
+    f2_m = apply(f2_replications, 1, mean);
+    # F2 standard error.
+    f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications);
+    return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))
+}
+
+get_tick_index = function(index, last_tick, ticks, tick_labels, tick_sep) {
+    # The R code tries hard not to draw overlapping tick labels, and so
+    # will omit labels where they would abut or overlap previously drawn
+    # labels. This can result in, for example, every other tick being
+    # labelled.  We'll keep track of the last tick to make sure all of
+    # the month labels are displayed, and missing ticks are restricted
+    # to Sundays which have no labels anyway.
+    if (last_tick==0) {
+        return(length(ticks)+1);
+    }
+    last_saved_tick = ticks[[length(ticks)]];
+    if (index-last_saved_tick<tick_sep) {
+        last_saved_month = tick_labels[[length(tick_labels)]];
+        if (last_saved_month=="") {
+            # We're safe overwriting a tick
+            # with no label (i.e., a Sunday tick).
+            return(length(ticks));
+        } else {
+            # Don't eliminate a Month label.
+            return(NULL);
+        }
+    }
+    return(length(ticks)+1);
+}
+
+get_total_days = function(is_leap_year) {
+    # Get the total number of days in the current year.
+    if (is_leap_year) {
+        return(366);
+    } else {
+        return(365);
+    }
+}
+
+get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm=0, append_start_doy_norm=0, date_interval=FALSE) {
+    # Generate a list of ticks and labels for plotting the x axis.
+    if (prepend_end_doy_norm > 0) {
+        prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm);
+    } else {
+        prepend_end_norm_row = 0;
+    }
+    if (append_start_doy_norm > 0) {
+        append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm);
+    } else {
+        append_start_norm_row = 0;
+    }
+    num_rows = dim(temperature_data_frame)[1];
+    tick_labels = list();
+    ticks = list();
+    current_month_label = NULL;
+    last_tick = 0;
+    if (date_interval) {
+        tick_sep = 0;
+    } else {
+        tick_sep = 3;
+    }
+    for (i in 1:num_rows) {
+        # Get the year and month from the date which
+        # has the format YYYY-MM-DD.
+        date = format(temperature_data_frame$DATE[i]);
+        # Get the month label.
+        items = strsplit(date, "-")[[1]];
+        month = items[2];
+        month_label = month.abb[as.integer(month)];
+        day = as.integer(items[3]);
+        doy = as.integer(temperature_data_frame$DOY[i]);
+        # We're plotting the entire year, so ticks will
+        # occur on Sundays and the first of each month.
+        if (i == prepend_end_norm_row) {
+            # Add a tick for the end of the 30 year normnals data
+            # that was prepended to the year-to-date data.
+            label_str = "End prepended 30 year normals";
+            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+            ticks[tick_index] = i;
+            if (date_interval) {
+                # Append the day to label_str
+                tick_labels[tick_index] = paste(label_str, day, sep=" ");
+            } else {
+                tick_labels[tick_index] = label_str;
+            }
+            last_tick = i;
+        } else if (doy == append_start_doy_norm) {
+            # Add a tick for the start of the 30 year normnals data
+            # that was appended to the year-to-date data.
+            label_str = "Start appended 30 year normals";
+            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+            ticks[tick_index] = i;
+            if (!identical(current_month_label, month_label)) {
+                # Append the month to label_str.
+                label_str = paste(label_str, month_label, spe=" ");
+                current_month_label = month_label;
+            }
+            if (date_interval) {
+                # Append the day to label_str
+                label_str = paste(label_str, day, sep=" ");
+            }
+            tick_labels[tick_index] = label_str;
+            last_tick = i;
+        } else if (i==num_rows) {
+            # Add a tick for the last day of the year.
+            label_str = "";
+            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+            ticks[tick_index] = i;
+            if (!identical(current_month_label, month_label)) {
+                # Append the month to label_str.
+                label_str = month_label;
+                current_month_label = month_label;
+            }
+            if (date_interval) {
+                # Append the day to label_str
+                label_str = paste(label_str, day, sep=" ");
+            }
+            tick_labels[tick_index] = label_str;
+        } else {
+            if (!identical(current_month_label, month_label)) {
+                # Add a tick for the month.
+                tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+                ticks[tick_index] = i;
+                if (date_interval) {
+                    # Append the day to the month.
+                    tick_labels[tick_index] = paste(month_label, day, sep=" ");
+                } else {
+                    tick_labels[tick_index] = month_label;
+                }
+                current_month_label = month_label;
+                last_tick = i;
+            }
+            tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+            if (!is.null(tick_index)) {
+                if (date_interval) {
+                    # Add a tick for every day. The first tick is the
+                    # month label, so add a tick only if i is not 1
+                    if (i>1 & day>1) {
+                        tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
+                        ticks[tick_index] = i;
+                        # Add the day as the label.
+                        tick_labels[tick_index] = day;
+                        last_tick = i;
+                    }
+                } else {
+                    # Get the day.
+                    day = weekdays(as.Date(date));
+                    if (day=="Sunday") {
+                        # Add a tick if we're on a Sunday.
+                        ticks[tick_index] = i;
+                        # Add a blank month label so it is not displayed.
+                        tick_labels[tick_index] = "";
+                        last_tick = i;
+                    }
+                }
+            }
+        }
+    }
+    return(list(ticks, tick_labels));
+}
+
+render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,
+            replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,
+            sub_life_stage=NULL) {
+    if (chart_type=="pop_size_by_life_stage") {
+        if (life_stage=="Total") {
+            title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
+            legend_text = c("Egg", "Nymph", "Adult");
+            columns = c(4, 2, 1);
+            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);
+            legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
+            lines(days, group2, lwd=2, lty=1, col=2);
+            lines(days, group3, lwd=2, lty=1, col=4);
+            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);
+            axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
+            if (plot_std_error=="yes") {
+                # Standard error for group.
+                lines(days, group+group_std_error, lty=2);
+                lines(days, group-group_std_error, lty=2);
+                # Standard error for group2.
+                lines(days, group2+group2_std_error, col=2, lty=2);
+                lines(days, group2-group2_std_error, col=2, lty=2);
+                # Standard error for group3.
+                lines(days, group3+group3_std_error, col=4, lty=2);
+                lines(days, group3-group3_std_error, col=4, lty=2);
+            }
+        } else {
+            if (life_stage=="Egg") {
+                title = paste(insect,  ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
+                legend_text = c(life_stage);
+                columns = c(4);
+            } else if (life_stage=="Nymph") {
+                stage = paste(sub_life_stage, "Nymph Pop :", sep=" ");
+                title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
+                legend_text = c(paste(sub_life_stage, life_stage, sep=" "));
+                columns = c(2);
+            } else if (life_stage=="Adult") {
+                stage = paste(sub_life_stage, "Adult Pop", sep=" ");
+                title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
+                legend_text = c(paste(sub_life_stage, life_stage, sep=" "));
+                columns = c(1);
+            }
+            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);
+            legend("topleft", legend_text, lty=c(1), col="black", cex=3);
+            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);
+            axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
+            if (plot_std_error=="yes") {
+                # Standard error for group.
+                lines(days, group+group_std_error, lty=2);
+                lines(days, group-group_std_error, lty=2);
+            }
+        }
+    } else if (chart_type=="pop_size_by_generation") {
+        if (life_stage=="Total") {
+            title_str = ": Total Pop by Gen :";
+        } else if (life_stage=="Egg") {
+            title_str = ": Egg Pop by Gen :";
+        } else if (life_stage=="Nymph") {
+            title_str = paste(":", sub_life_stage, "Nymph Pop by Gen", ":", sep=" ");
+        } else if (life_stage=="Adult") {
+            title_str = paste(":", sub_life_stage, "Adult Pop by Gen", ":", sep=" ");
+        }
+        title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
+        legend_text = c("P", "F1", "F2");
+        columns = c(1, 2, 4);
+        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);
+        legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
+        lines(days, group2, lwd=2, lty=1, col=2);
+        lines(days, group3, lwd=2, lty=1, col=4);
+        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);
+        axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
+        if (plot_std_error=="yes") {
+            # Standard error for group.
+            lines(days, group+group_std_error, lty=2);
+            lines(days, group-group_std_error, lty=2);
+            # Standard error for group2.
+            lines(days, group2+group2_std_error, col=2, lty=2);
+            lines(days, group2-group2_std_error, col=2, lty=2);
+            # Standard error for group3.
+            lines(days, group3+group3_std_error, col=4, lty=2);
+            lines(days, group3-group3_std_error, col=4, lty=2);
+        }
+    }
+}
+
+stop_err = function(msg) {
+    cat(msg, file=stderr());
+    quit(save="no", status=1);
+}
+
+validate_date = function(date_str) {
+    valid_date = as.Date(date_str, format="%Y-%m-%d");
+    if( class(valid_date)=="try-error" || is.na(valid_date)) {
+        msg = paste("Invalid date: ", date_str, ", valid date format is yyyy-mm-dd.", sep="");
+        stop_err(msg);
+    }
+    return(valid_date);
+}