library(stockassessment)

oldwd<-setwd("data")

  cn<-read.ices("cn.dat")
  cw<-read.ices("cw.dat")
  dw<-read.ices("dw.dat")
  lw<-read.ices("lw.dat")
  mo<-read.ices("mo.dat")
  nm<-read.ices("nm.dat")
  pf<-read.ices("pf.dat")
  pm<-read.ices("pm.dat")
  sw<-read.ices("sw.dat")
  lf<-read.ices("lf.dat")
  surveys<-read.ices("survey.dat")

  sv<-stockassessment:::read.surveys("survey_variance.txt")
  W1<-(surveys[[2]]^2)/sv[[1]]  # rationale sqrt(sv)/obs ~ cv ~ sd on log scale, hence (obs^2)/sv approx weight  
  W2<-(surveys[[3]]^2)/sv[[2]]
  attr(surveys[[2]],"weight")<-W1
  attr(surveys[[3]],"weight")<-W2

# Remove catch numbers-at-age value, considered unreliable
# As per WGCSE 2022
cn["2021", c("0", "1")] <- NA

setwd(oldwd)

dat<-setup.sam.data(surveys=surveys,
                    residual.fleet=cn, 
                    prop.mature=mo, 
                    stock.mean.weight=sw, 
                    catch.mean.weight=cw, 
                    dis.mean.weight=dw, 
                    land.mean.weight=lw,
                    prop.f=pf, 
                    prop.m=pm, 
                    natural.mortality=nm, 
                    land.frac=lf)


save(dat, file="run/data.RData")
