Skip to content

Commit

Permalink
Fix possible crashes in the alignment to abs catalog
Browse files Browse the repository at this point in the history
  • Loading branch information
mcara committed May 9, 2024
1 parent 9e857a5 commit 39bbe84
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,15 @@ tweakreg
- Changed default settings for ``abs_separation`` parameter for the ``tweakreg``
step to have a value compatible with the ``abs_tolerance`` parameter. [#8445]

- Improve error handling in the absolute alignment. [#8450]

wfss_contam
-----------

- Fixed flux scaling issue in model contamination image by adding background
subtraction and re-scaling fluxes to respect wavelength oversampling. [#8416]


1.14.0 (2024-03-29)
===================

Expand Down
72 changes: 61 additions & 11 deletions jwst/tweakreg/tweakreg_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def process(self, input):
if len(images) == 0:
raise ValueError("Input must contain at least one image model.")

rel_outcomes = set()

# Build the catalogs for input images
for image_model in images:
if use_custom_catalogs and image_model.meta.tweakreg_catalog:
Expand Down Expand Up @@ -365,6 +367,7 @@ def process(self, input):

for imcat in imcats:
model = imcat.meta['image_model']
rel_outcomes.add(model.meta.cal_step.tweakreg)
if model.meta.cal_step.tweakreg == "SKIPPED":
continue
wcs = model.meta.wcs
Expand All @@ -390,6 +393,7 @@ def process(self, input):
# catalog as a separate product with a name based on
# whatever convention is determined by the JWST Cal Working
# Group.

if self.save_abs_catalog:
if self.output_dir is None:
output_name = 'fit_{}_ref.ecsv'.format(self.abs_refcat.lower())
Expand All @@ -398,6 +402,11 @@ def process(self, input):
else:
output_name = None

rel_ok = (
len(rel_outcomes) > 1 or
(rel_outcomes and rel_outcomes.pop() != "SKIPPED")
)

# initial shift to be used with absolute astrometry
self.abs_xoffset = 0
self.abs_yoffset = 0
Expand Down Expand Up @@ -455,17 +464,58 @@ def process(self, input):
del imcat.meta['fit_info']

# Perform fit
align_wcs(
imcats,
refcat=ref_cat,
enforce_user_order=True,
expand_refcat=False,
minobj=self.abs_minobj,
match=xyxymatch_gaia,
fitgeom=self.abs_fitgeometry,
nclip=self.abs_nclip,
sigma=(self.abs_sigma, 'rmse')
)
try:
align_wcs(
imcats,
refcat=ref_cat,
enforce_user_order=True,
expand_refcat=False,
minobj=self.abs_minobj,
match=xyxymatch_gaia,
fitgeom=self.abs_fitgeometry,
nclip=self.abs_nclip,
sigma=(self.abs_sigma, 'rmse')
)
except ValueError as e:
msg = e.args[0]
if (msg == "Too few input images (or groups of images) with "

Check warning on line 481 in jwst/tweakreg/tweakreg_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/tweakreg/tweakreg_step.py#L479-L481

Added lines #L479 - L481 were not covered by tests
"non-empty catalogs."):
# we need at least two exposures to perform image alignment
self.log.warning(msg)
self.log.warning(

Check warning on line 485 in jwst/tweakreg/tweakreg_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/tweakreg/tweakreg_step.py#L484-L485

Added lines #L484 - L485 were not covered by tests
"At least one exposure is required to align images "
"to an absolute reference catalog. Alignment to an "
"absolute reference catalog will not be performed."
)
if not rel_ok:
self.log.warning("Nothing to do. Skipping 'TweakRegStep'...")
for model in images:
model.meta.cal_step.tweakreg = "SKIPPED"
self.skip = True
return images

Check warning on line 495 in jwst/tweakreg/tweakreg_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/tweakreg/tweakreg_step.py#L490-L495

Added lines #L490 - L495 were not covered by tests
else:
raise e

Check warning on line 497 in jwst/tweakreg/tweakreg_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/tweakreg/tweakreg_step.py#L497

Added line #L497 was not covered by tests

except RuntimeError as e:
msg = e.args[0]
if msg.startswith("Number of output coordinates exceeded allocation"):

Check warning on line 501 in jwst/tweakreg/tweakreg_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/tweakreg/tweakreg_step.py#L499-L501

Added lines #L499 - L501 were not covered by tests
# we need at least two exposures to perform image alignment
self.log.error(msg)
self.log.error(

Check warning on line 504 in jwst/tweakreg/tweakreg_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/tweakreg/tweakreg_step.py#L503-L504

Added lines #L503 - L504 were not covered by tests
"Multiple sources within specified tolerance "
"matched to a single reference source. Try to "
"adjust 'tolerance' and/or 'separation' parameters."
"Alignment to an absolute reference catalog will "
"not be performed."
)
if not rel_ok:
self.log.warning("Skipping 'TweakRegStep'...")
self.skip = True
for model in images:
model.meta.cal_step.tweakreg = "SKIPPED"
return images

Check warning on line 516 in jwst/tweakreg/tweakreg_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/tweakreg/tweakreg_step.py#L511-L516

Added lines #L511 - L516 were not covered by tests
else:
raise e

Check warning on line 518 in jwst/tweakreg/tweakreg_step.py

View check run for this annotation

Codecov / codecov/patch

jwst/tweakreg/tweakreg_step.py#L518

Added line #L518 was not covered by tests

for imcat in imcats:
image_model = imcat.meta['image_model']
Expand Down

0 comments on commit 39bbe84

Please sign in to comment.