Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated modules/data.atmosphere #2775

Merged
merged 30 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4264103
updated check_met_input.R
moki1202 Feb 19, 2021
f99e0c2
updated download.NARR_site.R
moki1202 Feb 19, 2021
06090fa
updated downscaling_helper_functions.R
moki1202 Feb 19, 2021
670b2bd
updated get_cf_variales_table.R
moki1202 Feb 19, 2021
cffae52
updated Rcheck_reference.log
moki1202 Feb 19, 2021
b70f745
final update to download.NARR_site.R
moki1202 Feb 20, 2021
7ec7fbd
final update to downscaling_helper_functions.R
moki1202 Feb 20, 2021
4ecfa00
Update modules/data.atmosphere/R/download.NARR_site.R
infotroph Feb 20, 2021
e9eba91
Update modules/data.atmosphere/R/download.NARR_site.R
infotroph Feb 20, 2021
e7cfb5b
automated documentation update
Feb 20, 2021
aa419c7
indentation
infotroph Feb 21, 2021
a242e31
Update download.NARR_site.R
moki1202 Feb 23, 2021
868a5bf
Update Rcheck_reference.log
moki1202 Feb 23, 2021
f933f1c
Update check_met_input.R
moki1202 Feb 23, 2021
e021c9d
typo
infotroph Feb 24, 2021
ac7708d
automated documentation update
Feb 24, 2021
bdebe36
Merge branch 'develop' into patch-5
infotroph Feb 27, 2021
98d9359
Merge branch 'develop' into patch-5
ashiklom Mar 8, 2021
5e210f7
Merge branch 'develop' into patch-5
ashiklom Mar 10, 2021
eaa7423
Update download.NARR_site.R
moki1202 Mar 10, 2021
002f2a3
Update Rcheck_reference.log
moki1202 Mar 11, 2021
0147052
Update modules/data.atmosphere/R/download.NARR_site.R
infotroph Mar 11, 2021
54785f3
Update modules/data.atmosphere/R/download.NARR_site.R
infotroph Mar 11, 2021
68ddb5e
Update download.NARR_site.R
moki1202 Mar 11, 2021
d3348f6
Update modules/data.atmosphere/tests/Rcheck_reference.log
infotroph Mar 13, 2021
0e8753d
Merge branch 'develop' into patch-5
infotroph Mar 24, 2021
311ca72
Merge branch 'develop' into patch-5
ashiklom Mar 25, 2021
89344c2
Merge branch 'develop' into patch-5
moki1202 Apr 8, 2021
98a20ef
Merge branch 'develop' into patch-5
infotroph May 24, 2021
aa73226
Merge branch 'develop' into patch-5
infotroph May 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions modules/data.atmosphere/R/check_met_input.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
check_met_input_file <- function(metfile,
variable_table = pecan_standard_met_table,
required_vars = variable_table %>%
dplyr::filter(is_required) %>%
dplyr::pull(cf_standard_name),
dplyr::filter(.data$is_required) %>%
dplyr::pull(.data$cf_standard_name),
warn_unknown = TRUE
) {

Expand Down Expand Up @@ -82,7 +82,7 @@ check_met_input_file <- function(metfile,
target_variable = required_vars,
test_passed = required_vars %in% nc_vars,
test_error_message = dplyr::if_else(
test_passed,
.data$test_passed,
NA_character_,
as.character(glue::glue("Missing variable '{target_variable}'."))
)
Expand All @@ -92,9 +92,9 @@ check_met_input_file <- function(metfile,
test_type = "variable has correct units",
target_variable = nc_vars,
test_raw = purrr::map(nc_vars, check_unit, nc = nc, variable_table = variable_table),
test_passed = !purrr::map_lgl(test_raw, inherits, "try-error"),
test_error_message = purrr::map_chr(test_raw, purrr::possibly(as.character, NA_character_))
) %>% dplyr::select(-test_raw)
test_passed = !purrr::map_lgl(.data$test_raw, inherits, "try-error"),
test_error_message = purrr::map_chr(.data$test_raw, purrr::possibly(as.character, NA_character_))
) %>% dplyr::select(-.data$test_raw)

results_df <- dplyr::bind_rows(test_dims_summary, test_required_vars, test_var_units)

Expand All @@ -115,7 +115,7 @@ check_unit <- function(variable, nc, variable_table, warn_unknown = TRUE) {
return(TRUE)
}
var_correct_unit <- variable_table %>%
dplyr::filter(cf_standard_name == variable) %>%
dplyr::filter(.data$cf_standard_name == variable) %>%
dplyr::pull(units)
ncvar_unit <- ncdf4::ncatt_get(nc, variable, "units")[["value"]]
try(testthat::expect_true(
Expand Down
68 changes: 34 additions & 34 deletions modules/data.atmosphere/R/download.NARR_site.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
#' @param overwrite Overwrite existing files? Default=FALSE
#' @param verbose Turn on verbose output? Default=FALSE
#' @param parallel Download in parallel? Default = TRUE
#' @param ncores Number of cores for parallel download. Default is
#' @param ncores Number of cores for parallel download. Default is
#' `parallel::detectCores()`
#'
#' @examples
#'
#'
#' \dontrun{
#' download.NARR_site(tempdir(), "2001-01-01", "2001-01-12", 43.372, -89.907)
#' }
#'
#'
#'
#' @export
#'
Expand Down Expand Up @@ -44,13 +44,13 @@ download.NARR_site <- function(outfolder,

narr_byyear <- narr_data %>%
dplyr::mutate(year = lubridate::year(datetime)) %>%
dplyr::group_by(year) %>%
dplyr::group_by(.data$year) %>%
tidyr::nest()

# Prepare result data frame
result_full <- narr_byyear %>%
dplyr::mutate(
file = file.path(outfolder, paste("NARR", year, "nc", sep = ".")),
file = file.path(outfolder, paste("NARR", .data$year, "nc", sep = ".")),
host = PEcAn.remote::fqdn(),
start_date = date_limits_chr[1],
end_date = date_limits_chr[2],
Expand All @@ -73,10 +73,10 @@ download.NARR_site <- function(outfolder,

narr_proc <- result_full %>%
dplyr::mutate(
data_nc = purrr::map2(data, file, prepare_narr_year, lat = lat, lon = lon)
data_nc = purrr::map2(.data$data, .data$file, prepare_narr_year, lat = lat, lon = lon)
)

results <- dplyr::select(result_full, -data)
results <- dplyr::select(result_full, -.data$data)
return(invisible(results))
} # download.NARR_site

Expand All @@ -86,8 +86,8 @@ download.NARR_site <- function(outfolder,
#' @param file Full path to target file
#' @param lat_nc `ncdim` object for latitude
#' @param lon_nc `ncdim` object for longitude
#' @param verbose
#' @return List of NetCDF variables in data. Creates NetCDF file containing
#' @param verbose logical: ask`ncdf4` functions to be very chatty while they work?
#' @return List of NetCDF variables in data. Creates NetCDF file containing
#' data as a side effect
prepare_narr_year <- function(dat, file, lat_nc, lon_nc, verbose = FALSE) {
starttime <- min(dat$datetime)
Expand Down Expand Up @@ -117,11 +117,11 @@ prepare_narr_year <- function(dat, file, lat_nc, lon_nc, verbose = FALSE) {
#' Create `ncvar` object from variable name
#'
#' @param variable CF variable name
#' @param dims List of NetCDF dimension objects (passed to
#' @param dims List of NetCDF dimension objects (passed to
#' `ncdf4::ncvar_def(..., dim)`)
#' @return `ncvar` object (from `ncvar_def`)
col2ncvar <- function(variable, dims) {
var_info <- narr_all_vars %>% dplyr::filter(CF_name == variable)
var_info <- narr_all_vars %>% dplyr::filter(.data$CF_name == variable)
ncdf4::ncvar_def(
name = variable,
units = var_info$units,
Expand All @@ -136,9 +136,9 @@ col2ncvar <- function(variable, dims) {
#' @param end_date End date for meteorology
#' @param lat.in Latitude coordinate
#' @param lon.in Longitude coordinate
#' @param progress Whether or not to show a progress bar (default = `TRUE`).
#' @param progress Whether or not to show a progress bar (default = `TRUE`).
#' Requires the `progress` package to be installed.
#' @param drop_outside Whether or not to drop dates outside of `start_date` to
#' @param drop_outside Whether or not to drop dates outside of `start_date` to
#' `end_date` range (default = `TRUE`).
#' @inheritParams download.NARR_site
#' @return `tibble` containing time series of NARR data for the given site
Expand Down Expand Up @@ -236,8 +236,8 @@ get_NARR_thredds <- function(start_date, end_date, lat.in, lon.in,
),
robustly(get_narr_url)(url, xy = xy, flx = flx)
infotroph marked this conversation as resolved.
Show resolved Hide resolved
)
flx_data_raw <- dplyr::filter(get_dfs, flx)
sfc_data_raw <- dplyr::filter(get_dfs, !flx)
flx_data_raw <- dplyr::filter(get_dfs, .data$flx)
sfc_data_raw <- dplyr::filter(get_dfs, !.data$flx)
} else {

# Retrieve remaining variables by iterating over URLs
Expand Down Expand Up @@ -294,21 +294,21 @@ get_NARR_thredds <- function(start_date, end_date, lat.in, lon.in,
#' @param dat Nested `tibble` from mapped call to [get_narr_url]
post_process <- function(dat) {
dat %>%
tidyr::unnest(data) %>%
tidyr::unnest(.data$data) %>%
dplyr::ungroup() %>%
dplyr::mutate(datetime = startdate + lubridate::dhours(dhours)) %>%
dplyr::select(-startdate, -dhours) %>%
dplyr::mutate(datetime = .data$startdate + lubridate::dhours(.data$dhours)) %>%
dplyr::select(-.data$startdate, -.data$dhours) %>%
dplyr::select(datetime, dplyr::everything()) %>%
dplyr::select(-url, url)
}

#' Generate NARR url from a vector of dates
#'
#' Figures out file names for the given dates, based on NARR's convoluted and
#' Figures out file names for the given dates, based on NARR's convoluted and
#' inconsistent naming scheme.
#'
#' @param dates Vector of dates for which to generate URL
#' @param flx (Logical) If `TRUE`, format for `flx` variables. Otherwise,
#' @param flx (Logical) If `TRUE`, format for `flx` variables. Otherwise,
#' format for `sfc` variables. See [narr_flx_vars].
#' @author Alexey Shiklomanov
generate_narr_url <- function(dates, flx) {
Expand All @@ -322,25 +322,25 @@ generate_narr_url <- function(dates, flx) {
)
tibble::tibble(date = dates) %>%
dplyr::mutate(
year = lubridate::year(date),
month = lubridate::month(date),
daygroup = daygroup(date, flx)
year = lubridate::year(.data$date),
month = lubridate::month(.data$date),
daygroup = daygroup(.data$date, flx)
) %>%
dplyr::group_by(year, month, daygroup) %>%
dplyr::group_by(.data$year, .data$month, .data$daygroup) %>%
dplyr::summarize(
startdate = min(date),
startdate = min(.data$date),
url = sprintf(
"%s/%d/NARR%s_%d%02d_%s.tar",
base_url,
unique(year),
unique(.data$year),
tag,
unique(year),
unique(month),
unique(daygroup)
unique(.data$year),
unique(.data$month),
unique(.data$daygroup)
)
) %>%
dplyr::ungroup() %>%
dplyr::select(startdate, url)
dplyr::select(.data$startdate, .data$url)
}

#' Assign daygroup tag for a given date
Expand Down Expand Up @@ -382,7 +382,7 @@ get_narr_url <- function(url, xy, flx, pb = NULL) {
if (dhours[1] == 3) dhours <- dhours - 3
narr_vars <- if (flx) narr_flx_vars else narr_sfc_vars
result <- purrr::pmap(
narr_vars %>% dplyr::select(variable = NARR_name, unit = units),
narr_vars %>% dplyr::select(variable = .data$NARR_name, unit = .data$units),
read_narr_var,
nc = nc, xy = xy, flx = flx, pb = pb
)
Expand Down Expand Up @@ -445,7 +445,7 @@ narr_all_vars <- dplyr::bind_rows(narr_flx_vars, narr_sfc_vars)
#'
#' @inheritParams read_narr_var
#' @inheritParams get_NARR_thredds
#' @return Vector length 2 containing NARR `x` and `y` indices, which can be
#' @return Vector length 2 containing NARR `x` and `y` indices, which can be
#' used in `ncdf4::ncvar_get` `start` argument.
#' @author Alexey Shiklomanov
latlon2narr <- function(nc, lat.in, lon.in) {
Expand All @@ -457,11 +457,11 @@ latlon2narr <- function(nc, lat.in, lon.in) {
c(x = x_ind, y = y_ind)
}

#' Convert latitude and longitude to x-y coordinates (in km) in Lambert
#' Convert latitude and longitude to x-y coordinates (in km) in Lambert
#' conformal conic projection (used by NARR)
#'
#' @inheritParams get_NARR_thredds
#' @return `sp::SpatialPoints` object containing transformed x and y
#' @return `sp::SpatialPoints` object containing transformed x and y
#' coordinates, in km, which should match NARR coordinates
#' @importFrom rgdal checkCRSArgs
# ^not used directly here, but needed by sp::CRS.
Expand Down