使用 Python xarray 使用 'where' 进行屏蔽不再有效

Masking with 'where' using Python xarray is not working anymore

我在更新 xarray 后遇到了麻烦 运行。

我有一个盐度数据集,其尺寸为:

(u'time', u'stations', u'layer')

对于特定的站点,我想获得所选深度的盐度,比如 -10 米。维度图层包含随时间变化的深度值,即给定图层包含随时间变化的不同深度值(由于水位变化)。

之前我在做以下事情:

ds.salinity.sel(stations='mystation').where( np.ceil(ds.layer(stations='mystation')) ==-10 )

工作正常。

但现在我收到这个错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-27-7e83f5e839cc> in <module>()
      1 fmstation = 'T1_50'
      2 
----> 3 sal10m = ds.salinity.sel(stations = ds.stations[ds.station_name == fmstation]).where(np.ceil(ds.Zcoordinate_c.isel(stations = ds.stations[ds.station_name == fmstation])) == -10) #.mean(axis=2).plot()
      4 sal10m #.plot()

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\xarray\core\common.pyc in where(self, cond)
    481           * x        (x) int64 0 1 2 3 4
    482         """
--> 483         return self._where(cond)
    484 
    485     # this has no runtime function - these are listed so IDEs know these methods

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\xarray\core\dataarray.pyc in func(self, other)
   1193                         if not reflexive
   1194                         else f(other_variable, self.variable))
-> 1195             coords = self.coords.merge(other_coords)._variables
   1196             name = self._result_name(other)
   1197 

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\xarray\core\coordinates.pyc in merge(self, other)
    139         ds = self.to_dataset()
    140         if other is not None:
--> 141             conflicts = self._merge_validate(other)
    142             _coord_merge_finalize(ds.coords, other, *conflicts)
    143         return ds

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\xarray\core\coordinates.pyc in _merge_validate(self, other)
     89                 self_var = self._variables[k]
     90                 other_var = other[k].variable
---> 91                 if not self_var.broadcast_equals(other_var):
     92                     if k in self.dims and k in other.dims:
     93                         raise ValueError('index %r not aligned' % k)

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\xarray\core\variable.pyc in broadcast_equals(self, other)
    976         except (ValueError, AttributeError):
    977             return False
--> 978         return self.equals(other)
    979 
    980     def identical(self, other):

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\xarray\core\variable.pyc in equals(self, other)
    961         other = getattr(other, 'variable', other)
    962         try:
--> 963             return (self.dims == other.dims and self._data_equals(other))
    964         except (TypeError, AttributeError):
    965             return False

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\xarray\core\variable.pyc in _data_equals(self, other)
    947     def _data_equals(self, other):
    948         return (self._data is other._data or
--> 949                 ops.array_equiv(self.data, other.data))
    950 
    951     def equals(self, other):

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\xarray\core\ops.pyc in array_equiv(arr1, arr2)
    188     if arr1.shape != arr2.shape:
    189         return False
--> 190     return bool(((arr1 == arr2) | (isnull(arr1) & isnull(arr2))).all())
    191 
    192 

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\dask\array\core.pyc in all(self, axis, keepdims, split_every)
   1146     def all(self, axis=None, keepdims=False, split_every=None):
   1147         from .reductions import all
-> 1148         return all(self, axis=axis, keepdims=keepdims, split_every=split_every)
   1149 
   1150     @wraps(np.min)

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\dask\array\reductions.pyc in all(a, axis, keepdims, split_every)
    161 def all(a, axis=None, keepdims=False, split_every=None):
    162     return reduction(a, chunk.all, chunk.all, axis=axis, keepdims=keepdims,
--> 163                      dtype='bool', split_every=split_every)
    164 
    165 

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\dask\array\reductions.pyc in reduction(x, chunk, aggregate, axis, keepdims, dtype, split_every, combine, name)
     42 
     43     return _tree_reduce(tmp, aggregate, axis, keepdims, dtype, split_every,
---> 44                        combine, name=name)
     45 
     46 

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\dask\array\reductions.pyc in _tree_reduce(x, aggregate, axis, keepdims, dtype, split_every, combine, name)
     75     return partial_reduce(func, x, split_every, keepdims=keepdims,
     76                           dtype=dtype,
---> 77                           name=(name or funcname(aggregate)) + '-aggregate')
     78 
     79 

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\dask\array\reductions.pyc in partial_reduce(func, x, split_every, keepdims, dtype, name)
    105         out_axis = [i for i in range(x.ndim) if i not in split_every]
    106         getter = lambda k: get(out_axis, k)
--> 107         keys = map(getter, keys)
    108         out_chunks = list(getter(out_chunks))
    109     dsk = {}

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\dask\array\reductions.pyc in <lambda>(k)
    104     if not keepdims:
    105         out_axis = [i for i in range(x.ndim) if i not in split_every]
--> 106         getter = lambda k: get(out_axis, k)
    107         keys = map(getter, keys)
    108         out_chunks = list(getter(out_chunks))

C:\Users\Smeirellesnune\AppData\Local\Continuum\Miniconda\lib\site-packages\toolz\itertoolz.pyc in get(ind, seq, default)
    374                     return operator.itemgetter(*ind)(seq)
    375                 else:
--> 376                     return (seq[ind[0]],)
    377             else:
    378                 return tuple(_get(i, seq, default) for i in ind)

IndexError: list index out of range

有人知道这是怎么回事吗?

或者是否有更智能的方法来切分我的数据集?

我使用的xarray版本是:

xarray 0.7.2

sys.version:

'2.7.11 |Anaconda 2.1.0 (64-bit)| (default, Feb 16 2016, 09:58:36) [MSC v.1500 64 bit (AMD64)]'

编辑:添加数据集详细信息

<xarray.Dataset>
Dimensions:                              (cross_section: 15, cross_section_pts: 3, laydim: 20, laydimw: 21, stations: 979, time: 2512)
Coordinates:
    station_x_coordinate                 (stations) float64 4.194 4.008 ...
    station_y_coordinate                 (stations) float64 52.07 51.99 52.0 ...
    station_name                         (stations) |S64 'zmBoei_wav' ...
    Zcoordinate_c                        (time, stations, laydim) float64 -10.98 ...
    Zcoordinate_w                        (time, stations, laydimw) float64 -11.28 ...
    cross_section_name                   (cross_section) |S64 'L1' 'L2' 'L3' ...
  * time                                 (time) datetime64[ns] 2014-10-02T06:00:00 ...
  * cross_section                        (cross_section) int64 0 1 2 3 4 5 6 ...
  * cross_section_pts                    (cross_section_pts) int64 0 1 2
  * laydim                               (laydim) int64 0 1 2 3 4 5 6 7 8 9 ...
  * laydimw                              (laydimw) int64 0 1 2 3 4 5 6 7 8 9 ...
  * stations                             (stations) int64 0 1 2 3 4 5 6 7 8 ...
Data variables:
    station_id                           (stations) |S64 'zmBoei_wav' ...
    waterlevel                           (time, stations) float64 0.6178 ...
    Waterdepth                           (time, stations) float64 11.9 20.36 ...
    x_velocity                           (time, stations, laydim) float64 0.1941 ...
    y_velocity                           (time, stations, laydim) float64 0.0954 ...
    z_velocity                           (time, stations, laydim) float64 0.000392 ...
    Tke                                  (time, stations, laydimw) float64 1e-32 ...
    Eps                                  (time, stations, laydimw) float64 1e-32 ...
    Vicww                                (time, stations, laydimw) float64 0.0 ...
    salinity                             (time, stations, laydim) float64 26.26 ...
    cross_section_x_coordinate           (cross_section, cross_section_pts) float64 4.206 ...
    cross_section_y_coordinate           (cross_section, cross_section_pts) float64 51.94 ...
    cross_section_discharge              (time, cross_section) float64 0.0 ...
    cross_section_area                   (time, cross_section) float64 8.762e+03 ...
    cross_section_velocity               (time, cross_section) float64 0.0 ...
    cross_section_salt                   (time, cross_section) float64 -1.142e+05 ...
    WaterBalance_total_volume            (time) float64 0.0 1.227e+11 ...
    WaterBalance_storage                 (time) float64 0.0 1.227e+11 ...
    WaterBalance_volume_error            (time) float64 0.0 -1.499e+07 ...
    WaterBalance_boundaries_in           (time) float64 0.0 3.827e+08 ...
    WaterBalance_boundaries_out          (time) float64 0.0 6.048e+07 ...
    WaterBalance_boundaries_total        (time) float64 0.0 3.222e+08 ...
    WaterBalance_exchange_with_1D_in     (time) float64 0.0 0.0 0.0 0.0 0.0 ...
    WaterBalance_exchange_with_1D_out    (time) float64 0.0 0.0 0.0 0.0 0.0 ...
    WaterBalance_exchange_with_1D_total  (time) float64 0.0 0.0 0.0 0.0 0.0 ...
    WaterBalance_precipitation           (time) float64 0.0 0.0 0.0 0.0 0.0 ...
    WaterBalance_source_sink             (time) float64 0.0 0.0 0.0 0.0 0.0 ...
Attributes:
    institution: Deltares
    references: http://www.deltares.nl
    source: Deltares, D-Flow FM Version 1.1.182.45395, Apr 07 2016, 10:16:58, model
    history: Created on 2016-05-23T21:50:20+0200, D-Flow FM
    date_created: 2016-05-23T21:50:20+0200
    date_modified: 2016-05-23T21:50:20+0200
    Conventions: CF-1.5 Deltares-0.1

从 IOOS 频道安装软件包和 dask 解决了这个问题。要设置 IOOS Python 环境,请按照以下步骤操作:

https://github.com/ioos/conda-recipes/wiki/Setting-up-the-IOOS-Python-environment

正如@Stephan 指出的那样:通用 "conda-forge" 渠道现在应该比 IOOS 更受青睐。 了解更多信息:

https://conda-forge.github.io/

其次,安装dask如下:

conda install dask -c conda-forge

开机前激活IOOS Python:

source activate IOOS #(OSX and Linux)
activate IOOS #(Windows)