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

Add thicknessOf() #306

Merged
merged 8 commits into from
Apr 1, 2024
Merged

Add thicknessOf() #306

merged 8 commits into from
Apr 1, 2024

Conversation

brownag
Copy link
Member

@brownag brownag commented Feb 20, 2024

Adds a commonly sought-after operation thicknessOf() allowing for calculation of cumulative or "minmax" thickness of horizons matching some criteria

This function calculates the cumulative (method="cumulative", default) or maximum difference between (method="minmax") horizons within a profile that match a defined pattern (pattern) or, more generally, any set of horizon-level logical expressions encoded in a function (FUN).

TODO:

  • add tests
  • add column prefix (optional) for easy joining of various thickness summaries sequentially to site table
  • return pattern (like depthOf()) I actually dont think this is necessary
  • allow for customization of min/max variable names (default to horizonDepths(x))

EDIT: fix for "minmax"

library(aqp)
#> This is aqp 2.0.3
data("jacobs2000")

# cumulative thickness of horizon designations matching "Bt"
thicknessOf(jacobs2000, "Bt")
#>     id thickness
#> 1 92-1       110
#> 2 92-2        99
#> 3 92-3       111
#> 4 92-4         0
#> 5 92-5        26
#> 6 92-6        64
#> 7 92-7         0

# maximum bottom depth minus minimum top depth of horizon designations matching "Bt"
thicknessOf(jacobs2000, "Bt", method = "minmax")
#>     id tmin tmax thickness
#> 1 92-1   43  153       110
#> 2 92-2   46  145        99
#> 3 92-3   64  175       111
#> 4 92-4  Inf -Inf         0
#> 5 92-5  109  135        26
#> 6 92-6  104  168        64
#> 7 92-7  Inf -Inf         0

# cumulative thickness of horizon designations matching "A|B"
thicknessOf(jacobs2000, "A|B")
#>     id thickness
#> 1 92-1       131
#> 2 92-2       117
#> 3 92-3       136
#> 4 92-4        20
#> 5 92-5        54
#> 6 92-6       110
#> 7 92-7        43

# maximum bottom depth minus minimum top depth of horizon designations matching "A|B"
thicknessOf(jacobs2000, "A|B", method = "minmax")
#>     id tmin tmax thickness
#> 1 92-1    0  156       156
#> 2 92-2    0  145       145
#> 3 92-3    0  175       175
#> 4 92-4    0   20        20
#> 5 92-5    0  135       135
#> 6 92-6    0  168       168
#> 7 92-7    0  140       140

# note that the latter includes the thickness of E horizons between the A and the B

# when using a custom function (be sure to accept ... and consider the effect of NA values)

# calculate cumulative thickness of horizons containing >18% clay
thicknessOf(jacobs2000, FUN = function(x, ...) !is.na(x[["clay"]]) & x[["clay"]] > 18)
#>     id thickness
#> 1 92-1       170
#> 2 92-2       167
#> 3 92-3        81
#> 4 92-4         0
#> 5 92-5         0
#> 6 92-6        49
#> 7 92-7         0

@dylanbeaudette
Copy link
Member

Nice idea. I'll do some testing this week,

R/thicknessOf.R Outdated
#' # note that the latter includes the thickness of E horizons between the A and the B
#'
#' # when using a custom function (be sure to accept ... and consider the effect of NA values)
#'
#' # calculate cumulative thickness of horizons containing >18% clay
#' thicknessOf(jacobs2000, FUN = function(x, ...) !is.na(x[["clay"]]) & x[["clay"]] > 18)
#' thicknessOf(jacobs2000, prefix = "claygt18_",
#' FUN = function(x, ...) !is.na(x[["clay"]]) & x[["clay"]] > 18)
#'
thicknessOf <- function(x,
pattern = NULL,
hzdesgn = guessHzDesgnName(x, required = TRUE),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't remember if we wanted to default to guessing (across all functions) when horizon names aren't specified or set in metadata. Currently there is a mixture of approaches across aqp.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you have more recently decided to not guess (which has broken or changed output in several places), but I don't think there is a rule we agreed on. If you are making one then we can start a change here.

The functions for guessing horizon designation, texture class names, and generic attributes are used in several places (pretty much all "my" functions I suppose) I could replace all uses of guessxx() with the metadata getter, and required=TRUE argument, but for now this is consistent with many of my other function usages. Let's address that (deprecating the 3 guess functions) in a separate issue or PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have started to address this in #309, and will make a corresponding fix in this PR. Thanks for prodding me on this, there was lots of room to improve consistency and I am happy to tackle it across all functions.

@dylanbeaudette
Copy link
Member

Tiny request: can you please add in a couple of comments so that I can keep track of the logic. Thanks.

@brownag
Copy link
Member Author

brownag commented Feb 25, 2024

Tiny request: can you please add in a couple of comments so that I can keep track of the logic. Thanks.

Done in a466959

@brownag
Copy link
Member Author

brownag commented Apr 1, 2024

Merging this now, can make any adjustments/additional method demonstrations in future vignette or examples

@brownag brownag merged commit 58187f2 into master Apr 1, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants