ValueError:请求的 xi 之一在维度 0 中超出范围

问题描述 投票:0回答:1

我正在尝试将具有风速数据的 48 x 86 网格(网格是时间 x 高度)插值到 71 x 81 网格(也是时间 x 高度)。我正在尝试使用 scipy.interpolate 中的 interpn 函数,因为根据我的阅读,它是用于插值常规网格的推荐函数。当我尝试使用此函数时,出现标题错误(ValueError:请求的 xi 之一在维度 0 中超出范围)

我尝试过使用以下方法:

此代码块代表我想要将数据插值到的网格。

rangeTime = np.arange(np.datetime64('2018-05-30T00:20:00'),np.datetime64('2018-05-31T00:00:00'),np.timedelta64(20,'m'))
rangeHeight = np.arange(200,4250,50)
rangeTime = rangeTime.astype("float64")
inTime, inHeight = np.meshgrid(rangeTime,rangeHeight,indexing='ij')

这是我要插值的数据,其维度表示为:

times= ds.time.values #1d time array
heightvalues = ds.heights.values #48 x 86 grid with height data
height1d = heightvalues[0,:] #only need 1st slice, so I get 1d array with all my heights.
wspeed = ds.wspd.values #wind speed data, 48 x 86 grid

最后我像这样使用 interpn 函数:

grid_wspeed = interpn((times,height1d),wspeed,(inTime,inHeight), method = 'linear')

我期望我的风速数据位于 71 x 81 网格中。我不明白为什么我会收到 ValueError 。预先感谢您对我的帮助。

python scipy valueerror
1个回答
0
投票

据我了解,您想要在规则网格上执行数据的二维插值,以实现放大(或也可能缩小)。

由于您的数据是定期分布的,我建议使用

scipy.interpolate.RegularGridInterpolator

这是一个 2D(时间 x 空间)的示例,其中我的数据

z
将对应于您的风速。

进口

from itertools import product

import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import RegularGridInterpolator

在规则网格上生成假样本数据

# 2D grid (time, x)
t_min, t_max = 0, 1
x_min, x_max = -1.5, 1.5

time = np.linspace(t_min, t_max, 50)
x = np.linspace(x_min, x_max, 100)

time_grid, x_grid = np.meshgrid(time, x, indexing="ij")


# function to generate random z data
def func(x, y):
    return x * (1 - x) * np.cos(4 * np.pi * x) * np.sin(4 * np.pi * y**2) ** 2


# z data to interpolate
z = func(time_grid, x_grid)

显示我们想要插值的原始数据是什么样的

fig, ax = plt.subplots()

ax.imshow(z.T, origin="lower", extent=[t_min, t_max, x_min, x_max], aspect="auto")

ax.set_xlabel("time")
ax.set_ylabel("x")

plt.show()

enter image description here

执行常规插值

"""NB: since the interpolation is made on a regular grid,
RegularGridInterpolator do not need the redondant meshgrid version of time and
x. It only requires the 1D arrays of time and x, in upgoing order. However z
data obviously must consist in a multidimensional grid.
"""
func_interp = RegularGridInterpolator((time, x), z)

# number of pixels that we want after upscaling or downscaling
n_time, n_x = 100, 200

time_upscaled = np.linspace(t_min, t_max, n_time)
x_upscaled = np.linspace(x_min, x_max, n_x)

# `func_interp` works with a "1D array" of points (each point being itself a
# "1D array" of coordinates). itertools.product is a fast way to generate the
# list of points present in a multidimensional grid.
points_upscaled = np.array(list(product(time_upscaled, x_upscaled)))

# we compute the corresponding z-value at each point
z_upscaled = func_interp(points_upscaled)

绘制结果

fig, ax = plt.subplots()

# z_upscaled being a 1D array of values, it must be reshaped into a grid
ax.imshow(
    z_upscaled.reshape(n_time, n_x).T,
    origin="lower",
    extent=[t_min, t_max, x_min, x_max],
    aspect="auto",
)

ax.set_xlabel("time")
ax.set_ylabel("x")

plt.show()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.