Skip to content

Commit

Permalink
rearrange the Array to process the given rgb array by `scale_percen…
Browse files Browse the repository at this point in the history
…tile` instead of the surface reflectance function
  • Loading branch information
MAfarrag committed Jul 19, 2024
1 parent b012e79 commit db1fc4a
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions cleopatra/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ def __init__(
exclude_value: List = np.nan,
extent: List = None,
rgb: List[int] = None,
surface_reflectance: int = 10000,
surface_reflectance: int = None,
cutoff: List = None,
ax: Axes = None,
fig: Figure = None,
percentile: int = 1,
**kwargs,
):
"""Array.
Expand All @@ -62,6 +63,8 @@ def __init__(
cutoff: List, Default is None.
clip the range of pixel values for each band. (take only the pixel values from 0 to the value of the cutoff
and scale them back to between 0 and 1.
percentile: int
The percentile value to be used for scaling.
the object does not need any parameters to be initialized.
Expand Down Expand Up @@ -105,11 +108,12 @@ def __init__(
f"{array.shape[0]}"
)
else:
array = self._prepare_sentinel_rgb(
array = self.prepare_array(
array,
rgb=rgb,
surface_reflectance=surface_reflectance,
cutoff=cutoff,
percentile=percentile,
)
else:
self.rgb = False
Expand Down Expand Up @@ -141,14 +145,15 @@ def __init__(
else:
self.fig, self.ax = fig, ax

def _prepare_sentinel_rgb(
def prepare_array(
self,
array: np.ndarray,
rgb: List[int] = None,
surface_reflectance: int = 10000,
surface_reflectance: int = None,
cutoff: List = None,
percentile: int = 1,
) -> np.ndarray:
"""Prepare for RGB plot.
"""Prepare Array.
Parameters
----------
Expand All @@ -161,6 +166,8 @@ def _prepare_sentinel_rgb(
cutoff: List, Default is None.
clip the range of pixel values for each band. (take only the pixel values from 0 to the value of the cutoff
and scale them back to between 0 and 1).
percentile: int
The percentile value to be used for scaling.
Returns
-------
Expand All @@ -170,6 +177,44 @@ def _prepare_sentinel_rgb(
# take the rgb arrays and reorder them to have the red-green-blue, if the order is not given, assume the
# order as sentinel data. [3, 2, 1]
array = array[rgb].transpose(1, 2, 0)

if surface_reflectance is None:
array = self.scale_percentile(array, percentile=percentile)
else:
array = self._prepare_sentinel_rgb(
array,
rgb=rgb,
surface_reflectance=surface_reflectance,
cutoff=cutoff,
)
return array

def _prepare_sentinel_rgb(
self,
array: np.ndarray,
rgb: List[int] = None,
surface_reflectance: int = 10000,
cutoff: List = None,
) -> np.ndarray:
"""Prepare for RGB plot.
Parameters
----------
array: np.ndarray
array.
rgb: List, Default is [3,2,1]
the indices of the red, green, and blue bands in the given array.
surface_reflectance: int, Default is 10000.
surface reflectance value of the sentinel data.
cutoff: List, Default is None.
clip the range of pixel values for each band. (take only the pixel values from 0 to the value of the cutoff
and scale them back to between 0 and 1).
Returns
-------
np.ndarray:
the rgb 3d array is converted into 2d array to be plotted using the plt.imshow function.
"""
array = np.clip(array / surface_reflectance, 0, 1)
if cutoff is not None:
array[0] = np.clip(rgb[0], 0, cutoff[0]) / cutoff[0]
Expand Down

0 comments on commit db1fc4a

Please sign in to comment.