---
params: 
    title: ""
    Pars: "`r list()`"
    plotPars: "`r list()`"
    tabs: "`r TRUE`"
    its: " `r numeric()`"
    nyears: " `r numeric()`"
    proyears: " `r numeric()`"
    date: "`r Sys.Date()`"
title: "`r params$title`"
date: "`r params$date`"
output:
  html_document
---


```{r, echo=FALSE, results="asis"}
if (params$tabs) {
  cat('### Growth Parameters {.tabset .tabset-fade .tabset-pills}' )
} else {
  cat('### Growth Parameters')
}

dd <- params$Pars$M_ageArray %>% dim()
nsim <- dd[1]
maxage <- dd[2]

nsamp <- length(params$its)
```

#### Sampled Parameters 
Histograms of `r nsim` simulations of von Bertalanffy growth parameters `Linf`, `K`, and `t0`, and inter-annual variability in Linf and K (`Linfsd` and `Ksd`), with vertical colored lines indicating `r nsamp` randomly drawn values used in other plots:

```{r, echo=FALSE}
par(mfrow=c(2,3))
hist2(Pars$Linf, main="Linf", col=params$plotPars$col, axes=params$plotPars$axes,
      breaks=params$plotPars$breaks, cex.main=params$plotPars$cex.main)
abline(v=Pars$Linf[params$its], col=1:nsamp, lwd=params$plotPars$lwd)
axis(side=1) 

hist2(Pars$Linfsd, main="Linfsd", col=params$plotPars$col, axes=params$plotPars$axes,
      breaks=params$plotPars$breaks, cex.main=params$plotPars$cex.main)
abline(v=Pars$Linfsd[params$its], col=1:nsamp, lwd=params$plotPars$lwd)
axis(side=1) 

hist2(Pars$K, main="K", col=params$plotPars$col, axes=params$plotPars$axes,
      breaks=params$plotPars$breaks, cex.main=params$plotPars$cex.main)
abline(v=Pars$K[params$its], col=1:nsamp, lwd=params$plotPars$lwd)
axis(side=1) 

hist2(Pars$Ksd, main="Ksd", col=params$plotPars$col, axes=params$plotPars$axes,
      breaks=params$plotPars$breaks, cex.main=params$plotPars$cex.main)
abline(v=Pars$Ksd[params$its], col=1:nsamp, lwd=params$plotPars$lwd)
axis(side=1)

hist2(Pars$t0, main="t0", col=params$plotPars$col, axes=params$plotPars$axes,
      breaks=params$plotPars$breaks, cex.main=params$plotPars$cex.main)
abline(v=Pars$t0[params$its], col=1:nsamp, lwd=params$plotPars$lwd)
axis(side=1) 

```

#### Time-Series
The Linf and K parameters in each year for `r nsamp` simulations. The vertical dashed line indicates the end of the historical period:

```{r, echo=FALSE}
years <- c(seq(-nyears+1, 0, by=1), seq(1, proyears,1))

matplot(years, t(Pars$Linfarray[params$its,]), type="l", bty="l", main="", lwd=params$plotPars$lwd, lty=1, ylab="Linf", xlab="Year")
abline(v=0, col="darkgray", lty=2)
matplot(years, t(Pars$Karray[params$its,]), type="l", bty="l", main="", lwd=params$plotPars$lwd, lty=1, ylab="K", xlab="Year")
abline(v=0, col="darkgray", lty=2)
```

#### Growth Curves
Sampled length-at-age curves for `r nsamp` simulations in the first historical year, the last historical year, and the last projection year.
```{r, echo=FALSE}
par(mfrow=c(1,3), oma=c(2,3,1,1), mar=c(3,2,1,1))
fstYr <- Pars$Len_age[params$its,,1]
curYr <- Pars$Len_age[params$its,,nyears]
lstYr <- Pars$Len_age[params$its,,proyears+nyears]
MaxL <- max(Pars$Len_age)
matplot(t(fstYr), type="l", bty="l", main="First historical year", ylim=c(0, MaxL), 
        xlab="Age", ylab="Length", cex.lab=2, lwd=params$plotPars$lwd, lty=1, xpd=NA)
matplot(t(curYr), type="l", bty="l", main="Last historical year", ylim=c(0, MaxL),  
        axes=params$plotPars$axes, xlab="Age", ylab="", cex.lab=2, lwd=params$plotPars$lwd, lty=1, xpd=NA)
axis(side=1)
axis(side=2, labels=FALSE)  
matplot(t(lstYr), type="l", bty="l", main="Last projected year", ylim=c(0, MaxL),
        axes=params$plotPars$axes, xlab="Age", ylab="", cex.lab=2, lwd=params$plotPars$lwd, lty=1, xpd=NA)	
axis(side=1)
axis(side=2, labels=FALSE)  
```

