outfly/tools/generate_starchart.py
2024-03-18 23:49:47 +01:00

86 lines
2.7 KiB
Python
Executable file

#!/usr/bin/env python
# This script requires the following file in the assets/tmp/ directory:
# https://github.com/astronexus/HYG-Database/blob/cbd21013d2bb89732b893be357a6f41836dbe614/hyg/CURRENT/hygdata_v41.csv
import csv
import sys
import math
#def entry(ra_hour, ra_min, ra_sec, dec_deg, dec_min, dec_sec, mag, name, color_index):
# return [
# ra_hour*15 + ra_min*0.25 + ra_sec*0.004166,
# dec_deg + dec_min/60 + dec_sec/3600,
# mag,
# name,
# color_index,
# ]
#
#
#CUSTOM_ENTRIES = [
# entry(0, 42, 44.3, 41, 16, 9, 3.44-15, 0.63, "Andromeda Galaxy"),
#]
# 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, ci, proper):
# Takes ra/deg in degrees
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
count_extra = 0
with open("assets/tmp/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, ci, proper)
count += 1
#for entry in CUSTOM_ENTRIES:
# render(entry[0], entry[1], entry[2], entry[3], entry[4])
# count_extra += 1
print("];")
print(f"Wrote {count} stars (total={total}) + {count_extra} extra entries.", file=sys.stderr)