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

Document and test calculate param #673

Merged
merged 5 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ Suggests:
LazyData: true
Encoding: UTF-8
VignetteBuilder: knitr
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
19 changes: 18 additions & 1 deletion R/calculate_param.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@
#' @return An object of the same class as `x`, either a `pvol` or `scan`.
#' @export
#'
#' @details
#' For the expression to work it is important that the operation can be
#' vectorized. For example the `base` `ifelse` function is not vectorized,
#' in these cases alternatives can be used (e.g. `dplyr::if_else`).
#'
#' Also note that some functions do not operate on a `matrix` or `param` object.
#' One example is the `dplyr::if_else` function. A workaround is
#' calling the `c()` function on a parameter to convert it to a vector
#' (e.g. `c(DBZH)`, see examples).
#'
#' @seealso
#' * [get_param()]
#'
Expand All @@ -42,6 +52,13 @@
#' calculate_param(example_scan, DR = 10 * log10((ZDR + 1 - 2 * ZDR^0.5 * RHOHV) /
#' (ZDR + 1 + 2 * ZDR^0.5 * RHOHV)))
#'
#' # set all reflectivity values to NA when correlation coefficient > 0.95
#' (indicating precipitation)
#' if (require(dplyr, quietly = TRUE)) {
#' calculate_param(pvol,
#' DBZH=if_else(c(RHOHV)>.95, NA, c(DBZH)) )
#' }
#'
#' # it also works for ppis
#' ppi <- project_as_ppi(example_scan)
#' calculate_param(ppi, exp(DBZH))
Expand Down Expand Up @@ -98,7 +115,7 @@ calculate_param.scan <- function(x, ...) {
if(length(x$params)!=1)
lapply(lapply(x$params[-1], attr, i), function(x,y) assertthat::assert_that(assertthat::are_equal(x,y)), y=attr(x$params[[1]], i))
}
if (as.character(as.list(substitute(...))[[1L]]) == "list") {
if (as.character(as.list(substitute(...))[[1L]])[1] == "list") {
calc <- as.list(substitute(...))[-1L]
} else {
calc <- as.list(substitute(list(...)))[-1L]
Expand Down
2 changes: 1 addition & 1 deletion man/beam_distance.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/beam_height.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/beam_range.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/beam_width.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/calculate_param.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/gaussian_beam_profile.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/testthat/test-calculate_param.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,15 @@ test_that("calculate_param() adds the calculated parameter", {
)
expect_equal(ppi_calc$data$DBZH * 2, ppi_calc$data$new_param)
})
test_that("calculate_param() works with if_else", {
skip_if_not_installed('dplyr')
require(dplyr)
expect_equal(
calculate_param(get_scan(example_pvol, 0.5), DBZH=if_else(c(RHOHV)>.95, NA, c(DBZH)) ),
get_scan(calculate_param(example_pvol, DBZH=if_else(c(RHOHV)>.95, NA, c(DBZH)) ), .5)
)
expect_equal(
calculate_param(example_scan, DBZH=dplyr::if_else(c(RHOHV)>.95, NA, c(DBZH)) ),
calculate_param(example_scan, DBZH=if_else(c(RHOHV)>.95, NA, c(DBZH)) )
)
})
6 changes: 3 additions & 3 deletions tests/testthat/test-list_vpts_aloft.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ test_that("list_vpts_aloft() warns if data was found for subset of radars or if
)
expect_warning(
list_vpts_aloft(
date_min = "1900-01-01",
date_max = "2023-05-22",
date_min = "2020-01-01",
date_max = "2020-06-01",
radars = c("nobml")
),
"Radar data found between 2023-02 and 2023-05 but not every date has radar data")
"Radar data found between 2020-02 and 2020-06 but not every date has radar data")
})

test_that("list_vpts_aloft() warns and returns emtpy vector on no data found",{
Expand Down
Loading