#' @title Aggregates OWNCD from *_COND files
#' @description TODO this
#'
#' @param dir Directory for *_COND files
#' @param CNs vector of PLT_CNs to aggregate by
#'
#' @export
#' @examples PullOwnerCode(CNs = FIA_mortality_with_explanatory$PLT_CN)
PullOwnerCode <- function(CNs, dir = getwd(), db_ver = 7.2, quietly = T) {
# Import data:
# 'C:/Users/Brandon/Documents/docs/PHD/FIA/data'
if (db_ver != 7.2) stop('only checked for db ver 7.2')
cond_df <- ImportConditions(dir = dir, filt_PLT_CN = CNs)
if (nrow(cond_df) < 1) stop('ImportConditions failed')
out_df <- data.frame(unique(cond_df$PLT_CN), stringsAsFactors = F)
colnames(out_df)[1] <- 'PLT_CN'
out_df$OWNCD <- NA
cat('\n')
if (quietly) sink(file = tempfile())
for (i in out_df$PLT_CN) {
i_sub <- cond_df[which(cond_df$PLT_CN == i), ]
if (nrow(i_sub) == 1) {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- i_sub$OWNCD
next
}
if (length(unique(i_sub$OWNCD)) == 1) {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- unique(i_sub$OWNCD)
next
}
if (all(i_sub$OWNCD < 31)) {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- 25
next
}
if (all(i_sub$OWNCD > 25) && (all(i_sub$OWNCD < 46))) {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- 33
next
}
if (all(i_sub$OWNCD < 46)) {
# If mixed state-federal, majority rule with tiebreakers to federal
ii <- i_sub[which(i_sub$OWNCD < 31)]
if (sum(ii$CONDPROP_UNADJ) < 0.5) {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- 33
cat('Mixed state/federal plot:', i, ', assigned as:', 33, '\n')
} else {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- 25
cat('Mixed state/federal plot:', i, ', assigned as:', 25, '\n')
}
next
}
if (any(i_sub$OWNCD == 46) && any(i_sub$OWNCD < 46)) {
# If mixed private-public, majority rule by CONDPROP_UNADJ:
ii <- i_sub[which(i_sub$OWNCD == 46), ]
if (sum(ii$CONDPROP_UNADJ) > 0.5) {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- 46
cat('Mixed private/public plot:', i, ', assigned as:', 46, '\n')
} else {
jj <- i_sub[which(i_sub$OWNCD %in% c(31, 32, 33)), ]
if (sum(jj$CONDPROP_UNADJ) > 0.5) {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- 33
cat('Mixed private/public plot:', i, ', assigned as:', 33, '\n')
} else {
out_df[which(out_df$PLT_CN == i), 'OWNCD'] <- 25
cat('Mixed private/public plot:', i, ', assigned as:', 25, '\n')
}
}
next
}
}
if (quietly) sink()
out_df$is_public <- KeyOwnerCode(out_df$OWNCD, ret = 'logical', db_ver = 7.2)
cat('\n')
if (sum(is.na(out_df$OWNCD)) > 0) {
stop('missing OWNCDs in aggregated table')
}
return(out_df)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.