![](/img/trans.png)
[英]Masking using Xarray.where throws an error "dimension 'y' are not equal"
[英]xarray.where() with multiple conditions
我有一個具有土地覆蓋類型的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種土地覆被進行繁瑣的工作。 有沒有更好的辦法?
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.