Skip to content

Commit bbea061

Browse files
committed
fix: update metadata location
1 parent aab2a24 commit bbea061

1 file changed

Lines changed: 52 additions & 15 deletions

File tree

‎eodag_cube/api/product/_product.py‎

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ def to_xarray(
350350

351351
return xd
352352

353-
def _build_cube_metadata(self, ds_dict: XarrayDict) -> tuple[dict, dict]:
353+
def _build_cube_metadata(self, ds_dict: XarrayDict) -> tuple[dict, dict, dict]:
354354
"""
355355
Build cube:dimensions and cube:variables from a dict of xarray.Dataset.
356356
@@ -359,6 +359,11 @@ def _build_cube_metadata(self, ds_dict: XarrayDict) -> tuple[dict, dict]:
359359
"""
360360
dimensions = {}
361361
variables = {}
362+
proj_info = {}
363+
auxiliary_geo_vars = {
364+
"latitude": "Latitude",
365+
"longitude": "Longitude",
366+
}
362367

363368
for ds in ds_dict.values():
364369
epsg_code = None
@@ -371,6 +376,10 @@ def _build_cube_metadata(self, ds_dict: XarrayDict) -> tuple[dict, dict]:
371376
except Exception:
372377
proj_bbox = None
373378
epsg_code = epsg_code or 4326
379+
proj_info["proj:code"] = f"EPSG:{epsg_code}"
380+
if proj_bbox is not None:
381+
proj_info["proj:bbox"] = proj_bbox
382+
proj_info["proj:shape"] = list(ds.sizes.values())
374383

375384
# Dimensions
376385
for dim_name in ds.sizes.keys():
@@ -397,10 +406,6 @@ def _build_cube_metadata(self, ds_dict: XarrayDict) -> tuple[dict, dict]:
397406
dim_entry["axis"] = "z"
398407

399408
dim_entry["reference_system"] = epsg_code
400-
dim_entry["proj:code"] = epsg_code
401-
402-
if proj_bbox is not None:
403-
dim_entry["proj:bbox"] = proj_bbox
404409

405410
if dim_name_str in ds.coords:
406411
values = ds[dim_name_str].values
@@ -425,9 +430,31 @@ def _build_cube_metadata(self, ds_dict: XarrayDict) -> tuple[dict, dict]:
425430

426431
# Variables
427432
for var_name, var in ds.data_vars.items():
428-
variables[str(var_name)] = {"dimensions": list(var.dims), "type": "data"}
429-
430-
return dimensions, variables
433+
variables[str(var_name)] = {
434+
"dimensions": list(var.dims),
435+
"type": "data",
436+
"data_type": str(var.dtype),
437+
"description": var.attrs.get("long_name", ""),
438+
"nodata": var.attrs.get("nodata", -9999),
439+
}
440+
for aux_name, desc in auxiliary_geo_vars.items():
441+
if aux_name in ds:
442+
var = ds[aux_name]
443+
444+
if aux_name in variables:
445+
continue
446+
if aux_name in ds.dims:
447+
continue
448+
449+
variables[aux_name] = {
450+
"dimensions": list(var.dims),
451+
"type": "auxiliary",
452+
"description": desc,
453+
"data_type": str(var.dtype),
454+
"nodata": var.attrs.get("nodata", -9999),
455+
}
456+
457+
return dimensions, variables, proj_info
431458

432459
def _build_bands(self, xd: XarrayDict) -> list[dict]:
433460
"""
@@ -482,10 +509,12 @@ def augment_from_xarray(self) -> EOProduct:
482509
except Exception:
483510
return self
484511

485-
dimensions, variables = self._build_cube_metadata(xd)
512+
dimensions, variables, proj_info = self._build_cube_metadata(xd)
486513
self.properties["cube:dimensions"] = dimensions
487514
self.properties["cube:variables"] = variables
488515
self.properties["bands"] = self._build_bands(xd)
516+
for key, value in proj_info.items():
517+
self.properties[key] = value
489518

490519
else:
491520
for asset_key, asset in self.assets.items():
@@ -494,14 +523,22 @@ def augment_from_xarray(self) -> EOProduct:
494523
except Exception:
495524
continue
496525

497-
dimensions, variables = self._build_cube_metadata(xd)
526+
dimensions, variables, proj_info = self._build_cube_metadata(xd)
498527
asset["cube:dimensions"] = dimensions
499528
asset["cube:variables"] = variables
529+
for key, value in proj_info.items():
530+
asset[key] = value
500531

501-
generated_bands = self._build_bands(xd)
502-
if "bands" in asset:
503-
asset["bands"] = self._merge_bands(asset["bands"], generated_bands)
504-
else:
505-
asset["bands"] = generated_bands
532+
has_band_data = any(
533+
"band_data" in ds.data_vars
534+
for ds in xd.values()
535+
)
536+
537+
if has_band_data:
538+
generated_bands = self._build_bands(xd)
539+
if "bands" in asset:
540+
asset["bands"] = self._merge_bands(asset["bands"], generated_bands)
541+
else:
542+
asset["bands"] = generated_bands
506543

507544
return self

0 commit comments

Comments
 (0)