gpt4 book ai didi

numpy - 如何将二维数组从较粗的分辨率插值到较精细的分辨率

转载 作者:行者123 更新时间:2023-12-01 22:01:39 25 4
gpt4 key购买 nike

假设我有一个形状为 (21600,43200) 的发射数据,对应于latlon,即

lat = np.arange(21600)*(-0.008333333)+90
lon = np.arange(43200)*0.00833333-180

我还有一个形状为 (720,1440,7) 的比例因子,它对应于 latlon星期几,和

lat = np.arange(720)*0.25-90 
lon = np.arange(1440)*0.25-180

现在,我想将该因子应用于排放数据,我认为我需要将 (720,1440) 上的因子插入到 (21600,43200)。之后,我可以将插值因子与排放数据相乘以获得新的排放输出。

但我对插值方法有困难。谁能给我一些建议?

最佳答案

这是您尝试执行的插值类型的完整示例。出于示例目的,我使用了 emission具有形状的数据 (10, 20)scale具有形状的数据 (5, 10) .它使用 scipy.interpolate.RectBivariateSpline ,这是在规则网格上进行插值的推荐方法:

import scipy.interpolate as sci

def latlon(res):
return (np.arange(res)*(180/res) - 90,
np.arange(2*res)*(360/(2*res)) - 180)

lat_fine,lon_fine = latlon(10)
emission = np.ones(10*20).reshape(10,20)

lat_coarse,lon_coarse = latlon(5)
scale = np.linspace(0, .5, num=5).reshape(-1, 1) + np.linspace(0, .5, num=10)

f = sci.RectBivariateSpline(lat_coarse, lon_coarse, scale)
scale_interp = f(lat_em, lon_em)

with np.printoptions(precision=1, suppress=True, linewidth=9999):
print('original emission data:\n%s\n' % emission)
print('original scale data:\n%s\n' % scale)
print('interpolated scale data:\n%s\n' % scale_interp)
print('scaled emission data:\n%s\n' % (emission*scale_interp))

哪些输出:

original emission data:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.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 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 0.9]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]
[0.5 0.5 0.6 0.6 0.6 0.6 0.7 0.7 0.7 0.7 0.8 0.8 0.8 0.9 0.9 0.9 0.9 1. 1. 1. ]]

注意事项

  • scipy.interpolate中的插值方法期望 x 和 y 都严格增加,所以你必须确保你的 emission数据排列在网格中,这样:

    lat = np.arange(21600)*0.008333333 - 90

    代替:

    lat = np.arange(21600)*(-0.008333333) + 90

    就像你上面说的那样。你可以翻转你的emission像这样的数据:

    emission = emission[::-1, :]

关于numpy - 如何将二维数组从较粗的分辨率插值到较精细的分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53392177/

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