Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions oceanarray/config/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@
#: where each variable is drawn as one line (not the multi-instrument stack, which
#: uses :data:`LINE_CMAPS_BY_VARIABLE`). Physics use the Okabe-Ito palette (Wong,
#: Nature Methods 8:441, 2011); biogeochemistry uses the Paul Tol palette. Look up
#: with ``VAR_COLORS.get(var, "#000000")``. Mirrors ctdcast's ``VAR_COLORS``.
#: with :func:`var_color` (single fallback, no scattered literals). Mirrors
#: ctdcast's ``VAR_COLORS``.
VAR_COLORS: dict[str, str] = {
# Physics — Okabe-Ito
"temperature": "#56B4E9", # sky blue
Expand All @@ -584,13 +585,40 @@
"turbidity": "#661100", # dark red
}

#: Colour for a variable with no :data:`VAR_COLORS` entry — one canonical fallback
#: so callers don't scatter their own literal defaults (which drift).
VAR_COLOR_DEFAULT: str = "#000000"


def var_color(var: str) -> str:
"""Return the line/marker colour for *var* from :data:`VAR_COLORS`.

Falls back to :data:`VAR_COLOR_DEFAULT` for an unregistered variable, so every
caller shares one default rather than hard-coding its own.

Parameters
----------
var : str
Variable name (key in :data:`VARIABLES` / :data:`VAR_COLORS`).

Returns
-------
str
A hex colour string.

"""
return VAR_COLORS.get(var, VAR_COLOR_DEFAULT)


def vlabel(var: str, prefix: str = "") -> str:
"""Return a matplotlib axis label for *var* from the :data:`VARIABLES` registry.

Format is ``"{prefix}Label (units)"`` when ``label_units`` is non-empty,
or ``"{prefix}Label"`` for dimensionless quantities. The ``prefix`` is
prepended to the label component only, not the units, so
Format is ``"{prefix}Label (units)"`` when ``label_units`` is non-empty, or
``"{prefix}Label (1)"`` for a dimensionless quantity — ``1`` is the CF /
UDUNITS unit string for dimensionless (e.g. practical salinity, whose NetCDF
``units`` attribute is ``"1"``), so the label matches the stored metadata
rather than leaving the reader to wonder whether a unit was forgotten. The
``prefix`` is prepended to the label component only, not the units, so
``vlabel("temperature", prefix="Δ")`` produces ``"ΔTemperature (°C)"``.

Parameters
Expand All @@ -609,8 +637,13 @@ def vlabel(var: str, prefix: str = "") -> str:
"""
entry = VARIABLES.get(var, {})
lbl = f"{prefix}{entry.get('label', var)}"
if var not in VARIABLES:
# Unknown variable: return the bare name. Do NOT append "( )" — that
# would falsely assert the quantity is dimensionless when we simply have
# no registry entry for it.
return lbl
lu = entry.get("label_units", "")
return f"{lbl} ({lu})" if lu else lbl
return f"{lbl} ({lu})" if lu else f"{lbl} (1)"


def vunit(var: str) -> str:
Expand Down
Loading
Loading