From f842bb784d7a1714a5d6da60dfc0b24d4b95253e Mon Sep 17 00:00:00 2001 From: "Joseph Caracappa (NOAA)" <57966543+jcaracappa1@users.noreply.github.com> Date: Fri, 20 Sep 2024 10:57:28 -0400 Subject: [PATCH] added catch by fleet diagnostics - magnitude of catch - center of gravity of catch --- DESCRIPTION | 3 +- NAMESPACE | 1 + R/diag_fleet_catch.R | 122 +++++++++++++++++++++++++++++++++++++ man/atlantisdiagnostics.Rd | 21 +++++++ man/diag_fleet_catch.Rd | 59 ++++++++++++++++++ man/diag_reasonability.Rd | 1 + man/diag_stability.Rd | 1 + 7 files changed, 207 insertions(+), 1 deletion(-) create mode 100644 R/diag_fleet_catch.R create mode 100644 man/diag_fleet_catch.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 02007e3..def7b4d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -47,5 +47,6 @@ Imports: tibble Remotes: Atlantis-Ecosystem-Model/atlantistools + NOAA-EDAB/atlantisprocessing Roxygen: list(markdown = TRUE) -RoxygenNote: 7.1.2 +RoxygenNote: 7.3.1 diff --git a/NAMESPACE b/NAMESPACE index 0bbfdf6..84dfaa9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(diag_cohortBiomass) export(diag_combine) +export(diag_fleet_catch) export(diag_maxsize) export(diag_persistence) export(diag_reasonability) diff --git a/R/diag_fleet_catch.R b/R/diag_fleet_catch.R new file mode 100644 index 0000000..67e1a3d --- /dev/null +++ b/R/diag_fleet_catch.R @@ -0,0 +1,122 @@ +#'Test fleet catch +#' +#'\code{diag_fleet_catch} determines whether the simulated fleets catcht the right magnitude and spatial distribution of catch +#'over the last n years of a run. +#' +#' +#'@param fgs.file A character string. Path to location of functional groups file. +#'@param fishery.file A character string. Path to the fishery definitions file. +#'@param bgm.file A character string. Path to the bgm file. +#'@param catch.ref A data.frame containing reference catch by fleet data (species|fleet|polygon|ref.value) +#'@param speciesCodes Character vector. A vector of Atlantis species codes in which to test for stability. +#'@param nYrs Numeric scalar. Number of years from the end of the time series that stability must occur. +#'@param relChangeThreshold Numeric Scalar. Maximum magnitude of relative change of slope (Default = 0.01) +#'@param min.dist Numeric Scalar. Maximum distance between model and reference center of gravity +#' +#'@return Returns a data frame of all species and how they measure up against the catch by fleet criteria +#'\item{species}{The common name of the species/functional group} +#'\item{fleet}{The fleet name from the fisheries file} +#'\item{catch.model}{Double. The mean catch in mT over the last nYrs of the model } +#'\item{catch.ref}{Double. The reference catch (mT)} +#'\item{cog.x.model}{Double. The mean x-coordinate center of gravity from the model} +#'\item{cog.x.ref}{Double. The mean x-coordinate center of gravity from the reference} +#'\item{cog.y.model}{Double. The mean y-coordinate center of gravity from the model} +#'\item{cog.y.ref}{Double. The mean y-coordinate center of gravity from the reference} +#'\item{dist}{Double. The mean distance between the center of gravity between the model and reference} +#'\item{catch.magnitude}{Logical. Is the relative difference between model and refernece catch within +/- relChangeThreshold?} +#'\item{catch.dist}{Logical. Is the distance between center of gravity betwene model and reference less than min.dist?} +#' +#'@family diags +#' +#'@export +#' +#'@importFrom magrittr %>% + +diag_fleet_catch <- function(fgs.file, + fishery.file, + catch.file, + bgm.file, + catch.ref, + speciesCodes = NULL, + nYrs = 20, + min.dist = 100, + relChangeThreshold = 0.01){ + + boxes = atlantistools::convert_bgm(bgm.file)%>% + dplyr::distinct(polygon,inside_lat,inside_long) + + catch.fleet =atlantisprocessing::process_catch_fleet(fishery.prm = fishery.file, + catch = catch.file, + groups.file = fgs.file) + + if(!is.null(speciesCodes)){ + fgs = read.csv(fgs.file,as.is =T) + + spp.match = fgs$LongName[which(fgs$Code %in% speciesCodes)] + + catch.fleet = catch.fleet %>% + dplyr::filter(species %in% spp.match) + + catch.ref = catch.ref %>% + dplyr::filter(species %in% spp.match) + } + max.yr = max(catch.fleet$time) + + #mnagnitude + catch.mag.model =catch.fleet %>% + dplyr::filter(time >= (max.yr - nYrs)) %>% + dplyr::group_by(species,fleet,time)%>% + dplyr::summarise(catch.model = sum(atoutput,na.rm=T))%>% + dplyr::group_by(species,fleet)%>% + dplyr::summarise(catch.model = mean(catch.model,na.rm=T)) + + catch.mag.ref = catch.ref %>% + dplyr::group_by(species,fleet)%>% + dplyr::summarise(catch.ref = mean(ref.value,na.rm=T)) + + + catch.mag.all = catch.mag.model %>% + dplyr::left_join(catch.mag.ref)%>% + dplyr::mutate(catch.rel = catch.ref/catch.model, + catch.magnitude = ifelse(catch.rel > (1 - relChangeThreshold) & catch.rel < (1+relChangeThreshold),T,F) + ) + + #distance + catch.cog.model =catch.fleet %>% + dplyr::filter(time >= (max.yr - nYrs))%>% + dplyr::left_join(boxes)%>% + dplyr::mutate(catch.wgt.x = inside_long * atoutput, + catch.wgt.y = inside_lat * atoutput)%>% + dplyr::group_by(species,fleet,time)%>% + dplyr::summarise(cog.x = sum(catch.wgt.x,na.rm=T), + cog.y = sum(catch.wgt.y,na.rm=T), + catch.tot = sum(atoutput,na.rm=T))%>% + dplyr::mutate(cog.x = cog.x/catch.tot, + cog.y = cog.y/catch.tot)%>% + dplyr::group_by(species,fleet)%>% + dplyr::summarise(cog.x.model = mean(cog.x, na.rm=T), + cog.y.model = mean(cog.y, na.rm=T)) + + catch.cog.ref =catch.ref %>% + dplyr::left_join(boxes)%>% + dplyr::mutate(catch.wgt.x = inside_long * ref.value, + catch.wgt.y = inside_lat * ref.value)%>% + dplyr::group_by(species,fleet)%>% + dplyr::summarise(cog.x = sum(catch.wgt.x,na.rm=T), + cog.y = sum(catch.wgt.y,na.rm=T), + catch.tot = sum(ref.value,na.rm=T))%>% + dplyr::mutate(cog.x.ref = cog.x/catch.tot, + cog.y.ref = cog.y/catch.tot) + + catch.cog.all = catch.cog.model %>% + dplyr::left_join(catch.cog.ref)%>% + dplyr::mutate(dist = sqrt((cog.x.ref - cog.x.model)^2 + (cog.y.ref-cog.y.model)^2), + catch.dist = ifelse(dist <= min.dist,T,F)) + + #combine for output + catch.diag.all = catch.mag.all %>% + dplyr::left_join(catch.cog.all)%>% + dplyr::select(species,fleet,catch.model,catch.ref, cog.x.model,cog.x.model,cog.x.ref,cog.y.model,cog.y.ref,dist,catch.magnitude,catch.dist) + + return(catch.diag.all) +} diff --git a/man/atlantisdiagnostics.Rd b/man/atlantisdiagnostics.Rd index 8b75837..2fe0a2d 100644 --- a/man/atlantisdiagnostics.Rd +++ b/man/atlantisdiagnostics.Rd @@ -2,6 +2,7 @@ % Please edit documentation in R/atlantisdiagnostics.r \docType{package} \name{atlantisdiagnostics} +\alias{atlantisdiagnostics-package} \alias{atlantisdiagnostics} \title{atlantisdiagnostics: Tools for calibrating Atlantis ecosystem models} \description{ @@ -23,3 +24,23 @@ Tools to aid in diagnosing issues in a parameterized Atlantis Ecosystem model To learn more about using \code{atlantisdiagnostics}, start with the vignette: \code{browseVignettes(package="atlantisdiagnostics")} or click the index below } +\seealso{ +Useful links: +\itemize{ + \item \url{https://github.com/NOAA-EDAB/atlantisdiagnostics} + \item Report bugs at \url{https://github.com/NOAA-EDAB/atlantisdiagnostics/issues} +} + +} +\author{ +\strong{Maintainer}: Andy Beet \email{andrew.beet@noaa.gov} (\href{https://orcid.org/0000-0001-8270-7090}{ORCID}) + +Authors: +\itemize{ + \item Sarah Gaichas \email{Sarah.Gaichas@noaa.gov} + \item Joeseph Caracappa \email{joseph.caracappa@noaa.gov} + \item Robert Gamble \email{Robert.Gamble@noaa.gov} + \item Scott Large \email{Scott.Large.gov} +} + +} diff --git a/man/diag_fleet_catch.Rd b/man/diag_fleet_catch.Rd new file mode 100644 index 0000000..e3e02e3 --- /dev/null +++ b/man/diag_fleet_catch.Rd @@ -0,0 +1,59 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/diag_fleet_catch.R +\name{diag_fleet_catch} +\alias{diag_fleet_catch} +\title{Test fleet catch} +\usage{ +diag_fleet_catch( + fgs.file, + fishery.file, + catch.file, + bgm.file, + catch.ref, + speciesCodes = NULL, + nYrs = 20, + min.dist = 100, + relChangeThreshold = 0.01 +) +} +\arguments{ +\item{fgs.file}{A character string. Path to location of functional groups file.} + +\item{fishery.file}{A character string. Path to the fishery definitions file.} + +\item{bgm.file}{A character string. Path to the bgm file.} + +\item{catch.ref}{A data.frame containing reference catch by fleet data (species|fleet|polygon|ref.value)} + +\item{speciesCodes}{Character vector. A vector of Atlantis species codes in which to test for stability.} + +\item{nYrs}{Numeric scalar. Number of years from the end of the time series that stability must occur.} + +\item{min.dist}{Numeric Scalar. Maximum distance between model and reference center of gravity} + +\item{relChangeThreshold}{Numeric Scalar. Maximum magnitude of relative change of slope (Default = 0.01)} +} +\value{ +Returns a data frame of all species and how they measure up against the catch by fleet criteria +\item{species}{The common name of the species/functional group} +\item{fleet}{The fleet name from the fisheries file} +\item{catch.model}{Double. The mean catch in mT over the last nYrs of the model } +\item{catch.ref}{Double. The reference catch (mT)} +\item{cog.x.model}{Double. The mean x-coordinate center of gravity from the model} +\item{cog.x.ref}{Double. The mean x-coordinate center of gravity from the reference} +\item{cog.y.model}{Double. The mean y-coordinate center of gravity from the model} +\item{cog.y.ref}{Double. The mean y-coordinate center of gravity from the reference} +\item{dist}{Double. The mean distance between the center of gravity between the model and reference} +\item{catch.magnitude}{Logical. Is the relative difference between model and refernece catch within +/- relChangeThreshold?} +\item{catch.dist}{Logical. Is the distance between center of gravity betwene model and reference less than min.dist?} +} +\description{ +\code{diag_fleet_catch} determines whether the simulated fleets catcht the right magnitude and spatial distribution of catch +over the last n years of a run. +} +\seealso{ +Other diags: +\code{\link{diag_reasonability}()}, +\code{\link{diag_stability}()} +} +\concept{diags} diff --git a/man/diag_reasonability.Rd b/man/diag_reasonability.Rd index 24bfdb1..a88dcd9 100644 --- a/man/diag_reasonability.Rd +++ b/man/diag_reasonability.Rd @@ -96,6 +96,7 @@ diag_reasonability(fgs, biomind, initialYr = 1964, speciesCodes =c("MAK","WHK"), } \seealso{ Other diags: +\code{\link{diag_fleet_catch}()}, \code{\link{diag_stability}()} } \concept{diags} diff --git a/man/diag_stability.Rd b/man/diag_stability.Rd index 1fba26d..c21439c 100644 --- a/man/diag_stability.Rd +++ b/man/diag_stability.Rd @@ -74,6 +74,7 @@ diag_stability(fgs,biomind, speciesCodes=c("HER","WHK"), nYrs = 10, relChangeThr } \seealso{ Other diags: +\code{\link{diag_fleet_catch}()}, \code{\link{diag_reasonability}()} } \concept{diags}