Skip to content

Commit

Permalink
Switch resample to the new drizzle package API
Browse files Browse the repository at this point in the history
  • Loading branch information
mcara committed Oct 12, 2024
1 parent 6e443ce commit 9267a3a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
9 changes: 6 additions & 3 deletions jwst/resample/gwcs_drizzle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import numpy as np

from drizzle import util
from drizzle import cdrizzle
from drizzle import cdrizzle, utils
from . import resample_utils

import logging
Expand Down Expand Up @@ -338,7 +337,7 @@ def dodrizzle(insci, input_wcs, inwht, output_wcs, outsci, outwht, outcon,
"""

# Insure that the fillval parameter gets properly interpreted for use with tdriz
if util.is_blank(str(fillval)):
if str(fillval).strip() == '':
fillval = 'NAN'
else:
fillval = str(fillval)
Expand Down Expand Up @@ -381,9 +380,13 @@ def dodrizzle(insci, input_wcs, inwht, output_wcs, outsci, outwht, outcon,

# Compute the mapping between the input and output pixel coordinates
# for use in drizzle.cdrizzle.tdriz

# pixmap = utils.calc_pixmap(input_wcs, output_wcs)
pixmap = resample_utils.calc_gwcs_pixmap(input_wcs, output_wcs, insci.shape)

# inwht[np.isnan(pixmap[:,:,0])] = 0.

print(f"Pixmap shape: {pixmap.shape}")
log.debug(f"Pixmap shape: {pixmap[:,:,0].shape}")
log.debug(f"Input Sci shape: {insci.shape}")
log.debug(f"Output Sci shape: {outsci.shape}")
Expand Down
14 changes: 7 additions & 7 deletions jwst/resample/resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import numpy as np
import psutil
from drizzle import cdrizzle, util
from drizzle import cdrizzle
from spherical_geometry.polygon import SphericalPolygon

from stdatamodels.jwst import datamodels
Expand Down Expand Up @@ -251,10 +251,10 @@ def _get_intensity_scale(self, img):
else:
iscale = 1.0
return iscale

def resample_group(self, input_models, indices):
"""Apply resample_many_to_many for one group
Parameters
----------
input_models : ModelLibrary
Expand Down Expand Up @@ -290,7 +290,7 @@ def resample_group(self, input_models, indices):
if isinstance(img, datamodels.SlitModel):
# must call this explicitly to populate area extension
# although the existence of this extension may not be necessary
img.area = img.area
img.area = img.area
iscale = self._get_intensity_scale(img)
log.debug(f'Using intensity scale iscale={iscale}')

Expand Down Expand Up @@ -339,7 +339,7 @@ def resample_many_to_many(self, input_models):
"""
output_models = []
for group_id, indices in input_models.group_indices.items():

output_model = self.resample_group(input_models, indices)

if not self.in_memory:
Expand Down Expand Up @@ -522,7 +522,7 @@ def resample_variance_arrays(self, output_model, input_models):
axis=0
)
total_weight_flat_var[mask] += weight[mask]

del model.meta.iscale
del weight
input_models.shelve(model, i)
Expand Down Expand Up @@ -753,7 +753,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 str(fillval).strip() == '':
fillval = 'NAN'
else:
fillval = str(fillval)
Expand Down
15 changes: 5 additions & 10 deletions jwst/resample/resample_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from astropy import units as u
import gwcs
from drizzle.utils import calc_pixmap

from stdatamodels.dqflags import interpret_bit_flags
from stdatamodels.jwst.datamodels.dqflags import pixel
Expand Down Expand Up @@ -124,17 +125,11 @@ def shape_from_bounding_box(bounding_box):
def calc_gwcs_pixmap(in_wcs, out_wcs, shape=None):
""" Return a pixel grid map from input frame to output frame.
"""
if shape:
bb = wcs_bbox_from_shape(shape)
log.debug("Bounding box from data shape: {}".format(bb))
else:
bb = in_wcs.bounding_box
log.debug("Bounding box from WCS: {}".format(in_wcs.bounding_box))

grid = gwcs.wcstools.grid_from_bounding_box(bb)
pixmap = np.dstack(reproject(in_wcs, out_wcs)(grid[0], grid[1]))
if shape and not np.array_equiv(shape, in_wcs.array_shape):
in_wcs = deepcopy(in_wcs)
in_wcs.array_shape = shape

return pixmap
return calc_pixmap(wcs_from=in_wcs, wcs_to=out_wcs)


def reproject(wcs1, wcs2):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ dependencies = [
"astropy>=5.3",
"BayesicFitting>=3.0.1",
"crds>=11.17.14",
"drizzle>=1.15.0",
"drizzle @ git+https://github.com/mcara/drizzle.git@redesign-api",
"gwcs>=0.21.0,<0.23.0",
"numpy>=1.22,<2.0",
"opencv-python-headless>=4.6.0.66",
Expand Down

0 comments on commit 9267a3a

Please sign in to comment.