From 45b8dfb7aedd49b05a11d26fe2432d131dc6e76e Mon Sep 17 00:00:00 2001 From: James Mineau Date: Thu, 27 Feb 2025 11:25:20 -0700 Subject: [PATCH 1/2] change n_hours_per_met_file parameter to met_file_tres --- docs/configuration.md | 2 +- r/dependencies.r | 1 + r/run_stilt.r | 4 ++-- r/src/find_met_files.r | 42 ++++++++++++++++++++--------------------- r/src/simulation_step.r | 12 ++++++------ r/stilt_cli.r | 2 +- 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 20ef947..684cb56 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -64,7 +64,7 @@ str(receptors) | -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `met_path` | Absolute path to ARL compatible meteorological data files | | `met_file_format` | String detailing file naming convention for meteorological data files using a mixture of datetime and regex syntax. The formatting string accepts `grep` compatible regular expressions (`.\*.arl`), `strftime` compatible datetime strings (`%Y%m%d%H`) or any combination of the two. Datetime syntax is expanded to all unique combinations required for the receptor and simulation duration and the intersection between the requested files and files available in `met_path` is determined with `grep`, allowing partial matching and compatible regular expressions to be used to identify the relevant data. Matching does not require the full format to be specified - e.g. `\*.arl`, `%Y`, `%Y%m%d`, `%Y%m%d_d0.*.arl` would all match with a file named `20180130_d01.arl`. | -| `n_hours_per_met_file` | Number of hours per meteorological data file. To determine the number of hours in an ARL compatible meteorological data file, refer to the README, including the file naming convention, provided by the data source. For example, the [NOAA HRRR README](https://www.ready.noaa.gov/data/archives/hrrr/README.TXT) specifies a "6 hour data file beginning with 00z - 05z in the first file of the day". Defaults to 6 | +| `met_file_tres` | Time resolution of meteorological data files. To determine the time resolution in an ARL compatible meteorological data file, refer to the README, including the file naming convention, provided by the data source. For example, the [NOAA HRRR README](https://www.ready.noaa.gov/data/archives/hrrr/README.TXT) specifies a "6 hour data file beginning with 00z - 05z in the first file of the day". Defaults to '6 hours' | | `met_subgrid_buffer` | Percent to extend footprint area for meteorological subdomain when using `met_subgrid_enable`. Defaults to 0.1 (10%) | | `met_subgrid_enable` | Enables extraction of spatial subdomains from files in `met_path` using HYSPLIT's `xtrct_grid` binary prior to executing simulations. If enabled, will create files in `/met/`. This can substantially accelerate simulation speed at the cost of increased disk usage. Defaults to disabled | | `met_subgrid_levels` | If set, extracts the defined number of vertical levels from the meteorological data files to further accelerate simulations. Defaults to `NA`, which includes all vertical levels available | diff --git a/r/dependencies.r b/r/dependencies.r index 97e1072..530f3de 100644 --- a/r/dependencies.r +++ b/r/dependencies.r @@ -12,6 +12,7 @@ invisible(lapply(rsc, source)) # Load external libraries if (!'lib.loc' %in% ls()) lib.loc <- NULL libs <- load_libs('dplyr', + 'lubridate', 'ncdf4', 'parallel', 'raster', diff --git a/r/run_stilt.r b/r/run_stilt.r index a3094ff..0691528 100755 --- a/r/run_stilt.r +++ b/r/run_stilt.r @@ -52,7 +52,7 @@ yres <- xres # Meteorological data input met_path <- '' met_file_format <- '%Y%m%d.%Hz.hrrra' -n_hours_per_met_file <- 6 +met_file_tres <- '6 hours' met_subgrid_buffer <- 0.2 met_subgrid_enable <- F met_subgrid_levels <- NA @@ -236,13 +236,13 @@ stilt_apply(FUN = simulation_step, maxdim = maxdim, maxpar = maxpar, met_file_format = met_file_format, + met_file_tres = met_file_tres, met_path = met_path, met_subgrid_buffer = met_subgrid_buffer, met_subgrid_enable = met_subgrid_enable, met_subgrid_levels = met_subgrid_levels, mgmin = mgmin, n_hours = n_hours, - n_hours_per_met_file = n_hours_per_met_file, n_met_min = n_met_min, ncycl = ncycl, ndump = ndump, diff --git a/r/src/find_met_files.r b/r/src/find_met_files.r index 96f541d..732aff7 100644 --- a/r/src/find_met_files.r +++ b/r/src/find_met_files.r @@ -7,41 +7,41 @@ #' @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_path directory to find meteorological data #' @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 met_path directory to find meteorological data +#' @param met_file_tres time resolution of meteorological data files #' #' @import dplyr +#' @import lubridate #' @export -find_met_files <- function(t_start, n_hours, n_hours_per_met_file, - met_file_format, met_path) { +find_met_files <- function(t_start, n_hours, met_path, + met_file_format, met_file_tres) { require(dplyr) + require(lubridate) - ts <- as.POSIXct(t_start, tz = 'UTC') - is_backward <- n_hours < 0 - met_bracket <- n_hours_per_met_file - 1 # ts can be in the middle of a met file + # Simulation timing + sim_start <- as.POSIXct(t_start, tz = 'UTC') + sim_end <- sim_start + as.difftime(n_hours, units = 'hours') # 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 - ) + met_start <- floor_date(min(sim_start, sim_end), unit = met_file_tres) + met_end <- max(sim_start, sim_end) + met_end_ceil <- ceiling_date(met_end, unit = met_file_tres) + if (n_hours < 0 + && hour(met_end) == hour(met_end_ceil) - 1 + && minute(met_end) > 0) { + # If the end time is at the end of a met file, + # add an hour to the end time to include the next file. + # This is necessary to interpolate the last hour of data. + met_end <- met_end_ceil } + met_times <- seq(met_start, met_end, by = met_file_tres) # Format the request and remove duplicates - request <- met_hours %>% + request <- met_times %>% strftime(tz = 'UTC', format = met_file_format) %>% unique() diff --git a/r/src/simulation_step.r b/r/src/simulation_step.r index 425874a..625605b 100644 --- a/r/src/simulation_step.r +++ b/r/src/simulation_step.r @@ -53,7 +53,8 @@ simulation_step <- function(before_footprint = list(function() {output}), lib.loc = NULL, maxdim = 1, maxpar = numpar, - met_file_format, + met_file_format, + met_file_tres = '6 hours', met_path, met_subgrid_buffer = 0.1, met_subgrid_enable = F, @@ -61,7 +62,6 @@ simulation_step <- function(before_footprint = list(function() {output}), mgmin = 10, mhrs = 9999, n_hours = -24, - n_hours_per_met_file = 6, n_met_min = 1, ncycl = 0, ndump = 0, @@ -258,8 +258,8 @@ simulation_step <- function(before_footprint = list(function() {output}), link_files(exe, rundir) # Find necessary met files - met_files <- find_met_files(r_run_time, n_hours, n_hours_per_met_file, - met_file_format, met_path) + met_files <- find_met_files(r_run_time, n_hours, met_path, + met_file_format, met_file_tres) if (length(met_files) < n_met_min) { msg <- paste('Insufficient number of meteorological files found. Check', 'specifications in run_stilt.r') @@ -276,8 +276,8 @@ simulation_step <- function(before_footprint = list(function() {output}), met_subgrid_buffer = met_subgrid_buffer) # Find necessary met files for subgrids - met_files <- find_met_files(r_run_time, n_hours, n_hours_per_met_file, - met_file_format, met_path) + met_files <- find_met_files(r_run_time, n_hours, met_path, + met_file_format, met_file_tres) if (length(met_files) < n_met_min) { msg <- paste('Insufficient number of meteorological files found. Check', 'specifications in run_stilt.r') diff --git a/r/stilt_cli.r b/r/stilt_cli.r index a7d560d..0ba21fb 100755 --- a/r/stilt_cli.r +++ b/r/stilt_cli.r @@ -119,13 +119,13 @@ stilt_args <- list( maxdim = as.numeric(args$maxdim), maxpar = as.numeric(args$maxpar), met_file_format = as.character(args$met_file_format), + met_file_tres = as.character(args$met_file_tres), met_path = as.character(args$met_path), met_subgrid_buffer = as.numeric(args$met_subgrid_buffer), met_subgrid_enable = as.logical(args$met_subgrid_enable), met_subgrid_levels = as.numeric(args$met_subgrid_levels), mgmin = as.numeric(args$mgmin), n_hours = as.numeric(args$n_hours), - n_hours_per_met_file = as.numeric(args$n_hours_per_met_file), n_met_min = as.numeric(args$n_met_min), ncycl = as.numeric(args$ncycl), ndump = as.numeric(args$ndump), From ba7cb14898b01d56d702a17653e73981e44e81d4 Mon Sep 17 00:00:00 2001 From: James Mineau Date: Thu, 27 Feb 2025 11:47:42 -0700 Subject: [PATCH 2/2] roxygen import --- r/src/link_files.r | 1 + 1 file changed, 1 insertion(+) diff --git a/r/src/link_files.r b/r/src/link_files.r index 61851ac..eca9a4f 100644 --- a/r/src/link_files.r +++ b/r/src/link_files.r @@ -4,6 +4,7 @@ #' @param from location of files #' @param to location to create links #' +#' @import R.utils #' @export link_files <- function(from, to) {