outfly/tools/generate_starchart.py

67 lines
2.1 KiB
Python
Executable file

#!/usr/bin/env python
# This script requires the following file in the current directory:
# https://github.com/astronexus/HYG-Database/blob/cbd21013d2bb89732b893be357a6f41836dbe614/hyg/CURRENT/hygdata_v41.csv
import csv
import sys
import math
# Lower apparent magnitude = brighter.
# Sun = -26.7
# Sirius = -1.44
# Betelgeuse = 0.45
# Polaris (north star) = 1.97
# Meissa (orion's head, 8th brightest star in orion) = 3.33
MAX_APPARENT_MAGNITUDE = 7.0
print("// This file was autogenerated by 'genrate_starchart.py' using data from")
print("// the HYG database: https://github.com/astronexus/HYG-Database/tree/main/hyg")
print("// License: CC BY-SA 4.0: https://creativecommons.org/licenses/by-sa/4.0/")
print("")
print("pub const STARS: &[[f32; 5]] = &[")
def render(ra, dec, mag, proper):
ra = float(ra)
dec = float(dec)
mag = float(mag)
distance = 1.0
ra_radians = math.radians(ra * 15) # ra is in [0, 24], multiplying by 15 gives degrees in [0, 360]
dec_radians = math.radians(dec)
#print(f"ra_radians={ra_radians}, dec_radians={dec_radians}, dec={dec}, ra={ra}", file=sys.stderr)
x = distance * math.cos(dec_radians) * math.cos(-ra_radians)
y = distance * math.cos(dec_radians) * math.sin(-ra_radians)
z = distance * math.sin(dec_radians)
# Correct for differences in coordinate system axes
x, y, z = x, z, y
comment = f" // {proper}" if proper else ""
brightness = 2.512 ** (0 - mag)
print(f" [{x:.04}, {y:.04}, {z:.04}, {brightness:.04}, {ci}],{comment}")
total = 0
count = 0
with open("hygdata_v41.csv", "r", encoding="utf-8") as f:
for entry in csv.DictReader(f):
total += 1
ra = entry['ra']
dec = entry['dec']
mag = entry['mag']
ci = entry['ci']
proper = entry['proper']
proper = "".join([c for c in proper if c.isalnum() or c in ' -_'])
if not all([ra, dec, mag, ci]):
continue
if float(mag) > MAX_APPARENT_MAGNITUDE:
continue
render(ra, dec, mag, proper)
count += 1
print("];")
print(f"Wrote {count} stars (total={total}).", file=sys.stderr)