Skip to content

Commit

Permalink
Vectorise c14_calibrate() with engine = "intcal"
Browse files Browse the repository at this point in the history
Also recycle c14_age and c14_error.
  • Loading branch information
joeroe committed Feb 4, 2022
1 parent 221d364 commit 5780314
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
25 changes: 22 additions & 3 deletions R/c14_calibrate.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#' * `"oxcal"`: [oxcAAR::oxcalCalibrate()]
#' * `"bchron"`: [Bchron::BchronCalibrate()]
#'
#' @details
#' `c14_age` and `c14_error` are recycled to a common length.
#'
#' @return A list of `cal` objects.
#' @export
#'
Expand All @@ -25,10 +28,12 @@ c14_calibrate <- function(c14_age,
c14_error,
...,
engine = c("intcal", "rcarbon", "oxcal", "bchron")) {
c(c14_age, c14_error) %<-% vec_recycle_common(c14_age, c14_error)
engine <- rlang::arg_match(engine)


if (engine == "intcal") {
cals <- IntCal::caldist(c14_age, c14_error)
cals <- c14_calibrate_intcal(c14_age, c14_error, ...)
}

else if (engine == "rcarbon") {
Expand All @@ -37,6 +42,7 @@ c14_calibrate <- function(c14_age,
}
cals <- rcarbon::calibrate(c14_age, c14_error, calMatrix = FALSE,
verbose = FALSE, ...)
cals <- as_cal(cals)
}

else if (engine == "oxcal") {
Expand All @@ -45,20 +51,33 @@ c14_calibrate <- function(c14_age,
}
oxcAAR::quickSetupOxcal()
cals <- oxcAAR::oxcalCalibrate(c14_age, c14_error, ...)
cals <- as_cal(cals)
}

else if (engine == "bchron") {
if(!requireNamespace("Bchron", quietly = TRUE)) {
stop('`engine` = "Bchron" requires package Bchron')
}

cals <- Bchron::BchronCalibrate(c14_age, c14_error, ...)
cals <- as_cal(cals)
}

cals <- as_cal(cals)
return(cals)
}

#' Vectorised wrapper for IntCal::caldist
#'
#' @return
#' A cal vector.
#'
#' @noRd
#' @keywords internal
c14_calibrate_intcal <- function(c14_age, c14_error, ...) {
purrr::map2(c14_age, c14_error, IntCal::caldist, ...) |>
purrr::map(as.data.frame) |>
do.call(what = cal)
}

#' Generate the normal distribution of a radiocarbon age
#'
#' @param age Radiocarbon age(s) in years.
Expand Down
3 changes: 3 additions & 0 deletions man/c14_calibrate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5780314

Please sign in to comment.