![](/img/trans.png)
[英]How to manipulate a 3D array to convert from cartesian coordinates to spherical coordinates
[英]How can I interpolate array from spherical to cartesian coordinates with Python?
我在球坐標中有一個密度值數組。 更具體地說,我有一個名為密度為(180,200,200)的數組。 我還有一個名為r_coord,theta_coord和phi_coord的數組,它們的形狀(180,200,200)是密度數組的球坐標。
我想使用python將此密度映射到笛卡爾坐標。 因此,我將需要一個新的密度2,該密度將插值在笛卡爾坐標x_coord,y_coord和z_coord上。 我發現scipy.ndimage.interpolation.map_coordinates看起來很有希望,但我不知道如何使它工作。
任何幫助,將不勝感激。 謝謝。
這樣的事情應該起作用:
import scipy.interpolate
rflat=scipy.array( r_coord.flat )
tflat=scipy.array( theta_coord.flat )
pflat=scipy.array( phi_coord.flat )
coordpoints=scipy.concatenate( [ rflat[:, scipy.newaxis], tflat[:,scipy.newaxis], pflat[:,scipy.newaxis] ] , axis=1 )
rtpinterpolator=scipy.interpolate.linearNDInterpolate( coordppoints, density.flat )
def xyz2rtp( x,y,z):
r=scipy.sqrt( x**2+y**2+z**2)
t=scipy.acos( z/r )
p=scipy.atan2( y, x )
return (r,t,p)
# now you can get the interpolated value for any (x,y,z) coordinate you want.
val=rtpinterpolator( xyz2rtp( x,y,z) )
關鍵點:
xyz
坐標傳遞到插值器時,將其轉換為rtp
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.