Skip to content

Commit

Permalink
Make sure bounding box is attached as order F
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson committed Nov 15, 2024
1 parent 315a2e5 commit 40d27d7
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 38 deletions.
9 changes: 7 additions & 2 deletions jwst/assign_wcs/fgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from astropy import units as u
from astropy import coordinates as coord
from astropy.modeling import bind_bounding_box
from gwcs import coordinate_frames as cf

from stdatamodels.jwst.datamodels import DistortionModel
Expand Down Expand Up @@ -78,7 +79,10 @@ def imaging(input_model, reference_files):
if subarray2full is not None:
# Assign a bounding_box based on subarray's xsize and ysize
distortion = subarray2full | distortion
distortion.bounding_box = bounding_box_from_subarray(input_model)

# Bind the bounding box to the distortion model using the bounding box ordering
# used by GWCS. This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, bounding_box_from_subarray(input_model, order="F"), order="F")

# Compute differential velocity aberration (DVA) correction:
va_corr = pointing.dva_corr_model(
Expand Down Expand Up @@ -106,7 +110,8 @@ def imaging_distortion(input_model, reference_files):
try:
transform.bounding_box
except NotImplementedError:
transform.bounding_box = transform_bbox_from_shape(input_model.data.shape)
bind_bounding_box(transform, transform_bbox_from_shape(input_model.data.shape, order="F"), order="F")

dist.close()
return transform

Expand Down
25 changes: 14 additions & 11 deletions jwst/assign_wcs/miri.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os.path
import logging
import numpy as np
from astropy.modeling import bind_bounding_box
from astropy.modeling import models
from astropy import coordinates as coord
from astropy import units as u
Expand Down Expand Up @@ -79,16 +80,17 @@ def imaging(input_model, reference_files):
subarray2full = subarray_transform(input_model)
if subarray2full is not None:
distortion = subarray2full | distortion
distortion.bounding_box = bounding_box_from_subarray(input_model)

# Bind the bounding box to the distortion model using the bounding box ordering
# used by GWCS. This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, bounding_box_from_subarray(input_model, order="F"), order="F")
else:
# TODO: remove setting the bounding box if it is set in the new ref file.
try:
bb = distortion.bounding_box
distortion.bounding_box
except NotImplementedError:
shape = input_model.data.shape
# Note: Since bounding_box is attached to the model here it's in reverse order.
bb = ((-0.5, shape[0] - 0.5), (3.5, shape[1] - 4.5))
distortion.bounding_box = bb
bind_bounding_box(distortion, ((3.5, shape[1] - 4.5), (-0.5, shape[0] - 0.5)), order="F")

# Compute differential velocity aberration (DVA) correction:
va_corr = pointing.dva_corr_model(
Expand Down Expand Up @@ -147,10 +149,11 @@ def imaging_distortion(input_model, reference_files):

if col_offset is not None and row_offset is not None:
distortion = models.Shift(col_offset) & models.Shift(row_offset) | distortion
if bbox is None:
distortion.bounding_box = transform_bbox_from_shape(input_model.data.shape)
else:
distortion.bounding_box = bbox

# Bind the bounding box to the distortion model using the bounding box ordering used by GWCS.
# This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, transform_bbox_from_shape(input_model.data.shape, order="F") if bbox is None else bbox, order="F")

return distortion


Expand Down Expand Up @@ -365,7 +368,7 @@ def lrs_xytoabl(input_model, reference_files):
dettoabl.inverse = models.Mapping((0, 1, 2, 0, 1, 2)) | aa & bb

# Bounding box is the subarray bounding box, because we're assuming subarray coordinates passed in
dettoabl.bounding_box = bb_sub[::-1]
bind_bounding_box(dettoabl, bb_sub, order="F")

return dettoabl

Expand Down Expand Up @@ -478,7 +481,7 @@ def ifu(input_model, reference_files):
tel2sky = pointing.v23tosky(input_model) & models.Identity(1)

# Put the transforms together into a single transform
det2abl.bounding_box = transform_bbox_from_shape(input_model.data.shape)
bind_bounding_box(det2abl, transform_bbox_from_shape(input_model.data.shape, order="F"), order="F")
pipeline = [(detector, det2abl),
(miri_focal, abl2v2v3l),
(v2v3, va_corr),
Expand Down
19 changes: 13 additions & 6 deletions jwst/assign_wcs/nircam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from astropy import coordinates as coord
from astropy import units as u
from astropy.modeling import bind_bounding_box
from astropy.modeling.models import Identity, Const1D, Mapping, Shift
import gwcs.coordinate_frames as cf

Expand Down Expand Up @@ -84,7 +85,10 @@ def imaging(input_model, reference_files):
subarray2full = subarray_transform(input_model)
if subarray2full is not None:
distortion = subarray2full | distortion
distortion.bounding_box = bounding_box_from_subarray(input_model)

# Bind the bounding box to the distortion model using the bounding box ordering
# used by GWCS. This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, bounding_box_from_subarray(input_model, order="F"), order="F")

# Compute differential velocity aberration (DVA) correction:
va_corr = pointing.dva_corr_model(
Expand Down Expand Up @@ -121,7 +125,9 @@ def imaging_distortion(input_model, reference_files):
transform = dist.model

try:
bbox = transform.bounding_box
# Purposefully grab the bounding box tuple from the transform model in the
# GWCS ordering
bbox = transform.bounding_box.bounding_box(order="F")
except NotImplementedError:
# Check if the transform in the reference file has a ``bounding_box``.
# If not set a ``bounding_box`` equal to the size of the image after
Expand All @@ -146,10 +152,11 @@ def imaging_distortion(input_model, reference_files):
transform = Shift(col_offset) & Shift(row_offset) | transform
else:
log.debug("No match in fitleroffset file.")
if bbox is None:
transform.bounding_box = transform_bbox_from_shape(input_model.data.shape)
else:
transform.bounding_box = bbox

# Bind the bounding box to the distortion model using the bounding box ordering used by GWCS.
# This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(transform, transform_bbox_from_shape(input_model.data.shape, order="F") if bbox is None else bbox, order="F")

return transform


Expand Down
27 changes: 16 additions & 11 deletions jwst/assign_wcs/niriss.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import asdf
from astropy import coordinates as coord
from astropy import units as u
from astropy.modeling import bind_bounding_box
from astropy.modeling.models import Const1D, Mapping, Identity, Shift
from astropy.modeling.bounding_box import CompoundBoundingBox
import gwcs.coordinate_frames as cf
Expand Down Expand Up @@ -191,11 +192,11 @@ def niriss_soss(input_model, reference_files):
cm_order2 = subarray2full | cm_order2
cm_order3 = subarray2full | cm_order3

bbox = ((-0.5, input_model.meta.subarray.ysize - 0.5),
(-0.5, input_model.meta.subarray.xsize - 0.5))
cm_order1.bounding_box = bbox
cm_order2.bounding_box = bbox
cm_order3.bounding_box = bbox
bbox = ((-0.5, input_model.meta.subarray.xsize - 0.5),
(-0.5, input_model.meta.subarray.ysize - 0.5))
bind_bounding_box(cm_order1, bbox, order='F')
bind_bounding_box(cm_order2, bbox, order='F')
bind_bounding_box(cm_order3, bbox, order='F')

# Define the transforms, they should accept (x,y) and return (ra, dec, lambda)
soss_model = NirissSOSSModel([1, 2, 3],
Expand Down Expand Up @@ -253,7 +254,10 @@ def imaging(input_model, reference_files):
subarray2full = subarray_transform(input_model)
if subarray2full is not None:
distortion = subarray2full | distortion
distortion.bounding_box = bounding_box_from_subarray(input_model)

# Bind the bounding box to the distortion model using the bounding box ordering
# used by GWCS. This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, bounding_box_from_subarray(input_model, order="F"), order="F")

tel2sky = pointing.v23tosky(input_model)
pipeline = [(detector, distortion),
Expand Down Expand Up @@ -282,7 +286,7 @@ def imaging_distortion(input_model, reference_files):
distortion = dist.model

try:
bbox = distortion.bounding_box
bbox = distortion.bounding_box.bounding_box(order='F')
except NotImplementedError:
# Check if the transform in the reference file has a ``bounding_box``.
# If not set a ``bounding_box`` equal to the size of the image after
Expand All @@ -308,10 +312,11 @@ def imaging_distortion(input_model, reference_files):
distortion = Shift(col_offset) & Shift(row_offset) | distortion
else:
log.debug("No match in fitleroffset file.")
if bbox is None:
distortion.bounding_box = transform_bbox_from_shape(input_model.data.shape)
else:
distortion.bounding_box = bbox

# Bind the bounding box to the distortion model using the bounding box ordering used by GWCS.
# This makes it clear the bounding box is set correctly to GWCS
bind_bounding_box(distortion, transform_bbox_from_shape(input_model.data.shape, order="F") if bbox is None else bbox, order="F")

return distortion


Expand Down
19 changes: 13 additions & 6 deletions jwst/assign_wcs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,7 @@ def get_num_msa_open_shutters(shutter_state):
return num


def transform_bbox_from_shape(shape):
def transform_bbox_from_shape(shape, order="C"):
"""Create a bounding box from the shape of the data.
This is appropriate to attached to a transform.
Expand All @@ -730,15 +730,19 @@ def transform_bbox_from_shape(shape):
----------
shape : tuple
The shape attribute from a `numpy.ndarray` array
order : str
The order of the array. Either "C" or "F".
Returns
-------
bbox : tuple
Bounding box in y, x order.
Bounding box in y, x order if order is "C" (default)
Boundsing box in x, y order if order is "F"
"""
bbox = ((-0.5, shape[-2] - 0.5),
(-0.5, shape[-1] - 0.5))
return bbox

return bbox if order == "C" else bbox[::-1]


def wcs_bbox_from_shape(shape):
Expand All @@ -760,7 +764,7 @@ def wcs_bbox_from_shape(shape):
return bbox


def bounding_box_from_subarray(input_model):
def bounding_box_from_subarray(input_model, order='C'):
"""Create a bounding box from the subarray size.
Note: The bounding_box assumes full frame coordinates.
Expand All @@ -771,11 +775,14 @@ def bounding_box_from_subarray(input_model):
----------
input_model : `~jwst.datamodels.JwstDataModel`
The data model.
order : str
The order of the array. Either "C" or "F".
Returns
-------
bbox : tuple
Bounding box in y, x order.
Bounding box in y, x order if order is "C" (default)
Boundsing box in x, y order if order is "F"
"""
bb_xstart = -0.5
bb_xend = -0.5
Expand All @@ -788,7 +795,7 @@ def bounding_box_from_subarray(input_model):
bb_yend = input_model.meta.subarray.ysize - 0.5

bbox = ((bb_ystart, bb_yend), (bb_xstart, bb_xend))
return bbox
return bbox if order == 'C' else bbox[::-1]


def update_s_region_imaging(model):
Expand Down
2 changes: 1 addition & 1 deletion jwst/barshadow/bar_shadow.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def create_empty_shadow_array(nshutters):
nshutters: int
The length of the slit in shutters
https://github.com/spacetelescope/gwcs/actions/runs/11661031952/job/32464576263
Returns:
empty_shadow: nddata_array
Expand Down
3 changes: 2 additions & 1 deletion jwst/extract_2d/grisms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import numpy as np

from astropy.modeling import bind_bounding_box
from astropy.modeling.models import Shift, Const1D, Mapping
from gwcs.wcstools import grid_from_bounding_box
from gwcs.utils import _toindex
Expand Down Expand Up @@ -470,7 +471,7 @@ def extract_grism_objects(input_model,
else:
var_flat = None

tr.bounding_box = util.transform_bbox_from_shape(ext_data.shape)
bind_bounding_box(tr, util.transform_bbox_from_shape(ext_data.shape, order="F"), order='F')
subwcs.set_transform('grism_detector', 'detector', tr)

new_slit = datamodels.SlitModel(data=ext_data,
Expand Down

0 comments on commit 40d27d7

Please sign in to comment.