library(spict)

## Fleet=1: catches, Fleet > 1 : indices of exploitable biomass, Fleet=999 : effort
d<-read.csv("data/data.txt", skip=1, comment.char="#")

d$time_mid = (d$time_end + d$time_start)*0.5;
d$dt = (d$time_end - d$time_start)

eff = d[ d$fleet==999 , ]
d = subset( d, fleet!=999 )

dfleet=split(d,d$fleet)

getnamedEl<-function(x,name)x[[name]]

inp=list(obsC=dfleet[[1]]$obs,
         obsI=lapply(dfleet[-1],getnamedEl,name="obs"),
         timeC=dfleet[[1]]$time_start,
         timeI=lapply(dfleet[-1],getnamedEl,name="time_mid"),
         dtc=dfleet[[1]]$dt,
         stdevfacI=lapply(dfleet[-1],getnamedEl,name="stdevfac"),
         stdevfacC=dfleet[[1]]$stdevfac
         )

if(nrow(eff)>0){
    inp$obsE <- eff$obs
    inp$timeE <- eff$time_start
    inp$dte <- eff$dt
    inp$stdevfacE <- eff$stdevfac
}
## Numerical solver time step (probably don't need to change)
inp$dteuler <- 1/16

## Don't report covariances -- not needed and size of object is much smaller without
inp$getReportCovariance = FALSE 
###########################
## Model configuration
###########################

## Priors
## (value, std.dev, 0/1=Off/On)
## logsdi may be given as a list of vectors, one for each index.
##inp$priors$logn <- c(log(2), 1, 1)
##inp$priors$logalpha <- c(log(2), 3, 1)
##inp$priors$logbeta <- c(log(2), 1, 1)
##inp$priors$logbkfrac <- c(log(1), 1, 1)
##inp$priors$logsdi <- c(log(0.5), 1, 1) 
##inp$priors$logsdc <- c(log(0.2), 1, 1)
# Predict catches until this year
inp$timepredc <- 1990

# Multiply last F with ffac and forecast using resulting F
inp$ffac <- 1.00

## Fix n to 2 (Schaefer)
#inp$ini$logn <- log(2)
#inp$phases$logn <- -1

inp<-check.inp(inp)
fit<-fit.spict(inp)
if(fit$opt$convergence!=0) stop("Error: model did not converge.");
fit<-calc.osa.resid(fit)

man<-manage(fit)


## Do not edit lines below 
attr(fit, "RemoteSha") <- substr(packageDescription("spict")$RemoteSha, 1, 12)
attr(fit, "Version") <- packageDescription("spict")$Version
save(fit,file="run/fit.RData")
cat("Version:",attr(fit,"Version"),"\n", file="run/ver")
cat("RemoteSha:", attr(fit,"RemoteSha"),"\n", file="run/ver", append=TRUE)

