2015-12-03 34 views
1

我已經花了兩天尋找答案,但我一直有相同的錯誤,我不明白爲什麼。無論我做什麼,我不斷收到錯誤「ValueError異常:1個維數據未知插值法陣」在PYTHON上使用griddata的未知插值方法陣列

我試圖值內插成較細使用的GridData

meshgrid這裏是我的代碼

import numpy as np 

lon_ww3=array([[-10. , -10. , -10. , -10. , -10. , -10. , -10. , -10. , -10. ], 
       [ -9.5, -9.5, -9.5, -9.5, -9.5, -9.5, -9.5, -9.5, -9.5], 
       [ -9. , -9. , -9. , -9. , -9. , -9. , -9. , -9. , -9. ], 
       [ -8.5, -8.5, -8.5, -8.5, -8.5, -8.5, -8.5, -8.5, -8.5]]) 

lat_ww3=array([[ 38. , 38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. ], 
       [ 38. , 38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. ], 
       [ 38. , 38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. ], 
       [ 38. , 38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. ]]) 

Z=np.random.random_sample((4,9))*3 

#Create finer mesh grid 

lon_pn=[-10,-9] 
lat_pn=[38,42] 

lon_points=np.arange(lon_pn[0],lon_pn[1]+(300./3600),300./3600)[:-1] 
lat_points=np.arange(lat_pn[0],lat_pn[1]+(300./3600),300./3600)[:-1] 

LON_grid,LAT_grid=np.meshgrid(lon_points,lat_points) 

from scipy.interpolate import griddata 

Z_interp=griddata(lon_ww3.ravel(),lat_ww3.ravel(), Z.ravel(),LON_grid,LAT_grid) 

Z_interp=griddata(lon_ww3.ravel(),lat_ww3.ravel(), Z.ravel(),lon_points,lat_points) 

而且幾乎所有可能的變化我能想到的......每一次我得到了SAM:

我也沒有成功嘗試這樣做,顯然E錯誤:

「ValueError異常:未知插值法陣[LON_grid對於‘第n個’三維數據」

任何人都可以嘗試重現代碼,幫我找出發生了什麼?

在此先感謝

保羅

回答

1

你的griddata用法是錯誤的。

將這些行添加到您的代碼示例中。

xi = np.c_[lon_ww3.ravel(),lat_ww3.ravel()] 
xx = np.c_[LON_grid.ravel(),LAT_grid.ravel()] 

Z_interp=griddata(xi,Z.ravel(),xx) 

xi是你原來的網格點的n,D載體。 xx是你的插值點的N,D向量。

np.c_是每個方向座標的列堆棧。

+0

謝謝!現在我因爲自己找不到答案而陷入了一種愚蠢的境地...... – Elcook

0

實際上documentation for griddata 中有一個很好的例子第一個參數必須是一列座標(不是每個軸的兩個單獨的值),第二個參數是值列,第三個參數是新的網格(不是兩個獨立的值)。您提供了5個參數,這些參數是錯誤的來源。

而且您的網格創建也存在一些問題。以下是可用的代碼:

import numpy as np 
from scipy.interpolate import griddata 

lon_ww3 = np.array([[-10. , -10. , -10. , -10. , -10. , -10. , -10. , -10. , -10. ], 
       [ -9.5, -9.5, -9.5, -9.5, -9.5, -9.5, -9.5, -9.5, -9.5], 
       [ -9. , -9. , -9. , -9. , -9. , -9. , -9. , -9. , -9. ], 
       [ -8.5, -8.5, -8.5, -8.5, -8.5, -8.5, -8.5, -8.5, -8.5]]) 

lat_ww3 = np.array([[ 38. , 38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. ], 
       [ 38. , 38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. ], 
       [ 38. , 38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. ], 
       [ 38. , 38.5, 39. , 39.5, 40. , 40.5, 41. , 41.5, 42. ]]) 

# in fact you'd better initialise them like this 
#lon_ww3, lat_ww3 = np.mgrid[-10:-9:3j,38:42:9j] 

# make the column of [lon,lat] values 
lon_lat = np.c_[lon_ww3.ravel(), lat_ww3.ravel()] 

Z = np.random.random_sample((lon_ww3.shape))*3 

# make the propper grid 
LON_grid, LAT_grid = np.mgrid[-10:-9:12j,38:42:48j] 

Z_interp = griddata(lon_lat, Z.ravel(), (LON_grid, LAT_grid)) 

# you can see what you've done like this 
import matplotlib.pyplot as plt 
plt.imshow(Z_interp.T, extent = (-10, -8.5, 38, 42)) 
plt.show() 
+0

感謝Leonid,看來我並不理解griddata文檔頁面中的示例。我沒有意識到需要使用兩列數組才能工作。 – Elcook