R/extractMDFR.ExploitationRates.R

Defines functions extractMDFR.ExploitationRates

Documented in extractMDFR.ExploitationRates

#'
#'@title Function to extract exploitation rates by year among several models
#'
#'@description This function extracts exploitation rates by year
#'   among several models.
#'
#'@param objs - list of resLst objects
#'@param verbose - flag (T/F) to print diagnostic information
#'
#'@return dataframe
#'
#'@details Results are extracted using \code{rTCSAM2013::getMDFR.Pop.Biomass},
#'\code{rsimTCSAM::getMDFR.Pop.Biomass}, and/or \code{rTCSAM02::getMDFR.Pop.Biomass}, as appropriate, and
#'cast to aggregate. This differs from \code{compareResults.Pop.Biomass1}.
#'
#'@export
#'
extractMDFR.ExploitationRates<-function(objs,
                                        verbose=FALSE){
    if (verbose) cat("--starting rCompTCMs::extractMDFR.ExploitationRates()\n");
    options(stringsAsFactors=FALSE);

    cases<-names(objs);

    mdfr1<-extractMDFR.Pop.Biomass(objs,cast='y',verbose=verbose);
    mdfr2<-extractMDFR.Fisheries.CatchBiomass(objs,cast='y',category='retained',verbose=verbose);
    mdfr3<-extractMDFR.Fisheries.CatchBiomass(objs,cast='y',category='discard mortality',verbose=verbose);

    dfr1<-reshape2::dcast(mdfr1,case+y~.,fun.aggregate=wtsUtilities::Sum,value.var="val");
    dfr2<-reshape2::dcast(mdfr2,case+y~.,fun.aggregate=wtsUtilities::Sum,value.var="val");
    dfr3<-reshape2::dcast(mdfr3,case+y~.,fun.aggregate=wtsUtilities::Sum,value.var="val");
    names(dfr1)[3]<-"biomass";
    names(dfr2)[3]<-"retmort";
    names(dfr3)[3]<-"dscmort";

    qry<-"select
            d.`case` as `case`,
            d.y as y,
            r.retmort,
            d.dscmort
          from dfr3 as d left join dfr2 as r
          on
            r.`case`=d.`case` and
            r.y=d.y;";
    dfrc<-sqldf::sqldf(qry);
    dfrc$retmort[is.na(dfrc$retmort)]<-0;
    dfrc$dscmort[is.na(dfrc$dscmort)]<-0;

    qry<-"select
            b.`case` as `case`,
            b.y as y,
            (retmort+dscmort)/biomass as val
          from dfr1 as b, dfrc as c
          where
            b.`case`=c.`case` and
            b.y=c.y
          order by b.`case`,b.y;";
    dfr<-sqldf::sqldf(qry);
    dfr$process<-"exploitation";
    dfr$fleet<-"";
    dfr$category<-"fishery";
    dfr$type<-"";
    dfr$pc<-"";
    dfr$x<-"all";
    dfr$m<-"all";
    dfr$s<-"all";
    dfr$z<-"all";
    dfr$lci<-"";
    dfr$uci<-"";

    if (verbose) cat("names = '",names(dfr),"'\n");
    nms<-c("case","process","fleet","category","type","pc",
           "y","x","m","s","z","val","lci","uci");
    dfr<-dfr[,nms];

    if (verbose) cat("rCompTCMs::extractMDFR.ExploitationRates: Done!\n");
    return(dfr)
}
wStockhausen/rCompTCMs documentation built on Sept. 12, 2023, 3:13 p.m.