where#
- where(cond, if_true, if_false, master_xda: DataArray | None = None, new_name: str = '') DataArray [source]#
Overloads
xr.where
with:setting metadata of
master_xda
preserving the nodata pixels of the
master_xda
If
master_xda
is None, use it likexr.where
. Else, it outputs axarray.DataArray
with the same dtype thanmaster_xda
.Warning
If you don’t give a
master_xda
, it is better to pass numpy arrays toif_false
andif_true
keywords as passing xarrays interfers with the output metadata (you may lose the CRS and so on). Just passif_true=true_xda.data
inplace ofif_true=true_xda
and the same forif_false
- Parameters:
cond (scalar, array, Variable, DataArray or Dataset) – Conditional array
if_true (scalar, array, Variable, DataArray or Dataset) – What to do if
cond
is Trueif_false (scalar, array, Variable, DataArray or Dataset) – What to do if
cond
is Falsemaster_xda – Master
xr.DataArray
used to set the metadata and the nodatanew_name (str) – New name of the array
- Returns:
Where array with correct mtd and nodata pixels
- Return type:
xr.DataArray
Example
>>> A = xr.DataArray(dims=("x", "y"), data=[[1, 0, 5], [np.nan, 0, 0]]) >>> mask_A = rasters.where(A > 3, 0, 1, A, new_name="mask_A") <xarray.DataArray 'mask_A' (x: 2, y: 3)> array([[ 1., 1., 0.], [nan, 1., 1.]]) Dimensions without coordinates: x, y