Skip to content

Commit

Permalink
Merge pull request #51 from crim-ca/datacube-fixes
Browse files Browse the repository at this point in the history
Fixes to datacube extension helper
  • Loading branch information
dchandan authored Feb 26, 2024
2 parents 7e7adbf + 30de2f6 commit b3d5e0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
8 changes: 5 additions & 3 deletions STACpopulator/api_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,16 @@ def post_stac_item(
item_url = os.path.join(stac_host, f"collections/{collection_id}/items")
r = session.post(item_url, json=json_data)

extra_log_info = {"item_id": item_id, "item_url": os.path.join(item_url, item_id)}

if r.status_code == 200:
LOGGER.info(f"Item {item_name} successfully added")
LOGGER.info(f"Item {item_name} successfully added", extra=extra_log_info)
elif r.status_code == 409:
if update:
LOGGER.info(f"Item {item_id} already exists. Updating.")
LOGGER.info(f"Item {item_id} already exists. Updating.", extra=extra_log_info)
r = session.put(os.path.join(stac_host, f"collections/{collection_id}/items/{item_id}"), json=json_data)
r.raise_for_status()
else:
LOGGER.warn(f"Item {item_id} already exists.")
LOGGER.warn(f"Item {item_id} already exists.", extra=extra_log_info)
else:
r.raise_for_status()
36 changes: 31 additions & 5 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 @@ -191,7 +189,11 @@ def variables(self) -> dict[str, Variable]:
if name in self.attrs["dimensions"]:
continue

attrs = meta["attributes"]
# Some variables like "time_bnds" in some model files do not have any attributes.
attrs = meta.get("attributes", {})

self._infer_variable_units_description(name, attrs)

variables[name] = Variable(
properties=dict(
dimensions=meta["shape"],
Expand All @@ -202,8 +204,32 @@ 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."""

if (desc := attrs.get("description", None)) is not None:
if "bounds for" in desc:
return True

for key, criteria in self.coordinate_criteria.items():
for criterion, expected in criteria.items():
if attrs.get(criterion, None) in expected:
Expand Down

0 comments on commit b3d5e0e

Please sign in to comment.