Skip to content

Commit 60cf0db

Browse files
authored
Merge pull request #7 from thhomas/fix/safe-spectral-band-selection
Fix SAFE/S2GM band selection picking up auxiliary rasters (AOT/WVP/TCI).
2 parents 6e8b602 + fd29b5c commit 60cf0db

5 files changed

Lines changed: 41 additions & 9 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ jobs:
1414
python-version: "3.12"
1515
- name: Install test runner
1616
run: python -m pip install --upgrade pip pytest
17+
- name: Install package
18+
run: python -m pip install --no-deps -e .
1719
- name: Syntax check
1820
run: python -m compileall opensr_utils setup.py demo.py tests
1921
- name: Shell syntax check

‎README.md‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44

55
![img1](resources/utils_poster.png)
66

7-
*Info*
8-
- It is functional for RGB+NIR images currently.
9-
- Functionalities coming in the future:
10-
- [x] Input many tiles at once
11-
- [x] Pass more file types
12-
- [ ] Run SR for a selectable amount of bands
137

148
# Description
159
This package performs super-resolution with any PyTorch or PyTorch lighning model for the Sentinel-2 10m bands (R-G-B-NIR).

‎opensr_utils/pipeline.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ def safe_band_name(path):
353353
used = set(lr_file_dict.values())
354354
for path in jp2s:
355355
if path not in used:
356-
lr_file_dict[safe_band_name(path)] = path
356+
name = safe_band_name(path)
357+
if name.startswith("B") and name[1:].isalnum():
358+
lr_file_dict[name] = path
357359
if not lr_file_dict:
358360
raise FileNotFoundError(f"No usable SAFE bands found in {self.root}.")
359361

@@ -407,7 +409,11 @@ def safe_band_name(path):
407409
lr_file_dict[label] = by_name[band_name]
408410
used = set(lr_file_dict.values())
409411
for band_name, path in by_name.items():
410-
if path not in used:
412+
if (
413+
path not in used
414+
and band_name.startswith("B")
415+
and band_name[1:].isalnum()
416+
):
411417
lr_file_dict[band_name] = path
412418
self.image_meta["lr_file_dict"] = lr_file_dict
413419

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name="opensr-utils",
9-
version="1.2.0",
9+
version="2.0.0",
1010
author="Simon Donike, Cesar Aybar, Luis Gomez Chova, Freddie Kalaitzis",
1111
author_email="accounts@donike.net",
1212
description="Utilities supporting the ESA opensr-model package for Super-Resolution of Sentinel-2 Imagery",

‎tests/test_pipeline_helpers.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,33 @@ def test_write_to_file_creates_placeholder_from_prediction_index(
438438
with rasterio.open(runner.placeholder_filepath) as src:
439439
assert (src.width, src.height, src.count) == (9, 6, 2)
440440
assert src.dtypes == ("uint16", "uint16")
441+
442+
443+
def test_safe_band_selection_excludes_auxiliary_rasters(write_raster, tmp_path):
444+
safe = tmp_path / "S2A_TEST.SAFE"
445+
granule = safe / "GRANULE" / "L1C_TEST" / "IMG_DATA" / "R10m"
446+
granule.mkdir(parents=True)
447+
for band in ("B02", "B03", "B04", "B08", "AOT", "WVP", "TCI"):
448+
write_raster(
449+
granule / f"T_TEST_{band}_10m.jp2",
450+
data=np.ones((1, 16, 16), dtype=np.uint16),
451+
width=16,
452+
height=16,
453+
)
454+
(safe / "manifest.safe").write_text("")
455+
456+
runner = pipeline.large_file_processing(
457+
root=str(safe),
458+
model=None,
459+
window_size=(8, 8),
460+
factor=2,
461+
overlap=4,
462+
eliminate_border_px=0,
463+
device="cpu",
464+
batch_size=1,
465+
num_workers=0,
466+
prefetch_factor=0,
467+
)
468+
469+
assert runner.image_meta["bands"] == 4
470+
assert set(runner.image_meta["lr_file_dict"]) == {"R", "G", "B", "NIR"}

0 commit comments

Comments
 (0)