Skip to content

Commit

Permalink
load update
Browse files Browse the repository at this point in the history
  • Loading branch information
tymjackson committed Oct 14, 2024
1 parent 491b7a7 commit e0470a7
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .Rproj.user/shared/notebooks/paths
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
C:/Users/tmjackson/Documents/BSAIcrabR/R/add_calc_wt.R="C7E093D6"
C:/Users/tmjackson/Documents/BSAIcrabR/R/add_crab_year.R="557BAC7E"
C:/Users/tmjackson/Documents/BSAIcrabR/R/add_legal.R="912948E2"
C:/Users/tmjackson/Documents/BSAIcrabR/R/clean_lat_lon.R="3259A04C"
C:/Users/tmjackson/Documents/BSAIcrabR/R/get_avg_wt.R="1761609E"
C:/Users/tmjackson/Documents/BSAIcrabR/R/get_discards.R="72FE693C"
C:/Users/tmjackson/Documents/BSAIcrabR/R/get_dockside_comp.R="10AEE7AD"
C:/Users/tmjackson/Documents/BSAIcrabR/R/get_observer_comp.R="CD7A873D"
C:/Users/tmjackson/Documents/BSAIcrabR/R/get_retained_catch.R="DAC15420"
Expand All @@ -10,7 +12,11 @@ C:/Users/tmjackson/Documents/BSAIcrabR/R/load_crab_dump.R="497ED5C0"
C:/Users/tmjackson/Documents/BSAIcrabR/R/load_dockside.R="BD7E1529"
C:/Users/tmjackson/Documents/BSAIcrabR/R/load_pot_dump.R="EE044C52"
C:/Users/tmjackson/Documents/BSAIcrabR/_pkgdown.yml="D934123F"
C:/Users/tmjackson/Documents/adfg_crab_observer/aigkc/code/aigkc_data_request.R="EB16A046"
C:/Users/tmjackson/Documents/adfg_crab_observer/bbrkc/bbrkc_data_summary_example.Rmd="8AE866D6"
C:/Users/tmjackson/Documents/adfg_crab_observer/bbrkc/code/bbrkc_BSAIcrabR_workflow.R="5E859338"
C:/Users/tmjackson/Documents/adfg_crab_observer/misc/code/custom_functions.R="8CFCC967"
C:/Users/tmjackson/Documents/adfg_crab_observer/misc/data/weight_parameters.csv="560DF23B"
C:/Users/tmjackson/Documents/adfg_crab_observer/pigkc/code/pigkc_data_request.R="DEEE6C75"
C:/Users/tmjackson/Documents/adfg_crab_observer/smbkc/code/smbkc_data_request.R="7A9A0835"
C:/Users/tmjackson/Documents/adfg_crab_observer/tanner_crab/code/tanner_crab_data_request.R="BCD9F781"
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
export(add_calc_wt)
export(add_crab_year)
export(add_legal)
export(clean_lat_lon)
export(get_avg_wt)
export(get_discards)
export(get_dockside_comp)
export(get_observer_comp)
export(get_retained_catch)
Expand Down
18 changes: 18 additions & 0 deletions R/clean_lat_lon.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' Clean Lat and Lon
#'
#' Clean latitude and longitude data across the dateline
#' @param x Pot or observer data with columns longitude, latitude, and eastwest
#' @return x with location information correctect across the date line to be negative
#' @examples
#' clean_lat_lon(pots)
#' @export
clean_lat_lon <- function(x) {

x %>%
mutate(longitude = ifelse(longitude > 0, longitude * -1, longitude),
longitude = ifelse(longitude == -9, NA, longitude),
longitude = ifelse(eastwest == "E", (-180 - longitude) + -180, longitude)) -> out

return(out)

}
54 changes: 54 additions & 0 deletions R/get_discards.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#' Get Directed Discards
#'
#' Estimate directed fishery discards by sex using subtraction method

#' @param retained_catch NULL. Output of `get_retaied_catch()`.
#' @param total_catch NULL. Output of `get_total_catch()`. 'by' argument could be anything as long as group or sex is included.
#' @param stock NULL. Character string stock abbreviation: BSSC, WBT, EBT, BBRKC, EAG, WAG, PIGKC, SMBKC, PIBKC, PIRKC, WAIRKC.
#' @return Data frame including crab year, fishery, sex, discards, and discard mortality.
#' @examples get_discards(retained_catch, total_catch, stock = "BBRKC", handling_mortality = 0.3)
#'
#' @export
#'
get_discards <- function(retained_catch, total_catch, stock, handling_mortality = 0.3) {

# directed fishery codes
tibble(stock_abbrev = c("BSSC", "WBT", "EBT", "BBRKC", "EAG", "WAG", "PIGKC", "SMBKC", "PIBKC", "PIRKC", "WAIRKC"),
fish_code = c("QO", "QT", "TT", "TR", "OB", "RB", "QB", "QP", "QP", "QR", "RR")) %>%
filter(stock_abbrev == stock) %>%
pull(fish_code) -> fish_code

# summarise total catch
if("group" %in% names(total_catch)){
total_catch %>%
# add sex
mutate(sex = case_when(group == "female" ~ 2,
group == "sublegal_male"~ 1,
group == "legal_male"~ 1)) -> total_catch
}

if(sum(grepl("group|sex", names(total_catch))) == 0){
stop("Total catch must be by 'group', 'sex', or both !!")
}

total_catch %>%
# filter for the directd fishery
filter(substring(fishery, 1, 2) == fish_code) %>%
group_by(crab_year, fishery, sex) %>%
summarise(total_catch_n = sum(total_catch_n, na.rm = T),
total_catch_wt = sum(total_catch_wt, na.rm = T)) %>% ungroup %>%
# join to retained catch
full_join(retained_catch %>%
mutate(sex = 1), by = c("crab_year", "fishery", "sex")) %>%
mutate(retained_n = ifelse(sex == 2, 0, retained_n),
retained_wt = ifelse(sex == 2, 0, retained_wt)) %>%
# add discards
mutate(discard_n = total_catch_n - retained_n,
discard_wt = total_catch_wt - retained_wt,
discard_mortality_n = discard_n * handling_mortality + retained_n,
discard_mortality_wt = discard_wt * handling_mortality + retained_wt) -> out

return(out)

}

48 changes: 39 additions & 9 deletions R/load_crab_dump.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ load_crab_dump <- function(path, stock, database_pull = F, clean = T) {

obs %>%
rename(sample_date = sampdate) %>%
{if(!("subdistrict" %in% names(.))){mutate(subdistrict = NA) %>% .} else{.}} %>%
{if(!("eastwest" %in% names(.))){mutate(eastwest = NA) %>% .} else{.}} %>%
{if(!("maturity" %in% names(.))){mutate(maturity = NA) %>% .} else{.}} %>%
{if(!("gearcode" %in% names(.))){mutate(gearcode = NA) %>% .} else{.}} %>%
# reorder
transmute(fishery, trip, adfg, sample_date, spn, statarea, latitude, longitude,
eastwest, depth, soaktime, gearcode = ifelse("gearcode" %in% names(.), gearcode, NA), ring, mesh, biotwine_ok, spcode, sex, size, legal, shell, clutch, eggdev,
clutchcon, maturity = ifelse("maturity" %in% names(.), maturity, NA), parasite) -> out
transmute(fishery, trip, adfg, sample_date, spn, statarea, subdistrict, latitude, longitude,
eastwest, depth, soaktime, gearcode, ring, mesh, biotwine_ok, spcode, sex, size, legal, shell, clutch, eggdev,
clutchcon, maturity, parasite) -> out
if(clean == T){
# stock specific
if(stock == "BBRKC"){
Expand All @@ -35,40 +39,66 @@ load_crab_dump <- function(path, stock, database_pull = F, clean = T) {
# filter EI and QT fisheries in early 90s by stat areas e166
filter(!(fishery %in% early_90s_tt & (statarea > 660000 | statarea < 0))) %>%
# combine all tanner e166 fishery codes
mutate(fishery = ifelse(fishery %in% early_90s_tt, gsub("EI|QT", "TT", fishery), fishery)) %>%
mutate(fishery = ifelse(fishery %in% early_90s_tt, gsub("EI|QT", "TT", fishery), fishery),
fishery = paste0(substring(fishery, 1, 2), substring(crab_year, 3, 4))) %>%
# fill in legal
add_legal(., stock = stock) %>%
# add regulatory group
mutate(group = case_when(sex == 2 ~ "female",
sex == 1 & legal == 0 ~ "sublegal_male",
sex == 1 & legal == 1 ~ "legal_male")) -> out
sex == 1 & legal == 1 ~ "legal_male")) %>%
dyply::select(-subdistrict) -> out
}
if(stock == "BSSC") {
if(stock %in% c("BSSC", "BSTC", "WBT", "EBT")) {
## fishery codes for early 90s tanner e166 fisheries
early_90s_tt <- c("EI91", "EI92", paste0("QT", 93:96))
## data mgmt specific to bssc
out %>%
# fix transition to rationalization yr
mutate(fishery = gsub("QO05r", "QO05", fishery),
fishery = gsub("QO05o", "QO04", fishery),
# bbrkc test fish and cdq fisheries to TR
fishery = gsub("CO|EO", "QO", fishery),
# cdq rkc and bkc fisheries to PIBKC
fishery = gsub("CK", "QP", fishery),
# bbrkc test fish and cdq fisheries to TR
fishery = gsub("XR|CR", "TR", fishery),
fishery = ifelse((fishery %in% early_90s_tt) & (statarea > 660000 | statarea < 0), paste0("QT", substring(fishery, 3, 4)), fishery),
fishery = ifelse((fishery %in% early_90s_tt) & (statarea <= 660000 | statarea >= 0), paste0("TT", substring(fishery, 3, 4)), fishery)) %>%
fishery = ifelse((fishery %in% early_90s_tt) & (statarea <= 660000 | statarea >= 0), paste0("TT", substring(fishery, 3, 4)), fishery),
fishery = paste0(substring(fishery, 1, 2), substring(crab_year, 3, 4))) %>%
# fill in legal
add_legal(., stock = stock) %>%
# add regulatory group
mutate(group = case_when(sex == 2 ~ "female",
sex == 1 & legal == 0 ~ "sublegal_male",
sex == 1 & legal == 1 ~ "legal_male")) %>%
dyply::select(-subdistrict) -> out
}
if(stock %in% c("AIGKC", "EAG", "WAG")) {
## data mgmt specific to gkc
out %>%
# fill in legal
add_legal(., stock = stock) %>%
# add regulatory group
mutate(group = case_when(sex == 2 ~ "female",
sex == 1 & legal == 0 ~ "sublegal_male",
sex == 1 & legal == 1 ~ "legal_male")) -> out
}
if(stock == "EAG") {
out %>%
filter(subdistrict == "EAG") -> out
}
if(stock == "WAG") {
out %>%
filter(subdistrict == "WAG") -> out
}
if(stock == "PIGKC") {
out %>%
mutate(fishery = gsub("CO|EO", "QO", fishery)) %>%
dyply::select(-subdistrict) -> out
}


if(stock %in% c("BSTC", "WBT", "EBT", "AIGKC", "EAG", "WAG", "PIGKC", "SMBKC", "PIBKC", "PIRKC", "WAIRKC")){
if(stock %in% c("SMBKC", "PIBKC", "PIRKC", "WAIRKC")){
stop(paste0("No method for ", stock, " yet !!"))
}

Expand Down
40 changes: 33 additions & 7 deletions R/load_dockside.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ load_dockside <- function(path, stock, database_pull = F, clean = T) {
dock %>%
# add crab year
add_crab_year() %>%
{if(!("subdistrict" %in% names(.))){mutate(subdistrict = NA) %>% .} else{.}} %>%
# reorder
transmute(crab_year, fishery, adfg, sample_date, spcode, size, legal, shell, numcrab) -> out
transmute(crab_year, fishery, adfg, sample_date, subdistrict, spcode, size, legal, shell, numcrab) -> out

if(clean == T){
# stock specific
if(stock == "BBRKC"){
out %>%
mutate(fishery = gsub("XR|CR", "TR", fishery)) %>%
filter(substring(fishery, 1, 2) == "TR") -> out
mutate(fishery = gsub("XR|CR", "TR", fishery),
fishery = paste0(substring(fishery, 1, 2), substring(crab_year, 3, 4))) %>%
filter(substring(fishery, 1, 2) == "TR") %>%
dyply::select(-subdistrict) -> out
}
if(stock == "BSSC"){
early_90s_tt <- c("EI91", "EI92", paste0("QT", 93:96))
if(stock %in% c("BSSC", "BSTC", "EBT", "WBT")){
early_90s_tt <- c("EI89", "EI90", "EI91", "EI92", paste0("QT", 93:96))
out %>%
# fix transition to rationalization yr
# cdq and eo fisheries to QO
Expand All @@ -44,11 +47,34 @@ load_dockside <- function(path, stock, database_pull = F, clean = T) {
fishery = gsub("CO|EO", "QO", fishery),
fishery = gsub("XR|CR", "TR", fishery),
fishery = ifelse(fishery %in% early_90s_tt, paste0("QT", substring(fishery, 3, 4)), fishery),
fishery = ifelse(fishery %in% c("EO91", "EO92"), paste0(substring(fishery, 1, 2), as.numeric(substring(fishery, 3, 4))-1), fishery)) -> out
fishery = paste0(substring(fishery, 1, 2), substring(crab_year, 3, 4))) %>%
dyply::select(-subdistrict) -> out
}
if(stock %in% c("AIGKC", "EAG", "WAG")){
out %>%
# make XE fisheries EAG
mutate(subdistrict = ifelse(substring(fishery, 1, 2) == "XE", "EAG", subdistrict)) %>%
# fix subdistrict for OB08
mutate(subdistrict = case_when(crab_year != 2008 ~ subdistrict,
(crab_year == 2008 & adfg %in% c(35767, 37887)) ~ "WAG",
(crab_year == 2008 & adfg %in% c(103, 5992, 20556)) ~ "EAG",
(crab_year == 2008 & adfg == 5992 & sample_date > as_date("2008-12-1")) ~ "WAG")) -> out
}
if(stock == "EAG"){
out %>%
filter(subdistrict == "EAG") -> out
}
if(stock == "WAG"){
out %>%
filter(subdistrict == "WAG") -> out
}
if(stock %in% c("PIGKC")){
out %>%
dyply::select(-subdistrict) -> out
}


if(stock %in% c("BSTC", "WBT", "EBT", "AIGKC", "EAG", "WAG", "PIGKC", "SMBKC", "PIBKC", "PIRKC", "WAIRKC")){
if(stock %in% c("SMBKC", "PIBKC", "PIRKC", "WAIRKC")){
stop(paste0("No method for ", stock, " yet !!"))
}
}
Expand Down
38 changes: 32 additions & 6 deletions R/load_pot_dump.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ load_pot_dump <- function(path, stock, database_pull = F, clean = T) {
mutate(biotwine_ok = case_when(biotwine_ok == "-" ~ NA,
biotwine_ok %in% c("n", "N") ~ "N",
biotwine_ok %in% c("y", "Y") ~ "Y")) %>%
{if(!("subdistrict" %in% names(.))){mutate(subdistrict = NA) %>% .} else{.}} %>%
{if(!("eastwest" %in% names(.))){mutate(eastwest = NA) %>% .} else{.}} %>%
rename(sample_date = sampdate) %>%
# reorder
transmute(fishery, trip, adfg, sample_date, spn, statarea, latitude, longitude,
eastwest = ifelse("eastwest" %in% names(.), eastwest, NA), depth, soaktime, gearcode, ring, mesh, biotwine_ok, female, sublegal, tot_legal, msr_pot) -> out
transmute(fishery, trip, adfg, sample_date, spn, statarea, subdistrict, latitude, longitude, eastwest, depth, soaktime, gearcode, ring, mesh, biotwine_ok, female, sublegal, tot_legal, msr_pot) -> out
if(clean == T){
# stock specific
if(stock == "BBRKC"){
Expand All @@ -38,9 +39,11 @@ load_pot_dump <- function(path, stock, database_pull = F, clean = T) {
# filter EI and QT fisheries in early 90s by stat areas e166
filter(!(fishery %in% early_90s_tt & (statarea > 660000 | statarea < 0))) %>%
# combine all tanner e166 fishery codes
mutate(fishery = ifelse(fishery %in% early_90s_tt, gsub("EI|QT", "TT", fishery), fishery)) -> out
mutate(fishery = ifelse(fishery %in% early_90s_tt, gsub("EI|QT", "TT", fishery), fishery),
fishery = paste0(substring(fishery, 1, 2), substring(crab_year, 3, 4))) %>%
dyply::select(-subdistrict) -> out
}
if(stock == "BSSC") {
if(stock %in% c("BSSC", "BSTC", "WBT", "EBT")) {
## fishery codes for early 90s tanner e166 fisheries
early_90s_tt <- c("EI91", "EI92", paste0("QT", 93:96))
## data mgmt specific to bssc
Expand All @@ -55,11 +58,34 @@ load_pot_dump <- function(path, stock, database_pull = F, clean = T) {
# bbrkc test fish and cdq fisheries to TR
fishery = gsub("XR|CR", "TR", fishery),
fishery = ifelse((fishery %in% early_90s_tt) & (statarea > 660000 | statarea < 0), paste0("QT", substring(fishery, 3, 4)), fishery),
fishery = ifelse((fishery %in% early_90s_tt) & (statarea <= 660000 | statarea >= 0), paste0("TT", substring(fishery, 3, 4)), fishery)) -> out
fishery = ifelse((fishery %in% early_90s_tt) & (statarea <= 660000 | statarea >= 0), paste0("TT", substring(fishery, 3, 4)), fishery),
fishery = paste0(substring(fishery, 1, 2), substring(crab_year, 3, 4))) %>%
dyply::select(-subdistrict) -> out
}
if(stock %in% c("AIGKC", "EAG", "WAG")) {
## data mgmt specific to gkc
out %>%
clean_lat_lon() %>%
mutate(subdistrict = ifelse(fishery == "OB08" & longitude < -174, "WAG", subdistrict)) %>%
# remove pots without district info
filter(subdistrict != "-") -> out
}
if(stock == "EAG") {
out %>%
filter(subdistrict == "EAG") -> out
}
if(stock == "WAG") {
out %>%
filter(subdistrict == "WAG") -> out
}
if(stock == "PIGKC") {
out %>%
mutate(fishery = gsub("CO|EO", "QO", fishery)) %>%
dyply::select(-subdistrict) -> out
}


if(stock %in% c("BSTC", "WBT", "EBT", "AIGKC", "EAG", "WAG", "PIGKC", "SMBKC", "PIBKC", "PIRKC", "WAIRKC")){
if(stock %in% c("SMBKC", "PIBKC", "PIRKC", "WAIRKC")){
stop(paste0("No method for ", stock, " yet !!"))
}

Expand Down
20 changes: 20 additions & 0 deletions man/clean_lat_lon.Rd

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

25 changes: 25 additions & 0 deletions man/get_discards.Rd

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

4 changes: 2 additions & 2 deletions man/get_total_catch.Rd

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

0 comments on commit e0470a7

Please sign in to comment.