inst/scripts/02.tables.r

---
title: "Snow crab tables"
author:
  - name: Snow crab group
    # orcid: 0000-0003-3632-5723 
    # email: jae.choi@dfo-mpo.gc.ca
    # email: choi.jae.seok@gmail.com
    # corresponding: true
    affiliation: 
      - name: Bedford Institute of Oceanography, Fisheries and Oceans Canada
        city: Dartmouth
        state: NS
        url: www.bio.gc.ca
date: 2024-08-17
keywords: 
  - snow crab fishery assessment
  - basic tables
abstract: |
  Snow crab demographic structure and basic fishery performance tables. 
toc: true
number-sections: true
highlight-style: pygments
bibliography: media/references.bib  
# csl: media/canadian-journal-of-fisheries-and-aquatic-sciences.csl  # see https://www.zotero.org/styles for more
license: "CC BY"
copyright: 
  holder: Jae S. Choi
  year: 2024
citation: 
  container-title: https://github.com/jae0/bio.snowcrab/
  doi: NA
funding: "The snow crab scientific survey was funded by the snow crab fishers of Maritimes Region of Atlantic Canada."
editor:
  render-on-save: false
format:
  html: 
    code-fold: true
    html-math-method: katex
    embed-resources: true
  pdf:
    pdf-engine: lualatex
  docx: default 
  beamer:
    pdf-engine: lualatex
---

<!-- Preamble
 
 
This is a Markdown/Quarto document ... To create HTML or PDF, etc, run: 

  make quarto FN=02.tables YR=2023 SOURCE=~/projects/bio.snowcrab/inst/markdown WK=~/bio.data/bio.snowcrab/assessments DOCEXTENSION=html # {via Quarto}

  # make rmarkdown FN=02.tables YR=2023 SOURCE=~/projects/bio.snowcrab/inst/markdown WK=~/bio.data/bio.snowcrab/assessments  DOCTYPE=pdf_document DOCEXTENSION=pdf {via Rmarkdown}

  # make pdf FN=02.tables  # {via pandoc}

Alter year and directories to reflect setup or copy Makefile and alter defaults to your needs.

YAML options:
jupyter: julia-1.10.4
https://quarto.org/docs/output-formats/all-formats.html

-->

````{=html}
<!-- NOTES: Make sure to have pulled observer data:

    year.assessment = 2023
    p = bio.snowcrab::load.environment( year.assessment=year.assessment )
    yrs = 1996:year.assessment # redo all years
    observer.db( DS="rawdata.redo", yrs=yrs )
    observer.db( DS="bycatch.redo", yrs=yrs )  
    observer.db( DS="odb.redo", p=p ) # 3 minutes
    observer.db( DS="bycatch_clean_data.redo", p=p, yrs=p$yrs ) # 3 minutes

quarto options:


The reserved prefixes are: fig, tbl, lst, tip, nte, wrn, imp, cau, thm, lem, cor, prp, cnj, def, exm, exr, sol, rem, eq, sec.
avoid using underscores (_) in labels and IDs as this can cause problems when rendering to PDF with LaTeX.

![Elephant](elephant.png){#fig-elephant}
Default     @fig-elephant   Figure 1
Capitalized     @Fig-elephant   Figure 1
Custom Prefix   [Fig @fig-elephant]     Fig 1
No Prefix   [-@fig-elephant]    1

::: {#fig-elephants layout-ncol=2}
![Surus](surus.png){#fig-surus}
![Hanno](hanno.png){#fig-hanno}
Famous Elephants
:::

See @fig-elephants for examples. In particular, @fig-hanno.

or:

#| label: fig-plots
#| fig-cap: "Plots" 
#| fig-subcap:
#|   - "Plot 1"
#|   - "Plot 2" 
#| layout-ncol: 2

| Col1 | Col2 | Col3 |
|------|------|------|
| A    | B    | C    |
| E    | F    | G    |
| A    | G    | G    |

: My Caption {#tbl-letters}

See @tbl-letters.

or 
::: {#tbl-letters}

| Col1 | Col2 | Col3 |
|------|------|------|
| A    | B    | C    |
| E    | F    | G    |
| A    | G    | G    |

My Caption

::: 

@fig-AAAA further explores the impact of temperature on ozone level.

```{r}
#| label: fig-AAAA
#| fig-cap: "Temperature and ozone level."
#| tbl-cap: "Fishery performance statistics."
#| eval: true
#| output: true
```

Black-Scholes (@eq-black-scholes) is a mathematical model:

$$
\frac{\partial \mathrm C}{ \partial \mathrm t } + \frac{1}{2}\sigma^{2} \mathrm S^{2}
\frac{\partial^{2} \mathrm C}{\partial \mathrm C^2}
  + \mathrm r \mathrm S \frac{\partial \mathrm C}{\partial \mathrm S}\ =
  \mathrm r \mathrm C 
$$ {#eq-black-scholes}

## Introduction {#sec-introduction}

See @sec-introduction 

---
# citations: https://quarto.org/docs/authoring/citations.html; https://pandoc.org/MANUAL.html#citations

Blah Blah [see @knuth1984, pp. 33-35; also @wickham2015, chap. 1]

    Blah Blah (see Knuth 1984, 3335; also Wickham 2015, chap. 1)

Blah Blah [@knuth1984, pp. 33-35, 38-39 and passim]

    Blah Blah (Knuth 1984, 3335, 3839 and passim)

Blah Blah [@wickham2015; @knuth1984].

    Blah Blah (Wickham 2015; Knuth 1984).

Wickham says blah [-@wickham2015]

    Wickham says blah (2015)

@knuth1984 says blah.

    Knuth (1984) says blah.

@knuth1984 [p. 33] says blah.

    Knuth (1984, 33) says blah.

#CSL: default is  Chicago Manual of Style author-date format

https://github.com/citation-style-language/styles
https://www.zotero.org/styles

### References

::: {#refs}
:::

```

-->
````

# Set up environment

First set up environment. Data comes from:

-   At seas observations of fishery (ISSDB)
-   Dockside monitoring (Marfis)
-   Snow crab survey

These are mostly imported and formatted in [01_snowcrab](https://github.com/jae0/bio.snowcrab/inst/scripts/01_snowcrab.R). The methods are mostly outlined in @Choi_et_al_2005b.

```{r}
#| label: setup
#| eval: true
#| output: false

  require(aegis)
  
  # Get data and format based upon parameters:
  year.assessment = 2023
  p = bio.snowcrab::load.environment( year.assessment=year.assessment )
  
  # loadfunctions( "aegis")
  # loadfunctions( "bio.snowcrab")  # in case of local edits

  # require(ggplot2)
  # require(data.table)

  require(gt)  # table formatting

  outtabledir = file.path( p$annual.results, "tables" )
  
  years = as.character(1996: year.assessment)

  regions = c("cfanorth", "cfasouth", "cfa4x")
  nregions = length(regions)

  FD = fishery_data()  # mass in tonnes
  fda = FD$summary_annual
  dt = as.data.frame( fda[ which(fda$yr %in% c(year.assessment - c(0:10))),] )
  dt =  dt[,c("region", "yr", "Licenses", "TAC", "landings", "effort", "cpue")] 
  names(dt) = c("Region", "Year", "Licenses", "TAC", "Landings", "Effort", "CPUE") 
  rownames(dt) = NULL
  odb0 = setDT(observer.db("odb"))
  odb0$region = NA
  for ( reg in regions) {
    r = polygon_inside(x = odb0, region = aegis.polygons::polygon_internal_code(reg), planar=FALSE)
    odb0$region[r] = reg
  }

```

## Fishery statistics from at sea observations

NENS:

```{r}
#| label: table-fishery-nens-perf
#| tbl-cap: "Fishery performance statistics in NENS. Units are: TACs and Landings (tons, t), Effort ($\\times 10^3$ trap hauls, th) and CPUE (kg/th)."
#| eval: true
#| output: true

ii = which(dt$Region=="cfanorth")
oo = dt[ii, c("Year", "Licenses", "TAC", "Landings", "Effort", "CPUE")] 
gt::gt(oo) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

SENS:

```{r}
#| label: table-fishery-sens-perf
#| tbl-cap: "Fishery performance statistics in SENS. Units are: TACs and Landings (tons, t), Effort ($\\times 10^3$ trap hauls, th) and CPUE (kg/th)."
#| eval: true
#| output: true

ii = which(dt$Region=="cfasouth")
oo = dt[ii, c("Year", "Licenses", "TAC", "Landings", "Effort", "CPUE")] 
gt::gt(oo) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

4X:

```{r}
#| label: table-fishery-4x-perf
#| tbl-cap: "Fishery performance statistics in 4X. Units are: TACs and Landings (tons, t), Effort ($\\times 10^3$ trap hauls, th) and CPUE (kg/th). There were no landings or TACs in 2018/2019 due to indications of low abundance. The 2022 season is ongoing."
#| eval: true
#| output: true

ii = which(dt$Region=="cfa4x")
oo = dt[ii, c("Year", "Licenses", "TAC", "Landings", "Effort", "CPUE")] 
gt::gt(oo) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

## At sea observed data

### Carapace condition from observed data \< 95mm CW

```{r}
#| label: setup-observer-data
#| eval: true
#| output: false
  odb = odb0[ cw < 95 & prodcd_id==0 & shell %in% c(1:5) & region %in% regions & sex==0, ]  # male
```

NENS:

```{r}
#| label: table-fishery-nens-sublegal
#| tbl-cap: "Fishery performance statistics in NENS. Distribution of at sea observations of males less than 95 mm CW by year and shell condition."
#| eval: true
#| output: true

resN = dcast( odb0[ region=="cfanorth", .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
if ( "NA" %in% names(resN) ) resN$"NA" = NULL
names(resN) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resN$Total = rowSums( resN[, 2:6 ], na.rm=TRUE)
resN[, 2:6 ] = round(resN[, 2:6 ] / resN$Total * 100, digits=2)
gt::gt(resN) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

SENS:

```{r}
#| eval: true
#| output: true
#| label: table-fishery-sens-sublegal
#| tbl-cap: "Fishery performance statistics in SENS. Distribution of at sea observations of males less than 95 mm CW by year and shell condition."
resS = dcast( odb0[ region=="cfasouth", .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
if ( "NA" %in%  names(resS)) resS$"NA" = NULL
names(resS) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resS$Total = rowSums( resS[, 2:6 ], na.rm=TRUE)
resS[, 2:6 ] = round(resS[, 2:6 ] / resS$Total * 100, digits=2)
gt::gt(resS) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

4X:

```{r}
#| eval: true
#| output: true
#| label: table-fishery-4x-sublegal
#| tbl-cap: "Fishery performance statistics in 4X. Distribution of at sea observations of males less than 95 mm CW by year and shell condition."
resX = dcast( odb0[ region=="cfa4x", .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
if ("NA" %in% names(resX)) resX$"NA" = NULL
names(resX) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resX$Total = rowSums( resX[, 2:6 ], na.rm=TRUE)
resX[, 2:6 ] = round(resX[, 2:6 ] / resX$Total * 100, digits=2)
gt::gt(resX) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

### Carapace condition from observed data \>= 95mm CW

```{r}
#| eval: true
#| output: false
odb = odb0[ cw >= 95 & cw < 170  & prodcd_id==0 & shell %in% c(1:5) & region %in% regions & sex==0, ]  # male
```

NENS:

```{r}
#| eval: true
#| output: true
#| label: table-fishery-nens-comm
#| tbl-cap: "Fishery performance statistics in NENS. Distribution of at sea observations of males greater than 95 mm CW by year and shell condition."
resN = dcast( odb[ region=="cfanorth", .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
names(resN) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resN$Total = rowSums( resN[, 2:6 ], na.rm=TRUE)
resN[, 2:6 ] = round(resN[, 2:6 ] / resN$Total * 100, digits=2)
gt::gt(resN) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

SENS:

```{r}
#| eval: true
#| output: true
#| label: table-fishery-sens-comm
#| tbl-cap: "Fishery performance statistics in SENS. Distribution of at sea observations of males greater than 95 mm CW by year and shell condition."
resS = dcast( odb[ region=="cfasouth", .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
names(resS) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resS$Total = rowSums( resS[, 2:6 ], na.rm=TRUE)
resS[, 2:6 ] = round(resS[, 2:6 ] / resS$Total * 100, digits=2)
gt::gt(resS) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

4X:

```{r}
#| eval: true
#| output: true
#| label: table-fishery-4x-comm
#| tbl-cap: "Fishery performance statistics in 4X. Distribution of at sea observations of males greater than 95 mm CW by year and shell condition."
resX = dcast( odb[ region=="cfa4x", .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
names(resX) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resX$Total = rowSums( resX[, 2:6 ], na.rm=TRUE)
resX[, 2:6 ] = round(resX[, 2:6 ] / resX$Total * 100, digits=2)
gt::gt(resX) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

### Percent soft from observed data

There are two possible definitions:

-   durometer \< 68 (Soft, Total)
-   carapace conditions 1 and 2 (SoftSC, TotalSC)

```{r}
#| eval: true
#| output: true
odb = odb0[ cw >= 95 & cw < 170  & prodcd_id==0 & shell %in% c(1:5) & region %in% regions & sex==0, ]  # male
shell_condition = odb[ !is.na(odb$region), .N, by=.(region, fishyr, shell) ]
shell_condition[, total:=sum(N, na.rm=TRUE), by=.(region, fishyr)]
shell_condition$percent = round(shell_condition$N / shell_condition$total, 3) * 100
shell_condition$Year = shell_condition$fishyr
```

NENS:

```{r}
#| eval: true
#| output: true
#| label: table-fishery-nens-soft-durometer
#| tbl-cap: "Fishery performance statistics in NENS. Distribution of at sea observations of males soft-shelled based on durometer (<68) and shell condition (1 and 2, SC)."
softN  = odb[ region=="cfanorth" & durometer <  68, .(Soft=.N), by=.(fishyr ) ] 
totalN = odb[ region=="cfanorth" & is.finite(durometer) , .(Total=.N), by=.(fishyr) ] 
resN = softN[totalN, on="fishyr"]
resN = resN[, .(Year=fishyr, Soft=round(Soft/Total*100,2), Total=Total) ]  
ssN = shell_condition[ region=="cfanorth" & shell %in% c(1,2), .(SoftSC=sum(percent), TotalSC=unique(total)[1]), by=.(Year)]
resN = resN[ssN, on="Year"]
gt::gt(resN) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

SENS:

```{r}
#| eval: true
#| output: true
#| label: table-fishery-sens-soft-durometer
#| tbl-cap: "Fishery performance statistics in SENS. Distribution of at sea observations of males soft-shelled  based on durometer (<68) and shell condition (1 and 2, SC)."
softS  = odb[ region=="cfasouth" & durometer <  68, .(Soft=.N), by=.(fishyr ) ] 
totalS = odb[ region=="cfasouth" & is.finite(durometer) , .(Total=.N), by=.(fishyr) ] 
resS = softS[totalS, on="fishyr"]
resS = resS[, .(Year=fishyr, Soft=round(Soft/Total*100,2), Total=Total) ]  
ssS = shell_condition[ region=="cfasouth" & shell %in% c(1,2), .(SoftSC=sum(percent), TotalSC=unique(total)[1]), by=.(Year)]
resS = resS[ssS, on="Year"]
gt::gt(resS) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

4X:

```{r}
#| eval: true
#| output: true
#| label: table-fishery-4x-soft-durometer
#| tbl-cap: "Fishery performance statistics in 4X. Distribution of at sea observations of males soft-shelled  based on durometer (<68) and shell condition (1 and 2, SC)."
softX  = odb[ region=="cfa4x" & durometer <  68, .(Soft=.N), by=.(fishyr ) ] 
totalX = odb[ region=="cfa4x" & is.finite(durometer) , .(Total=.N), by=.(fishyr) ] 
resX = softX[totalX, on="fishyr"]
resX = resX[, .(Year=fishyr, Soft=round(Soft/Total*100,2), Total=Total) ]  
ssX = shell_condition[ region=="cfa4x" & shell %in% c(1,2), .(SoftSC=sum(percent), TotalSC=unique(total)[1]), by=.(Year)]
resX = resX[ssX, on="Year"]
gt::gt(resX) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
    summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
    footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
    row_group.padding = gt::px(1))
```

<!--
# instars of interest: 11 and 12

# growth increment (assumming average weight in the midpoint of each increment)
growth.11.to.12 =  predict.mass.g.from.CW.mm( mean(CW.interval.male(12)) ) - predict.mass.g.from.CW.mm (mean(CW.interval.male(11)) )
(growth.11.to.12)
# = 419 g
#  12to13 = ~450
-->

### Compare discard rates Maritimes:

NENS:discard

```{r}
#| eval: true
#| output: true
#| warning: false
#| error: false 
#| label: table-fishery-nens-discard
#| tbl-cap: "Fishery performance statistics in NENS. Average by-catch discard rate by weight observed (kg/trap haul; and standard deviation, SD)."
region="cfanorth"
o = observer.db( DS="bycatch_summary", p=p,  yrs=p$yrs, region=region )   
resN = o$eff_summ[ order(fishyr), ]
names(resN) = c("Year", "Discards", "SD")
resN$Discards = round( resN$Discards*100, 2)
resN$SD = round( resN$SD*100, 2)
gt::gt(resN) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
  summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
  footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
  row_group.padding = gt::px(1))
```

SENS:

```{r}
#| eval: true
#| output: true
#| warning: false
#| error: false 
#| label: table-fishery-sens-discard
#| tbl-cap: "Fishery performance statistics in SENS. Average by-catch discard rate by weight observed (kg/trap haul; and standard deviation, SD)."
region="cfasouth"
o = observer.db( DS="bycatch_summary", p=p,  yrs=p$yrs, region=region )   
resS = o$eff_summ[ order(fishyr), ]
names(resS) = c("Year", "Discards", "SD")
resS$Discards = round( resS$Discards*100, 2)
resS$SD = round( resS$SD*100, 2)
gt::gt(resS) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
  summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
  footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
  row_group.padding = gt::px(1))
```

4X:

```{r}
#| eval: true
#| output: true
#| warning: false
#| error: false 
#| label: table-fishery-4x-discard
#| tbl-cap: "Fishery performance statistics in 4X. Average by-catch discard rate by weight observed (kg/trap haul; and standard deviation, SD)."
region="cfa4x"
o = observer.db( DS="bycatch_summary", p=p,  yrs=p$yrs, region=region )   
resX = o$eff_summ[ order(fishyr), ]
names(resX) = c("Year", "Discards", "SD")
resX$Discards = round( resX$Discards*100, 2)
resX$SD = round( resX$SD*100, 2)
gt::gt(resX) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
  summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
  footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
  row_group.padding = gt::px(1))
```

## Survey-based tables

### Carapace condition from trawl data \>= 95mm CW

```{r}
#| eval: true
#| output: false
det = snowcrab.db( p=p, DS="det.georeferenced" )
setDT(det)
det$fishyr = det$yr  ## the counting routine expectes this variable
det = det[ cw >= 95 ,]  # commerical sized crab only
years = sort( unique( det$yr ) )
det$region = NA
for ( reg in regions) {
  r = polygon_inside(x = det, region = aegis.polygons::polygon_internal_code(reg), planar=FALSE)
  det$region[r] = reg
}

```

NENS:

```{r}
#| eval: true
#| output: true
#| label: table-survey-nens-comm
#| tbl-cap: "Distribution of NENS survey: males less than 95 mm CW by year and shell condition."
resN = dcast( det[ region=="cfanorth" & !is.na(shell), .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
names(resN) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resN$Total = rowSums( resN[, 2:6 ], na.rm=TRUE)
resN[, 2:6 ] = round(resN[, 2:6 ] / resN$Total * 100, digits=2)
gt::gt(resN) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
  summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
  footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
  row_group.padding = gt::px(1))
```

SENS:

```{r}
#| eval: true
#| output: true
#| label: table-survey-sens-comm
#| tbl-cap: "Distribution of SENS survey: males less than 95 mm CW by year and shell condition."
resS = dcast( det[ region=="cfasouth" & !is.na(shell), .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
names(resS) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resS$Total = rowSums( resS[, 2:6 ], na.rm=TRUE)
resS[, 2:6 ] = round(resS[, 2:6 ] / resS$Total * 100, digits=2)
gt::gt(resS) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
  summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
  footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
  row_group.padding = gt::px(1))
```

4X:

```{r}
#| eval: true
#| output: true
#| label: table-survey-4X-comm
#| tbl-cap: "Distribution of 4X survey: males less than 95 mm CW by year and shell condition."
resX = dcast( det[ region=="cfa4x" & !is.na(shell), .(N=.N), by=.(fishyr, shell) ], fishyr  ~ shell, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
names(resX) = c("Year", "CC1", "CC2", "CC3", "CC4", "CC5" )
resX$Total = rowSums( resX[, 2:6 ], na.rm=TRUE)
resX[, 2:6 ] = round(resX[, 2:6 ] / resX$Total * 100, digits=2)
gt::gt(resX) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
  summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
  footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
  row_group.padding = gt::px(1))
```

### Counts of stations in each area

```{r}
#| eval: true
#| output: true
#| label: table-survey-station-count
#| tbl-cap: "Survey station counts"
set = snowcrab.db(p=p, DS="set.clean")
setDT(set)
# check towquality .. this should always == 1
if (length( unique( set$towquality) ) != 1 ) print("error -- not good tows")
set$region = NA
for (reg in c( "cfanorth", "cfasouth", "cfa4x"  ) ) {
  d = polygon_inside(set[,c("lon","lat")], reg)
  set$region[d] = reg 
}
out = dcast( set[, .(N=.N), by=.(region, yr)], yr~region, value.var="N", fill=0, drop=FALSE, na.rm=TRUE )
out[,Total:=sum(cfanorth,cfasouth,cfa4x, na.rm=TRUE)]
out = out[, .(yr, cfanorth, cfasouth, cfa4x)]
names(out) = c("Year", "NENS", "SENS", "4X")
gt::gt(out) |> gt::tab_options(table.font.size = 12, data_row.padding = gt::px(1), 
  summary_row.padding = gt::px(1), grand_summary_row.padding = gt::px(1), 
  footnotes.padding = gt::px(1), source_notes.padding = gt::px(1), 
  row_group.padding = gt::px(1))
```

## References

<!--

deprecated = TRUE
if (deprecated) {
  # the following are deprecated methods as of 2024 (JC), here only for reference .. most functionality is nowin the Quarto/Rmarkdown above. 
  

  # Tables obtained after completion of data assimilation and processing up to the end of "01.snowcrab.r"
 
  year.assessment = 2023

  p = bio.snowcrab::load.environment( year.assessment=year.assessment )
 

  require(gridExtra)
  library("xtable")
  library("R2HTML")

  odb0 = observer.db("odb")
  regions = c("cfanorth", "cfasouth", "cfa4x")
  nregions = length(regions)

  #------------------------------------------------
  #Fisheries statistics per region
  tabledir = file.path(project.datadirectory("bio.snowcrab"), "data", "fisheries")
  outtabledir= file.path(project.datadirectory("bio.snowcrab"), "assessments", p$year.assessment, "tables", "logbook")
  if(!dir.exists(tabledir)) dir.create(tabledir, recursive =T)
  if(!dir.exists(outtabledir)) dir.create(outtabledir, recursive =T)

  setwd(tabledir)

  NFS <- xtable(read.csv("NENS_FisherySummary.csv"))
  SFS <- xtable(read.csv("SENS_FisherySummary.csv"))
  Fx <- xtable(read.csv("4x_FisherySummary.csv"))

  setwd(outtabledir)
  print.xtable(NFS, type="latex", file="NENS_FisherySummary.tex")
  print.xtable(NFS, type="html", file="NENS_FisherySummary.html")

  print.xtable(SFS, type="latex", file="SENS_FisherySummary.tex")
  print.xtable(SFS, type="html", file="SENS_FisherySummary.html")

  print.xtable(Fx, type="latex", file="4x_FisherySummary.tex")
  print.xtable(Fx, type="html", file="4x_FisherySummary.html")

  #regions = c("cfaall")
  #regions = c("cfanorth", "cfasouth", "cfa4x")
  regions = c("cfanorth", "cfa23", "cfa24", "cfa4x")
  l = NULL
  for (r in regions) {
    res = get.fishery.stats.by.region( Reg=r) #need to add the TACs per year and number of licences
    #round the landings to ton
    #round CPUE to no decimal places
    #round the effort to per x1000 trap hauls
    print(r)
    print(res)
  }


  # ----------------------------------------
  #  Carapace condition from observed data  < 95mm CW

  outtabledir= file.path(project.datadirectory("bio.snowcrab"), "assessments", p$year.assessment, "tables", "observer")
  dir.create(outtabledir, recursive=TRUE)

  odb = odb0
  odb = odb[ which( odb$cw < 95 & odb$prodcd_id=="0" ) ,]
  regions = c("cfanorth", "cfasouth", "cfa4x")
  nregions = length(regions)
  years = sort( unique( odb$fishyr ) )

  res = NULL
  for (r in p$regions) {
    for (y in years) {
      out = proportion.cc (odb, region=r, year=y)
      res = rbind( res, cbind( r, y, t(out)) )
    }}

  cnames = c("region", "fishyr", c(1:5), "ntot")
  colnames(res) = cnames
  print(res)
  res = as.data.frame(res)
  res[is.na(res)] <- NA
  ct <- c("CC1", "CC2", "CC3", "CC4", "CC5", "Total")

  setwd(outtabledir)

  Rn= res[res$region=="cfanorth", 3:8]
  print(Rn)
  rownames(Rn) = years
  colnames(Rn) = ct
  print.xtable(Rn, type="latex", file="table.CC.small.north.obs.tex")
  HTML(Rn, file="table.CC.Small.north.obs.html")

  Rs= res[res$region=="cfasouth", 3:8]
  rownames(Rs) = years
  colnames(Rs) = ct
  print.xtable(Rs, type="latex", file="table.CC.small.south.obs.tex")
  HTML(Rs, file="table.CC.small.south.obs.html")

  Rx= res[res$region=="cfa4x", 3:8]
  rownames(Rx) = years
  colnames(Rx) = ct
  print.xtable(Rs, type="latex", file="table.CC.small.4x.obs.tex")
  HTML(Rx, file="table.CC.small.4x.obs.html")


  # ----------------------------------------
  #  Carapace condition from observed data >=95mm CW
  odb = odb0
  odb = odb[ which( odb$cw >= 95 & odb$cw < 170 & odb$prodcd_id=="0" ) ,]  # commerical sized crab only
  years = sort( unique( odb$fishyr ) )

  # get proportion by cc
  regions = c("cfanorth", "cfasouth", "cfa4x")
  years = sort( unique( odb$fishyr ) )

  res = NULL
  for (r in regions) {
    for (y in years) {
      out = proportion.cc (odb, region=r, year=y)
      res = rbind( res, cbind( r, y, t(out)) )
    }}

  cnames = c("region", "fishyr", c(1:5), "ntot")
  colnames(res) = cnames
  print(res)
  res = as.data.frame(res)
  res[is.na(res)] <- NA
  #  for (i in cnames[-1]) res[,i] = as.numeric(as.character((res[,i])))
  setwd(outtabledir)

  ct <- c("CC1", "CC2", "CC3", "CC4", "CC5")
  Rn = res[res$region=="cfanorth", 3:7]
  #Rn = as.matrix( res[ which(res$region=="cfanorth") , as.character(c(1:5)) ] )
  rownames(Rn) = years
  colnames(Rn) = ct
  print.xtable(Rn, type="latex", file="table.CC.large.north.obs.tex")
  HTML(Rn, file="table.CC.large.north.obs.html")

  #Rs = as.matrix( res[ which(res$region=="cfasouth") , as.character(c(1:5)) ] )
  Rs = res[res$region=="cfasouth", 3:7]
  rownames(Rs) = years
  colnames(Rs) = ct
  print.xtable(Rs, type="latex", file="table.CC.large.south.obs.tex")
  HTML(Rs, file="table.CC.large.south.obs.html")

  #Rx = as.matrix( res[ which(res$region=="cfa4x") , as.character(c(1:5)) ] )
  Rx = res[res$region=="cfa4x", 3:7]
  rownames(Rx) = years
  colnames(Rx) = ct
  print.xtable(Rx, type="latex", file="table.CC.large.4x.obs.tex")
  HTML(Rx, file="table.CC.large.4x.obs.html")

  # ----------------------------------------
  #  Percent soft from observed data

  odb = odb0
  odb = odb[ which( odb$cw > 95 & odb$cw < 170 & odb$prodcd_id=="0" ) ,]  # commercial crab
  years = sort( unique( odb$fishyr ) )


  res = NULL
  for (r in p$regions) {
    for (y in years) {
      out = proportion.soft (odb, region=r, year=y)
      res = rbind( res, cbind( r, y, t(out)) )
    }}

  cnames = c("region", "fishyr", "pr.soft", "nsoft", "ntot")
  colnames(res) = cnames
  print(res)
  res = as.data.frame(res)

  for (i in cnames[-1]) res[,i] = as.numeric(as.character((res[,i])))

  Rn = as.matrix( res[ which(res$region=="cfanorth") , ] )
  rownames(Rn) = years
  HTML(Rn, file="table.proportion.soft.north.obs.html")

  Rs = as.matrix( res[ which(res$region=="cfasouth" ),   ] )
  rownames(Rs) = years
  HTML(Rs, file="table.proportion.soft.south.obs.html")

  Rx = as.matrix( res[ which(res$region=="cfa4x") , ] )
  rownames(Rx) = years
  HTML(Rx, file="table.proportion.soft.4x.obs.html")



  # instars of interest: 11 and 12

  # growth increment (assumming average weight in the midpoint of each increment)
  growth.11.to.12 =  predict.mass.g.from.CW.mm( mean(CW.interval.male(12)) ) - predict.mass.g.from.CW.mm (mean(CW.interval.male(11)) )

  # = 419 g
  #  12to13 = ~450



  # Table of proportion discarded
  odb = observer.db("odb")
  regions = c("cfanorth", "cfasouth", "cfa4x")
  years = sort( unique( odb$fishyr ) )
  out = NULL
  for (r in regions) {
    for (y in years) {
      res = proportion.legal (odb, region=r, year=y)
      out = rbind(out, cbind( r, y, res[1], res[2], res[3] ) )
    } }
  out

  HTML(out, file="table.proportion.discarded.html")


  # ---------------------------------------- USED
  #  Carapace condition from trawl data  >= 95mm CW  ... not kriged .. simple proportions

  det0 = snowcrab.db( p=p, DS="det.georeferenced" )
  det0$fishyr = det0$yr  ## the counting routine expectes this variable

  det = det0[ which( det0$cw >= 95 ) ,]  # commerical sized crab only
  years = sort( unique( det$yr ) )

  res = NULL
  for (r in p$regions) {
    for (y in years) {
      out = proportion.cc (det, region=r, year=y)
      res = rbind( res, cbind( r, y, t(out)) )
    }}

  cnames = c("region", "fishyr", c(1:5), "ntot")
  colnames(res) = cnames
  print(res)
  res = as.data.frame(res)

  for (i in cnames[-1]) res[,i] = as.numeric(as.character((res[,i])))
  (res)

  HTML(res, file="table.CC.large.survey.html")

  # ------------------
  # counts of stations in each area

  # check towquality .. this should always == 1
  set = snowcrab.db(p=p, DS="set.clean")
  if (length( unique( set$towquality) ) != 1 ) print("error -- not good tows")

  out = data.frame(yr=sort( unique(set$yr )) )
  for (reg in c("cfaall", "cfanorth", "cfasouth","cfa4x"  ) ) {
    d = polygon_inside(set[,c("lon","lat")], reg)
    e = as.data.frame( xtabs(~yr, data=set[d,])  )
    names(e) = c("yr", reg)
    e$yr = as.numeric(as.character(e$yr) )
    out = merge(out, e, by="yr", all=T)
  }
  print(out)
 
  HTML(out, file="table.tow_counts_survey.html")



  # % mat calculations: deprecated  ... size analysis is now in 01_snowcrab.R

  # loc = file.path(sc.R, "size.data")
  # dir.create(path=loc, recursive=T, showWarnings=F)
  # outfilename = paste( c("mi", "mm", "fi", "fm"), "rdata", sep=".")
  # outfile = file.path(loc, paste(outfilename))
  # for (f in  outfile) load(f)


  # f.i = f.imm[which( rownames(f.imm)%in% sids ) ,]
  # f.i.means = apply(X=f.i, MARGIN=2, FUN=mean)
  # f.m = f.mat[which( rownames(f.mat)%in% sids ) ,]
  # f.m.means = apply(X=f.m, MARGIN=2, FUN=mean)

  # toplot = rbind(f.m.means, f.i.means)

  # ii = as.data.frame(t(toplot))
  # ii$cw = as.numeric(rownames(ii))
  # ii$pmat = ii[,1]/ (ii[,1]+ii[,2]) * 100

  # plot(ii$cw, ii$pmat)
  # abline(h=50)

  # str(ii)
  #`data.frame':   70 obs. of  4 variables:
  # $ f.m.means: num  0 0 0 0 0 ...
  # $ f.i.means: num   2.80  6.19 20.05 24.29 74.11 ...
  # $ cw       : num  12 14 16 18 20 22 24 26 28 30 ...
  # $ pmat     : num  0 0 0 0 0 ...
 

  # ----------------------------------------   NOT USED ____________
  #  Carapace condition from trawl data  < 95mm CW  ... not kriged .. simple proportions

  det0 = snowcrab.db( p=p, DS="det.georeferenced" )
  det0$fishyr = det0$yr  ## the counting routine expectes this variable

  det = det0[ which( det0$cw < 95 ) ,]  # commerical sized crab only
  years = sort( unique( det$yr ) )

  res = NULL
  for (r in p$regions) {
    for (y in years) {
      out = proportion.cc (det, region=r, year=y)
      res = rbind( res, cbind( r, y, t(out)) )
    }}

  cnames = c("region", "fishyr", c(1:5), "ntot")
  colnames(res) = cnames
  print(res)
  res = as.data.frame(res)

  for (i in cnames[-1]) res[,i] = as.numeric(as.character((res[,i])))
  (res)



}
-->
jae0/bio.snowcrab documentation built on Nov. 6, 2024, 10:10 p.m.