---
title: "Ingest SWFSC California Current cetacean densities (`cce`)"
subtitle: "Becker et al. 2020: 13 species, summer–fall habitat-based density on 0.1° cells, area-weighted onto the global 0.05° grid → population-percentile suitability [1,100]"
msens:
target_name: ingest_swfsc_cce
workflow_type: ingest
dependency: [build_cell_grid]
output: data/manifests/ingest_swfsc_cce.json
dataset:
ds_key: cce
response_type: density
source_authority: NOAA SWFSC
temporal_interval: seasonal
native_format: vector
name_short: "SWFSC CCE cetacean densities"
name_display: "SWFSC California Current cetacean densities (Becker et al. 2020)"
description: "NOAA SWFSC habitat-based density models for 13 cetacean species in the California Current Ecosystem (Becker et al. 2020; 1991-2018 line-transect surveys, summer-fall), delivered as animals per km2 on 0.1 degree cells and area-weighted onto the 0.05 degree grid. Mapped onto [1,100] by the population-percentile transform (ud), which is scale-free. With one modelled interval, the annual surface is that interval."
value_info: "population percentile of the cell's density within its model (100 = densest cell; val >= 100 - p is the p % core); the density itself (animals per km2, area-weighted mean) and its CV ride beside val as dens / cv in the Parquet"
regions: "California Current Ecosystem: US West Coast to ~300 nmi offshore, 30.05-48.5 N"
taxa_groups: "cetaceans"
is_mask: false
citation: "Becker EA, Forney KA, Miller DL, Fiedler PC, Barlow J, Moore JE (2020) Habitat-based density estimates for cetaceans in the California Current Ecosystem based on 1991-2018 survey data. NOAA Technical Memorandum NMFS-SWFSC-638. https://doi.org/10.25923/3znq-yx13"
link_info: "https://www.fisheries.noaa.gov/inport/item/64349"
link_download: "https://noaa.maps.arcgis.com/home/item.html?id=566b4ad31f1d40eeb65b8cf3a4f087ca"
editor_options:
chunk_output_type: console
---
NOAA Southwest Fisheries Science Center (SWFSC) habitat-based **density** models for 13 cetacean
species in the California Current Ecosystem (CCE), summer–fall [@becker2020cce], put on the
MarineSensitivity **global 0.05° cell grid** (`global05`) as dataset `cce`, and mapped onto the
merge's **[1,100]** suitability scale by the **population-percentile** transform
(`msens::density_to_suit(method = "ud")`, v11 Q6 / D12), the same recipe as the `gm` and `nc` density
ingests.
`cce` is **registered but not merged** by default: like `gm` and `nc`, its annual models enter the
merge only under `FOLD_DENSITY=1` (the interval models are published for the species app and never
`max()`ed into a year), and `merge_models_prep` excludes the dataset otherwise.
## Source
The models come from line-transect ship surveys by SWFSC in the CCE, nine summer–fall surveys
between 1991 and 2018. Habitat-based density models were fitted to those surveys using dynamic ocean
covariates from a model calibrated to the CCE. The authors averaged the predictions over the survey
seasons and assigned them to a 0.1° grid, clipped at the coast and the study-area edge. The delivery is
an Esri file geodatabase with one layer per species, holding 12,257 cells each. Every cell carries
`DENSITY` (animals km⁻²), `UNCERTAINTY`, `AREA_SQKM` and `ABUNDANCE` (`DENSITY × AREA_SQKM`).
The geodatabase lives at `~/_big/msens/raw/sdm/swfsc/` (`cce_gdb` in `libs/paths.R`; `CCE_GDB`
overrides it).
Two points from the InPort metadata ([item 64349](https://www.fisheries.noaa.gov/inport/item/64349)
and its attribute entity [64350](https://www.fisheries.noaa.gov/inport/item/64350)) matter here:
- **`UNCERTAINTY` is a coefficient of variation.** The attribute definition (quoted in
[Credits and use constraints](#credits-and-use-constraints)) calls it "Numerical uncertainty value
(CV)", with `-99999` where no estimate exists. No layer here uses that sentinel. The values run from
0.12 to 6.4, and a CV above 1 is legitimate for a sparse species.
- **`AREA_SQKM` is the true ellipsoidal area of each clipped cell.** The `source` chunk shows that it
matches `sf::st_area()` on WGS84 to within 0.1 %. Because the 0.05° cell areas below are also
ellipsoidal, the abundance check can be held to 1 %.
The record's abstract also lists sperm whale and a small-beaked-whale guild (*Mesoplodon* spp. and
Cuvier's beaked whale). Those surfaces are **not** in this geodatabase, which holds 13 layers.
## What this notebook does
1. Reads the 13 layers and asserts what it relies on: 12,257 cells per layer, identical geometry
across layers, one species and one season per layer, and `ABUNDANCE = DENSITY × AREA_SQKM`.
2. Resolves each `SPECIES_2` to a WoRMS AphiaID from the pipeline's own WoRMS snapshot
(`spp.duckdb` `worms`), with a live `worrms` cross-check when the network is available.
3. **Area-weighted regrid.** For each 0.05° cell *c*:
$\hat d_c = \sum_p d_p\,A(p \cap c) \,/\, \sum_p A(p \cap c)$. Here $A$ is the exact overlap area in
km², computed as the `exactextractr` coverage fraction times the cell's WGS84 ellipsoidal area,
which depends on latitude. The same weighting gives the CV and the covered fraction of each cell.
The rule lives in msens (`poly_cell_overlap()`, `cells_area_mean()`), and `test-ingest.R` checks it
against a synthetic fixture. The polygon-cell weights are computed **once**, because all 13 layers
share the same geometry.
4. Checks every species: the regridded abundance $\sum_c \hat d_c \cdot \text{covered km}^2$ is
within 1 % of $\sum$ `ABUNDANCE`, no output cell lies outside the source extent, and every density
is ≥ 0.
5. **Value transform (v11 Q6, D12).** Per model, after regridding,
`val = density_to_suit(d, "ud", area = cell area_km2)`: 100 minus the percent of the modelled
population in cells strictly denser than this one, so the densest cell is 100 and the cells with
`val >= 100 - p` are exactly the p % core. Cells with `val < 1` are dropped (the merge's floor).
The transform is scale-free. The old recipe (`CCE_TRANSFORM=cap`: linear to the species' p99.5 of
positive density) stays available and is compared per model in `data/cce_transform_compare.csv`.
6. **Interval and annual models.** CCE models one interval, `summer-fall`, so the **annual model is
that interval**: `msens::density_annual(n_intervals = 1)` is the identity on the density (asserted),
and the annual surface is the same `ud` transform of the same cells. Both keys are written
(`cce|WORMS:{id}|summer-fall` for the species app, `cce|WORMS:{id}` for the merge) so `cce` has the
same two-part / three-part key shape as `gm` and `nc`. They describe summer–fall conditions only;
the annual name does not mean the model says anything about winter–spring.
7. Writes the long Parquet (`mdl_key`, `cell_id`, `val`, plus `dens`, `cv`, `frac`) to the version's
`dist/dataset=cce/`, `model_cce.csv` beside it, and the manifest. `CCE_COG=1` also writes one
2-band Float32 COG per species (density, CV).
::: {.callout-note}
## The CV band is an approximation
An area-weighted mean is right for a density, which is intensive: the regridded abundance equals the
source abundance. It is **not** right for a CV, because the CVs of the parts of a cell do not average
linearly. The CV of a mixed cell's density would need the covariance between its parts, which the
delivery does not carry. The `cv` column is therefore an area-weighted average of the source CVs. It is
exact only where an output cell lies inside a single source cell, and the [regrid section](#regrid)
counts how many cells do.
:::
```{mermaid}
%%| label: fig-design
%%| fig-cap: "SWFSC CCE geodatabase → exact overlap weights (once) → per-species area-weighted density + CV → checks → population-percentile val (ud) → interval + annual Parquet, model table and manifest"
flowchart LR
gdb["swfsc_cce_becker_et_al_2020b.gdb<br/>13 layers x 12,257 cells (0.1 deg)<br/>DENSITY, UNCERTAINTY (CV)"] --> w["msens::poly_cell_overlap()<br/>overlap km2 per (polygon, cell)<br/>computed once: shared geometry"]
grid["r_cellid_global.tif<br/>global05, 0.05 deg"] --> w
w --> m["msens::cells_area_mean()<br/>per species: density, cv, frac"]
worms[("spp.duckdb worms<br/>+ worrms cross-check")] --> key["mdl_key = cce, WORMS id<br/>(summer-fall | annual)"]
m --> chk{"checks<br/>abundance within 1 pct<br/>extent, counts, density >= 0"}
chk --> ud["msens::density_to_suit(ud)<br/>area = cell table area_km2<br/>msens::density_annual(n = 1)"]
key --> ud
ud --> pq[("dist/dataset=cce/*.parquet<br/>mdl_key, cell_id, val, dens, cv, frac")]
ud --> csv["model_cce.csv<br/>+ data/swfsc_cce_models.csv"]
chk -.-> cog["CCE_COG=1: 2-band Float32 COG"]
```
## Setup
```{r}
#| label: setup
librarian::shelf(
arrow, DBI, digest, dplyr, duckdb, fs, ggplot2, glue, here, jsonlite, knitr, logger, patchwork, purrr,
readr, rnaturalearth, sf, stringr, terra, tibble, tidyr, worrms, xml2, quiet = T)
library(msens) # the installed msens (poly_cell_overlap / density_to_suit / density_annual); never via shelf(), which can reinstall a stale main
source(here("libs/paths.R"))
source(here("libs/vars.R"))
source(here("libs/duckdb_budget.R"))
options(readr.show_col_types = FALSE)
stopifnot(
"msens >= 0.64.1 needed (poly_cell_overlap, cells_area_mean, density_to_suit, density_annual)" =
packageVersion("msens") >= "0.64.1")
ds_key <- "cce"
season <- "summer-fall" # the SEASON field ("Summer-Fall"), slugged for keys and file names
# source geodatabase (read-only): libs/paths.R `cce_gdb` (~/_big/msens/raw/sdm/swfsc/...); CCE_GDB overrides it
gdb <- path_expand(cce_gdb)
stopifnot("source gdb not found; set CCE_GDB or hydrate raw/sdm/swfsc (scripts/pull_from_laptop.sh)" = dir_exists(gdb))
# outputs: the version's dist/ (the merge and build_registry read it), model_cce.csv beside it
dir_atlas <- glue("{dir_big_v}/marine-atlas")
dir_dist <- path_expand(glue("{dir_atlas}/dist/dataset={ds_key}")); dir_create(dir_dist)
mdl_csv <- path_expand(glue("{dir_atlas}/dist/model_{ds_key}.csv"))
dir_cog <- path_expand(glue("{dir_atlas}/cce_cog")) # CCE_COG=1 only
dir_inport <- path_expand(glue("{dir_raw}/sdm/swfsc/inport_cce")); dir_create(dir_inport)
repo_csv <- here("data/swfsc_cce_models.csv")
cmp_csv <- here("data/cce_transform_compare.csv")
manifest <- here("data/manifests/ingest_swfsc_cce.json"); dir_create(path_dir(manifest))
p_cap <- 0.995 # the density percentile the OLD recipe used as the 100 point
cellid_tif <- path_expand(cellid_tif) # GDAL does not expand "~"
stopifnot(file_exists(cellid_tif))
grid <- msens::grid_spec_for("global05", cellid_tif)
# flags (this notebook's own; default = the v11 recipe) ----
cce_transform <- tolower(Sys.getenv("CCE_TRANSFORM", "ud")) # ud (v11) | cap (the linear-to-p99.5 recipe, for comparison)
stopifnot("CCE_TRANSFORM must be 'ud' or 'cap'" = cce_transform %in% c("ud", "cap"))
cce_cog <- .flag("CCE_COG") # also write one 2-band (density, CV) Float32 COG per species
cce_fetch <- .flag("CCE_FETCH") # re-download the InPort metadata records
log_info("cce: transform = {cce_transform}; COG = {cce_cog}; redo = {redo_ingest}")
log_info("source: {gdb}")
log_info("outputs: {dir_dist}")
# REDO_INGEST starts the dataset clean; otherwise the 26 files are simply overwritten in place (the
# parquet chunk asserts that exactly 26 are there afterwards)
if (redo_ingest) { dir_delete(dir_dist); dir_create(dir_dist) }
if (cce_cog) { if (dir_exists(dir_cog)) dir_delete(dir_cog); dir_create(dir_cog) }
```
## Cell areas
The `ud` transform weights each cell by its area (the population in a cell is density × area). The
`cell` table is a property of the grid, not of the release: this version's `sdm.duckdb` is used when
`build_cell_grid` has written it, else `ver_prev`'s (which `build_cell_grid` copies unchanged). It is
opened read-only.
```{r}
#| label: cell-area
sdm_cell_db <- c(sdm_db, glue("{dir_big}/{ver_prev}/sdm.duckdb")) |> path_expand() |> keep(file_exists) |> first()
stopifnot("no sdm.duckdb with a cell table (this version's or ver_prev's)" = !is.null(sdm_cell_db))
log_info("cell areas from {sdm_cell_db}")
dir_create(glue("{dir_big_v}/_tmp/cce"))
con_sdm <- dbConnect(duckdb(sdm_cell_db, read_only = TRUE))
duckdb_tune(con_sdm, glue("{dir_big_v}/_tmp/cce"))
ca <- dbGetQuery(con_sdm, "SELECT cell_id, area_km2 FROM cell")
dbDisconnect(con_sdm, shutdown = TRUE)
area_by_id <- rep(NA_real_, 7200 * 3600); area_by_id[ca$cell_id] <- ca$area_km2
# the cell table holds the OCEAN cells only, while the regridded surfaces fade into land cells along the
# coast (whole range, no land mask): those take the area of their latitude row (a cell's area depends on
# its row alone; the table's rows are constant to 1e-9 relative)
row_of <- \(id) (id - 1L) %/% 7200L + 1L
a_row <- ca |> mutate(row = row_of(cell_id)) |> group_by(row) |>
summarise(a = first(area_km2), spread = max(area_km2) / min(area_km2) - 1, .groups = "drop")
stopifnot("cell area is not constant within a row" = max(a_row$spread) < 1e-6)
area_by_row <- rep(NA_real_, 3600); area_by_row[a_row$row] <- a_row$a
cell_area <- function(id) {
a <- area_by_id[id]; miss <- is.na(a)
if (any(miss)) a[miss] <- area_by_row[row_of(id[miss])]
stopifnot("a cell id has no area (no ocean cell in its row)" = !anyNA(a), all(a > 0))
a }
log_info("cell table: {nrow(ca)} ocean cells, area {round(min(ca$area_km2), 3)}-{round(max(ca$area_km2), 1)} km2; ",
"land cells take their row's area")
rm(ca)
```
## Read the source {#read-source}
```{r}
#| label: source
lyrs <- st_layers(gdb)
src <- set_names(lyrs$name) |> map(\(l) read_sf(gdb, l))
# the delivery we rely on: 13 layers x 12,257 cells, one species + season + study per layer
need <- c("SPECIES", "SPECIES_2", "SEASON", "STUDY", "STRATUM", "MODEL_TYPE",
"DENSITY", "UNCERTAINTY", "AREA_SQKM", "ABUNDANCE")
stopifnot(
"expected 13 layers" = length(src) == 13,
"expected 12,257 cells per layer" = all(map_int(src, nrow) == 12257L),
"missing fields" = all(map_lgl(src, \(x) all(need %in% names(x)))),
"one species per layer" = all(map_int(src, \(x) n_distinct(x$SPECIES_2)) == 1L),
"season is Summer-Fall" = all(map_lgl(src, \(x) all(x$SEASON == "Summer-Fall"))),
"stratum is CCE" = all(map_lgl(src, \(x) all(x$STRATUM == "CCE"))),
"no NA density" = all(map_lgl(src, \(x) !anyNA(x$DENSITY))),
"ABUNDANCE = DENSITY x AREA_SQKM" = all(map_lgl(src, \(x)
max(abs(x$ABUNDANCE - x$DENSITY * x$AREA_SQKM) / pmax(x$ABUNDANCE, 1e-12)) < 1e-4)))
# every layer carries the SAME cells in the SAME order, so the polygon->cell weights are computed once
geom_hash <- map_chr(src, \(x) digest::digest(st_as_binary(st_geometry(x))))
stopifnot("layers do not share one geometry" = n_distinct(geom_hash) == 1L)
# AREA_SQKM is the true (ellipsoidal) area: what lets the abundance check below be held to 1 %
geom <- st_sf(geometry = st_geometry(src[[1]]))
op <- sf_use_s2(FALSE) # planar GEOS predicate engine -> st_area on the ellipsoid
a_ell <- as.numeric(st_area(geom)) / 1e6
sf_use_s2(op)
r_area <- a_ell / src[[1]]$AREA_SQKM
bb_src <- st_bbox(geom)
# the source lattice: full (unclipped) cells are 0.1 x 0.1 deg; how far are their edges from the
# 0.05-degree grid lines? (an offset makes output cells straddle two source cells)
full <- abs(src[[1]]$Shape_Area - 0.01) < 1e-9
bb_full <- do.call(rbind, lapply(st_geometry(geom)[full], st_bbox))
off_05 <- \(v) v - round(v / 0.05) * 0.05
lat_off <- unique(round(off_05(bb_full[, "ymin"]), 6))
lon_off <- unique(round(off_05(bb_full[, "xmin"]), 6))
stopifnot("source cells are not one regular 0.1-degree lattice" =
length(lon_off) == 1, length(lat_off) == 1,
all(abs(bb_full[, "xmax"] - bb_full[, "xmin"] - 0.1) < 1e-6),
all(abs(bb_full[, "ymax"] - bb_full[, "ymin"] - 0.1) < 1e-6))
log_info("source lattice: {sum(full)} full 0.1-degree cells, edges offset {lon_off} deg (lon), {lat_off} deg (lat) from the 0.05 grid lines")
log_info("AREA_SQKM vs ellipsoidal area: ratio {round(min(r_area[src[[1]]$AREA_SQKM > 1]), 4)}-{round(max(r_area[src[[1]]$AREA_SQKM > 1]), 4)} (cells > 1 km2); totals {round(sum(src[[1]]$AREA_SQKM))} vs {round(sum(a_ell))} km2")
stopifnot(abs(sum(a_ell) / sum(src[[1]]$AREA_SQKM) - 1) < 1e-3)
d_src <- imap_dfr(src, \(x, l) tibble(
layer = l,
species = x$SPECIES[1],
scientific_name = x$SPECIES_2[1],
n_src = nrow(x),
model_type = x$MODEL_TYPE[1],
density_min = min(x$DENSITY),
density_max = max(x$DENSITY),
cv_min = min(x$UNCERTAINTY),
cv_max = max(x$UNCERTAINTY),
n_cv_sentinel = sum(x$UNCERTAINTY <= -99999),
abundance_src = sum(x$ABUNDANCE)))
kable(d_src |> mutate(across(c(density_min, density_max), \(v) formatC(v, format = "g", digits = 3))),
digits = 4, format.args = list(big.mark = ","),
caption = glue("Source layers ({unique(src[[1]]$STUDY)}): extent ",
"{paste(round(bb_src, 4), collapse = ', ')} (xmin, ymin, xmax, ymax)"))
```
## Taxa → WoRMS {#taxa}
Each delivered name (`SPECIES_2`) is matched exactly against the pipeline's WoRMS snapshot. Where a
name has several WoRMS records (homonyms and synonym records), the rule picks one in this order:
1. the record that is itself **accepted**;
2. otherwise, a synonym whose accepted taxon is accepted **and in the same genus**;
3. otherwise, a synonym whose accepted taxon is accepted.
The render stops if more than one record ties at the top, or if any accepted target is not itself
accepted.
```{r}
#| label: taxa
con_spp <- dbConnect(duckdb(), path_expand(spp_db), read_only = TRUE)
worms_ver <- dbGetQuery(con_spp, "SELECT worms_ver, source FROM worms_meta")
nm <- unique(d_src$scientific_name)
w_rec <- tbl(con_spp, "worms") |>
filter(scientificName %in% !!nm) |>
select(taxonID, scientificName, scientificNameAuthorship, taxonRank, taxonomicStatus,
acceptedNameUsageID) |>
collect()
w_acc <- tbl(con_spp, "worms") |>
filter(taxonID %in% !!unique(na.omit(w_rec$acceptedNameUsageID))) |>
select(acc_id = taxonID, acc_name = scientificName, acc_status = taxonomicStatus, acc_genus = genus) |>
collect()
dbDisconnect(con_spp, shutdown = TRUE)
w_pick <- w_rec |>
left_join(w_acc, by = c(acceptedNameUsageID = "acc_id")) |>
mutate(
src_genus = word(scientificName, 1),
tier = case_when(
taxonomicStatus == "accepted" ~ 1L,
acc_status %in% "accepted" & acc_genus %in% src_genus ~ 2L,
acc_status %in% "accepted" ~ 3L,
.default = NA_integer_)) |>
filter(!is.na(tier)) |>
group_by(scientificName) |>
filter(tier == min(tier)) |>
ungroup()
stopifnot(
"a name did not resolve to WoRMS" = setequal(w_pick$scientificName, nm),
"a name resolved to more than one record" = !any(duplicated(w_pick$scientificName)),
"an accepted target is not accepted" = all(w_pick$acc_status == "accepted"),
"the record's own id must be unique (mdl_key)" = !any(duplicated(w_pick$taxonID)))
taxa <- d_src |>
select(layer, species, scientific_name) |>
left_join(
w_pick |> transmute(
scientific_name = scientificName,
worms_id = as.integer(taxonID),
worms_status = taxonomicStatus,
worms_id_accepted = as.integer(acceptedNameUsageID),
scientific_name_accepted = acc_name),
by = "scientific_name") |>
mutate(
sp_id = glue("WORMS:{worms_id}") |> as.character(),
mdl_key = glue("{ds_key}|{sp_id}|{season}") |> as.character(), # the interval model
mdl_key_an = glue("{ds_key}|{sp_id}") |> as.character(), # the annual model (= the interval: one interval)
cog = glue("{ds_key}_{worms_id}_{season}.tif") |> as.character(),
parquet = glue("{ds_key}_{worms_id}_{season}.parquet") |> as.character(),
parquet_an = glue("{ds_key}_{worms_id}.parquet") |> as.character())
stopifnot(nrow(taxa) == 13, !anyNA(taxa$worms_id), !any(duplicated(taxa$mdl_key)), !any(duplicated(taxa$mdl_key_an)))
# live cross-check against the WoRMS web service: informative only (an offline render still passes)
live <- tryCatch(
worrms::wm_record(taxa$worms_id) |>
transmute(worms_id = as.integer(AphiaID), live_status = status,
live_valid_id = as.integer(valid_AphiaID)),
error = \(e) { log_warn("worrms live cross-check skipped: {conditionMessage(e)}"); NULL })
if (!is.null(live)) {
taxa <- taxa |> left_join(live, by = "worms_id")
n_disagree <- sum(taxa$live_status != taxa$worms_status |
taxa$live_valid_id != taxa$worms_id_accepted, na.rm = TRUE)
if (n_disagree > 0)
log_warn("{n_disagree} taxa differ between the {worms_ver$worms_ver} snapshot and live WoRMS")
}
kable(taxa |> select(any_of(c("species", "scientific_name", "worms_id", "worms_status",
"worms_id_accepted", "scientific_name_accepted", "live_status",
"mdl_key"))),
caption = glue("WoRMS: snapshot {worms_ver$worms_ver} ({worms_ver$source}); ",
"live = worrms::wm_record() at render time"))
n_syn <- sum(taxa$worms_status != "accepted")
acc_dup <- taxa |> filter(worms_id_accepted %in% worms_id_accepted[duplicated(worms_id_accepted)])
```
`r 13 - n_syn` of the 13 delivered names are accepted in WoRMS, and `r n_syn` are synonyms:
- ***Delphinus capensis***, the long-beaked common dolphin, is unaccepted in WoRMS and placed under
*Delphinus delphis*. The InPort abstract itself calls this population *Delphinus delphis bairdii*,
but WoRMS lists that subspecies as *nomen dubium*, so no accepted WoRMS taxon is specific to the CCE
long-beaked form.
- ***Lagenorhynchus obliquidens***, the Pacific white-sided dolphin, is a junior objective synonym of
*Sagmatias obliquidens*.
**Key choice.** `mdl_key` and the file names use the AphiaID of the name **as delivered**
(`worms_id`), not the accepted one. A raw model key identifies the source's own species, in the same
way that AquaMaps keys use its species code. Using the accepted id would collide for the two
*Delphinus* layers: `r paste(acc_dup$species, collapse = " and ")` share accepted AphiaID
`r unique(acc_dup$worms_id_accepted)`. The accepted id is kept in `worms_id_accepted`. A per-taxon
merge would fold both layers into one *D. delphis* surface, and that is a decision for whoever
promotes this dataset.
## Regrid onto the 0.05° grid {#regrid}
```{r}
#| label: regrid
# exact overlap (km2) of every source cell with every 0.05-degree grid cell, once for all 13 layers
t0 <- Sys.time()
w <- msens::poly_cell_overlap(geom, cellid_tif)
log_info("poly_cell_overlap: {nrow(w)} (polygon, cell) rows over {n_distinct(w$cell_id)} cells in {round(difftime(Sys.time(), t0, units = 'secs'), 1)} s")
# how much each output cell mixes source cells: share of its covered area from its largest polygon
mixing <- w |>
group_by(cell_id) |>
summarise(n_poly = n(), dom = max(area_km2) / sum(area_km2), .groups = "drop")
# per species: area-weighted density and CV (a -99999 CV would be NA, i.e. drop out of the CV only)
cells <- imap(src, \(x, l) msens::cells_area_mean(
w, tibble(density = x$DENSITY,
cv = if_else(x$UNCERTAINTY <= -99999, NA_real_, x$UNCERTAINTY))))
stopifnot("layers must regrid to the same cell set" =
all(map_lgl(cells, \(d) identical(d$cell_id, cells[[1]]$cell_id))))
n_out <- nrow(cells[[1]])
n_single <- sum(mixing$dom >= 0.999)
pct <- \(n) round(100 * n / n_out, 1)
med_dom <- round(100 * median(mixing$dom[mixing$dom < 0.999]), 1)
kable(tibble(
output_cells = n_out,
partial_cells = sum(cells[[1]]$frac < 1 - 1e-6),
frac_min = signif(min(cells[[1]]$frac), 3),
in_one_source_cell = n_single,
mixed_cells = n_out - n_single,
dom_median_mixed = round(median(mixing$dom[mixing$dom < 0.999]), 4),
dom_min = round(min(mixing$dom), 4)),
format.args = list(big.mark = ","),
caption = paste(
"Output cells. partial = covered fraction < 1 (coast, Canada/Mexico and offshore edges);",
"in_one_source_cell = the largest source cell supplies >= 99.9 % of the covered area;",
"dom = that share in the mixed cells (median, minimum)"))
```
The source's 0.1° lattice is regular, but its edges sit `r lon_off`° (longitude) and `r lat_off`°
(latitude) off the 0.05° grid lines. That is `r round(100 * abs(lon_off) / 0.05, 1)` % and
`r round(100 * abs(lat_off) / 0.05, 1)` % of a grid cell. So every other output column and every other
output row straddles a source edge. `r pct(n_single)` % of output cells lie inside a single source cell
and carry its values unchanged. In the other `r pct(n_out - n_single)` %, the largest source cell
supplies a median `r med_dom` % of the covered area, and the result is an area-weighted blend across
a sliver of the neighbour. That is harmless for the density, since the abundance is conserved (next
section). It is the approximation for the CV described in the callout above.
## Checks {#checks}
```{r}
#| label: checks
ctr <- msens::cell_lonlat(cells[[1]]$cell_id, grid) # cell centres
h <- grid$resx / 2 # a cell intersects the bbox iff its centre is within h
chk <- imap_dfr(cells, \(d, l) {
x <- src[[l]]
tibble(
layer = l,
n_src = nrow(x),
n_cells = nrow(d),
abundance_source = sum(x$ABUNDANCE),
abundance_regridded = sum(d$density * d$area_km2),
covered_km2 = sum(d$area_km2),
area_sqkm_src = sum(x$AREA_SQKM),
density_min = min(d$density),
n_outside_extent = sum(ctr$lon < bb_src[["xmin"]] - h | ctr$lon > bb_src[["xmax"]] + h |
ctr$lat < bb_src[["ymin"]] - h | ctr$lat > bb_src[["ymax"]] + h),
frac_max = max(d$frac),
n_na_density = sum(is.na(d$density)))
}) |>
mutate(abundance_rel_diff = abundance_regridded / abundance_source - 1,
ok = n_src == 12257 & abs(abundance_rel_diff) < 0.01 & n_outside_extent == 0 &
density_min >= 0 & frac_max <= 1 + 1e-6 & n_na_density == 0)
kable(chk |>
select(layer, n_src, n_cells, abundance_source, abundance_regridded, abundance_rel_diff,
covered_km2, area_sqkm_src, density_min, n_outside_extent, frac_max, ok) |>
mutate(density_min = formatC(density_min, format = "g", digits = 3),
abundance_rel_diff = formatC(abundance_rel_diff, format = "e", digits = 1)),
digits = c(0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 6, 0), format.args = list(big.mark = ","),
caption = "Per species: regridded abundance (sum of density x covered km2) vs the source's sum of ABUNDANCE (must be within 1 %), cells outside the source extent (must be 0), density >= 0")
stopifnot(
"13 species x 12,257 source cells" = nrow(chk) == 13 && all(chk$n_src == 12257L),
"regridded abundance not within 1 % of source" = all(abs(chk$abundance_rel_diff) < 0.01),
"an output cell lies outside the source extent" = all(chk$n_outside_extent == 0),
"negative density" = all(chk$density_min >= 0),
"covered fraction > 1 (overlapping sources?)" = all(chk$frac_max <= 1 + 1e-6),
"NA density in an output cell" = all(chk$n_na_density == 0),
"every output cell is covered" = all(cells[[1]]$frac > 0))
```
## Value transform: density → population percentile {#transform}
Per species, on the regridded cells, **after** the checks above. `val` is
`msens::density_to_suit(density, "ud", area = cell area_km2)`: 100 minus the percent of the modelled
population (density × the cell table's `area_km2`) held by cells *strictly denser* than this one. The
densest cell is 100 and `val >= 100 - p` is exactly the p % core of the population, whatever the
units or the skew of the model. Cells below `val` 1 are the tail holding the last percent of the
population and are not written (the merge's floor).
The annual surface is `msens::density_annual(n_intervals = 1)` of the one interval: the identity on the
density, asserted below, so **the annual model has exactly the interval model's cells and `val`**. The
two keys exist so `cce` has the same key shape as `gm` and `nc`; the merge reads only the annual one.
```{r}
#| label: transform
to_val <- function(dens, cell_id, how, cap = NA_real_) {
if (how == "ud") msens::density_to_suit(dens, "ud", area = cell_area(cell_id), digits = 2)
else round(100 * pmin(dens / cap, 1), 2)
}
# per species: the interval surface and the annual surface (density_annual, n_intervals = 1), each
# transformed on its own cells under BOTH recipes; `val` is the selected one
surf <- imap(cells, \(d, l) {
pos <- d$density[d$density > 0]
cap <- as.numeric(quantile(pos, p_cap, names = FALSE))
ann <- msens::density_annual(tibble(cell_id = d$cell_id, dens = d$density), n_intervals = 1)
stopifnot("annual (one interval) is not the interval's density" =
identical(ann$cell_id, d$cell_id), isTRUE(all(ann$dens == d$density)))
mk <- \(dens) tibble(cell_id = d$cell_id, dens = dens, cv = d$cv, frac = d$frac,
v_ud = to_val(dens, d$cell_id, "ud"), v_cap = to_val(dens, d$cell_id, "cap", cap)) |>
mutate(val = if (cce_transform == "ud") v_ud else v_cap)
list(cap = cap, interval = mk(d$density), annual = mk(ann$dens))
})
stopifnot("annual val != interval val" =
all(map_lgl(surf, \(s) identical(s$interval$val, s$annual$val))))
# what the transform did, per species (interval model; the annual is identical)
pop_share <- \(s, v, thr) sum((s$dens * cell_area(s$cell_id))[v >= thr]) / sum(s$dens * cell_area(s$cell_id))
tf <- imap_dfr(surf, \(s, l) {
x <- s$interval
tibble(layer = l, n_cells = nrow(x), n_kept = sum(x$val >= 1), val_max = max(x$val),
n_at_100 = sum(x$val == 100),
pop_in_val_ge_50 = pop_share(x, x$v_ud, 50), pop_dropped_val_lt_1 = 1 - pop_share(x, x$v_ud, 1),
q10 = quantile(x$val[x$val >= 1], .1, names = FALSE), q50 = median(x$val[x$val >= 1]),
q90 = quantile(x$val[x$val >= 1], .9, names = FALSE))
})
kable(tf |> mutate(across(starts_with("pop_"), \(v) round(100 * v, 2))),
digits = 1, format.args = list(big.mark = ","),
caption = glue("{cce_transform} transform per species (interval = annual): cells kept (val >= 1), cells at 100, ",
"% of the population in cells with ud val >= 50 (>= 50 by construction) and % dropped below val 1 (< 1), ",
"quantiles of the kept val"))
stopifnot(
"no model reaches 100" = all(tf$val_max == 100),
"a model has no kept cells" = all(tf$n_kept > 0),
"ud: the 50 % core holds < 50 % of the population" = all(tf$pop_in_val_ge_50 >= 0.5 - 1e-9),
"ud: dropped tail holds > 1 % of the population" = all(tf$pop_dropped_val_lt_1 <= 0.01 + 1e-9))
# the two recipes side by side, per model (cells written = val >= 1)
q3 <- \(v) quantile(v, c(.1, .5, .9), names = FALSE)
cmp <- imap_dfr(surf, \(s, l) {
x <- s$interval
tibble(sp_id = taxa$sp_id[taxa$layer == l], layer = l, interval = c(season, "annual"),
n_cells = nrow(x), n_ud = sum(x$v_ud >= 1), n_cap = sum(x$v_cap >= 1),
ud_q10 = q3(x$v_ud[x$v_ud >= 1])[1], ud_q50 = q3(x$v_ud[x$v_ud >= 1])[2], ud_q90 = q3(x$v_ud[x$v_ud >= 1])[3],
cap_q10 = q3(x$v_cap[x$v_cap >= 1])[1], cap_q50 = q3(x$v_cap[x$v_cap >= 1])[2], cap_q90 = q3(x$v_cap[x$v_cap >= 1])[3],
dens_cap = s$cap)
}) |> arrange(sp_id, interval)
write_csv(cmp |> mutate(across(where(is.double), \(v) signif(v, 5))), cmp_csv)
kable(cmp |> filter(interval == season) |> select(-layer, -interval, -sp_id) |>
mutate(across(where(is.double), \(v) signif(v, 3))),
caption = glue("ud vs the linear-to-p99.5 recipe (cap) per species: cells kept and val quantiles. ",
"written to data/cce_transform_compare.csv; the written surface is {cce_transform}"))
```
## Write COGs (`CCE_COG=1`) {#cog}
Off by default. With `CCE_COG=1` each species gets one COG, `cce_cog/cce_<worms_id>_summer-fall.tif`
beside `dist/`: the **delivered** density, not `val`. Band 1 is `density`
(animals km⁻²) and band 2 is `cv`. Both are Float32 with nodata −9999 outside the modelled cells,
DEFLATE-compressed with the floating-point predictor, and in 256 × 256 blocks. There are no internal
overviews, because titiler decimates from full resolution. The window is cut from the cell-id raster
by row and column, so its origin and pixel size are the grid's own, including the ~5 × 10⁻⁶° float
drift. Each output pixel *is* a `global05` cell.
```{r}
#| label: cog
if (!cce_cog) {
log_info("CCE_COG not set: no COGs written")
} else {
# the data window in grid rows/cols (arithmetic, row-major, 1-based, top-left origin)
ids <- as.double(cells[[1]]$cell_id)
row <- ((ids - 1) %/% grid$nc) + 1; col <- ((ids - 1) %% grid$nc) + 1
r0 <- min(row); r1 <- max(row); c0 <- min(col); c1 <- max(col)
# cut that window from the cell-id raster itself: exact grid geometry, and a lookup check for free
r_id <- rast(cellid_tif)[r0:r1, c0:c1, drop = FALSE]
win_id <- values(r_id, mat = FALSE)
win_rc <- expand_grid(rr = r0:r1, cc = c0:c1) # row-major, same order as values()
stopifnot("cell-id window != row-major arithmetic" =
all(win_id == (win_rc$rr - 1) * grid$nc + win_rc$cc))
win_idx <- match(win_id, cells[[1]]$cell_id) # pixel -> row of cells[[l]] (NA = no data)
log_info("COG window: rows {r0}-{r1}, cols {c0}-{c1} ({nrow(r_id)} x {ncol(r_id)} px), {sum(!is.na(win_idx))} data px")
write_cog <- function(d, tx) {
r <- rast(r_id, nlyrs = 2)
values(r) <- cbind(d$density[win_idx], d$cv[win_idx])
names(r) <- c("density", "cv")
tmp <- file_temp(ext = "tif"); on.exit(file_delete(tmp))
writeRaster(r, tmp, datatype = "FLT4S", NAflag = -9999, overwrite = TRUE)
# COG via gdal_translate: -stats writes real band statistics (terra's own COG path writes
# STATISTICS_MEAN = -9999), -mo carries what a viewer needs to read the file on its own
sf::gdal_utils("translate", tmp, path(dir_cog, tx$cog), options = c(
"-of", "COG", "-co", "COMPRESS=DEFLATE", "-co", "PREDICTOR=YES", "-co", "BLOCKSIZE=256",
"-co", "OVERVIEWS=NONE", "-stats",
"-mo", glue("MDL_KEY={tx$mdl_key}"),
"-mo", glue("SPECIES={tx$species}"),
"-mo", glue("SCIENTIFIC_NAME={tx$scientific_name}"),
"-mo", glue("WORMS_ID={tx$worms_id}"),
"-mo", glue("WORMS_ID_ACCEPTED={tx$worms_id_accepted}"),
"-mo", "SEASON=summer-fall",
"-mo", "GRID=global05 (0.05 deg, -180..180; pixel = cell_id of r_cellid_global.tif)",
"-mo", "BAND_1=density: animals per km2, area-weighted mean of the source 0.1 deg cells",
"-mo", "BAND_2=cv: coefficient of variation of density, same weighting (an approximation for a CV)",
"-mo", "SOURCE=Becker et al. 2020, NOAA-TM-NMFS-SWFSC-638, https://doi.org/10.25923/3znq-yx13",
"-mo", "INPORT=https://www.fisheries.noaa.gov/inport/item/64349"))
invisible(path(dir_cog, tx$cog))
}
walk(seq_len(nrow(taxa)), \(i) write_cog(cells[[taxa$layer[i]]], taxa[i, ]))
# validate: gdalinfo says COG, 2 Float32 bands named density/cv with nodata, on the grid; then reopen
# and compare EVERY pixel with what was written (float32 rounding only), plus a few cells by location
set.seed(42)
cog_chk <- map_dfr(seq_len(nrow(taxa)), \(i) {
tx <- taxa[i, ]; f <- path(dir_cog, tx$cog); d <- cells[[tx$layer]]
info <- fromJSON(sf::gdal_utils("info", f, options = "-json", quiet = TRUE), simplifyVector = FALSE)
b <- info$bands
gt <- unlist(info$geoTransform)
off_x <- (gt[1] - grid$xmin) / grid$resx; off_y <- (grid$ymax - gt[4]) / grid$resy
r <- rast(f)
v <- values(r, mat = TRUE)
exp_d <- d$density[win_idx]; exp_cv <- d$cv[win_idx]
rel <- function(a, e) max(abs(a - e) / pmax(abs(e), 1e-30), na.rm = TRUE)
# a few cells looked up by LOCATION (cell centre -> pixel), incl. the densest one
k <- c(which.max(d$density), sample(nrow(d), 5))
ll <- msens::cell_lonlat(d$cell_id[k], grid)
ex <- terra::extract(r, as.matrix(ll[, c("lon", "lat")]))
tibble(
cog = tx$cog,
layout = info$metadata$IMAGE_STRUCTURE$LAYOUT %||% NA,
compression = info$metadata$IMAGE_STRUCTURE$COMPRESSION %||% NA,
bands = length(b),
type = paste(unique(map_chr(b, "type")), collapse = ","),
descriptions = paste(map_chr(b, \(x) x$description %||% ""), collapse = ","),
nodata = paste(unique(map_dbl(b, \(x) x$noDataValue %||% NA_real_)), collapse = ","),
overviews = sum(map_int(b, \(x) length(x$overviews))),
on_grid = abs(off_x - round(off_x)) < 1e-6 && abs(off_y - round(off_y)) < 1e-6 &&
abs(gt[2] - grid$resx) < 1e-12 && abs(-gt[6] - grid$resy) < 1e-12,
n_data_px = sum(!is.na(v[, 1])),
max_rel_density = rel(v[, 1], exp_d),
max_rel_cv = rel(v[, 2], exp_cv),
same_na = identical(is.na(v[, 1]), is.na(exp_d)),
pts_rel_max = max(abs(ex$density - d$density[k]) / pmax(d$density[k], 1e-30)))
})
print(kable(cog_chk, digits = 9, format.args = list(big.mark = ","),
caption = "COG validation: gdalinfo layout/bands, alignment to the grid, and every pixel read back vs what was written (float32: relative error < 1e-6)"))
stopifnot(
all(cog_chk$layout == "COG"), all(cog_chk$compression == "DEFLATE"), all(cog_chk$bands == 2),
all(cog_chk$type == "Float32"), all(cog_chk$descriptions == "density,cv"),
all(cog_chk$nodata == "-9999"), all(cog_chk$overviews == 0), all(cog_chk$on_grid),
all(cog_chk$n_data_px == nrow(cells[[1]])), all(cog_chk$same_na),
all(cog_chk$max_rel_density < 1e-6), all(cog_chk$max_rel_cv < 1e-6), all(cog_chk$pts_rel_max < 1e-6))
}
```
## Write Parquet {#parquet}
The long table has one row per (model, cell): `mdl_key`, `cell_id`, `val` (the transformed value in
[1,100]), and beside it `dens` (the area-weighted density, animals km⁻², full double precision), `cv` and
`frac` (the covered fraction of the cell). It lives in `dist/dataset=cce/`, the version's `dist/` layout.
The merge reads `mdl_key`, `cell_id` and `val` only. 26 files: for each species the interval model
(`cce|WORMS:{id}|summer-fall`) and the annual model (`cce|WORMS:{id}`), which has the same rows.
```{r}
#| label: parquet
walk(seq_len(nrow(taxa)), \(i) {
tx <- taxa[i, ]; s <- surf[[tx$layer]]
one <- \(x, key, f) msens::write_atlas_parquet(
x |> filter(val >= 1) |>
transmute(mdl_key = key, cell_id = as.integer(cell_id), val, dens, cv, frac),
path(dir_dist, f))
one(s$interval, tx$mdl_key, tx$parquet)
one(s$annual, tx$mdl_key_an, tx$parquet_an)
})
stopifnot("not exactly 26 parquet files in dist/dataset=cce (stale files? REDO_INGEST=1)" =
length(dir_ls(dir_dist, glob = "*.parquet")) == 26)
# read back through DuckDB: one mdl_key per file, the expected rows and range
con <- dbConnect(duckdb())
duckdb_tune(con, glue("{dir_big_v}/_tmp/cce"))
pq <- dbGetQuery(con, glue(
"SELECT mdl_key, count(*) AS n_cells, count(DISTINCT cell_id) AS n_distinct, min(val) AS val_min,
max(val) AS val_max, min(dens) AS dens_min, max(frac) AS frac_max
FROM read_parquet('{dir_dist}/*.parquet') GROUP BY mdl_key ORDER BY mdl_key"))
dbDisconnect(con, shutdown = TRUE)
pq_hash <- msens::hash_parquet(glue("{dir_dist}/*.parquet"))
n_exp <- bind_rows(
tibble(mdl_key = taxa$mdl_key, n_exp = map_int(taxa$layer, \(l) sum(surf[[l]]$interval$val >= 1))),
tibble(mdl_key = taxa$mdl_key_an, n_exp = map_int(taxa$layer, \(l) sum(surf[[l]]$annual$val >= 1))))
pq_chk <- pq |> left_join(n_exp, by = "mdl_key")
stopifnot(
"26 models" = nrow(pq) == 26,
"model set != 13 interval + 13 annual" = setequal(pq$mdl_key, n_exp$mdl_key),
"row counts != the transform's" = all(pq_chk$n_cells == pq_chk$n_exp),
"duplicate cell in a model" = all(pq$n_distinct == pq$n_cells),
"val outside [1,100]" = all(pq$val_min >= 1 & pq$val_max <= 100),
"a model's max val is not 100" = all(pq$val_max == 100),
"negative density" = all(pq$dens_min >= 0))
kable(pq, digits = 6, format.args = list(big.mark = ","),
caption = glue("dist/dataset={ds_key}/ read back (content hash {pq_hash})"))
```
## Model table {#models}
```{r}
#| label: models
mm <- taxa |>
left_join(chk |> select(layer, abundance_source, abundance_regridded), by = "layer") |>
mutate(
density_min = map_dbl(layer, \(l) min(cells[[l]]$density)),
density_p98 = map_dbl(layer, \(l) quantile(cells[[l]]$density, 0.98, names = FALSE)),
density_max = map_dbl(layer, \(l) max(cells[[l]]$density)),
cv_median = map_dbl(layer, \(l) median(cells[[l]]$cv, na.rm = TRUE)),
dens_cap = map_dbl(layer, \(l) surf[[l]]$cap))
# one row per written model: the interval and the annual
model_cce <- bind_rows(
mm |> mutate(interval = season, mdl_key = mdl_key, parquet = parquet),
mm |> mutate(interval = "annual", mdl_key = mdl_key_an, parquet = parquet_an)) |>
inner_join(pq |> select(mdl_key, n_cells, val_min, val_max), by = "mdl_key") |>
transmute(
mdl_key, interval, sp_id, layer, common_name = species, scientific_name,
is_guild = FALSE, worms_id, response_type = "density", transform = cce_transform,
dens_cap = signif(dens_cap, 6), val_min, val_max, n_cells,
abundance_source = round(abundance_source, 3), abundance_regridded = round(abundance_regridded, 3),
density_min = signif(density_min, 6), density_p98 = signif(density_p98, 6),
density_max = signif(density_max, 6), cv_median = signif(cv_median, 6),
worms_status, worms_id_accepted, scientific_name_accepted, parquet) |>
arrange(sp_id, interval)
stopifnot("model table != 26 rows" = nrow(model_cce) == 26,
"val outside [1,100]" = all(model_cce$val_min >= 1 & model_cce$val_max <= 100),
"no guild here" = !any(model_cce$is_guild))
write_csv(model_cce, mdl_csv) # dist/model_cce.csv: what build_registry + merge_models_prep read
write_csv(model_cce, repo_csv) # the committed copy
kable(model_cce |> filter(interval == season) |>
select(common_name, worms_id, n_cells, abundance_source, abundance_regridded, density_p98, density_max,
cv_median, val_min, val_max),
format.args = list(big.mark = ","),
caption = glue("model_cce.csv ({mdl_csv}; committed copy data/swfsc_cce_models.csv): 13 interval rows shown, plus 13 annual"))
```
## Humpback whale: source vs regridded {#map}
```{r}
#| label: fig-humpback
#| fig-cap: "Humpback whale summer–fall density (animals km⁻², log scale over four orders of magnitude below the maximum; lower densities, down to 10⁻¹⁴ offshore, take the bottom colour): the source 0.1° cells (left) and the regridded 0.05° cells (right). The bottom row zooms to Monterey Bay. Each source cell maps onto about 2 × 2 grid cells; its edges sit ~0.003° off the grid lines, so alternate rows and columns blend a sliver of the neighbouring cell. Cells clipped by the coast keep the source density over the part of the grid cell they cover."
#| fig-width: 10
#| fig-height: 11
hb_l <- "Humpback_whale_summer_fall"
hb <- src[[hb_l]]
hb_c <- cells[[hb_l]]
hb_r <- bind_cols(msens::cell_lonlat(hb_c$cell_id, grid) |> transmute(x = lon, y = lat), density = hb_c$density)
lims <- max(hb$DENSITY) * c(1e-4, 1) # four orders of magnitude; lower values take the bottom colour
land <- ne_countries(scale = "medium", returnclass = "sf",
country = c("United States of America", "Mexico", "Canada"))
fill <- scale_fill_viridis_c(
"animals / km²", trans = "log10", limits = lims, oob = scales::squish,
breaks = scales::breaks_log(n = 6), labels = \(v) formatC(v, format = "g", digits = 2))
bb <- st_bbox(geom)
zoom <- c(xmin = -123.1, xmax = -121.7, ymin = 36.3, ymax = 37.3)
panel <- function(p, title, b) p + fill +
geom_sf(data = land, fill = "grey85", color = "grey40", linewidth = 0.2, inherit.aes = FALSE) +
coord_sf(xlim = b[c("xmin", "xmax")], ylim = b[c("ymin", "ymax")], expand = FALSE) +
labs(title = title, x = NULL, y = NULL) + theme_minimal(base_size = 10)
p_src <- panel(ggplot() + geom_sf(data = hb, aes(fill = DENSITY), color = NA),
"source: 0.1° cells (DENSITY)", bb)
p_rg <- panel(ggplot() + geom_tile(data = hb_r, aes(x, y, fill = density)),
"regridded: 0.05° cells (dens)", bb)
hb_z <- st_crop(hb, zoom)
p_src_z <- panel(ggplot() + geom_sf(data = hb_z, aes(fill = DENSITY), color = "white", linewidth = 0.15),
"source, Monterey Bay (cell outlines)", zoom)
p_rg_z <- panel(ggplot() + geom_tile(data = hb_r |> filter(between(x, zoom[["xmin"]], zoom[["xmax"]]),
between(y, zoom[["ymin"]], zoom[["ymax"]])),
aes(x, y, fill = density), color = "white", linewidth = 0.1),
"regridded, Monterey Bay (0.05° cells)", zoom)
(p_src | p_rg) / (p_src_z | p_rg_z) + plot_layout(guides = "collect", heights = c(1.6, 1))
```
```{r}
#| label: fig-ud
#| fig-cap: "Humpback whale summer–fall, the written `val`: the population-percentile (ud) transform of the density above, on the 0.05° cells. The brightest cells are the densest 1 %, `val >= 50` holds half of the modelled population, and the dark tail is the last few percent of it down to the floor of 1."
#| fig-width: 10
#| fig-height: 5
hb_v <- bind_cols(hb_r |> select(x, y), val = surf[[hb_l]]$interval$val) |> filter(val >= 1)
ggplot() +
geom_tile(data = hb_v, aes(x, y, fill = val)) +
scale_fill_viridis_c("val (ud)", limits = c(1, 100), option = "magma") +
geom_sf(data = land, fill = "grey85", color = "grey40", linewidth = 0.2, inherit.aes = FALSE) +
coord_sf(xlim = bb[c("xmin", "xmax")], ylim = bb[c("ymin", "ymax")], expand = FALSE) +
labs(x = NULL, y = NULL) + theme_minimal(base_size = 10)
```
## Credits and use constraints {#credits-and-use-constraints}
The quotes below are read from the InPort metadata records at render time. The XML is cached under
`raw/sdm/swfsc/inport_cce/`, and `CCE_FETCH=1` refreshes it.
```{r}
#| label: inport
inport_xml <- function(id) {
f <- path(dir_inport, glue("{id}.xml"))
if (cce_fetch || !file_exists(f))
download.file(glue("https://www.fisheries.noaa.gov/inport/item/{id}/inport-xml"), f,
quiet = TRUE, mode = "wb")
read_xml(f)
}
x_ds <- inport_xml(64349)
x_att <- inport_xml(64350)
txt1 <- \(x, xp) xml_text(xml_find_first(x, xp)) |> str_squish()
md <- \(s) str_replace_all(s, "([*_\"'])", "\\\\\\1") # quote verbatim: escape emphasis + smart quotes
ip <- list(
title = txt1(x_ds, "//item-identification/title"),
published = txt1(x_ds, "//item-identification/publication-date"),
credit = txt1(x_ds, "//data-set-information/data-set-credit"),
constraints = txt1(x_ds, "//access-information/data-use-constraints"),
security = txt1(x_ds, "//access-information/security-class"),
download = txt1(x_ds, "//distribution-information/distribution/download-url"),
modified = txt1(x_ds, "//catalog-details/metadata-record-last-modified"),
cv_def = txt1(x_att, "//data-attribute[name='UNCERTAINTY']/description"),
area_def = txt1(x_att, "//data-attribute[name='AREA_SQKM']/description"))
stopifnot(map_lgl(ip, \(s) !is.na(s) && nzchar(s)))
```
**Citation.** Becker EA, Forney KA, Miller DL, Fiedler PC, Barlow J, Moore JE (2020).
*Habitat-based density estimates for cetaceans in the California Current Ecosystem based on
1991–2018 survey data.* U.S. Department of Commerce, NOAA Technical Memorandum NMFS-SWFSC-638.
<https://doi.org/10.25923/3znq-yx13> [@becker2020cce]. The InPort record, *`r ip$title`*
(published `r ip$published`; metadata last modified `r ip$modified`), gives this credit:
> `r md(ip$credit)`
**Data use constraints** (InPort, verbatim; security class `r ip$security`):
> `r md(ip$constraints)`
**Attribute definitions relied on above** (InPort entity 64350):
> **UNCERTAINTY**: `r md(ip$cv_def)`
>
> **AREA_SQKM**: `r md(ip$area_def)`
The data were downloaded from the ArcGIS item <`r ip$download`> (NOAA Fisheries Office of Science
and Technology). Point of contact: Karin A. Forney (SWFSC). Credit SWFSC and Becker et al. (2020)
wherever these surfaces are shown. The constraint above also asks users to read the metadata before
use. The models describe summer–fall (July–November survey) conditions averaged over 1991–2018, and
carry no information about winter–spring distribution.
## Manifest
```{r}
#| label: manifest
# content-addressed, deterministic manifest (Phase A) -- no wall-clock, so an identical re-ingest
# produces byte-identical bytes and does NOT invalidate downstream targets
smry <- arrow::open_dataset(dir_dist) |>
summarise(n_rows = n(), n_mdl = n_distinct(mdl_key),
val_min = round(min(val), 2), val_max = round(max(val), 2)) |> collect()
msens::report_table(smry, caption = glue("cce density model_cell surface (density → population percentile [1,100], {cce_transform})"))
msens::write_manifest(
manifest, target = "ingest_swfsc_cce", content_hash = pq_hash,
stats = list(
ds_key = ds_key, n_species = nrow(taxa), n_mdl = nrow(model_cce), n_guild = 0L, transform = cce_transform,
p_cap = p_cap, season = season, source_gdb = path_file(gdb), source_geometry_md5 = unname(geom_hash[1]),
worms_snapshot = worms_ver$worms_ver, msens = as.character(packageVersion("msens")),
note = "13 CCE species, one summer-fall interval -> population percentile [1,100]; annual = that interval (one interval); registered, merged only under FOLD_DENSITY"),
force = msens::force_target("ingest_swfsc_cce"))
kable(tibble(output = c(dir_dist, mdl_csv, repo_csv, cmp_csv, manifest, if (cce_cog) dir_cog),
n_files = map_int(output, \(p) if (dir_exists(p)) length(dir_ls(p)) else as.integer(file_exists(p)))),
caption = "Outputs")
```