---
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:
    toc: true
    toc_float: true
---

## Historical Time-Series
```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
Pars <- params$Pars$Hist
nsim <- dim(Pars@TSdata$VB)[1]
nyears <- dim(Pars@TSdata$VB)[2]

CurrentYr <- params$Pars$CurrentYr
histYr <- (CurrentYr-nyears+1):CurrentYr
futYr <- (CurrentYr+1):(CurrentYr+proyears)
Years <- histYr

```



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

#### Depletion 
Time-series plots of SB/SB0:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

Depletion <- apply(Pars@TSdata$SBiomass, 1:2, sum)/matrix(Pars@Ref$ReferencePoints$SSB0, nrow=nsim, ncol=nyears)
ylim <- c(0, max(c(1, max(Depletion))))
matplot(Years, t(Depletion),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Depletion (spawning biomass)", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')
maxL <- ceiling(max(ylim/0.2))*0.2
abline(h=seq(0.2, maxL, by=0.2), lwd=1, col="lightgray")
```


#### Absolute
Time-series plots of absolute SB:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

SSB <- apply(Pars@TSdata$SBiomass, 1:2, sum)
ylim <- c(0, max(SSB))
matplot(Years, t(SSB),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="SSB", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')
```



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

#### Depletion 
Time-series plots of VB/VB0:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

Depletion <- apply(Pars@TSdata$VBiomass, 1:2, sum)/matrix(Pars@Ref$ReferencePoints$VB0, nrow=nsim, ncol=nyears)
ylim <- c(0, max(c(1, max(Depletion))))
matplot(Years, t(Depletion),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Depletion (vulnerable biomass)", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')
maxL <- ceiling(max(ylim/0.2))*0.2
abline(h=seq(0.2, maxL, by=0.2), lwd=1, col="lightgray")
```


#### Absolute
Time-series plots of absolute VB:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

Vb <- apply(Pars@TSdata$VBiomass, 1:2, sum)
ylim <- c(0, max(Vb))
matplot(Years, t(Vb),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Vuln. Biomass", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')
```


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

#### Depletion
Time-series plots of B/B0:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

Depletion <- apply(Pars@TSdata$Biomass, 1:2, sum)/matrix(Pars@Ref$ReferencePoints$B0, nrow=nsim, ncol=nyears)
ylim <- c(0, max(c(1, max(Depletion))))
matplot(Years, t(Depletion),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Depletion (total biomass)", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')
maxL <- ceiling(max(ylim/0.2))*0.2
abline(h=seq(0.2, maxL, by=0.2), lwd=1, col="lightgray")
```

#### Absolute
Time-series plots of absolute B:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))


B <- apply(Pars@TSdata$Biomass, 1:2, sum)
ylim <- c(0, max(B))
matplot(Years, t(B),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Total Biomass", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')

```


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

#### Relative
Time-series plot of recruitment relative to R0:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))


Depletion <- apply(Pars@AtAge$Number[,1,,], 1:2, sum)/matrix( Pars@SampPars$Stock$R0, nrow=nsim, ncol=nyears)
ylim <- c(0, max(c(1,Depletion)))
matplot(Years, t(Depletion),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Recruitment (relative to R0)", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')
maxL <- ceiling(max(ylim/0.2))*0.2
abline(h=seq(0.2, maxL, by=0.2), lwd=1, col="lightgray")
```

#### Absolute
Time-series plot of absolute recruitment:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

Rec <- apply(Pars@AtAge$Number[,1,,], 1:2, sum)
ylim <- c(0, max(Rec))
matplot(Years, t(Rec),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Recruitment", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')

```


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

#### Relative
Time-series of catch relative to the current year:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

Depletion <- apply(Pars@TSdata$Landings, 1:2, sum)/matrix(apply(Pars@TSdata$Landings[,nyears,], 1, sum), nrow=nsim, ncol=nyears)
ylim <- c(0, max(c(1,max(Depletion))))
matplot(Years, t(Depletion),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Catch (relative to current year)", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i', axes=F)
axis(side=1)
maxL <- ceiling(max(ylim/0.5))*0.5
yax <- seq(0, maxL, by=0.25*maxL)
axis(side=2, at=yax, las=1)

abline(h=yax, lwd=1, col="lightgray")

```

#### Absolute 
Time-series of absolute catch:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

Catch <- apply(Pars@TSdata$Landings, 1:2, sum)
ylim <- c(0, max(Catch))
matplot(Years, t(Catch),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Catch", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')

```



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

#### Historical Time-Series
Time-series of historical fishing mortality:

```{r, echo=FALSE}
par(mfrow=c(1,1), oma=c(3,3,1,1), mar=c(1,1,1,1))

Find <- Pars@TSdata$Find * Pars@SampPars$Fleet$qs
ylim <- c(0, max(c(1, max(Find))))
matplot(Years, t(Find),
        type="l", lty=1, bty="l", main="", 
         lwd=params$plotPars$lwd, ylab="Fishing mortality rate (apical)", xlab="Historical Years", las=1, xpd=NA,
        ylim=ylim, yaxs = 'i')
maxL <- ceiling(max(ylim/0.2))*0.2
abline(h=seq(0.2, maxL, by=0.2), lwd=1, col="lightgray")
```

