55
66import numpy as np
77from astropy .nddata import NDData
8+ from astropy import units as u
89
9- from specreduce .extract import _ap_weight_image
10+ from specreduce .extract import _ap_weight_image , _to_spectrum1d_pixels
1011from specreduce .tracing import Trace , FlatTrace
1112
1213__all__ = ['Background' ]
@@ -193,8 +194,8 @@ def bkg_image(self, image=None):
193194 Parameters
194195 ----------
195196 image : nddata-compatible image or None
196- image with 2-D spectral image data. If None, will use ``image`` passed
197- to extract the background .
197+ image with 2-D spectral image data. If None, will extract
198+ the background from ``image`` used to initialize the class .
198199
199200 Returns
200201 -------
@@ -205,15 +206,37 @@ def bkg_image(self, image=None):
205206
206207 return np .tile (self .bkg_array , (image .shape [0 ], 1 ))
207208
209+ def bkg_spectrum (self , image = None ):
210+ """
211+ Expose the 1D spectrum of the background.
212+
213+ Parameters
214+ ----------
215+ image : nddata-compatible image or None
216+ image with 2-D spectral image data. If None, will extract
217+ the background from ``image`` used to initialize the class.
218+
219+ Returns
220+ -------
221+ spec : `~specutils.Spectrum1D`
222+ The background 1-D spectrum, with flux expressed in the same
223+ units as the input image (or u.DN if none were provided) and
224+ the spectral axis expressed in pixel units.
225+ """
226+ bkg_image = self .bkg_image (image = image )
227+
228+ ext1d = np .sum (bkg_image , axis = self .crossdisp_axis )
229+ return _to_spectrum1d_pixels (ext1d * getattr (image , 'unit' , u .DN ))
230+
208231 def sub_image (self , image = None ):
209232 """
210233 Subtract the computed background from ``image``.
211234
212235 Parameters
213236 ----------
214237 image : nddata-compatible image or None
215- image with 2-D spectral image data. If None, will use ``image`` passed
216- to extract the background .
238+ image with 2-D spectral image data. If None, will extract
239+ the background from ``image`` used to initialize the class .
217240
218241 Returns
219242 -------
@@ -228,6 +251,28 @@ def sub_image(self, image=None):
228251 else :
229252 return image - self .bkg_image (image )
230253
254+ def sub_spectrum (self , image = None ):
255+ """
256+ Expose the 1D spectrum of the background-subtracted image.
257+
258+ Parameters
259+ ----------
260+ image : nddata-compatible image or None
261+ image with 2-D spectral image data. If None, will extract
262+ the background from ``image`` used to initialize the class.
263+
264+ Returns
265+ -------
266+ spec : `~specutils.Spectrum1D`
267+ The background 1-D spectrum, with flux expressed in the same
268+ units as the input image (or u.DN if none were provided) and
269+ the spectral axis expressed in pixel units.
270+ """
271+ sub_image = self .sub_image (image = image )
272+
273+ ext1d = np .sum (sub_image , axis = self .crossdisp_axis )
274+ return _to_spectrum1d_pixels (ext1d * getattr (image , 'unit' , u .DN ))
275+
231276 def __rsub__ (self , image ):
232277 """
233278 Subtract the background from an image.
0 commit comments