changeset 123:e69e30d853fb draft

Uploaded
author greg
date Thu, 31 May 2018 14:08:46 -0400
parents 8946ddb9d72c
children 534658644efe
files insect_phenology_model.R
diffstat 1 files changed, 38 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/insect_phenology_model.R	Thu May 31 13:09:32 2018 -0400
+++ b/insect_phenology_model.R	Thu May 31 14:08:46 2018 -0400
@@ -269,10 +269,10 @@
     }
 }
 
-get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval) {
+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 restricted_date_interval is TRUE:
+    # 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);
@@ -330,7 +330,7 @@
             }
             tick_index = get_tick_index(i, last_tick, ticks, month_labels)
             if (!is.null(tick_index)) {
-                if (restricted_date_interval) {
+                if (date_interval) {
                     # Add a tick for every day.
                     ticks[tick_index] = i;
                     # Add a blank month label so it is not displayed.
@@ -412,9 +412,9 @@
     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;
+        date_interval = FALSE;
     } else {
-        restricted_date_interval = TRUE;
+        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");
@@ -432,7 +432,7 @@
         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) {
+        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) {
@@ -448,30 +448,39 @@
                 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]);
             }
             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];
             date_str = format(start_date);
+            # Extract the year from the start date.
+            date_str_items = strsplit(date_str, "-")[[1]];
+            # Get the year.
+            year = date_str_items[1];
+            # Properly set the start_date to be Jan 1 of the year.
+            start_date = paste(year, "01", "01", sep="-");
+            # Properly set the end_date to be Dec 31 of the year.
+            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]);
         }
-        # 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");
-        if (restricted_date_interval) {
+        if (date_interval) {
             # We're plotting a date interval.
             # Extract the year, month and day from the start date.
             start_date_str = format(start_date);
@@ -485,7 +494,7 @@
             end_date_str_items = strsplit(end_date_str, "-")[[1]];
             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="-");
+            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
@@ -508,7 +517,7 @@
     }
     if (processing_year_to_date_data) {
         # Merge the year-to-date data with the 30 year normals data.
-        if (restricted_date_interval) {
+        if (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
@@ -604,16 +613,15 @@
                 # 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) {
+                for (i in first_norm_row:total_days) {
                     temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);
                 }
             }
         }
     } else {
         # We're processing only the 30 year normals data.
-        if (restricted_date_interval) {
+        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);
         } else {
@@ -726,19 +734,7 @@
     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)) {
+if (is.null(opt$input_ytd)) {
     processing_year_to_date_data = FALSE;
 } else {
     processing_year_to_date_data = TRUE;
@@ -751,7 +747,7 @@
 }
 if (is.null(opt$start_date) && is.null(opt$end_date)) {
     # We're plotting an entire year.
-    restricted_date_interval = FALSE;
+    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");
@@ -768,7 +764,7 @@
     # Galaxy should soon include support for a date selector
     # at which point this tool should be enhanced to use it.
     # Validate start_date.
-    restricted_date_interval = TRUE;
+    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);
@@ -787,6 +783,17 @@
     # Display the total number of days in the Galaxy history item blurb.
     cat("Number of days in date interval: ", num_days, "\n");
 }
+# 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]];
 
 # Create copies of the temperature data for generations P, F1 and F2 if we're plotting generations separately.
 if (plot_generations_separately) {
@@ -796,7 +803,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, restricted_date_interval);
+ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, 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.