Skip to content

Commit 0d2ea80

Browse files
committed
revert to having "pixels" -> "rectified" -> "distortion_corrected" as the frame order
This is required in case the distortion correction only has a single line to work with, in which case it will only be a function of position in the slit so the slit *must* be vertical (since the model is defined as a function of pixel). This should work now because the transformation is calculated *after* transforming the input *and* output coordinated via the rectification model. Also add a check in transferDistortionModel() that the previous CoordinateFrame has the same name in both the donor and recipient WCSs.
1 parent 7fc83eb commit 0d2ea80

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

geminidr/core/primitives_spect.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,21 @@ def determineDistortion(self, adinputs=None, **params):
15241524
ref_coords = np.array([coord for trace in traces for
15251525
coord in trace.reference_coordinates()]).T
15261526

1527+
# If the frame has a rectification model, then we want to
1528+
# calculate the distortion transform *after* applying this
1529+
# model. This is important because, if we only have one line
1530+
# from which to determine the distortion, the model will only
1531+
# be a function of X and so we need a vertical spectrum.
1532+
try:
1533+
rect_model = ext.wcs.get_transform(ext.wcs.input_frame,
1534+
"rectified")
1535+
except CoordinateFrameError:
1536+
has_rect_model = False
1537+
else:
1538+
has_rect_model = True
1539+
in_coords = rect_model(*in_coords)
1540+
ref_coords = rect_model(*ref_coords)
1541+
15271542
# The model is computed entirely in the pixel coordinate frame
15281543
# of the data, so it could be used as a gWCS object
15291544
m_init = models.Chebyshev2D(x_degree=orders[1 - dispaxis],
@@ -1562,6 +1577,9 @@ def determineDistortion(self, adinputs=None, **params):
15621577
if ext.wcs is None:
15631578
ext.wcs = gWCS([(cf.Frame2D(name="pixels"), model),
15641579
(cf.Frame2D(name="world"), None)])
1580+
elif has_rect_model:
1581+
ext.wcs.insert_frame("rectified", model,
1582+
cf.Frame2D(name="distortion_corrected"))
15651583
else:
15661584
ext.wcs.insert_frame(ext.wcs.input_frame, model,
15671585
cf.Frame2D(name="distortion_corrected"))
@@ -5023,13 +5041,19 @@ def transferDistortionModel(self, adinputs=None, suffix=None, source=None):
50235041
# Distortion model is the transform immediately before the
50245042
# "distortion_corrected" frame, regardless of anything else
50255043
frame_index = wcs2.available_frames.index('distortion_corrected')
5026-
m_distcorr = wcs2.pipeline[frame_index - 1].transform
5027-
distortion_models.append(m_distcorr)
5044+
distortion_models.append(wcs2.pipeline[frame_index - 1])
50285045

50295046
if not fail:
5030-
for ext, dist in zip(ad1, distortion_models):
5031-
ext.wcs.insert_frame(ext.wcs.input_frame, dist,
5032-
cf.Frame2D(name="distortion_corrected"))
5047+
for ext, (previous_frame, m_distcorr) in zip(ad1, distortion_models):
5048+
try:
5049+
ext.wcs.insert_frame(previous_frame.name, m_distcorr,
5050+
cf.Frame2D(name="distortion_corrected"))
5051+
except ValueError:
5052+
raise ValueError(
5053+
"Input distortion model corrects from "
5054+
f"{previous_frame.name} but this frame does not "
5055+
f"exist in {ad1.filename}:{ext.id} WCS."
5056+
)
50335057
ad1.update_filename(suffix=suffix, strip=True)
50345058

50355059
return adinputs

0 commit comments

Comments
 (0)