Full pipeline to link bottle casts to the SWFSC cruise table
by: (1) finding unmatched ship codes, (2) running match_ships() for
fuzzy matching, (3) adding ship_key and cruise_key columns
to casts, (4) validating against the cruise table.
Usage
derive_cruise_key_on_casts(
con,
ship_renames_csv = NULL,
fetch_ices = TRUE,
datetime_col = "datetime_utc",
table_name = "casts",
cruise_ym_col = NULL,
tolerance_days = 3L
)Arguments
- con
DBI connection to DuckDB with the target table plus
shipandcruise- ship_renames_csv
Optional path to manual ship overrides CSV
- fetch_ices
Logical; if TRUE, also query ICES ship API (default TRUE)
- datetime_col
Name of the timestamp column on the target table used to derive the YYYY-MM prefix (default
"datetime_utc")- table_name
Name of the table to annotate (default
"casts"). Any table with aship_codecolumn anddatetime_colworks — e.g."picoplankton_bacteria_bottle". Aship_namecolumn is used for the unmatched report when present, and treated as NULL when not.- cruise_ym_col
Optional column on the target table holding the source's own cruise designation as YYYYMM (bottle:
"cruise"); seeresolve_cruise_key().- tolerance_days
Span tolerance passed to
resolve_cruise_key().
Value
List with components:
ship_matches: tibble of ship match resultscruise_stats: tibble of cruise bridge match statisticskey_stats: tibble of rows resolved per method (span/source/month/none)unmatched_report: tibble of unmatched ship codes
Details
The cruise_key format is YYYY-MM-NODC (4-digit year, 2-digit month,
NODC ship code), e.g. "1998-02-33JD". The YYYY-MM is the cruise's
designated month, resolved by resolve_cruise_key(): first by the
reference cruise whose observed date span contains the event, then by the
source's own designation (cruise_ym_col), and only last by the
event's calendar month — the rule this function used alone before calcofi4db
3.20.0, which split every cruise that straddled a month boundary (184 of the
664 bottle cruises) onto two keys.
Requires that ship and cruise tables are already loaded in
the DuckDB connection (e.g., via load_prior_tables()); cruise
must carry date_min/date_max from add_cruise_date_span().