Skip to content

Commit 39e4887

Browse files
authored
retrieve bursts by ID (#37)
* retrieve bursts by ID and print found and not found burst IDs
1 parent 40ae1c7 commit 39e4887

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

src/s1reader/s1_reader.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,8 @@ def _is_zip_annotation_xml(path: str, id_str: str) -> bool:
425425
return True
426426
return False
427427

428-
def load_bursts(path: str, orbit_path: str, swath_num: int, pol: str = 'vv'):
428+
def load_bursts(path: str, orbit_path: str, swath_num: int, pol: str='vv',
429+
burst_ids: list[str]=None):
429430
'''Find bursts in a Sentinel-1 zip file or a SAFE structured directory.
430431
431432
Parameters:
@@ -438,16 +439,27 @@ def load_bursts(path: str, orbit_path: str, swath_num: int, pol: str = 'vv'):
438439
Integer of subswath of desired burst. {1, 2, 3}
439440
pol : str
440441
Polarization of desired burst. {hh, vv, hv, vh}
442+
burst_ids : list[str]
443+
List of burst IDs for which their Sentinel1BurstSlc objects will be
444+
returned. Default of None returns all bursts. Empty list returned if
445+
none of the burst IDs are found. If not all burst IDs are found, a list
446+
containing found bursts will be returned.
441447
442448
Returns:
443449
--------
444450
bursts : list
445451
List of Sentinel1BurstSlc objects found in annotation XML.
446452
'''
447-
448453
if swath_num < 1 or swath_num > 3:
449454
raise ValueError("swath_num not <1 or >3")
450455

456+
if burst_ids is None:
457+
burst_ids = []
458+
459+
# ensure burst IDs is a list
460+
if not isinstance(burst_ids, list):
461+
burst_ids = [burst_ids]
462+
451463
# lower case polarity to be consistent with file naming convention
452464
pol = pol.lower()
453465
pols = ['vv', 'vh', 'hh', 'hv']
@@ -459,12 +471,30 @@ def load_bursts(path: str, orbit_path: str, swath_num: int, pol: str = 'vv'):
459471
if not os.path.exists(path):
460472
raise FileNotFoundError(f'{path} not found')
461473
elif os.path.isdir(path):
462-
return _burst_from_safe_dir(path, id_str, orbit_path)
474+
bursts = _burst_from_safe_dir(path, id_str, orbit_path)
463475
elif os.path.isfile(path):
464-
return _burst_from_zip(path, id_str, orbit_path)
476+
bursts = _burst_from_zip(path, id_str, orbit_path)
465477
else:
466478
raise ValueError(f'{path} is unsupported')
467479

480+
if burst_ids:
481+
bursts = [b for b in bursts if b.burst_id in burst_ids]
482+
483+
burst_ids_found = set([b.burst_id for b in bursts])
484+
485+
if not burst_ids_found:
486+
warnings.warn("None of provided bursts IDs found")
487+
488+
set_burst_ids = set(burst_ids)
489+
if burst_ids_found != set_burst_ids:
490+
diff = set_burst_ids.difference(burst_ids_found)
491+
warn_str = f'Not all burst IDs found. Not found: {diff}. '
492+
warn_str += f'Found: {burst_ids_found}'
493+
warnings.warn(warn_str)
494+
495+
return bursts
496+
497+
468498
def _burst_from_zip(zip_path: str, id_str: str, orbit_path: str):
469499
'''Find bursts in a Sentinel-1 zip file.
470500

0 commit comments

Comments
 (0)