简体   繁体   English

xarray 沿切片维度删除元素

[英]xarray dropping elements along a sliced dimension

I am reading a NetCDF (time, level, lat, lon) file with xarray.我正在使用 xarray 读取 NetCDF(时间、水平、纬度、经度)文件。 I am selecting two slices over level with the same size.我正在选择两个大小相同的切片。 I am wondering when I am doing an addition of the two files.我想知道何时添加这两个文件。

The resulting file is not giving the right dimension.生成的文件没有给出正确的尺寸。

slice1 -> (72, 22 , 41, 36) slice1 -> (72, 22 , 41, 36)

slice2 -> (72, 22 , 41, 36) slice2 -> (72, 22 , 41, 36)

result -> (72, 21 , 41, 36)结果 -> (72, 21 , 41, 36)

What is wrong?怎么了?

Here code that I am using这是我正在使用的代码

import xarray as xr
fname  =  "DJF_uvwq_lev_monhourly_2017.nc"
ds = xr.open_dataset(fname)
            
u = ds['u']
level   = ds['level']
            
a = u.isel(level=slice(0, len(plev)-1))
b = u.isel(level=slice(1, len(plev)))
            
fluxInterp = a + b
            
print(np.shape(a))
print(np.shape(b))
print(np.shape(fluxInterp))

Unlike numpy, which aligns data based on the position in the array, xarray uses labels along a given dimension to align the data when performing data operations.与 numpy 根据数组中的位置对齐数据不同,xarray 在执行数据操作时使用沿给定维度的标签来对齐数据。 Therefore, having offset labels as in the case of a and b along the level dimension will not perform the way you're expecting.因此,在水平维度的ab的情况下具有偏移标签不会执行您期望的方式。

Setting up a dummy example:设置一个虚拟示例:

In [1]: import xarray as xr, pandas as pd, numpy as np

In [2]: u = xr.DataArray(np.arange(5), dims=['level'], coords=[list('abcde')])

In [3]: u
Out[3]:
<xarray.DataArray (level: 5)>
array([0, 1, 2, 3, 4])
Coordinates:
  * level    (level) <U1 'a' 'b' 'c' 'd' 'e'

When looking at a and b in your example, you're slicing the data using an offset ( slice(0, length-1) vs slice(1, length) ).在您的示例中查看 a 和 b 时,您正在使用偏移量( slice(0, length-1) vs slice(1, length) )对数据进行slice(0, length-1) When you do this, notice that the indices of "level" are no longer aligned:执行此操作时,请注意“级别”的索引不再对齐:

In [4]: a = u.isel(level=slice(0, len(u.level)- 1))
   ...: b = u.isel(level=slice(1, len(u.level)))

In [5]: a
Out[5]:
<xarray.DataArray (level: 4)>
array([0, 1, 2, 3])
Coordinates:
  * level    (level) <U1 'a' 'b' 'c' 'd'

In [6]: b
Out[6]:
<xarray.DataArray (level: 4)>
array([1, 2, 3, 4])
Coordinates:
  * level    (level) <U1 'b' 'c' 'd' 'e'

When adding the two, or in any operation that involves broadcasting & automatic alignment (see the docs references below), missing values will be dropped from the result.当添加两者时,或在任何涉及广播和自动对齐的操作中(请参阅下面的文档参考),缺失值将从结果中删除。 Furthermore, in the results, note that the sum is the elementwise-sum where each element is aligned based on the label (b + b, c + c, d + d), not based on position (b + a, c + b, d + c).此外,在结果中,请注意总和是元素总和,其中每个元素根据标签 (b + b, c + c, d + d) 对齐,而不是基于位置 (b + a, c + b , d + c)。

In [7]: a + b
Out[7]:
<xarray.DataArray (level: 3)>
array([2, 4, 6])
Coordinates:
  * level    (level) <U1 'b' 'c' 'd'

What you're looking for is to first use the shift method to shift the axis labels, so that the coordinate labels are aligned before you do the addition:您要寻找的是首先使用shift方法移动轴标签,以便在添加之前对齐坐标标签:

In [10]: c = u.shift(level=-1)

In [11]: c
Out[11]:
<xarray.DataArray (level: 5)>
array([ 1.,  2.,  3.,  4., nan])
Coordinates:
  * level    (level) <U1 'a' 'b' 'c' 'd' 'e'

In [12]: a + c
Out[12]:
<xarray.DataArray (level: 4)>
array([1., 3., 5., 7.])
Coordinates:
  * level    (level) <U1 'a' 'b' 'c' 'd'

Now, when you add across a + c the coordinates line up in the way you'd like.现在,当您在a + c添加时,坐标以您想要的方式排列。

For more information, see the xarray Computation docs on broadcasting by dimension name and automatic alignment .有关更多信息,请参阅有关按维度名称自动对齐进行广播的 xarray 计算文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM