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'#
- class eoio.processors.datatree.processor.ToDataTree(params: Dict[str, Any] | None = None, context: Dict[str, Any] | None = None)[source]#
Bases:
BaseProcessorReorganise 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.DataTreenot anxr.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"). IfNone(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 plainx/ywithin each node, since the grid is now encoded in the node path. Set toFalseto 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_10m→reflectanceandband_10m→bandwithin the/10mnode. Names without the suffix are left unchanged. Set toFalseto preserve original names.
- grid_attr: str | None#
- name = 'to_datatree'#
- rename_dims: bool#
- rename_vars: bool#
- 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
Noneor if the attribute is absent on a variable.rename_dims – Rename grid-suffixed spatial dimensions (
x_10m→x) within each node.rename_vars – Strip the grid suffix from variable and non-spatial coordinate names that carry it (
reflectance_10m→reflectance).
- 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 (
x→x_<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.reflectancein/10mstays asreflectanceafter flattening.- Parameters:
dt – DataTree to flatten.
- Returns:
Merged Dataset with grid-suffixed names restored.