99from astropy .modeling import Model , models , fitting
1010from astropy .nddata import NDData , VarianceUncertainty
1111
12- from specreduce .core import SpecreduceOperation
12+ from specreduce .core import _ImageParser , SpecreduceOperation
1313from specreduce .tracing import Trace , FlatTrace
1414from specutils import Spectrum1D
1515
@@ -164,41 +164,6 @@ class BoxcarExtract(SpecreduceOperation):
164164 def spectrum (self ):
165165 return self .__call__ ()
166166
167- def _parse_image (self ):
168- """
169- Convert all accepted image types to a consistently formatted Spectrum1D.
170- """
171-
172- if isinstance (self .image , np .ndarray ):
173- img = self .image
174- elif isinstance (self .image , u .quantity .Quantity ):
175- img = self .image .value
176- else : # NDData, including CCDData and Spectrum1D
177- img = self .image .data
178-
179- # mask and uncertainty are set as None when they aren't specified upon
180- # creating a Spectrum1D object, so we must check whether these
181- # attributes are absent *and* whether they are present but set as None
182- if getattr (self .image , 'mask' , None ) is not None :
183- mask = self .image .mask
184- else :
185- mask = np .ma .masked_invalid (img ).mask
186-
187- if getattr (self .image , 'uncertainty' , None ) is not None :
188- uncertainty = self .image .uncertainty
189- else :
190- uncertainty = VarianceUncertainty (np .ones (img .shape ))
191-
192- unit = getattr (self .image , 'unit' , u .Unit ('DN' )) # or u.Unit()?
193-
194- spectral_axis = getattr (self .image , 'spectral_axis' ,
195- (np .arange (img .shape [self .disp_axis ])
196- if hasattr (self , 'disp_axis' )
197- else np .arange (img .shape [1 ])) * u .pix )
198-
199- self .image = Spectrum1D (img * unit , spectral_axis = spectral_axis ,
200- uncertainty = uncertainty , mask = mask )
201-
202167 def __call__ (self , image = None , trace_object = None , width = None ,
203168 disp_axis = None , crossdisp_axis = None ):
204169 """
@@ -231,12 +196,7 @@ def __call__(self, image=None, trace_object=None, width=None,
231196 crossdisp_axis = crossdisp_axis if crossdisp_axis is not None else self .crossdisp_axis
232197
233198 # handle image processing based on its type
234- if isinstance (image , Spectrum1D ):
235- img = image .data
236- unit = image .unit
237- else :
238- img = image
239- unit = getattr (image , 'unit' , u .DN )
199+ self .image = self ._parse_image (image )
240200
241201 # TODO: this check can be removed if/when implemented as a check in FlatTrace
242202 if isinstance (trace_object , FlatTrace ):
@@ -252,11 +212,11 @@ def __call__(self, image=None, trace_object=None, width=None,
252212 width ,
253213 disp_axis ,
254214 crossdisp_axis ,
255- img .shape )
215+ self . image .shape )
256216
257217 # extract
258- ext1d = np .sum (img * wimg , axis = crossdisp_axis ) * unit
259- return _to_spectrum1d_pixels (ext1d )
218+ ext1d = np .sum (self . image . data * wimg , axis = crossdisp_axis )
219+ return _to_spectrum1d_pixels (ext1d * self . image . unit )
260220
261221
262222@dataclass
0 commit comments