Skip to content
This repository has been archived by the owner on Jul 22, 2021. It is now read-only.

Commit

Permalink
Merge branch 'master' into upstream_master
Browse files Browse the repository at this point in the history
  • Loading branch information
nmearl committed Nov 10, 2016
2 parents 46a8718 + 91f1d13 commit e7bdcda
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 21 deletions.
60 changes: 43 additions & 17 deletions mosviz/viewers/mos_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

from specutils.core.generic import Spectrum1DRef
from astropy.nddata.nduncertainty import StdDevUncertainty
from astropy.units import Unit
from astropy.wcs import WCS


try:
from specviz.external.glue.data_viewer import SpecVizViewer
Expand Down Expand Up @@ -47,7 +50,7 @@ def load_ui(self):
"""
Setup the MOSView viewer interface.
"""
self.central_widget = QWidget()
self.central_widget = QWidget(self)

path = os.path.abspath(
os.path.join(os.path.dirname(__file__),
Expand Down Expand Up @@ -246,7 +249,6 @@ def _unpack_selection(self, data):

# Clear the table
self.catalog = Table()
self.catalog.meta = data.meta

This comment has been minimized.

Copy link
@mattjhill

mattjhill Nov 10, 2016

Contributor

Is there a reason this line was deleted? This makes it impossible to select loaders from the table because the meta data is never added to catalog.


col_names = data.components
for att in col_names:
Expand All @@ -273,22 +275,31 @@ def _unpack_selection(self, data):

self.catalog[str(att)] = comp_data

# Update gui elements
self._update_navigation()
self._set_navigation(0)
if len(self.catalog) > 0:
# Load the first source in the catalog
self.load_selection(self.catalog[0])

# Load the first source in the catalog
self.load_selection(self.catalog[0])
# Update gui elements
self._update_navigation()
self._set_navigation(0)

def _update_navigation(self):
"""
Updates the :class:`qtpy.QtWidgets.QComboBox` widget with the
appropriate source `id`s from the MOS catalog.
"""
if self.toolbar is None:
return

self.toolbar.source_select.clear()
self.toolbar.source_select.addItems(self.catalog['id'][:])

if len(self.catalog) > 0 and 'id' in self.catalog.colnames:
self.toolbar.source_select.addItems(self.catalog['id'][:])

def _set_navigation(self, index):
if len(self.catalog) < index:
return

if 0 <= index < self.toolbar.source_select.count():
self.toolbar.source_select.setCurrentIndex(index)
self.load_selection(self.catalog[index])
Expand All @@ -304,7 +315,7 @@ def _set_navigation(self, index):
self.toolbar.cycle_next_action.setDisabled(False)

def _get_loaders(self):
loaders = self.catalog.meta.get("loaders", [])
loaders = self.catalog.meta.get("loaders", {})
# if loader is specified
if "spec1d" in loaders:
spectrum1d_loader = next((x.function for x in config.data_factory.members if x.label == self.catalog.meta["loaders"]["spec1d"]),
Expand All @@ -326,19 +337,19 @@ def _get_loaders(self):
return spectrum1d_loader, spectrum2d_loader, cutout_loader

def _open_in_specviz(self):
if self._specviz_instance is None:
self._specviz_instance = self.session.application.new_data_viewer(
SpecVizViewer)
_specviz_instance = self.session.application.new_data_viewer(
SpecVizViewer)

spec1d_data = self._loaded_data['spec1d']

spec_data = Spectrum1DRef(
data=spec1d_data.get_component(spec1d_data.id['Flux']).data,
dispersion=spec1d_data.get_component(spec1d_data.id['Wavelength']).data,
uncertainty=StdDevUncertainty(spec1d_data.get_component(spec1d_data.id['Uncertainty']).data),
unit="", name=self.current_row['id'])
unit="", name=self.current_row['id'],
wcs=WCS(spec1d_data.header))

self._specviz_instance.open_data(spec_data)
_specviz_instance.open_data(spec_data)

def load_selection(self, row):
"""
Expand Down Expand Up @@ -398,16 +409,31 @@ def render_data(self, row, spec1d_data=None, spec2d_data=None,
y=spec1d_data.get_component(spec1d_data.id['Flux']).data,
yerr=spec1d_data.get_component(spec1d_data.id['Uncertainty']).data)

self.spectrum1d_widget.axes.set_xlabel("Wavelength")
self.spectrum1d_widget.axes.set_ylabel("Flux")
# Try to retrieve the wcs information
try:
flux_unit = spec1d_data.header.get('BUNIT', 'Jy').lower()
flux_unit = flux_unit.replace('counts', 'count')
flux_unit = Unit(flux_unit)
except ValueError:
flux_unit = Unit("Jy")

try:
disp_unit = spec1d_data.header.get('CUNIT1', 'Angstrom').lower()
disp_unit = Unit(disp_unit)
except ValueError:
disp_unit = Unit("Angstrom")

self.spectrum1d_widget.axes.set_xlabel("Wavelength [{}]".format(disp_unit))
self.spectrum1d_widget.axes.set_ylabel("Flux [{}]".format(flux_unit))

if spec2d_data is not None:
wcs = spec2d_data.coords.wcs

self.spectrum2d_widget.set_image(
image=spec2d_data.get_component(
spec2d_data.id['Flux']).data,
wcs=wcs, interpolation='none', aspect='auto')
wcs=wcs, interpolation='none', aspect='auto',
header=spec2d_data.header)

self.spectrum2d_widget.axes.set_xlabel("Wavelength")
self.spectrum2d_widget.axes.set_ylabel("Spatial Y")
Expand Down
40 changes: 36 additions & 4 deletions mosviz/widgets/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
from glue.viewers.common.qt.toolbar import BasicToolbar
from glue.viewers.common.qt.mpl_toolbar import MatplotlibViewerToolbar

import numpy as np
from astropy.wcs import WCS, WCSSUB_SPECTRAL

from matplotlib import rcParams
rcParams.update({'figure.autolayout': True})
# rcParams.update({'figure.autolayout': True})

__all__ = ['Line1DWidget', 'ShareableAxesImageWidget', 'DrawableImageWidget']

Expand Down Expand Up @@ -63,8 +66,37 @@ def set_data(self, x, y, yerr=None):
def _redraw(self):
self.central_widget.canvas.draw()

def set_status(self):
pass


class MOSImageWidget(StandaloneImageWidget):
def __init__(self, *args, **kwargs):
super(MOSImageWidget, self).__init__(*args, **kwargs)

def set_image(self, image=None, wcs=None, header=None, **kwargs):
super(MOSImageWidget, self).set_image(image, wcs, **kwargs)

if header is not None:
hwcs = WCS(header)

# Try to reference the spectral axis
hwcs_spec = hwcs.sub([WCSSUB_SPECTRAL])

# Check to see if it actually is a real coordinate description
if hwcs_spec.naxis == 0:
# It's not real, so attempt to get the spectral axis by
# specifying axis by integer
hwcs_spec = hwcs.sub([hwcs.naxis])

# Construct the dispersion array
dispersion = hwcs_spec.all_pix2world(
np.arange(image.shape[0]), 0)[0]

self.axes.set_xticklabels(["{}".format(x) for x in dispersion])


class ShareableAxesImageWidget(StandaloneImageWidget):
class ShareableAxesImageWidget(MOSImageWidget):
def __init__(self, *args, **kwargs):
super(ShareableAxesImageWidget, self).__init__(*args, **kwargs)

Expand Down Expand Up @@ -96,11 +128,11 @@ def set_status(self):
pass


class DrawableImageWidget(StandaloneImageWidget):
class DrawableImageWidget(MOSImageWidget):
def __init__(self, *args, **kwargs):
super(DrawableImageWidget, self).__init__(*args, **kwargs)
self._slit_patch = None

def draw_shapes(self, x=0, y=0, width=100, length=100):
self._slit_patch = plt.Rectangle((x-length, y-width), width, length, fc='r')
self.axes.add_patch(self._slit_patch)
# self.axes.add_patch(self._slit_patch)

0 comments on commit e7bdcda

Please sign in to comment.