File: reformat.py

package info (click to toggle)
xarray-sentinel 0.9.5%2Bds-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 21,540 kB
  • sloc: xml: 77,744; python: 2,076; makefile: 32; sh: 17
file content (51 lines) | stat: -rw-r--r-- 1,573 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from typing import Any, Dict, Optional

import xarray as xr

from . import esa_safe


def to_group_zarr(
    product_path: esa_safe.PathType,
    output_store: Any,
    groups: Optional[Dict[str, str]] = None,
    **kwargs: Any,
) -> None:
    root = xr.open_dataset(product_path, engine="sentinel-1")
    root.to_zarr(output_store, mode="w", **kwargs)

    if groups is None:
        groups = {g: g for g in root.attrs["subgroups"]}

    for group_out, group_in in groups.items():
        try:
            group_ds = xr.open_dataset(
                product_path, engine="sentinel-1", group=group_in
            )
            group_ds.to_zarr(output_store, mode="a", group=group_out, **kwargs)
        except FileNotFoundError:
            pass


# Apparently there is no way to save SLC images because "netcdf4" doesn't support complex data
# and "h5netcdf" crashes on an issue related to dimension names
def to_group_netcdf(
    product_path: esa_safe.PathType,
    output_store: str,
    groups: Optional[Dict[str, str]] = None,
    **kwargs: Any,
) -> None:
    root = xr.open_dataset(product_path, engine="sentinel-1")
    root.to_netcdf(output_store, mode="w", **kwargs)

    if groups is None:
        groups = {g: g for g in root.attrs["subgroups"]}

    for group_out, group_in in groups.items():
        try:
            group_ds = xr.open_dataset(
                product_path, engine="sentinel-1", group=group_in
            )
            group_ds.to_netcdf(output_store, mode="a", group=group_out, **kwargs)
        except FileNotFoundError:
            pass