Returns the SQL strings that compute the share score from the scoring schema. The caller (normally
share_run()) materializes these relations in the connection first:
model_cell(mdl_key, cell_id, <val_col>)– the US scoring surface, one row per (taxon, cell, model): values 0-100 as the merge wrote them (suitability inside the range, the governinger_scoreon range-only cells, ER x suitability for the premultiplied sea turtles). A multi-model AquaMaps-only taxon keeps duplicate (taxon, cell) rows, as the merge does.model_cell_er(mdl_key, cell_id, er)– the per-cell extinction risk of theer_mode = 'cell'taxa. May be empty, must exist, must cover every scored cell of those taxa.taxon(ms_merge_key, sp_cat, er_score, er_mode, ...)plus whatevertaxa_wherereferences (is_valid_usa,is_marine).er_modeis one oftaxon,premultiplied,cell.taxon_denom(ms_merge_key, total_global, total_global_strip, source)– each species' totalsum(val / 100 * area_km2)over its WHOLE modelled range (fromdenom_sql()), on the samevalscale asmodel_cell; the_stripvariant omits the floored cells (val = 1) of premultiplied taxa. Must exist; only theglobaldenominator divides by it.cell(cell_id, area_km2, in_usa).zone,zone_cell(zone_seq, fld, val)and(zone_seq, cell_id, pct_covered)withpct_coveredin 0-100.- the nation
nation_sql, a query yielding(cell_id, pct_covered): the cells the national reference (share_USA,A_USA) sums over. Default: everycell.in_usacell in full. It is deliberately NOT a zone row: v8'sUSApseudo-zone is the union of the subregion polygons and misses 10,996 coastal and estuarine study-area cells (1.7 % of the cells, 3.4 % of the scored rows – the Emperor Goose's whole US range), so a species' national share would not be its US total. Every scored cell must lie in the nation;share_run()asserts it.primprod_cell(cell_id, npp)– optional, for$primprod.
Usage
share_sql(
cats = c("bird", "coral", "fish", "invertebrate", "mammal", "primary_producer",
"turtle"),
denom = c("global", "usa", "pooled"),
floor_strip = FALSE,
er_sql =
"(CASE t.er_mode WHEN 'premultiplied' THEN 100 WHEN 'cell' THEN e.er ELSE t.er_score END)",
val_col = "val",
taxa_where = "t.is_valid_usa AND t.is_marine",
nation_sql = "SELECT cell_id, 100.0 AS pct_covered FROM cell WHERE in_usa"
)Arguments
- cats
character; the scored categories (
taxon.sp_catvalues), default the seven of v8+.- denom
one of
"global","usa","pooled".- floor_strip
logical;
TRUEdrops the floored cells (val <= 1) of premultiplied taxa from every sum (numerator, US total, coverage, species counts) and takestotal_global_stripas their denominator (page §9b, the sensitivity column; the default keeps the surface continuous, plan D4).- er_sql
character; the SQL expression of the per-(taxon, cell) risk weight over aliases
t(taxon) ande(model_cell_er): the CASEscore_cell_metrics.qmduses. Its species-level counterpart for the percent of the world,w_s, iser_scorefor thetaxonandcellmodes and 100 forpremultiplied.- val_col
character; the value column of
model_cell(sdm_val_col(con, "model_cell")).- taxa_where
character; SQL over alias
tselecting the scored species.- nation_sql
character; SQL yielding
(cell_id, pct_covered)for the national reference.
Details
The strings, in the order share_run() executes them:
taxon_wCREATE OR REPLACE TEMP TABLE share_taxon: per scored species ofcats, the species weightw_s, its US total (total_usa, the risk-free value-area over the scoring surface), its denominatordenom_totalfor thisdenom,f_us = total_usa / total_globaland the denominatorsource.baseCREATE OR REPLACE TEMP TABLE share_base: per (zone, species) the two numerators every denominator divides –num = sum(v * A * p / 100)andnum_w = sum(w * v * A * p / 100)(the risk weight applied at cell level so a per-cell ER is honoured) – andn_rows. This is the one pass overmodel_cell x zone_cell;share_run()builds it once and reuses it across denominators (share_base_sigrecords the arguments it was built with).zone_taxonCREATE OR REPLACE TEMP TABLE share_zone_taxon: per (zone, species), the species' share of its range inside the zone under BOTH per-species denominators (share_global,share_usa) and its risk-weighted contribution to the zone total under the chosen one (contrib = num_w / D_s / 100).cat_cellCREATE OR REPLACE TEMP TABLE share_cat_cell: per (category, cell), the risk-weighted sum of the species' cell shares (share) andn_species(distinct).zone_areaCREATE OR REPLACE TEMP TABLE share_zone_area: each zone's covered areaarea_km2 = sum(area_km2 * pct_covered / 100)and, per category, the covered area of the cells carrying that category (area_cat).zone_catSELECT: per (zone, category) the zone totalshare,n_species,mean_share,area_km2,coverage,pct_nation,pct_world,v10; the caller applies the floor.cell_scoreSELECT: per (category, cell)share,n_speciesand the cell-levelv10(100 * (share / A_c) / (share_USA / A_USA)).primprodSELECT: per zone the area-weighted mean ofnppover ALL the zone's cells (a cell without a value counts 0, as the publishedpct_areadown-weight does), itscoverage, andv10 = 100 * mean_z / mean_USA(mean_usaover the nation, same estimator).nationSELECT: per category the national referenceshare_usa,area_usa,n_cells.outside_nationSELECT: scored rows / cells outside the nation (must be 0).er_gapSELECT: scored cells ofcell-mode taxa without a per-cell ER (must be 0).er_dupSELECT: duplicate(mdl_key, cell_id)rows ofmodel_cell_er(must be 0: the LEFT JOIN would double a species' share).
Examples
sq <- share_sql(denom = "global")
names(sq)
#> [1] "taxon_w" "base" "zone_taxon" "cat_cell"
#> [5] "zone_area" "zone_cat" "cell_score" "primprod"
#> [9] "nation" "outside_nation" "er_gap" "er_dup"