Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/ax/catalog/CONTEXT.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ _Avoid_: PiFinder format (ambiguous in prose), JSON format (Stellarium is JSON t

**Resolution**:
The umbrella term for matching an observing list entry to a `CompositeObject` in the catalog collection. Two strategies are tried in a fixed order, then a coordinate fallback:
1. **Catalog-keyed resolution** — match by catalog code + sequence, with alias mapping (Messier→M, Caldwell→C, Collinder→Cr). Tried whenever the entry carries a catalog key.
2. **Name resolution** — exact, case-insensitive match of the entry's name against catalog object names, also trying a constellation-genitive–normalized variant ("VY Andromedae" → "VY And"). **Strictly a fallback**: attempted only when catalog-keyed resolution finds nothing. Names collide more readily than catalog keys, so a name never overrides a key match.
1. **Catalog-keyed resolution** — match by catalog code + sequence. A designation is read by `designations.parse_designation`, the same parser the import uses, so "Cr 399", "Collinder 399" and "Col 399" all name Collinder 399, and a designation the import refuses ("H 24", a double star; "M 2-9", a Minkowski nebula) names no listing. The native format's catalog key is PiFinder's own and is looked up as it is. Tried whenever the entry carries a catalog key.
2. **Name resolution** — case-, space- and hyphen-insensitive match of the entry's name against catalog object names (the digit groups must match, so "M 2-9" never matches "M 29"), also trying a constellation-genitive–normalized variant ("VY Andromedae" → "VY And"). **Strictly a fallback**: attempted only when catalog-keyed resolution finds nothing. Names collide more readily than catalog keys, so a name never overrides a key match. A designation with a bare "A", "B" or "H" prefix ("H 24", "B 33") is never matched by name: sources use those names for different objects.
3. If both fail, an entry carrying coordinates becomes a **coordinate object**; an entry with no key, no name match, and no coordinates is dropped.
_Avoid_: lookup, import (import is the whole read-then-resolve flow).

Expand Down
9 changes: 6 additions & 3 deletions docs/ax/catalog/obslist-formats/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,12 @@ decimal as hours instead. The hint only affects a bare decimal — the colon and
sexagesimal forms are already in hours.

**Resolution.** A CSV list carries no special flag; its entries resolve through
the normal path (`obslist.py`): a `Name` that matches a catalog designation
resolves to that catalog object, otherwise the row's own coordinates become an
OBS target. Name lookup runs through the shared `ui_utils.normalize` (case-,
the normal path (`obslist.py`): a `CatalogCode` + `Sequence` pair, or a `Name`,
that the designation parser reads (`designations.parse_designation`, shared
with the catalog import) resolves to that catalog object; then a `Name` that
matches a catalog object's name; otherwise the row's own coordinates become an
OBS target. A bare `H`, `h`, `A` or `B` designation (`H 24`) is a double star
in some sources, so it never resolves by catalog or by name. Name lookup runs through the shared `ui_utils.normalize` (case-,
space- and hyphen-insensitive, plus constellation-genitive folding), so `M 3`,
`M3` and `NGC 224` all match regardless of how the catalog stores them. To force
coordinates for an otherwise-matchable name, use a non-designation label (e.g.
Expand Down
96 changes: 5 additions & 91 deletions python/PiFinder/catalog_imports/catalog_import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
from tqdm import tqdm

from PiFinder.composite_object import MagnitudeObject, SizeObject
from PiFinder.designations import ( # noqa: F401 -- re-exported for the loaders
CATALOG_CODE_ALIASES,
listing_named,
parse_designation,
)
from PiFinder import calc_utils
from PiFinder.db.objects_db import ObjectsDatabase
from PiFinder.db.observations_db import ObservationsDatabase
Expand Down Expand Up @@ -584,97 +589,6 @@ def trim_string(s):
return " ".join(s.split())


# Designation prefixes that name a PiFinder catalog, keyed on the lowercase,
# whitespace-free prefix as source catalogs write it. A prefix must be listed
# here for an alias to link one object to another; every other designation is
# kept as a plain name. The same catalog is spelled several ways across
# sources, and SIMBAD writes Abell planetaries as "PN A66 nn".
#
# Deliberately absent:
# - A bare "h". Herschel 400 uses catalog code "H", but "H 1-1" and similar
# names are Haro planetary nebulae, and a bare "H 12" can also be Hubble.
# - A bare "a" or "b". Double-star sources use them for the discoverers
# Aitken and van den Bos ("A 58", "B 53"). A loader whose source means
# Abell or Barnard passes them as extra_aliases to parse_designation.
# - "wds" and "pk". Their designations end in digits that are not a sequence
# ("WDS 00001+7508", "PK 120+09.1").
CATALOG_CODE_ALIASES: Dict[str, str] = {
"abell": "Abl",
"abl": "Abl",
"pna66": "Abl",
"arp": "Arp",
"barnard": "B",
"c": "C",
"caldwell": "C",
"col": "Col",
"collinder": "Col",
"cr": "Col",
"egc": "EGC",
"har": "Har",
"herschel": "H",
"ic": "IC",
"lyn": "Lyn",
"m": "M",
"messier": "M",
"ngc": "NGC",
"rds": "RDS",
"saa": "SaA",
"sam": "SaM",
"sar": "SaR",
"sh2": "Sh2",
"sharpless": "Sh2",
"str": "Str",
"ta2": "Ta2",
"tlk": "TLK",
}

# A designation reads "<prefix><sequence>": the sequence is the trailing run of
# digits, the prefix is everything before it.
_DESIGNATION_RE = re.compile(r"(.*?)\s*(\d+)")


def parse_designation(
raw: str, extra_aliases: Optional[Dict[str, str]] = None
) -> Optional[Tuple[str, int]]:
"""Split a source designation into a PiFinder catalog code and sequence.

"NGC 7008" -> ("NGC", 7008), "Sh 2-176" -> ("Sh2", 176),
"PN A66 80" -> ("Abl", 80).

Returns None when the designation names no PiFinder catalog. That covers
the planetary-nebula families whose numeric part is compound: "M 1-92"
(Minkowski), "H 3-29" (Haro), "K 2-1" (Kohoutek), "NGC 650-1" (the two
halves of M76). Those leave a prefix of "m1", "h3", "k2" or "ngc650",
none of which is a catalog, so they are rejected instead of being read as
Messier 92, Herschel 29, "K 21" or NGC 6501.

``extra_aliases`` adds prefixes for one source, for example {"a": "Abl"}
for a source that writes Abell planetaries as "A 50".
"""
match = _DESIGNATION_RE.fullmatch(trim_string(raw).lower())
if match is None:
return None
prefix, sequence = match.groups()
key = prefix.rstrip("-.").replace(" ", "")
catalog_code = CATALOG_CODE_ALIASES.get(key)
if catalog_code is None and extra_aliases:
catalog_code = extra_aliases.get(key)
if catalog_code is None:
return None
return catalog_code, int(sequence)


def listing_named(name: str) -> Optional[Tuple[str, int]]:
"""The listing a name resolves to in the loaders' lookup, or None.

parse_designation alone answers it, so its prefix allowlist governs every
lookup: "B53" and "h99" name no listing, though Barnard 53 and Herschel
400 #99 exist. ObjectFinder and the correction pass's checks
(corrections.resolves_to) both ask this, so they cannot disagree.
"""
return parse_designation(name)


def delete_catalog_from_database(catalog_code: str):
"""Delete a catalog and all its related records from the database"""
if objects_db is None:
Expand Down
146 changes: 146 additions & 0 deletions python/PiFinder/designations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
"""
Designation parsing shared by the catalog import and the observing-list lookup.

One parser answers "which PiFinder listing does this designation name?" for
both, so a name an observing list resolves is the listing the import would
link it to. This module has no import-time dependencies beyond the standard
library, so the running app can use it.
"""

import re
from typing import Dict, Optional, Tuple

# Designation prefixes that name a PiFinder catalog, keyed on the lowercase,
# whitespace-free prefix as source catalogs write it. A prefix must be listed
# here for an alias to link one object to another; every other designation is
# kept as a plain name. The same catalog is spelled several ways across
# sources, and SIMBAD writes Abell planetaries as "PN A66 nn".
#
# Deliberately absent:
# - A bare "h". Herschel 400 uses catalog code "H", but "H 1-1" and similar
# names are Haro planetary nebulae, and a bare "H 12" can also be Hubble.
# - A bare "a" or "b". Double-star sources use them for the discoverers
# Aitken and van den Bos ("A 58", "B 53"). A loader whose source means
# Abell or Barnard passes them as extra_aliases to parse_designation.
# - "wds" and "pk". Their designations end in digits that are not a sequence
# ("WDS 00001+7508", "PK 120+09.1").
CATALOG_CODE_ALIASES: Dict[str, str] = {
"abell": "Abl",
"abl": "Abl",
"pna66": "Abl",
"arp": "Arp",
"barnard": "B",
"c": "C",
"caldwell": "C",
"col": "Col",
"collinder": "Col",
"cr": "Col",
"egc": "EGC",
"har": "Har",
"herschel": "H",
"ic": "IC",
"lyn": "Lyn",
"m": "M",
"messier": "M",
"ngc": "NGC",
"rds": "RDS",
"saa": "SaA",
"sam": "SaM",
"sar": "SaR",
"sh2": "Sh2",
"sharpless": "Sh2",
"str": "Str",
"ta2": "Ta2",
"tlk": "TLK",
}

# A designation reads "<prefix><sequence>": the sequence is the trailing run of
# digits, the prefix is everything before it.
_DESIGNATION_RE = re.compile(r"(.*?)\s*(\d+)")

# The bare one-letter prefixes left out of CATALOG_CODE_ALIASES (see above).
# Each is a double-star discoverer in some sources and a PiFinder catalog in
# others, so "H 24", "h 99", "A 58" or "B 33" names no single object.
AMBIGUOUS_PREFIXES = frozenset({"a", "b", "h"})


def _prefix_and_sequence(raw: str) -> Optional[Tuple[str, int]]:
"""The lowercase, whitespace-free prefix and the sequence of ``raw``."""
match = _DESIGNATION_RE.fullmatch(" ".join(raw.split()).lower())
if match is None:
return None
prefix, sequence = match.groups()
return prefix.rstrip("-.").replace(" ", ""), int(sequence)


def parse_designation(
raw: str, extra_aliases: Optional[Dict[str, str]] = None
) -> Optional[Tuple[str, int]]:
"""Split a source designation into a PiFinder catalog code and sequence.

"NGC 7008" -> ("NGC", 7008), "Sh 2-176" -> ("Sh2", 176),
"PN A66 80" -> ("Abl", 80).

Returns None when the designation names no PiFinder catalog. That covers
the planetary-nebula families whose numeric part is compound: "M 1-92"
(Minkowski), "H 3-29" (Haro), "K 2-1" (Kohoutek), "NGC 650-1" (the two
halves of M76). Those leave a prefix of "m1", "h3", "k2" or "ngc650",
none of which is a catalog, so they are rejected instead of being read as
Messier 92, Herschel 29, "K 21" or NGC 6501.

``extra_aliases`` adds prefixes for one source, for example {"a": "Abl"}
for a source that writes Abell planetaries as "A 50".
"""
parsed = _prefix_and_sequence(raw)
if parsed is None:
return None
key, sequence = parsed
catalog_code = CATALOG_CODE_ALIASES.get(key)
if catalog_code is None and extra_aliases:
catalog_code = extra_aliases.get(key)
if catalog_code is None:
return None
return catalog_code, sequence


def listing_named(name: str) -> Optional[Tuple[str, int]]:
"""The listing a name resolves to in the loaders' lookup, or None.

parse_designation alone answers it, so its prefix allowlist governs every
lookup: "B53" and "h99" name no listing, though Barnard 53 and Herschel
400 #99 exist. ObjectFinder, the correction pass's checks
(corrections.resolves_to) and the observing-list lookup all ask this, so
they cannot disagree.
"""
return parse_designation(name)


def is_ambiguous_designation(raw: str) -> bool:
"""True if ``raw`` is a designation with a bare "A", "B" or "H" prefix.

"H 24" and "h 99" are double stars in SAC and WDS, and Herschel 400
entries in PiFinder, so a name lookup must not answer them either.
Compound designations such as "H 1-1" (Haro) are not ambiguous.
"""
parsed = _prefix_and_sequence(raw)
return parsed is not None and parsed[0] in AMBIGUOUS_PREFIXES


def designation(catalog_code: str, sequence: int) -> Optional[str]:
"""A designation of a listing that listing_named reads back, or None.

("Col", 399) -> "Col 399". The parser refuses a bare "H" or "B", so
Herschel 400 and Barnard listings get their long form: ("H", 24) ->
"Herschel 24", ("B", 33) -> "Barnard 33". A catalog with no readable
prefix (WDS) gives None.
"""
candidates = [catalog_code] + [
prefix.capitalize()
for prefix, code in CATALOG_CODE_ALIASES.items()
if code == catalog_code
]
for prefix in candidates:
text = f"{prefix} {sequence}"
if listing_named(text) == (catalog_code, sequence):
return text
return None
57 changes: 31 additions & 26 deletions python/PiFinder/obslist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
from PiFinder.catalog_base import VirtualIDManager
from PiFinder.catalogs import Catalogs
from PiFinder.composite_object import CompositeObject
from PiFinder.designations import is_ambiguous_designation, listing_named
from PiFinder.ui.ui_utils import normalize
from PiFinder.obslist_formats import (
ObsList,
ObsListEntry,
SKYSAFARI_CATALOG_NAMES_INV,
SUPPORTED_EXTENSIONS,
read_file as formats_read_file,
write_skylist,
Expand Down Expand Up @@ -63,38 +63,44 @@ def _entry_from_composite(obj: CompositeObject) -> ObsListEntry:
)


CATALOG_ALIASES: dict = {
"Messier": "M",
"Caldwell": "C",
"Collinder": "Cr",
}


def resolve_object(catalog_numbers, catalogs: Catalogs):
"""
Takes a list of catalog number strings
(e.g. ["M 31", "NGC 224"]) and tries to
find a matching object in the PiFinder DB.

Each string is read by listing_named, the parser the catalog import uses,
so "Cr 399" and "Collinder 399" both name Collinder 399, and "H 24" (a
double-star discoverer, not Herschel 400) names nothing.

Pure lookup: returns the shared catalog object (or None). Callers must not
mutate its catalog description; per-list text goes in `list_descriptions`.
"""
for catalog_number in catalog_numbers:
parts = catalog_number.strip().split(" ", 1)
catalog = SKYSAFARI_CATALOG_NAMES_INV.get(parts[0], parts[0])
catalog = CATALOG_ALIASES.get(catalog, catalog)
try:
sequence = int(parts[1].strip())
except (ValueError, IndexError):
sequence = None

if sequence is not None:
_object = catalogs.get_object(catalog, sequence)
listing = listing_named(catalog_number)
if listing:
_object = catalogs.get_object(*listing)
if _object:
return _object
return None


def resolve_entry(entry: ObsListEntry, catalogs: Catalogs):
"""
Catalog-keyed resolution of one observing list entry, or None.

A SkySafari entry's catalog numbers are source spellings, so each goes
through resolve_object. Every other entry's catalog code and sequence were
already parsed by its reader (see ObsListEntry), or are the native
format's own catalog key, so they are looked up as they are.
"""
if entry.catalog_names:
return resolve_object(entry.catalog_names, catalogs)
if entry.catalog_code and entry.sequence:
return catalogs.get_object(entry.catalog_code, entry.sequence)
return None


def _coordinate_object(entry: ObsListEntry, index: int) -> CompositeObject:
"""
Creates a CompositeObject from an ObsListEntry's coordinates
Expand Down Expand Up @@ -281,8 +287,13 @@ def resolve_by_name(name: str, name_index: dict):
space- and hyphen-insensitive, but the digit groups must match), also
trying a constellation-normalized variant ("VY Andromedae" -> "VY And").
Returns the shared catalog object or None.

A designation with a bare "A", "B" or "H" prefix ("H 24", "B 33") is not
matched by name: catalogs use those names for different objects (a double
star in SAC, a Herschel 400 or Barnard object in PiFinder), and the
designation parser refuses them for the same reason.
"""
if not name:
if not name or is_ambiguous_designation(name):
return None
keys = [_name_key(name)]
designation = _normalize_designation(name)
Expand Down Expand Up @@ -322,13 +333,7 @@ def read_list(catalogs: Catalogs, name):
try:
_object = None

# Try catalog resolution with catalog_names (skylist multi-name)
if entry.catalog_names:
_object = resolve_object(entry.catalog_names, catalogs)
elif entry.catalog_code and entry.sequence:
_object = resolve_object(
[f"{entry.catalog_code} {entry.sequence}"], catalogs
)
_object = resolve_entry(entry, catalogs)

# No catalog match: try resolving by object name (handles lists
# that identify objects only by name, e.g. "VY Andromedae").
Expand Down
Loading
Loading