diff --git a/R/check_argument_validation.R b/R/check_argument_validation.R new file mode 100644 index 0000000..6f1715e --- /dev/null +++ b/R/check_argument_validation.R @@ -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 + +} diff --git a/R/get_comland_data.R b/R/get_comland_data.R index 79e6fa5..d108408 100644 --- a/R/get_comland_data.R +++ b/R/get_comland_data.R @@ -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')) { @@ -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, @@ -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, @@ -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)