Skip to content

Commit ec035ff

Browse files
committed
INTERNAL: Rationalize band paths and filename functions (still early work) #31
1 parent d544ac5 commit ec035ff

File tree

8 files changed

+213
-128
lines changed

8 files changed

+213
-128
lines changed

ci/weekly/test_satellites.py

+21-16
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def check_product_consistency(prod: Product):
133133
assert isinstance(orbit_dir, OrbitDirection)
134134

135135

136-
def check_load(prod: Product, first_band) -> None:
136+
def check_load(prod: Product, first_band, pixel_size) -> None:
137137
"""
138138
Check if the loading process
139139
Args:
@@ -145,33 +145,38 @@ def check_load(prod: Product, first_band) -> None:
145145

146146
# Don't orthorectify with a window to 1000 m
147147
with tempenv.TemporaryEnvironment({SAR_DEF_PIXEL_SIZE: "0"}):
148-
# Load with the raw process
149-
band_arr_raw = prod.load(
150-
first_band.value,
151-
window=Window(col_off=0, row_off=0, width=100, height=100),
152-
clean_optical="raw",
153-
)[first_band]
154-
155148
# Check that band loaded 2 times gives the same results (disregarding float uncertainties)
156149
band_arr1 = prod.load(
157150
first_band,
151+
pixel_size=pixel_size,
158152
window=Window(col_off=0, row_off=0, width=100, height=100),
159153
clean_optical="nodata",
160154
)[first_band]
161155
band_arr2 = prod.load(
162156
first_band,
157+
pixel_size=pixel_size,
163158
window=Window(col_off=0, row_off=0, width=100, height=100),
164159
)[first_band]
165-
166-
np.testing.assert_array_almost_equal(band_arr1, band_arr2)
167-
168-
# Check dtypes
169-
ci.assert_val(band_arr_raw.dtype, np.float32, "band_arr_raw dtype")
170160
ci.assert_val(band_arr1.dtype, np.float32, "band_arr1 dtype")
171161
ci.assert_val(band_arr2.dtype, np.float32, "band_arr2 dtype")
162+
ci.assert_val(band_arr1.shape, band_arr2.shape, "band_arr2 shape")
163+
164+
if prod.sensor_type == SensorType.OPTICAL:
165+
# Load with the raw process
166+
band_arr_raw = prod.load(
167+
first_band.value,
168+
pixel_size=pixel_size,
169+
window=Window(col_off=0, row_off=0, width=100, height=100),
170+
clean_optical="raw",
171+
)[first_band]
172+
173+
np.testing.assert_array_almost_equal(band_arr1, band_arr2)
174+
175+
# Check dtypes
176+
ci.assert_val(band_arr_raw.dtype, np.float32, "band_arr_raw dtype")
172177

173-
# Check shapes between raw and no data cleaning
174-
ci.assert_val(band_arr_raw.shape, band_arr1.shape, "band_arr1 shape")
178+
# Check shapes between raw and no data cleaning
179+
ci.assert_val(band_arr_raw.shape, band_arr1.shape, "band_arr_raw shape")
175180

176181

177182
def check_attrs(prod: Product, array: xr.DataArray, long_name) -> None:
@@ -367,7 +372,7 @@ def core(prod_path, possible_bands, debug, **kwargs):
367372
first_band = stack_bands[0]
368373

369374
# Check that band loaded 2 times gives the same results (disregarding float uncertainties)
370-
check_load(prod, first_band)
375+
check_load(prod, first_band, pixel_size)
371376

372377
# Check stack
373378
stack = test_satellites.check_stack(

eoreader/bands/band_names.py

+20
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,26 @@ def is_despeckle(cls, band: "SarBandNames"):
484484
"""
485485
return "DSPK" in band.name
486486

487+
@classmethod
488+
def is_speckle(cls, band: "SarBandNames"):
489+
"""
490+
Returns True if the band corresponds to a speckle one.
491+
492+
.. code-block:: python
493+
494+
>>> SarBandNames.is_despeckle(SarBandNames.VV)
495+
True
496+
>>> SarBandNames.is_despeckle(SarBandNames.VV_DSPK)
497+
False
498+
499+
Args:
500+
band (SarBandNames): Band to test
501+
502+
Returns:
503+
SarBandNames: Despeckled band
504+
"""
505+
return not cls.is_despeckle(band)
506+
487507
@classmethod
488508
def speckle_list(cls):
489509
return [band for band in cls if not cls.is_despeckle(band)]

eoreader/products/optical/optical_product.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -596,10 +596,10 @@ def _load_clouds(
596596
# First, try to open the cloud band written on disk
597597
bands_to_load = []
598598
for band in bands:
599-
cloud_path = self.get_band_path(
600-
band, pixel_size, size, writable=False, **kwargs
599+
cloud_path, cloud_exists = self._is_existing(
600+
self.get_band_file_name(band, pixel_size, size, **kwargs)
601601
)
602-
if cloud_path.is_file():
602+
if cloud_exists:
603603
band_dict[band] = utils.read(cloud_path)
604604
else:
605605
bands_to_load.append(band)

eoreader/products/optical/s3_slstr_product.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -632,14 +632,6 @@ def _preprocess(
632632
LOGGER.debug(f"Converting {band_str} to reflectance")
633633
band_arr = self._rad_2_refl(band_arr, band, suffix)
634634

635-
# Debug
636-
# utils.write(
637-
# band_arr,
638-
# self._get_band_folder(writable=True).joinpath(
639-
# f"{self.condensed_name}_{band.name}_rad2refl.tif"
640-
# ),
641-
# )
642-
643635
# Geocode
644636
LOGGER.debug(f"Geocoding {pp_name}")
645637
kwargs.pop("suffix", None)
@@ -675,7 +667,12 @@ def _get_suffix(self, band: Union[str, BandNames] = None, **kwargs) -> str:
675667
if band is not None:
676668
# Get the stripe
677669
if isinstance(band, BandNames):
678-
band = self.bands[band].id
670+
try:
671+
# Spectral bands
672+
band = self.bands[band].id
673+
except KeyError:
674+
# Cloud bands
675+
band = band.value
679676

680677
if band == F1.value:
681678
stripe = SlstrStripe.F if self._F1_is_f else SlstrStripe.I
@@ -1136,6 +1133,7 @@ def get_band_path(
11361133
self,
11371134
band: BandNames,
11381135
pixel_size: float = None,
1136+
size: Union[list, tuple] = None,
11391137
writable: bool = False,
11401138
**kwargs,
11411139
) -> AnyPathType:

eoreader/products/product.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ def get_band_file_name(
822822

823823
# Specific if needed
824824

825-
return f"{self.condensed_name}_{self.bands[band].id}_{res_str.replace('.', '-')}{win_suffix}{self._get_band_file_name_sensor_specific_suffix(band, **kwargs)}.tif"
825+
return f"{self.condensed_name}_{to_str(band, as_list=False)}_{res_str.replace('.', '-')}{win_suffix}{self._get_band_file_name_sensor_specific_suffix(band, **kwargs)}.tif"
826826

827827
def _get_band_file_name_sensor_specific_suffix(
828828
self, band: BandNames, **kwargs
@@ -1393,10 +1393,10 @@ def _load_spectral_indices(
13931393
"""
13941394
band_dict = {}
13951395
for idx in index_list:
1396-
idx_path = self.get_band_path(
1397-
idx, pixel_size, size, writable=False, **kwargs
1396+
idx_path, idx_exists = self._is_existing(
1397+
self.get_band_file_name(idx, pixel_size, size, **kwargs)
13981398
)
1399-
if idx_path.is_file():
1399+
if idx_exists:
14001400
band_dict[idx] = utils.read(idx_path)
14011401
else:
14021402
idx_arr = compute_index(index=idx, bands=loaded_bands, **kwargs).rename(
@@ -1405,9 +1405,6 @@ def _load_spectral_indices(
14051405
idx_arr.attrs["long_name"] = idx
14061406

14071407
# Write on disk
1408-
idx_path = self.get_band_path(
1409-
idx, pixel_size, size, writable=True, **kwargs
1410-
)
14111408
idx_arr = utils.write_path_in_attrs(idx_arr, idx_path)
14121409
utils.write(idx_arr, idx_path)
14131410
band_dict[idx] = idx_arr

eoreader/products/sar/cosmo_product.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,9 @@ def get_orbit_direction(self) -> OrbitDirection:
479479

480480
return od
481481

482-
def _pre_process_sar(self, band, pixel_size: float = None, **kwargs) -> AnyPathType:
482+
def _pre_process_sar(
483+
self, pre_processed_path, band, pixel_size: float = None, **kwargs
484+
) -> AnyPathType:
483485
"""
484486
Pre-process SAR data (geocoding...)
485487
@@ -517,11 +519,11 @@ def _pre_process_sar(self, band, pixel_size: float = None, **kwargs) -> AnyPathT
517519
)
518520
return ortho_path
519521
elif misc.compare_version(self.get_snap_version(), "11.0.0", ">="):
520-
return super()._pre_process_sar(band, pixel_size=pixel_size, **kwargs)
522+
return super()._pre_process_sar(pre_processed_path, band, **kwargs)
521523
else:
522524
with h5netcdf.File(self._img_path, phony_dims="access") as raw_h5:
523525
if self.nof_swaths == 1:
524-
return super()._pre_process_sar(band, pixel_size, **kwargs)
526+
return super()._pre_process_sar(pre_processed_path, band, **kwargs)
525527
else:
526528
LOGGER.warning(
527529
"SNAP (before version 11.0.0) doesn't handle multiswath Cosmo-SkyMed products. This is a workaround. See https://github.com/sertit/eoreader/issues/78"
@@ -581,8 +583,8 @@ def _pre_process_sar(self, band, pixel_size: float = None, **kwargs) -> AnyPathT
581583
# Pre-process swath
582584
pp_swath_path.append(
583585
super()._pre_process_sar(
586+
pre_processed_path,
584587
band,
585-
pixel_size,
586588
prod_path=prod_path,
587589
suffix=group,
588590
**kwargs,

0 commit comments

Comments
 (0)