簡體   English   中英

使用python scipy interpolate.interpn值錯誤將具有常規網格的變量插值到不在常規網格上的位置

[英]Interpolating a variable with regular grid to a location not on the regular grid with Python scipy interpolate.interpn value error

我有一個netcdf文件中的變量,它是時間,高度,lon和lat的常規網格數據的函數:U [time,height,lon,lat]。 我想將此變量插值到不在常規網格上的lon_new,lat_new的定義位置(它位於網格點之間)。 我希望能夠根據單個插值位置獲得變量U [0,0,lon_new,lat_new]。

我閱讀了scipy插值函數,並認為scipy.interpolate.interpn是我要使用的函數。 我試圖做一個簡單的例子來說明這個功能,但是總是出錯。

x_points = [1,2,3,4] #lets call this list of lons on the grid
y_points = [1,2,3,4] #lets call this list of lats on the grid

#Get the lon,lat pairs
point_pairs=[]
for i in x_points:
    for j in y_points:
        points = [i,j]
        point_pairs.append(points)
print point_pairs
print np.shape(point_pairs)

[[1, 1], [1, 2], [1, 3], [1, 4], [2, 1], [2, 2], [2, 3], [2, 4], [3, 1], [3,  2], [3, 3], [3, 4], [4, 1], [4, 2], [4, 3], [4, 4]]
(16L, 2L)

xi = (2.5,2.5) #point in between grid points that I am interested in getting     the interpolated value
xi=np.array(xi)
print xi
print np.shape(xi)

[ 2.5  2.5]
(2L,)

values = np.ones(16) #array of values at every grid point Let's say I loop   over every grid point and get the value at each one
print values

[ 1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.  1.]

interpolated_value = interpolate.interpn(point_pairs, values, xi, method='linear')

ValueError: There are 16 point arrays, but values has 1 dimensions

您可以從scipy使用任何適當的多元插值函數。 在下面的示例中進行更正會產生正確的結果。

# -*- coding: utf-8 -*-

import numpy as np
from scipy import interpolate

x_points = np.array([1, 2, 3, 4])
y_points = np.array([1, 2, 3, 4])
values = np.ones((4, 4))   # 2 dimensional array
xi = np.array([2.5, 2.5])

interpolated_value = interpolate.interpn((x_points, y_points), values, xi, method='linear')
print(interpolated_value)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM