eoio.processors.datatree.processor module#

eoio.processors.datatree.processor#

Reorganise a multi-grid Dataset into an xarray.DataTree.

Many EO products contain variables on multiple spatial grids (e.g. Sentinel-2 bands at 10 m, 20 m and 60 m). Storing all of these in a single Dataset leads to multiple sets of coordinates and dimensions that complicate spatial operations with rioxarray/rasterio, which expect a single grid.

This processor groups variables by grid and places each group into a separate DataTree node. Dimension names such as x_10m/y_10m are simplified to x/y within each node (controlled by rename_dims).

Note: This processor returns an xr.DataTree, not an xr.Dataset. It must be the final step in a processing pipeline.

Example output tree:

/
├── 10m
│   ├── B02(y, x)
│   ├── B03(y, x)
│   └── viewing_zenith_angle(band_10m, y, x)
├── 20m
│   ├── B05(y, x)
│   └── B06(y, x)
└── 60m
    └── B01(y, x)

User config example:

processors = {
    "to_datatree": {},
}

# Use a specific metadata attribute to identify the grid:
processors = {
    "to_datatree": {"grid_attr": "product_metadata.geometry_id"},
}

# Keep original dimension names within each node:
processors = {
    "to_datatree": {"rename_dims": False},
}

The reverse operation from_datatree reassembles a DataTree produced by this processor back into a flat Dataset.

class eoio.processors.datatree.processor.FromDataTree(context: Any | None = None, processor_path: str | None = None, **kwargs)[source]#

Bases: BaseProcessor

name = 'from_datatree'#
run(data: DataTree) Dataset[source]#

Runs processor subprocessors sequentially in order, output of each feeding into the next

Parameters:

args – processor input arguments

Returns:

output values of final processor

class eoio.processors.datatree.processor.ToDataTree(params: Dict[str, Any] | None = None, context: Dict[str, Any] | None = None)[source]#

Bases: BaseProcessor

Reorganise a multi-grid Dataset into an xarray.DataTree.

Variables are grouped by grid identity (resolved from metadata or dimension names) and placed into separate DataTree nodes. Each node contains only variables on a common grid.

This processor returns an xr.DataTree not an xr.Dataset. It must be the final step in a processing pipeline.

Processor parameters#

param grid_attr:

Dot-separated attribute path used to identify which grid a variable belongs to (e.g. "product_metadata.geometry_id"). If None (default), the grid is inferred from dimension name suffixes (x_10m/y_10m"10m").

param rename_dims:

If True (default), simplify grid-suffixed dimension names to plain x/y within each node, since the grid is now encoded in the node path. Set to False to preserve original dimension names.

param rename_vars:

If True (default), strip the grid suffix from variable and non-spatial coordinate names that carry it. For example, reflectance_10mreflectance and band_10mband within the /10m node. Names without the suffix are left unchanged. Set to False to preserve original names.

grid_attr: str | None#
name = 'to_datatree'#
rename_dims: bool#
rename_vars: bool#
run(ds: Dataset) DataTree[source]#

Reorganise the dataset into a DataTree grouped by grid.

Parameters:

ds – Input multi-grid dataset.

Returns:

DataTree with one node per grid.

eoio.processors.datatree.processor.build_datatree(ds: Dataset, *, grid_attr: str | None = None, rename_dims: bool = True, rename_vars: bool = True) DataTree[source]#

Reorganise a multi-grid Dataset into an xarray.DataTree.

Variables without a detectable grid are placed at the root node alongside the global dataset attributes.

Parameters:
  • ds – Input dataset.

  • grid_attr – Dot-separated attribute path used to identify the grid for each variable. Falls back to dimension name suffix detection if None or if the attribute is absent on a variable.

  • rename_dims – Rename grid-suffixed spatial dimensions (x_10mx) within each node.

  • rename_vars – Strip the grid suffix from variable and non-spatial coordinate names that carry it (reflectance_10mreflectance).

Returns:

DataTree with one child node per grid.

eoio.processors.datatree.processor.from_datatree(dt: DataTree) Dataset[source]#

Reassemble a DataTree produced by build_datatree() into a flat Dataset.

Restores grid-suffixed dimension names (xx_<grid>) so the merged dataset can coexist in one flat namespace. Global attrs from the root are preserved.

Note: variable name renaming (rename_vars=True) is not reversed — it is a lossy simplification. reflectance in /10m stays as reflectance after flattening.

Parameters:

dt – DataTree to flatten.

Returns:

Merged Dataset with grid-suffixed names restored.