From d44c92adfe7a515824867939eb282151eca557bf Mon Sep 17 00:00:00 2001 From: Bart Date: Fri, 13 Sep 2024 11:01:50 +0200 Subject: [PATCH 1/4] Document and test calculate param --- R/calculate_param.R | 17 ++++++++++++++++- man/calculate_param.Rd | 14 ++++++++++++++ tests/testthat/test-calculate_param.R | 12 ++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/R/calculate_param.R b/R/calculate_param.R index a92b70a85..e960824bd 100644 --- a/R/calculate_param.R +++ b/R/calculate_param.R @@ -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`). +#' +#' Next some times functions do not operate on a `matrix` or `param` object. +#' One example is the `dplyr::if_else` function, this can be resolved by +#' calling the `c` function on a parameter to convert it to a vector +#' (e.g. `c(DBZH)`). +#' #' @seealso #' * [get_param()] #' @@ -42,6 +52,11 @@ #' calculate_param(example_scan, DR = 10 * log10((ZDR + 1 - 2 * ZDR^0.5 * RHOHV) / #' (ZDR + 1 + 2 * ZDR^0.5 * RHOHV))) #' +#' 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)) @@ -98,7 +113,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] diff --git a/man/calculate_param.Rd b/man/calculate_param.Rd index 70528e0a8..79d964f6f 100644 --- a/man/calculate_param.Rd +++ b/man/calculate_param.Rd @@ -34,6 +34,15 @@ parameters. Useful for calculating quantities that are defined in terms of other basic radar moments, like linear reflectivity eta, depolarization ratio (Kilambi et al. 2018), or for applying clutter corrections (\code{CCORH}) to uncorrected reflectivity moments (\code{TH}) as \code{TH + CCORH}. + +For the expression to work it is important that the operation can be +vectorized. For example the \code{base} \code{ifelse} function is not vectorized, +in these cases alternatives can be used (e.g. \code{dplyr::if_else}). + +Next some times functions do not operate on a \code{matrix} or \code{param} object. +One example is the \code{dplyr::if_else} function, this can be resolved by +calling the \code{c} function on a parameter to convert it to a vector +(e.g. \code{c(DBZH)}). } \section{Methods (by class)}{ \itemize{ @@ -64,6 +73,11 @@ pvol <- calculate_param(pvol, DR = 10 * log10((ZDR + 1 - 2 * ZDR^0.5 * RHOHV) / calculate_param(example_scan, DR = 10 * log10((ZDR + 1 - 2 * ZDR^0.5 * RHOHV) / (ZDR + 1 + 2 * ZDR^0.5 * RHOHV))) +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)) diff --git a/tests/testthat/test-calculate_param.R b/tests/testthat/test-calculate_param.R index 631ca0556..9a83d96af 100644 --- a/tests/testthat/test-calculate_param.R +++ b/tests/testthat/test-calculate_param.R @@ -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)) ) + ) +}) From 31e6d27110c3b63b17edc25b0e87d25e708b5d70 Mon Sep 17 00:00:00 2001 From: Adriaan Dokter Date: Fri, 13 Sep 2024 10:25:38 -0400 Subject: [PATCH 2/4] small documentation edits, fix test list_vpts_aloft --- DESCRIPTION | 2 +- R/calculate_param.R | 10 ++++++---- man/beam_distance.Rd | 2 +- man/beam_height.Rd | 2 +- man/beam_range.Rd | 2 +- man/beam_width.Rd | 2 +- man/calculate_param.Rd | 10 ++++++---- man/gaussian_beam_profile.Rd | 2 +- tests/testthat/test-list_vpts_aloft.R | 6 +++--- 9 files changed, 21 insertions(+), 17 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index c00e3372c..af7f0a9cd 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/R/calculate_param.R b/R/calculate_param.R index e960824bd..efad5e905 100644 --- a/R/calculate_param.R +++ b/R/calculate_param.R @@ -21,10 +21,10 @@ #' vectorized. For example the `base` `ifelse` function is not vectorized, #' in these cases alternatives can be used (e.g. `dplyr::if_else`). #' -#' Next some times functions do not operate on a `matrix` or `param` object. -#' One example is the `dplyr::if_else` function, this can be resolved by -#' calling the `c` function on a parameter to convert it to a vector -#' (e.g. `c(DBZH)`). +#' 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()] @@ -52,6 +52,8 @@ #' 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)) ) diff --git a/man/beam_distance.Rd b/man/beam_distance.Rd index 2d463693e..11ca391a3 100644 --- a/man/beam_distance.Rd +++ b/man/beam_distance.Rd @@ -41,8 +41,8 @@ beam_distance(100000, 5) Other beam_functions: \code{\link{beam_height}()}, -\code{\link{beam_profile_overlap}()}, \code{\link{beam_profile}()}, +\code{\link{beam_profile_overlap}()}, \code{\link{beam_range}()}, \code{\link{beam_width}()}, \code{\link{gaussian_beam_profile}()} diff --git a/man/beam_height.Rd b/man/beam_height.Rd index 787278a33..4a559557e 100644 --- a/man/beam_height.Rd +++ b/man/beam_height.Rd @@ -58,8 +58,8 @@ plot(range, beam_height(range, 0.5), ylab = "beam height [m]", xlab = "range [m] Other beam_functions: \code{\link{beam_distance}()}, -\code{\link{beam_profile_overlap}()}, \code{\link{beam_profile}()}, +\code{\link{beam_profile_overlap}()}, \code{\link{beam_range}()}, \code{\link{beam_width}()}, \code{\link{gaussian_beam_profile}()} diff --git a/man/beam_range.Rd b/man/beam_range.Rd index 1e45b78c6..29b23a23d 100644 --- a/man/beam_range.Rd +++ b/man/beam_range.Rd @@ -38,8 +38,8 @@ beam_range(100000, 5) Other beam_functions: \code{\link{beam_distance}()}, \code{\link{beam_height}()}, -\code{\link{beam_profile_overlap}()}, \code{\link{beam_profile}()}, +\code{\link{beam_profile_overlap}()}, \code{\link{beam_width}()}, \code{\link{gaussian_beam_profile}()} } diff --git a/man/beam_width.Rd b/man/beam_width.Rd index f21165cfc..fdbe53469 100644 --- a/man/beam_width.Rd +++ b/man/beam_width.Rd @@ -32,8 +32,8 @@ plot(range, beam_width(range), ylab = "beam width [m]", xlab = "range [m]") Other beam_functions: \code{\link{beam_distance}()}, \code{\link{beam_height}()}, -\code{\link{beam_profile_overlap}()}, \code{\link{beam_profile}()}, +\code{\link{beam_profile_overlap}()}, \code{\link{beam_range}()}, \code{\link{gaussian_beam_profile}()} } diff --git a/man/calculate_param.Rd b/man/calculate_param.Rd index 79d964f6f..5ec7b17a2 100644 --- a/man/calculate_param.Rd +++ b/man/calculate_param.Rd @@ -39,10 +39,10 @@ For the expression to work it is important that the operation can be vectorized. For example the \code{base} \code{ifelse} function is not vectorized, in these cases alternatives can be used (e.g. \code{dplyr::if_else}). -Next some times functions do not operate on a \code{matrix} or \code{param} object. -One example is the \code{dplyr::if_else} function, this can be resolved by -calling the \code{c} function on a parameter to convert it to a vector -(e.g. \code{c(DBZH)}). +Also note that some functions do not operate on a \code{matrix} or \code{param} object. +One example is the \code{dplyr::if_else} function. A workaround is +calling the \code{c()} function on a parameter to convert it to a vector +(e.g. \code{c(DBZH)}, see examples). } \section{Methods (by class)}{ \itemize{ @@ -73,6 +73,8 @@ pvol <- calculate_param(pvol, DR = 10 * log10((ZDR + 1 - 2 * ZDR^0.5 * RHOHV) / 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)) ) diff --git a/man/gaussian_beam_profile.Rd b/man/gaussian_beam_profile.Rd index edb080804..3dd28fe62 100644 --- a/man/gaussian_beam_profile.Rd +++ b/man/gaussian_beam_profile.Rd @@ -53,8 +53,8 @@ Beam profile is calculated using \link{beam_height} and \link{beam_width}. \code Other beam_functions: \code{\link{beam_distance}()}, \code{\link{beam_height}()}, -\code{\link{beam_profile_overlap}()}, \code{\link{beam_profile}()}, +\code{\link{beam_profile_overlap}()}, \code{\link{beam_range}()}, \code{\link{beam_width}()} } diff --git a/tests/testthat/test-list_vpts_aloft.R b/tests/testthat/test-list_vpts_aloft.R index 6ae77701c..9467e2efd 100644 --- a/tests/testthat/test-list_vpts_aloft.R +++ b/tests/testthat/test-list_vpts_aloft.R @@ -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",{ From 36d71b6b268b39a35d171b95535a868cb9dcd816 Mon Sep 17 00:00:00 2001 From: Adriaan Dokter Date: Fri, 13 Sep 2024 10:31:20 -0400 Subject: [PATCH 3/4] disable ubuntu-latest, r: 'oldrel-1' runner --- .github/workflows/R-CMD-check.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 22ca63f78..e48bbe1e6 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -22,7 +22,7 @@ jobs: - {os: windows-latest, r: 'release'} # - {os: ubuntu-latest, r: 'devel'} # disabled, runtime > 45 min. - {os: ubuntu-latest, r: 'release'} - - {os: ubuntu-latest, r: 'oldrel-1'} + # - {os: ubuntu-latest, r: 'oldrel-1'} # disabled, pak error installing rjson: Needs R >= 4.4.0 env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} From a771262a9f24b1899199446eb6a16a5b2dc065c9 Mon Sep 17 00:00:00 2001 From: Bart Date: Mon, 16 Sep 2024 10:40:27 +0200 Subject: [PATCH 4/4] Make sure remark is comment --- R/calculate_param.R | 2 +- man/calculate_param.Rd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/R/calculate_param.R b/R/calculate_param.R index efad5e905..312d68d6f 100644 --- a/R/calculate_param.R +++ b/R/calculate_param.R @@ -53,7 +53,7 @@ #' (ZDR + 1 + 2 * ZDR^0.5 * RHOHV))) #' #' # set all reflectivity values to NA when correlation coefficient > 0.95 -#' (indicating precipitation) +#' # (indicating precipitation) #' if (require(dplyr, quietly = TRUE)) { #' calculate_param(pvol, #' DBZH=if_else(c(RHOHV)>.95, NA, c(DBZH)) ) diff --git a/man/calculate_param.Rd b/man/calculate_param.Rd index 5ec7b17a2..0a77d1bca 100644 --- a/man/calculate_param.Rd +++ b/man/calculate_param.Rd @@ -74,7 +74,7 @@ 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) +# (indicating precipitation) if (require(dplyr, quietly = TRUE)) { calculate_param(pvol, DBZH=if_else(c(RHOHV)>.95, NA, c(DBZH)) )