annotate extract_ipm_date_interval.R @ 7:869e7d06bdf3 draft

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