Skip to content

Commit

Permalink
FIX(PullCatch.PacFIN): nominal code wasn't working for some species
Browse files Browse the repository at this point in the history
* pulled out nominal code into PullNominal.PacFIN
* much better code to search for nominal code
* create an issue regarding should other types of catches be included? #55
  • Loading branch information
kellijohnson-NOAA committed Feb 23, 2021
1 parent 5123e21 commit 8b32b2a
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 17 deletions.
37 changes: 23 additions & 14 deletions R/PullCatch.PacFIN.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
#' specify the species and the nominal species code without searching for
#' it. This is helpful for when you are getting data for two species and
#' you only want nominal catch for one or if you only wanted nominal catch.
#' Nominal species code will be searched for using [PullNominal.PacFIN] if
#' the input value for `addnominal` is `TRUE`.
#' @template verbose
#'
#' @export
#' @author John R. Wallace, Kelli Faye Johnson
#' @author Kelli Faye Johnson
#' @seealso [PullNominal.PacFIN] for how nominal species codes are determined.
#' @return RData frames are saved to the disk and the pulled data
#' are returned as a data frame.
#' * CompFT - pulled data
Expand All @@ -30,27 +35,31 @@
#'
PullCatch.PacFIN <- function(pacfin_species_code,
username = getUserName("PacFIN"), password, savedir = getwd(),
addnominal = TRUE) {
addnominal = TRUE, verbose = FALSE) {

inputcode <- pacfin_species_code

#### Pull from PacFIN
if (missing(password)) {
password <- readline(prompt = "Enter PacFIN password without quotes\n")
}
ar <- getDB(sql.area(),
username = username, password = password)

# Find nominal code if there is one and the user hasn't passed it
if (addnominal) {
spp <- getDB(sql.species(),
username = username, password = password)
pacfin_species_code <- spp[
grepl(paste0(collapse = "|", gsub("NOM. ", "",
spp[
match(pacfin_species_code, spp[["PACFIN_SPECIES_CODE"]]),
"PACFIN_SPECIES_COMMON_NAME"])),
spp[, "PACFIN_SPECIES_COMMON_NAME"]
), "PACFIN_SPECIES_CODE"]
if (addnominal[1] == TRUE) {
thenominal <- PullNominal.PacFIN(
pacfin_species_code = pacfin_species_code,
username = username,
password = password)
pacfin_species_code <- c(pacfin_species_code, na.omit(thenominal))
if (verbose) {
message("The following nominal species codes were added: ",
knitr::combine_words(thenominal)
)
}
} else { # Add the nominal code passed by the user unless FALSE
if (addnominal[1] != FALSE) {
pacfin_species_code <- c(pacfin_species_code, addnominal)
}
}
data <- getDB(sql.catch(pacfin_species_code),
username = username, password = password)
Expand Down
56 changes: 56 additions & 0 deletions R/PullNominal.PacFIN.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Pull the species information table and return nominal code
#'
#' Pull the species information table with PACFIN_SPECIES_CODE and
#' PACFIN_COMMON_NAME from PacFIN to determine the nominal code for
#' a given species code.
#'
#' @template pacfin_species_code
#' @template username
#' @template password
#' @importFrom magrittr %>%
#' @return A vector of character values, where each value is a
#' PACFIN_SPECIES_CODE that pertains to the input `pacfin_species_code`.
#' `NA` is returned if no values are found.
#'
PullNominal.PacFIN <- function(pacfin_species_code, username, password) {

spp <- getDB(sql.species(),
username = username, password = password
)

nom <- spp[grepl("NOM\\.", spp[,2]), ]

thenominal <- tibble::tibble(spp) %>%
dplyr::mutate(searchname = gsub(
"BLACK AND YELLOW",
"BLACK-AND-YELLOW",
PACFIN_SPECIES_COMMON_NAME
)) %>%
dplyr::mutate(searchname = gsub(
"CALIFORNIA HALIBUT",
"CALIF HALIBUT",
searchname
)) %>%
dplyr::mutate(searchname = gsub(
"PACIFIC OCEAN PERCH",
"POP",
searchname
)) %>%
dplyr::mutate(searchname = gsub(
"(CHILIPEPPER|SQUARESPOT|VERMILION) ROCKFISH",
"\\1",
searchname
)) %>%
dplyr::mutate(nominal = purrr::map_chr(searchname,
~paste0(grep(.x, nom[, 2], value = TRUE), collapse = "|"))) %>%
dplyr::mutate(code = purrr::map(nominal, ~{
if (.x[1] == "") return(NA)
return(nom[grep(.x, nom[, 2]), 1])
})) %>%
dplyr::filter(PACFIN_SPECIES_CODE == pacfin_species_code) %>%
dplyr::pull(code) %>%
unlist()

return(thenominal)

}
22 changes: 19 additions & 3 deletions man/PullCatch.PacFIN.Rd

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

31 changes: 31 additions & 0 deletions man/PullNominal.PacFIN.Rd

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

0 comments on commit 8b32b2a

Please sign in to comment.