簡體   English   中英

Python:將一維數組(具有等面積坐標系)轉換為二維數組(具有地理坐標參考系)

[英]Python: convert 1-D array (with equal area coordinate system) to a 2-D array (with Geographic Coordinate Reference System)

我有一個一維數據數組(例如降水[沉淀])。 另外,我有 1D 緯度(最小 -90 度,最大 +90 度)和 1D 經度(最小 0,最大 360 度)arrays 代表此數據的坐標。 坐標系是“等面積”。 它是一個全球數據集。

我的問題是如何將這個一維數組轉換為具有地理坐標參考系的二維數組(即等距網格,平行線和經線),空間分辨率為 1 x 1 度,這樣我就有了一個 180*360 的數組(最好使用 pyproj / xarray)?

謝謝!

以下是數據集的信息:

xarray.Dataset

尺寸:(eqcell:41252)

無坐標尺寸:eqcell

數據變量:

lat                (eqcell) float32 dask.array chunksize=(41252,), meta=np.ndarray

lon                (eqcell) float32 dask.array chunksize=(41252,), meta=np.ndarray

precip              (eqcell) float32 dask.array chunksize=(41252,), meta=np.ndarray

看起來您想要scipy.interpolate.griddata 這是文檔中的示例:


假設我們要對二維 function 進行插值

>>> def func(x, y):
...     return x*(1-x)*np.cos(4*np.pi*x) * np.sin(4*np.pi*y**2)**2

在 [0, 1]x[0, 1] 的網格上

>>> grid_x, grid_y = np.mgrid[0:1:100j, 0:1:200j]

但我們只知道它在 1000 個數據點處的值:

>>> points = np.random.rand(1000, 2)
>>> values = func(points[:,0], points[:,1])

這可以通過 griddata 來完成——下面我們嘗試所有的插值方法:

>>> from scipy.interpolate import griddata
>>> grid_z0 = griddata(points, values, (grid_x, grid_y), method='nearest')
>>> grid_z1 = griddata(points, values, (grid_x, grid_y), method='linear')
>>> grid_z2 = griddata(points, values, (grid_x, grid_y), method='cubic')

可以看出,所有方法都在某種程度上重現了確切的結果,但是對於這種平滑的 function,分段三次插值法給出了最好的結果:

>>> import matplotlib.pyplot as plt
>>> plt.subplot(221)
>>> plt.imshow(func(grid_x, grid_y).T, extent=(0,1,0,1), origin='lower')
>>> plt.plot(points[:,0], points[:,1], 'k.', ms=1)
>>> plt.title('Original')
>>> plt.subplot(222)
>>> plt.imshow(grid_z0.T, extent=(0,1,0,1), origin='lower')
>>> plt.title('Nearest')
>>> plt.subplot(223)
>>> plt.imshow(grid_z1.T, extent=(0,1,0,1), origin='lower')
>>> plt.title('Linear')
>>> plt.subplot(224)
>>> plt.imshow(grid_z2.T, extent=(0,1,0,1), origin='lower')
>>> plt.title('Cubic')
>>> plt.gcf().set_size_inches(6, 6)
>>> plt.show()

陰謀
(來源: scipy.org

暫無
暫無

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

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