首页 文章

Python / SciPy:将DataFrame从极地转换为笛卡尔网格的问题

提问于
浏览
1

我用多普勒风激光雷达测量(PPI电弧扫描) . 数据存储在pandas数据帧中,其中行表示方位角,列表示径向距离(输入形状= 30x197) . Link to example scan, (csv) . 我想将其转换为笛卡尔坐标系,并输出一个二维数组,该数组被重新网格化为x,y坐标而不是极坐标,其值存储在适当的网格单元格中 . 插值(最近邻居)是正常的,因此没有数据存在的区域为零或NaN填充 .

理想情况下,X和Y网格应该对应于点之间的实际距离,但是现在我只是想让它工作 . 这应该不是非常困难,但我无法获得我想要的结果 . 到目前为止,我的工作代码在极轴上精心绘制(example image)但这不适用于我的分析的后续步骤 .

我已经尝试了许多不同的方法 scipy.interpolate.griddatascipy.ndimage.geometric_transformscipy.ndimage.map_coordinates 但没有得到正确的输出 . 以下是我最近尝试的示例(df_polar是链接的csv文件):

# Generate polar and cartesian meshgrids
r = df_polar.columns
theta = df_polar.index
theta = np.deg2rad(theta)

# Polar meshgrid
rad_c, theta_c = np.meshgrid(r,theta)

# Cartesian meshgrid
X = rad_c * np.cos(theta_c)
Y = rad_c * np.sin(theta_c)
x,y = np.meshgrid(X,Y)

# Interpolate from polar to cartesian grid
new_grid = scipy.interpolate.griddata(
    (rad_c.flatten(), theta_c.flatten()), 
    np.array(df_polar).flatten(), (x,y), method='nearest')

结果根本不正确,从阅读文档和示例我不明白为什么 . 我非常感谢任何关于我出错的提示 . 非常感谢!!

1 回答

  • 0

    我想你可能会错误地提供 griddata . 它需要笛卡尔点,如果你想在常规的x / y网格上插值,你需要创建一个并提供它 .

    试试这个,让我知道它是否产生了预期的结果 . 我很难判断这是否应该产生:

    from scipy.interpolate import griddata
    import pandas as pd
    import numpy as np
    
    df_polar = pd.read_csv('onescan.txt', index_col=0)
    
    # Generate polar and cartesian meshgrids
    r = pd.to_numeric(df_polar.columns)
    theta = np.deg2rad(df_polar.index)
    
    # Polar meshgrid
    rad_c, theta_c = np.meshgrid(r, theta)
    
    # Cartesian equivalents of polar co-ordinates
    X = rad_c*np.cos(theta_c)
    Y = rad_c*np.sin(theta_c)
    
    # Cartesian (x/y) meshgrid
    grid_spacing = 100.0   # You can change this
    nx = (X.max() - X.min())/grid_spacing
    ny = (Y.max() - Y.min())/grid_spacing
    x = np.arange(X.min(), X.max() + grid_spacing, grid_spacing)
    y = np.arange(Y.min(), Y.max() + grid_spacing, grid_spacing)
    grid_x, grid_y = np.meshgrid(x, y)
    
    # Interpolate from polar to cartesian grid
    new_grid = griddata(
        (X.flatten(), Y.flatten()),
        df_polar.values.flatten(),
        (grid_x, grid_y),
        method='nearest'
    )
    

    结果值看起来像这样(使用 grid_spacing = 10 并翻转x和y):

    import matplotlib.pyplot as plt
    plt.imshow(new_grid.T, cmap='hot')
    

    enter image description here

    显然插入“最近”需要驯服......

相关问题