annotate extract_ipm_date_interval.R @ 9:fd41f452c9fe draft

Uploaded
author greg
date Wed, 01 Aug 2018 09:18:55 -0400
parents cf6a06a2346c
children c2eb16ef23c0
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 return(temperature_data_frame);
4dccc60b3525 Uploaded
greg
parents:
diff changeset
20 }
4dccc60b3525 Uploaded
greg
parents:
diff changeset
21
4dccc60b3525 Uploaded
greg
parents:
diff changeset
22 # Import the shared utility functions.
4dccc60b3525 Uploaded
greg
parents:
diff changeset
23 utils_path <- paste(opt$script_dir, "utils.R", sep="/");
4dccc60b3525 Uploaded
greg
parents:
diff changeset
24 source(utils_path);
4dccc60b3525 Uploaded
greg
parents:
diff changeset
25
4dccc60b3525 Uploaded
greg
parents:
diff changeset
26 # FIXME: currently custom date fields are free text, but
4dccc60b3525 Uploaded
greg
parents:
diff changeset
27 # Galaxy should soon include support for a date selector
4dccc60b3525 Uploaded
greg
parents:
diff changeset
28 # at which point this tool should be enhanced to use it.
4dccc60b3525 Uploaded
greg
parents:
diff changeset
29 # Validate start_date.
5
87482a201699 Uploaded
greg
parents: 4
diff changeset
30 start_date = format(opt$start_date);
87482a201699 Uploaded
greg
parents: 4
diff changeset
31 end_date = format(opt$end_date);
0
4dccc60b3525 Uploaded
greg
parents:
diff changeset
32
4dccc60b3525 Uploaded
greg
parents:
diff changeset
33 # Calaculate the number of days in the date interval.
5
87482a201699 Uploaded
greg
parents: 4
diff changeset
34 start_date = validate_date(start_date);
0
4dccc60b3525 Uploaded
greg
parents:
diff changeset
35 # Validate end_date.
5
87482a201699 Uploaded
greg
parents: 4
diff changeset
36 end_date = validate_date(end_date);
0
4dccc60b3525 Uploaded
greg
parents:
diff changeset
37 if (start_date >= end_date) {
4dccc60b3525 Uploaded
greg
parents:
diff changeset
38 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
39 }
4dccc60b3525 Uploaded
greg
parents:
diff changeset
40 # Calculate the number of days in the date interval.
4dccc60b3525 Uploaded
greg
parents:
diff changeset
41 num_days = difftime(end_date, start_date, units=c("days"));
4dccc60b3525 Uploaded
greg
parents:
diff changeset
42 # Add 1 to the number of days to make the dates inclusive. For
4dccc60b3525 Uploaded
greg
parents:
diff changeset
43 # example, if the user enters a date range of 2018-01-01 to
4dccc60b3525 Uploaded
greg
parents:
diff changeset
44 # 2018-01-31, they likely expect the end date to be included.
4dccc60b3525 Uploaded
greg
parents:
diff changeset
45 num_days = num_days + 1;
4dccc60b3525 Uploaded
greg
parents:
diff changeset
46 if (num_days > 50) {
4dccc60b3525 Uploaded
greg
parents:
diff changeset
47 # We need to restrict date intervals since
4dccc60b3525 Uploaded
greg
parents:
diff changeset
48 # plots render tick marks for each day.
4dccc60b3525 Uploaded
greg
parents:
diff changeset
49 stop_err("Date intervals for plotting cannot exceed 50 days.");
4dccc60b3525 Uploaded
greg
parents:
diff changeset
50 }
4dccc60b3525 Uploaded
greg
parents:
diff changeset
51 # Display the total number of days in the Galaxy history item blurb.
4dccc60b3525 Uploaded
greg
parents:
diff changeset
52 cat("Number of days in date interval: ", num_days, "\n");
4dccc60b3525 Uploaded
greg
parents:
diff changeset
53
9
fd41f452c9fe Uploaded
greg
parents: 8
diff changeset
54 input_files = list.files(path=opt$input_dir, full.names=TRUE);
0
4dccc60b3525 Uploaded
greg
parents:
diff changeset
55 for(input_file in input_files) {
4dccc60b3525 Uploaded
greg
parents:
diff changeset
56 temperature_data_frame = get_new_temperature_data_frame(input_file);
4dccc60b3525 Uploaded
greg
parents:
diff changeset
57 start_date_row = which(temperature_data_frame$DATE==start_date);
4dccc60b3525 Uploaded
greg
parents:
diff changeset
58 end_date_row = which(temperature_data_frame$DATE==end_date);
4dccc60b3525 Uploaded
greg
parents:
diff changeset
59 # Extract the date interval.
4dccc60b3525 Uploaded
greg
parents:
diff changeset
60 temperature_data_frame = temperature_data_frame[start_date_row:end_date_row,];
7
869e7d06bdf3 Uploaded
greg
parents: 5
diff changeset
61 # Get the ticks date labels for plots.
869e7d06bdf3 Uploaded
greg
parents: 5
diff changeset
62 ticks_and_labels = get_x_axis_ticks_and_labels(temperature_data_frame, date_interval=TRUE);
869e7d06bdf3 Uploaded
greg
parents: 5
diff changeset
63 ticks = c(unlist(ticks_and_labels[1]));
869e7d06bdf3 Uploaded
greg
parents: 5
diff changeset
64 date_labels = c(unlist(ticks_and_labels[2]));
869e7d06bdf3 Uploaded
greg
parents: 5
diff changeset
65 # All latitude values are the same, so get the value
869e7d06bdf3 Uploaded
greg
parents: 5
diff changeset
66 # for plots from the first row.
8
cf6a06a2346c Uploaded
greg
parents: 7
diff changeset
67 latitude = temperature_data_frame$LATITUDE[1];
0
4dccc60b3525 Uploaded
greg
parents:
diff changeset
68 # Save the date interval data into an output file
4dccc60b3525 Uploaded
greg
parents:
diff changeset
69 # named the same as the input.
8
cf6a06a2346c Uploaded
greg
parents: 7
diff changeset
70 file_path = paste("output_data_dir", basename(input_file), sep="/");
0
4dccc60b3525 Uploaded
greg
parents:
diff changeset
71 write.csv(temperature_data_frame, file=file_path, row.names=F);
8
cf6a06a2346c Uploaded
greg
parents: 7
diff changeset
72 # TODO: Save the date interval plots...
0
4dccc60b3525 Uploaded
greg
parents:
diff changeset
73 }