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

improve altitude bin validation read_vpts() and as.vpts() #684

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
27 changes: 22 additions & 5 deletions R/as.vpts.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,28 @@
height <- datetime <- source_file <- radar <- NULL

# Throw error if nrows per height are not identical

assertthat::assert_that(
remainder_is_zero(dim(data)[1], length(unique(data$height))) > 0,
msg = "Number of rows per height variable must be identical"
)
# FIXME: first if statement is a weak check that could fail, could be improved.
# retaining for now because of speed
if(!remainder_is_zero(dim(data)[1], length(unique(data$height)))){
data %>%
dplyr::group_by(radar, datetime) %>%
dplyr::mutate(bioRad_internal_interval = height-lag(height)) %>%
dplyr::add_count(name="bioRad_internal_levels") -> data
interval_median <- median(data$bioRad_internal_interval, na.rm=TRUE)
interval_unique <- unique(data$bioRad_internal_interval)
interval_unique <- interval_unique[!is.na(interval_unique)]
if(length(interval_unique)>1){
warning(paste("profiles found with different altitude interval:",paste(sort(interval_unique),collapse=" ")), ", retaining ",interval_median, " only.")
data <- dplyr::filter(data, bioRad_internal_interval == interval_median)

Check warning on line 37 in R/as.vpts.R

View check run for this annotation

Codecov / codecov/patch

R/as.vpts.R#L36-L37

Added lines #L36 - L37 were not covered by tests
}
levels_median <- median(data$bioRad_internal_levels)
levels_unique <- unique(data$bioRad_internal_levels)
if(length(levels_unique)>1){
warning(paste("profiles found with different number of height layers:",paste(sort(levels_unique),collapse=" ")), ", retaining ",levels_median, " only.")
data <- dplyr::filter(data, bioRad_internal_levels == levels_median)
}
data <- dplyr::select(data, -c(bioRad_internal_interval, bioRad_internal_levels))
}

radar <- unique(data[["radar"]])

Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-as.vpts.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
test_that("as.vpts() returns error message for incorrect data", {
test_that("as.vpts() returns warning message for incorrect data", {
df <- read.csv(system.file("extdata", "example_vpts.csv", package = "bioRad"))

#randomly remove row
randomIndex <- sample(nrow(df), 1)
df <- df[-randomIndex, ]

expect_error(as.vpts(df),"identical")
expect_warning(as.vpts(df),"profiles found with different")
})

test_that("as.vpts() handles multiple unique attribute values correctly", {
Expand Down
Loading