简体   繁体   English

Python:将一维数组(具有等面积坐标系)转换为二维数组(具有地理坐标参考系)

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

I have a 1-D array of data (eg Precipitation [precip]).我有一个一维数据数组(例如降水[沉淀])。 Also, I have 1D latitude (min -90 deg., max +90 deg.) and 1D longitude (min 0, max 360 deg.) arrays representing the coordinates of this data.另外,我有 1D 纬度(最小 -90 度,最大 +90 度)和 1D 经度(最小 0,最大 360 度)arrays 代表此数据的坐标。 The coordinate system is " equal area ".坐标系是“等面积”。 It is a global dataset.它是一个全球数据集。

My question is how can I convert this 1-D array to a 2-D array with Geographic Coordinate Reference System (ie equally spaced grid, both parallels, and meridians) with a spatial resolution of 1 by 1 degree, so that I would have a 180*360 array (preferably, using pyproj / xarray)?我的问题是如何将这个一维数组转换为具有地理坐标参考系的二维数组(即等距网格,平行线和经线),空间分辨率为 1 x 1 度,这样我就有了一个 180*360 的数组(最好使用 pyproj / xarray)?

Thanks!谢谢!

The following is the information of the dataset:以下是数据集的信息:

xarray.Dataset xarray.Dataset

Dimensions: (eqcell: 41252)尺寸:(eqcell:41252)

Dimensions without coordinates: eqcell无坐标尺寸:eqcell

Data variables:数据变量:

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

It looks like you want scipy.interpolate.griddata .看起来您想要scipy.interpolate.griddata Here's the example from the documentation:这是文档中的示例:


Suppose we want to interpolate the 2-D function假设我们要对二维 function 进行插值

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

on a grid in [0, 1]x[0, 1]在 [0, 1]x[0, 1] 的网格上

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

but we only know its values at 1000 data points:但我们只知道它在 1000 个数据点处的值:

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

This can be done with griddata – below we try out all of the interpolation methods:这可以通过 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')

One can see that the exact result is reproduced by all of the methods to some degree, but for this smooth function the piecewise cubic interpolant gives the best results:可以看出,所有方法都在某种程度上重现了确切的结果,但是对于这种平滑的 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()

阴谋
(source: scipy.org ) (来源: scipy.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM