Skip to content

Commit ff9ff12

Browse files
committed
fix: support object name parsing in parse_coordinates
- Added SkyCoord.from_name() support for object names like 'M31' - Try parsing as 'RA Dec' numbers first - Then try object name resolution (requires network) - Finally try coordinate string parsing - Updated error message to include object name examples
1 parent 3131c37 commit ff9ff12

1 file changed

Lines changed: 19 additions & 7 deletions

File tree

src/utils.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,30 +61,42 @@ def suggest_web_view(ctx: typer.Context, result_url: str, reason: str = ""):
6161
def parse_coordinates(ctx: typer.Context, coords_str: str) -> Optional[SkyCoord]:
6262
"""
6363
Parses a coordinate string into an Astropy SkyCoord object.
64-
Handles various common formats including decimal degrees and HMS/DMS.
64+
Handles various common formats including decimal degrees, HMS/DMS, and object names.
6565
"""
6666
if not coords_str:
6767
console.print("[bold red]Error: Coordinate string cannot be empty.[/bold red]")
6868
raise typer.Exit(code=1)
69-
try:
70-
if re.match(r"^\s*[\d\.\-+]+\s+[\d\.\-+]+\s*$", coords_str):
71-
parts = coords_str.split()
72-
if len(parts) == 2:
69+
70+
# Try parsing as "RA Dec" (two numbers)
71+
if re.match(r"^\s*[\d\.\-+]+\s+[\d\.\-+]+\s*$", coords_str):
72+
parts = coords_str.split()
73+
if len(parts) == 2:
74+
try:
7375
return SkyCoord(
7476
ra=float(parts[0]),
7577
dec=float(parts[1]),
7678
unit=(u.deg, u.deg),
7779
frame="icrs",
7880
)
81+
except Exception:
82+
pass
83+
84+
# Try parsing as object name (requires network)
85+
try:
86+
return SkyCoord.from_name(coords_str)
87+
except Exception:
88+
pass
89+
90+
# Try parsing as coordinate string
91+
try:
7992
return SkyCoord(coords_str, frame="icrs")
8093
except Exception as e1:
81-
# Do not exit here, just print error and return None
8294
console.print(
8395
f"[bold red]Error: Could not parse coordinates '{coords_str}'.[/bold red]"
8496
)
8597
console.print(f"[yellow]Details: {e1}[/yellow]")
8698
console.print(
87-
f"[yellow]Ensure format is recognized by Astropy (e.g., '10.68h +41.26d', '10d30m0s 20d0m0s', '150.0 2.0' for deg).[/yellow]"
99+
f"[yellow]Ensure format is recognized by Astropy (e.g., '10.68h +41.26d', '10d30m0s 20d0m0s', '150.0 2.0' for deg, or an object name like 'M31').[/yellow]"
88100
)
89101
return None
90102

0 commit comments

Comments
 (0)