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