HEALPix
HEALPix 具有以下三个基本特性:

-
球体被分层镶嵌成曲线四边形。最低分辨率分区由 12 个基础像素组成。镶嵌的分辨率通过将每个像素划分为四个新像素来提高。下图(从左上到左下顺时针方向)展示了分辨率从基础级别开始逐级提升的过程(即球体分别被划分为 12、48、192 和 768 个像素)。
-
给定分辨率下所有像素的面积都是相同的。
-
像素分布在等纬度线上。此属性对于所有涉及球谐函数的谐波分析应用至关重要。由于采样点的等纬度分布,单个球谐函数的积分计算速度与像素总数呈约 N 1/2的比例关系,而非等纬度采样分布(例如 用于 NASA 宇宙 探测卫星 (COBE)数据的 四边形球面立方体 ,以及任何基于二十面体对称性的分布)则与约 N 的比例关系。

HEALPix 在球面上使用标准的球坐标:
-
θ (theta):余纬(colatitude),从北极到南极,范围 [0, π],(θ 从北极向南增加)
-
φ (phi):经度,范围 [0, 2π]
转换关系如下,这两个函数是一对“互逆变换”:
-
pix2ang:告诉你第 ipix 个像素中心点的球面坐标。
-
ang2pix:告诉你某个经纬点属于哪个像素编号。
import healpy as hp
theta, phi = hp.pix2ang(nside, ipix) # pixel -> (θ, φ)
ipix = hp.ang2pix(nside, theta, phi) # (θ, φ) -> pixel
HEALPix 各行(纬圈)是等纬度分布的(isoLatitude),但每个纬圈上的像素数量不同:
-
赤道附近像素数量最多;
-
接近两极时每圈像素数量减少;
-
但每个像素面积仍然相同。
把 HEALPix 与经纬度映射起来:

转换pix2ang
cell = pr_cntl.cell.values
nside = int(np.sqrt(len(cell) / 12))
print(f"nside = {nside}")
lon, lat = hp.pix2ang(nside, cell, nest=False,lonlat=True)
print(lon, lat )

获取nside和npix的其他方法
order = zoom = 8
nside = hp.order2nside(order)
npix = hp.nside2npix(nside)
print(nside,npix)
获取区域的lon&lat
hp_lon, hp_lat = hp.pix2ang(nside, np.arange(npix), nest=False, lonlat=True)
hp_lon = (hp_lon + 180) % 360 - 180
extent = [0, 360, -15, 15]
# Find all grid points within the defined extent
icell, = np.where(
(hp_lon > extent[0]) &
(hp_lon < extent[1]) &
(hp_lat > extent[2]) &
(hp_lat < extent[3])
)
print(hp_lon[icell],hp_lat[icell],icell)
print(f"纬度范围: {hp_lat[icell].min():.2f} ~ {hp_lat[icell].max():.2f}")
print(f"经度范围: {lon.min():.2f} ~ {lon.max():.2f}")

绘制区域空间分布图
def get_axis_tropics():
fig, ax = plt.subplots(
figsize=(10, 4),
subplot_kw={"projection": ccrs.PlateCarree(180)}
)
ax.coastlines(linewidth=1)
ax.add_feature(cfeature.BORDERS, linewidth=0.5)
ax.add_feature(cfeature.LAND, facecolor="white")
ax.add_feature(cfeature.OCEAN, facecolor="lightblue")
# ✅ 用 -180~180,而不是 0~360
ax.set_extent([-180, 180, -15, 15], crs=ccrs.PlateCarree())
gl = ax.gridlines(draw_labels=True, linewidth=0.3, color="gray", alpha=0.6, linestyle="--")
gl.top_labels = False
gl.right_labels = False
return fig, ax
def plot_extent_box(ax, extent, edgecolor="m", linewidth=2, label=None):
rect = plt.Rectangle(
xy=[extent[0], extent[2]],
width=extent[1] - extent[0],
height=extent[3] - extent[2],
facecolor="none",
edgecolor=edgecolor,
linewidth=linewidth,
transform=ccrs.PlateCarree(),
)
ax.add_patch(rect)
if label:
ax.text(
extent[0] + 1,
extent[3] - 1,
label,
transform=ccrs.PlateCarree(),
fontsize=10,
color=edgecolor,
weight="bold",
)
return rect
# 示例
fig, ax = get_axis_tropics()
extent = [0, 360, -15, 15]
plot_extent_box(ax, extent, edgecolor="crimson", linewidth=2, label="")
plt.title("Tropical Region (±15°) with Highlighted Box", fontsize=14)
plt.tight_layout()
plt.show()
