From 7f5ef2ae410ce899fa2a0f0781f8c800b5f0f52b Mon Sep 17 00:00:00 2001 From: Xen0Xys Date: Tue, 16 Apr 2024 14:21:42 +0000 Subject: [PATCH] :sparkles: Add support for astropy SkyCoord --- src/ipyaladin/__init__.py | 53 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/ipyaladin/__init__.py b/src/ipyaladin/__init__.py index 2499f88f..fa3b70c7 100644 --- a/src/ipyaladin/__init__.py +++ b/src/ipyaladin/__init__.py @@ -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 @@ -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: