Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save and restore camera parameters #95

Merged
merged 6 commits into from
May 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion glue_wwt/viewer/data_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

from __future__ import absolute_import, division, print_function

from astropy.coordinates import SkyCoord
import astropy.units as u
from glue.core.coordinates import WCSCoordinates
from glue.logger import logger
from pywwt import ViewerNotAvailableError

from .image_layer import WWTImageLayerArtist
from .table_layer import WWTTableLayerArtist
Expand All @@ -25,7 +29,6 @@ class WWTDataViewerBase(object):
}

_UPDATE_SETTINGS = [
"foreground", "background", "foreground_opacity", "galactic",
"equatorial_grid", "equatorial_grid_color", "equatorial_text",
"ecliptic_grid", "ecliptic_grid_color", "ecliptic_text",
"alt_az_grid", "alt_az_grid_color", "alt_az_text",
Expand All @@ -37,6 +40,8 @@ class WWTDataViewerBase(object):
"precession_chart_color"
]

_IMAGERY_UPDATE_SETTINGS = ["foreground", "background", "foreground_opacity", "galactic"]

def __init__(self):
self._initialize_wwt()
self._wwt.actual_planet_scale = True
Expand All @@ -55,6 +60,7 @@ def _update_wwt(self, force=False, **kwargs):
self._wwt.solar_system.cosmos = self.state.mode == 'Universe'
# Only show local stars when not in Universe or Milky Way mode
self._wwt.solar_system.stars = self.state.mode not in ['Universe', 'Milky Way']
force = True

if force or 'constellation_boundaries' in kwargs:
self._wwt.constellation_boundaries = self.state.constellation_boundaries != 'None'
Expand All @@ -65,6 +71,12 @@ def _update_wwt(self, force=False, **kwargs):
wwt_attr = self._GLUE_TO_WWT_ATTR_MAP.get(setting, setting)
setattr(self._wwt, wwt_attr, getattr(self.state, setting, None))

show_imagery = self.state.mode == 'Sky'
if show_imagery:
for setting in self._IMAGERY_UPDATE_SETTINGS:
if force or setting in kwargs:
setattr(self._wwt, setting, getattr(self.state, setting, None))

def get_layer_artist(self, cls, **kwargs):
"In this package, we must override to append the wwt_client argument."
return cls(self.state, wwt_client=self._wwt, **kwargs)
Expand All @@ -84,3 +96,34 @@ def get_data_layer_artist(self, layer=None, layer_state=None):
def get_subset_layer_artist(self, layer=None, layer_state=None):
# At some point maybe we'll use different classes for this?
return self.get_data_layer_artist(layer=layer, layer_state=layer_state)

def __gluestate__(self, context):
state = super(WWTDataViewerBase, self).__gluestate__(context)
try:
center = self._wwt.get_center()
camera = {
"ra": center.ra.deg,
"dec": center.dec.deg,
"fov": self._wwt.get_fov().value
}
if hasattr(self._wwt, 'get_roll'):
camera["roll"] = self._wwt.get_roll().value
state["camera"] = camera
except ViewerNotAvailableError:
logger.error("Unable to export camera parameters as WWT viewer is not responding.")
return state

@classmethod
def __setgluestate__(cls, rec, context):
viewer = super(WWTDataViewerBase, cls).__setgluestate__(rec, context)
if "camera" in rec:
camera = rec["camera"]
ra = camera.get("ra", 0)
dec = camera.get("dec", 0)
fov = camera.get("fov", 60)
roll = camera.get("roll", None)
camera_kwargs = dict(fov=fov * u.deg, instant=True)
if hasattr(viewer._wwt, 'get_roll') and roll is not None:
camera_kwargs["roll"] = roll * u.deg
viewer._wwt.center_on_coordinates(SkyCoord(ra, dec, unit=u.deg), **camera_kwargs)
return viewer