我有一个大型高分辨率 3D (
time: 200, y: 2000, x: 2000
) xarray.DataArray
与此类似:
import xarray as xr
import numpy as np
import pandas as pd
time = pd.date_range("2019-01-01", "2021-12-30", periods=200)
y_large = np.linspace(-1000000, -1032000, 2000)
x_large = np.linspace(-1968000, -2000000, 2000)
data_large = np.random.randint(low=0, high=10, size=(200, 2000, 2000))
da_large = xr.DataArray(
data=data_large,
coords={"time": time, "y": y_large, "x": x_large},
dims=("time", "y", "x"),
)
da_large
我还有一个较小的低分辨率 (
time: 200, y: 100, x: 100
) xarray.DataArray
,其中包含不同的数据,但覆盖相同的 x
和 y
范围:
y_small = np.linspace(-1000000, -1032000, 100)
x_small = np.linspace(-1968000, -2000000, 100)
data_small = np.random.randint(low=100, high=110, size=(200, 100, 100))
da_small = xr.DataArray(
data=data_small,
coords={"time": time, "y": y_small, "x": x_small},
dims=("time", "y", "x"),
)
da_small
我需要将我的小型低分辨率数组(da_small
)插值到较大数组(
da_large
)的高分辨率网格中,这样我最终会得到一个包含从
time: 200, y: 2000, x: 2000
重新采样的值的da_small
数组.我想我可以使用
xarray
的
.interp()
方法来做到这一点,通过传入我的高分辨率坐标来采样并将
da_small
中的值插入到
da_large
的每个像素中:
da_small.interp(x=da_large.x, y=da_large.y, method="linear")
但是,我的挑战是此操作会导致内存出现极大的峰值,从而导致我的内核崩溃。这对我来说是一个障碍,因为我的实际数据可能比这个示例更大(高达几千像素高/宽和几百个时间步长)。我的问题:
如何以更有效的方式执行这种操作(重新缩放或将小数组插入到较大数组的网格中),避免如此大的内存使用峰值?
(如果可能的话,我更喜欢与xarray
兼容的解决方案,以便它可以插入我现有的工作流程中。)