changeset 122:8946ddb9d72c draft

Uploaded
author greg
date Thu, 31 May 2018 13:09:32 -0400
parents da67a24b04ba
children e69e30d853fb
files insect_phenology_model.R
diffstat 1 files changed, 289 insertions(+), 117 deletions(-) [+]
line wrap: on
line diff
--- a/insect_phenology_model.R	Thu May 31 13:09:22 2018 -0400
+++ b/insect_phenology_model.R	Thu May 31 13:09:32 2018 -0400
@@ -33,10 +33,11 @@
 args <- parse_args(parser, positional_arguments=TRUE);
 opt <- args$options;
 
-add_daylight_length = function(temperature_data_frame, num_rows) {
-    # Return a vector of daylight length (photoperido profile) for
-    # the number of days specified in the input_ytd temperature data
-    # (from Forsythe 1995).
+add_daylight_length = function(temperature_data_frame) {
+    # Return temperature_data_frame with an added column
+    # of daylight length (photoperido profile).
+    num_rows = dim(temperature_data_frame)[1];
+    # From Forsythe 1995.
     p = 0.8333;
     latitude = temperature_data_frame$LATITUDE[1];
     daylight_length_vector = NULL;
@@ -70,6 +71,19 @@
     return(date_interval_rows);
 }
 
+from_30_year_normals = function(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy) {
+    # 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;
+    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, is_leap_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);
@@ -255,21 +269,37 @@
     }
 }
 
-get_x_axis_ticks_and_labels = function(temperature_data_frame, num_rows, start_doy_ytd, end_doy_ytd) {
-    # Keep track of the years to see if spanning years.
+get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval) {
+    # Generate a list of ticks and labels for plotting the
+    # x axis.  There are several scenarios that affect this.
+    # 1. If restricted_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) {
-        if (start_doy_ytd > 1 & i==start_doy_ytd-1) {
+        # 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 (end_doy_ytd > 0 & i==end_doy_ytd+1) {
+        } 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)
@@ -291,7 +321,7 @@
             month = items[2];
             month_label = month.abb[as.integer(month)];
             if (!identical(current_month_label, month_label)) {
-                # Add an x-axis tick for the month.
+                # 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;
@@ -300,14 +330,22 @@
             }
             tick_index = get_tick_index(i, last_tick, ticks, month_labels)
             if (!is.null(tick_index)) {
-                # Get the day.
-                day = weekdays(as.Date(date));
-                if (day=="Sunday") {
-                    # Add an x-axis tick if we're on a Sunday.
+                if (restricted_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;
+                    }
                 }
             }
         }
@@ -360,23 +398,85 @@
     return(mortality.probability);
 }
 
-parse_input_data = function(input_ytd, input_norm, num_days, location, start_date, end_date) {
+parse_input_data = function(input_ytd, input_norm, location, start_date, end_date) {
+    # The end DOY for norm data prepended to ytd data.
+    prepend_end_doy_norm = 0;
+    # The start DOY for norm data appended to ytd data.
+    append_start_doy_norm = 0;
     if (is.null(input_ytd)) {
-        # We're analysing only the 30 year normals data, so create an empty
+        # We're processing only the 30 year normals data.
+        processing_year_to_date_data = FALSE;
+    } else {
+        processing_year_to_date_data = TRUE;
+    }
+    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.
+        restricted_date_interval = FALSE;
+    } else {
+        restricted_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");
+    }
+    # 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");
+    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 (restricted_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) {
+                # The start date is contained within the input_ytd data.
+                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);
+            }
+            end_date_ytd_row = which(temperature_data_frame$DATE==end_date);
+            if (end_date_ytd_row > 0) {
+                # The end date is contained within the input_ytd data.
+                end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]);
+            }
+            date_str = start_date;
+        } 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;
+            start_date = temperature_data_frame$DATE[1];
+            end_date_ytd_row = num_rows;
+            end_date = temperature_data_frame$DATE[num_rows];
+            date_str = format(start_date);
+            # 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]);
+        }
+        # Extract the year from the start date.
+        date_str_items = strsplit(date_str, "-")[[1]];
+        year = date_str_items[1];
+    } 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");
-        # Base all dates on the current date since 30 year
-        # normals data does not include any dates.
-        year = format(Sys.Date(), "%Y");
-        if (is.null(start_date) && is.null(end_date)) {
-            start_date = paste(year, "01", "01", sep="-");
-            end_date = paste(year, "12", "31", sep="-");
-        } else {
-            # Extract the month and day from the start date.
+        if (restricted_date_interval) {
+            # We're plotting a date interval.
+            # Extract the year, month and day from the start date.
             start_date_str = format(start_date);
             start_date_str_items = strsplit(start_date_str, "-")[[1]];
+            year = start_date_str_items[1];
             start_date_month = start_date_str_items[2];
             start_date_day = start_date_str_items[3];
             start_date = paste(year, start_date_month, start_date_day, sep="-");
@@ -386,87 +486,146 @@
             end_date_month = end_date_str_items[2];
             end_date_day = end_date_str_items[3];
             end_date = paste(year, start_date_month, start_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="-");
         }
-        # Set invalid start and end DOY.
-        start_doy_ytd = 0;
-        end_doy_ytd = 0;
-    } else {
-        # 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 (is.null(start_date) && is.null(end_date)) {
-            # Get the start date.
-            start_date = temperature_data_frame$DATE[1];
-            end_date = temperature_data_frame$DATE[num_days];
-        } else {
-            # Extract the custom date interval from temperature_data_frame.
-            temperature_data_frame = extract_date_interval_rows(temperature_data_frame, start_date, end_date);
-        }
-        # Extract the year from the start date.
-        date_str = format(start_date);
-        date_str_items = strsplit(date_str, "-")[[1]];
-        year = date_str_items[1];
-        # 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_days]);
     }
     # 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");
     # 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),];
     }
-    if (is.null(start_date) && is.null(end_date)) {
-        # Get the number of days in the year.
-        total_days = get_total_days(is_leap_year);
-    } else {
-        # Extract the custom date interval from norm_data_frame.
-        norm_data_frame = extract_date_interval_rows(norm_data_frame, start_date, end_date);
-        # Use the pre-determined num_days for total_days.
-        total_days = num_days
-    }
-    # Set the location to be the station name if the user elected no to enter it.
-    if (is.null(location) | length(location)==0) {
+    # 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];
     }
-    if (is.null(input_ytd)) {
-        # Convert the 30 year normals data to the year-to-date format.
-        for (i in 1:total_days) {
-            temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
+    if (processing_year_to_date_data) {
+        # Merge the year-to-date data with the 30 year normals data.
+        if (restricted_date_interval) {
+            # The values of start_date_ytd_row and end_date_ytd_row were set above.
+            if (start_date_ytd_row > 0 & end_date_ytd_row > 0) {
+                # The date interval is contained within the input_ytd
+                # data, so we don't need to merge the 30 year normals data.
+                temperature_data_frame = temperature_data_frame[start_date_ytd_row:end_date_ytd_row,];
+            } else if (start_date_ytd_row == 0 & end_date_ytd_row > 0) {
+                # The date interval starts in input_norm and ends in
+                # input_ytd, so prepend appropriate rows from input_norm
+                # to appropriate rows from input_ytd.
+                first_norm_row = which(norm_data_frame$DOY==start_date_doy);
+                # Get the first DOY from temperature_data_frame.
+                first_ytd_doy = temperature_data_frame$DOY[1];
+                # End DOY of input_norm data prepended to input_ytd.
+                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;
+                # 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;
+                # 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));
+                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, is_leap_year, i);
+                }
+                # 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,];
+                # 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
+                # Get the DOY of the last row in the input_ytd data.
+                last_ytd_doy = temperature_data_frame$DOY[num_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;
+                # Get the row from norm_data_frame containing first_norm_doy.
+                first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm);
+                # Get the row from norm_data_frame containing end_date_doy.
+                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;
+                # Create a temporary data frame to contain the data
+                # taken from both temperateu_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));
+                # 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,];
+                # 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, is_leap_year, i);
+                }
+                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);
+            }
+        } else {
+            # We're plotting an entire year.
+            if (start_doy_ytd > 1) {
+                # The input_ytd data starts after Jan 1, so prepend
+                # appropriate rows from input_norm to temperature_data_frame.
+                prepend_end_doy_norm = start_doy_ytd - 1;
+                first_norm_row = 1;
+                last_norm_row = which(norm_data_frame$DOY == prepend_end_doy_norm);
+                # Create a temporary data frame to contain the input_norm data
+                # from Jan 1 to the date immediately prior to start_date.
+                tmp_data_frame = temperature_data_frame[FALSE,];
+                # Populate tmp_data_frame with appropriate rows from norm_data_frame.
+                for (i in first_norm_row:last_norm_row) {
+                    tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
+                }
+                # Merge the temporary data frame with temperature_data_frame.
+                temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame);
+            }
+            # Set the value of total_days.
+            total_days = get_total_days(is_leap_year);
+            if (end_doy_ytd < total_days) {
+                # Define the next row for the year-to-date data from the 30 year normals data.
+                append_start_doy_norm = end_doy_ytd + 1;
+                first_norm_row = which(norm_data_frame$DOY == append_start_doy_norm);
+                last_norm_row = which(norm_data_frame$DOY == total_days);
+                # Append the 30 year normals data to the year-to-date data.
+                for (i in first_norm_row:last_norm_row) {
+                    temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
+                }
+            }
         }
     } else {
-        # Merge the year-to-date data with the 30 year normals data.
-        if (start_doy_ytd > 1) {
-            # The year-to-date data starts after Jan 1, so create a
-            # temporary data frame to contain the 30 year normals data
-            # from Jan 1 to the date immediately prior to start_date.
-            tmp_data_frame = temperature_data_frame[FALSE,];
-            for (i in 1:start_doy_ytd-1) {
-                tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
+        # We're processing only the 30 year normals data.
+        if (restricted_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);
+        } else {
+            total_days = get_total_days(is_leap_year);
+            for (i in 1:total_days) {
+                temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
             }
-            # Next merge the temporary data frame with the year-to-date data frame.
-            temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame);
-        }
-        # Define the next row for the year-to-date data from the 30 year normals data.
-        first_normals_append_row = end_doy_ytd + 1;
-        # Append the 30 year normals data to the year-to-date data.
-        for (i in first_normals_append_row:total_days) {
-            temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
         }
     }
     # Add a column containing the daylight length for each day.
-    temperature_data_frame = add_daylight_length(temperature_data_frame, total_days);
-    return(list(temperature_data_frame, start_date, end_date, start_doy_ytd, end_doy_ytd, is_leap_year, total_days, location));
+    temperature_data_frame = add_daylight_length(temperature_data_frame);
+    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,
@@ -567,56 +726,67 @@
     return(valid_date);
 }
 
+# Parse the inputs.
+data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$location, opt$start_date, opt$end_date);
+temperature_data_frame = data_list[[1]];
+# Information needed for plots, some of these values are
+# being reset here since in some case they were set above.
+start_date = data_list[[2]];
+end_date = data_list[[3]];
+prepend_end_doy_norm = data_list[[4]];
+append_start_doy_norm = data_list[[5]];
+is_leap_year = data_list[[6]];
+location = data_list[[7]];
+
+if (is.null(input_ytd)) {
+    processing_year_to_date_data = FALSE;
+} else {
+    processing_year_to_date_data = TRUE;
+}
 # Determine if we're plotting generations separately.
 if (opt$plot_generations_separately=="yes") {
     plot_generations_separately = TRUE;
 } else {
     plot_generations_separately = FALSE;
 }
-# If opt$start_date and opt$end_date have values, then the
-# user chose to plot a custom date interval rather than the
-# entire contents of the input temperature data, so we'll
-# calaculate the number of days in the custom date interval
-# rather than using the number of rows in the input temperature
-# data.
 if (is.null(opt$start_date) && is.null(opt$end_date)) {
-    # Use the default number of rows in the input temperature
-    # data as the number of days.
-    num_days = opt$num_days_ytd;
+    # We're plotting an entire year.
+    restricted_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");
+    }
 } 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.
-    start_date =  validate_date(opt$start_date);
+    restricted_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 before the end date for custom date intervals.");
+        stop_err("The start date must be between 1 and 50 days before the end date when setting date intervals for plots.");
     }
-    # Calculate the number of days in the custom date interval.
+    # 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 custom date intervals since
+        # We need to restrict date intervals since
         # plots render tick marks for each day.
-        stop_err("Custom date intervals cannot exceed 50 days.");
+        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");
 }
-# Display the total number of days in the Galaxy history item blurb.
-cat("Year-to-date number of days: ", num_days, "\n");
-# Parse the inputs.
-data_list = parse_input_data(opt$input_ytd, opt$input_norm, num_days, opt$location, opt$start_date, opt$end_date);
-temperature_data_frame = data_list[[1]];
-# Information needed for plots, some of these values are
-# being reset here since in some case they were set above.
-start_date = data_list[[2]];
-end_date = data_list[[3]];
-start_doy_ytd = data_list[[4]];
-end_doy_ytd = data_list[[5]];
-is_leap_year = data_list[[6]];
-total_days = data_list[[7]];
-total_days_vector = c(1:total_days);
-location =  data_list[[8]];
 
 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately.
 if (plot_generations_separately) {
@@ -626,7 +796,7 @@
 }
 
 # Get the ticks date labels for plots.
-ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, total_days, start_doy_ytd, end_doy_ytd);
+ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval);
 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.
@@ -692,6 +862,7 @@
     }
 }
 # Initialize matrices.
+total_days = dim(temperature_data_frame)[1];
 if (process_eggs) {
     Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);
 }
@@ -1536,6 +1707,7 @@
     write.csv(temperature_data_frame_F2, file=file_path, row.names=F);
 }
 
+total_days_vector = c(1:dim(temperature_data_frame)[1]);
 if (plot_generations_separately) {
     for (life_stage in life_stages) {
         if (life_stage == "Egg") {