From 614fa8b5f59dc7f956b0f99c3605ed08617f55f7 Mon Sep 17 00:00:00 2001 From: Brandon Rhodes Date: Fri, 24 Jul 2020 05:34:25 -0400 Subject: [PATCH] Fix #143: routine to read Stellarium star names --- skyfield/data/stellarium.py | 26 ++++++++++++++++++++++++++ skyfield/named_stars.py | 1 + skyfield/tests/test_io_parsing.py | 22 ++++++++++++++++++++-- 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/skyfield/data/stellarium.py b/skyfield/data/stellarium.py index 049ff62..2107710 100644 --- a/skyfield/data/stellarium.py +++ b/skyfield/data/stellarium.py @@ -1,5 +1,9 @@ """Parse Stellarium data files.""" +from collections import namedtuple + +StarName = namedtuple('StarName', 'hip name') + def parse_constellations(lines): """Return a list of constellation outlines. @@ -29,3 +33,25 @@ def parse_constellations(lines): for i in range(2, len(fields), 2)] constellations.append((name.decode('utf-8'), edges)) return constellations + +def parse_star_names(lines): + """Return the names in a Stellarium ``star_names.fab`` file. + + Returns a list of named tuples, each of which offers a ``.hip`` + attribute with a Hipparcos catalog number and a ``.name`` attribute + with the star name. Do not depend on the tuple having only length + two; additional fields may be added in the future. + + """ + names = [] + for line in lines: + line = line.strip() + if line == b'' or line.startswith(b'#'): + continue + fields = line.split() + hip, name = fields[0].split(b'|') + names.append(StarName( + int(hip), + name.strip(b'_(")').decode('utf-8'), + )) + return names diff --git a/skyfield/named_stars.py b/skyfield/named_stars.py index 7d4b493..9347e00 100644 --- a/skyfield/named_stars.py +++ b/skyfield/named_stars.py @@ -1,3 +1,4 @@ +# TODO: Deprecate; maybe even remove, since it's not documented? """ Convenience functions for users to get a Star instance using a small database of named stars. diff --git a/skyfield/tests/test_io_parsing.py b/skyfield/tests/test_io_parsing.py index a21c101..783056a 100644 --- a/skyfield/tests/test_io_parsing.py +++ b/skyfield/tests/test_io_parsing.py @@ -2,7 +2,7 @@ import gzip from skyfield import iokit -from skyfield.data.hipparcos import load_dataframe +from skyfield.data import hipparcos, stellarium from skyfield.functions import BytesIO from skyfield.iokit import parse_tle @@ -125,7 +125,7 @@ def test_hipparcos(): g.close() b.seek(0) try: - df = load_dataframe(b) + df = hipparcos.load_dataframe(b) except ImportError: # raise SkipTest('pandas not available') # Assay doesn't understand skipping tests yet; just pass @@ -135,3 +135,21 @@ def test_hipparcos(): row = df.iloc[0] assert abs(row.ra_degrees - 000.00091185) < 1e-30 assert abs(row.dec_degrees - +01.08901332) < 1e-30 + +star_text = b"""\ +# star names by constellation +# Andromeda (And) + 677|_("Alpheratz") 1,2,5,6,11,12 + 677|_("Sirrah") + 5447|_("Mirach") 1,2,5,6,11,12,23 + 9640|_("Almach") 1,2,5,6,11,12 + 9640|_("Almaak") +""" + +def test_stellarium_star_names(): + f = BytesIO(star_text) + star_names = stellarium.parse_star_names(f) + assert star_names[0].hip == 677 + assert star_names[0].name == 'Alpheratz' + assert star_names[4].hip == 9640 + assert star_names[4].name == 'Almaak'