python - xarray.where() with multiple conditions -
i have dataarray landcover types. mask out values have in list. possible use xr.where() function multiple conditions?
import numpy np import xarray xr = xr.dataarray(np.arange(25).reshape(5, 5), dims=('x', 'y')) print lc = [10,12,19] a.where((a == lc[0]) | (a == lc[1])) which gives:
<xarray.dataarray (x: 5, y: 5)> array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14], [15, 16, 17, 18, 19], [20, 21, 22, 23, 24]]) coordinates: * x (x) int64 0 1 2 3 4 * y (y) int64 0 1 2 3 4 <xarray.dataarray (x: 5, y: 5)> array([[ nan, nan, nan, nan, nan], [ nan, nan, nan, nan, nan], [ 10., nan, 12., nan, nan], [ nan, nan, nan, nan, nan], [ nan, nan, nan, nan, nan]]) coordinates: * x (x) int64 0 1 2 3 4 * y (y) int64 0 1 2 3 4 the above works 2 landcover values, tedious 30 types. there better way?
xr.dataarray(np.in1d(a, lc).reshape(a.shape), dims=a.dims, coords=a.coords) should it:
<xarray.dataarray (x: 5, y: 5)> array([[false, false, false, false, false], [false, false, false, false, false], [ true, false, true, false, false], [false, false, false, false, true], [false, false, false, false, false]], dtype=bool) coordinates: * x (x) int64 0 1 2 3 4 * y (y) int64 0 1 2 3 4
Comments
Post a Comment