Skip to content

Commit 34ab571

Browse files
committed
allow attachWavelengthSolution() to progress without a distortion model (since there may be no lines in some GNIRS XD orders)
1 parent 755053a commit 34ab571

2 files changed

Lines changed: 50 additions & 26 deletions

File tree

geminidr/core/primitives_spect.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -714,15 +714,10 @@ def attachWavelengthSolution(self, adinputs=None, **params):
714714
# ADs, so just set the found transforms to empty and present
715715
# the warning at the end
716716
try:
717-
if 'distortion_corrected' not in wcs.available_frames:
718-
distortion_models = []
719-
break
720-
except AttributeError:
721-
distortion_models = []
722-
break
723-
724-
m_distcorr = wcs.get_transform(wcs.input_frame,
725-
'distortion_corrected')
717+
m_distcorr = wcs.get_transform(wcs.input_frame,
718+
'distortion_corrected')
719+
except CoordinateFrameError:
720+
m_distcorr = None
726721
distortion_models.append(m_distcorr)
727722

728723
try:
@@ -736,14 +731,6 @@ def attachWavelengthSolution(self, adinputs=None, **params):
736731
wave_frames.extend([frame for frame in wcs.output_frame.frames
737732
if isinstance(frame, cf.SpectralFrame)])
738733

739-
if not distortion_models:
740-
log.warning("Could not find a 'distortion_corrected' frame "
741-
f"in arc {arc.filename} extension {ext.id} - "
742-
"continuing")
743-
if 'sq' in self.mode:
744-
fail = True
745-
continue
746-
747734
# Determine whether we're producing a single-extension AD
748735
# or keeping the number of extensions as-is
749736
if len_arc == 1:
@@ -767,7 +754,8 @@ def attachWavelengthSolution(self, adinputs=None, **params):
767754
geotable = import_module('.geometry_conf', self.inst_lookups)
768755
transform.add_mosaic_wcs(ad, geotable)
769756
for ext in ad:
770-
if 'mosaic' in ext.wcs.available_frames:
757+
if ('mosaic' in ext.wcs.available_frames and
758+
m_distcorr is not None):
771759
ext.wcs.insert_frame('mosaic', m_distcorr,
772760
cf.Frame2D(name='distortion_corrected'))
773761

@@ -859,10 +847,12 @@ def attachWavelengthSolution(self, adinputs=None, **params):
859847
# No mosaicking, so we can just do a shift
860848
m_shift = (models.Shift((ad_detsec.x1 - arc_detsec.x1) / xbin) &
861849
models.Shift((ad_detsec.y1 - arc_detsec.y1) / ybin))
862-
m_distcorr = m_shift | m_distcorr
850+
m_distcorr = (m_shift if m_distcorr is None else
851+
m_shift | m_distcorr)
863852

864-
ad[0].wcs.insert_frame(ad[0].wcs.input_frame, m_distcorr,
865-
cf.Frame2D(name='distortion_corrected'))
853+
if m_distcorr is not None:
854+
ad[0].wcs.insert_frame(ad[0].wcs.input_frame, m_distcorr,
855+
cf.Frame2D(name='distortion_corrected'))
866856

867857
if wave_model is None:
868858
log.warning(f"{arc.filename} has no wavelength solution")
@@ -887,11 +877,15 @@ def attachWavelengthSolution(self, adinputs=None, **params):
887877
# applying the distortion correction
888878
shifts = [c1 - c2 for c1, c2 in zip(ext.detector_section(),
889879
ext_arc.detector_section())]
890-
dist_model = (models.Shift(shifts[0] / xbin) &
891-
models.Shift(shifts[1] / ybin)) | dist_model
892-
893-
ext.wcs.insert_frame(ad[0].wcs.input_frame, dist_model,
894-
cf.Frame2D(name='distortion_corrected'))
880+
if shifts.count(0) < len(shifts):
881+
shift_model = (models.Shift(shifts[0] / xbin) &
882+
models.Shift(shifts[1] / ybin))
883+
dist_model = (shift_model if dist_model is None else
884+
shift_model | dist_model)
885+
886+
if dist_model is not None:
887+
ext.wcs.insert_frame(ad[0].wcs.input_frame, dist_model,
888+
cf.Frame2D(name='distortion_corrected'))
895889

896890
if wave_model is None:
897891
log.warning(f"{arc.filename} extension {ext.id} has "
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import pytest
2+
import os
3+
4+
import astrodata, gemini_instruments
5+
from geminidr.gnirs.primitives_gnirs_crossdispersed import GNIRSCrossDispersed
6+
7+
8+
@pytest.mark.gnirsxd
9+
@pytest.mark.preprocessed_data
10+
def test_attach_wavelength_solution_missing_distortion_models(path_to_inputs):
11+
"""
12+
Simple test to check that the attachWavelengthSolution primitive works if
13+
extensions are missing distortion models.
14+
"""
15+
ad = astrodata.open(os.path.join(path_to_inputs, "N20200818S0038_flatCorrected.fits"))
16+
arc = astrodata.open(os.path.join(path_to_inputs, "N20200818S0350_arc.fits"))
17+
18+
p = GNIRSCrossDispersed([ad])
19+
p.attachWavelengthSolution(arc=arc)
20+
21+
num_ext_without_distortion = 0
22+
for ext, arc_ext in zip(p.adinputs[0], arc):
23+
assert (("distortion_corrected" in ext.wcs.available_frames) ==
24+
("distortion_corrected" in arc_ext.wcs.available_frames))
25+
if "distortion_corrected" not in ext.wcs.available_frames:
26+
num_ext_without_distortion += 1
27+
28+
# Because this test is specifically for the case where some extensions
29+
# are missing distortion models!
30+
assert num_ext_without_distortion > 0, "At least one extension should be missing distortion model"

0 commit comments

Comments
 (0)