Skip to content

Commit

Permalink
infer units and description of some auxiliary variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dchandan committed Feb 23, 2024
1 parent 106d497 commit 6316908
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions STACpopulator/extensions/datacube.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"],
Expand All @@ -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():
Expand Down

0 comments on commit 6316908

Please sign in to comment.