From 948e51d6f133fbf6a10d016639dd5a08c7beec02 Mon Sep 17 00:00:00 2001 From: Gabe Joseph Date: Thu, 20 Jan 2022 16:11:15 -0700 Subject: [PATCH] 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. --- stackstac/ops.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/stackstac/ops.py b/stackstac/ops.py index 924d585..a8570ed 100644 --- a/stackstac/ops.py +++ b/stackstac/ops.py @@ -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,