Skip to content

Commit 948e51d

Browse files
authored
Catch some mosaic usage errors with nodata=nan (#123)
If you've specified `dtype=int` to `stackstac.stack`, this is an easy way to catch that you need to use a custom nodata value.
1 parent e21f746 commit 948e51d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

stackstac/ops.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,21 @@ def mosaic(
4545
nodata:
4646
The value to treat as invalid. Default: NaN.
4747
48+
To catch common mis-use, raises a ``ValueError`` if ``nodata=nan``
49+
is used when the array has an integer or boolean dtype. Since NaN
50+
cannot exist in those arrays, this indicates a different ``nodata``
51+
value needs to be used.
52+
4853
Returns
4954
-------
5055
xarray.DataArray:
5156
The mosaicked `~xarray.DataArray`.
5257
"""
58+
if np.isnan(nodata) and arr.dtype.kind in "biu":
59+
# Try to catch usage errors forgetting to set `nodata=`
60+
raise ValueError(
61+
f"Cannot use {nodata=} when mosaicing a {arr.dtype} array, since {nodata} cannot exist in the array."
62+
)
5363
return arr.reduce(
5464
_mosaic,
5565
dim=dim,

0 commit comments

Comments
 (0)