outfly/doc/scripts/generate_starchart.py
2024-04-14 22:34:12 +02:00

103 lines
2.9 KiB
Python
Executable file

#!/usr/bin/env python
# This script requires the following file in the extra/ directory:
# https://github.com/astronexus/HYG-Database/blob/cbd21013d2bb89732b893be357a6f41836dbe614/hyg/CURRENT/hygdata_v41.csv
import csv
import sys
import math
import re
PATH = "extra/hygdata_v41.csv"
#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 the")
print("// 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("// Each star's values: (x, y, z, magnitude, color index, distance, name)")
print("[")
def render(ra, dec, mag, ci, dist, name):
# Takes ra/deg in degrees
ra = float(ra)
dec = float(dec)
mag = float(mag)
name = re.sub(r'\s+', ' ', name)
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
#brightness = 2.512 ** (0 - mag)
print(f'({x:.04},{y:.04},{z:.04},{mag:.04},{ci},{dist},"{name}"),')
def clean_name(string):
return "".join([c for c in string if c.isalnum() or c in ' -_'])
total = 0
count = 0
count_extra = 0
entries = []
with open(PATH, "r", encoding="utf-8") as f:
for entry in csv.DictReader(f):
entries.append(entry)
entries.sort(key=lambda entry: float(entry['mag']))
for entry in entries:
total += 1
ra = entry['ra']
dec = entry['dec']
mag = entry['mag']
ci = entry['ci']
dist = entry['dist']
name = clean_name(entry['proper'])
if not name:
name = clean_name(entry['bf'])
if not name:
name = clean_name(entry['gl'])
if not all([ra, dec, mag, ci]):
continue
if float(mag) > MAX_APPARENT_MAGNITUDE:
continue
render(ra, dec, mag, ci, dist, name)
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)