python-xarray从一个DataArray复制掩码到另一个 [英] python-xarray copy mask from one DataArray to another
问题描述
我得到了一个简单的案例:
I got this to work for a simple case:
arr2 = xr.DataArray((np.arange(16)-8).reshape(4, 4), dims=['x', 'y'])
arr3 = xr.DataArray(np.arange(16).reshape(4, 4), dims=['x', 'y'])
<xarray.DataArray (x: 4, y: 4)>
array([[ nan, nan, nan, nan],
[ nan, nan, nan, nan],
[ nan, 9., 10., 11.],
[ 12., 13., 14., 15.]])
Dimensions without coordinates: x, y
但是,我在申请NetCDF文件时遇到了麻烦. 我有两个数据集:重要的波高(Hs)和风速(ws). 我想使用Hs <0的掩码并将其应用于ws. 数据集的大小为[time = 1,lat = 81,lon = 131].在未来的某个时候,我的ws DataArray的大小会略有不同,例如[time = 1,ens = 10,lat = 81,lon = 131].
However, i'm having troubling applying to NetCDF files. I have two datasets: significant wave height (Hs) and wind speed (ws). I would like to use the mask of where Hs<0 and apply it to ws. The size of the datasets are [time=1,lat=81,lon=131]. There will be a time in the futures where my ws DataArray will be a slightly different size e.g. [time=1,ens=10,lat=81,lon=131].
如果我尝试:
f = xr.open_dataset('CCSM4_ens1_19821201_19831130_ws10_0_NAtl_DJFmean.nc')
ws10 = f.ws10
f = xr.open_dataset('ww3.Hs.19820901_19830831_NAtl_DJFmean.nc')
hs = f.hs
ws10_masked = ws10.where(hs > 0)
ws10_masked看起来像:
ws10_masked looks like:
xarray.DataArray (time: 1, lat: 81, lon: 131, latitude: 81, longitude: 131)
array([[[[[ nan, ..., nan],
...,
[ nan, ..., nan]],
...,
[[ nan, ..., nan],
...,
[ nan, ..., nan]]],
...,
[[[ nan, ..., nan],
...,
[ nan, ..., nan]],
...,
[[ nan, ..., nan],
...,
[ nan, ..., nan]]]]])
Coordinates:
* lat (lat) float64 0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 ...
* lon (lon) float64 260.0 261.0 262.0 263.0 264.0 265.0 266.0 267.0 ...
* time (time) datetime64[ns] 1983-01-15T12:00:00
* 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
...
Attributes:
associated_files: baseURL: http://cmip-
pcmdi.llnl.gov/CMIP5/dataLocation...
cell_methods: time: mean
history: 2014-07-03T07:58:56Z altered by CMOR: Treated
scalar d...
long_name: Eastward Near-Surface Wind
standard_name: eastward_wind
units: m s-1
您可以看到,因为ws的维度名称为lon和lat,而ws的维度名称为经度和纬度,因此它创建的是5维的DataArray并且未正确拾取蒙版.
You can see because ws has dimension names lon and lat where as Hs has dimension names longitude and latitude it is creating a 5 dimension DataArray and not picking the mask up correctly.
无论尺寸名称不同还是DataArrays尺寸不同,我都可以选择蒙版吗?
Any way I can pick the mask regardless if the dimensions names are different or if the DataArrays are different sizes?
我以前是用numpy.math(ma)这样做的:
I previously did this with numpy.math (ma) as:
hs = f.variables['hs'][:]
hs_masked = ma.masked_values(hs, -65.534004)
tmp = np.zeros((len(lat), len(lon))
# Create masked array
data_cs = ma.masked_values(tmp, 0)
# Read new file
tmp = f.variables['cusp'][:]
data_cs[:,:] = ma.masked_array(tmp, hs_masked.mask)
但是希望学习/使用xarray.
But hoping to learn/use xarray.
干杯, 雷
推荐答案
您将需要显式重命名尺寸名称以进行匹配,例如hs = hs.rename({'lat': 'latitude', 'longitude': 'longitude'})
.您可能还需要使用最近邻居索引,如果坐标标签不完全匹配,例如hs.reindex_like(ws10, method='nearest', tolerance=0.01)
.
You will need to explicitly rename dimension names to match, e.g., hs = hs.rename({'lat': 'latitude', 'longitude': 'longitude'})
. You might also need to reindex with nearest-neighbor indexing, if the coordinate labels don't match exactly, e.g., hs.reindex_like(ws10, method='nearest', tolerance=0.01)
.
或者,出于不太安全的考虑,您可以从第二个参数中删除坐标标签,而只是传入未标记的数组,例如ws10.where(hs.data > 0)
.但我不建议使用此选项,因为没有任何方法可以保证元数据的一致性.
Or, less safely, you could strip out the coordinate labels from the second argument, and just pass in a unlabeled array instead, e.g., ws10.where(hs.data > 0)
. But I don't recommend this option, because nothing guarantees the consistency of the metadata.
这篇关于python-xarray从一个DataArray复制掩码到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!