demo/BET_19751999.R

library(BSE)
# global setup
raw_data_dir <- "D:/OneDrive - IATTC/IATTC/2022/BSE stuff from Cleridy/single spp programs_PS_1975-1999/Raw data extractions/"
save_dir <- "D:/OneDrive - IATTC/IATTC/2022/BSE stuff from Cleridy/BET/"
yr.start <- 1975
yr.end <- 1999
Species <- "BET"
# grow.increments <- grow.increments.betyftskj # the growth increment matrix

# Load the base files generated by Cleridy
load("D:/OneDrive - IATTC/IATTC/2022/BSE stuff from Cleridy/single spp programs_PS_1975-1999/Raw data extractions/base files_1975-1999_for SAC 2020.RData")


# Running BET OBJ
PS <- "OBJ"

cae.stratflg <- create.strat.flg.f(cae$latc5,cae$lonc5,is.lwrght=F,cae$month,cae$setype,cae$class,PS=PS,Species=Species)
check.strat.flg.f(cae$latc5,cae$lonc5,cae.stratflg)

# Loop to get all years' fishery estimates
for(year in yr.start:yr.end) {
  print(paste0("Year: ",year))
  
  print("Step 1: get well estimates")
  well.estimates <- well.estimates.7599.f(year)
  
  print("Step 2: get catch estimates")
  if(year!=1988) catch.estimates <- get.catch.estimates.7599.f(cae,cae.stratflg,corrected.unlds,lfgrpd,lfmm,year,2,well.estimates,PS,Species)
  else catch.estimates <- get.catch.estimates.7599.f(cae,cae.stratflg,corrected.unlds,lfgrpd,lfmm,year,1,well.estimates,PS,Species)
  
  print("Step 3: get fishery estimates")
  # str(catch.estimates$stratum.estimates.withsamps)
  fishery.estimates <- call.fishery.estimates.f(catch.estimates$stratum.estimates.withsamps,catch.estimates$totunlds.bystrat,year,PS,Species)
  
  fishery.estimates.bet <- fishery.estimates$bet
  
  assign(paste0("fishery.estimates.bet.", year), fishery.estimates.bet, pos=1)
}
save(list=objects(pat="fishery.estimates.bet."),file=paste0(save_dir,"BET_",PS,"_1975-1999.RData"))

# get final catch and comp output for the stock assessment
BET.OBJ.Catch.19751999<-compile.catch.output.7599.f(yr.start,yr.end,PS=PS,Species=Species,c("A1","A2","A3","A4","A5"))
BET.OBJ.Comp.19751999<-compile.sizecomps.output.7599.f(yr.start,yr.end,PS=PS,Species=Species)


# Running BET NOA
PS <- "NOA"

cae.stratflg <- create.strat.flg.f(cae$latc5,cae$lonc5,is.lwrght=F,cae$month,cae$setype,cae$class,PS=PS,Species=Species)
check.strat.flg.f(cae$latc5,cae$lonc5,cae.stratflg)

# Loop to get all years' fishery estimates
for(year in yr.start:yr.end) {
  print(paste0("Year: ",year))
  
  print("Step 1: get well estimates")
  well.estimates <- well.estimates.7599.f(year)
  
  print("Step 2: get catch estimates")
  if(year!=1988) catch.estimates <- get.catch.estimates.7599.f(cae,cae.stratflg,corrected.unlds,lfgrpd,lfmm,year,2,well.estimates,PS,Species)
  else catch.estimates <- get.catch.estimates.7599.f(cae,cae.stratflg,corrected.unlds,lfgrpd,lfmm,year,1,well.estimates,PS,Species)
  
  print("Step 3: get fishery estimates")
  # str(catch.estimates$stratum.estimates.withsamps)
  fishery.estimates <- call.fishery.estimates.f(catch.estimates$stratum.estimates.withsamps,catch.estimates$totunlds.bystrat,year,PS,Species)
  
  fishery.estimates.bet <- fishery.estimates$bet
  
  assign(paste0("fishery.estimates.bet.", year), fishery.estimates.bet, pos=1)
}
save(list=objects(pat="fishery.estimates.bet."),file=paste0(save_dir,"BET_",PS,"_1975-1999.RData"))

# get final catch and comp output for the stock assessment
BET.NOA.Catch.19751999<-compile.catch.output.7599.f(yr.start,yr.end,PS=PS,Species=Species,c("A1","A2","A3","A4","A5"))
BET.NOA.Comp.19751999<-compile.sizecomps.output.7599.f(yr.start,yr.end,PS=PS,Species=Species)


# Running BET DEL
PS <- "DEL"

cae.stratflg <- create.strat.flg.f(cae$latc5,cae$lonc5,is.lwrght=F,cae$month,cae$setype,cae$class,PS=PS,Species=Species)
check.strat.flg.f(cae$latc5,cae$lonc5,cae.stratflg)

# Loop to get all years' fishery estimates
for(year in yr.start:yr.end) {
  print(paste0("Year: ",year))
  
  print("Step 1: get well estimates")
  well.estimates <- well.estimates.7599.f(year)
  
  print("Step 2: get catch estimates")
  if(year!=1988) catch.estimates <- get.catch.estimates.7599.f(cae,cae.stratflg,corrected.unlds,lfgrpd,lfmm,year,2,well.estimates,PS,Species)
  else catch.estimates <- get.catch.estimates.7599.f(cae,cae.stratflg,corrected.unlds,lfgrpd,lfmm,year,1,well.estimates,PS,Species)
  
  print("Step 3: get fishery estimates")
  # str(catch.estimates$stratum.estimates.withsamps)
  fishery.estimates <- call.fishery.estimates.f(catch.estimates$stratum.estimates.withsamps,catch.estimates$totunlds.bystrat,year,PS,Species)
  
  fishery.estimates.bet <- fishery.estimates$bet
  
  assign(paste0("fishery.estimates.bet.", year), fishery.estimates.bet, pos=1)
}
save(list=objects(pat="fishery.estimates.bet."),file=paste0(save_dir,"BET_",PS,"_1975-1999.RData"))

# get final catch and comp output for the stock assessment
BET.DEL.Catch.19751999<-compile.catch.output.7599.f(yr.start,yr.end,PS=PS,Species=Species,c("A1","A2","A3","A4","A5"))
BET.DEL.Comp.19751999<-compile.sizecomps.output.7599.f(yr.start,yr.end,PS=PS,Species=Species)


# save results
write.csv(BET.OBJ.Catch.19751999,file=paste0(save_dir,"BET.OBJ.Catch.19751999.csv"),row.names = FALSE)
write.csv(BET.OBJ.Comp.19751999,file=paste0(save_dir,"BET.OBJ.Comp.19751999.csv"),row.names = FALSE)
write.csv(BET.NOA.Catch.19751999,file=paste0(save_dir,"BET.NOA.Catch.19751999.csv"),row.names = FALSE)
write.csv(BET.NOA.Comp.19751999,file=paste0(save_dir,"BET.NOA.Comp.19751999.csv"),row.names = FALSE)
write.csv(BET.DEL.Catch.19751999,file=paste0(save_dir,"BET.DEL.Catch.19751999.csv"),row.names = FALSE)
write.csv(BET.DEL.Comp.19751999,file=paste0(save_dir,"BET.DEL.Comp.19751999.csv"),row.names = FALSE)
HaikunXu/BSE documentation built on Nov. 22, 2024, 8:22 a.m.