From 867f1d1e9a408f985cf3b114fe39ae7eb8ac9994 Mon Sep 17 00:00:00 2001 From: BRAUN REMI Date: Wed, 8 Nov 2023 13:30:49 +0100 Subject: [PATCH] FIX: Fix an `xarray` issue when trying to compute percentiles when stacking bands --- CHANGES.md | 1 + eoreader/utils.py | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index 95ffda66..2aca3557 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ - **ENH: Manage Sentinel-2 (currently L2A) as formatted on the cloud (Element84's way). ([#104](https://github.com/sertit/eoreader/issues/104)** - **ENH: Handle Python 3.12. ([#113](https://github.com/sertit/eoreader/issues/113)** - FIX: Fix jpg, png... quicklooks management when plotting +- FIX: Fix an `xarray` issue when trying to compute percentiles when stacking bands - CI: Update pre-commit hooks - CI: Revamping `test_satellites` - DEPS: Remove as many mention as possible to `cloudpathlib` diff --git a/eoreader/utils.py b/eoreader/utils.py index 12b50695..051e5c17 100644 --- a/eoreader/utils.py +++ b/eoreader/utils.py @@ -410,7 +410,11 @@ def stack( scale = 10000 round_nb = 1000 round_min = -0.1 - stack_min = float(band_xds.to_array().quantile(0.001)) + try: + stack_min = float(band_xds.to_array().quantile(0.001)) + except ValueError: + stack_min = np.nanpercentile(band_xds.to_array(), 1) + if np.round(stack_min * round_nb) / round_nb < round_min: LOGGER.warning( f"Cannot convert the stack to uint16 as it has negative values ({stack_min} < {round_min}). Keeping it in float32."