xarray:遍历数据集的所有坐标

问题描述

我要遍历nd xarray数据集中的所有坐标,请考虑:

Dimensions:  (x0: 2,x1: 4,x2: 3)
Coordinates:
  * x0       (x0) int64 0 1
  * x1       (x1) int64 0 1 2 3
  * x2       (x2) int64 0 1 2
Data variables:
    y0       (x0,x1,x2) ...

我的用例是,我将读取每个坐标元组,将其发送给函数,然后将结果保存在数据集中。我当前的代码

# first build a list of tuples containing every dimension-coordinate pair
# we add the dimension so we can find this location later on
locations = []
for coord in dataset.coords:
    dim = []
    for v in dataset[coord].values
        dim.append((coord,v))
    locations.append(dim)
# locations Now looks like [('x0',0),('x0,1),('x1',0) ... ('x2',2)]

# cross product the locations to create all possible coordinates
for tup in itertools.product(*locations):
    # tup looks like (('x0',3),('x2',2)) etc.
    val = my_function(marshal(tup))     # call the function with the relevant values
    dataset['y0'].loc[dict(tup)] = val  # set the returned value to the right position in the dataset

这很好用,但我想知道是否还有更简洁的方法来实现相同目的?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)