changeset 125:24f389a2dd93 draft

Uploaded
author greg
date Tue, 07 Aug 2018 12:58:20 -0400
parents 534658644efe
children 397c24c1adc9
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, 542 insertions(+), 466 deletions(-) [+]
line wrap: on
line diff
--- a/insect_phenology_model.R	Fri Jun 01 08:00:53 2018 -0400
+++ b/insect_phenology_model.R	Tue Aug 07 12:58:20 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,82 +65,61 @@
     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(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy) {
+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);
     last_norm_row = which(norm_data_frame$DOY==end_date_doy);
-    norm_data_frame_rows = last_norm_row - first_norm_row;
+    # Add 1 to the number of rows to ensure that the end date is included.
+    tmp_data_frame_rows = last_norm_row - first_norm_row + 1;
+    tmp_data_frame = get_new_temperature_data_frame(nrow=tmp_data_frame_rows);
     j = 0;
     for (i in first_norm_row:last_norm_row) {
         j = j + 1;
-        temperature_data_frame[j,] = get_next_normals_row(norm_data_frame, year, i);
+        tmp_data_frame[j,] = get_next_normals_row(norm_data_frame, year, i);
     }
-    return (temperature_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);
+    return (tmp_data_frame);
 }
 
-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";
+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
+    column_names = c("STATIONID", "LATITUDE","LONGITUDE", "ELEV_M", "NAME", "ST", "MMDD", "DOY", "TMIN", "TMAX");
+    if (is.null(input_norm)) {
+        norm_data_frame = data.frame(matrix(ncol=10, nrow));
+        # Set the norm_data_frame column names for access.
+        colnames(norm_data_frame) = column_names;
+    } else {
+        norm_data_frame = read.csv(file=input_norm, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
+        # Set the norm_data_frame column names for access.
+        colnames(norm_data_frame) = column_names;
+        if (!is_leap_year) {
+            # All normals data includes Feb 29 which is row 60 in
+            # the data, so delete that row if we're not in a leap year.
+            norm_data_frame = norm_data_frame[-c(60),];
+            # Since we've removed row 60, we need to subtract 1 from
+            # each value in the DOY column of the data frame starting
+            # with the 60th row.
+            num_rows = dim(norm_data_frame)[1];
+            for (i in 60:num_rows) {
+                leap_year_doy = norm_data_frame$DOY[i];
+                non_leap_year_doy = leap_year_doy - 1;
+                norm_data_frame$DOY[i] = non_leap_year_doy;
+            }
         }
-    } 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);
+    return (norm_data_frame);
 }
 
-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_temperature_data_frame = function(input_ytd=NULL, nrow=0) {
+    # The input_ytd data has the following 6 columns:
+    # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
+    if (is.null(input_ytd)) {
+        temperature_data_frame = data.frame(matrix(ncol=6, nrow));
+    } else {
+        temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
+    }
+    colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
+    return(temperature_data_frame);
 }
 
 get_next_normals_row = function(norm_data_frame, year, index) {
@@ -229,124 +207,6 @@
     return(c(curr_mean_temp, averages))
 }
 
-get_tick_index = function(index, last_tick, ticks, month_labels) {
-    # 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<3) {
-        last_saved_month = month_labels[[length(month_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.  There are several scenarios that affect this.
-    # 1. If date_interval is TRUE:
-    #    a.
-    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];
-    month_labels = list();
-    ticks = list();
-    current_month_label = NULL;
-    last_tick = 0;
-    for (i in 1:num_rows) {
-        # 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.
-            tick_index = get_tick_index(i, last_tick, ticks, month_labels)
-            ticks[tick_index] = i;
-            month_labels[tick_index] = "End prepended 30 year normals";
-            last_tick = i;
-        } else if (i == 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.
-            tick_index = get_tick_index(i, last_tick, ticks, month_labels)
-            ticks[tick_index] = i;
-            month_labels[tick_index] = "Start appended 30 year normals";
-            last_tick = i;
-        } else if (i==num_rows) {
-            # Add a tick for the last day of the year.
-            tick_index = get_tick_index(i, last_tick, ticks, month_labels)
-            ticks[tick_index] = i;
-            month_labels[tick_index] = "";
-            last_tick = i;
-        } else {
-            # 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)];
-            if (!identical(current_month_label, month_label)) {
-                # Add a tick for the month.
-                tick_index = get_tick_index(i, last_tick, ticks, month_labels)
-                ticks[tick_index] = i;
-                month_labels[tick_index] = month_label;
-                current_month_label = month_label;
-                last_tick = i;
-            }
-            tick_index = get_tick_index(i, last_tick, ticks, month_labels)
-            if (!is.null(tick_index)) {
-                if (date_interval) {
-                    # Add a tick for every day.
-                    ticks[tick_index] = i;
-                    # Add a blank month label so it is not displayed.
-                    month_labels[tick_index] = "";
-                    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.
-                        month_labels[tick_index] = "";
-                        last_tick = i;
-                    }
-                }
-            }
-        }
-    }
-    return(list(ticks, month_labels));
-}
-
 is_leap_year = function(date_str) {
     # Extract the year from the date_str.
     date = format(date_str);
@@ -397,64 +257,72 @@
     prepend_end_doy_norm = 0;
     # The start DOY for norm data appended to ytd data.
     append_start_doy_norm = 0;
+    if (is.null(start_date) && is.null(end_date)) {
+        # We're not dealing with a date interval.
+        date_interval = FALSE;
+        if (is.null(input_ytd)) {
+            # Base all dates on the current date since 30 year
+            # normals data does not include any dates.
+            year = format(Sys.Date(), "%Y");
+        }
+    } else {
+        date_interval = TRUE;
+        year = get_year_from_date(start_date);
+        # Get the DOY for start_date and end_date.
+        start_date_doy = as.integer(strftime(start_date, format="%j"));
+        end_date_doy = as.integer(strftime(end_date, format="%j"));
+    }
     if (is.null(input_ytd)) {
         # We're processing only the 30 year normals data.
         processing_year_to_date_data = FALSE;
+        if (is.null(start_date) && is.null(end_date)) {
+            # We're processing the entire year, so we can
+            # set the start_date to Jan 1.
+            start_date = paste(year, "01", "01", sep="-");
+        }
     } else {
         processing_year_to_date_data = TRUE;
+        # Read the input_ytd temperature data file into a data frame.
+        temperature_data_frame = get_new_temperature_data_frame(input_ytd=input_ytd);
+        num_ytd_rows = dim(temperature_data_frame)[1];
+        if (!date_interval) {
+            start_date = temperature_data_frame$DATE[1];
+            year = get_year_from_date(start_date);
+        }
     }
-    if (is.null(start_date) && is.null(end_date)) {
-        # We're processing the entire year, possibly merging
-        # data from input_norm with data from input_ytd.
-        date_interval = FALSE;
-    } else {
-        date_interval = TRUE;
-        # Get the DOY for start_date and end_date.
-        start_date_doy = strftime(start_date, format="%j");
-        end_date_doy = strftime(end_date, format="%j");
-    }
+    # See if we're in a leap year.
+    is_leap_year = is_leap_year(start_date);
     # Read the input_norm temperature datafile into a data frame.
-    # The input_norm data has the following 10 columns:
-    # STATIONID, LATITUDE, LONGITUDE, ELEV_M, NAME, ST, MMDD, DOY, TMIN, TMAX
-    norm_data_frame = read.csv(file=input_norm, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
-    # Set the norm_data_frame column names for access.
-    colnames(norm_data_frame) = c("STATIONID", "LATITUDE","LONGITUDE", "ELEV_M", "NAME", "ST", "MMDD", "DOY", "TMIN", "TMAX");
+    norm_data_frame = get_new_norm_data_frame(is_leap_year, input_norm=input_norm);
     if (processing_year_to_date_data) {
-        # Read the input_ytd temperature data file into a data frame.
-        # The input_ytd data has the following 6 columns:
-        # LATITUDE, LONGITUDE, DATE, DOY, TMIN, TMAX
-        temperature_data_frame = read.csv(file=input_ytd, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=",");
-        # Set the temperature_data_frame column names for access.
-        colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
         if (date_interval) {
             # We're plotting a date interval.
             start_date_ytd_row = which(temperature_data_frame$DATE==start_date);
-            if (start_date_ytd_row > 0) {
+            if (length(start_date_ytd_row) > 0) {
                 # The start date is contained within the input_ytd data.
+                start_date_ytd_row = start_date_ytd_row[1];
                 start_doy_ytd = as.integer(temperature_data_frame$DOY[start_date_ytd_row]);
             } else {
                 # The start date is contained within the input_norm data.
-                start_date_norm_row = which(norm_data_frame$DATE==start_date);
+                start_date_ytd_row = 0;
+                start_date_norm_row = which(norm_data_frame$DOY==start_date_doy);
             }
             end_date_ytd_row = which(temperature_data_frame$DATE==end_date);
-            if (end_date_ytd_row > 0) {
+            if (length(end_date_ytd_row) > 0) {
+                end_date_ytd_row = end_date_ytd_row[1];
                 # The end date is contained within the input_ytd data.
                 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]);
+            } else {
+                end_date_ytd_row = 0;
             }
-            date_str = start_date;
-            # Extract the year from the start date.
-            date_str_items = strsplit(date_str, "-")[[1]];
-            year = date_str_items[1];
         } else {
             # We're plotting an entire year.
-            # Get the number of days contained in temperature_data_frame.
-            num_rows = dim(temperature_data_frame)[1];
             # Get the start date and end date from temperature_data_frame.
             start_date_ytd_row = 1;
             # Temporarily set start_date to get the year.
             start_date = temperature_data_frame$DATE[1];
-            end_date_ytd_row = num_rows;
-            end_date = temperature_data_frame$DATE[num_rows];
+            end_date_ytd_row = num_ytd_rows;
+            end_date = temperature_data_frame$DATE[num_ytd_rows];
             date_str = format(start_date);
             # Extract the year from the start date.
             date_str_items = strsplit(date_str, "-")[[1]];
@@ -466,14 +334,13 @@
             end_date = paste(year, "12", "31", sep="-");
             # Save the first DOY to later check if start_date is Jan 1.
             start_doy_ytd = as.integer(temperature_data_frame$DOY[1]);
-            end_doy_ytd = as.integer(temperature_data_frame$DOY[num_rows]);
+            end_doy_ytd = as.integer(temperature_data_frame$DOY[num_ytd_rows]);
         }
     } else {
         # We're processing only the 30 year normals data, so create an empty
         # data frame for containing temperature data after it is converted
         # from the 30 year normals format to the year-to-date format.
-        temperature_data_frame = data.frame(matrix(ncol=6, nrow=0));
-        colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX");
+        temperature_data_frame = get_new_temperature_data_frame();
         if (date_interval) {
             # We're plotting a date interval.
             # Extract the year, month and day from the start date.
@@ -491,20 +358,10 @@
             end_date = paste(year, end_date_month, end_date_day, sep="-");
         } else {
             # We're plotting an entire year.
-            # Base all dates on the current date since 30 year
-            # normals data does not include any dates.
-            year = format(Sys.Date(), "%Y");
             start_date = paste(year, "01", "01", sep="-");
             end_date = paste(year, "12", "31", sep="-");
         }
     }
-    # See if we're in a leap year.
-    is_leap_year = is_leap_year(start_date);
-    # All normals data includes Feb 29 which is row 60 in
-    # the data, so delete that row if we're not in a leap year.
-    if (!is_leap_year) {
-        norm_data_frame = norm_data_frame[-c(60),];
-    }
     # Set the location to be the station name if the user elected not to enter it.
     if (is.null(location) | length(location) == 0) {
         location = norm_data_frame$NAME[1];
@@ -528,36 +385,36 @@
                 prepend_end_doy_norm = first_ytd_doy - 1;
                 # Get the number of rows for the restricted date interval
                 # that are contained in temperature_data_frame.
-                temperature_data_frame_rows = end_date_ytd_row;
+                num_temperature_data_frame_rows = end_date_ytd_row;
                 # Get the last row needed from the 30 year normals data.
                 last_norm_row = which(norm_data_frame$DOY==prepend_end_doy_norm);
                 # Get the number of rows for the restricted date interval
                 # that are contained in norm_data_frame.
-                norm_data_frame_rows = last_norm_row - first_norm_row;
+                num_norm_data_frame_rows = last_norm_row - first_norm_row;
                 # Create a temporary data frame to contain the 30 year normals
                 # data from the start date to the date immediately prior to the
                 # first row of the input_ytd data.
-                tmp_norm_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows));
+                tmp_norm_data_frame = get_new_temperature_data_frame(nrow=num_temperature_data_frame_rows+num_norm_data_frame_rows);
+                j = 1;
                 for (i in first_norm_row:last_norm_row) {
                     # Populate the temp_data_frame row with
                     # values from norm_data_frame.
-                    tmp_norm_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i);
+                    tmp_norm_data_frame[j,] = get_next_normals_row(norm_data_frame, year, i);
+                    j = j + 1;
                 }
                 # Create a second temporary data frame containing the
                 # appropriate rows from temperature_data_frame.
-                tmp_temperature_data_frame = temperature_data_frame[1:first_norm_row-1,];
+                tmp_temperature_data_frame = temperature_data_frame[1:num_temperature_data_frame_rows,];
                 # Merge the 2 temporary data frames.
                 temperature_data_frame = rbind(tmp_norm_data_frame, tmp_temperature_data_frame);
             } else if (start_date_ytd_row > 0 & end_date_ytd_row == 0) {
                 # The date interval starts in input_ytd and ends in input_norm,
                 # so append appropriate rows from input_norm to appropriate rows
-                # from input_ytd.
-                num_rows = dim(temperature_data_frame)[1];
-                # Get the number of rows for the restricted date interval
-                # that are contained in temperature_data_frame.
-                temperature_data_frame_rows = num_rows - start_date_ytd_row
+                # from input_ytd. First, get the number of rows for the restricted
+                # date interval that are contained in temperature_data_frame.
+                num_temperature_data_frame_rows = num_ytd_rows - start_date_ytd_row + 1;
                 # Get the DOY of the last row in the input_ytd data.
-                last_ytd_doy = temperature_data_frame$DOY[num_rows];
+                last_ytd_doy = temperature_data_frame$DOY[num_ytd_rows];
                 # Get the DOYs for the first and last rows from norm_data_frame
                 # that will be appended to temperature_data_frame.
                 append_start_doy_norm = last_ytd_doy + 1;
@@ -567,21 +424,29 @@
                 last_norm_row = which(norm_data_frame$DOY == end_date_doy);
                 # Get the number of rows for the restricted date interval
                 # that are contained in norm_data_frame.
-                norm_data_frame_rows = last_norm_row - first_norm_row;
+                num_norm_data_frame_rows = last_norm_row - first_norm_row;
                 # Create a temporary data frame to contain the data
-                # taken from both temperateu_data_frame and norm_data_frame
+                # taken from both temperature_data_frame and norm_data_frame
                 # for the date interval.
-                tmp_data_frame = data.frame(matrix(ncol=6, nrow=temperature_data_frame_rows+norm_data_frame_rows));
+                tmp_data_frame = get_new_temperature_data_frame(nrow=num_temperature_data_frame_rows+num_norm_data_frame_rows);
                 # Populate tmp_data_frame with the appropriate rows from temperature_data_frame.
-                tmp_data_frame[temperature_data_frame_rows,] = temperature_data_frame[start_date_ytd_row:temperature_data_frame_rows,];
+                j = start_date_ytd_row;
+                for (i in 1:num_temperature_data_frame_rows) {
+                    tmp_data_frame[i,] = temperature_data_frame[j,];
+                    j = j + 1;
+                }
                 # Apppend the appropriate rows from norm_data_frame to tmp_data_frame.
-                for (i in first_norm_row:last_norm_row) {
-                    tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i);
+                current_iteration = num_temperature_data_frame_rows + 1;
+                num_iterations = current_iteration + num_norm_data_frame_rows;
+                j = first_norm_row;
+                for (i in current_iteration:num_iterations) {
+                    tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, j);
+                    j = j + 1;
                 }
                 temperature_data_frame = tmp_data_frame[,];
             } else if (start_date_ytd_row == 0 & end_date_ytd_row == 0) {
                 # The date interval is contained witin input_norm.
-                temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy);
+                temperature_data_frame = from_30_year_normals(norm_data_frame, start_date_doy, end_date_doy, year);
             }
         } else {
             # We're plotting an entire year.
@@ -616,7 +481,7 @@
         # We're processing only the 30 year normals data.
         if (date_interval) {
             # Populate temperature_data_frame from norm_data_frame.
-            temperature_data_frame = from_30_year_normals(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy);
+            temperature_data_frame = from_30_year_normals(norm_data_frame, start_date_doy, end_date_doy, year);
         } else {
             total_days = get_total_days(is_leap_year);
             for (i in 1:total_days) {
@@ -629,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;
@@ -750,44 +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(start_date, end_date, units=c("days"));
-    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);
@@ -796,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.
@@ -1533,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);
@@ -1574,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");
     }
 }
 
@@ -1727,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") {
@@ -1760,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();
             }
@@ -1768,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") {
@@ -1810,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();
             }
@@ -1849,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") {
@@ -1868,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();
             }
@@ -1876,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") {
@@ -1899,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	Fri Jun 01 08:00:53 2018 -0400
+++ b/insect_phenology_model.xml	Tue Aug 07 12:58:20 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	Fri Jun 01 08:00:53 2018 -0400
+++ b/test-data/output_combined1.csv	Tue Aug 07 12:58:20 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:58:20 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:58:20 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	Fri Jun 01 08:00:53 2018 -0400
+++ b/test-data/output_f1_3.csv	Tue Aug 07 12:58:20 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	Fri Jun 01 08:00:53 2018 -0400
+++ b/test-data/output_f1_4.csv	Tue Aug 07 12:58:20 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	Fri Jun 01 08:00:53 2018 -0400
+++ b/test-data/output_f2_3.csv	Tue Aug 07 12:58:20 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	Fri Jun 01 08:00:53 2018 -0400
+++ b/test-data/output_f2_4.csv	Tue Aug 07 12:58:20 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	Fri Jun 01 08:00:53 2018 -0400
+++ b/test-data/output_p_3.csv	Tue Aug 07 12:58:20 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	Fri Jun 01 08:00:53 2018 -0400
+++ b/test-data/output_p_4.csv	Tue Aug 07 12:58:20 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:58:20 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);
+}