From 2c80121f47be86e48d9c175f0c8f23468fe2f4d2 Mon Sep 17 00:00:00 2001 From: Ned Molter Date: Tue, 9 Jul 2024 18:05:16 -0400 Subject: [PATCH] moved update_s_region_ out of jwst, altered step flow logic --- jwst/assign_wcs/assign_wcs.py | 3 +- jwst/assign_wcs/util.py | 44 +--------------------- jwst/cube_build/cube_build_step.py | 2 +- jwst/lib/set_telescope_pointing.py | 3 +- jwst/resample/resample_step.py | 3 +- jwst/tweakreg/tests/test_multichip_jwst.py | 9 +++-- jwst/tweakreg/tweakreg_step.py | 40 ++++++++++++-------- 7 files changed, 38 insertions(+), 66 deletions(-) diff --git a/jwst/assign_wcs/assign_wcs.py b/jwst/assign_wcs/assign_wcs.py index ca61e6d188f..3c181330fc3 100644 --- a/jwst/assign_wcs/assign_wcs.py +++ b/jwst/assign_wcs/assign_wcs.py @@ -1,7 +1,8 @@ import logging import importlib from gwcs.wcs import WCS -from .util import (update_s_region_spectral, update_s_region_imaging, +from stcal.alignment.util import update_s_region_imaging +from .util import (update_s_region_spectral, update_s_region_nrs_ifu, update_s_region_mrs) from ..lib.exposure_types import IMAGING_TYPES, SPEC_TYPES, NRS_LAMP_MODE_SPEC_TYPES from ..lib.dispaxis import get_dispersion_direction diff --git a/jwst/assign_wcs/util.py b/jwst/assign_wcs/util.py index 5262b1c2093..b629cfcfab5 100644 --- a/jwst/assign_wcs/util.py +++ b/jwst/assign_wcs/util.py @@ -17,6 +17,7 @@ from gwcs.wcstools import grid_from_bounding_box from gwcs import utils as gwutils from stpipe.exceptions import StpipeExitException +from stcal.alignment.util import update_s_region_keyword from stdatamodels.jwst.datamodels import WavelengthrangeModel from stdatamodels.jwst.transforms.models import GrismObject @@ -789,32 +790,6 @@ def bounding_box_from_subarray(input_model): return bbox -def update_s_region_imaging(model): - """ - Update the ``S_REGION`` keyword using ``WCS.footprint``. - """ - - bbox = model.meta.wcs.bounding_box - - if bbox is None: - bbox = wcs_bbox_from_shape(model.data.shape) - model.meta.wcs.bounding_box = bbox - - # footprint is an array of shape (2, 4) as we - # are interested only in the footprint on the sky - footprint = model.meta.wcs.footprint(bbox, center=True, axis_type="spatial").T - # take only imaging footprint - footprint = footprint[:2, :] - - # Make sure RA values are all positive - negative_ind = footprint[0] < 0 - if negative_ind.any(): - footprint[0][negative_ind] = 360 + footprint[0][negative_ind] - - footprint = footprint.T - update_s_region_keyword(model, footprint) - - def compute_footprint_spectral(model): """ Determine spatial footprint for spectral observations using the instrument model. @@ -888,23 +863,6 @@ def update_s_region_nrs_slit(slit): slit.meta.wcsinfo.spectral_region = spectral_region -def update_s_region_keyword(model, footprint): - """ Update the S_REGION keyword. - """ - s_region = ( - "POLYGON ICRS " - " {0:.9f} {1:.9f}" - " {2:.9f} {3:.9f}" - " {4:.9f} {5:.9f}" - " {6:.9f} {7:.9f}".format(*footprint.flatten())) - if "nan" in s_region: - # do not update s_region if there are NaNs. - log.info("There are NaNs in s_region, S_REGION not updated.") - else: - model.meta.wcsinfo.s_region = s_region - log.info("Update S_REGION to {}".format(model.meta.wcsinfo.s_region)) - - def compute_footprint_nrs_ifu(dmodel, mod): """ Determine NIRSPEC IFU footprint using the instrument model. diff --git a/jwst/cube_build/cube_build_step.py b/jwst/cube_build/cube_build_step.py index 15a9dde45d0..9f8bd59b343 100755 --- a/jwst/cube_build/cube_build_step.py +++ b/jwst/cube_build/cube_build_step.py @@ -7,7 +7,7 @@ from . import cube_build from . import ifu_cube from . import data_types -from ..assign_wcs.util import update_s_region_keyword +from stcal.alignment.util import update_s_region_keyword import time __all__ = ["CubeBuildStep"] diff --git a/jwst/lib/set_telescope_pointing.py b/jwst/lib/set_telescope_pointing.py index 06dfd587a34..3d9cb663624 100644 --- a/jwst/lib/set_telescope_pointing.py +++ b/jwst/lib/set_telescope_pointing.py @@ -72,11 +72,12 @@ from scipy.interpolate import interp1d from stdatamodels.jwst import datamodels +from stcal.alignment.util import update_s_region_keyword from .exposure_types import IMAGING_TYPES, FGS_GUIDE_EXP_TYPES from .set_velocity_aberration import compute_va_effects_vector from .siafdb import SIAF, SiafDb -from ..assign_wcs.util import update_s_region_keyword, calc_rotation_matrix +from ..assign_wcs.util import calc_rotation_matrix from ..assign_wcs.pointing import v23tosky from ..lib.engdb_tools import ENGDB_Service from ..lib.pipe_utils import is_tso diff --git a/jwst/resample/resample_step.py b/jwst/resample/resample_step.py index 1f99d12a41a..5c98ae8d3ca 100755 --- a/jwst/resample/resample_step.py +++ b/jwst/resample/resample_step.py @@ -11,6 +11,7 @@ from . import resample from ..stpipe import Step from ..assign_wcs import util +from stcal.alignment.util import update_s_region_imaging log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) @@ -94,7 +95,7 @@ def process(self, input): for model in result: model.meta.cal_step.resample = 'COMPLETE' self.update_fits_wcs(model) - util.update_s_region_imaging(model) + update_s_region_imaging(model) model.meta.asn.pool_name = input_models.asn_pool_name model.meta.asn.table_name = input_models.asn_table_name diff --git a/jwst/tweakreg/tests/test_multichip_jwst.py b/jwst/tweakreg/tests/test_multichip_jwst.py index 6daa383c330..d3386a46996 100644 --- a/jwst/tweakreg/tests/test_multichip_jwst.py +++ b/jwst/tweakreg/tests/test_multichip_jwst.py @@ -14,6 +14,7 @@ from tweakwcs.correctors import JWSTWCSCorrector from tweakwcs.imalign import align_wcs +from tweakwcs import imalign from stdatamodels.jwst.datamodels import ImageModel @@ -224,7 +225,7 @@ def test_multichip_jwst_alignment(monkeypatch): # 2. test_multichip_alignment_step() does not have access to 'fit_info' # in the meta data and so test_multichip_jwst_alignment() can test # the fit more extensively. - monkeypatch.setattr(tweakreg_step, 'align_wcs', _align_wcs) + monkeypatch.setattr(imalign, 'align_wcs', _align_wcs) monkeypatch.setattr(tweakreg_step, 'make_tweakreg_catalog', _make_tweakreg_catalog) w1 = _make_gwcs_wcs(os.path.join(data_path, 'wfc3_uvis1.hdr')) @@ -294,7 +295,7 @@ def test_multichip_jwst_alignment(monkeypatch): def test_multichip_alignment_step(monkeypatch): - monkeypatch.setattr(tweakreg_step, 'align_wcs', _align_wcs) + monkeypatch.setattr(imalign, 'align_wcs', _align_wcs) monkeypatch.setattr(tweakreg_step, 'make_tweakreg_catalog', _make_tweakreg_catalog) # image 1 @@ -404,6 +405,8 @@ def test_multichip_alignment_step(monkeypatch): # step._is_wcs_correction_small = lambda x, y: True mr, m1, m2 = step.process(mc) + for im in [mr, m1, m2]: + assert im.meta.cal_step.tweakreg == 'COMPLETE' wc1 = m1.meta.wcs wc2 = m2.meta.wcs @@ -422,7 +425,7 @@ def test_multichip_alignment_step(monkeypatch): def test_multichip_alignment_step_abs(monkeypatch): - monkeypatch.setattr(tweakreg_step, 'align_wcs', _align_wcs) + monkeypatch.setattr(imalign, 'align_wcs', _align_wcs) monkeypatch.setattr(tweakreg_step, 'make_tweakreg_catalog', _make_tweakreg_catalog) refcat_path = os.path.join(data_path, 'ref.ecsv') diff --git a/jwst/tweakreg/tweakreg_step.py b/jwst/tweakreg/tweakreg_step.py index bf9646f65cd..c286eca5b27 100644 --- a/jwst/tweakreg/tweakreg_step.py +++ b/jwst/tweakreg/tweakreg_step.py @@ -249,10 +249,10 @@ def process(self, input): # wrapper to stcal tweakreg routines # step skip conditions should throw TweakregError from stcal - try: + if n_groups > 1: + try: # relative alignment of images to each other (if more than one group) - if n_groups > 1: - correctors, local_align_failed = \ + correctors = \ twk.relative_align(correctors, enforce_user_order=self.enforce_user_order, expand_refcat=self.expand_refcat, @@ -265,14 +265,19 @@ def process(self, input): separation=self.separation, tolerance=self.tolerance, xoffset=self.xoffset, - yoffset=self.yoffset, - align_to_abs_refcat=align_to_abs_refcat) - else: + yoffset=self.yoffset,) + except twk.TweakregError as e: + self.log.warning(str(e)) local_align_failed = True + else: + local_align_failed = False + else: + local_align_failed = True - # absolute alignment to the reference catalog - # can (and does) occur after alignment between groups - if align_to_abs_refcat: + # absolute alignment to the reference catalog + # can (and does) occur after alignment between groups + if align_to_abs_refcat: + try: correctors = \ twk.absolute_align(correctors, self.abs_refcat, images[0], abs_minobj=self.abs_minobj, @@ -285,20 +290,23 @@ def process(self, input): abs_tolerance=self.abs_tolerance, save_abs_catalog=self.save_abs_catalog, abs_catalog_output_dir=self.output_dir, - local_align_failed=local_align_failed, ) - # one final pass through all the models to update them based - # on the results of this step - self._apply_tweakreg_solution(images, correctors, - align_to_abs_refcat=align_to_abs_refcat) + except twk.TweakregError as e: + self.log.warning(str(e)) + for model in images: + model.meta.cal_step.tweakreg = "SKIPPED" + return images - except twk.TweakregError as e: - self.log.error(str(e)) + if local_align_failed and not align_to_abs_refcat: for model in images: model.meta.cal_step.tweakreg = "SKIPPED" return images + # one final pass through all the models to update them based + # on the results of this step + self._apply_tweakreg_solution(images, correctors, + align_to_abs_refcat=align_to_abs_refcat) return images