| 0 | 1 #!/usr/bin/env Rscript | 
|  | 2 | 
|  | 3 suppressPackageStartupMessages(library("data.table")) | 
|  | 4 suppressPackageStartupMessages(library("optparse")) | 
|  | 5 | 
|  | 6 option_list <- list( | 
| 3 | 7             make_option(c("--input_dir"), action="store", dest="input_dir", help="Directory containing .csv outputs from insect_phenology_model"), | 
|  | 8             make_option(c("--end_date"), action="store", dest="end_date", help="End date for date interval"), | 
|  | 9             make_option(c("--start_date"), action="store", dest="start_date", help="Start date for date interval"), | 
|  | 10             make_option(c("--script_dir"), action="store", dest="script_dir", help="R script source directory") | 
| 0 | 11 ) | 
|  | 12 | 
|  | 13 parser <- OptionParser(usage="%prog [options] file", option_list=option_list); | 
|  | 14 args <- parse_args(parser, positional_arguments=TRUE); | 
|  | 15 opt <- args$options; | 
|  | 16 | 
|  | 17 get_new_temperature_data_frame = function(input_file) { | 
|  | 18     temperature_data_frame = read.csv(file=input_file, header=T, strip.white=TRUE, stringsAsFactors=FALSE, sep=","); | 
|  | 19     colnames(temperature_data_frame) = c("LATITUDE", "LONGITUDE", "DATE", "DOY", "TMIN", "TMAX"); | 
|  | 20     return(temperature_data_frame); | 
|  | 21 } | 
|  | 22 | 
|  | 23 # Import the shared utility functions. | 
|  | 24 utils_path <- paste(opt$script_dir, "utils.R", sep="/"); | 
|  | 25 source(utils_path); | 
|  | 26 | 
|  | 27 # FIXME: currently custom date fields are free text, but | 
|  | 28 # Galaxy should soon include support for a date selector | 
|  | 29 # at which point this tool should be enhanced to use it. | 
|  | 30 # Validate start_date. | 
| 5 | 31 start_date = format(opt$start_date); | 
|  | 32 end_date = format(opt$end_date); | 
| 0 | 33 | 
|  | 34 # Calaculate the number of days in the date interval. | 
| 5 | 35 start_date = validate_date(start_date); | 
| 0 | 36 # Validate end_date. | 
| 5 | 37 end_date = validate_date(end_date); | 
| 0 | 38 if (start_date >= end_date) { | 
|  | 39     stop_err("The start date must be between 1 and 50 days before the end date when setting date intervals for plots."); | 
|  | 40 } | 
|  | 41 # Calculate the number of days in the date interval. | 
|  | 42 num_days = difftime(end_date, start_date, units=c("days")); | 
|  | 43 # Add 1 to the number of days to make the dates inclusive.  For | 
|  | 44 # example, if the user enters a date range of 2018-01-01 to | 
|  | 45 # 2018-01-31, they likely expect the end date to be included. | 
|  | 46 num_days = num_days + 1; | 
|  | 47 if (num_days > 50) { | 
|  | 48     # We need to restrict date intervals since | 
|  | 49     # plots render tick marks for each day. | 
|  | 50     stop_err("Date intervals for plotting cannot exceed 50 days."); | 
|  | 51 } | 
|  | 52 # Display the total number of days in the Galaxy history item blurb. | 
|  | 53 cat("Number of days in date interval: ", num_days, "\n"); | 
|  | 54 | 
|  | 55 input_files = list.files(path=input_dir, full.names=TRUE); | 
|  | 56 for(input_file in input_files) { | 
|  | 57     temperature_data_frame = get_new_temperature_data_frame(input_file); | 
|  | 58     start_date_row = which(temperature_data_frame$DATE==start_date); | 
|  | 59     end_date_row = which(temperature_data_frame$DATE==end_date); | 
|  | 60     # Extract the date interval. | 
|  | 61     temperature_data_frame = temperature_data_frame[start_date_row:end_date_row,]; | 
| 7 | 62     # Get the ticks date labels for plots. | 
|  | 63         ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, date_interval=TRUE); | 
|  | 64         ticks = c(unlist(ticks_and_labels[1])); | 
|  | 65         date_labels = c(unlist(ticks_and_labels[2])); | 
|  | 66     # All latitude values are the same, so get the value | 
|  | 67     # for plots from the first row. | 
|  | 68         latitude = temperature_data_frame$LATITUDE[1]; | 
| 0 | 69     # Save the date interval data into an output file | 
|  | 70     # named the same as the input. | 
|  | 71     file_path = paste("output_data_dir", input_file, sep="/"); | 
|  | 72     write.csv(temperature_data_frame, file=file_path, row.names=F); | 
| 5 | 73     # TODO: Save the datE interval plots... | 
| 0 | 74 } |