Skip to content

Commit

Permalink
reorganize
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Jan 17, 2024
1 parent 1ec39cc commit 448f894
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions specreduce/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ class FitTrace(Trace, _ImageParser):
_disp_axis = 1

def __post_init__(self):

# parse image
self.image = self._parse_image(self.image)

# mask any previously uncaught invalid values
Expand Down Expand Up @@ -262,14 +264,20 @@ def __post_init__(self):
warnings.warn('TRACE: Converting window to int')
self.window = int(self.window)

# fit the trace
self._fit_trace(img)

def _fit_trace(self, img):

yy = np.arange(img.shape[self._crossdisp_axis])

# set max peak location by user choice or wavelength with max avg flux
ztot = img.sum(axis=self._disp_axis) / img.shape[self._disp_axis]
peak_y = self.guess if self.guess is not None else ztot.argmax()
# NOTE: peak finder can be bad if multiple objects are on slit

yy = np.arange(img.shape[self._crossdisp_axis])

if self.peak_method == 'gaussian':

# guess the peak width as the FWHM, roughly converted to gaussian sigma
yy_above_half_max = np.sum(ztot > (ztot.max() / 2))
width_guess = yy_above_half_max / gaussian_sigma_to_fwhm
Expand All @@ -292,6 +300,8 @@ def __post_init__(self):
ilum2 = (yy if self.window is None
else yy[np.arange(peak_y - self.window,
peak_y + self.window, dtype=int)])

# check if everything in window region is masked
if img[ilum2].mask.all():
raise ValueError('All pixels in window region are masked. Check '
'for invalid values or use a larger window value.')
Expand All @@ -303,11 +313,10 @@ def __post_init__(self):
warn_bins = []
for i in range(self.bins):

# binned column
# binned column (sum) if bins < ncols. otherwise, just columns.
z_i = img[ilum2, x_bins[i]:x_bins[i+1]].sum(axis=self._disp_axis)

if self.peak_method == 'gaussian':

# if binned column is fully masked for peak_method='gaussian',
# the fit value for this bin should be nan, then continue to next
warn_msg = 'nan'
Expand Down Expand Up @@ -363,9 +372,8 @@ def __post_init__(self):
y_bins[i] = ilum2[z_i.argmax()]

# warning message for fully masked bin
# NOTE this reflects current behavior, should eventually be changed
# to set to nan by default (or zero fill / interpoate option once
# available) and warn accordingly.
# NOTE: should eventually be changed to set to nan by default
# (or zero fill / interpoate option onceavailable) and warn.
warn_msg = 'zero'

# warn about fully-masked bins
Expand Down

0 comments on commit 448f894

Please sign in to comment.