Repository 'insect_phenology_model'
hg clone https://toolshed.g2.bx.psu.edu/repos/greg/insect_phenology_model

Changeset 39:169c8180205a (2018-04-11)
Previous changeset 38:c0f76f4f84fc (2018-04-10) Next changeset 40:d8e6304dc5e4 (2018-04-11)
Commit message:
Uploaded
modified:
insect_phenology_model.R
b
diff -r c0f76f4f84fc -r 169c8180205a insect_phenology_model.R
--- a/insect_phenology_model.R Tue Apr 10 14:22:45 2018 -0400
+++ b/insect_phenology_model.R Wed Apr 11 11:26:53 2018 -0400
[
b'@@ -123,6 +123,26 @@\n     return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))\n }\n \n+get_next_normals_row = function(norm_data_frame, year, is_leap_year, index) {\n+    # Return the next 30 year normals row formatted\n+    # appropriately for the year-to-date data.\n+    latitude = norm_data_frame[index,"LATITUDE"][1];\n+    longitude = norm_data_frame[index,"LONGITUDE"][1];\n+    # Format the date.\n+    mmdd = norm_data_frame[index,"MMDD"][1];\n+    date_str = paste(year, mmdd, sep="-");\n+    doy = norm_data_frame[index,"DOY"][1];\n+    if (!is_leap_year) {\n+        # Since all normals data includes Feb 29, we have to\n+        # subtract 1 from DOY if we\'re not in a leap year since\n+        # we removed the Feb 29 row from the data frame above.\n+        doy = as.integer(doy) - 1;\n+    }\n+    tmin = norm_data_frame[index,"TMIN"][1];\n+    tmax = norm_data_frame[index,"TMAX"][1];\n+    return(list(latitude, longitude, date_str, doy, tmin, tmax));\n+}\n+\n get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) {\n     # Base development threshold for Brown Marmorated Stink Bug\n     # insect phenology model.\n@@ -222,59 +242,67 @@\n get_total_days = function(is_leap_year) {\n     # Get the total number of days in the current year.\n     if (is_leap_year) {\n-        return (366);\n+        return(366);\n     } else {\n-        return (365);\n+        return(365);\n     }\n }\n \n-get_x_axis_ticks_and_labels = function(temperature_data_frame, num_rows, num_days_ytd) {\n+get_x_axis_ticks_and_labels = function(temperature_data_frame, num_rows, start_doy_ytd, end_doy_ytd) {\n     # Keep track of the years to see if spanning years.\n     month_labels = list();\n     ticks = list();\n     current_month_label = NULL;\n     last_tick = 0;\n     for (i in 1:num_rows) {\n-        if (i==num_days_ytd) {\n-            # Add a tick for the start of the 30 year normnals data.\n+        if (start_doy_ytd > 1 & i==start_doy_ytd-1) {\n+            # Add a tick for the end of the 30 year normnals data\n+            # that was prepended to the year-to-date data.\n             tick_index = get_tick_index(i, last_tick, ticks, month_labels)\n             ticks[tick_index] = i;\n-            month_labels[tick_index] = "Start 30 year normals";\n+            month_labels[tick_index] = "End prepended 30 year normals";\n             last_tick = i;\n-        }\n-        # Get the year and month from the date which\n-        # has the format YYYY-MM-DD.\n-        date = format(temperature_data_frame$DATE[i]);\n-        # Get the month label.\n-        items = strsplit(date, "-")[[1]];\n-        month = items[2];\n-        month_label = month.abb[as.integer(month)];\n-        tick_index = get_tick_index(i, last_tick, ticks, month_labels)\n-        if (!identical(current_month_label, month_label)) {\n-            # Add an x-axis tick for the month.\n+        } else  if (i==end_doy_ytd+1) {\n+            # Add a tick for the start of the 30 year normnals data\n+            # that was appended to the year-to-date data.\n+            tick_index = get_tick_index(i, last_tick, ticks, month_labels)\n             ticks[tick_index] = i;\n-            month_labels[tick_index] = month_label;\n-            current_month_label = month_label;\n+            month_labels[tick_index] = "Start appended 30 year normals";\n             last_tick = i;\n-        }\n-        tick_index = get_tick_index(i, last_tick, ticks, month_labels)\n-        if (!is.null(tick_index)) {\n-            # Get the day.\n-            day = weekdays(as.Date(date));\n-            if (day=="Sunday") {\n-                # Add an x-axis tick if we\'re on a Sunday.\n-                ticks[tick_index] = i;\n-                # Add a blank month label so it is not displayed.\n-                month_labels[tick_index] = "";\n-                last_tick = i;\n-            }\n-        }\n-        if (i==num_rows) {\n+        } else if (i==num_rows) {\n             # Add a tick for the last day of the year.\n             tick_index = get_tick_index(i, last_tick, tic'..b'start_doy_ytd > 1) {\n+        # The year-to-date data starts after Jan 1, so create a\n+        # temporary data frame to contain the 30 year normals data\n+        # from Jan 1 to the date immediately prior to start_date.\n+        tmp_data_frame = temperature_data_frame[FALSE,];\n+        for (i in 1:start_doy_ytd-1) {\n+            tmp_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);\n         }\n-        tmin = norm_data_frame[i,"TMIN"][1];\n-        tmax = norm_data_frame[i,"TMAX"][1];\n-        # Append the row to temperature_data_frame.\n-        new_row = list(latitude, longitude, date_str, doy, tmin, tmax);\n-        temperature_data_frame[i,] = new_row;\n+        # Next merge the temporary data frame with the year-to-date data frame.\n+        temperature_data_frame = rbind(tmp_data_frame, temperature_data_frame);\n+    }\n+    # Define the next row for the year-to-date data from the 30 year normals data.\n+    first_normals_append_row = end_doy_ytd + 1;\n+    # Append the 30 year normals data to the year-to-date data.\n+    for (i in first_normals_append_row:total_days) {\n+        temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);\n     }\n     # Add a column containing the daylight length for each day.\n     temperature_data_frame = add_daylight_length(temperature_data_frame, total_days);\n-    return(temperature_data_frame);\n+    return(list(temperature_data_frame, start_date, start_doy_ytd, end_doy_ytd, is_leap_year, total_days));\n }\n \n render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval,\n-                        replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,\n-                        life_stages_adult=NULL, life_stages_nymph=NULL) {\n+    replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL,\n+    life_stages_adult=NULL, life_stages_nymph=NULL) {\n     if (chart_type=="pop_size_by_life_stage") {\n         if (life_stage=="Total") {\n             title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");\n@@ -472,8 +499,17 @@\n # Display the total number of days in the Galaxy history item blurb.\n cat("Year-to-date number of days: ", opt$num_days_ytd, "\\n");\n \n-# Read the temperature data into a data frame.\n-temperature_data_frame = parse_input_data(opt$input_ytd, opt$input_norm, opt$num_days_ytd);\n+# Parse the inputs.\n+data_list = parse_input_data(opt$input_ytd, opt$input_norm, opt$num_days_ytd);\n+temperature_data_frame = data_list[[1]];\n+# Information needed for plots.\n+start_date = data_list[[2]];\n+end_date = temperature_data_frame$DATE[opt$num_days_ytd];\n+start_doy_ytd = data_list[[3]];\n+end_doy_ytd = data_list[[4]];\n+is_leap_year = data_list[[5]];\n+total_days = data_list[[6]];\n+total_days_vector = c(1:total_days);\n \n # Create copies of the temperature data for generations P, F1 and F2 if we\'re plotting generations separately.\n if (plot_generations_separately) {\n@@ -482,16 +518,8 @@\n     temperature_data_frame_F2 = data.frame(temperature_data_frame);\n }\n \n-# Information needed for plots.\n-start_date = temperature_data_frame$DATE[1];\n-end_date = temperature_data_frame$DATE[opt$num_days_ytd];\n-# See if we\'re in a leap year.\n-is_leap_year = is_leap_year(start_date);\n-total_days = get_total_days(is_leap_year);\n-total_days_vector = c(1:total_days);\n-\n # Get the ticks date labels for plots.\n-ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, total_days, opt$num_days_ytd);\n+ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, total_days, start_doy_ytd, end_doy_ytd);\n ticks = c(unlist(ticks_and_labels[1]));\n date_labels = c(unlist(ticks_and_labels[2]));\n # All latitude values are the same, so get the value for plots from the first row.\n'