-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement n_hours_per_met_file (#102)
* num hours per met file * only call find_met_files twice if met_subgrid is enabled * implement met_hours using n_hours_per_met_file * call unique on request and output * add n_hours_per_met_file param to stilt_cli * n_hours_per_met_file guidance --------- Co-authored-by: John-C-Lin <[email protected]>
- Loading branch information
1 parent
5b31c43
commit 0b81580
Showing
5 changed files
with
57 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,61 @@ | ||
#' find_met_files searches for meteorological data files | ||
#' @author Ben Fasoli | ||
#' @author Ben Fasoli & James Mineau | ||
#' | ||
#' Searches for available meteorological files matching the given strftime | ||
#' compatible file naming convention | ||
#' | ||
#' @param t_start time of simulation start | ||
#' @param n_hours number of hours to run each simulation; negative indicates | ||
#' backward in time | ||
#' @param n_hours_per_met_file number of hours of meteorological data in each | ||
#' met file | ||
#' @param met_file_format grep compatible file naming convention to identify | ||
#' meteorological data files necessary for the timing of the simulation | ||
#' indicated by \code{t_start} and \code{n_hours} | ||
#' @param n_hours number of hours to run each simulation; negative indicates | ||
#' backward in time | ||
#' @param met_path directory to find meteorological data | ||
#' | ||
#' @import dplyr | ||
#' @export | ||
|
||
find_met_files <- function(t_start, met_file_format, n_hours, met_path) { | ||
find_met_files <- function(t_start, n_hours, n_hours_per_met_file, | ||
met_file_format, met_path) { | ||
require(dplyr) | ||
|
||
|
||
ts <- as.POSIXct(t_start, tz = 'UTC') | ||
is_backward <- n_hours < 0 | ||
|
||
# TODO: implement n_hours_per_met_file to better determine file names at | ||
# varying time resolutions | ||
request <- as.POSIXct(t_start, tz='UTC') %>% | ||
c(. + c(1, -1, n_hours, is_backward * (n_hours - 5)) * 3600) %>% | ||
range() %>% | ||
(function(x) seq(x[1], x[2], by = 'hour')) %>% | ||
strftime(tz = 'UTC', format = met_file_format) | ||
|
||
met_bracket <- n_hours_per_met_file - 1 # ts can be in the middle of a met file | ||
|
||
# Generate the hours to search for | ||
if (is_backward) { | ||
met_hours <- seq( | ||
ts - as.difftime(abs(n_hours) + met_bracket, units = 'hours'), | ||
ts, | ||
by = 3600 | ||
) | ||
} else { | ||
met_hours <- seq( | ||
ts - as.difftime(met_bracket, units = 'hours'), | ||
ts + as.difftime(n_hours, units = 'hours'), | ||
by = 3600 | ||
) | ||
} | ||
|
||
# Format the request and remove duplicates | ||
request <- met_hours %>% | ||
strftime(tz = 'UTC', format = met_file_format) %>% | ||
unique() | ||
|
||
# Find the available files | ||
available <- dir(met_path, full.names = T, recursive = T) | ||
available <- available[!grepl('.lock', available)] | ||
|
||
|
||
# Find the files that match the request | ||
idx <- do.call(c, lapply(request, function(pattern) { | ||
grep(pattern = pattern, x = available) | ||
})) | ||
|
||
if (any(idx < 1)) | ||
return() | ||
unique(available[idx]) | ||
|
||
unique(available[idx]) # Available files that match the request | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters