Skip to content

Commit

Permalink
add depth screening function to guide user to checkHzDepthLogic()
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Oct 11, 2024
1 parent 2902a8a commit 80dc40f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
15 changes: 15 additions & 0 deletions R/SoilProfileCollection-setters.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ setReplaceMethod("depths", "data.frame",
return(depth)
}

.checkDepthOrder <- function(x, depthcols) {
if (any(x[[depthcols[2]]] < x[[depthcols[1]]], na.rm = TRUE)) {
warning("One or more horizon bottom depths are shallower than top depth. Check depth logic with aqp::checkHzDepthLogic()", call. = FALSE)
}
}

.screenDepths <- function(x, depthcols = horizonDepths(x)) {
.checkNAdepths(x[[depthcols[1]]], "top")
.checkNAdepths(x[[depthcols[2]]], "bottom")
.checkDepthOrder(x, depthcols)
}

# create 0-length spc from id and horizon depth columns (`idn`, `hzd`)
# - allows template horizon (`hz`) and site (`st`) data to be provided (for additional columns)
.prototypeSPC <- function(idn, hzd,
Expand Down Expand Up @@ -178,6 +190,9 @@ setReplaceMethod("depths", "data.frame",
data[[depthcols[1]]] <- .checkNAdepths(data[[depthcols[1]]], "top")
data[[depthcols[2]]] <- .checkNAdepths(data[[depthcols[2]]], "bottom")

# warn if bottom depth shallower than top (old style O horizons, data entry issues, etc.)
.checkDepthOrder(data, depthcols)

tdep <- data[[depthcols[1]]]

# calculate ID-top depth order, re-order input data
Expand Down
2 changes: 2 additions & 0 deletions R/collapseHz.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ collapseHz <- function(x,
idn <- idname(x)
hzd <- horizonDepths(x)

.screenDepths(x, hzd)

# use exact match of existing genhz labels as default in lieu of pattern
if (is.null(pattern) & missing(by)) {
by <- GHL(x, required = TRUE)
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/test-collapseHz.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ test_that("collapseHz works", {

# works on SPC with filled profile (1 horizon with NA depths)
all_na <- subsetHz(jacobs2000_gen[1,], TRUE)
all_na$top <- NA
all_na$bottom <- NA
all_na$top <- NA_real_
all_na$bottom <- NA_real_
expect_warning(na_nonna <- c(all_na, jacobs2000_gen[2:5,]))
expect_silent(f <- collapseHz(all_na, by = "genhz"))
expect_silent(n <- collapseHz(na_nonna, by = "genhz"))
expect_warning(f <- collapseHz(all_na, by = "genhz"), "contain NA")
na_nonna$top[2] <- 19
expect_warning(n <- collapseHz(na_nonna, by = "genhz"), "bottom depths are shallower than top")
expect_equal(nrow(n), 14)


Expand Down

0 comments on commit 80dc40f

Please sign in to comment.