require(RODBC)
require(rgdal)
require(devtools)
require(roxygen2)
require(geosphere)
require(SpatialHub)
require(lubridate)
require(bio.utilities)
require(bio.lobster)
require(rstanarm)
require(rlang)
require(glue)
require(PBSmapping)


    p = bio.lobster::load.environment()
    la()
    assessment.year = 2023 ##Check Year
    p$syr = 1989
  p$yrs = p$syr:assessment.year



figdir = file.path("LFA35_Update")

        p$lfas = c("35") # specify lfa
        p$subareas = c("35") # specify subareas for data summary


## from Oracle
#lobster.db('logs.redo') 
#lobster.db('temperature.data.redo')
#lobster.db('fsrs.redo')
groundfish.db('odbc.redo',datayrs = 1970:2020)


#Not Oracle     
logs=lobster.db('process.logs.redo')
logs=lobster.db("process.logs")
land = lobster.db('annual.landings')
land = lobster.db('seasonal.landings')
land=lobster.db('seasonal.landings.redo')

Analysis and Response

Primary Indicator

The LFA 35-38 assessment (DFO 2020) provided a full analysis of stock health by describing fishery performance and indicators for abundance and biomass, fishing pressure and reproduction. From this assessment commercial Catch Per Unit Effort (CPUE) has been identified as the primary indicator.

Catch Per Unit Effort

Catch rates are a preferred indicator over landings data as they are standardized to account for the level of fishing effort, which is especially important in effort controlled fisheries. However, they vary during the fishing season due to changes in biomass and catchability, which can be accounted for in catch rate models.

Data for assessing catch rates primarily comes from mandatory logs that were not put in place until the mid 2000’s. This time series covers the current high productivity period and a lower productivity period from 2006-2010. The median of the high productivity period (2011-2018 was used as the proxy for the biomass at carrying capacity (K). Following the recommendations of DFO (2009), the USR and LRP were set to 40% and 20% of the K proxy. The 3-year running median is used to compare the commercial catch rates to the USR and LRP. This value will dampen the impact of any anomalous years, which may occur due to factors outside of changes in abundance.

        logs=lobster.db("process.logs")
        TempModelling = TempModel( annual.by.area=F, redo.data=F)
            CPUE.data<-CPUEModelData(p,redo=T,TempModelling)

            ## Commercial CPUE MOdels
            mf1 = formula(logWEIGHT ~ fYEAR + DOS + TEMP + DOS * TEMP)

            CPUE.data<- CPUEModelData(p,redo=F)
            t=with(subset(CPUE.data,DOS==1),tapply(TEMP,LFA,mean))

            pData=list()

            CPUEModelResults = list()

            for(i in 1:length( p$lfas)){

                mdata = subset(CPUE.data,LFA==p$lfas[i]&SYEAR%in%p$yrs)
                if(nrow(mdata)>10){
                CPUEModelResults[[i]] = CPUEmodel(mf1,mdata,t=t[i],d=1)
                pData[[i]]=CPUEModelResults[[i]]$pData
                pData[[i]]$LFA=p$lfas[i]
            }
            }

            names(CPUEModelResults) = p$lfas

            CPUEindex=do.call("rbind",pData)


    # plot

    tiff("C:/Users/Howsevj/Documents/LFA35_Update/plotcpue.tiff", width=7,height=6, units="in",res=300)

    l35 = subset(CPUEindex,LFA==35,c("YEAR","mu"))
    k = median(l35$mu[which(l35$YEAR %in% 2011:2018)])
    usr = .4*k
    lrp = .2*k


plot(l35$YEAR,l35$mu,xlab='Year',ylab='CPUE (kg/TH)',type='p',pch=16,ylim=c(0,6))
    running.median = with(rmed(l35$YEAR,l35$mu),data.frame(YEAR=yr,running.median=x))
    l35=merge(l35,running.median,all=T)
    lines(l35$YEAR,l35$running.median,col='blue',lty=1,lwd=3)
    abline(h=usr,col='green',lwd=2,lty=2)
    abline(h=lrp,col='red',lwd=2,lty=3)

dev.off()

The CPUE trend indicates an increase in stock biomass occured between 2005 and 2011. The CPUE time series has remained high (more than twice the USR)

Secondary Indicators

Landings and Effort

    land = lobster.db('seasonal.landings')
    land$YR = as.numeric(substr(land$SYEAR,6,9))
    land =land[order(land$YR),]


        d1 = data.frame(YEAR = land$YR, LANDINGS = land[,paste0("LFA",p$lfas[i])])
        d2 = subset(CPUEindex,LFA==p$lfas,c("LFA","YEAR","mu"))
        names(d2)[3]="CPUE"

        d2  = merge(data.frame(LFA=d2$LFA[1],YEAR=min(d2$YEAR):max(d2$YEAR)),d2,all.x=T)

        fishData = merge(d2,d1) 
        fishData$EFFORT2 = fishData$LANDINGS * 1000 / fishData$CPUE


        tiff("C:/Users/Howsevj/Documents/LFA35_Update/35landings.tiff", width=8,height=6, units="in",res=300)

    par(mar=c(5.1, 4.1, 4.1, 5.1),las=1)

  plot(fishData$YEAR,fishData$LANDINGS,xlab='Year',ylab='Landings(t)',type='h',ylim=c(0,max(fishData$LANDINGS)*1.2),pch=15,col='grey',lwd=10,lend=3)
        lines(fishData$YEAR[nrow(fishData)],fishData$LANDINGS[nrow(fishData)],type='h',pch=21,col='steelblue4',lwd=10,lend=3)
        par(new=T)
        plot(fishData$YEAR,fishData$EFFORT2/1000,ylab='',xlab='', type='b', pch=16, axes=F,ylim=c(0,max(fishData$EFFORT2/1000,na.rm=T)))
    points(fishData$YEAR[nrow(fishData)],fishData$EFFORT2[nrow(fishData)]/1000, type='b', pch=24,bg='black')
        axis(4)
        mtext("Effort ('000s Trap Hauls)", 4, 3.5, outer = F,las=0) 
dev.off()       
require(bio.survey)
require(bio.lobster)

p = bio.lobster::load.environment()
p$libs = NULL
ff = "LFA35-38Assessment"
fp1 = file.path(project.datadirectory('bio.lobster'),"analysis",ff)
fpf1 = file.path(project.figuredirectory('bio.lobster'),ff)
dir.create(fpf1,showWarnings=F)
dir.create(fp1,showWarnings=F)

p$yrs = 1970:2019
p1 = p


stratifiedAnalysesCommercial = function( p=p1, survey,lfa, fpf = fpf1, fp = fp1,f=ff,wd=10,ht=8){
    p$series =c('summer')# p$series =c('georges');p$series =c('fall')
    p$years.to.estimate = p$yrs
    p$length.based = T
    p$by.sex = T
    p$size.class = c(83,300)
    p$sex = c(1,2)
    p$bootstrapped.ci=T
    p$strata.files.return=F
    p$vessel.correction.fixed=1.2
    p$strat = NULL
    p$clusters = c( rep( "localhost", 7) )
    p$strata.efficiencies = F
    p = make.list(list(yrs=p$years.to.estimate),Y=p)
    p$define.by.polygons = T
    p$lobster.subunits=F
    p$area = lfa
    p$reweight.strata = T #this subsets 

    aout= dfo.rv.analysis(DS='stratified.estimates.redo',p=p)
    write.csv(aout,file=file.path(fpf, paste(lfa,'DFOCommB.csv')))

      p$add.reference.lines = F
    p$time.series.start.year = p$years.to.estimate[1]
    p$time.series.end.year = p$years.to.estimate[length(p$years.to.estimate)]
    p$metric = 'weights' #weights
    p$measure = 'stratified.total' #'stratified.total'
    p$figure.title = ""
    p$reference.measure = 'median' # mean, geomean
    p$file.name =  file.path(f,paste(lfa,'DFOrestratifiedtotalweightscommercial.png',sep=""))

    p$y.maximum = NULL # NULL # if ymax is too high for one year
    p$show.truncated.weights = F #if using ymax and want to show the weights that are cut off as values on figure

    p$legend = FALSE
    p$running.median = T
    p$running.length = 3
    p$running.mean = F #can only have rmedian or rmean
     p$error.polygon=F
    p$error.bars=T
    require(bio.lobster)
     p$ylim2 = NULL
    xx = aggregate(ObsLobs~yr,data=aout,FUN=sum)
    names(xx) =c('x','y')
     p$ylim=NULL
     ref.out= figure.stratified.analysis(x=aout,out.dir = 'bio.lobster', p=p,wd=wd,ht=ht)
     return(aout)
  }



aout = stratifiedAnalysesCommercial(survey='DFO',lfa='LFA35-38')
write.csv(aout,file.path(fp1,'LFA3538CommercialB.csv'))
require(bio.survey)
require(bio.lobster)
require(lubridate)
require(devtools)
require(bio.utilities)
require(PBSmapping)
la()
p = bio.lobster::load.environment()
p$libs = NULL
ff = "LFA35-38Assessment"
fp1 = file.path(project.datadirectory('bio.lobster'),"analysis",ff)
fpf1 = file.path(project.figuredirectory('bio.lobster'),ff)
dir.create(fpf1,showWarnings=F)
dir.create(fp1,showWarnings=F)
p1 = p
p1$yrs = 1970:2023

stratifiedAnalyses = function(p=p1, survey,lfa, fpf = fpf1, fp = fp1,f=ff,wd=10,ht=8){
                p$series =c('summer')
                p$define.by.polygons = T
                p$lobster.subunits=F
                p$area = lfa
                p$years.to.estimate = p$yrs[-1]
                p$length.based = F
                p$by.sex = F
                p$bootstrapped.ci=T
                p$strata.files.return=F
                p$vessel.correction.fixed=1.2
                p$strat = NULL
                p$clusters = c( rep( "localhost", 7) )
                p$strata.efficiencies = F
                p = make.list(list(yrs=p$years.to.estimate),Y=p)
                p$reweight.strata = T #this subsets 


      aout= dfo.rv.analysis(DS='stratified.estimates.redo',p=p)
      write.csv(aout,file=file.path(fpf, paste(lfa,'DFOtotalabund.csv')))


                              p$add.reference.lines = F
                              p$time.series.start.year = p$years.to.estimate[1]
                              p$time.series.end.year = p$years.to.estimate[length(p$years.to.estimate)]
                              p$metric = 'numbers' #weights
                              p$measure = 'stratified.mean' #'stratified.total'
                              p$figure.title = ""
                              p$reference.measure = 'median' # mean, geomean
                              p$file.name = file.path(f,paste(lfa,'DFOrestratifiednumbers.png',sep=""))

                          p$y.maximum = NULL # NULL # if ymax is too high for one year
                        p$show.truncated.numbers = F #if using ymax and want to show the numbers that are cut off as values on figure

                                p$legend = FALSE
                                p$running.median = T
                                p$running.length = 3
                                p$running.mean = F #can only have rmedian or rmean
                               p$error.polygon=F
                              p$error.bars=T

                               p$ylim=NULL
                      if(lfa == 'LFA35-38') p$ylim=c(0,150)
                       p$box=T
                       p$file.name = file.path(f,paste(lfa,'DFOrestratifiednumbersNOY.png', sep=""))
                       ref.out=   figure.stratified.analysis(x=aout,out.dir = 'bio.lobster', p=p,wd=wd,ht=ht)

                       p$box=T
                        p$ylim=NULL
                       p$metric = 'weights'
                       p$file.name = file.path(f,paste(lfa,'DFOrestratifiedweightsNOY.png',sep=""))
                       ref.out=   figure.stratified.analysis(x=aout,out.dir = 'bio.lobster', p=p,wd=wd,ht=ht)

                       p$box=NULL
                       p$ylim=NULL
                       p$file.name = file.path(f,paste(lfa,'DFOrestratifiedDWAO.png',sep=""))
                       p$metric = 'dwao'
                       ref.out=   figure.stratified.analysis(x=aout,out.dir = 'bio.lobster', p=p,wd=wd,ht=ht)

                       p$file.name = file.path(f,paste(lfa,'DFOrestratifiedgini.png',sep=""))
                       p$metric = 'gini'
                       p$ylim =c(0,1)
                       ref.out=   figure.stratified.analysis(x=aout,out.dir = 'bio.lobster', p=p,wd=wd,ht=ht)
                       p$ylim = NULL
                       return(aout)
        }


a = stratifiedAnalyses(survey='DFO',lfa='LFA35-38')
require(bio.survey)
require(bio.lobster)
require(lubridate)
require(devtools)
require(bio.utilities)
require(PBSmapping)
la()
p = bio.lobster::load.environment()
p$libs = NULL
ff = "LFA35-38Assessment"
fp1 = file.path(project.datadirectory('bio.lobster'),"analysis",ff)
fpf1 = file.path(project.figuredirectory('bio.lobster'),ff)
dir.create(fpf1,showWarnings=F)
dir.create(fp1,showWarnings=F)
p1 = p
p1$yrs = 1970:2019

stratifiedAnalysesRecruits = function(p=p1, survey,lfa, fpf = fpf1, fp = fp1,f=ff,wd=10,ht=8){
      p$series =c('summer')# p$series =c('georges');p$series =c('fall')
      p$area = lfa
      p$years.to.estimate = p$yrs
      p$length.based = T
      p$by.sex = T
      p$size.class = c(70,82)
      p$sex = c(1,2)
      p$bootstrapped.ci=T
      p$strata.files.return=F
      p$vessel.correction.fixed=1.2
      p$strat = NULL
      p$clusters = c( rep( "localhost", 7) )
      p$strata.efficiencies = F
      p = make.list(list(yrs=p$years.to.estimate),Y=p)
      p$define.by.polygons = T
      p$lobster.subunits=F
      p$reweight.strata = T #this subsets 

    aout= dfo.rv.analysis(DS='stratified.estimates.redo',p=p)
     write.csv(aout,file=file.path(fpf, paste(lfa,'DFOrecruits.csv')))

                        p$add.reference.lines = F
                              p$time.series.start.year = p$years.to.estimate[1]
                              p$time.series.end.year = p$years.to.estimate[length(p$years.to.estimate)]
                              p$metric = 'numbers' #numbers
                              p$measure = 'stratified.mean' #'stratified.total'
                              p$figure.title = ""
                              p$reference.measure = 'median' # mean, geomean
                              p$file.name =  file.path(f,paste(lfa,'DFOrestratifiednumbersrecruits.png',sep=""))
                          p$y.maximum = NULL # NULL # if ymax is too high for one year
                        p$show.truncated.numbers = F #if using ymax and want to show the numbers that are cut off as values on figure
                                p$legend = FALSE
                                p$running.median = T
                                p$running.length = 3
                                p$running.mean = F #can only have rmedian or rmean
                               p$error.polygon=F
                              p$error.bars=T
                      if(lfa == 'LFA35-38') p$ylim = c(0,80)
                      p$file.name =  file.path(f,paste(lfa,'NOYDFOrestratifiednumbersrecruits.png',sep=""))
                      ref.out=   figure.stratified.analysis(x=aout,out.dir = 'bio.lobster', p=p,wd=wd,ht=ht)
            }

stratifiedAnalysesRecruits(survey='DFO',lfa='LFA35-38')
require(bio.lobster)
require(PBSmapping)

a = lobster.db('annual.landings')
b = lobster.db('seasonal.landings')
ff = "LFA35-38Assessment"

b$YR = substr(b$SYEAR,6,9)
a = subset(a,YR<1976)
b = subset(b,YR>1975 & YR<=2019)
fpf1 = file.path(project.figuredirectory('bio.lobster'),ff)
fp1 = file.path(project.datadirectory('bio.lobster'),"analysis",ff)

LFA = c('LFA35','LFA36','LFA38')
for(i in LFA){
        aa = a[,c('YR',i)]
        bb = b[,c('YR',i)]
        aa = (rbind(aa,bb))
        aa = aa[order(aa$YR),]
        file.name = paste('Landings',i,'.png',sep="")
        png(file=file.path(fpf1,'LandingsL3538.png'),units='in',width=15,height=12,pointsize=18, res=300,type='cairo')
        plot(aa$YR,aa[,i],type='h',lwd=4,col='black',xlab='Year',ylab='Landings (t)')
        lines(aa$YR,runmed(aa[,i],3),col='salmon',lwd=3)
        dev.off()
        }


###lfa 35-38

df2 = read.csv(file.path(fpf1,'LFA35-38 DFOCommB.csv'))
df =  read.csv(file.path(fpf1,'LFA35-38 DFOtotalabund.csv'))
df = subset(df,yr<1999)
df2 = subset(df2,yr>1998)
df = as.data.frame(rbind(df,df2))
df = df[,c('yr','w.Yst','w.ci.Yst.l','w.ci.Yst.u')] #proportion of total weight that is commercial
df$w.Yst[which(df$yr<1999)] <- df$w.Yst[which(df$yr<1999)]*0.746
df$w.ci.Yst.l[which(df$yr<1999)] <- df$w.ci.Yst.l[which(df$yr<1999)]*0.746
df$w.ci.Yst.u[which(df$yr<1999)] <- df$w.ci.Yst.u[which(df$yr<1999)]*0.746



     #png(file=file.path(fpf1,'LFA35-38CommBDFOextended.png'),units='in',width=10,height=8,pointsize=18, res=300,type='cairo')
     with(df,plot(yr,w.Yst,pch=1,xlab='Year',ylab='Commerical Biomass (t)',ylim=c(0,9500)))
     with(df,arrows(yr,y0=w.ci.Yst.u,y1=w.ci.Yst.l, length=0))
     with(subset(df,yr>1998),points(yr,w.Yst,pch=16))
     xx = rmed(df$yr,df$w.Yst)
     xx = as.data.frame(do.call(cbind,xx))
     with(subset(xx,yr<1999),lines(yr,x,col='salmon',lwd=1))
     with(subset(xx,yr>1998),lines(yr,x,col='salmon',lwd=3))
     dev.off()

a$L3538 = rowSums(a[,12:14])
b$L3538 = rowSums(b[,4:6])
c358 = as.data.frame(rbind(a[,c('YR','L3538')],b[,c('YR','L3538')]))
names(c358)[1] = 'yr'
df  =merge(df,c358)
df$rL = df$L3538/(df$w.ci.Yst.l+df$L3538)
df$rU =df$L3538/ (df$w.ci.Yst.u+df$L3538)
df$rM = df$L3538/(df$w.Yst+df$L3538)


     png(file=file.path(fpf1,'LFA3538RelFDFO.png'),units='in',width=10,height=8,pointsize=18, res=300,type='cairo')
        plot(df$yr,df$rM,type='p',pch=16,col='black',xlab='Year',ylab='Relative F')
        arrows(df$yr,y0 = df$rL,y1 = df$rU,length=0)
        with(rmed(df$yr,df$rM),lines(yr,x,col='salmon',lwd=3))
dev.off()

DFO RV Survey Commercial Biomass and Recruit Abundance






LobsterScience/bio.lobster documentation built on Feb. 14, 2025, 3:28 p.m.