git clone https://garage44.eu/jeroen/cartopolis.git.. /tools/gen-near-stars.ts
// The star systems within 16 light years, for the zoom ladder's Stars rung
// (`crates/cartopolis/src/utils/near_stars.rs`): one row per system, named for
// its proper name or catalogue entry, at its primary's equatorial position.
// Source: the HYG database v3.8 (Astronexus, CC BY-SA 4.0), the catalogue the
// sky's `bright_stars.bin` comes from.
//
// Usage: bun tools/gen-near-stars.ts hyg_v38.csv.gz > crates/cartopolis/src/utils/near_stars.rs
import { gunzipSync } from "bun";
const LY_PER_PC = 3.261563777;
const MAX_LY = 16;
const raw = gunzipSync(await Bun.file(Bun.argv[2]).arrayBuffer());
const lines = new TextDecoder().decode(raw).split("\n");
const head = lines[0].split(",").map((h) => h.replaceAll('"', ""));
const col = (n: string) => head.indexOf(n);
const parse = (l: string) => {
const out: string[] = []; let cur = "", q = false;
for (const ch of l) { if (ch === '"') q = !q; else if (ch === "," && !q) { out.push(cur); cur = ""; } else cur += ch; }
out.push(cur); return out;
};
type Star = { id: string; name: string; dist: number; x: number; y: number; z: number; spect: string; mag: number; primary: string };
const stars: Star[] = [];
for (const l of lines.slice(1)) {
if (!l) continue;
const r = parse(l);
const dist = +r[col("dist")];
if (!(dist > 0) || dist * LY_PER_PC > MAX_LY || dist >= 100000) continue;
// Proper name, else Bayer letter or Flamsteed number with the constellation,
// else the Gliese, Hipparcos or HD number.
const v = (n: string) => r[col(n)].trim().replace(/\s+/g, " ");
const con = v("con"), bayer = v("bayer"), flam = v("flam"), gl = v("gl");
const name = v("proper") || (bayer && con ? `${bayer} ${con}` : "") || (flam && con ? `${flam} ${con}` : "")
|| (gl ? gl.replace(/^Gl /, "Gliese ") : "") || (v("hip") ? `HIP ${v("hip")}` : "") || (v("hd") ? `HD ${v("hd")}` : `HYG ${v("id")}`);
stars.push({ id: r[col("id")], name,
dist, x: +r[col("x")], y: +r[col("y")], z: +r[col("z")], spect: r[col("spect")], mag: +r[col("absmag")], primary: r[col("comp_primary")] });
}
// One row per system: the brightest member stands for it.
const systems = new Map<string, Star[]>();
for (const s of stars) systems.set(s.primary, [...(systems.get(s.primary) ?? []), s]);
const rows = [...systems.values()].map((m) => { m.sort((a, b) => a.mag - b.mag); return { ...m[0], members: m.length }; })
.sort((a, b) => a.dist - b.dist);
const f = (n: number) => n.toFixed(3);
console.log(`//! The star systems within ${MAX_LY} light years: generated by \`tools/gen-near-stars.ts\`
//! from the HYG database v3.8 (Astronexus, CC BY-SA 4.0 — \`assets/sky/CREDITS.md\`).
//! One row per system, at its brightest member: name, distance (light years),
//! equatorial x, y, z (parsecs; x towards RA 0h, z towards the north celestial
//! pole), members listed. Do not edit by hand.
/// One star system near the Sun.
pub struct NearStar {
pub name: &'static str,
pub light_years: f32,
pub xyz_pc: [f32; 3],
pub members: u8,
}
pub const NEAR_STARS: &[NearStar] = &[`);
for (const r of rows) {
const ly = r.dist * LY_PER_PC;
console.log(` NearStar { name: ${JSON.stringify(r.name)}, light_years: ${ly.toFixed(2)}, xyz_pc: [${f(r.x)}, ${f(r.y)}, ${f(r.z)}], members: ${r.members} },`);
}
console.log("];");