knitr::opts_chunk$set(results = FALSE)

This example code demonstrates how to extract the purse-seine catch and length composition data for the stock assessment of skipjack tuna in the eastern Pacific Ocean. Data are extracted for skipjack between 2000 and 2023 based on the R package BSE (version r packageVersion("BSE")). The package can be installed using devtools::install_github('HaikunXu/BSE',ref='main'). Note that a revised version for SKJ is used. Fishery definition for this data extraction is based on regression tree analysis in 2024.

devtools::install_github('RujiaBi/BSE_SKJ_2024',ref='main')  # Updated for SKJ
library(BSE)

# Load the base files (please ask Haikun to get those data)
raw_data_dir <- "C:/Users/rbi/OneDrive - IATTC/BSE/Data/"
# the directory where output will be saved
save_dir <- "C:/Users/rbi/OneDrive - IATTC/BSE/Output/"
yr.start <- 2000
yr.end <- 2023
Species <- "SKJ"
grow.increments <- grow.increments.betyftskj # the growth increment for SKJ (2cm per month)

# area substitution matrix
area.substitution.mat.SKJ.FLT.SAC2024 <- matrix(c(1,2,3,4,5,
                                                  2,1,4,5,4,
                                                  5,4,2,3,2,
                                                  4,3,5,2,3,
                                                  3,5,1,1,1), nrow=5)


area.substitution.mat.SKJ.UNA.SAC2024 <- matrix(c(1,2,3,4,
                                                  3,3,4,3,
                                                  4,4,2,2,
                                                  2,1,1,1), nrow=4)

area.substitution.mat.SKJ.DEL.SAC2024 <- area.substitution.mat.SKJ.DEL.SAC2022

#fishery substitute matrix
my.FOmatrix <- matrix(paste0("FO.A", area.substitution.mat.OBJ),ncol=ncol(area.substitution.mat.OBJ))
my.UNmatrix <- matrix(paste0("UN.A", area.substitution.mat.NOA),ncol=ncol(area.substitution.mat.NOA))
my.DPmatrix <- matrix(paste0("DP.A", area.substitution.mat.DEL),ncol=ncol(area.substitution.mat.DEL))
# Get the total unloads for the PS fleet
total.unlds <- read.unloads.f(raw_data_dir,"Unloading2000-2023.txt",yr.start,yr.end)
# Get the CAE+IDM data
cae <- read.cae.f(raw_data_dir,"CAE-LatLon2000-2023.txt",yr.start,yr.end)
# Get the length-frequency data (length in millimeters)
lfmm <- read.lfmmdata.f(raw_data_dir,"LengthMM2000-2023.txt")
# Get the grouped length-frequency output
lfgrpd <- read.lengthfreq.f(raw_data_dir,"LengthFreq2000-2023.txt")
PS <- "OBJ"
area.substitution.mat <- area.substitution.mat.OBJ

cae.stratflg <- create.strat.flg.f(cae$latc5,cae$lonc5,is.lwrght=F,cae$month,cae$setype,cae$class,PS=PS,Species=Species)
lfgrpd.stratflg <- create.strat.flg.f(lfgrpd$lat.5deg,lfgrpd$lon.5deg,is.lwrght=T,floor(lfgrpd$moda/100),lfgrpd$setype,lfgrpd$class,PS=PS,Species=Species)

Check the strata definition for OBJ in both cae and lf data sets to make sure that they are correct

check.strat.flg.f(cae$latc5,cae$lonc5,cae.stratflg)
check.strat.flg.f(lfgrpd$lat.5deg,lfgrpd$lon.5deg,lfgrpd.stratflg)

Loop through every year between yr.start and yr.end to get catch and composition data for SKJ in the OBJ fishery

for(year in yr.start:yr.end) {
  # print(paste0("Year: ",year))

  # print("Step 1: get well estimates")
  well.estimates <- well.estimates.f(lfgrpd[lfgrpd$year.firstset==year,],lfmm)

  # print("Step 2: get catch estimates")
  catch.estimates <- get.catch.estimates.f(cae,cae.stratflg,total.unlds,lfgrpd,lfgrpd.stratflg,lfmm,year,2,well.estimates,area.substitution.mat,grow.increments,PS=PS,Species=Species,my.FOmatrix,my.UNmatrix,my.DPmatrix)

  # print("Step 3: get fishery estimates")
  fishery.estimates <- fishery.estimates.f(catch.estimates$stratum.estimates.withsamps,catch.estimates$stratum.estimates.NOsamps,year,PS=PS,Species=Species)

  assign(paste0("fishery.estimates.", year), fishery.estimates, pos=1)
}
# save middle-step data as a record
save(list=objects(pat="fishery.estimates"),file=paste0(save_dir,"SKJ_",PS,"_2000-2023.RData"))

Get final OBJ catch and comp output for the stock assessment

SKJ.OBJ.Catch.20002023<-compile.catch.output.f(yr.start,yr.end,PS=PS,Species=Species,c("A1","A2","A3","A4","A5")) # five OBJ fisheries
SKJ.OBJ.Comp.20002023<-compile.sizecomps.output.f(yr.start,yr.end,PS=PS,Species=Species)
PS <- "NOA"
area.substitution.mat <- area.substitution.mat.NOA

cae.stratflg <- create.strat.flg.f(cae$latc5,cae$lonc5,is.lwrght=F,cae$month,cae$setype,cae$class,PS=PS,Species=Species)
lfgrpd.stratflg <- create.strat.flg.f(lfgrpd$lat.5deg,lfgrpd$lon.5deg,is.lwrght=T,floor(lfgrpd$moda/100),lfgrpd$setype,lfgrpd$class,PS=PS,Species=Species)

Check the strata definition for NOA in both cae and lf data sets to make sure that they are correct

check.strat.flg.f(cae$latc5,cae$lonc5,cae.stratflg)
check.strat.flg.f(lfgrpd$lat.5deg,lfgrpd$lon.5deg,lfgrpd.stratflg)

Loop through every year between yr.start and yr.end to get catch and composition data for SKJ in the NOA fishery

for(year in yr.start:yr.end) {
  # print(paste0("Year: ",year))

  # print("Step 1: get well estimates")
  well.estimates <- well.estimates.f(lfgrpd[lfgrpd$year.firstset==year,],lfmm)

  # print("Step 2: get catch estimates")
  catch.estimates <- get.catch.estimates.f(cae,cae.stratflg,total.unlds,lfgrpd,lfgrpd.stratflg,lfmm,year,2,well.estimates,area.substitution.mat,grow.increments,PS=PS,Species=Species,my.FOmatrix,my.UNmatrix,my.DPmatrix)

  # print("Step 3: get fishery estimates")
  fishery.estimates <- fishery.estimates.f(catch.estimates$stratum.estimates.withsamps,catch.estimates$stratum.estimates.NOsamps,year,PS=PS,Species=Species)

  assign(paste0("fishery.estimates.", year), fishery.estimates, pos=1)
}
# save middle-step data as a record
save(list=objects(pat="fishery.estimates"),file=paste0(save_dir,"SKJ_",PS,"_2000-2023.RData"))

Get final NOA catch and comp output for the stock assessment

SKJ.NOA.Catch.20002023<-compile.catch.output.f(yr.start,yr.end,PS=PS,Species=Species,c("A1","A2","A3","A4","A5"))  # five NOA fisheries
SKJ.NOA.Comp.20002023<-compile.sizecomps.output.f(yr.start,yr.end,PS=PS,Species=Species)
PS <- "DEL"
area.substitution.mat <- area.substitution.mat.DEL

cae.stratflg <- create.strat.flg.f(cae$latc5,cae$lonc5,is.lwrght=F,cae$month,cae$setype,cae$class,PS=PS,Species=Species)
lfgrpd.stratflg <- create.strat.flg.f(lfgrpd$lat.5deg,lfgrpd$lon.5deg,is.lwrght=T,floor(lfgrpd$moda/100),lfgrpd$setype,lfgrpd$class,PS=PS,Species=Species)

Check the strata definition for DEL in both cae and lf data sets to make sure that they are correct

check.strat.flg.f(cae$latc5,cae$lonc5,cae.stratflg)
check.strat.flg.f(lfgrpd$lat.5deg,lfgrpd$lon.5deg,lfgrpd.stratflg)

Loop through every year between yr.start and yr.end to get catch and composition data for SKJ in the DEL fishery

for(year in yr.start:yr.end) {
  # print(paste0("Year: ",year))

  # print("Step 1: get well estimates")
  well.estimates <- well.estimates.f(lfgrpd[lfgrpd$year.firstset==year,],lfmm)

  # print("Step 2: get catch estimates")
  catch.estimates <- get.catch.estimates.f(cae,cae.stratflg,total.unlds,lfgrpd,lfgrpd.stratflg,lfmm,year,2,well.estimates,area.substitution.mat,grow.increments,PS=PS,Species=Species,my.FOmatrix,my.UNmatrix,my.DPmatrix)

  # print("Step 3: get fishery estimates")
  fishery.estimates <- fishery.estimates.f(catch.estimates$stratum.estimates.withsamps,catch.estimates$stratum.estimates.NOsamps,year,PS=PS,Species=Species)

  assign(paste0("fishery.estimates.", year), fishery.estimates, pos=1)
}
# save middle-step data as a record
save(list=objects(pat="fishery.estimates"),file=paste0(save_dir,"SKJ_",PS,"_2000-2023.RData"))

Get final DEL catch and comp output for the stock assessment

SKJ.DEL.Catch.20002023<-compile.catch.output.f(yr.start,yr.end,PS=PS,Species=Species,c("A1","A2")) # two DEL fisheries
SKJ.DEL.Comp.20002023<-compile.sizecomps.output.f(yr.start,yr.end,PS=PS,Species=Species)
write.csv(SKJ.OBJ.Catch.20002023,file=paste0(save_dir,"SKJ.OBJ.Catch.20002023.csv"),row.names = FALSE)
write.csv(SKJ.OBJ.Comp.20002023,file=paste0(save_dir,"SKJ.OBJ.Comp.20002023.csv"),row.names = FALSE)
write.csv(SKJ.NOA.Catch.20002023,file=paste0(save_dir,"SKJ.NOA.Catch.20002023.csv"),row.names = FALSE)
write.csv(SKJ.NOA.Comp.20002023,file=paste0(save_dir,"SKJ.NOA.Comp.20002023.csv"),row.names = FALSE)
write.csv(SKJ.DEL.Catch.20002023,file=paste0(save_dir,"SKJ.DEL.Catch.20002023.csv"),row.names = FALSE)
write.csv(SKJ.DEL.Comp.20002023,file=paste0(save_dir,"SKJ.DEL.Comp.20002023.csv"),row.names = FALSE)


HaikunXu/BSE documentation built on Nov. 22, 2024, 8:22 a.m.