我正在使用healpy.query_polygon
来获取多边形内的healpix索引列表。根据文档:
顶点:包含多边形(形状为(N,3))的顶点的顶点数组。
但是当我尝试从以下多边形中获取所有索引时,会出现RuntimeError: Unknown exception
:
在[1]中:
import healpy as hp
vertex_array = np.array([[0.65, -0.04, 0.76], [0.58, 0.38, 0.72], [0.91, -0.29, 0.31],[0.91, 0.18, 0.38]])
print(vertex_array.shape)
vertex_array
出[1]:
(4, 3)
array([[ 0.65, -0.04, 0.76],
[ 0.58, 0.38, 0.72],
[ 0.91, -0.29, 0.31],
[ 0.91, 0.18, 0.38]])
在[2]中:
healpix_indexes_test = hp.query_polygon(4, vertex_array)
出[2]:
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-63-5a14f69cb078> in <module>
----> 1 healpix_indexes_test = hp.query_polygon(4, vertex_array)
healpy/src/_query_disc.pyx in healpy._query_disc.query_polygon()
RuntimeError: Unknown exception
Here,您可以看到球形的这些点的可视化。
只是出于乐趣,我尝试对输入数组进行转置,因此其形状变为(3,4)。 Unknown exception
问题消失了。但是这样的输入与文档相矛盾,所以我不相信。
在[1]中:
print(vertex_array.T.shape)
vertex_array.T
出[1]:
(3, 4)
array([[ 0.65, 0.58, 0.91, 0.91],
[-0.04, 0.38, -0.29, 0.18],
[ 0.76, 0.72, 0.31, 0.38]])
在[2]中:
healpix_indexes_test_1 = hp.query_polygon(4, vertex_array.T)
healpix_indexes_test_1
出[2]:
array([ 42, 58, 75, 107, 123, 140])
如果有任何建议,我将不胜感激。
答案 0 :(得分:2)
With the help of Healpy members on Github, the solution was found. It's important to define the order of the vertices correctly. In my case it means that the last two dot's coordinates should be swopped to make the rectangle simple, not self-crossing:
In[1]:
vertex_array_fixed = np.array([[0.65, -0.04, 0.76], [0.58, 0.38, 0.72], [0.91, 0.18, 0.38], [0.91, -0.29, 0.31]])
print(vertex_array_fixed.shape)
vertex_array_fixed
Out[1]:
(4, 3)
array([[ 0.65, -0.04, 0.76],
[ 0.58, 0.38, 0.72],
[ 0.91, 0.18, 0.38],
[ 0.91, -0.29, 0.31]])
In[2]:
healpix_indexes_test = hp.query_polygon(4, vertex_array_fixed)
healpix_indexes_test
Out[2]:
array([24, 40, 71])
Here comes the visualisation:
In[3]:
Nside = 2048
healpix_indexes_test = hp.query_polygon(Nside, vertex_array_fixed)
healpix_indexes_test
Out[3]:
array([ 5968575, 5968576, 5968577, ..., 17387119, 17387120, 17395310])
In[4]:
Npix = hp.nside2npix(Nside)
whole_map = np.arange(Npix, dtype=float)
whole_map[healpix_indexes_test] = hp.UNSEEN
hp.mollview(m, title="Fixed rectangle")
Out[4]: Output plot