Skip to content

Commit

Permalink
Recalculate autocoord when linktype changes
Browse files Browse the repository at this point in the history
  • Loading branch information
duytnguyendtn committed Sep 4, 2024
1 parent 5a61cc9 commit c0e9086
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from numpy.testing import assert_allclose
from astropy.io import fits
import numpy as np
import pytest

from jdaviz.configs.imviz.tests.utils import BaseImviz_WCS_WCS
Expand Down Expand Up @@ -46,10 +47,10 @@ def test_autocenter_coords(self):
# Switch to first viewer and verify coordinates have switched
vo_plugin.viewer_selected = "imviz-0"
ra_str, dec_str = vo_plugin.source.split()
assert_allclose(
np.testing.assert_allclose(
float(ra_str), self._data_center_coords["has_wcs_1[SCI,1]"]["ra"]
)
assert_allclose(
np.testing.assert_allclose(
float(dec_str), self._data_center_coords["has_wcs_1[SCI,1]"]["dec"]
)

Expand All @@ -60,10 +61,10 @@ def test_autocenter_coords(self):
# Now load second data into second viewer and verify coordinates
self.imviz.app.add_data_to_viewer("imviz-1", "has_wcs_2[SCI,1]")
ra_str, dec_str = vo_plugin.source.split()
assert_allclose(
np.testing.assert_allclose(
float(ra_str), self._data_center_coords["has_wcs_2[SCI,1]"]["ra"]
)
assert_allclose(
np.testing.assert_allclose(
float(dec_str), self._data_center_coords["has_wcs_2[SCI,1]"]["dec"]
)

Expand Down Expand Up @@ -134,6 +135,65 @@ def test_populate_table_url_header_fallback(self):
assert vo_plugin.table.headers_visible == ["URL"]


def test_link_type_autocoord(imviz_helper):
"""
Tests switching linking types forces recalculation of viewer center coordinates
"""
# First data with WCS, same as the one in BaseImviz_WCS_NoWCS.
hdu1 = fits.ImageHDU(np.random.rand(100, 100), name="SCI")
hdu1.header.update(
{
"CTYPE1": "RA---TAN",
"CUNIT1": "deg",
"CDELT1": -2.777777778,
"CRPIX1": 1,
"CRVAL1": 337.5202808,
"NAXIS1": 10,
"CTYPE2": "DEC--TAN",
"CUNIT2": "deg",
"CDELT2": 2.777777778,
"CRPIX2": 1,
"CRVAL2": -20.833333059999998,
"NAXIS2": 10,
}
)
imviz_helper.load_data(hdu1, data_label="has_wcs_1")

# Second data with WCS, similar to above but dithered by 1 pixel in X.
hdu2 = fits.ImageHDU(np.ones((10, 10)), name="SCI")
hdu2.header.update(
{
"CTYPE1": "RA---TAN",
"CUNIT1": "deg",
"CDELT1": -0.0002777777778,
"CRPIX1": 2,
"CRVAL1": 137.5202808,
"NAXIS1": 10,
"CTYPE2": "DEC--TAN",
"CUNIT2": "deg",
"CDELT2": 0.0002777777778,
"CRPIX2": 1,
"CRVAL2": -20.833333059999998,
"NAXIS2": 10,
}
)
imviz_helper.load_data(hdu2, data_label="has_wcs_2")

vo_plugin = imviz_helper.plugins["Virtual Observatory"]._obj
vo_plugin.viewer_selected = "imviz-0"
vo_plugin._center_on_data()
ra_str, dec_str = vo_plugin.source.split()
np.testing.assert_allclose(float(ra_str), 284.2101962057667)
np.testing.assert_allclose(float(dec_str), 32.23616603681311)

imviz_helper.plugins[orientation_plugin_label].link_type = "WCS"

ra_str, dec_str = vo_plugin.source.split()

np.testing.assert_allclose(float(ra_str), 326.7884142245305)
np.testing.assert_allclose(float(dec_str), -9.905948925234416)


@pytest.mark.remote_data
class TestVOImvizRemote:

Expand Down
12 changes: 9 additions & 3 deletions jdaviz/configs/default/plugins/virtual_observatory/vo_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
from jdaviz.configs.imviz.plugins.orientation.orientation import (
orientation_plugin_label,
)
from jdaviz.core.events import SnackbarMessage, AddDataMessage, RemoveDataMessage
from jdaviz.core.events import (
SnackbarMessage,
AddDataMessage,
RemoveDataMessage,
LinkUpdatedMessage,
)
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import (
PluginTemplateMixin,
Expand Down Expand Up @@ -74,8 +79,9 @@ def __init__(self, *args, **kwargs):

self.hub.subscribe(self, AddDataMessage, handler=self._center_on_data)
self.hub.subscribe(self, RemoveDataMessage, handler=self._center_on_data)
self.hub.subscribe(self, LinkUpdatedMessage, handler=self._center_on_data)

@observe("viewer_selected", "change")
@observe("viewer_selected", type="change")
def _center_on_data(self, _=None):
"""
If data is present in the default viewer, center the plugin's coordinates on
Expand Down Expand Up @@ -343,7 +349,7 @@ def vue_load_selected_data(self, _=None):
str(entry["URL"]), # Open URL as FITS object
data_label=f"{self.source}_{self.resource_selected}_{entry.get('Title', entry.get('URL', ''))}", # noqa: E501
cache=False,
timeout=1e6 # Set to arbitrarily large value to prevent timeouts
timeout=1e6, # Set to arbitrarily large value to prevent timeouts
)
except Exception as e:
self.hub.broadcast(
Expand Down

0 comments on commit c0e9086

Please sign in to comment.