HEALPix neighbours, interpolation, spherical queries, spatial MOC and scalar maps
moon add chenliyi-cly/moonhealpix@0.2.2moonc -v
moon version
moon check
moon test --target js
moon test --target wasm
moon test --target wasm-gc
moon run examples/catalog --target js
moon run examples/observations --target js
moon run examples/simulation --target js
moon run examples/spatial --target jsimport {
"chenliyi-cly/moonhealpix" @hp,
}
options(is_main: true)fn main {
try {
let grid = @hp.Grid::new(8)
let point = @hp.LonLat::degrees(120.0, 30.0)
let pixel = grid.index(point, @hp.Nested)
let ring = grid.convert(pixel, @hp.Nested, @hp.Ring)
assert_eq(grid.convert(ring, @hp.Ring, @hp.Nested), pixel)
let adjacent = grid.neighbours(pixel, @hp.Nested)
assert_eq(adjacent.length(), 8)
let field = @hp.ScalarMap::filled(grid, @hp.Nested, Some(2.0), 1000)
let value = field.interpolate_bilinear(point).unwrap()
assert_true((value - 2.0).abs() < 1.0e-12)
println("像素编号:\{pixel},插值:\{value}")
} catch {
error => abort("示例失败:\{error}")
}
}| 项目 | 约定 |
|---|---|
| 分辨率 | Grid::new(nside) 接受 1..8192 内的二次幂;Grid::from_level 接受 0..13,nside = 2^level。 |
| 编号 | 零基 Int32,范围 [0, 12*nside*nside);RING 与 NESTED 必须显式转换。Cell 和目录桶使用 NESTED。 |
| 角度 | LonLat::new 使用弧度,LonLat::degrees 使用度;经度环绕到 [0, 2π),非法纬度与非有限值报错。查询半径和命中距离均为弧度。 |
| 面积与尺度 | 面积单位是球面度;resolution() 是像素面积平方根,不能当作像素包围半径。 |
| 区间 | PixelRange 为半开区间 [start, end);PixelSet 保存网格和顺序,规范化为有序、不重叠且不相接的区间。 |
| 缺失值 | None 表示缺失,Some(0.0) 是有效零;有效值必须有限,不能用 NaN 充当缺失值。 |
| 参考系 | 普通经纬度不带参考系转换能力。MOC 约定为 ICRS,调用者必须先统一坐标系与历元。 |
| 调用 | 输出顺序 | 几何含义 |
|---|---|---|
| query_disc、query_box | RING | 区域内的像素中心集合,沿纬圈查询。 |
| query_polygon | NESTED | 多边形内的像素中心集合。 |
| search_disc、search_polygon | NESTED | 层级剪枝后的中心集合,另有 visited() 访问计数。 |
| cover_disc、cover_polygon | NESTED | 与区域可能相交的曲边像素覆盖候选,通过 RegionCover 返回。 |
let grid = @hp.Grid::new(16)
let polygon = @hp.SphericalPolygon::new([
@hp.LonLat::degrees(350.0, -10.0),
@hp.LonLat::degrees(15.0, -10.0),
@hp.LonLat::degrees(15.0, 12.0),
@hp.LonLat::degrees(350.0, 12.0),
], 16)
let cover = grid.cover_polygon(polygon, 3, grid.npix(), 200000)
let limits = @hp.MocLimits::default()
let moc = @hp.Moc::from_pixel_set(cover.pixels(), grid.level(), limits)
let text = moc.to_ascii(limits)
assert_true(@hp.Moc::from_ascii(text, limits).equals(moc))| 示例输入 | 结果及计数 |
|---|---|
| nside=16,上述多边形,额外细分三层 | 候选 65,全内 13,未决 52,访问 902 次。 |
| nside=8192,编号 268435456 的像素中心,零半径查询 | 命中该中心,访问 352 次;网格共有 805306368 个像素,无需创建全图数组。 |
| nside=64 目录,12000 个确定性分布源,以第 6000 个源为中心、半径 0.02 弧度 | 命中 1,访问树节点 249,逐项检查源 1;工作计数为 250,结果与逐源暴力检查一致。 |
pub struct Catalog {
grid : Grid
sources : Array[LonLat]
indices : Array[(Int, Int)]
bins : Array[CatalogBin]
tree : Array[CatalogNode]
}fn Catalog::bin_values(self : Catalog, values : Array[Double?], weights : Array[Double], max_pixels : Int) -> BinnedStats raise HealpixErrorfn Catalog::pixel_sources(self : Catalog, nested_pixel : Int, max_sources : Int) -> Array[Int] raise HealpixErrorfn Catalog::search_cone(self : Catalog, center : LonLat, radius : Double, max_hits : Int, max_work : Int) -> CatalogSearch raise HealpixErrorfn Grid::convert(self : Grid, pixel : Int, from : Ordering, to : Ordering) -> Int raise HealpixErrorfn Grid::cover_disc(self : Grid, center : LonLat, radius : Double, refine_levels : Int, max_pixels : Int, max_work : Int) -> RegionCover raise HealpixErrorfn Grid::cover_polygon(self : Grid, polygon : SphericalPolygon, refine_levels : Int, max_pixels : Int, max_work : Int) -> RegionCover raise HealpixErrorfn Grid::pixel_position(self : Grid, pixel : Int, dx : Double, dy : Double) -> LonLat raise HealpixErrorfn Grid::query_box(self : Grid, region : LonLatBox, max_pixels : Int, max_work : Int) -> PixelSet raise HealpixErrorfn Grid::query_disc(self : Grid, center : LonLat, radius : Double, max_pixels : Int, max_work : Int) -> PixelSet raise HealpixErrorfn Grid::query_polygon(self : Grid, polygon : SphericalPolygon, max_pixels : Int, max_work : Int) -> PixelSet raise HealpixErrorfn Grid::search_disc(self : Grid, center : LonLat, radius : Double, max_pixels : Int, max_work : Int) -> PixelSearch raise HealpixErrorfn Grid::search_polygon(self : Grid, polygon : SphericalPolygon, max_pixels : Int, max_work : Int) -> PixelSearch raise HealpixErrorpub struct LonLatBox {
west : Double
width : Double
south : Double
north : Double
}fn LonLatBox::degrees(west : Double, width : Double, south : Double, north : Double) -> LonLatBox raise HealpixErrorfn LonLatBox::new(west : Double, width : Double, south : Double, north : Double) -> LonLatBox raise HealpixErrortype Mocfn Moc::from_pixel_set(set : PixelSet, max_order : Int, limits : MocLimits) -> Moc raise HealpixErrorpub struct MocLimits {
max_ranges : Int
max_cells : Int
max_chars : Int
}fn MocLimits::new(max_ranges : Int, max_cells : Int, max_chars : Int) -> MocLimits raise HealpixErrorfn PixelSet::new(grid : Grid, ordering : Ordering, ranges : Array[PixelRange]) -> PixelSet raise HealpixErrorfn PixelSet::reorder(self : PixelSet, ordering : Ordering, max_pixels : Int) -> PixelSet raise HealpixErrorfn ScalarMap::filled(grid : Grid, ordering : Ordering, value : Double?, max_pixels : Int) -> ScalarMap raise HealpixErrorfn ScalarMap::resample(self : ScalarMap, target : Grid, mode : ResampleMode, max_pixels : Int) -> ScalarMap raise HealpixErrorfn ScalarMap::with_value(self : ScalarMap, pixel : Int, value : Double?) -> ScalarMap raise HealpixErrorfn SphericalPolygon::new(vertices : Array[LonLat], max_vertices : Int) -> SphericalPolygon raise HealpixErrorInstall
Download zipHEALPix neighbours, interpolation, spherical queries, spatial MOC and scalar maps