gpt4 book ai didi

python - 合并具有重叠坐标的多个 xarray 数据集

转载 作者:行者123 更新时间:2023-12-05 05:45:01 31 4
gpt4 key购买 nike

我正在尝试将具有重叠坐标的多个数据集合并为一个。当我将 compat= 设置为 'override' 时,仅保留第一个数据集的值,而结果数据集的其余部分设置为 nan。对于有冲突的单元格,我可以使用任何相交值。

看下面的例子

import numpy as np
import pandas as pd
import xarray as xr


temperature = np.random.randint(1,255,size=(9,10,10))
precipitation = np.random.randint(1,255,size=(9,10,10))
lon = np.linspace(10,100,10)
lat = np.linspace(10,100,10)
time_0 = pd.date_range("2014-09-06", periods=9, freq='2D')
time_1 = pd.date_range("2014-09-06", periods=9, freq='3D')

ds_0 = xr.Dataset(
data_vars=dict(
temperature=(('time', 'y', 'x'), temperature),
precipitation=(('time', 'y', 'x'), precipitation)),
coords=dict(
x=lon,
y=lat,
time=time_0)
)
ds_1 = xr.Dataset(
data_vars=dict(
temperature=(('time', 'y', 'x'), temperature),
precipitation=(('time', 'y', 'x'), precipitation)),
coords=dict(
x=lon + 90,
y=lat,
time=time_0)
)
ds_2 = xr.Dataset(
data_vars=dict(
temperature=(('time', 'y', 'x'), temperature),
precipitation=(('time', 'y', 'x'), precipitation)),
coords=dict(
x=lon + 90,
y=lat + 90,
time=time_1)
)
ds_3 = xr.Dataset(
data_vars=dict(
temperature=(('time', 'y', 'x'), temperature),
precipitation=(('time', 'y', 'x'), precipitation)),
coords=dict(
x=lon,
y=lat + 90,
time=time_1)
)

# Combine
ds = xr.merge([ds_0, ds_1, ds_2, ds_3], compat='override')
print(ds)

最佳答案

是的 - 当 compat='override'xr.merge 的预期和记录行为是使用第一个传递的数据对象的坐标。来自xr.merge docs :

compat ({"identical", "equals", "broadcast_equals", "no_conflicts", "override"}, optional) – String indicating how to compare variables of the same name for potential conflicts:

  • “broadcast_equals”: all values must be equal when variables are broadcast against each other to ensure common dimensions.

  • “equals”: all values and dimensions must be the same.

  • “identical”: all values, dimensions and attributes must be the same.

  • “no_conflicts”: only values which are not null in both datasets must be equal. The returned dataset then contains the combination of all non-null values.

  • “override”: skip comparing and pick variable from first dataset

所以我认为您可能正在寻找 compat='no_conflicts'

关于python - 合并具有重叠坐标的多个 xarray 数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71487497/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com