Skip to content

Commit

Permalink
Catch some mosaic usage errors with nodata=nan (#123)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gjoseph92 authored Jan 20, 2022
1 parent e21f746 commit 948e51d
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions stackstac/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,21 @@ def mosaic(
nodata:
The value to treat as invalid. Default: NaN.
To catch common mis-use, raises a ``ValueError`` if ``nodata=nan``
is used when the array has an integer or boolean dtype. Since NaN
cannot exist in those arrays, this indicates a different ``nodata``
value needs to be used.
Returns
-------
xarray.DataArray:
The mosaicked `~xarray.DataArray`.
"""
if np.isnan(nodata) and arr.dtype.kind in "biu":
# Try to catch usage errors forgetting to set `nodata=`
raise ValueError(
f"Cannot use {nodata=} when mosaicing a {arr.dtype} array, since {nodata} cannot exist in the array."
)
return arr.reduce(
_mosaic,
dim=dim,
Expand Down

0 comments on commit 948e51d

Please sign in to comment.