where#

where(cond, if_true, if_false, master_xda: Optional[xarray.core.dataarray.DataArray] = None, new_name: str = '') xarray.core.dataarray.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 like xr.where. Else, it outputs a xarray.DataArray with the same dtype than master_xda.

Warning

If you don’t give a master_xda, it is better to pass numpy arrays to if_false and if_true keywords as passing xarrays interfers with the output metadata (you may lose the CRS and so on). Just pass if_true=true_xda.data inplace of if_true=true_xda and the same for if_false

>>> 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
Parameters
  • cond (scalar, array, Variable, DataArray or Dataset) – Conditional array

  • if_true (scalar, array, Variable, DataArray or Dataset) – What to do if cond is True

  • if_false (scalar, array, Variable, DataArray or Dataset) – What to do if cond is False

  • master_xda – Master xr.DataArray used to set the metadata and the nodata

  • new_name (str) – New name of the array

Returns

Where array with correct mtd and nodata pixels

Return type

xr.DataArray