| Previous changeset 121:da67a24b04ba (2018-05-31) Next changeset 123:e69e30d853fb (2018-05-31) |
|
Commit message:
Uploaded |
|
modified:
insect_phenology_model.R |
| b |
| diff -r da67a24b04ba -r 8946ddb9d72c insect_phenology_model.R --- 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 |
| [ |
| b'@@ -33,10 +33,11 @@\n args <- parse_args(parser, positional_arguments=TRUE);\n opt <- args$options;\n \n-add_daylight_length = function(temperature_data_frame, num_rows) {\n- # Return a vector of daylight length (photoperido profile) for\n- # the number of days specified in the input_ytd temperature data\n- # (from Forsythe 1995).\n+add_daylight_length = function(temperature_data_frame) {\n+ # Return temperature_data_frame with an added column\n+ # of daylight length (photoperido profile).\n+ num_rows = dim(temperature_data_frame)[1];\n+ # From Forsythe 1995.\n p = 0.8333;\n latitude = temperature_data_frame$LATITUDE[1];\n daylight_length_vector = NULL;\n@@ -70,6 +71,19 @@\n return(date_interval_rows);\n }\n \n+from_30_year_normals = function(temperature_data_frame, norm_data_frame, start_date_doy, end_date_doy) {\n+ # The data we want is fully contained within the 30 year normals data.\n+ first_norm_row = which(norm_data_frame$DOY==start_date_doy);\n+ last_norm_row = which(norm_data_frame$DOY==end_date_doy);\n+ norm_data_frame_rows = last_norm_row - first_norm_row;\n+ j = 0;\n+ for (i in first_norm_row:last_norm_row) {\n+ j = j + 1;\n+ temperature_data_frame[j,] = get_next_normals_row(norm_data_frame, year, is_leap_year, i);\n+ }\n+ return (temperature_data_frame);\n+}\n+\n get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) {\n if (!is.null(life_stage_nymph)) {\n lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph);\n@@ -255,21 +269,37 @@\n }\n }\n \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+get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval) {\n+ # Generate a list of ticks and labels for plotting the\n+ # x axis. There are several scenarios that affect this.\n+ # 1. If restricted_date_interval is TRUE:\n+ # a.\n+ if (prepend_end_doy_norm > 0) {\n+ prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm);\n+ } else {\n+ prepend_end_norm_row = 0;\n+ }\n+ if (append_start_doy_norm > 0) {\n+ append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm);\n+ } else {\n+ append_start_norm_row = 0;\n+ }\n+ num_rows = dim(temperature_data_frame)[1];\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 (start_doy_ytd > 1 & i==start_doy_ytd-1) {\n+ # We\'re plotting the entire year, so ticks will\n+ # occur on Sundays and the first of each month.\n+ if (i == prepend_end_norm_row) {\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] = "End prepended 30 year normals";\n last_tick = i;\n- } else if (end_doy_ytd > 0 & i==end_doy_ytd+1) {\n+ } else if (i == append_start_doy_norm) {\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@@ -291,7 +321,7 @@\n month = items[2];\n month_label = month.abb[as.integer(month)];\n if (!identical(current_month_label, month_label)) {\n- # Add an x-axis tick for the month.\n+ # Add a tick for the month.\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@@ -300,14 +330,22 @@\n }\n tick_index = get_ti'..b'than using the number of rows in the input temperature\n-# data.\n if (is.null(opt$start_date) && is.null(opt$end_date)) {\n- # Use the default number of rows in the input temperature\n- # data as the number of days.\n- num_days = opt$num_days_ytd;\n+ # We\'re plotting an entire year.\n+ restricted_date_interval = FALSE;\n+ # Display the total number of days in the Galaxy history item blurb.\n+ if (processing_year_to_date_data) {\n+ cat("Number of days year-to-date: ", opt$num_days_ytd, "\\n");\n+ } else {\n+ if (is_leap_year) {\n+ num_days = 366;\n+ } else {\n+ num_days = 365;\n+ }\n+ cat("Number of days in year: ", num_days, "\\n");\n+ }\n } else {\n # FIXME: currently custom date fields are free text, but\n # Galaxy should soon include support for a date selector\n # at which point this tool should be enhanced to use it.\n # Validate start_date.\n- start_date = validate_date(opt$start_date);\n+ restricted_date_interval = TRUE;\n+ # Calaculate the number of days in the date interval rather\n+ # than using the number of rows in the input temperature data.\n+ start_date = validate_date(opt$start_date);\n # Validate end_date.\n end_date = validate_date(opt$end_date);\n if (start_date >= end_date) {\n- stop_err("The start date must be before the end date for custom date intervals.");\n+ stop_err("The start date must be between 1 and 50 days before the end date when setting date intervals for plots.");\n }\n- # Calculate the number of days in the custom date interval.\n+ # Calculate the number of days in the date interval.\n num_days = difftime(start_date, end_date, units=c("days"));\n if (num_days > 50) {\n- # We need to restrict custom date intervals since\n+ # We need to restrict date intervals since\n # plots render tick marks for each day.\n- stop_err("Custom date intervals cannot exceed 50 days.");\n+ stop_err("Date intervals for plotting cannot exceed 50 days.");\n }\n+ # Display the total number of days in the Galaxy history item blurb.\n+ cat("Number of days in date interval: ", num_days, "\\n");\n }\n-# Display the total number of days in the Galaxy history item blurb.\n-cat("Year-to-date number of days: ", num_days, "\\n");\n-# Parse the inputs.\n-data_list = parse_input_data(opt$input_ytd, opt$input_norm, num_days, opt$location, opt$start_date, opt$end_date);\n-temperature_data_frame = data_list[[1]];\n-# Information needed for plots, some of these values are\n-# being reset here since in some case they were set above.\n-start_date = data_list[[2]];\n-end_date = data_list[[3]];\n-start_doy_ytd = data_list[[4]];\n-end_doy_ytd = data_list[[5]];\n-is_leap_year = data_list[[6]];\n-total_days = data_list[[7]];\n-total_days_vector = c(1:total_days);\n-location = data_list[[8]];\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@@ -626,7 +796,7 @@\n }\n \n # Get the ticks date labels for plots.\n-ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, total_days, start_doy_ytd, end_doy_ytd);\n+ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, prepend_end_doy_norm, append_start_doy_norm, restricted_date_interval);\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@@ -692,6 +862,7 @@\n }\n }\n # Initialize matrices.\n+total_days = dim(temperature_data_frame)[1];\n if (process_eggs) {\n Eggs.replications = matrix(rep(0, total_days*opt$replications), ncol=opt$replications);\n }\n@@ -1536,6 +1707,7 @@\n write.csv(temperature_data_frame_F2, file=file_path, row.names=F);\n }\n \n+total_days_vector = c(1:dim(temperature_data_frame)[1]);\n if (plot_generations_separately) {\n for (life_stage in life_stages) {\n if (life_stage == "Egg") {\n' |