gpt4 book ai didi

python - basemap 插值替代方案 - 重新网格化数据

转载 作者:太空宇宙 更新时间:2023-11-04 09:48:21 24 4
gpt4 key购买 nike

鉴于 basemap 将被淘汰,我正在从 basemap 转向 cartopy。我以前使用过 basemap.interp 功能来插入数据,例如假设我有 1 度分辨率 (180x360) 的数据,我将运行以下命令以插值到 0.5 度。

import numpy as np
from mpl_toolkits import basemap

Old_Lon = np.linspace(-180,180,360)
Old_Lat = np.linspace(-90,90,180)
New_Lon = np.linspace(-180,180,720)
New_Lat = np.linspace(-90,90,360)

New_Lon,New_Lat = np.meshgrid(New_Lon,New_Lat)

New_Data = basemap.interp(Old_Data,Old_Lon,Old_Lat,New_Lon,New_Lat,order=0)

order 为我提供了从最近邻、双线性等选项中进行选择的选项。是否有一种替代方法能够以如此简单的方式做到这一点?我已经看到 scipy 有插值,但我不确定如何应用它。任何帮助将不胜感激!

最佳答案

我最终决定从 Basemap 获取原始代码并将其变成一个独立的函数——我将把它推荐给 cartopy 的人,让他们将其作为一个有用的功能来实现。在这里张贴可能对其他人有用:

def Interp(datain,xin,yin,xout,yout,interpolation='NearestNeighbour'):

"""
Interpolates a 2D array onto a new grid (only works for linear grids),
with the Lat/Lon inputs of the old and new grid. Can perfom nearest
neighbour interpolation or bilinear interpolation (of order 1)'

This is an extract from the basemap module (truncated)
"""

# Mesh Coordinates so that they are both 2D arrays
xout,yout = np.meshgrid(xout,yout)

# compute grid coordinates of output grid.
delx = xin[1:]-xin[0:-1]
dely = yin[1:]-yin[0:-1]

xcoords = (len(xin)-1)*(xout-xin[0])/(xin[-1]-xin[0])
ycoords = (len(yin)-1)*(yout-yin[0])/(yin[-1]-yin[0])


xcoords = np.clip(xcoords,0,len(xin)-1)
ycoords = np.clip(ycoords,0,len(yin)-1)

# Interpolate to output grid using nearest neighbour
if interpolation == 'NearestNeighbour':
xcoordsi = np.around(xcoords).astype(np.int32)
ycoordsi = np.around(ycoords).astype(np.int32)
dataout = datain[ycoordsi,xcoordsi]

# Interpolate to output grid using bilinear interpolation.
elif interpolation == 'Bilinear':
xi = xcoords.astype(np.int32)
yi = ycoords.astype(np.int32)
xip1 = xi+1
yip1 = yi+1
xip1 = np.clip(xip1,0,len(xin)-1)
yip1 = np.clip(yip1,0,len(yin)-1)
delx = xcoords-xi.astype(np.float32)
dely = ycoords-yi.astype(np.float32)
dataout = (1.-delx)*(1.-dely)*datain[yi,xi] + \
delx*dely*datain[yip1,xip1] + \
(1.-delx)*dely*datain[yip1,xi] + \
delx*(1.-dely)*datain[yi,xip1]

return dataout

--

关于python - basemap 插值替代方案 - 重新网格化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48945682/

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