From 63169082177784cb5c31f3e050571e7b1f662300 Mon Sep 17 00:00:00 2001 From: Deepak Chandan Date: Thu, 22 Feb 2024 19:24:07 -0500 Subject: [PATCH] infer units and description of some auxiliary variables --- STACpopulator/extensions/datacube.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/STACpopulator/extensions/datacube.py b/STACpopulator/extensions/datacube.py index 5f452b3..a98f9aa 100644 --- a/STACpopulator/extensions/datacube.py +++ b/STACpopulator/extensions/datacube.py @@ -169,10 +169,8 @@ def dimensions(self) -> dict[str, Dimension]: properties = dict( type=type_.value, extent=extent, - description=v.get( - "description", - v.get("long_name", criteria["standard_name"][0]) - ) or "", + description=v.get("description", v.get("long_name", criteria["standard_name"][0])) + or "", ) if type_ == DimensionType.SPATIAL: properties["axis"] = axis @@ -197,6 +195,8 @@ def variables(self) -> dict[str, Variable]: # Some variables like "time_bnds" in some model files do not have any attributes. attrs = {} + self._infer_variable_units_description(name, attrs) + variables[name] = Variable( properties=dict( dimensions=meta["shape"], @@ -207,6 +207,25 @@ def variables(self) -> dict[str, Variable]: ) return variables + def _infer_variable_units_description(self, name, attrs): + """Try to infer the units and description of some simple coordinate variables.""" + if name == "time_bnds": + related_variable = "time" + attrs["description"] = "bounds for the time coordinate" + elif name == "lat_bnds": + related_variable = "lat" + attrs["description"] = "bounds for the latitude coordinate" + elif name == "lon_bnds": + related_variable = "lon" + attrs["description"] = "bounds for the longitude coordinate" + else: + return + + try: + attrs["units"] = self.attrs["variables"][related_variable]["attributes"]["units"] + except KeyError: + pass + def is_coordinate(self, attrs: MutableMapping[str, Any]) -> bool: """Return whether variable is a coordinate.""" for key, criteria in self.coordinate_criteria.items():