Skip to content

Commit

Permalink
included a check_argument_validation function to alert user to misspe…
Browse files Browse the repository at this point in the history
…cified arguments prior to pull.

- added function call in get_comland_data
  • Loading branch information
andybeet committed Oct 1, 2024
1 parent 560b113 commit 3143f36
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 10 deletions.
107 changes: 107 additions & 0 deletions R/check_argument_validation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#' Argument Flag check
#'
#' check to make sure arguments passed bby user do not contradict each other.
#'
#' @inheritParams get_comland_data
#'
#' @noRd

# We can either create error messages (like below) or fix argument specs

check_argument_validation <- function(aggArea,
userAreas,
areaDescription,
propDescription,
applyProp,
aggGear,
userGears,
fleetDescription,
unkVar,
knStrata) {

############## AggArea ###############

# if aggArea = T then userAreas, areaDescription and propDescription
# follow rules
if(aggArea) {
# field names of userAreas
if(!(areaDescription %in% names(userAreas))) {
stop(paste0(areaDescription, " is not a field name in userAreas object"))
}
if(!(propDescription %in% names(userAreas))) {
stop(paste0(propDescription, " is not a field name in userAreas object"))
}
if(!("AREA" %in% names(userAreas))) {
stop(paste0("AREA is not a field name in userAreas object.
This object is used to aggregate Statistical Areas to larger regional units.
Field names must include: AREA and 'areaDescription' "))
}
}

############## AggGear ###############

# if aggGear = T then userGears, fleetDescription follow rules
if(aggGear) {
# field names of userGears
if(!(fleetDescription %in% names(userGears))) {
stop(paste0(fleetDescription, " is not a field name in userGears object"))
}
}

############## applyProp ###############

# applyProp currently relies on a specific format of userAreas
if(applyProp) {
if (!aggArea) {
stop("Can not have 'aggArea = F' if you want to propotion landings ('applyProp = T') by
statistical area to a larger spatial unit")
}

# if aggArea = T hen these conditions should be met
if(!("NESPP3" %in% names(userAreas))) {
stop(paste0("NESPP3 is not a field name in userAreas object.
This object is used to aggregate Statistical Areas to larger regional units and
proportion landings to these larger regional unit depening on the species and Statistical area.
Field names must include: NESPP3, AREA and 'areaDescription' "))
}

stop("Proportion allocation is currently not implemented correctly.
Please use 'applyProp = F'")

}

################ UNKNOWNS #################

# checks for filling in missing values (assign_unknowns)
# Depending on the flags above will determine how the arguments unkVar and knStrata are defined
if (aggArea) {
if (!(areaDescription %in% unkVar) | !(areaDescription %in% knStrata)) {
stop(paste0("To assign unknowns when using 'aggArea = T', then you need to replace
'AREA' with '" ,areaDescription,"' in both 'unkVar' and 'knStrata' arguments"))
}
} else {
if (!("AREA" %in% unkVar) | !("AREA" %in% knStrata)) {
stop(paste0("To assign unknowns when using 'aggArea = F', then you need to use
'AREA' in both 'unkVar' and 'knStrata' arguments"))
}

}

if (aggGear) {
if (!(fleetDescription %in% unkVar) | !(fleetDescription %in% knStrata)) {
stop(paste0("To assign unknowns when using 'aggGear = T', then you need to replace
'NEGEAR' with '" ,fleetDescription,"' in both 'unkVar' and 'knStrata' arguments"))
}
} else {
if (!("NEGEAR" %in% unkVar) | !("NEGEAR" %in% knStrata)) {
stop(paste0("To assign unknowns when using 'aggGear = F', then you need to use
'NEGEAR' in both 'unkVar' and 'knStrata' arguments"))
}
}




# check for character or numeric

}
39 changes: 29 additions & 10 deletions R/get_comland_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,23 @@
#'@export


get_comland_data <- function(channel, filterByYear = NA,
filterByArea = NA, useLanded = T, removeParts = T,
useHerringMaine = T, useForeign = T, refYear = NA,
refMonth = NA, disagSkatesHakes = T, aggArea = F,
get_comland_data <- function(channel,
filterByYear = NA,
filterByArea = NA,
useLanded = T,
removeParts = T,
useHerringMaine = T,
useForeign = T,
refYear = NA,
refMonth = NA,
disagSkatesHakes = T,
aggArea = F,
userAreas = comlandr::mskeyAreas,
areaDescription = 'EPU', propDescription = 'MeanProp',
areaDescription = 'EPU',
propDescription = 'MeanProp',
applyProp = F,
aggGear = F, userGears = comlandr::mskeyGears,
aggGear = F,
userGears = comlandr::mskeyGears,
fleetDescription = 'Fleet',
unkVar = c('MONTH','NEGEAR','AREA'),
knStrata = c('HY', 'QY','MONTH','NEGEAR', 'TONCL2', 'AREA')) {
Expand All @@ -67,6 +76,20 @@ get_comland_data <- function(channel, filterByYear = NA,

call <- dbutils::capture_function_call()


check_argument_validation(aggArea,
userAreas,
areaDescription,
propDescription,
applyProp,
aggGear,
userGears,
fleetDescription,
unkVar,
knStrata
)


#Pull raw data
comland <- comlandr::get_comland_raw_data(channel,
filterByYear, filterByArea,
Expand Down Expand Up @@ -111,7 +134,6 @@ 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,
Expand All @@ -121,9 +143,6 @@ get_comland_data <- function(channel, filterByYear = NA,
channel,
applyProp)

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


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

Expand Down

0 comments on commit 3143f36

Please sign in to comment.