API reference¶
Everything is importable from the top level: import calcofi4py as cc.
Public database releases¶
cc_get_db ¶
cc_get_db(version='latest', tables=None, supplemental=False, con=None)
DuckDB connection with every release table registered as a view.
Parameters mirror calcofi4r::cc_get_db():
version:"latest"(default) or a pinned"vYYYY.MM.DD"— pin for reproducibility, releases are immutable.tables: restrict to these table names (also the way to opt in to a single supplemental table by name).supplemental: include the supplemental tables (obs_ctd_full~216M rows,obs_mets_full~20M) that are hosted + cataloged but excluded by default.con: register the views on an existing DuckDB connection (e.g. one that already has the PostgreSQL database attached) instead of a new in-memory one.
con = cc_get_db() con.sql("SELECT count(*) FROM sample").fetchone()
Source code in src/calcofi4py/release.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
cc_query ¶
cc_query(sql, version='latest')
One-shot SQL against a release; returns a duckdb relation.
cc_query(...).df() for a pandas DataFrame, .fetchall() for tuples.
Source code in src/calcofi4py/release.py
109 110 111 112 113 114 | |
cc_list_versions ¶
cc_list_versions()
All published release versions (newest first), from versions.json.
Source code in src/calcofi4py/release.py
25 26 27 | |
cc_catalog ¶
cc_catalog(version='latest')
The release catalog.json: table names, row counts, partitioned/supplemental flags.
Source code in src/calcofi4py/release.py
39 40 41 42 | |
cc_resolve_version ¶
cc_resolve_version(version='latest')
Resolve "latest" to the promoted version string (e.g. v2026.08.14).
Source code in src/calcofi4py/release.py
30 31 32 33 34 35 36 | |
PostgreSQL (CTD QA/QC working database)¶
cc_pg_connect ¶
cc_pg_connect(dbname='calcofi', host=None, port=None, user=None, tunnel=False, **kwargs)
psycopg connection to the CalCOFI PostgreSQL database, defaults resolved.
- host:
postgison the CalCOFI server (RStudio/Jupyter there), otherwiselocalhost— the local end of your SSH tunnel.PGHOSToverrides. - user:
PGUSERif set, else the role in your~/.pgpassfor this host/port/db, else your OS user name. - password: never passed — libpq reads
~/.pgpass. tunnel=Truestartsssh -N calcofifor you first (off-server only).
con = cc_pg_connect(tunnel=True) con.execute("SELECT count(*) FROM ctd.cast").fetchone()
Source code in src/calcofi4py/postgres.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
cc_pg_tunnel ¶
cc_pg_tunnel(ssh_host='calcofi', local_port=5432, remote_port=5432, wait=10.0)
Open ssh -N -L {local_port}:localhost:{remote_port} {ssh_host} in the background.
Uses your ~/.ssh/config alias (host, user, key) so no credentials are
handled here; Windows 10+ has ssh.exe built in. Reused while alive;
close with :func:cc_pg_tunnel_close. If something already listens on
local_port it is left alone (use local_port=15432 in both places if
that is not the CalCOFI tunnel).
Source code in src/calcofi4py/postgres.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
cc_pg_tunnel_close ¶
cc_pg_tunnel_close(ssh_host='calcofi', local_port=5432)
Stop a tunnel started by :func:cc_pg_tunnel.
Source code in src/calcofi4py/postgres.py
168 169 170 171 172 173 | |
cc_pg_attach ¶
cc_pg_attach(con, alias='pg', dbname='calcofi', host=None, port=None, user=None, read_only=True)
ATTACH the PostgreSQL database inside a DuckDB connection.
One DuckDB query can then join the public release tables (from
:func:calcofi4py.cc_get_db) with the team's PostgreSQL tables
(pg.ctd.flag, pg.work.*). The password comes from ~/.pgpass
(DuckDB's postgres extension uses libpq). read_only=False also allows
bulk writes from Parquet into PostgreSQL.
con = cc_get_db() cc_pg_attach(con) con.sql("SELECT count(*) FROM pg.ctd.flag").fetchone()
Source code in src/calcofi4py/postgres.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
cc_pgpass_user ¶
cc_pgpass_user(host, port, dbname)
The role name recorded in ~/.pgpass for host:port:dbname (first match).
Lets a user who copied the file from the server connect with no PGUSER.
Format per line: host:port:database:user:password (password may contain
:; * wildcards; # comments).
Source code in src/calcofi4py/postgres.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
cc_on_server ¶
cc_on_server()
True inside the CalCOFI server containers, where the DB is host postgis.
Source code in src/calcofi4py/postgres.py
32 33 34 35 36 | |
CTD QA/QC (ctd schema)¶
Read casts and scans, run the portable QC rules, propose flags, derive clean products, and plot — see the worked notebook clean_ctd_cruise-var.
cc_ctd_casts ¶
cc_ctd_casts(con, study, best_only=True)
One row per physical cast of a cruise, from ctd.cast.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
con
|
psycopg connection (:func: |
required | |
study
|
str
|
source cruise id as in the files, e.g. |
required |
best_only
|
bool
|
only the best data stage per cruise × direction (default);
|
True
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
DataFrame with |
Source code in src/calcofi4py/ctd.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
cc_ctd_scans ¶
cc_ctd_scans(con, study, columns=('tempave', 'salt1', 'ox1'), cast_id=None, qc=True)
Scan-level data for a cruise from ctd.v_scan_qc (best stage).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
Iterable[str]
|
measurement columns to include (see |
('tempave', 'salt1', 'ox1')
|
cast_id
|
str | None
|
restrict to one cast (e.g. |
None
|
qc
|
bool
|
read from |
True
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
DataFrame keyed |
Source code in src/calcofi4py/ctd.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
cc_qc_spike ¶
cc_qc_spike(con, study, column='tempave', spike_threshold=0.5, neighbour_tol=0.5)
Single-scan spikes against a locally smooth profile (ctd_spike.sql).
Neighbour agreement is the whole trick: a point qualifies only if it deviates
from the midpoint of its neighbours by more than spike_threshold WHILE
the neighbours agree with each other within neighbour_tol — otherwise
every steep-but-smooth thermocline gradient fires. (Measured on one cruise:
naive 92 hits, with neighbour agreement 19 — the other 73 were real
gradients.)
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
one row per suspect scan: |
Source code in src/calcofi4py/ctd.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
cc_qc_sensor_pair ¶
cc_qc_sensor_pair(con, study, column1='temp1', column2='temp2', threshold=0.05)
Primary vs secondary sensor disagreement (ctd_sensor1_vs_sensor2).
The source's own *Q codes 1/2 mean "use primary"/"use secondary"
precisely because one sensor of a pair can misbehave; a persistent gap
between the pair is how that is spotted.
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
one row per scan where |
Source code in src/calcofi4py/ctd.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
cc_qc_range ¶
cc_qc_range(con, study, column, valid_min=None, valid_max=None)
Values outside declared physical bounds (ctd_value_out_of_range).
Bounds are deliberately generous — they catch the impossible (an unconverted
-99 sentinel, a scaling error), they do not police oceanography.
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
one row per out-of-bounds scan: |
Source code in src/calcofi4py/ctd.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
cc_propose_flags ¶
cc_propose_flags(con, scan_ids, variable, qual_code, reason, rule_key=None, proposed_value=None, commit=True)
Propose QC flags: one ctd.flag row per scan × variable.
Idempotent: a scan that already carries a proposed or accepted flag
for this variable (any proposer) is skipped, so re-running a notebook does
not stack duplicates. Curators then accept/reject in SQL or pgAdmin; only
accepted flags affect ctd.v_scan_qc / ctd.v_scan_clean.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
qual_code
|
int
|
IODE code from |
required |
rule_key
|
str | None
|
the QC rule that proposed it (e.g. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
number of flags actually inserted |
Source code in src/calcofi4py/ctd.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
cc_flags ¶
cc_flags(con, study=None, status=None)
The QC ledger, joined to its scans: who proposed what, where, and its fate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
study
|
str | None
|
restrict to one cruise; default all |
None
|
status
|
str | None
|
|
None
|
Source code in src/calcofi4py/ctd.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
cc_bin_1m ¶
cc_bin_1m(con, study, column='tempave', cast_dir='D', write_table=None, commit=True)
Clean 1 m binned averages per cast, from ctd.v_scan_clean.
"Clean" means: accepted fixes substituted, accepted-bad values NULLed — exactly the ledger's verdicts and nothing else. Regenerable at any time, which is the point of keeping originals immutable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cast_dir
|
str
|
|
'D'
|
write_table
|
str | None
|
also write the result to |
None
|
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
DataFrame |
Source code in src/calcofi4py/ctd.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
cc_station_map ¶
cc_station_map(casts, zoom=4.7, title=None)
Map of station occupations: one labeled marker per cast_seq.
Every occupation has a down- and an upcast at the same position, so plotting
casts individually just stacks markers; here the pair collapses to one point
(downcast position preferred) labeled with its cast_seq — the number to
cross-reference in every other figure and table. Hover carries the station,
time, both directions' scan counts and the max depth. Occupations whose
source files carry -99 positions are absent from the map but present in
every table.
One markers+text trace, and the labels MUST be strings: integer text
serializes to plotly's binary typed-array encoding, which the symbol layer
treats as icon names ("Image -15 could not be loaded") and drops. The
opposite decomposition — a separate mode="text" trace — does not work
either: plotly 3.7 never creates the symbol layer for a text-only
scattermap trace, so the labels silently vanish. String labels on the
combined trace is the one configuration that renders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
casts
|
'pd.DataFrame'
|
from :func: |
required |
zoom
|
float
|
initial map zoom (the CalCOFI grid fits at ~4.7) |
4.7
|
Source code in src/calcofi4py/ctd.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 | |
cc_profile_plot ¶
cc_profile_plot(scans, column='tempave', cast_ids=None, flags=None, units='', title=None)
Depth profiles with down- and upcasts distinguished; optional flag overlay.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scans
|
'pd.DataFrame'
|
from :func: |
required |
cast_ids
|
Iterable[str] | None
|
restrict to these casts (default: all in |
None
|
flags
|
'pd.DataFrame | None'
|
rows with |
None
|
Source code in src/calcofi4py/ctd.py
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | |
cc_profile_explorer ¶
cc_profile_explorer(scans, column='tempave', flags=None, units='', title=None, default='all')
Depth profiles with a dropdown selector per cast_seq.
The all-casts envelope is unreadable at cruise scale, so this builds one
down + up trace pair per occupation and a dropdown ("all casts" + every
cast_seq) that isolates a single pair — with its flagged scans (red ×)
when flags rows fall on it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
flags
|
'pd.DataFrame | None'
|
rows with |
None
|
default
|
int | str
|
initially selected option — |
'all'
|
Source code in src/calcofi4py/ctd.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | |
cc_section_plot ¶
cc_section_plot(scans, casts, column='tempave', units='', title=None)
Section through the cruise: cast_seq (x) × depth (y), colored by value.
Downcasts only — a quick-look transect, not an interpolated product (each
vertical stripe is one cast's scans). The x axis is the same cast_seq
that labels :func:cc_station_map and indexes :func:cc_flag_summary.
Source code in src/calcofi4py/ctd.py
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | |
cc_flag_summary ¶
cc_flag_summary(ledger, scans, column)
Flags rolled up per cast_seq — the triage table.
Joins the ledger (:func:cc_flags) to the scans it points at and answers
"which casts most need a human": flags by rule, the depth span and value
range flagged, and the share of the cast's scans affected. Sort descending
and start at the top; casts absent from the table have no flags.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ledger
|
'pd.DataFrame'
|
from :func: |
required |
scans
|
'pd.DataFrame'
|
from :func: |
required |
column
|
str
|
the measurement column the flags refer to |
required |
Returns:
| Type | Description |
|---|---|
'pd.DataFrame'
|
one row per flagged |
Source code in src/calcofi4py/ctd.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
cc_session_info ¶
cc_session_info(packages=_DEFAULT_PKGS, repos=None, extra=None)
The Python equivalent of R's devtools::session_info(), as printable text.
Made for the tail of a QA/QC notebook: when the rendered HTML is kept as the
archive of a cleaning run, this block records exactly what produced it —
Python, platform, package versions (calcofi4py above all), and the git
commit of any data-rule directory the run depended on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
packages
|
tuple[str, ...]
|
distributions to report (missing ones are noted, not fatal) |
_DEFAULT_PKGS
|
repos
|
dict[str, tuple[str, str | None]] | None
|
|
None
|
extra
|
dict[str, str] | None
|
extra
|
None
|
Source code in src/calcofi4py/session.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |