From 756349966b855986aad31266b90b7829e55fc696 Mon Sep 17 00:00:00 2001 From: Nada Akkari Date: Mon, 15 Dec 2025 15:14:43 +0100 Subject: [PATCH 1/2] First draft of the process to check and ensure all variables have a unit of measurement across all the workflow. --- src/medunda/tools/units.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/medunda/tools/units.py diff --git a/src/medunda/tools/units.py b/src/medunda/tools/units.py new file mode 100644 index 0000000..007cbda --- /dev/null +++ b/src/medunda/tools/units.py @@ -0,0 +1,22 @@ +import logging + +from medunda.components.dataset import Dataset +from medunda.plotter import VAR_METADATA + +logger = logging.getLogger(__name__) + + +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") From bebd86959694f7c20dbb0e4ff0286609c1fd558c Mon Sep 17 00:00:00 2001 From: Nada Akkari Date: Mon, 15 Dec 2025 15:17:50 +0100 Subject: [PATCH 2/2] Unit correction --- src/medunda/plotter.py | 2 +- src/medunda/tools/units.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/medunda/plotter.py b/src/medunda/plotter.py index 8079923..55c6250 100644 --- a/src/medunda/plotter.py +++ b/src/medunda/plotter.py @@ -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³", diff --git a/src/medunda/tools/units.py b/src/medunda/tools/units.py index 007cbda..6450d9a 100644 --- a/src/medunda/tools/units.py +++ b/src/medunda/tools/units.py @@ -6,6 +6,19 @@ 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] @@ -20,3 +33,9 @@ def check_units(ds: Dataset, fill_from_metadata: bool = True): ) 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