Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discard documentation cleanup #72

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
Package: comlandr
Title: Pulls and processes commercial fishing data
Title: Pulls and processes commercial fishing data from the Northeast US
Version: 0.3.0
Authors@R: c(person("Sean", "Lucey", email = "[email protected]", role = c("aut","cre")),
person("Andy", "Beet", email = "[email protected]", role = c("aut")))
Authors@R: c(person(given = "Sean",
family = "Lucey",
role = c("aut")),
person(given= "Andy",
family= "Beet",
email = "[email protected]",
role = c("cre","aut"),
comment = c(ORCID = "0000-0001-8270-7090")))
Description: Pulls and processes commercial fishing data (US and NAFO)
URL: https://github.com/NOAA-EDAB/comlandr
BugReports: https://github.com/NOAA-EDAB/comlandr/issues
Expand Down
7 changes: 0 additions & 7 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(adjust_inflation)
export(aggregate_area)
export(aggregate_gear)
export(assign_area)
export(assign_unknown)
export(calc_DK)
export(calc_discards)
export(disaggregate_skates_hakes)
export(get_areas)
export(get_comdisc_data)
Expand All @@ -23,7 +17,6 @@ export(get_species_itis)
export(get_vessels)
export(plot_comland)
export(process_foreign_data)
export(process_foreign_data_skate_hake)
importFrom(data.table,":=")
importFrom(data.table,"as.data.table")
importFrom(data.table,"key")
Expand Down
60 changes: 30 additions & 30 deletions R/aggregate_area.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' Assign landing records to an aggregated area
#'
#' Takes the output from \code{get_comland_data} and further aggregates from NAFO
#' Takes the output from \code{get_comland_data} and further aggregates from NAFO
#' statistical areas to a user defined area. Allows for species to be assigned by
#' proportions to more than two user defined areas from one stat area
#'
Expand All @@ -9,33 +9,33 @@
#' areas
#'@param areaDescription Character. Name of column in userAreas that defines the new
#' area.
#'@param propDescription Character. Name of column in userAreas that defines the
#'@param propDescription Character. Name of column in userAreas that defines the
#' proportions of landings assigned to new area.
#'
#'@export
#'@noRd

aggregate_area <- function(comData, userAreas, areaDescription, propDescription,
useForeign, channel, applyPropLand = T,
useForeign, channel, applyPropLand = T,
applyPropValue = T){

#Pulling data
message("Aggregating Areas ...")

#Add message
if(applyPropLand == F & applyPropValue == T){
message("Can not apply proportions to Value and not Landings -- setting applyPropLand to F")
applyPropLand <- F
}

#Grab just the data
comdata <- comData[[1]]

call <- dbutils::capture_function_call()

#Convert userAreas to data.table
areas <- data.table::as.data.table(userAreas)
data.table::setnames(areas, c(areaDescription, propDescription), c('newarea', 'prop'))

#Figure out NAFO divisions/weightings
if(useForeign){
#Pull NAFO divisions
Expand All @@ -46,10 +46,10 @@ aggregate_area <- function(comData, userAreas, areaDescription, propDescription,
missing54 <- data.table::copy(missingNAFO)[, NAFDVCD := 54]
missing56 <- data.table::copy(missingNAFO)[, NAFDVCD := 56]
NAFOAreas <- data.table::rbindlist(list(NAFOAreas, missing54, missing56))
NAFOAreas <- NAFOAreas[, .(AREA = as.integer(AREA),
NAFOAreas <- NAFOAreas[, .(AREA = as.integer(AREA),
NAFDVCD = as.integer(NAFDVCD))]
areasNAFO <- merge(unique(areas[, c('AREA')]), NAFOAreas, by = 'AREA', all.x = T)

#Calc area weights
#Pull area of stat areas
sf::sf_use_s2(F) #Fixes an error with the stat area shapefile
Expand All @@ -59,62 +59,62 @@ aggregate_area <- function(comData, userAreas, areaDescription, propDescription,
area = sf::st_area(statarea))
statarea.area[, area := as.numeric(area)]
areasNAFO <- merge(areasNAFO, statarea.area, by = 'AREA', all.x = T)

#Get total per division
areasNAFO[, divarea := sum(area, na.rm = T), by = NAFDVCD]

#Calculate strata weight
areasNAFO[, weight := area / divarea]

#Drop extra columns
areasNAFO[, c('area', 'divarea') := NULL]

#Calculate weighted proportions from Stat areas
areas.weighted <- merge(areas, areasNAFO, by = 'AREA', all.x = T, allow.cartesian = T)
div.prop <- areas.weighted[, .(prop = sum(prop * weight, na.rm = T)),
div.prop <- areas.weighted[, .(prop = sum(prop * weight, na.rm = T)),
by = c('NESPP3', 'NAFDVCD', 'newarea')]
#Fix zeros
div.prop[prop == 0, prop := 1]

#Get in the right format and merge
data.table::setnames(div.prop, 'NAFDVCD', 'AREA')
numberCols <- c('AREA', 'prop')
div.prop[, (numberCols):= lapply(.SD, as.numeric), .SDcols = numberCols]
areas <- data.table::rbindlist(list(areas, div.prop), use.names = T)
}

#Merge new area descriptions to landings
if(applyPropLand){
new.area <- merge(comdata, areas, by = c('NESPP3', 'AREA'), all.x = T,
new.area <- merge(comdata, areas, by = c('NESPP3', 'AREA'), all.x = T,
allow.cartesian = T)
} else {
areas[, NESPP3 := NULL]
new.area <- merge(comdata, areas, by = 'AREA', all.x = T, allow.cartesian = T)
}

#If area is known but outside of aggregation set - set to "Other"
new.area[AREA != 0 & is.na(newarea), newarea := 'Other']

#If no proportion assume 100% in
new.area[is.na(prop), prop := 1]

#Proportion landings to new areas
new.area[, newspplivmt := SPPLIVMT * prop]
if(applyPropValue) new.area[, newsppvalue := SPPVALUE * prop]

#Drop extra columns and rename
if(applyPropValue){
new.area[, c('AREA', 'SPPLIVMT', 'SPPVALUE', 'prop') := NULL]
data.table::setnames(new.area, c('newarea', 'newspplivmt', 'newsppvalue'),
data.table::setnames(new.area, c('newarea', 'newspplivmt', 'newsppvalue'),
c(areaDescription, 'SPPLIVMT', 'SPPVALUE'))
} else {
new.area[, c('AREA', 'SPPLIVMT', 'prop') := NULL]
data.table::setnames(new.area, c('newarea', 'newspplivmt'),
data.table::setnames(new.area, c('newarea', 'newspplivmt'),
c(areaDescription, 'SPPLIVMT'))
}

#Aggregate to new areas
catch.var <- names(new.area)[which(!names(new.area) %in% c('SPPLIVMT',
catch.var <- names(new.area)[which(!names(new.area) %in% c('SPPLIVMT',
'SPPVALUE'))]
#Discard data does not have value so need to ensure this runs on both
if(length(which(names(new.area) == 'SPPVALUE')) > 0){
Expand All @@ -123,12 +123,12 @@ aggregate_area <- function(comData, userAreas, areaDescription, propDescription,
} else {
new.area <- new.area[, .(SPPLIVMT = sum(SPPLIVMT)), by = catch.var]
}

#
#Add changes back into comData
comData[[1]] <- new.area[]
comData$call <- c(comData$call, call)
comData$userAreas <- userAreas

return(comData[])
}
}
40 changes: 20 additions & 20 deletions R/aggregate_gear.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
#' Assign landing records to an aggregated gear fleet
#'
#' Takes the output from \code{get_comland_data} or \code{get_comdisc_data} and
#' Takes the output from \code{get_comland_data} or \code{get_comdisc_data} and
#' further aggregates from NEGEAR2 codes to a user defined fleet.
#'
#'@param comdata Data set generated by \code{get_comland_data} or
#'@param comdata Data set generated by \code{get_comland_data} or
#' \code{get_comdisc_data}
#'@param userGears Data frame. Definitions to aggregate NEGEAR2 to user defined
#' gears
#'@param fleetDescription Character. Name of column in userGears that defines
#'@param fleetDescription Character. Name of column in userGears that defines
#' the new gears.
#'
#'@export
#'@noRd

aggregate_gear <- function(comData, userGears, fleetDescription){

#Pulling data
message("Aggregating Gears ...")

#Grab just the data
comdata <- data.table::copy(comData[[1]])

call <- dbutils::capture_function_call()

#Convert userGears to data.table
gears <- data.table::as.data.table(userGears)
gears <- data.table::setnames(gears, fleetDescription, 'fleet')

#Assign gears to fleets
#Generate NEGEAR2 codes from NEGEAR
if(is.numeric(comdata$NEGEAR)){
Expand All @@ -36,9 +36,9 @@ aggregate_gear <- function(comData, userGears, fleetDescription){
} else {
comdata[, NEGEAR2 := as.numeric(substr(NEGEAR, 1, 2))]
}

fleets <- unique(gears$fleet)

for(ifleet in 1:length(fleets)){
fleet.gear <- gears[fleet == fleets[ifleet], NEGEAR2]
fleet.mesh <- unique(gears[fleet == fleets[ifleet], MESHCAT])
Expand All @@ -49,16 +49,16 @@ aggregate_gear <- function(comData, userGears, fleetDescription){
comdata[NEGEAR2 %in% fleet.gear & MESHCAT == fleet.mesh, fleet := fleets[ifleet]]
}
}

comdata[, fleet := as.factor(fleet)]

#Drop extra columns and rename
comdata[, c('NEGEAR2', 'NEGEAR', 'MESHCAT') := NULL]
data.table::setnames(comdata, 'fleet', fleetDescription)

#Aggregate over new gears
#Aggregate to new fleets
catch.var <- names(comdata)[which(!names(comdata) %in% c('SPPLIVMT',
catch.var <- names(comdata)[which(!names(comdata) %in% c('SPPLIVMT',
'SPPVALUE'))]
#Discard data does not have value so need to ensure this runs on both
if(length(which(names(comdata) == 'SPPVALUE')) > 0){
Expand All @@ -67,13 +67,13 @@ aggregate_gear <- function(comData, userGears, fleetDescription){
} else {
comdata <- comdata[, .(SPPLIVMT = sum(SPPLIVMT)), by = catch.var]
}


#Add changes back into comdata
comData[[1]] <- comdata[]
comData$call <- c(comdata$call, call)
comData$userGears <- userGears

return(comData[])
}
}

Loading
Loading