我有一个带有landcover类型的dataArray。我想掩盖列表中的某些值。是否可以在多个条件下使用xr.where()函数?
import numpy as np
import xarray as xr
a = xr.DataArray(np.arange(25).reshape(5, 5), dims=('x', 'y'))
print a
LC = [10,12,19]
a.where((a == LC[0]) | (a == LC[1]))
给出了:
<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
上述作品适用于两种土地覆盖价值,但对于30种类型而言,这样做很繁琐。还有更好的方法吗?
答案 0 :(得分:5)
xr.DataArray(np.in1d(a, LC).reshape(a.shape),
dims=a.dims, coords=a.coords)
应该这样做:
<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