diff --git a/STACpopulator/api_requests.py b/STACpopulator/api_requests.py index 05aeaa5..ded9984 100644 --- a/STACpopulator/api_requests.py +++ b/STACpopulator/api_requests.py @@ -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() diff --git a/STACpopulator/extensions/datacube.py b/STACpopulator/extensions/datacube.py index 17fd112..2d52bb4 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 @@ -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"], @@ -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: