6

我有一个带有土地覆盖类型的 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 种类型执行此操作很乏味。有没有更好的办法?

4

1 回答 1

6
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
于 2016-11-11T21:26:50.097 回答