-
Notifications
You must be signed in to change notification settings - Fork 171
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
JP-3330: Add NIRSpec wavelength corrections to slit WCS #8376
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a4b23a9
Update wavecorr to add a zeropoint correction to the wcs pipeline
hayescr 788ea61
updating wavecorr tests
hayescr 882eafc
Code style fixes
melanieclarke 86075ba
Updating comments
hayescr 257d341
Added wcorr wcs frame and check on transform inverse
hayescr e57f82f
documentation updates
hayescr 4779ca8
updated changelog
hayescr e420506
update get_wavelengths to use new wavecorr
hayescr 280f6df
refactor FS flatfield and pathloss to use get_wavelengths
hayescr 70e72a0
updated documentation and the changelog
hayescr a6f1175
remove unused import
hayescr 910210f
record wavecorr completion for each mos slit
hayescr d03ef6f
Minor change log updates
hayescr e66388f
catch missing wcs case in get_wavelengths
hayescr 88ff501
Merge branch 'master' into nirspec_wavecorr_offsets
hbushouse 31ef63c
Merge branch 'master' into nirspec_wavecorr_offsets
hayescr 772ed05
specify NIRSpec in change log
hayescr 470fd2e
Merge branch 'master' into nirspec_wavecorr_offsets
nden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import numpy as np | ||
from numpy.testing import assert_allclose | ||
|
||
from astropy import units as u | ||
from astropy import coordinates as coord | ||
from astropy.modeling.models import Mapping, Identity, Shift, Scale | ||
from gwcs import wcstools, wcs | ||
from gwcs import coordinate_frames as cf | ||
|
||
from stdatamodels.jwst import datamodels | ||
from stdatamodels.jwst.transforms.models import NirissSOSSModel | ||
from jwst.lib.wcs_utils import get_wavelengths | ||
from jwst.assign_wcs import util | ||
|
||
|
||
def create_model(): | ||
|
||
det = cf.Frame2D(name="detector", axes_order=(0, 1)) | ||
|
||
sky = cf.CelestialFrame(name="sky", axes_order=(0, 1), reference_frame=coord.ICRS()) | ||
slit_spatial = cf.Frame2D( | ||
name="slit_spatial", | ||
axes_order=(0, 1), | ||
unit=("", ""), | ||
axes_names=("x_slit", "y_slit"), | ||
) | ||
|
||
spec = cf.SpectralFrame( | ||
name="spectral", axes_order=(2,), unit=(u.micron,), axes_names=("wavelength",) | ||
) | ||
slit_frame = cf.CompositeFrame([slit_spatial, spec], name="slit_frame") | ||
world = cf.CompositeFrame([sky, spec], name="world") | ||
|
||
det2slit = Mapping((0, 1, 1)) | (Identity(2) & (Scale(0.5) | Shift(0.5))) | ||
slit2sky = Identity(3) | ||
|
||
slit_wcs = wcs.WCS([(det, det2slit), (slit_frame, slit2sky), (world, None)]) | ||
|
||
# compute wavelengths | ||
|
||
data = np.full((10, 10), fill_value=5.0) | ||
|
||
bounding_box = util.wcs_bbox_from_shape(data.shape) | ||
|
||
x, y = wcstools.grid_from_bounding_box(bounding_box, step=(1, 1)) | ||
_, _, lam = slit_wcs(x, y) | ||
lam = lam.astype(np.float32) | ||
model = datamodels.SlitModel(data=data, wavelength=lam) | ||
model.meta.wcs = slit_wcs | ||
|
||
return model | ||
|
||
|
||
def create_mock_wl(): | ||
wl = np.arange(10.0) | ||
wl = wl[:, np.newaxis] | ||
wl = np.repeat(wl, 10, axis=1) | ||
wl = (wl * 0.5) + 0.5 | ||
return wl | ||
|
||
|
||
def test_get_wavelengths(): | ||
|
||
# create a mock SlitModel | ||
model = create_model() | ||
|
||
# calculate what the wavelength array should be | ||
wl_og = create_mock_wl() | ||
|
||
# Test that the get wavelengths returns the wavelength grid | ||
wl = get_wavelengths(model) | ||
assert_allclose(wl, wl_og) | ||
|
||
del model.wavelength | ||
|
||
# Check that wavelengths can be generated from wcs when the | ||
# wavelength attribute is unavailable | ||
wl = get_wavelengths(model) | ||
assert_allclose(wl, wl_og) | ||
|
||
# Check that wavelengths are generated correctly when given a WFSS exp_type | ||
wl = get_wavelengths(model, exp_type="NRC_TSGRISM") | ||
assert_allclose(wl, wl_og) | ||
|
||
|
||
def test_get_wavelengths_soss(): | ||
|
||
# create a mock SlitModel | ||
model = create_model() | ||
|
||
del model.wavelength | ||
model.meta.exposure.type = "NIS_SOSS" | ||
|
||
wcs = model.meta.wcs | ||
new_wcs = NirissSOSSModel( | ||
[ | ||
1, | ||
], | ||
[ | ||
wcs, | ||
], | ||
) | ||
model.meta.wcs = new_wcs | ||
|
||
# calculate what the wavelength array should be | ||
wl_og = create_mock_wl() | ||
|
||
wl = get_wavelengths(model, order=1) | ||
assert_allclose(wl, wl_og) | ||
|
||
|
||
def test_get_wavelength_wavecorr(): | ||
|
||
# create a mock SlitModel | ||
model = create_model() | ||
|
||
wl_og = create_mock_wl() | ||
|
||
# Test use_wavecorr with no wavelength correction modificiation | ||
# get_wavelengths should return the same wavelengths for use_wavecorr | ||
# True and False | ||
|
||
wl_corr = get_wavelengths(model, use_wavecorr=True) | ||
assert_allclose(wl_corr, wl_og) | ||
|
||
wl_uncorr = get_wavelengths(model, use_wavecorr=False) | ||
assert_allclose(wl_corr, wl_uncorr) | ||
|
||
# Update the model wcs to add a wavelength corrected slit frame | ||
slit_spatial = cf.Frame2D( | ||
name="slit_spatial", | ||
axes_order=(0, 1), | ||
unit=("", ""), | ||
axes_names=("x_slit", "y_slit"), | ||
) | ||
spec = cf.SpectralFrame( | ||
name="spectral", axes_order=(2,), unit=(u.micron,), axes_names=("wavelength",) | ||
) | ||
wcorr_frame = cf.CompositeFrame([slit_spatial, spec], name="wavecorr_frame") | ||
|
||
# Insert the new transform into the slit wcs object | ||
wave2wavecorr = Identity(2) & Shift(0.1) | ||
model.meta.wcs.insert_frame("slit_frame", wave2wavecorr, wcorr_frame) | ||
|
||
bounding_box = util.wcs_bbox_from_shape(model.data.shape) | ||
x, y = wcstools.grid_from_bounding_box(bounding_box, step=(1, 1)) | ||
_, _, lam = model.meta.wcs(x, y) | ||
model.wavelength = lam | ||
|
||
# calculate what the corrected wavelength array should be | ||
wl_corr_og = wl_og + 0.1 | ||
|
||
wl_corr = get_wavelengths(model, use_wavecorr=True) | ||
assert_allclose(wl_corr, wl_corr_og) | ||
|
||
wl_uncorr = get_wavelengths(model, use_wavecorr=False) | ||
assert_allclose(wl_uncorr, wl_og) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there still a use case for retrieving and returning the uncorrected wavelengths? Is this necessary for steps like pathloss and/or flatfield that want to compute their calibrations based on both uncorrected and corrected wavelengths (i.e. for uniform and point sources, respectively)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct. Pathloss, flatfield, and photom need access to the uncorrected and corrected wavelengths to calculate and store the uniform/point source corrections. These corrections are then used in the master_background step in spec3 for properly handling background subtraction (with the background as a uniform source) for point sources that are observed in the NIRSpec fixed slit mode.