From a1fb05849b1f4f3643cfdfc4fd51cf6f55ced405 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 23 Oct 2026 18:41:16 -0700 Subject: [PATCH] fix(catalog-import): load the Messier names as preferred names, title-case NGC 2000 names, add the Magellanic globulars to EGC The Messier names reader took parts[2] of a two-field file, so no Messier name loaded. messier_names.dat is now MName; Name (curated: NGC prefixes dropped, descriptions and designations removed, M 33 "Triangulum Galaxy" only, M 44 "Beehive Cluster; Praesepe"), and a post-processing step puts each object's Messier names first, so the first is its preferred name. That demotes "Great Nebula in Andromeda" and "Great Nebula in Orion". NGC 2000.0's names.dat is read as published but title-cased, by the atlas's rule: small words after the first stay lower case. extra_names.dat is gone: M 24 takes "IC 4715" as an aka, M 45's "Pleiades" comes from messier_names.dat, and M 102 already has "NGC 5866". EGC gains NGC 1754, 1786, 1898, 2210, 2257 (old) and NGC 1783, 1846, 1978, 2154, 2173, 419 (1.2-2.1 Gyr): a cluster 1 Gyr or older is a globular. Positions and sizes are Bica et al. 2008's, magnitudes Steinicke's. Refs #670 Co-Authored-By: Claude Opus 5.5 --- astro_data/EGC.tsv | 13 +- astro_data/extra_names.dat | 3 - astro_data/ngc_ic_m/messier/messier_names.dat | 141 +++------- docs/ax/catalog.md | 12 +- python/PiFinder/catalog_imports/main.py | 1 + .../catalog_imports/post_processing.py | 174 +++++++----- .../catalog_imports/steinicke_loader.py | 5 + python/tests/test_catalog_names.py | 261 ++++++++++++++++++ 8 files changed, 427 insertions(+), 183 deletions(-) delete mode 100644 astro_data/extra_names.dat create mode 100644 python/tests/test_catalog_names.py diff --git a/astro_data/EGC.tsv b/astro_data/EGC.tsv index 62d85058b..238ec676c 100644 --- a/astro_data/EGC.tsv +++ b/astro_data/EGC.tsv @@ -89,4 +89,15 @@ EGC Index Other Names RA DEC Mag Size Constellation Desc 88 U49 01 33 48 +30 48 30 16.3 Tri Globular Cluster of M33 89 Hubble VII 19 44 57 -14 49 30 16.3 Sgr Globular Cluster of NGC 6822 90 Hodge V 00 39 12 +48 23 30 16.7 Cas Globular Cluster of NGC 185 -91 Hodge III 00 33 18 +48 27 30 16.8 Cas Globular Cluster of NGC 147 \ No newline at end of file +91 Hodge III 00 33 18 +48 27 30 16.8 Cas Globular Cluster of NGC 147 +92 NGC 1754 04 54 17 -70 26 29 12.0 1.6' Men Globular Cluster of LMC +93 NGC 1786 04 59 06 -67 44 42 10.9 2.0' Dor Globular Cluster of LMC +94 NGC 1898 05 16 41 -69 39 23 11.9 1.6' Dor Globular Cluster of LMC +95 NGC 2210 06 11 31 -69 07 18 10.2 3.3' Dor Globular Cluster of LMC +96 NGC 2257 06 30 12 -64 19 34 13.5 4.0' Dor Globular Cluster of LMC +97 NGC 1783 04 59 08 -65 59 18 10.9 5.3'x4.7' Dor Globular Cluster of LMC +98 NGC 1846 05 07 35 -67 27 39 11.3 3.8' Dor Globular Cluster of LMC +99 NGC 1978 05 28 45 -66 14 10 10.7 4.0'x2.7' Dor Globular Cluster of LMC +100 NGC 2154 05 57 38 -67 15 43 11.8 2.4' Dor Globular Cluster of LMC +101 NGC 2173 05 57 58 -72 58 43 11.9 2.6' Men Globular Cluster of LMC +102 NGC 419 01 08 19 -72 53 03 11.2 2.8' Tuc Globular Cluster of SMC \ No newline at end of file diff --git a/astro_data/extra_names.dat b/astro_data/extra_names.dat deleted file mode 100644 index 7a8ecd914..000000000 --- a/astro_data/extra_names.dat +++ /dev/null @@ -1,3 +0,0 @@ -Pleiades M 45 -M 24 I4715 -M 102 5866 diff --git a/astro_data/ngc_ic_m/messier/messier_names.dat b/astro_data/ngc_ic_m/messier/messier_names.dat index 3ffbc875e..e1cd61af9 100644 --- a/astro_data/ngc_ic_m/messier/messier_names.dat +++ b/astro_data/ngc_ic_m/messier/messier_names.dat @@ -1,110 +1,31 @@ -M1 NGC 1952 Crab Nebula -M2 NGC 7089 -M3 NGC 5272 -M4 NGC 6121 -M5 NGC 5904 -M6 NGC 6405 Butterfly Cluster -M7 NGC 6475 Ptolemy's Cluster -M8 NGC 6523 Lagoon Nebula -M9 NGC 6333 -M10 NGC 6254 -M11 NGC 6705 Wild Duck Cluster -M12 NGC 6218 -M13 NGC 6205 Hercules Globular Cluster -M14 NGC 6402 -M15 NGC 7078 -M16 NGC 6611 Eagle Nebula Cluster -M17 NGC 6618 Omega, Swan, Horseshoe Nebula -M18 NGC 6613 -M19 NGC 6273 -M20 NGC 6514 Trifid Nebula -M21 NGC 6531 -M22 NGC 6656 -M23 NGC 6494 -M24 NGC 6603 Milky Way Patch -M25 IC 4725 -M26 NGC 6694 -M27 NGC 6853 Dumbbell Nebula -M28 NGC 6626 -M29 NGC 6913 -M30 NGC 7099 -M31 NGC 224 Andromeda Galaxy -M32 NGC 221 Satellite of M31 -M33 NGC 598 Triangulum/Pinwheel Galaxy -M34 NGC 1039 -M35 NGC 2168 -M36 NGC 1960 -M37 NGC 2099 -M38 NGC 1912 -M39 NGC 7092 -M40 Winecke 4 -M41 NGC 2287 -M42 NGC 1976 Great Orion Nebula -M43 NGC 1982 de Mairan's Nebula -M44 NGC 2632 Beehive Cluster (Praesepe) -M45 Pleiades, Subaru, Seven Sisters -M46 NGC 2437 -M47 NGC 2422 -M48 NGC 2548 -M49 NGC 4472 -M50 NGC 2323 -M51 NGC 5194 Whirlpool Galaxy -M52 NGC 7654 -M53 NGC 5024 -M54 NGC 6715 -M55 NGC 6809 -M56 NGC 6779 -M57 NGC 6720 Ring Nebula -M58 NGC 4579 -M59 NGC 4621 -M60 NGC 4649 -M61 NGC 4303 -M62 NGC 6266 -M63 NGC 5055 Sunflower Galaxy -M64 NGC 4826 Blackeye Galaxy -M65 NGC 3623 -M66 NGC 3627 -M67 NGC 2682 -M68 NGC 4590 -M69 NGC 6637 -M70 NGC 6681 -M71 NGC 6838 -M72 NGC 6981 -M73 NGC 6994 Group of 4 stars -M74 NGC 628 -M75 NGC 6864 -M76 NGC 650 Little Dumbell, Cork, Butterfly Nebula -M77 NGC 1068 Cetus A -M78 NGC 2068 -M79 NGC 1904 -M80 NGC 6093 -M81 NGC 3031 Bode's Galaxy -M82 NGC 3034 Cigar Galaxy -M83 NGC 5236 Southern Pinwheel Galaxy -M84 NGC 4374 -M85 NGC 4382 -M86 NGC 4406 -M87 NGC 4486 Virgo A -M88 NGC 4501 -M89 NGC 4552 -M90 NGC 4569 -M91 NGC 4548 -M92 NGC 6341 -M93 NGC 2447 -M94 NGC 4736 -M95 NGC 3351 -M96 NGC 3368 -M97 NGC 3587 Owl Nebula -M98 NGC 4192 -M99 NGC 4254 -M100 NGC 4321 -M101 NGC 5457 Pinwheel Galaxy -M102 NGC 5866 Spindle Galaxy -M103 NGC 581 -M104 NGC 4594 Sombrero Galaxy -M105 NGC 3379 -M106 NGC 4258 -M107 NGC 6171 -M108 NGC 3556 -M109 NGC 3992 -M110 NGC 205 Satellite of M31 +M1 Crab Nebula +M6 Butterfly Cluster +M7 Ptolemy's Cluster +M8 Lagoon Nebula +M11 Wild Duck Cluster +M13 Hercules Globular Cluster +M16 Eagle Nebula +M17 Omega Nebula; Swan Nebula; Horseshoe Nebula +M20 Trifid Nebula +M24 Sagittarius Star Cloud +M27 Dumbbell Nebula +M31 Andromeda Galaxy +M33 Triangulum Galaxy +M42 Orion Nebula; Great Orion Nebula +M43 de Mairan's Nebula +M44 Beehive Cluster; Praesepe +M45 Pleiades; Subaru; Seven Sisters +M51 Whirlpool Galaxy +M57 Ring Nebula +M63 Sunflower Galaxy +M64 Black Eye Galaxy +M76 Little Dumbbell Nebula; Cork Nebula; Butterfly Nebula +M77 Cetus A +M81 Bode's Galaxy +M82 Cigar Galaxy +M83 Southern Pinwheel Galaxy +M87 Virgo A +M97 Owl Nebula +M101 Pinwheel Galaxy +M102 Spindle Galaxy +M104 Sombrero Galaxy diff --git a/docs/ax/catalog.md b/docs/ax/catalog.md index 1079d3334..eea028af9 100644 --- a/docs/ax/catalog.md +++ b/docs/ax/catalog.md @@ -432,7 +432,8 @@ machine; a device never runs it, and ships the committed file instead. │ join an existing sky object, or mint a new one ▼ post-processing (POST_PROCESSING_FUNCTIONS) - │ type normalisation, the hand-built Messier objects + │ type normalisation, the hand-built Messier objects, + │ the Messier names ▼ star identity: Hipparcos cross-match, merge by HIP │ (`hip` corrections are read here, ahead of position) @@ -591,9 +592,12 @@ by a correction. An object without one shows its first cross-designation. The name files: -- `ngc_ic_m/messier/messier_names.dat` is curated, one line per object - (`MName; Name`); its first name is the object's preferred - name. +- `ngc_ic_m/messier/messier_names.dat` is curated, one line per named + Messier object (`MName; Name`). A post-processing step puts a + line's names ahead of the object's other names, in order, so its first + name is the object's preferred name. A cross-designation ("Winnecke 4") + or a description ("Satellite of M31") is not a name, so it has no + place here. - `ngc_ic_m/ngc2000/names.dat` is NGC 2000.0's, published. It is read as published but title-cased on read (the atlas's rule: small words after the first stay lower case), so "Crab nebula" reads "Crab diff --git a/python/PiFinder/catalog_imports/main.py b/python/PiFinder/catalog_imports/main.py index f8530e475..ec316a942 100644 --- a/python/PiFinder/catalog_imports/main.py +++ b/python/PiFinder/catalog_imports/main.py @@ -41,6 +41,7 @@ POST_PROCESSING_FUNCTIONS = [ ("post_processing", "fix_object_types"), ("post_processing", "add_missing_messier_objects"), + ("post_processing", "add_messier_names"), ] diff --git a/python/PiFinder/catalog_imports/post_processing.py b/python/PiFinder/catalog_imports/post_processing.py index 651369216..acb85b8f6 100644 --- a/python/PiFinder/catalog_imports/post_processing.py +++ b/python/PiFinder/catalog_imports/post_processing.py @@ -6,71 +6,126 @@ """ import logging +import sqlite3 from pathlib import Path +from typing import Dict, Iterable, List, Optional, Tuple # Import shared database object from .database import objects_db from .catalog_import_utils import NewCatalogObject from PiFinder.composite_object import MagnitudeObject, SizeObject +from PiFinder.db.objects_db import ObjectsDatabase import PiFinder.utils as utils -def _load_messier_names(): +MESSIER_NAMES_FILE = Path(utils.astro_data_dir) / "ngc_ic_m/messier/messier_names.dat" + +# Words a title leaves small after its first, as the Paper Chart atlas sets +# common names (paperChart, build/sources.py). +_SMALL = frozenset({"in", "of", "the", "and", "or", "a", "an"}) + + +def title_case(name: str) -> str: + """A common name in title case, by the atlas's rule: each word + capitalised, and each part of a hyphenated one, but the small words + after the first. A capital is never lowered: "Owl or E.T. Cluster".""" + words = name.split(" ") + return " ".join( + w + if i and w in _SMALL + else "-".join(p[:1].upper() + p[1:] for p in w.split("-")) + for i, w in enumerate(words) + ) + + +def parse_messier_names(lines: Iterable[str]) -> Dict[int, List[str]]: """ - Load Messier common names from messier_names.dat file. + Messier common names by M number, from the lines of messier_names.dat. - Returns: - dict: Mapping of {messier_number: [list_of_common_names]} + Each line is ``MName; Name``, and its first name is that + object's preferred name. The file is a curated source, so a line that + doesn't parse fails the import rather than being skipped. """ - messier_names = {} - messier_names_file = ( - Path(utils.astro_data_dir) / "ngc_ic_m/messier/messier_names.dat" + messier_names: Dict[int, List[str]] = {} + for line_number, line in enumerate(lines, 1): + line = line.strip() + if not line: + continue + where = f"messier_names.dat line {line_number}" + designation, tab, names_part = line.partition("\t") + if not tab: + raise ValueError(f"{where}: expected MName; Name, got {line!r}") + if not designation.startswith("M") or not designation[1:].isdigit(): + raise ValueError(f"{where}: {designation!r} is not an M number") + m_number = int(designation[1:]) + if m_number in messier_names: + raise ValueError(f"{where}: M {m_number} is listed twice") + names = [name.strip() for name in names_part.split(";") if name.strip()] + if not names: + raise ValueError(f"{where}: M {m_number} has no name") + messier_names[m_number] = names + + return messier_names + + +def _load_messier_names(path: Path = MESSIER_NAMES_FILE) -> Dict[int, List[str]]: + with open(path, "r", encoding="utf-8") as f: + return parse_messier_names(f) + + +def put_names_first( + cursor: sqlite3.Cursor, object_id: int, names: List[str], origin: str +) -> None: + """ + Re-order one sky object's names so that `names` come first, in order. + + Names are ordered by row id, so putting some first means re-inserting + them all. A name the sky object already has keeps its row (and origin); + a new one is added with `origin`. The rest keep their order behind. + """ + rows = [ + (row[0], row[1]) + for row in cursor.execute( + "SELECT common_name, origin FROM names WHERE object_id = ? ORDER BY id", + (object_id,), + ) + ] + first: List[Tuple[str, str]] = [] + for name in names: + first += [row for row in rows if row[0] == name] or [(name, origin)] + rest = [row for row in rows if row[0] not in names] + cursor.execute("DELETE FROM names WHERE object_id = ?", (object_id,)) + cursor.executemany( + "INSERT INTO names (object_id, common_name, origin) VALUES (?, ?, ?)", + [(object_id, name, row_origin) for name, row_origin in first + rest], ) - if not messier_names_file.exists(): - logging.warning(f"Messier names file not found: {messier_names_file}") - return messier_names - - try: - with open(messier_names_file, "r", encoding="utf-8") as f: - for line_num, line in enumerate(f, 1): - line = line.strip() - if not line: - continue - - parts = line.split("\t") - if len(parts) < 2: - continue - - # Extract M number (e.g., "M1" -> 1) - m_designation = parts[0].strip() - if not m_designation.startswith("M"): - continue - - try: - m_number = int(m_designation[1:]) - except ValueError: - continue - - # Extract common names (everything after NGC reference) - common_names = [] - if len(parts) >= 3: - names_part = parts[2].strip() - if names_part: - # Split on commas and clean up each name - for name in names_part.split(","): - name = name.strip() - if name: - common_names.append(name) - - messier_names[m_number] = common_names - - logging.debug(f"Loaded {len(messier_names)} Messier objects with common names") - - except Exception as e: - logging.error(f"Error reading messier_names.dat: {e}") - return messier_names +def add_messier_names( + db: Optional[ObjectsDatabase] = None, + messier_names: Optional[Dict[int, List[str]]] = None, +) -> None: + """ + Put each Messier object's names from messier_names.dat ahead of its + other names, so the first becomes its preferred name (catalog.md 9.6). + + Runs after add_missing_messier_objects, so every M listing exists. + """ + logging.info("ADDING: Messier names") + db = db or objects_db + assert db is not None + if messier_names is None: + messier_names = _load_messier_names() + + for m_number, names in messier_names.items(): + listing = db.get_catalog_object_by_sequence("M", m_number) + if listing is None: + raise ValueError( + f"messier_names.dat names M {m_number}, which no loader created" + ) + put_names_first(db.cursor, listing["object_id"], names, "M") + db.conn.commit() + logging.info(f"ADDED: names for {len(messier_names)} Messier objects") def fix_object_types(): @@ -106,15 +161,12 @@ def add_missing_messier_objects(): """ logging.info("ADDING: Missing Messier objects M24, M40, M45, and M102") - # Load Messier common names mapping - messier_names_map = _load_messier_names() + # Their common names from messier_names.dat are placed by add_messier_names, + # which runs next. # M40 - Winnecke 4 (Double star in Ursa Major) # RA: 12h 22m 12.5272s = 185.552°, Dec: +58° 4′ 58.549″ = +58.083° m40_aka_names = ["Winnecke 4", "WNC 4"] - # Add common names from messier_names.dat if available - if 40 in messier_names_map: - m40_aka_names.extend(messier_names_map[40]) m40 = NewCatalogObject( object_type="D*", @@ -132,9 +184,6 @@ def add_missing_messier_objects(): # M45 - Pleiades (Open cluster in Taurus) # RA: 03h 47m 24s = 56.85°, Dec: +24° 07′ 00″ = +24.117° m45_aka_names = ["Cr 42", "Mel 22"] # Keep catalog designations - # Add common names from messier_names.dat if available - if 45 in messier_names_map: - m45_aka_names.extend(messier_names_map[45]) m45 = NewCatalogObject( object_type="OC", @@ -160,10 +209,8 @@ def add_missing_messier_objects(): # M24 - Sagittarius Star Cloud (no NGC equivalent, it's a dense star field) # RA: 18h 18m 24s = 274.6°, Dec: -18° 24′ 00″ = -18.4° - m24_aka_names = ["Sagittarius Star Cloud"] - # Add common names from messier_names.dat if available - if 24 in messier_names_map: - m24_aka_names.extend(messier_names_map[24]) + # "IC 4715" links M24 to the IC 4715 object: Steinicke identifies it as M 24. + m24_aka_names = ["Sagittarius Star Cloud", "IC 4715"] m24 = NewCatalogObject( object_type="Ast", # Asterism/Star cloud @@ -181,9 +228,6 @@ def add_missing_messier_objects(): # M102 - Usually identified with NGC 5866 (Spindle Galaxy) # RA: 15h 06m 29.5s = 226.623°, Dec: +55° 45′ 48″ = +55.763° m102_aka_names = ["NGC 5866", "Spindle Galaxy"] - # Add common names from messier_names.dat if available - if 102 in messier_names_map: - m102_aka_names.extend(messier_names_map[102]) m102 = NewCatalogObject( object_type="Gx", # Galaxy diff --git a/python/PiFinder/catalog_imports/steinicke_loader.py b/python/PiFinder/catalog_imports/steinicke_loader.py index 8bccebad8..8a00356b0 100644 --- a/python/PiFinder/catalog_imports/steinicke_loader.py +++ b/python/PiFinder/catalog_imports/steinicke_loader.py @@ -25,6 +25,7 @@ # Import shared database object from .database import objects_db +from .post_processing import title_case # Basic object type mappings for exact matches @@ -542,6 +543,10 @@ def get_priority(obj): if common_name.startswith("M "): m_sequence = common_name[2:].strip() common_name = "M" + m_sequence + else: + # NGC 2000.0's names are mostly lower case after the first + # word ("Crab nebula"). + common_name = title_case(common_name) catalog = line[36:37] if catalog == " ": catalog = "N" diff --git a/python/tests/test_catalog_names.py b/python/tests/test_catalog_names.py new file mode 100644 index 000000000..a79fc030c --- /dev/null +++ b/python/tests/test_catalog_names.py @@ -0,0 +1,261 @@ +"""Tests for the catalog import's common names: the Messier names, their +placement as preferred names, title-casing NGC 2000.0's names, and the hand- +built Messier objects' akas. + +Everything runs against a small objects DB built here, never the committed +one. See docs/ax/catalog.md 9.6 for the name order these tests pin down. +""" + +import json + +import pytest + +import PiFinder.catalog_imports.catalog_import_utils as catalog_import_utils +import PiFinder.catalog_imports.post_processing as post_processing +from PiFinder.catalog_imports.post_processing import ( + add_messier_names, + add_missing_messier_objects, + parse_messier_names, + title_case, +) +from PiFinder.db.objects_db import ObjectsDatabase + +pytestmark = pytest.mark.unit + + +def _mag(*mags): + return json.dumps({"mags": list(mags), "filter_mag": mags[0] if mags else 99}) + + +def _size(*arcsec): + return json.dumps({"e": list(arcsec), "p": 0.0}) + + +@pytest.fixture +def db(tmp_path): + """Sky objects with their listings and names, in insertion order, as the + NGC loader and the NGC 2000.0 names leave them.""" + db = ObjectsDatabase(tmp_path / "objects.db") + db.create_tables() + for code in ("M", "NGC", "IC", "Lyn"): + db.insert_catalog(code, 10000, code) + objects = [ + ( + ("Gx", 10.6846, 41.2692, "And", _size(11400, 3600), _mag(3.4)), + [("NGC", 224), ("M", 31)], + [ + ("NGC 224", "NGC"), + ("Great Nebula in Andromeda", "NGC"), + ("M31", "NGC"), + ], + ), + ( + ("OC", 130.1, 19.67, "Cnc", _size(5700), _mag(3.1)), + [("NGC", 2632), ("M", 44)], + [ + ("NGC 2632", "NGC"), + ("Beehive Cluster", "NGC"), + ("M44", "NGC"), + ("Praesepe", "NGC"), + ("Lyn 449", "Lyn"), + ], + ), + ( + ("Nb", 274.7, -18.55, "Sgr", _size(), _mag()), + [("IC", 4715)], + [("IC 4715", "IC")], + ), + ] + for values, listings, names in objects: + object_id = db.insert_object(*values) + for code, sequence in listings: + db.insert_catalog_object(object_id, code, sequence, "") + for name, origin in names: + db.insert_name(object_id, name, origin) + yield db + db.conn.close() + + +def object_id_of(db, code, sequence): + return db.get_catalog_object_by_sequence(code, sequence)["object_id"] + + +def names_of(db, code, sequence): + """(name, origin) of a listing's sky object, in the order the device reads + them (by row id).""" + rows = db.cursor.execute( + "SELECT common_name, origin FROM names WHERE object_id = ? ORDER BY id", + (object_id_of(db, code, sequence),), + ) + return [(row[0], row[1]) for row in rows] + + +# --------------------------------------------------------------------------- +# messier_names.dat parsing +# --------------------------------------------------------------------------- + + +def test_parse_messier_names_reads_each_line_in_order(): + lines = [ + "M1\tCrab Nebula\n", + "\n", + "M44\tBeehive Cluster; Praesepe \n", + "M45\tPleiades; Subaru; Seven Sisters\n", + ] + names = parse_messier_names(lines) + assert names == { + 1: ["Crab Nebula"], + 44: ["Beehive Cluster", "Praesepe"], + 45: ["Pleiades", "Subaru", "Seven Sisters"], + } + assert list(names) == [1, 44, 45] + + +def test_parse_messier_names_keeps_commas_inside_a_name(): + # The separator is ";", so a comma is part of the name. + assert parse_messier_names(["M8\tLagoon Nebula, core\n"]) == { + 8: ["Lagoon Nebula, core"] + } + + +@pytest.mark.parametrize( + "line, problem", + [ + ("M1 NGC 1952 Crab Nebula", "expected MName; Name"), + ("NGC 1952\tCrab Nebula", "is not an M number"), + ("M\tCrab Nebula", "is not an M number"), + ("M1\t ; ", "has no name"), + ], +) +def test_parse_messier_names_rejects_a_malformed_line(line, problem): + with pytest.raises(ValueError, match=problem) as excinfo: + parse_messier_names(["M2\tGlobular\n", line]) + assert "line 2" in str(excinfo.value) + + +def test_parse_messier_names_rejects_a_number_listed_twice(): + with pytest.raises(ValueError, match="M 1 is listed twice"): + parse_messier_names(["M1\tCrab Nebula", "M1\tTaurus A"]) + + +def test_the_curated_file_parses(): + names = post_processing._load_messier_names() + assert set(names) <= set(range(1, 111)) + assert names[33] == ["Triangulum Galaxy"] # "Pinwheel" is M 101 + assert names[101] == ["Pinwheel Galaxy"] + assert names[44] == ["Beehive Cluster", "Praesepe"] + assert names[45][0] == "Pleiades" + # "Satellite of M31" describes M 32 and M 110; it isn't a name. + assert 32 not in names + assert 110 not in names + for m_names in names.values(): + for name in m_names: + assert not name.startswith(("NGC", "IC")), name + + +# --------------------------------------------------------------------------- +# Preferred names: the first Messier name goes first +# --------------------------------------------------------------------------- + + +def test_a_messier_name_becomes_the_preferred_name(db): + add_messier_names(db, {31: ["Andromeda Galaxy"]}) + assert names_of(db, "M", 31) == [ + ("Andromeda Galaxy", "M"), + ("NGC 224", "NGC"), + ("Great Nebula in Andromeda", "NGC"), + ("M31", "NGC"), + ] + + +def test_a_name_the_object_has_moves_first_and_keeps_its_row(db): + add_messier_names(db, {44: ["Beehive Cluster", "Praesepe"]}) + assert names_of(db, "M", 44) == [ + ("Beehive Cluster", "NGC"), + ("Praesepe", "NGC"), + ("NGC 2632", "NGC"), + ("M44", "NGC"), + ("Lyn 449", "Lyn"), + ] + + +def test_other_objects_names_are_untouched(db): + before = names_of(db, "IC", 4715) + ic_ids = [ + row[0] + for row in db.cursor.execute( + "SELECT id FROM names WHERE object_id = ?", + (object_id_of(db, "IC", 4715),), + ) + ] + add_messier_names(db, {31: ["Andromeda Galaxy"], 44: ["Praesepe"]}) + assert names_of(db, "IC", 4715) == before + assert [ + row[0] + for row in db.cursor.execute( + "SELECT id FROM names WHERE object_id = ?", + (object_id_of(db, "IC", 4715),), + ) + ] == ic_ids + + +def test_placing_the_names_twice_gives_the_same_order(db): + messier_names = {31: ["Andromeda Galaxy"], 44: ["Beehive Cluster", "Praesepe"]} + add_messier_names(db, messier_names) + once = (names_of(db, "M", 31), names_of(db, "M", 44)) + add_messier_names(db, messier_names) + assert (names_of(db, "M", 31), names_of(db, "M", 44)) == once + + +def test_a_messier_number_no_loader_created_fails(db): + with pytest.raises(ValueError, match="M 57"): + add_messier_names(db, {57: ["Ring Nebula"]}) + + +# --------------------------------------------------------------------------- +# NGC 2000.0 names: title case, by the atlas's rule +# --------------------------------------------------------------------------- + + +@pytest.mark.parametrize( + "name, expected", + [ + ("Crab nebula", "Crab Nebula"), + ("Bode's nebulae", "Bode's Nebulae"), + # Small words after the first stay lower case. + ("Ring nebula in Lyra", "Ring Nebula in Lyra"), + ("Great Cluster in Hercules", "Great Cluster in Hercules"), + ("Owl or E.T. Cluster", "Owl or E.T. Cluster"), + # ... but not as the first word. + ("the Eyes", "The Eyes"), + # Each part of a hyphenated word. + ("Pin-wheel nebula", "Pin-Wheel Nebula"), + # A capital is never lowered. + ("North America nebula", "North America Nebula"), + ("Polarissima Borealis", "Polarissima Borealis"), + ("eta Car nebula", "Eta Car Nebula"), + ("47 Tuc", "47 Tuc"), + ], +) +def test_title_case(name, expected): + assert title_case(name) == expected + + +# --------------------------------------------------------------------------- +# The hand-built Messier objects +# --------------------------------------------------------------------------- + + +def test_m24_joins_ic_4715(db, monkeypatch): + """M 24 takes "IC 4715" as an aka, so it lists on IC 4715's sky object.""" + monkeypatch.setattr(catalog_import_utils, "objects_db", db) + monkeypatch.setattr(post_processing, "objects_db", db) + monkeypatch.setattr(catalog_import_utils, "ObjectsDatabase", lambda: db) + + add_missing_messier_objects() + + assert object_id_of(db, "M", 24) == object_id_of(db, "IC", 4715) + names = [name for name, _origin in names_of(db, "M", 24)] + assert names[0] == "IC 4715" + assert "M 24" in names + assert "Sagittarius Star Cloud" in names