【发布时间】:2017-06-08 15:09:08
【问题描述】:
我有一个数组,表示三维空间中云水浓度的值。在云水浓度高于某个阈值的地方,我说我有云(见下面的横截面)。大部分领域是干燥的,但在大部分领域都有层积云,底部在 400 米左右。
我要做的是提取云底和云顶的 (x,y,z) 坐标。然后我想在代表风速垂直分量的不同三维数组上使用这些坐标来获得云底的上升气流。
我现在正在做的工作有效但速度很慢。我觉得一定有办法利用 NumPy 来加快速度。
这就是我现在正在做的事情:
# 3d array representing cloud water at a particular timestep t
qc = QC(t)
# get the coordinates where there is cloud
cloud_coords = argwhere( qc > qc_thresh )
# Arrays to hold the z values of cloud base (cb) and cloud top (ct)
zcb = zeros((nx,ny))
zct = zeros((nx,ny))
# Since each coordinate (x,y) will in general have multiple z values
# for cloud I have to loop over all (x,y) and
# pull out max and min height for each point (x,y)
for x in range(nx):
# Pull out all the coordinates with a given x value
xslice = cloud_coords[ where(cloud_coords[:,0] == x) ]
for y in range(ny):
# for the given x value select a particular y value
column = xslice[ where(xslice[:,1] == y) ]
try:
zcb[x,y] = min( column[:,2] )
zct[x,y] = max( column[:,2] )
except:
# Because there may not be any cloud at all
# (a "hole") we fill the array with an average value
zcb[x,y] = mean(zcb[zcb.nonzero()])
zct[x,y] = mean(zct[zct.nonzero()])
# Because I intend to use these as indices I need them to be ints
zcb = array(zcb, dtype='int')
zct = array(zct, dtype='int')
输出是一个二维数组,包含云底(和顶部)的 z 坐标
然后我在另一个数组上使用这些索引来获取云底的风速等变量:
wind = W(t)
j,i = meshgrid(arange(ny),arange(nx))
wind_base = wind[i,j,zcb]
我在模拟中的许多时间步都这样做,最慢的部分是所有 (x,y) 坐标上的 python 循环。任何有关使用 NumPy 更快地提取这些值的帮助将不胜感激!
【问题讨论】:
标签: python arrays numpy vectorization array-broadcasting