Skip to content

Commit

Permalink
refactor. support new header blender
Browse files Browse the repository at this point in the history
  • Loading branch information
mcara committed Oct 2, 2024
1 parent 356fd22 commit 3baaf32
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 145 deletions.
4 changes: 1 addition & 3 deletions jwst/resample/gwcs_drizzle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np

from drizzle import util
from drizzle import cdrizzle
from . import resample_utils

Expand Down Expand Up @@ -336,9 +335,8 @@ def dodrizzle(insci, input_wcs, inwht, output_wcs, outsci, outwht, outcon,
output input image.
"""

# Insure that the fillval parameter gets properly interpreted for use with tdriz
if util.is_blank(str(fillval)):
if resample_utils.is_blank(str(fillval)):

Check warning on line 339 in jwst/resample/gwcs_drizzle.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/gwcs_drizzle.py#L339

Added line #L339 was not covered by tests
fillval = 'NAN'
else:
fillval = str(fillval)
Expand Down
136 changes: 82 additions & 54 deletions jwst/resample/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from astropy.io import fits

from stdatamodels.jwst import datamodels
from stcal.resample import LibModelAccess, Resample, OutputTooLargeError
from stcal.resample import LibModelAccessBase, Resample, OutputTooLargeError

from drizzle.resample import Drizzle
from stdatamodels.jwst.datamodels.dqflags import pixel
from stdatamodels.properties import ObjectNode

Check warning on line 15 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L14-L15

Added lines #L14 - L15 were not covered by tests

Expand All @@ -20,6 +19,7 @@

from . import gwcs_drizzle
from jwst.model_blender.blender import ModelBlender
from jwst.assign_wcs import util

Check warning on line 22 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L22

Added line #L22 was not covered by tests
from jwst.resample import resample_utils

log = logging.getLogger(__name__)
Expand All @@ -29,12 +29,13 @@
__all__ = [

Check warning on line 29 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L29

Added line #L29 was not covered by tests
"ResampleData",
"OutputTooLargeError",
"JWSTLibModelAccess",
"ResampleJWST",
"LibModelAccess",
"ResampleImage",
"is_imaging_wcs",
]


class JWSTLibModelAccess(LibModelAccess):
class LibModelAccess(LibModelAccessBase):
attributes_path = {

Check warning on line 39 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L38-L39

Added lines #L38 - L39 were not covered by tests
"data": "data",
"dq": "dq",
Expand Down Expand Up @@ -64,6 +65,10 @@ class JWSTLibModelAccess(LibModelAccess):
"weight_type": "meta.resample.weight_type",
"pointings": "meta.resample.pointings",
"n_coadds": "meta.resample.n_coadds",

# spectroscopy-specific:
"instrument_name": "meta.instrument.name",
"exposure_type": "meta.exposure.type",
}

def __new__(cls, *args, **kwargs):
Expand Down Expand Up @@ -135,12 +140,11 @@ def set_active_group(self, group_id=None):
self._active_group = group_id

Check warning on line 140 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L139-L140

Added lines #L139 - L140 were not covered by tests


class ResampleJWST(Resample):
class ResampleImage(Resample):
dq_flag_name_map = pixel

Check warning on line 144 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L143-L144

Added lines #L143 - L144 were not covered by tests

def __init__(self, input_models, *args, blendheaders=True,

Check warning on line 146 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L146

Added line #L146 was not covered by tests
output_model=None, output_file_name=None, in_memory=True,
**kwargs):
output_model=None, **kwargs):
if output_model is None:
self.resampled_model = datamodels.ImageModel()
self._update_output_meta_with_first_model = True

Check warning on line 150 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L148-L150

Added lines #L148 - L150 were not covered by tests
Expand All @@ -153,11 +157,20 @@ def __init__(self, input_models, *args, blendheaders=True,
enable_ctx=kwargs.get("enable_ctx", True),
enable_var=kwargs.get("enable_var", True),
)
output_model = JWSTLibModelAccess.get_model_attributes(
output_model = LibModelAccess.get_model_attributes(

Check warning on line 160 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L160

Added line #L160 was not covered by tests
output_model,
attributes=attributes,
)

if blendheaders:
self.blender = ModelBlender(

Check warning on line 166 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L165-L166

Added lines #L165 - L166 were not covered by tests
blend_ignore_attrs=[
'meta.photometry.pixelarea_steradians',
'meta.photometry.pixelarea_arcsecsq',
'meta.filename',
]
)

super().__init__(

Check warning on line 174 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L174

Added line #L174 was not covered by tests
input_models,
*args,
Expand All @@ -175,11 +188,15 @@ def add_model(self, model_info, image_model):
self._update_output_meta_with_first_model = False

Check warning on line 188 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L184-L188

Added lines #L184 - L188 were not covered by tests

# blend headers if needed:
# TODO: add blenheaders operation here
if self._blendheaders:
self.blender.accumulate(image_model)

Check warning on line 192 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L191-L192

Added lines #L191 - L192 were not covered by tests

def update_output_model_data(self):

Check warning on line 194 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L194

Added line #L194 was not covered by tests
# update data and meta for the output model:
# * arrays:
if self._blendheaders:
self.blender.finalize_model(self.resampled_model)

Check warning on line 198 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L197-L198

Added lines #L197 - L198 were not covered by tests

self.resampled_model.data = self.output_model["data"]
self.resampled_model.wht = self.output_model["wht"]

Check warning on line 201 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L200-L201

Added lines #L200 - L201 were not covered by tests

Expand All @@ -197,8 +214,13 @@ def update_output_model_data(self):
self.resampled_model.meta.cal_step.resample = 'COMPLETE'
self.resampled_model.meta.resample.pixel_scale_ratio = self._pixel_scale_ratio
self.resampled_model.meta.resample.pixfrac = self.pixfrac

Check warning on line 216 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L213-L216

Added lines #L213 - L216 were not covered by tests
_update_fits_wcsinfo(self.resampled_model)
util.update_s_region_imaging(self.resampled_model)

if is_imaging_wcs(self.resampled_model.meta.wcs):

Check warning on line 218 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L218

Added line #L218 was not covered by tests
# only for an imaging WCS:
self.update_fits_wcsinfo(self.resampled_model)
util.update_s_region_imaging(self.resampled_model)

Check warning on line 221 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L220-L221

Added lines #L220 - L221 were not covered by tests
else:
util.update_s_region_spectral(self.resampled_model)

Check warning on line 223 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L223

Added line #L223 was not covered by tests

self.resampled_model.meta.asn.pool_name = self._input_models.asn.get(

Check warning on line 225 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L225

Added line #L225 was not covered by tests
"pool_name",
Expand Down Expand Up @@ -231,49 +253,55 @@ def run(self):
self.update_output_model_data()
return self.resampled_model

Check warning on line 254 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L251-L254

Added lines #L251 - L254 were not covered by tests

@staticmethod
def update_fits_wcsinfo(model):

Check warning on line 257 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L256-L257

Added lines #L256 - L257 were not covered by tests
""" Update FITS WCS keywords of the resampled image. """
# Delete any SIP-related keywords first
pattern = r"^(cd[12]_[12]|[ab]p?_\d_\d|[ab]p?_order)$"
regex = re.compile(pattern)

Check warning on line 261 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L260-L261

Added lines #L260 - L261 were not covered by tests

keys = list(model.meta.wcsinfo.instance.keys())
for key in keys:
if regex.match(key):
del model.meta.wcsinfo.instance[key]

Check warning on line 266 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L263-L266

Added lines #L263 - L266 were not covered by tests

# Write new PC-matrix-based WCS based on GWCS model
transform = model.meta.wcs.forward_transform
model.meta.wcsinfo.crpix1 = -transform[0].offset.value + 1
model.meta.wcsinfo.crpix2 = -transform[1].offset.value + 1
model.meta.wcsinfo.cdelt1 = transform[3].factor.value
model.meta.wcsinfo.cdelt2 = transform[4].factor.value
model.meta.wcsinfo.ra_ref = transform[6].lon.value
model.meta.wcsinfo.dec_ref = transform[6].lat.value
model.meta.wcsinfo.crval1 = model.meta.wcsinfo.ra_ref
model.meta.wcsinfo.crval2 = model.meta.wcsinfo.dec_ref
model.meta.wcsinfo.pc1_1 = transform[2].matrix.value[0][0]
model.meta.wcsinfo.pc1_2 = transform[2].matrix.value[0][1]
model.meta.wcsinfo.pc2_1 = transform[2].matrix.value[1][0]
model.meta.wcsinfo.pc2_2 = transform[2].matrix.value[1][1]
model.meta.wcsinfo.ctype1 = "RA---TAN"
model.meta.wcsinfo.ctype2 = "DEC--TAN"

Check warning on line 283 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L269-L283

Added lines #L269 - L283 were not covered by tests

# Remove no longer relevant WCS keywords
rm_keys = [

Check warning on line 286 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L286

Added line #L286 was not covered by tests
'v2_ref',
'v3_ref',
'ra_ref',
'dec_ref',
'roll_ref',
'v3yangle',
'vparity',
]
for key in rm_keys:
if key in model.meta.wcsinfo.instance:
del model.meta.wcsinfo.instance[key]

Check warning on line 297 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L295-L297

Added lines #L295 - L297 were not covered by tests

def _update_fits_wcsinfo(model):
""" Update FITS WCS keywords of the resampled image. """
# Delete any SIP-related keywords first
pattern = r"^(cd[12]_[12]|[ab]p?_\d_\d|[ab]p?_order)$"
regex = re.compile(pattern)

keys = list(model.meta.wcsinfo.instance.keys())
for key in keys:
if regex.match(key):
del model.meta.wcsinfo.instance[key]

# Write new PC-matrix-based WCS based on GWCS model
transform = model.meta.wcs.forward_transform
model.meta.wcsinfo.crpix1 = -transform[0].offset.value + 1
model.meta.wcsinfo.crpix2 = -transform[1].offset.value + 1
model.meta.wcsinfo.cdelt1 = transform[3].factor.value
model.meta.wcsinfo.cdelt2 = transform[4].factor.value
model.meta.wcsinfo.ra_ref = transform[6].lon.value
model.meta.wcsinfo.dec_ref = transform[6].lat.value
model.meta.wcsinfo.crval1 = model.meta.wcsinfo.ra_ref
model.meta.wcsinfo.crval2 = model.meta.wcsinfo.dec_ref
model.meta.wcsinfo.pc1_1 = transform[2].matrix.value[0][0]
model.meta.wcsinfo.pc1_2 = transform[2].matrix.value[0][1]
model.meta.wcsinfo.pc2_1 = transform[2].matrix.value[1][0]
model.meta.wcsinfo.pc2_2 = transform[2].matrix.value[1][1]
model.meta.wcsinfo.ctype1 = "RA---TAN"
model.meta.wcsinfo.ctype2 = "DEC--TAN"

# Remove no longer relevant WCS keywords
rm_keys = [
'v2_ref',
'v3_ref',
'ra_ref',
'dec_ref',
'roll_ref',
'v3yangle',
'vparity',
]
for key in rm_keys:
if key in model.meta.wcsinfo.instance:
del model.meta.wcsinfo.instance[key]

def is_imaging_wcs(wcs):
imaging = all(

Check warning on line 301 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L300-L301

Added lines #L300 - L301 were not covered by tests
ax == 'SPATIAL' for ax in wcs.output_frame.axes_type
)
return imaging

Check warning on line 304 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L304

Added line #L304 was not covered by tests

####################################################
# Code below was left for spectral data for now #
Expand Down Expand Up @@ -1005,7 +1033,7 @@ def drizzle_arrays(insci, inwht, input_wcs, output_wcs, outsci, outwht,
"""

# Insure that the fillval parameter gets properly interpreted for use with tdriz
if util.is_blank(str(fillval)):
if resample_utils.is_blank(str(fillval)):

Check warning on line 1036 in jwst/resample/resample.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample.py#L1036

Added line #L1036 was not covered by tests
fillval = 'NAN'
else:
fillval = str(fillval)
Expand Down
54 changes: 43 additions & 11 deletions jwst/resample/resample_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@

from jwst.assign_wcs.util import compute_scale, wrap_ra
from jwst.resample import resample_utils
from jwst.resample.resample import ResampleData
from jwst.resample.resample import (

Check warning on line 20 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L20

Added line #L20 was not covered by tests
ResampleImage,
LibModelAccess,
)
from stcal.alignment.util import (

Check warning on line 24 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L24

Added line #L24 was not covered by tests
compute_scale,
wcs_bbox_from_shape,
)


log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)

_S2C = SphericalToCartesian()

__all__ = ["ResampleSpecData"]
__all__ = ["ResampleSpec"]

Check warning on line 35 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L35

Added line #L35 was not covered by tests


class ResampleSpecData(ResampleData):
class ResampleSpec(ResampleImage):

Check warning on line 38 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L38

Added line #L38 was not covered by tests
"""
This is the controlling routine for the resampling process for spectral data.
Expand All @@ -45,9 +52,12 @@ class ResampleSpecData(ResampleData):
a record of metadata from all input models.
"""

def __init__(self, input_models, output=None, single=False, blendheaders=False,
pixfrac=1.0, kernel="square", fillval=0, wht_type="ivm",
good_bits=0, pscale_ratio=1.0, pscale=None, **kwargs):
def __init__(self, input_models, *args, output_model=None, **kwargs):

Check warning on line 55 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L55

Added line #L55 was not covered by tests
# def __init__(self, input_models, pixfrac=1.0, kernel="square",
# fillval=0.0, wht_type="ivm", good_bits=0,
# output_wcs=None, wcs_pars=None, output_model=None,
# accumulate=False, enable_ctx=True, enable_var=True,
# allowed_memory=None):
"""
Parameters
----------
Expand All @@ -60,6 +70,30 @@ def __init__(self, input_models, output=None, single=False, blendheaders=False,
kwargs : dict
Other parameters
"""
if output_model is None:
self.resampled_model = datamodels.SlitModel()
self._update_output_meta_with_first_model = True

Check warning on line 75 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L73-L75

Added lines #L73 - L75 were not covered by tests
else:
self.resampled_model = output_model
self._update_output_meta_with_first_model = False

Check warning on line 78 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L77-L78

Added lines #L77 - L78 were not covered by tests
# convert output_model to dictionary:
attributes = ResampleImage.output_model_attributes(

Check warning on line 80 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L80

Added line #L80 was not covered by tests
accumulate=False,
enable_ctx=kwargs.get("enable_ctx", True),
enable_var=kwargs.get("enable_var", True),
)
output_model = LibModelAccess.get_model_attributes(

Check warning on line 85 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L85

Added line #L85 was not covered by tests
output_model,
attributes=attributes,
)

super().__init__(

Check warning on line 90 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L90

Added line #L90 was not covered by tests
input_models,
*args,
output_model=output_model,
**kwargs
)

self.output_dir = None
self.output_filename = output
if output is not None and '.fits' not in str(output):
Expand All @@ -68,8 +102,6 @@ def __init__(self, input_models, output=None, single=False, blendheaders=False,
self.intermediate_suffix = 'outlier_s2d'

self.pscale_ratio = pscale_ratio
self.single = single
self.blendheaders = blendheaders
self.pixfrac = pixfrac
self.kernel = kernel
self.fillval = fillval
Expand Down Expand Up @@ -441,7 +473,7 @@ def build_nirspec_output_wcs(self, input_models, refmodel=None):

# Compute bounding box and output array shape.
self.data_size = (ny, n_lam)
bounding_box = resample_utils.wcs_bbox_from_shape(self.data_size)
bounding_box = wcs_bbox_from_shape(self.data_size)

Check warning on line 476 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L476

Added line #L476 was not covered by tests
output_wcs.bounding_box = bounding_box
output_wcs.array_shape = self.data_size

Expand Down Expand Up @@ -728,7 +760,7 @@ def build_interpolated_output_wcs(self, input_models):
# turn the size into a numpy shape in (y, x) order
output_wcs.array_shape = output_array_size[::-1]
output_wcs.pixel_shape = output_array_size
bounding_box = resample_utils.wcs_bbox_from_shape(output_array_size[::-1])
bounding_box = wcs_bbox_from_shape(output_array_size[::-1])

Check warning on line 763 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L763

Added line #L763 was not covered by tests
output_wcs.bounding_box = bounding_box

return output_wcs
Expand Down Expand Up @@ -837,7 +869,7 @@ def build_nirspec_lamp_output_wcs(self, input_models):
# turn the size into a numpy shape in (y, x) order
output_wcs.array_shape = output_array_size[::-1]
output_wcs.pixel_shape = output_array_size
bounding_box = resample_utils.wcs_bbox_from_shape(output_array_size[::-1])
bounding_box = wcs_bbox_from_shape(output_array_size[::-1])

Check warning on line 872 in jwst/resample/resample_spec.py

View check run for this annotation

Codecov / codecov/patch

jwst/resample/resample_spec.py#L872

Added line #L872 was not covered by tests
output_wcs.bounding_box = bounding_box

return output_wcs
Expand Down
Loading

0 comments on commit 3baaf32

Please sign in to comment.