Skip to content
Draft
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
2 changes: 1 addition & 1 deletion src/medunda/plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
LOGGER = logging.getLogger(__name__)

VAR_METADATA = {
"o2": {"label": "Oxygen", "unit": "µmol/m³", "cmap": cmocean.cm.deep}, # pyright: ignore[reportAttributeAccessIssue]
"o2": {"label": "Oxygen", "unit": "mmol/m³", "cmap": cmocean.cm.deep}, # pyright: ignore[reportAttributeAccessIssue]
"chl": {
"label": "Chlorophyll-a",
"unit": "mg/m³",
Expand Down
41 changes: 41 additions & 0 deletions src/medunda/tools/units.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging

from medunda.components.dataset import Dataset
from medunda.plotter import VAR_METADATA

logger = logging.getLogger(__name__)


def check_variable_units(var):
"""
Check if a single variable has a "unit" attribute.
"""
var_name = var.name
units = var.attrs.get("units")
result = {
"variable": var_name,
"units": units,
}
return result


def check_units(ds: Dataset, fill_from_metadata: bool = True):
for var_name in ds.data_vars:
var = ds[var_name]
units = ds.attrs.get("units", None)

if units is None and fill_from_metadata:
unit_meta = VAR_METADATA.get(var_name, {}).get("unit")
if unit_meta:
var.attrs["units"] = unit_meta
logger.warning(
f"Variable {var_name} have no unit so it is filled from VAR_METADATA"
)
else:
raise ValueError(f"Variable {var_name} has no units")


def check_file_units():
"""Open a NetCDF file and check the presence of a variable.
If a unit is missing, it fills it from VAR_METADATA."""
pass