Skip to content

Commit

Permalink
FIX: Use already computed bands stored in tmp for Planet products
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-braun committed Jun 21, 2023
1 parent 9bc0f41 commit 8f1fa49
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 118 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Release History

## 0.20.2 (2023-06-20)

### Bug Fixes

- FIX: Use already computed bands stored in `tmp` for Planet products

## 0.20.1 (2023-06-20)

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion eoreader/__meta__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"""
**EOReader** library
"""
__version__ = "0.20.1"
__version__ = "0.20.2.dev0"
__title__ = "eoreader"
__description__ = (
"Remote-sensing opensource python library reading optical and SAR constellations, "
Expand Down
35 changes: 0 additions & 35 deletions eoreader/products/optical/pla_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,41 +467,6 @@ def _get_stack_path(self, as_list: bool = False) -> Union[str, list]:

return stack_path

def get_band_paths(
self, band_list: list, pixel_size: float = None, **kwargs
) -> dict:
"""
Return the paths of required bands.
.. code-block:: python
>>> from eoreader.reader import Reader
>>> from eoreader.bands import *
>>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
>>> prod = Reader().open(path)
>>> prod.get_band_paths([GREEN, RED])
{
<SpectralBandNames.GREEN: 'GREEN'>:
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
<SpectralBandNames.RED: 'RED'>:
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
}
Args:
band_list (list): List of the wanted bands
pixel_size (float): Band pixel size
kwargs: Other arguments used to load bands
Returns:
dict: Dictionary containing the path of each queried band
"""
band_paths = {}
path = self._get_stack_path(as_list=False)
for band in band_list:
band_paths[band] = path

return band_paths

def _to_reflectance(
self,
band_arr: xr.DataArray,
Expand Down
84 changes: 73 additions & 11 deletions eoreader/products/optical/planet_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import geopandas as gpd
import numpy as np
import rasterio
import xarray as xr
from cloudpathlib import CloudPath
from lxml import etree
Expand Down Expand Up @@ -200,7 +201,9 @@ def _post_init(self, **kwargs) -> None:
(setting sensor type, band names and so on)
"""
# Manage Raw unit
band_name = files.get_filename(self.get_default_band_path()).upper().split("_")
band_name = (
files.get_filename(self._get_stack_path(as_list=False)).upper().split("_")
)
if "SR" in band_name:
self._raw_units = RawUnits.REFL
elif "DN" in band_name:
Expand Down Expand Up @@ -389,6 +392,48 @@ def footprint(self) -> gpd.GeoDataFrame:

return gpd.GeoDataFrame(geometry=footprint.geometry, crs=footprint.crs)

def get_band_paths(
self, band_list: list, pixel_size: float = None, **kwargs
) -> dict:
"""
Return the paths of required bands.
.. code-block:: python
>>> from eoreader.reader import Reader
>>> from eoreader.bands import *
>>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
>>> prod = Reader().open(path)
>>> prod.get_band_paths([GREEN, RED])
{
<SpectralBandNames.GREEN: 'GREEN'>:
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
<SpectralBandNames.RED: 'RED'>:
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
}
Args:
band_list (list): List of the wanted bands
pixel_size (float): Band pixel size
kwargs: Other arguments used to load bands
Returns:
dict: Dictionary containing the path of each queried band
"""
band_paths = {}
path = self._get_stack_path(as_list=False)
for band in band_list:
# Get clean band path
clean_band = self._get_clean_band_path(
band, pixel_size=pixel_size, **kwargs
)
if clean_band.is_file():
band_paths[band] = clean_band
else:
band_paths[band] = path

return band_paths

def _read_band(
self,
path: Union[CloudPath, Path],
Expand All @@ -413,15 +458,32 @@ def _read_band(
xr.DataArray: Band xarray
"""
# Read band
band_arr = utils.read(
path,
pixel_size=pixel_size,
size=size,
resampling=Resampling.bilinear,
indexes=[self.bands[band].id],
**kwargs,
)
with rasterio.open(str(path)) as dst:
# Manage the case if we open a simple band (EOReader processed bands)
if dst.count == 1:
# Read band
band_arr = utils.read(
path,
pixel_size=pixel_size,
size=size,
resampling=Resampling.bilinear,
**kwargs,
)

# Manage the case if we open a stack (native DIMAP bands)
else:
band_arr = utils.read(
path,
pixel_size=pixel_size,
size=size,
resampling=Resampling.bilinear,
indexes=[self.bands[band].id],
**kwargs,
)

# Pop useless long name
if "long_name" in band_arr.attrs:
band_arr.attrs.pop("long_name")

# To float32
if band_arr.dtype != np.float32:
Expand Down Expand Up @@ -526,7 +588,7 @@ def _load_bands(
return {}

# Get band paths
band_paths = self.get_band_paths(bands, **kwargs)
band_paths = self.get_band_paths(bands, pixel_size, **kwargs)

# Open bands and get array (resampled if needed)
band_arrays = self._open_bands(
Expand Down
36 changes: 0 additions & 36 deletions eoreader/products/optical/re_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,42 +261,6 @@ def _get_stack_path(self, as_list: bool = False) -> Union[str, list]:

return stack_path

def get_band_paths(
self, band_list: list, pixel_size: float = None, **kwargs
) -> dict:
"""
Return the paths of required bands.
.. code-block:: python
>>> from eoreader.reader import Reader
>>> from eoreader.bands import *
>>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
>>> prod = Reader().open(path)
>>> prod.get_band_paths([GREEN, RED])
{
<SpectralBandNames.GREEN: 'GREEN'>:
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
<SpectralBandNames.RED: 'RED'>:
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
}
Args:
band_list (list): List of the wanted bands
pixel_size (float): Band pixel size
kwargs: Other arguments used to load bands
Returns:
dict: Dictionary containing the path of each queried band
"""
band_paths = {}
path = self._get_stack_path(as_list=False)

for band in band_list:
band_paths[band] = path

return band_paths

def _dn_to_toa_rad(self, dn_arr: xr.DataArray, band: BandNames) -> xr.DataArray:
"""
Compute DN to TOA radiance
Expand Down
35 changes: 0 additions & 35 deletions eoreader/products/optical/sky_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,41 +375,6 @@ def _get_stack_path(self, as_list: bool = False) -> Union[str, list]:

return stack_path

def get_band_paths(
self, band_list: list, pixel_size: float = None, **kwargs
) -> dict:
"""
Return the paths of required bands.
.. code-block:: python
>>> from eoreader.reader import Reader
>>> from eoreader.bands import *
>>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
>>> prod = Reader().open(path)
>>> prod.get_band_paths([GREEN, RED])
{
<SpectralBandNames.GREEN: 'GREEN'>:
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
<SpectralBandNames.RED: 'RED'>:
'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
}
Args:
band_list (list): List of the wanted bands
pixel_size (float): Band pixel size
kwargs: Other arguments used to load bands
Returns:
dict: Dictionary containing the path of each queried band
"""
band_paths = {}
path = self._get_stack_path(as_list=False)
for band in band_list:
band_paths[band] = path

return band_paths

def _to_reflectance(
self,
band_arr: xr.DataArray,
Expand Down

0 comments on commit 8f1fa49

Please sign in to comment.