2016-09-21 78 views
4

我需要将尺寸添加到DataArray,填充整个新尺寸的值。这是原始数组。将尺寸添加到xarray DataArray

a_size = 10 
a_coords = np.linspace(0, 1, a_size) 

b_size = 5 
b_coords = np.linspace(0, 1, b_size) 

# original 1-dimensional array 
x = xr.DataArray(
    np.random.random(a_size), 
    coords=[('a', a coords)]) 

我想我可以创建新的层面空DataArray中复制现有数据。

y = xr.DataArray(
    np.empty((b_size, a_size), 
    coords=([('b', b_coords), ('a', a_coords)]) 
y[:] = x 

一个更好的想法可能是使用concat。我花了一段时间才弄清楚如何为concat维度指定dims和coords,而这些选项都不是很好。有什么我错过,可以使这个版本更清洁?

# specify the dimension name, then set the coordinates 
y = xr.concat([x for _ in b_coords], 'b') 
y['b'] = b_coords 

# specify the coordinates, then rename the dimension 
y = xr.concat([x for _ in b_coords], b_coords) 
y.rename({'concat_dim': 'b'}) 

# use a DataArray as the concat dimension 
y = xr.concat(
    [x for _ in b_coords], 
    xr.DataArray(b_coords, name='b', dims=['b'])) 

不过,是否有更好的方法来做到这一点比上述两个选项之一?

回答

1

您已经对目前的选项进行了非常透彻的分析,实际上这些都不是很干净。

这对于xarray来说肯定是有用的函数,但没有人能够实现它。也许你会对帮忙感兴趣?

一些API提案认为这个问题GitHub的:https://github.com/pydata/xarray/issues/170

2

由于有这种数学被应用在新的层面我想,以增加新的维度繁殖的方式。

identityb = xr.DataArray(np.ones_like(b_coords), coords=[('b', b_coords)]) 
y = x * identityb