【问题标题】:python-xarray copy nan from one DataArray to anotherpython-xarray 将 nan 从一个 DataArray 复制到另一个
【发布时间】:2017-09-15 01:41:18
【问题描述】:

对我的上一个问题稍作修改 python-xarray copy mask from one DataArray to another

当我读入另一个 Hs 文件时,它会选择属性 valid_min: 0,因此会自动使用 nan 读入该文件

f = xr.open_dataset('ww3.Hs.mask.nc')
<xarray.DataArray 'hs' (time: 1, latitude: 81, longitude: 131)>
array([[[   nan,    nan, ...,    nan,    nan],
    [   nan,    nan, ...,    nan,    nan],
    ..., 
    [   nan,  0.   , ...,  0.422,    nan],
    [   nan,    nan, ...,    nan,    nan]]])
Coordinates:
* latitude   (latitude) float32 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ...
* longitude  (longitude) float32 -100.0 -99.0 -98.0 -97.0 -96.0 -95.0 ...
* time       (time) datetime64[ns] 1983-01-30
Attributes:
long_name:      significant height of wind and swell waves
standard_name:  sea_surface_wave_significant_height
globwave_name:  significant_wave_height
units:          m
valid_min:      0
valid_max:      32000

我现在想知道如何为另一个变量选择这个掩码。我对语法的猜测是:

ws10_masked = ws10.where(hs == nan)
NameError: name 'nan' is not defined

ws10_masked = ws10.where(hs.isnull)
# Does not give an error message but does not set any values in ws10 to nan

ws10_masked = ws10.where(hs.isnull())
Traceback (most recent call last):
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-
packages/xarray/backends/netCDF4_.py", line 60, in __getitem__
data = getitem(self.get_array(), key)
File "netCDF4/_netCDF4.pyx", line 3730, in 
netCDF4._netCDF4.Variable.__getitem__ (netCDF4/_netCDF4.c:39703)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-
packages/netCDF4/utils.py", line 391, in _out_array_shape
c = count[..., i].ravel()[0] # All elements should be identical.
IndexError: index 0 is out of bounds for axis 0 with size 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "paper_test.py", line 62, in <module>
ws10_masked = ws10.where(hs.isnull())
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/common.py", line 637, in where
out = outobj._where(outcond)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-
packages/xarray/core/dataarray.py", line 1541, in func
if not reflexive
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/ops.py", line 191, in func
return getattr(self, name)(*args, **kwargs)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/variable.py", line 873, in where
return self._where(cond)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/variable.py", line 1141, in func
self_data, other_data, dims = _broadcast_compat_data(self, other)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/variable.py", line 1376, in _broadcast_compat_data
self_data = new_self.data
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/variable.py", line 267, in data
return self.values
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/variable.py", line 308, in values
return _as_array_or_item(self._data)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/variable.py", line 184, in _as_array_or_item
data = np.asarray(data)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/numpy/core/numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/indexing.py", line 419, in __array__
self._ensure_cached()
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/indexing.py", line 416, in _ensure_cached
self.array = np.asarray(self.array)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/numpy/core/numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/indexing.py", line 400, in __array__
return np.asarray(self.array, dtype=dtype)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/numpy/core/numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/indexing.py", line 375, in __array__
return np.asarray(array[self.key], dtype=None)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/conventions.py", line 363, in __getitem__
self.scale_factor, self.add_offset, self._dtype)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/conventions.py", line 59, in mask_and_scale
values = np.array(array, dtype=dtype, copy=True)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/core/indexing.py", line 375, in __array__
return np.asarray(array[self.key], dtype=None)
File "/nethome/rxb826/PYTHON3/myenv/lib/python3.6/site-packages/xarray/backends/netCDF4_.py", line 71, in __getitem__
raise IndexError(msg)
IndexError: The indexing operation you are attempting to perform is not valid on netCDF4.Variable object. Try loading your data into memory first by calling .load().

干杯, 雷

【问题讨论】:

    标签: python-xarray


    【解决方案1】:

    DataArray.isnull()是方法,不是属性,所以需要调用:

    ws10_masked = ws10.where(hs.isnull())
    

    【讨论】:

    • 遇到一个复杂的错误并在上面添加。可能与我正在使用的 python 3.6 版有关。很高兴详细说明
    • 你能在 xarray 的 GitHub 页面上打开一个问题吗?一个指向我们可以用来重现它的文件的链接也会很有帮助。
    • @shoyer 在尝试对尚未从 netcdf 加载的数据进行布尔索引时,我一直收到此错误。这不是预期的行为吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-26
    • 2021-03-25
    • 2016-12-14
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 2011-11-23
    相关资源
    最近更新 更多