Skip to content

Commit

Permalink
make vol2birdR suggested (#565)
Browse files Browse the repository at this point in the history
* Change rsl to NEXRAD in message

* Updated message untill vol2bird r is released

* Update hooks.R

Installation instructions are now with `check_installed`

* refer to vol2birdR install instructions website

* Update system requirements message

* don't warn about system dependencies on intel macs, which have pre-compiled binary available

* shorten dependency warning

---------

Co-authored-by: Adriaan Dokter
  • Loading branch information
bart1 authored May 22, 2023
1 parent fce98a1 commit a464125
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 49 deletions.
5 changes: 2 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ Imports:
utils,
viridisLite,
viridis,
vol2birdR
rlang
Remotes:
adokter/vol2birdR,
bioc::rhdf5
Suggests:
vol2birdR,
aws.s3,
ggmap (>= 3.0.0),
knitr,
rlang,
tidyselect,
rmarkdown,
testthat,
Expand Down
2 changes: 1 addition & 1 deletion R/apply_mistnet.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ apply_mistnet_body <- function(file, pvolfile_out, verbose = FALSE,
local_install, local_mistnet) {

assert_that(file.exists(file))

rlang::check_installed('vol2birdR',format_reason_vol2bird("to run `apply_mist`."))
if (!vol2birdR::mistnet_exists()) {
stop("MistNet has not been installed, see vol2birdR package documentation for install instructions")
}
Expand Down
1 change: 1 addition & 0 deletions R/bioRad-deprecated.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ update_docker <- function(...) {
#' @export
vol2bird_version <- function(...) {
.Deprecated("vol2birdR::vol2bird_version")
rlang::check_installed('vol2birdR',format_reason_vol2bird("to run `vol2bird_version`."))
warning("vol2bird_version has been moved to package vol2birdR")
vol2birdR::vol2bird_version()
}
Expand Down
40 changes: 25 additions & 15 deletions R/calculate_vp.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ calculate_vp <- function(file, vpfile = "", pvolfile_out = "",
vpfile = vpfile, pvolfile_out = pvolfile_out,
autoconf = autoconf, verbose = verbose, warnings = warnings,
mount = mount, sd_vvp_threshold = sd_vvp_threshold,
rcs = rcs, dual_pol = dual_pol, rho_hv = rho_hv, single_pol=single_pol,
rcs = rcs, dual_pol = dual_pol, rho_hv = rho_hv, single_pol = single_pol,
elev_min = elev_min, elev_max = 90, azim_min = 0, azim_max = 360,
range_min = range_min, range_max = range_max, n_layer = n_layer,
h_layer = h_layer, dealias = dealias,
Expand All @@ -219,7 +219,7 @@ calculate_vp <- function(file, vpfile = "", pvolfile_out = "",
file.remove(tmp_pvol_file)
return(res)
}

rlang::check_installed("vol2birdR", format_reason_vol2bird("to run `calculate_vp`."))
# check input arguments
assert_that(
is.character(file),
Expand All @@ -237,25 +237,24 @@ calculate_vp <- function(file, vpfile = "", pvolfile_out = "",
if (!is.logical(mistnet)) {
stop("`mistnet` must be a logical value.")
}
if (mistnet){
if(missing(local_mistnet)){
if (mistnet) {
if (missing(local_mistnet)) {
if (!vol2birdR::mistnet_exists()) {
stop("MistNet has not been installed, see vol2birdR package documentation for install instructions.")
}
}
else{
if(!file.exists(local_mistnet)){
stop(paste0("'",local_mistnet,"' does not exist, `local_mistnet` should specify the path of MistNet segmentation model"))
} else {
if (!file.exists(local_mistnet)) {
stop(paste0("'", local_mistnet, "' does not exist, `local_mistnet` should specify the path of MistNet segmentation model"))
}
}
}
if (!is.logical(dealias)) {
stop("`dealias` must be a logical value.")
}
if(!missing(mount)) {
if (!missing(mount)) {
warning("mount argument is deprecated")
}
if(!missing(local_install)){
if (!missing(local_install)) {
warning("local_install argument is deprecated")
}

Expand Down Expand Up @@ -333,7 +332,8 @@ calculate_vp <- function(file, vpfile = "", pvolfile_out = "",
assert_that(is.flag(mistnet))
assert_that(
!(mistnet && !vol2birdR::mistnet_exists() && missing(local_mistnet)),
msg = "Can't find MistNet installation, see vol2birdR package for install instructions.")
msg = "Can't find MistNet installation, see vol2birdR package for install instructions."
)
assert_that(is.flag(dealias))

filedir <- dirname(normalizePath(file[1], winslash = "/"))
Expand All @@ -342,7 +342,7 @@ calculate_vp <- function(file, vpfile = "", pvolfile_out = "",
profile.tmp <- tempfile()

config <- vol2birdR::vol2bird_config()
if(!autoconf){
if (!autoconf) {
config$birdRadarCrossSection <- rcs
config$rhohvThresMin <- rho_hv
config$elevMin <- elev_min
Expand All @@ -358,16 +358,16 @@ calculate_vp <- function(file, vpfile = "", pvolfile_out = "",
config$dualPol <- dual_pol
config$dealiasVrad <- dealias
if (!missing(sd_vvp_threshold)) config$stdDevMinBird <- sd_vvp_threshold
} else{
} else {
# setting stdDevMinBird triggers it to be set according to wavelength (1 m/s for S-band, 2 m/s for C-band)
config$stdDevMinBird <- -1
}
config$mistNetElevs <- mistnet_elevations
config$useMistNet <- mistnet
if(!missing(local_mistnet) & mistnet) config$mistNetPath <- local_mistnet
if (!missing(local_mistnet) & mistnet) config$mistNetPath <- local_mistnet

# run vol2bird
vol2birdR::vol2bird(file=file, config=config, vpfile=profile.tmp, pvolfile_out=pvolfile_out, verbose = verbose)
vol2birdR::vol2bird(file = file, config = config, vpfile = profile.tmp, pvolfile_out = pvolfile_out, verbose = verbose)

# read output into a vp object
output <- read_vpfiles(profile.tmp)
Expand All @@ -380,3 +380,13 @@ calculate_vp <- function(file, vpfile = "", pvolfile_out = "",

output
}

format_reason_vol2bird <- function(x) {
on_intel_mac <- Sys.info()[["sysname"]]=="Darwin" & Sys.info()[["machine"]] == "x86_64"
if (.Platform$OS.type == "unix" & !on_intel_mac) {
return(paste(x, "Installation may require pre-installation of additional system libraries, see https://github.com/adokter/vol2birdR#install for instructions."))
} else {
# platform is Windows or Intel Mac, pre-compiled binaries are available on CRAN
return(x)
}
}
5 changes: 3 additions & 2 deletions R/hooks.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
.onLoad <- function(libname, pkgname) {
register_all_s3_methods() # dynamically registers non-imported pkgs (tidyverse) # nocov
assert_that(is.string(find.package("vol2birdR", verbose=F, quiet=T)),msg="package vol2birdR not found. See https://adokter.github.io/vol2birdR/ for install instructions")
}

.onAttach <- function(libname, pkgname) {
packageStartupMessage(paste("Welcome to", pkgname, "version", packageVersion(pkgname)))
packageStartupMessage(paste("using vol2birdR version ", packageVersion("vol2birdR"), ifelse(vol2birdR::mistnet_exists(), " (MistNet installed)", " (MistNet not installed)"), sep = ""))
if (rlang::is_installed("vol2birdR")) {
packageStartupMessage(paste("using vol2birdR version ", packageVersion("vol2birdR"), ifelse(vol2birdR::mistnet_exists(), " (MistNet installed)", " (MistNet not installed)"), sep = ""))
}
}
1 change: 1 addition & 0 deletions R/nexrad_odim.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
nexrad_to_odim <- function(pvolfile_nexrad, pvolfile_odim, verbose = FALSE) {
assert_that(dir.exists(dirname(pvolfile_odim)),msg=paste("output directory", dirname(pvolfile_odim), "not found"))
assert_that(is.writeable(dirname(pvolfile_odim)))
rlang::check_installed('vol2birdR',format_reason_vol2bird('to run `nexrad_to_odim`.'))

config <- vol2birdR::vol2bird_config()
vol2birdR::rsl2odim(file=pvolfile_nexrad, config=config, pvolfile_out=pvolfile_odim, verbose=verbose)
Expand Down
53 changes: 28 additions & 25 deletions R/read_pvolfile.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,20 @@ read_pvolfile <- function(file, param = c(
sort = TRUE, lat, lon, height, elev_min = 0,
elev_max = 90, verbose = TRUE,
mount = dirname(file), local_install) {
if(!missing(local_install)) warning("argument 'local_install' has been deprecated")
if(!file.exists(file)){
stop(paste0("'",file,"' does not exist in current working directory ('",getwd(),"')."))
}
tryCatch(read_pvolfile_body(
file, param, sort, lat, lon,
height, elev_min, elev_max,
verbose, mount, local_install
),
error = function(err) {
rhdf5::h5closeAll()
stop(err)
if (!missing(local_install)) warning("argument 'local_install' has been deprecated")
if (!file.exists(file)) {
stop(paste0("'", file, "' does not exist in current working directory ('", getwd(), "')."))
}
tryCatch(
read_pvolfile_body(
file, param, sort, lat, lon,
height, elev_min, elev_max,
verbose, mount, local_install
),
error = function(err) {
rhdf5::h5closeAll()
stop(err)
}
)
}

Expand Down Expand Up @@ -125,9 +126,9 @@ read_pvolfile_body <- function(file, param = c(
}
} else {
pvol_tmp <- tempfile()

rlang::check_installed("vol2birdR", format_reason_vol2bird("to read `NEXRAD` files."))
config <- vol2birdR::vol2bird_config()
vol2birdR::rsl2odim(file=file, config=config, pvolfile_out=pvol_tmp, verbose=verbose)
vol2birdR::rsl2odim(file = file, config = config, pvolfile_out = pvol_tmp, verbose = verbose)

if (!is.pvolfile(pvol_tmp)) {
file.remove(pvol_tmp)
Expand Down Expand Up @@ -168,9 +169,9 @@ read_pvolfile_body <- function(file, param = c(
}

# construct wavelength attribute from frequency attribute if possible:
if(is.null(attribs.how$wavelength) & !is.null(attribs.how$frequency)){
speed_of_light = 299792458
attribs.how$wavelength = 100*speed_of_light/attribs.how$frequency
if (is.null(attribs.how$wavelength) & !is.null(attribs.how$frequency)) {
speed_of_light <- 299792458
attribs.how$wavelength <- 100 * speed_of_light / attribs.how$frequency
}

vol.lat <- c(attribs.where$lat) # need the c() to convert single element matrix to single element vector
Expand Down Expand Up @@ -218,7 +219,7 @@ read_pvolfile_body <- function(file, param = c(
datetime <- as.POSIXct(paste(attribs.what$date, attribs.what$time),
format = "%Y%m%d %H%M%S", tz = "UTC"
)
if(is.null(attribs.what$source)) attribs.what$source=""
if (is.null(attribs.what$source)) attribs.what$source <- ""
sources <- strsplit(attribs.what$source, ",")[[1]]
radar <- gsub("NOD:", "", sources[which(grepl("NOD:", sources))])
if (length(radar) == 0) {
Expand Down Expand Up @@ -254,8 +255,8 @@ read_pvolfile_body <- function(file, param = c(
# filter out NULL output from read_pvolfile_scan
valid_scans <- which(!sapply(data, is.null))
assert_that(length(valid_scans) > 0, msg = paste("none of the requested scan parameters found in file", file))
if(length(valid_scans) < length(scans)){
warning(paste("ignoring",length(scans)-length(valid_scans),"scan(s) in file",file,"because requested scan parameter(s) are missing."))
if (length(valid_scans) < length(scans)) {
warning(paste("ignoring", length(scans) - length(valid_scans), "scan(s) in file", file, "because requested scan parameter(s) are missing."))
}
data <- data[valid_scans]

Expand Down Expand Up @@ -349,7 +350,7 @@ read_pvolfile_scan <- function(file, scan, param, radar, datetime, geo, attribut
names(quantities) <- quantityNames

# if wavelength is attribute is missing at the scan level, copy it from the pvol level
if(is.null(attribs.how$wavelength)) attribs.how$wavelength = attributes$how$wavelength
if (is.null(attribs.how$wavelength)) attribs.how$wavelength <- attributes$how$wavelength

output <- list(
radar = radar, datetime = datetime, params = quantities,
Expand All @@ -370,12 +371,14 @@ read_pvolfile_quantity <- function(file, quantity, radar, datetime, geo, dtype)
data <- replace(data, data == as.numeric(attr$nodata), NA)
data <- replace(data, data == as.numeric(attr$undetect), NaN)
data <- as.numeric(attr$offset) + as.numeric(attr$gain) * data
if(attr$quantity == "RHOHV"){
if (attr$quantity == "RHOHV") {
data <- replace(data, data > 10, NaN)
}
conversion <- list(gain = as.numeric(attr$gain), offset = as.numeric(attr$offset),
nodata = as.numeric(attr$nodata), undetect = as.numeric(attr$undetect),
dtype = dtype)
conversion <- list(
gain = as.numeric(attr$gain), offset = as.numeric(attr$offset),
nodata = as.numeric(attr$nodata), undetect = as.numeric(attr$undetect),
dtype = dtype
)
class(data) <- c("param", class(data))
attributes(data)$radar <- radar
attributes(data)$datetime <- datetime
Expand Down
8 changes: 5 additions & 3 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ is.nan.data.frame <- function(x) do.call(cbind, lapply(x, is.nan))
#' Inspired by <https://testthat.r-lib.org/articles/skipping.html#helpers>.
#'
#' @keywords internal
skip_if_no_mistnet <- function() {
if (vol2birdR::mistnet_exists()) {
return(invisible(TRUE))
skip_if_no_mistnet <- function(){
if(rlang::is_installed('vol2birdR')){
if (vol2birdR::mistnet_exists()) {
return(invisible(TRUE))
}
}
testthat::skip("No MistNet")
}

0 comments on commit a464125

Please sign in to comment.