Skip to content

Commit

Permalink
remove dependence on ModelContainer, operate on one model at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
emolter committed Jul 2, 2024
1 parent 9ce63b4 commit a4e86c0
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 426 deletions.
170 changes: 84 additions & 86 deletions src/stcal/alignment/resample_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,89 +42,87 @@ def calc_pixmap(in_wcs, out_wcs, shape=None):
return np.dstack(transform_function(grid[0], grid[1]))


# is this allowed in stcal, since it operates on a datamodel?
# seems ok. jump step for example does use models
def make_output_wcs(input_models, ref_wcs=None,
pscale_ratio=None, pscale=None, rotation=None, shape=None,
crpix=None, crval=None):
"""Generate output WCS here based on footprints of all input WCS objects.
Parameters
----------
input_models : list of `DataModel objects`
Each datamodel must have a ~gwcs.WCS object.
pscale_ratio : float, optional
Ratio of input to output pixel scale. Ignored when ``pscale``
is provided.
pscale : float, None, optional
Absolute pixel scale in degrees. When provided, overrides
``pscale_ratio``.
rotation : float, None, optional
Position angle of output image Y-axis relative to North.
A value of 0.0 would orient the final output image to be North up.
The default of `None` specifies that the images will not be rotated,
but will instead be resampled in the default orientation for the camera
with the x and y axes of the resampled image corresponding
approximately to the detector axes.
shape : tuple of int, None, optional
Shape of the image (data array) using ``numpy.ndarray`` convention
(``ny`` first and ``nx`` second). This value will be assigned to
``pixel_shape`` and ``array_shape`` properties of the returned
WCS object.
crpix : tuple of float, None, optional
Position of the reference pixel in the image array. If ``crpix`` is not
specified, it will be set to the center of the bounding box of the
returned WCS object.
crval : tuple of float, None, optional
Right ascension and declination of the reference pixel. Automatically
computed if not provided.
Returns
-------
output_wcs : object
WCS object, with defined domain, covering entire set of input frames
"""
if ref_wcs is None:
wcslist = [i.meta.wcs for i in input_models]
for w, i in zip(wcslist, input_models):
if w.bounding_box is None:
w.bounding_box = util.wcs_bbox_from_shape(i.data.shape)
naxes = wcslist[0].output_frame.naxes

if naxes != 2:
msg = f"Output WCS needs 2 spatial axes \
but the supplied WCS has {naxes} axes."
raise RuntimeError(msg)

output_wcs = util.wcs_from_footprints(
input_models,
pscale_ratio=pscale_ratio,
pscale=pscale,
rotation=rotation,
shape=shape,
crpix=crpix,
crval=crval
)

else:
naxes = ref_wcs.output_frame.naxes
if naxes != 2:
msg = f"Output WCS needs 2 spatial axes \
but the supplied WCS has {naxes} axes."
raise RuntimeError(msg)
output_wcs = deepcopy(ref_wcs)
if shape is not None:
output_wcs.array_shape = shape

# Check that the output data shape has no zero length dimensions
if not np.prod(output_wcs.array_shape):
msg = f"Invalid output frame shape: {tuple(output_wcs.array_shape)}"
raise ValueError(msg)

return output_wcs
# def make_output_wcs(input_models, ref_wcs=None,
# pscale_ratio=None, pscale=None, rotation=None, shape=None,
# crpix=None, crval=None):
# """Generate output WCS here based on footprints of all input WCS objects.

# Parameters
# ----------
# input_models : list of `DataModel objects`
# Each datamodel must have a ~gwcs.WCS object.

# pscale_ratio : float, optional
# Ratio of input to output pixel scale. Ignored when ``pscale``
# is provided.

# pscale : float, None, optional
# Absolute pixel scale in degrees. When provided, overrides
# ``pscale_ratio``.

# rotation : float, None, optional
# Position angle of output image Y-axis relative to North.
# A value of 0.0 would orient the final output image to be North up.
# The default of `None` specifies that the images will not be rotated,
# but will instead be resampled in the default orientation for the camera
# with the x and y axes of the resampled image corresponding
# approximately to the detector axes.

# shape : tuple of int, None, optional
# Shape of the image (data array) using ``numpy.ndarray`` convention
# (``ny`` first and ``nx`` second). This value will be assigned to
# ``pixel_shape`` and ``array_shape`` properties of the returned
# WCS object.

# crpix : tuple of float, None, optional
# Position of the reference pixel in the image array. If ``crpix`` is not
# specified, it will be set to the center of the bounding box of the
# returned WCS object.

# crval : tuple of float, None, optional
# Right ascension and declination of the reference pixel. Automatically
# computed if not provided.

# Returns
# -------
# output_wcs : object
# WCS object, with defined domain, covering entire set of input frames
# """
# if ref_wcs is None:
# wcslist = [i.meta.wcs for i in input_models]
# for w, i in zip(wcslist, input_models):
# if w.bounding_box is None:
# w.bounding_box = util.wcs_bbox_from_shape(i.data.shape)
# naxes = wcslist[0].output_frame.naxes

# if naxes != 2:
# msg = f"Output WCS needs 2 spatial axes \
# but the supplied WCS has {naxes} axes."
# raise RuntimeError(msg)

# output_wcs = util.wcs_from_footprints(
# input_models,
# pscale_ratio=pscale_ratio,
# pscale=pscale,
# rotation=rotation,
# shape=shape,
# crpix=crpix,
# crval=crval
# )

# else:
# naxes = ref_wcs.output_frame.naxes
# if naxes != 2:
# msg = f"Output WCS needs 2 spatial axes \
# but the supplied WCS has {naxes} axes."
# raise RuntimeError(msg)
# output_wcs = deepcopy(ref_wcs)
# if shape is not None:
# output_wcs.array_shape = shape

# # Check that the output data shape has no zero length dimensions
# if not np.prod(output_wcs.array_shape):
# msg = f"Invalid output frame shape: {tuple(output_wcs.array_shape)}"
# raise ValueError(msg)

# return output_wcs
2 changes: 0 additions & 2 deletions src/stcal/alignment/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,6 @@ def update_fits_wcsinfo(datamodel, max_pix_error=0.01, degree=None,
Parameters
----------
datamodel : `ImageModel`
The input data model for imaging or WFSS mode whose ``meta.wcsinfo``
field should be updated from GWCS. By default, ``datamodel.meta.wcs``
Expand Down Expand Up @@ -687,7 +686,6 @@ def update_fits_wcsinfo(datamodel, max_pix_error=0.01, degree=None,
Notes
-----
Use of this requires a judicious choice of required accuracies.
Attempts to use higher degrees (~7 or higher) will typically fail due
to floating point problems that arise with high powers.
Expand Down
44 changes: 16 additions & 28 deletions src/stcal/tweakreg/astrometric_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.table import Table

Check warning on line 7 in src/stcal/tweakreg/astrometric_utils.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/tweakreg/astrometric_utils.py#L4-L7

Added lines #L4 - L7 were not covered by tests
from astropy.time import Time

from stcal.alignment import compute_fiducial, resample_utils
from stcal.alignment import compute_fiducial

Check warning on line 9 in src/stcal/tweakreg/astrometric_utils.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/tweakreg/astrometric_utils.py#L9

Added line #L9 was not covered by tests

ASTROMETRIC_CAT_ENVVAR = "ASTROMETRIC_CATALOG_URL"
DEF_CAT_URL = "http://gsss.stsci.edu/webservices"

Check warning on line 12 in src/stcal/tweakreg/astrometric_utils.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/tweakreg/astrometric_utils.py#L11-L12

Added lines #L11 - L12 were not covered by tests
Expand All @@ -31,20 +30,27 @@


def create_astrometric_catalog(

Check warning on line 32 in src/stcal/tweakreg/astrometric_utils.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/tweakreg/astrometric_utils.py#L32

Added line #L32 was not covered by tests
input_models,
wcs,
epoch,
catalog="GAIADR3",
output="ref_cat.ecsv",
gaia_only=False,
table_format="ascii.ecsv",
existing_wcs=None,
num_sources=None,
epoch=None):
num_sources=None):
"""Create an astrometric catalog that covers the inputs' field-of-view.
Parameters
----------
input_models : list of `~jwst.datamodel.JwstDataModel`
Each datamodel must have a ~gwcs.WCS object.
wcs : `~astropy.wcs.WCS`
WCS object specified by the user as generated by
`resample.resample_utils.make_output_wcs`. This will typically
have the same plate-scale and orientation as the first member in the
list of input images to make_output_wcs. Fortunately, for alignment,
this doesn't matter since no resampling of data will be performed.
epoch : float
Reference epoch used to update the coordinates for proper motion
(in decimal year).
catalog : str, optional
Name of catalog to extract astrometric positions for sources in the
Expand All @@ -58,19 +64,12 @@ def create_astrometric_catalog(
gaia_only : bool, optional
Specify whether or not to only use sources from GAIA in output catalog
existing_wcs : model
existing WCS object specified by the user as generated by
`resample.resample_utils.make_output_wcs`
num_sources : int
Maximum number of brightest/faintest sources to return in catalog.
If `num_sources` is negative, return that number of the faintest
sources. By default, all sources are returned.
epoch : float, optional
Reference epoch used to update the coordinates for proper motion
(in decimal year). If `None` then the epoch is obtained from
the metadata.
Notes
-----
Expand All @@ -83,20 +82,9 @@ def create_astrometric_catalog(
Astropy Table object of the catalog
"""
# start by creating a composite field-of-view for all inputs
# This default output WCS will have the same plate-scale and orientation
# as the first member in the list.
# Fortunately, for alignment, this doesn't matter since no resampling of
# data will be performed.
outwcs = existing_wcs if existing_wcs is not None \
else resample_utils.make_output_wcs(input_models)
radius, fiducial = compute_radius(outwcs)
radius, fiducial = compute_radius(wcs)

Check warning on line 85 in src/stcal/tweakreg/astrometric_utils.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/tweakreg/astrometric_utils.py#L85

Added line #L85 was not covered by tests

# perform query for this field-of-view
epoch = (
epoch
if epoch is not None
else Time(input_models[0].meta.observation.date).decimalyear
)
ref_dict = get_catalog(

Check warning on line 88 in src/stcal/tweakreg/astrometric_utils.py

View check run for this annotation

Codecov / codecov/patch

src/stcal/tweakreg/astrometric_utils.py#L88

Added line #L88 was not covered by tests
fiducial[0],
fiducial[1],
Expand Down
Loading

0 comments on commit a4e86c0

Please sign in to comment.