Skip to content

Commit

Permalink
collapse applyPropx arguments into a single argument. Make default = F
Browse files Browse the repository at this point in the history
  • Loading branch information
andybeet committed Oct 1, 2024
1 parent 7890eec commit 380b5a7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
22 changes: 7 additions & 15 deletions R/aggregate_area.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@
#' 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
#'
#'@inheritParams get_comland_data
#'@param comland Data set generated by \code{get_comland_data}
#'@param userAreas Data frame. Definitions to aggregate statistical areas to user defined 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
#'proportions of landings assigned to new area.
#'
#'@noRd

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

#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]]

Expand Down Expand Up @@ -81,8 +71,9 @@ aggregate_area <- function(comData, userAreas, areaDescription, propDescription,
areas <- data.table::rbindlist(list(areas, div.prop), use.names = T)
}

#Apply proportions to Landings
#Merge new area descriptions to landings
if(applyPropLand){
if(applyProp){
new.area <- merge(comdata, areas, by = c('NESPP3', 'AREA'), all.x = T,
allow.cartesian = T)
} else {
Expand All @@ -98,10 +89,11 @@ aggregate_area <- function(comData, userAreas, areaDescription, propDescription,

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

# Apply proportions to Value
#Drop extra columns and rename
if(applyPropValue){
if(applyProp){
new.area[, newsppvalue := SPPVALUE * prop]
new.area[, c('AREA', 'SPPLIVMT', 'SPPVALUE', 'prop') := NULL]
data.table::setnames(new.area, c('newarea', 'newspplivmt', 'newsppvalue'),
c(areaDescription, 'SPPLIVMT', 'SPPVALUE'))
Expand Down
13 changes: 8 additions & 5 deletions R/get_comland_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
#'@param userAreas data frame. Spatial units in which Statistical areas should be aggregated (eg. \code{\link{mskeyAreas}})
#'@param areaDescription character string. Field name in \code{userAreas} denoting spatial unit. (Default = "EPU")
#'@param propDescription character string. Field name in \code{userAreas} denoting the scaling factor. (Default = "MeanProp")
#'@param applyPropLand boolean. Apply the proportions in userAreas to the landings (Default = F)
#'@param applyPropValue boolean. Apply the proportions in userAreas to the value (Default = F)
#'@param applyProp boolean. Apply the proportions in userAreas to the landings and value (Default = T)
#'@param aggGear boolean. Aggregate NEGEAR codes to larger "fleets" (Default = F)
#'@param userGears data frame. Fleet designations in which NEGEAR codes should be grouped (eg. \code{\link{mskeyGears}})
#'@param fleetDescription character string. Field name in \code{userGears} denoting Fleet. (Default = "Fleet")
Expand Down Expand Up @@ -58,7 +57,7 @@ get_comland_data <- function(channel, filterByYear = NA,
refMonth = NA, disagSkatesHakes = T, aggArea = F,
userAreas = comlandr::mskeyAreas,
areaDescription = 'EPU', propDescription = 'MeanProp',
applyPropLand = T, applyPropValue = T,
applyProp = F,
aggGear = F, userGears = comlandr::mskeyGears,
fleetDescription = 'Fleet',
unkVar = c('MONTH','NEGEAR','AREA'),
Expand Down Expand Up @@ -112,15 +111,19 @@ get_comland_data <- function(channel, filterByYear = NA,
channel,
filterByYear, filterByArea)

saveRDS(comland,here::here("data_raw/data/64preagg.rds"))
#Aggregate areas
if(aggArea) comland <- aggregate_area(comland,
userAreas,
areaDescription,
propDescription,
useForeign,
channel,
applyPropLand,
applyPropValue)
applyProp)

saveRDS(comland,here::here("data-raw/data/64postagg.rds"))


#Aggregate gears
if(aggGear) comland <- aggregate_gear(comland, userGears, fleetDescription)

Expand Down
7 changes: 2 additions & 5 deletions man/get_comland_data.Rd

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

0 comments on commit 380b5a7

Please sign in to comment.