Skip to content

Commit

Permalink
moved update_s_region_ out of jwst, altered step flow logic
Browse files Browse the repository at this point in the history
  • Loading branch information
emolter committed Jul 9, 2024
1 parent 43cc0a7 commit 2c80121
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 66 deletions.
3 changes: 2 additions & 1 deletion jwst/assign_wcs/assign_wcs.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
44 changes: 1 addition & 43 deletions jwst/assign_wcs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion jwst/cube_build/cube_build_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
3 changes: 2 additions & 1 deletion jwst/lib/set_telescope_pointing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion jwst/resample/resample_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions jwst/tweakreg/tests/test_multichip_jwst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand Down
40 changes: 24 additions & 16 deletions jwst/tweakreg/tweakreg_step.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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


Expand Down

0 comments on commit 2c80121

Please sign in to comment.