Skip to content

Commit

Permalink
✨ Add support for astropy SkyCoord
Browse files Browse the repository at this point in the history
  • Loading branch information
Xen0Xys committed Apr 16, 2024
1 parent 1143ca1 commit 7f5ef2a
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/ipyaladin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class Aladin(anywidget.AnyWidget):

# Options for the view initialization
height = Int(400).tag(sync=True, init_option=True)
target = Unicode("0 0").tag(sync=True, init_option=True)
_target = Unicode("0 0").tag(sync=True, init_option=True)
_frame = Unicode("icrs").tag(sync=True, init_option=True)
fov = Float(60.0).tag(sync=True, init_option=True)
survey = Unicode("https://alaskybis.unistra.fr/DSS/DSSColor").tag(
sync=True, init_option=True
Expand Down Expand Up @@ -105,6 +106,56 @@ def _handle_custom_message(self, model, message, list_of_buffers): # noqa: ARG0
elif event_type == "select" and "select" in self.listener_callback:
self.listener_callback["select"](message_content)

@property
def target(self):
from astropy.coordinates import SkyCoord

try:
return SkyCoord(
ra=self._target.split(" ")[0],
dec=self._target.split(" ")[1],
frame="icrs",
unit="deg",
)
except ValueError as e:
raise RuntimeError(
"If target is an object, please retry "
"after object coordinates are fetched"
) from e

@target.setter
def target(self, target):
from astropy.coordinates import SkyCoord

if isinstance(target, str):
if target[0].isalpha(): # If the target is an object
sc = SkyCoord.from_name(target)
self._target = f"{sc.icrs.ra.deg} {sc.icrs.dec.deg}"
self.send(
{
"event_name": "goto_ra_dec",
"ra": sc.icrs.ra.deg,
"dec": sc.icrs.dec.deg,
}
)
else: # If the target is a coordinate string
self._target = target
self.send({"event_name": "goto_ra_dec", "object": target})
elif isinstance(target, SkyCoord): # If the target is a SkyCoord object
self._target = f"{target.icrs.ra.deg} {target.icrs.dec.deg}"
self.send(
{
"event_name": "goto_ra_dec",
"ra": target.icrs.ra.deg,
"dec": target.icrs.dec.deg,
}
)
else:
raise ValueError(
"target must be a string or an astropy.coordinates.SkyCoord object"
)
self.send({"event_name": "change_target", "target": self._target})

def add_catalog_from_URL(self, votable_URL, votable_options=None):
"""load a VOTable table from an url and load its data into the widget
Args:
Expand Down

0 comments on commit 7f5ef2a

Please sign in to comment.