gpt4 book ai didi

python - 将极坐标重新投影到笛卡尔网格

转载 作者:太空狗 更新时间:2023-10-29 16:56:56 25 4
gpt4 key购买 nike

我有一个极坐标 (r,theta) 网格(这意味着每个单元格都是一个环形部分),其中包含一些物理量(例如温度)的值,我想重新网格化(或重新投影,或resample) 这些值到笛卡尔网格上。是否有任何 Python 包可以执行此操作?

我对将单元格中心坐标从极坐标转换为笛卡尔坐标不感兴趣——这很容易。相反,我正在寻找一个实际上可以正确重新网格化数据的包。

感谢您的任何建议!

最佳答案

感谢您的回答 - 在对此进行更多思考后,我想出了以下代码:

import numpy as np

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as mpl

from scipy.interpolate import interp1d
from scipy.ndimage import map_coordinates


def polar2cartesian(r, t, grid, x, y, order=3):

X, Y = np.meshgrid(x, y)

new_r = np.sqrt(X*X+Y*Y)
new_t = np.arctan2(X, Y)

ir = interp1d(r, np.arange(len(r)), bounds_error=False)
it = interp1d(t, np.arange(len(t)))

new_ir = ir(new_r.ravel())
new_it = it(new_t.ravel())

new_ir[new_r.ravel() > r.max()] = len(r)-1
new_ir[new_r.ravel() < r.min()] = 0

return map_coordinates(grid, np.array([new_ir, new_it]),
order=order).reshape(new_r.shape)

# Define original polar grid

nr = 10
nt = 10

r = np.linspace(1, 100, nr)
t = np.linspace(0., np.pi, nt)
z = np.random.random((nr, nt))

# Define new cartesian grid

nx = 100
ny = 200

x = np.linspace(0., 100., nx)
y = np.linspace(-100., 100., ny)

# Interpolate polar grid to cartesian grid (nearest neighbor)

fig = mpl.figure()
ax = fig.add_subplot(111)
ax.imshow(polar2cartesian(r, t, z, x, y, order=0), interpolation='nearest')
fig.savefig('test1.png')

# Interpolate polar grid to cartesian grid (cubic spline)

fig = mpl.figure()
ax = fig.add_subplot(111)
ax.imshow(polar2cartesian(r, t, z, x, y, order=3), interpolation='nearest')
fig.savefig('test2.png')

这不是严格意义上的重新网格化,但可以很好地满足我的需要。只是发布代码以防它对其他人有用。欢迎提出改进建议!

关于python - 将极坐标重新投影到笛卡尔网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2164570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com