简体   繁体   English

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

[英]Python/SciPy: Issues converting DataFrame from polar to Cartesian grid

I have measurements (PPI arc scans) taken with a doppler wind lidar. 我使用多普勒测风激光雷达进行了测量(PPI电弧扫描)。 The data is stored in a pandas dataframe where rows represent azimuth angle and columns represent radial distance (input shape = 30x197). 数据存储在pandas数据框中,其中行表示方位角,列表示径向距离(输入形状= 30x197)。 Link to example scan, (csv) . 链接到示例扫描(csv) I want to transform this to a cartesian coordinate system, and output a 2d array which is re-gridded into x,y coordinates instead of polar with the values stored in the appropriate grid cell. 我想将其转换为笛卡尔坐标系,并输出一个2d数组,将其重新栅格化为x,y坐标,而不是使用存储在适当网格单元中的极坐标。 Interpolation (nearest neighbor) is ok and so is zero or NaN padding of areas where no data exists. 插值(最近邻)可以,没有数据的区域的零或NaN填充也可以。

Ideally the X and Y grid should correspond to the actual distances between points, but right now I'm just trying to get this working. 理想情况下,X和Y网格应与点之间的实际距离相对应,但是现在我只是在尝试使其工作。 This shouldn't be terribly difficult, but I'm having trouble obtaining the result I want. 这应该不会很困难,但是我很难获得想要的结果。 So far, I have working code which plots on a polar axis beautifully (example image) but this won't work for the next steps of my analysis. 到目前为止,我的工作代码可以很好地绘制在极轴上(示例图像),但这不适用于我的下一步分析工作。

I have tried many different approaches with scipy.interpolate.griddata , scipy.ndimage.geometric_transform , and scipy.ndimage.map_coordinates but haven't gotten the correct output. 我已经尝试了scipy.interpolate.griddatascipy.ndimage.geometric_transformscipy.ndimage.map_coordinates许多不同的方法,但是没有获得正确的输出。 Here is an example of my recent attempt (df_polar is the csv file linked): 这是我最近尝试的一个示例(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')

The result is not correct at all, and from reading the documentation and examples I don't understand why. 结果根本不正确,从阅读文档和示例中我不明白为什么。 I would greatly appreciate any tips on where I have gone wrong. 我会很感激关于哪里出了问题的任何提示。 Thanks a lot!! 非常感谢!!

I think you might be feeding griddata the wrong points. 我认为您可能在为griddata错误的点。 It wants cartesian points and if you want the values interpolated over a regular x/y grid you need to create one and provide that too. 它需要笛卡尔点,如果要在常规x / y网格上插值,则需要创建一个并提供它。

Try this and let me know if it produces the expected result. 试试这个,让我知道它是否产生预期的结果。 It's hard for me to tell if this is what it should produce: 我很难说这是否应该产生:

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'
)

The resulting values look something like this (with grid_spacing = 10 and flipping x and y): 结果值看起来像这样( grid_spacing = 10并翻转x和y):

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

在此处输入图片说明

Clearly interpolate "nearest" needs taming... 显然插值“最近”需要驯服...

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

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