8000 Merge the two update_wcsinfo functions into one by mcara · Pull Request #7412 · spacetelescope/jwst · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Merge the two update_wcsinfo functions into one #7412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ assign_wcs
- Updated the loading of NIRSpec MSA configuration data to assign the source_id
for each slitlet from the shutter entry that contains the primary/main source. [#7379]

- Added approximated imaging FITS WCS to the grism image headers. [#7373]
- Added approximated imaging FITS WCS to the grism image headers. [#7373, #7412]

associations
------------
Expand Down
74 changes: 36 additions & 38 deletions jwst/assign_wcs/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,10 +1251,14 @@ def in_ifu_slice(slice_wcs, ra, dec, lam):
return onslice_ind


def fits_wcsinfo(wcs, max_pix_error=0.01, degree=None, npoints=32,
crpix=None, projection='TAN', **kwargs):
def update_fits_wcsinfo(datamodel, max_pix_error=0.01, degree=None, npoints=32,
crpix=None, projection='TAN', imwcs=None, **kwargs):
"""
Compute a FITS WCS + SIP approximation of the GWCS object.
Update ``datamodel.meta.wcsinfo`` based on a FITS WCS + SIP approximation
of a GWCS object. By default, this function will approximate
the datamodel's GWCS object stored in ``datamodel.meta.wcs`` but it can
also approximate a user-supplied GWCS object when provided via
the ``imwcs`` parameter.

The default mode in using this attempts to achieve roughly 0.01 pixel
accuracy over the entire image.
Expand All @@ -1275,6 +1279,13 @@ def fits_wcsinfo(wcs, max_pix_error=0.01, degree=None, npoints=32,
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``
is used to compute FITS WCS + SIP approximation. When ``imwcs`` is
not `None` then computed FITS WCS will be an approximation of the WCS
provided through the ``imwcs`` parameter.

max_pix_error : float, optional
Maximum allowed error over the domain of the pixel array. This
error is the equivalent pixel error that corresponds to the maximum
Expand Down Expand Up @@ -1318,6 +1329,17 @@ def fits_wcsinfo(wcs, max_pix_error=0.01, degree=None, npoints=32,
projection models inherited from
:py:class:`~astropy.modeling.projections.Pix2SkyProjection`.

imwcs : `gwcs.WCS`, None, optional
Imaging GWCS object for WFSS mode whose FITS WCS approximation should
be computed and stored in the ``datamodel.meta.wcsinfo`` field.
When ``imwcs`` is `None` then WCS from ``datamodel.meta.wcs``
will be used.

.. warning::

Used with WFSS modes only. For other modes, supplying a different
WCS from ``datamodel.meta.wcs`` will result in the GWCS and
FITS WCS descriptions to diverge.

Other Parameters
----------------
Expand Down Expand Up @@ -1371,6 +1393,16 @@ def fits_wcsinfo(wcs, max_pix_error=0.01, degree=None, npoints=32,
For more details, see :py:meth:`~gwcs.wcs.WCS.to_fits_sip`.

"""
if crpix is None:
crpix = [datamodel.meta.wcsinfo.crpix1, datamodel.meta.wcsinfo.crpix2]
if None in crpix:
crpix = None

# For WFSS modes the imaging WCS is passed as an argument.
# For imaging modes it is retrieved from the datamodel.
if imwcs is None:
imwcs = datamodel.meta.wcs

# make a copy of kwargs:
kwargs = {k: v for k, v in kwargs.items()}

Expand All @@ -1384,7 +1416,7 @@ def fits_wcsinfo(wcs, max_pix_error=0.01, degree=None, npoints=32,
if degree is None:
degree = range(1, _MAX_SIP_DEGREE)

hdr = wcs.to_fits_sip(
hdr = imwcs.to_fits_sip(
max_pix_error=max_pix_error,
degree=degree,
max_inv_pix_error=max_inv_pix_error,
Expand All @@ -1393,24 +1425,7 @@ def fits_wcsinfo(wcs, max_pix_error=0.01, degree=None, npoints=32,
crpix=crpix,
**kwargs
)
return hdr


def update_fits_wcsinfo(datamodel, max_pix_error=0.01, degree=None, npoints=32,
crpix=None, projection='TAN', imwcs=None, **kwargs):

if crpix is None:
crpix = [datamodel.meta.wcsinfo.crpix1, datamodel.meta.wcsinfo.crpix2]
if None in crpix:
crpix = None

# For WFSS modes the imaging WCS is passed as an argument.
# For imaging modes it is retrieved from the datamodel.
if imwcs is None:
imwcs = datamodel.meta.wcs

hdr = fits_wcsinfo(imwcs, max_pix_error=max_pix_error, degree=degree, npoints=npoints,
crpix=crpix, projection=projection, **kwargs)
# update meta.wcsinfo with FITS keywords except for naxis*
del hdr['naxis*']

Expand All @@ -1435,23 +1450,6 @@ def update_fits_wcsinfo(datamodel, max_pix_error=0.01, degree=None, npoints=32,
return hdr


update_fits_wcsinfo.__doc__ = """
Update the datamodel and FITS headers with FITS WCS approximation.

For imaging and WFSS data models *only*, update data model's ``meta.wcsinfo``
attribute using a FITS SIP approximation of the current data model's imaging
GWCS from ``meta.wcs``.

Parameters
----------
datamodel : `ImageModel`
The input data model, imaging or WFSS mode.

imwcs : `WCS` or None
The GWCS object for imaging mode. Used with WFSS modes only.
""" + "".join(fits_wcsinfo.__doc__.split('Parameters\n ----------')[1:])


def wfss_imaging_wcs(wfss_model, imaging, bbox=None, **kwargs):
""" Add a FITS WCS approximation for imaging mode to WFSS headers.

Expand Down
0