|
12 | 12 | #' @examples
|
13 | 13 | #' extractSMR(c("aquic haploxeralfs", "typic epiaqualfs", "humic inceptic eutroperox"))
|
14 | 14 | extractSMR <- function(taxon, as.is = FALSE, droplevels = FALSE, ordered = TRUE) {
|
15 |
| - res <- vapply(taxon, .extractSMR, character(1)) |
| 15 | + |
| 16 | + .get_SMR_element_connotation <- function() { |
| 17 | + data.frame(element = c("ids", |
| 18 | + "per", "aqu", "torr", "ud", "ust", "xer", "sapr", "hem", "fibr", "wass", |
| 19 | + "torri", "ud", "ust", "xer", "aqu", |
| 20 | + "ud", "ust", "xer"), |
| 21 | + level = c("order", |
| 22 | + "suborder", "suborder", "suborder", "suborder", "suborder", "suborder", "suborder", "suborder", "suborder", "suborder", |
| 23 | + "greatgroup", "greatgroup", "greatgroup", "greatgroup", "greatgroup", |
| 24 | + "subgroup", "subgroup", "subgroup"), |
| 25 | + connotation = c("aridic (torric)", |
| 26 | + "perudic", "aquic", "aridic (torric)", "udic", "ustic", "xeric", "aquic", "aquic", "aquic", "peraquic", |
| 27 | + "aridic (torric)", "udic", "ustic", "xeric", "aquic", |
| 28 | + "udic", "ustic", "xeric"), |
| 29 | + stringsAsFactors = FALSE) |
| 30 | + } |
| 31 | + |
| 32 | + # get SMR formative element connotation LUT |
| 33 | + co <- .get_SMR_element_connotation() |
| 34 | + |
| 35 | + res <- vapply(taxon, function(taxon) { |
| 36 | + |
| 37 | + # extract formative elements |
| 38 | + el <- FormativeElements(taxon) |
| 39 | + |
| 40 | + # determine taxon level and position |
| 41 | + el$defs$hierarchy <- level_hierarchy(el$defs$level) |
| 42 | + th <- min(el$defs$hierarchy, na.rm = TRUE) |
| 43 | + |
| 44 | + # only consider SMR formative elements at or below taxon level |
| 45 | + el$defs <- el$defs[grepl(paste0(co$element, collapse = "|"), el$defs$element) & th <= el$defs$level, ] |
| 46 | + maxlevel <- suppressWarnings(max(el$defs$hierarchy, na.rm = TRUE)) |
| 47 | + el$defs <- el$defs[el$defs$hierarchy == maxlevel, ] |
| 48 | + |
| 49 | + # THEN get highest level taxon SMR connotation |
| 50 | + co2 <- co[!is.na(pmatch(co$element, el$defs$element, duplicates.ok = TRUE)) & |
| 51 | + co$level %in% el$defs$level & |
| 52 | + co$level == maxlevel, ] |
| 53 | + nrx <- nrow(co2) |
| 54 | + if (nrx == 1) { |
| 55 | + co2$connotation |
| 56 | + } else NA_character_ |
| 57 | + }, character(1)) |
| 58 | + |
16 | 59 | if (as.is) {
|
17 | 60 | return(res)
|
18 | 61 | }
|
| 62 | + |
19 | 63 | res <- factor(res, levels = SoilMoistureRegimeLevels(as.is = TRUE), ordered = ordered)
|
20 | 64 | if (droplevels) {
|
21 | 65 | return(droplevels(res))
|
22 | 66 | }
|
| 67 | + |
23 | 68 | names(res) <- taxon
|
24 | 69 | res
|
25 | 70 | }
|
26 |
| - |
27 |
| -.extractSMR <- function(taxon) { |
28 |
| - |
29 |
| - # extract formative elements |
30 |
| - el <- FormativeElements(taxon) |
31 |
| - |
32 |
| - # determine taxon level and position |
33 |
| - el$defs$hierarchy <- level_hierarchy(el$defs$level) |
34 |
| - th <- min(el$defs$hierarchy, na.rm = TRUE) |
35 |
| - |
36 |
| - # get SMR formative element connotation LUT |
37 |
| - co <- .get_SMR_element_connotation() |
38 |
| - |
39 |
| - # only consider SMR formative elements at or below taxon level |
40 |
| - el$defs <- el$defs[el$defs$element %in% co$element & th <= el$defs$level,] |
41 |
| - |
42 |
| - # THEN get highest level taxon SMR connotation |
43 |
| - co <- co[co$element %in% el$defs$element & |
44 |
| - co$level %in% el$defs$level & |
45 |
| - co$level == suppressWarnings(max(el$defs$hierarchy, na.rm = TRUE)), ] |
46 |
| - nrx <- nrow(co) |
47 |
| - if (nrx == 1) { |
48 |
| - # todo handle per+aqu and per+ud |
49 |
| - co$connotation |
50 |
| - } else NA_character_ |
51 |
| -} |
52 |
| - |
53 |
| -.get_SMR_element_connotation <- function() { |
54 |
| - # x <- get_ST_formative_elements() |
55 |
| - # x[grepl("SMR|wetness", x$connotation) & x$level != "subgroup",][c("element","level")] |
56 |
| - ## NB: currently there is no formative element connotation for "peraquic" soils |
57 |
| - data.frame(element = c("per", "ids", "aqu", "torr", "ud", "ust", "xer", "torri", "ud", "ust", "xer", "aqu"), |
58 |
| - level = c("suborder", "order", "suborder", "suborder", "suborder", "suborder", "suborder", "greatgroup", "greatgroup", "greatgroup", "greatgroup", "greatgroup"), |
59 |
| - connotation = c("perudic", "aridic (torric)", "aquic", "aridic (torric)", "udic", "ustic", "xeric", "aridic (torric)", "udic", "ustic", "xeric", "aquic"), |
60 |
| - stringsAsFactors = FALSE) |
61 |
| -} |
|
0 commit comments