summaryrefslogtreecommitdiff
path: root/tools/sky/build-stars.py
blob: 061cea8711b8cc3e67f255a5b6a5cb5d226316ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
# Packs the Yale Bright Star Catalog (5th ed.) into public/assets/sky/stars.bin
# for the sky component: four little-endian float32s per star, right ascension
# and declination (J2000, radians), V magnitude and B-V colour index.
#
#   curl -O http://tdc-www.harvard.edu/catalogs/bsc5.dat.gz
#   python3 tools/sky/build-stars.py bsc5.dat.gz public/assets/sky/stars.bin
#
# Byte columns follow the catalog's ReadMe (CDS V/50).
import gzip
import math
import struct
import sys


def main(src, dst):
    opener = gzip.open if src.endswith('.gz') else open
    stars = []
    with opener(src, 'rt', encoding='ascii', errors='replace') as f:
        for line in f:
            line = line.rstrip('\n').ljust(197)
            if not line[75:77].strip() or not line[102:107].strip():
                continue  # the 14 entries that turned out not to be stars
            ra = (int(line[75:77]) + int(line[77:79]) / 60 + float(line[79:83]) / 3600) * 15
            dec = int(line[84:86]) + int(line[86:88]) / 60 + int(line[88:90]) / 3600
            if line[83] == '-':
                dec = -dec
            vmag = float(line[102:107])
            bv = float(line[109:114]) if line[109:114].strip() else 0.6
            stars.append((math.radians(ra), math.radians(dec), vmag, bv))
    with open(dst, 'wb') as out:
        for s in stars:
            out.write(struct.pack('<4f', *s))
    print(f'{len(stars)} stars, {min(s[2] for s in stars):.2f} to {max(s[2] for s in stars):.2f} mag -> {dst}')


if __name__ == '__main__':
    main(sys.argv[1], sys.argv[2])