使用Python重新生成三维笛卡尔到极坐标NetCDF数据

使用Python重新生成三维笛卡尔到极坐标NetCDF数据,python,numpy,scipy,netcdf,Python,Numpy,Scipy,Netcdf,我有一个NetCDF变量,它的维度是时间x,y。目前是笛卡尔坐标系,但我需要极坐标系的数据。我曾尝试创建一个函数来实现这一点,但我似乎无法正确实现。有人知道有没有更简单的方法 def regrid(x,y,xcent,ycent,vardat): x=np.subtract(x,xcent) y=np.subtract(y,ycent) threshmin = np.min(vardat) threshmax = np.max(vardat) rmax =

我有一个NetCDF变量,它的维度是时间x,y。目前是笛卡尔坐标系,但我需要极坐标系的数据。我曾尝试创建一个函数来实现这一点,但我似乎无法正确实现。有人知道有没有更简单的方法

def regrid(x,y,xcent,ycent,vardat):
    x=np.subtract(x,xcent)
    y=np.subtract(y,ycent)
    threshmin = np.min(vardat)
    threshmax = np.max(vardat)
    rmax = np.ceil(np.sqrt(((x[-1]-x[0])/2.)**2 + ((y[-1]-y[0])/2.)**2))
    r = np.arange(0,rmax,(x[1]-x[0]))
    theta_inc = np.floor(np.arctan2(y[1]-y[0],(x[-1]-x[0])/2.)/np.pi*180.)
    if theta_inc <1.0:
        theta_inc = 1
    theta = np.arange(0,(360-theta_inc),theta_inc)
    r2d, theta2d = np.meshgrid(r,theta)
    x_polar = r2d*np.cos(np.pi/180.*theta2d)
    y_polar = r2d*np.sin(np.pi/180.*theta2d)
    x_range = np.arange(x[0],x[-1]+1,(x[1]-x[0]))
    y_range = np.arange(y[0],y[-1]+1,(y[1]-y[0]))
    field_rt = np.zeros((len(r),len(theta)))


    field_interp = interp2d(x_range,y_range,vardat,kind='linear')
    for i in np.arange(0,len(r)):
        for j in np.arange(0,len(theta)):

    *       field_rt[i,j] = field_interp(x_polar[i,j],y_polar[i,j])

    return r, theta, field_rt

r1,theta1, field = regrid(we_ea,no_so,124,124,olr[0,:,:])
def regrid(x、y、xcent、ycent、vardat):
x=np.减法(x,xcent)
y=np.减法(y,ycent)
threshmin=np.min(vardat)
threshmax=np.max(vardat)
rmax=np.ceil(np.sqrt(((x[-1]-x[0])/2.)**2+((y[-1]-y[0])/2.)**2))
r=np.arange(0,rmax,(x[1]-x[0]))
theta_inc=np.floor(np.arctan2(y[1]-y[0],(x[-1]-x[0])/2.)/np.pi*180.)
如果theta_inc这里有一种(未优化的)最近邻方法将数据从常规笛卡尔网格重新映射到常规极坐标网格。应根据您的需要调整坐标间距
dr
dphi
。您还可以搜索N个最近邻并应用一些统计度量,例如距离加权平均值等。对于大型数据集,我建议使用来加速最近邻搜索。对于重新映射地理空间数据,有一个漂亮且快速的包,名为

将numpy导入为np
将matplotlib.pyplot作为plt导入
deg2rad=np.pi/180.0
#定义笛卡尔网格的属性
x_vals=np.arange(-1,1,0.01)
y_vals=np.arange(-1,1,0.01)
mx,my=np.meshgrid(y值,x值)
#在笛卡尔网格上定义数据
数据车=np.sin(mx)+np.cos(my)
#定义极轴网的属性
dr=0.1
dphi=1*deg2rad
rmax=np.sqrt(x_vals.max()**2+y_vals.max()**2)
r_vals=np.arange(0,rmax,dr)
phi_vals=np.arange(0,2*np.pi,dphi)
如果len(r_vals)*len(phi_vals)>len(x_vals)*len(y_vals):
打印“警告:过采样”
mr,mphi=np.meshgrid(phi_-vals,r_-vals)
#使用填充值初始化极轴栅格上的数据
填充值=-9999.0
数据=填充值*np.ones((len(r\u vals),len(phi\u vals)))
#定义影响半径。在此半径之外的最近邻居将不会
#必须考虑到这一点。
影响半径=np.sqrt(0.1**2+0.1**2)
#对于极坐标网格中的每个单元,在笛卡尔坐标系中查找最近的邻居
#网格。如果它位于影响半径内,则传递相应的
#数据。
对于r,zip中的极坐标行(r值,范围(len(r值)):
对于phi,zip中的极坐标(phi值,范围(长度(phi值)):
#将极坐标变换为笛卡尔坐标
x=r*np.cos(φ)
y=r*np.sin(φ)
#在笛卡尔网格中寻找最近邻
d=np.sqrt((x-mx)**2+(y-my)**2)
nn_行_车,nn_列_车=np.展开索引(np.argmin(d),d.形状)
dmin=d[nn_行车,nn_列车]
#传输数据
如果dmin,这里有一种(未优化的)最近邻方法将数据从常规笛卡尔网格重新映射到常规极坐标网格。应根据您的需要调整坐标间距
dr
dphi
。您还可以搜索N个最近邻并应用一些统计度量,例如距离加权平均值等。对于大型数据集,我建议使用来加速最近邻搜索。对于重新映射地理空间数据,有一个漂亮且快速的包,名为

将numpy导入为np
将matplotlib.pyplot作为plt导入
deg2rad=np.pi/180.0
#定义笛卡尔网格的属性
x_vals=np.arange(-1,1,0.01)
y_vals=np.arange(-1,1,0.01)
mx,my=np.meshgrid(y值,x值)
#在笛卡尔网格上定义数据
数据车=np.sin(mx)+np.cos(my)
#定义极轴网的属性
dr=0.1
dphi=1*deg2rad
rmax=np.sqrt(x_vals.max()**2+y_vals.max()**2)
r_vals=np.arange(0,rmax,dr)
phi_vals=np.arange(0,2*np.pi,dphi)
如果len(r_vals)*len(phi_vals)>len(x_vals)*len(y_vals):
打印“警告:过采样”
mr,mphi=np.meshgrid(phi_-vals,r_-vals)
#使用填充值初始化极轴栅格上的数据
填充值=-9999.0
数据=填充值*np.ones((len(r\u vals),len(phi\u vals)))
#定义影响半径。在此半径之外的最近邻居将不会
#必须考虑到这一点。
影响半径=np.sqrt(0.1**2+0.1**2)
#对于极坐标网格中的每个单元,在笛卡尔坐标系中查找最近的邻居
#网格。如果它位于影响半径内,则传递相应的
#数据。
对于r,zip中的极坐标行(r值,范围(len(r值)):
对于phi,zip中的极坐标(phi值,范围(长度(phi值)):
#将极坐标变换为笛卡尔坐标
x=r*np.cos(φ)
y=r*np.sin(φ)
#在笛卡尔网格中寻找最近邻
d=np.sqrt((x-mx)**2+(y-my)**2)
nn_行_车,nn_列_车=np.展开索引(np.argmin(d),d.形状)
dmin=d[nn_行车,nn_列车]
#传输数据

如果德明知道你到底想做什么会很有帮助。从笛卡尔坐标变换到极坐标变换通常是一个非常简单的变换,只要在维基百科上查一下就可以了。我不明白你为什么要执行插值。查看准确的错误输出以及使用函数的数据也会很有帮助。你应该做一个简单的测试用例,首先你知道答案。这将有助于你准确地知道你想要做什么。从笛卡尔坐标变换到极坐标变换通常是一个非常简单的变换,只要在维基百科上查一下就可以了。我不明白你为什么要执行插值。查看准确的错误输出以及使用函数的数据也会很有帮助。你应该做一个简单的测试用例,你知道答案。
import numpy as np
import matplotlib.pyplot as plt

deg2rad = np.pi/180.0

# Define properties of cartesian grid
x_vals = np.arange(-1, 1, 0.01)
y_vals = np.arange(-1, 1, 0.01)
mx, my = np.meshgrid(y_vals, x_vals)

# Define data on cartesian grid
data_cart = np.sin(mx) + np.cos(my)

# Define properties of polar grid
dr = 0.1
dphi = 1*deg2rad
rmax = np.sqrt(x_vals.max()**2 + y_vals.max()**2)
r_vals = np.arange(0, rmax, dr)
phi_vals = np.arange(0, 2*np.pi, dphi)
if len(r_vals)*len(phi_vals) > len(x_vals)*len(y_vals):
    print "Warning: Oversampling"
mr, mphi = np.meshgrid(phi_vals, r_vals)

# Initialize data on polar grid with fill values
fill_value = -9999.0
data_polar = fill_value*np.ones((len(r_vals), len(phi_vals)))

# Define radius of influence. A nearest neighbour outside this radius will not
# be taken into account.
radius_of_influence = np.sqrt(0.1**2 + 0.1**2)

# For each cell in the polar grid, find the nearest neighbour in the cartesian
# grid. If it lies within the radius of influence, transfer the corresponding
# data.
for r, row_polar in zip(r_vals, range(len(r_vals))):
    for phi, col_polar in zip(phi_vals, range(len(phi_vals))):
        # Transform polar to cartesian
        x = r*np.cos(phi)
        y = r*np.sin(phi)

        # Find nearest neighbour in cartesian grid
        d = np.sqrt((x-mx)**2 + (y-my)**2)
        nn_row_cart, nn_col_cart = np.unravel_index(np.argmin(d), d.shape)
        dmin = d[nn_row_cart, nn_col_cart]

        # Transfer data
        if dmin <= radius_of_influence:
            data_polar[row_polar, col_polar] = data_cart[nn_row_cart, nn_col_cart]

# Mask remaining fill values
data_polar = np.ma.masked_equal(data_polar, fill_value)

# Plot results
plt.figure()
im = plt.pcolormesh(mx, my, data_cart)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Cartesian')
plt.colorbar(im)

plt.figure()
ax = plt.subplot(111, projection='polar')
im = ax.pcolormesh(mr, mphi, data_polar)
ax.set_title('Polar')
plt.colorbar(im)

plt.show()