Skip to content

Commit edfeb88

Browse files
committed
Allow parsing GRD files in the Planetary Computer
1 parent 18e0fc8 commit edfeb88

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

tests/test_10_esa_safe.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def test_parse_annotation_filename() -> None:
159159
"s1b-iw1-slc-vv-20210401t052624-20210401t052649-026269-032297-004.xml"
160160
)
161161

162-
assert res == ("iw1", "vv", "20210401t052624")
162+
assert res == ("", "iw1", "vv", "20210401t052624")
163163

164164
with pytest.raises(ValueError):
165165
esa_safe.parse_annotation_filename("")

xarray_sentinel/esa_safe.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ def findall(
9494
return values
9595

9696

97-
def parse_annotation_filename(name: str) -> T.Tuple[str, str, str]:
97+
def parse_annotation_filename(name: str) -> T.Tuple[str, str, str, str]:
9898
match = re.match(
99-
r"[a-z-]*s1[ab]-([^-]*)-[^-]*-([^-]*)-([\dt]*)-", os.path.basename(name)
99+
r"([a-z-]*)s1[ab]-([^-]*)-[^-]*-([^-]*)-([\dt]*)-", os.path.basename(name)
100100
)
101101
if match is None:
102102
raise ValueError(f"cannot parse name {name!r}")
@@ -106,7 +106,7 @@ def parse_annotation_filename(name: str) -> T.Tuple[str, str, str]:
106106
@functools.lru_cache
107107
def parse_manifest_sentinel1(
108108
manifest_path: PathOrFileType,
109-
) -> T.Tuple[T.Dict[str, T.Any], T.Dict[str, T.Tuple[str, str, str, str]]]:
109+
) -> T.Tuple[T.Dict[str, T.Any], T.Dict[str, T.Tuple[str, str, str, str, str]]]:
110110
# We use ElementTree because we didn't find a XSD definition for the manifest
111111
manifest = ElementTree.parse(manifest_path)
112112

@@ -155,12 +155,10 @@ def parse_manifest_sentinel1(
155155
if location_tag is not None:
156156
file_href = location_tag.attrib["href"]
157157
try:
158-
swath, polarization, start = parse_annotation_filename(
159-
os.path.basename(file_href)
160-
)
158+
description = parse_annotation_filename(os.path.basename(file_href))
161159
except ValueError:
162160
continue
163161
file_type = file_tag.attrib["repID"]
164-
files[file_href] = (file_type, swath, polarization, start)
162+
files[file_href] = (file_type,) + description
165163

166164
return attributes, files

xarray_sentinel/sentinel1.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,13 @@ def open_azimuth_fm_rate_dataset(annotation: esa_safe.PathOrFileType) -> xr.Data
353353

354354

355355
def find_available_groups(
356-
product_files: T.Dict[str, T.Tuple[str, str, str, str]],
356+
product_files: T.Dict[str, T.Tuple[str, str, str, str, str]],
357357
product_path: str,
358358
check_files_exist: bool = False,
359359
fs: fsspec.AbstractFileSystem = fsspec.filesystem("file"),
360360
) -> T.Dict[str, T.List[str]]:
361361
groups: T.Dict[str, T.List[str]] = {}
362-
for path, (type, swath, polarization, _) in product_files.items():
362+
for path, (type, _, swath, polarization, _) in product_files.items():
363363
swath_pol_group = f"{swath}/{polarization}".upper()
364364
abspath = os.path.join(product_path, os.path.normpath(path))
365365
if check_files_exist:
@@ -419,6 +419,7 @@ def open_pol_dataset(
419419
}
420420
encoding = {}
421421
swap_dims = {}
422+
chunks: T.Union[None, T.Dict[str, int]] = None
422423

423424
azimuth_time = pd.date_range(
424425
start=first_azimuth_time,
@@ -452,6 +453,7 @@ def open_pol_dataset(
452453
import dask # noqa
453454

454455
encoding["preferred_chunks"] = {"line": lines_per_burst}
456+
chunks = {}
455457
except ModuleNotFoundError:
456458
pass
457459

@@ -486,7 +488,7 @@ def open_pol_dataset(
486488
# the with is needed to avoid polluting stderr when the try block fails
487489
with contextlib.redirect_stderr(open("/dev/null", "w")):
488490
try:
489-
arr = xr.open_dataarray(fs.open(measurement), engine="rasterio") # type: ignore
491+
arr = xr.open_dataarray(fs.open(measurement), engine="rasterio", chunks=chunks) # type: ignore
490492
except AttributeError:
491493
arr = xr.open_dataarray(measurement, engine="rasterio") # type: ignore
492494

@@ -669,14 +671,15 @@ def compute_burst_centres(
669671

670672

671673
def do_override_product_files(
672-
template: str, product_files: T.Dict[str, T.Tuple[str, str, str, str]]
673-
) -> T.Dict[str, T.Tuple[str, str, str, str]]:
674+
template: str, product_files: T.Dict[str, T.Tuple[str, str, str, str, str]]
675+
) -> T.Dict[str, T.Tuple[str, str, str, str, str]]:
674676
overridden_product_files = {}
675-
for path, (type, swath, polarization, date) in product_files.items():
677+
for path, description in product_files.items():
678+
type, prefix, swath, polarization, date = description
676679
ext = os.path.splitext(path)[1]
677680
dirname = os.path.dirname(path)
678681
overridden_path = template.format(**locals())
679-
overridden_product_files[overridden_path] = (type, swath, polarization, date)
682+
overridden_product_files[overridden_path] = description
680683
return overridden_product_files
681684

682685

0 commit comments

Comments
 (0)