Skip to content

Commit

Permalink
🥅 Add warning when planetary body fetching is not returning the same …
Browse files Browse the repository at this point in the history
…object as expected
  • Loading branch information
Xen0Xys committed Aug 1, 2024
1 parent 4d15929 commit 2e1a97b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/ipyaladin/utils/_coordinate_parser.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import warnings
from typing import Tuple

import requests
from astropy.coordinates import SkyCoord, Angle
import re

from ipyaladin.utils.exceptions import NameResolverWarning


def parse_coordinate_string(string: str, body: str = "sky") -> SkyCoord:
"""Parse a string containing coordinates.
Expand Down Expand Up @@ -64,9 +67,17 @@ def _from_name_on_planet(string: str, body: str) -> SkyCoord:
if request.status_code != requests.codes.ok:
raise ValueError(f"Invalid coordinate string: {string}")
data = request.json()
identifier = data["data"][0][1]
lat = data["data"][0][5]
lon = data["data"][0][6]
system = data["data"][0][11]
if identifier != string:
warnings.warn(
f"Nothing found for '{string}', but found close"
f" name '{identifier}'. Moving to {identifier}.",
NameResolverWarning,
stacklevel=2,
)
if "+West" in system:
lon = 360 - lon
return SkyCoord(ra=lon, dec=lat, frame="icrs", unit="deg")
Expand Down
8 changes: 8 additions & 0 deletions src/ipyaladin/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ class WidgetCommunicationError(OSError):
def __init__(self, message: str) -> None:
self.message = message
super(WidgetCommunicationError, self).__init__(message)


class NameResolverWarning(UserWarning):
"""Warning raised when a name resolver is used."""

def __init__(self, message: str) -> None:
self.message = message
super(NameResolverWarning, self).__init__(message)

0 comments on commit 2e1a97b

Please sign in to comment.