Skip to content
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

Add nrc_ta_image_comparison function to trending tools #789

Merged
merged 4 commits into from
Jan 22, 2024
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
40 changes: 40 additions & 0 deletions webbpsf/mast_wss.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import os
import functools

import astropy
import astropy.io.fits as fits
Expand Down Expand Up @@ -581,3 +582,42 @@
'Post Move Sensing OPD Filename'])

return correction_table


# Functions for retrieving images


@functools.lru_cache
def get_visit_nrc_ta_image(visitid, verbose=True):
"""Retrieve from MAST the NIRCam target acq image for a given visit.

This retrieves an image from MAST and returns it as a HDUList variable
without writing to disk.
"""

from astroquery.mast import Mast
keywords = {

Check warning on line 599 in webbpsf/mast_wss.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/mast_wss.py#L598-L599

Added lines #L598 - L599 were not covered by tests
'visit_id': [visitid[1:]], # note: drop the initial character 'V'
'category': ['CAL'],
'exp_type': ['NRC_TACQ']
}

def set_params(parameters):
return [{"paramName" : p, "values" : v} for p, v in parameters.items()]

Check warning on line 606 in webbpsf/mast_wss.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/mast_wss.py#L605-L606

Added lines #L605 - L606 were not covered by tests


# Restructuring the keywords dictionary to the MAST syntax
params = {'columns': '*',

Check warning on line 610 in webbpsf/mast_wss.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/mast_wss.py#L610

Added line #L610 was not covered by tests
'filters': set_params(keywords)
}

service = 'Mast.Jwst.Filtered.Nircam'
t = Mast.service_request(service, params)
filename = t[0]['filename']

Check warning on line 616 in webbpsf/mast_wss.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/mast_wss.py#L614-L616

Added lines #L614 - L616 were not covered by tests

if verbose:
print(f"TA filename: {filename}")
mast_file_url = f"https://mast.stsci.edu/api/v0.1/Download/file?uri=mast:JWST/product/{filename}"
ta_hdul = fits.open(mast_file_url)

Check warning on line 621 in webbpsf/mast_wss.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/mast_wss.py#L618-L621

Added lines #L618 - L621 were not covered by tests

return ta_hdul

Check warning on line 623 in webbpsf/mast_wss.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/mast_wss.py#L623

Added line #L623 was not covered by tests
96 changes: 96 additions & 0 deletions webbpsf/trending.py
Original file line number Diff line number Diff line change
Expand Up @@ -1590,3 +1590,99 @@

webbpsf.trending.show_opd_image((wfe_after-wfe_before)*nanmask*1e6, ax=ax4, vmax=vmax, fontsize=10)
ax4.set_title("Delta WFE\nAfter-Before", color='C1', fontweight='bold')


#### Functions for image comparisons
def show_wfs_ta_img(visitid, ax=None, return_handles=False):
""" Retrieve and display a WFS target acq image"""

hdul = webbpsf.mast_wss.get_visit_nrc_ta_image(visitid)

Check warning on line 1599 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1599

Added line #L1599 was not covered by tests

ta_img = hdul['SCI'].data
mask = np.isfinite(ta_img)
rmean, rmedian, rsig = astropy.stats.sigma_clipped_stats(ta_img[mask])
bglevel = rmedian

Check warning on line 1604 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1601-L1604

Added lines #L1601 - L1604 were not covered by tests

vmax = np.nanmax(ta_img) - bglevel
cmap = matplotlib.cm.viridis.copy()
cmap.set_bad('orange')

Check warning on line 1608 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1606-L1608

Added lines #L1606 - L1608 were not covered by tests


norm = matplotlib.colors.AsinhNorm(linear_width = vmax*0.003, vmax=vmax, #vmin=0)

Check warning on line 1611 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1611

Added line #L1611 was not covered by tests
vmin=-1*rsig)


if ax is None:
ax = plt.gca()
ax.imshow(ta_img - bglevel, norm=norm, cmap=cmap, origin='lower')
ax.set_title(f"WFS TA on {visitid}\n{hdul[0].header['DATE-OBS']}")
ax.set_ylabel("[Pixels]")
ax.text(0.05, 0.9, hdul[0].header['TARGPROP'],

Check warning on line 1620 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1615-L1620

Added lines #L1615 - L1620 were not covered by tests
color='white', transform=ax.transAxes)


if return_handles:
return hdul, ax, norm, cmap, bglevel

Check warning on line 1625 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1624-L1625

Added lines #L1624 - L1625 were not covered by tests

def nrc_ta_image_comparison(visitid):
""" Retrieve a NIRCam target acq image and compare to a simulation

Parameters:
-----------
visitid : string
Visit ID. Should be one of the WFSC visits, starting with a NIRCam target acq, or at least
some other sort of visit that begins with a NIRCam target acquisition.
"""
from skimage.registration import phase_cross_correlation

Check warning on line 1636 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1636

Added line #L1636 was not covered by tests

fig, axes = plt.subplots(figsize=(10,5), ncols=3)

Check warning on line 1638 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1638

Added line #L1638 was not covered by tests

# Get and plot the observed TA image
hdul, ax, norm, cmap, bglevel = show_wfs_ta_img(visitid, ax=axes[0], return_handles=True)
im_obs = hdul['SCI'].data
im_obs_err = hdul['ERR'].data

Check warning on line 1643 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1641-L1643

Added lines #L1641 - L1643 were not covered by tests

# Make a matching sim
nrc = webbpsf.setup_sim_to_match_file(hdul, verbose=False)
opdname = nrc.pupilopd[0].header['CORR_ID'] + "-NRCA3_FP1-1.fits"
psf = nrc.calc_psf(fov_pixels=64)

Check warning on line 1648 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1646-L1648

Added lines #L1646 - L1648 were not covered by tests

# Align and Shift:

im_sim = psf['DET_DIST'].data # Use the extension including distortion and IPC

Check warning on line 1652 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1652

Added line #L1652 was not covered by tests

im_obs_clean = astropy.convolution.interpolate_replace_nans(im_obs, kernel=np.ones((5,5)))

Check warning on line 1654 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1654

Added line #L1654 was not covered by tests

shift, _, _ = phase_cross_correlation(im_obs_clean, im_sim, upsample_factor=32)
im_sim_shifted = scipy.ndimage.shift(im_sim, shift, order=5)

Check warning on line 1657 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1656-L1657

Added lines #L1656 - L1657 were not covered by tests

# figure out the background level and scale factor
scalefactor = np.nanmax(im_obs) / im_sim.max()

Check warning on line 1660 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1660

Added line #L1660 was not covered by tests

im_sim_scaled_aligned = im_sim_shifted*scalefactor

Check warning on line 1662 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1662

Added line #L1662 was not covered by tests

# Plot
axes[1].imshow(im_sim_scaled_aligned, norm=norm, cmap=cmap, origin='lower')
axes[1].set_title(f"Simulated PSF in F212N\nusing {opdname}")

Check warning on line 1666 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1665-L1666

Added lines #L1665 - L1666 were not covered by tests

diffim = im_obs -bglevel - im_sim_scaled_aligned

Check warning on line 1668 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1668

Added line #L1668 was not covered by tests

dofs = np.isfinite(diffim).sum() - 4 # 4 estimated parameters: X and Y offsets, flux scaling, background level
reduced_chisq = np.nansum(((diffim / im_obs_err)**2)) / dofs

Check warning on line 1671 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1670-L1671

Added lines #L1670 - L1671 were not covered by tests

axes[2].imshow(diffim, cmap=cmap, norm=norm, origin='lower')
axes[2].set_title('Difference image\nafter alignment and scaling')
axes[2].text(0.05, 0.9, f"$\\chi^2_r$ = {reduced_chisq:.3g}" + (

Check warning on line 1675 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1673-L1675

Added lines #L1673 - L1675 were not covered by tests
" Alert, not a good fit!" if (reduced_chisq > 1.5) else ""),
transform = axes[2].transAxes, color='white' if (reduced_chisq <1.5) else 'yellow')

for ax in axes:
fig.colorbar(ax.images[0], ax=ax, orientation='horizontal',

Check warning on line 1680 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1679-L1680

Added lines #L1679 - L1680 were not covered by tests
label=hdul['SCI'].header['BUNIT'])

plt.tight_layout()

Check warning on line 1683 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1683

Added line #L1683 was not covered by tests


outname = f'wfs_ta_comparison_{visitid}.pdf'
plt.savefig(outname)
print(f" => {outname}")

Check warning on line 1688 in webbpsf/trending.py

View check run for this annotation

Codecov / codecov/patch

webbpsf/trending.py#L1686-L1688

Added lines #L1686 - L1688 were not covered by tests
Loading