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

spectral extraction: updates for Spectrum1D support in specreduce #1847

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from jdaviz.core.custom_traitlets import IntHandleEmpty, FloatHandleEmpty
from jdaviz.core.marks import PluginLine

from astropy.nddata import NDData, StdDevUncertainty, VarianceUncertainty, UnknownUncertainty
from specutils import Spectrum1D
from astropy.nddata import UnknownUncertainty
from astropy import units
from specreduce import tracing
from specreduce import background
from specreduce import extract
Expand Down Expand Up @@ -644,18 +644,18 @@ def export_trace(self, add_data=False, **kwargs):
# being able to load back into the plugin)
orig_trace = self.trace_trace.selected_obj
if isinstance(orig_trace, tracing.FlatTrace):
trace = tracing.FlatTrace(self.trace_dataset.selected_obj.data,
trace = tracing.FlatTrace(self.trace_dataset.selected_obj,
orig_trace.trace_pos+self.trace_offset)
else:
trace = tracing.ArrayTrace(self.trace_dataset.selected_obj.data,
trace = tracing.ArrayTrace(self.trace_dataset.selected_obj,
self.trace_trace.selected_obj.trace+self.trace_offset)

elif self.trace_type_selected == 'Flat':
trace = tracing.FlatTrace(self.trace_dataset.selected_obj.data,
trace = tracing.FlatTrace(self.trace_dataset.selected_obj,
self.trace_pixel)

elif self.trace_type_selected == 'Auto':
trace = tracing.KosmosTrace(self.trace_dataset.selected_obj.data,
trace = tracing.KosmosTrace(self.trace_dataset.selected_obj,
guess=self.trace_pixel,
bins=int(self.trace_bins),
window=self.trace_window,
Expand All @@ -674,7 +674,7 @@ def vue_create_trace(self, *args):

def _get_bg_trace(self):
if self.bg_type_selected == 'Manual':
trace = tracing.FlatTrace(self.trace_dataset.selected_obj.data,
trace = tracing.FlatTrace(self.trace_dataset.selected_obj,
self.bg_trace_pixel)
elif self.bg_trace_selected == 'From Plugin':
trace = self.export_trace(add_data=False)
Expand Down Expand Up @@ -736,15 +736,15 @@ def export_bg(self, **kwargs):
trace = self._get_bg_trace()

if self.bg_type_selected == 'Manual':
bg = background.Background(self.bg_dataset.selected_obj.data,
bg = background.Background(self.bg_dataset.selected_obj,
[trace], width=self.bg_width)
elif self.bg_type_selected == 'OneSided':
bg = background.Background.one_sided(self.bg_dataset.selected_obj.data,
bg = background.Background.one_sided(self.bg_dataset.selected_obj,
trace,
self.bg_separation,
width=self.bg_width)
elif self.bg_type_selected == 'TwoSided':
bg = background.Background.two_sided(self.bg_dataset.selected_obj.data,
bg = background.Background.two_sided(self.bg_dataset.selected_obj,
trace,
self.bg_separation,
width=self.bg_width)
Expand All @@ -763,10 +763,7 @@ def export_bg_img(self, add_data=False, **kwargs):
Whether to add the resulting image to the application, according to the options
defined in the plugin.
"""
bg = self.export_bg(**kwargs)

bg_spec = Spectrum1D(spectral_axis=self.bg_dataset.selected_obj.spectral_axis,
flux=bg.bkg_image()*self.bg_dataset.selected_obj.flux.unit)
bg_spec = self.export_bg(**kwargs).bkg_image()

if add_data:
self.bg_add_results.add_results_from_plugin(bg_spec, replace=True)
Expand All @@ -792,8 +789,7 @@ def export_bg_spectrum(self, add_data=False, **kwargs):
Whether to add the resulting spectrum to the application, according to the options
defined in the plugin.
"""
bg = self.export_bg(**kwargs)
spec = bg.bkg_spectrum()
spec = self.export_bg(**kwargs).bkg_spectrum()

if add_data:
self.bg_spec_add_results.add_results_from_plugin(spec, replace=False)
Expand All @@ -813,10 +809,7 @@ def export_bg_sub(self, add_data=False, **kwargs):
Whether to add the resulting image to the application, according to the options
defined in the plugin.
"""
bg = self.export_bg(**kwargs)

bg_sub_spec = Spectrum1D(spectral_axis=self.bg_dataset.selected_obj.spectral_axis,
flux=bg.sub_image()*self.bg_dataset.selected_obj.flux.unit)
bg_sub_spec = self.export_bg(**kwargs).sub_image()

if add_data:
self.bg_sub_add_results.add_results_from_plugin(bg_sub_spec, replace=True)
Expand Down Expand Up @@ -867,13 +860,9 @@ def export_extract(self, **kwargs):
inp_sp2d = self._get_ext_input_spectrum()

if self.ext_type_selected == 'Boxcar':
ext = extract.BoxcarExtract(inp_sp2d.data, trace, width=self.ext_width)
ext = extract.BoxcarExtract(inp_sp2d, trace, width=self.ext_width)
elif self.ext_type_selected == 'Horne':
uncert = inp_sp2d.uncertainty if inp_sp2d.uncertainty is not None else VarianceUncertainty(np.ones_like(inp_sp2d.data)) # noqa
if not hasattr(uncert, 'uncertainty_type'):
uncert = StdDevUncertainty(uncert)
image = NDData(inp_sp2d.data, uncertainty=uncert)
ext = extract.HorneExtract(image, trace)
ext = extract.HorneExtract(inp_sp2d, trace)
else:
raise NotImplementedError(f"extraction type '{self.ext_type_selected}' not supported") # noqa

Expand All @@ -892,12 +881,9 @@ def export_extract_spectrum(self, add_data=False, **kwargs):
extract = self.export_extract(**kwargs)
spectrum = extract.spectrum

# Specreduce returns a spectral axis in pixels, so we'll replace with input spectral_axis
# NOTE: this is currently disabled until proper handling of axes-limit linking between
# the 2D spectrum image (plotted in pixels) and a 1D spectrum (plotted in freq or
# wavelength) is implemented.

# spectrum = Spectrum1D(spectral_axis=inp_sp2d.spectral_axis, flux=spectrum.flux)
# TEMPORARY: override spectral axis to be in pixels until properly supporting plotting
# in wavelength/frequency
spectrum._spectral_axis = np.arange(len(spectrum.spectral_axis)) * units.pix

if add_data:
self.ext_add_results.add_results_from_plugin(spectrum, replace=False)
Expand Down