<jupyter_start><jupyter_text>Plot FITS images Being able to visualize images is key to understand any data reduction. Therefore, the first thing to be done for any reduction is to plot the images. This can be easily done with Astropy and Matplotlib. We are going to define a handy function that will be used in the rest of the class.<jupyter_code>%matplotlib widget
import sys
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy import wcs
from astropy.visualization.mpl_normalize import simple_norm
def plot(filename, ext=0):

    hdu = fits.open(filename)[ext]
    image = hdu.data.T
    header = hdu.header

    norm = simple_norm(image, percent=99)

    wcsheader = wcs.WCS(header).swapaxes(0,1)

    projection = dict(projection=wcsheader) if wcsheader.is_celestial else None
    fig, norm_image = plt.subplots(subplot_kw=projection, figsize=(9,4))
    cmap = norm_image.imshow(image, norm=norm, origin='lower', cmap='Greys_r')
    norm_image.invert_yaxis()
    fig.colorbar(cmap)

    if wcsheader.is_celestial:
        norm_image.coords.grid(color="yellow", ls="solid")

        lon, lat = norm_image.coords

        lon.set_axislabel('Right Ascension')
        lon.set_ticks_position('l')
        lon.set_ticklabel_position('l')
        
        lat.set_axislabel('Declination')
        lat.set_ticks_position('b')
        lat.set_ticklabel_position('b')

    plt.show(fig)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0935754189944134, 'fraction_numerical': 0.004888268156424581, 'mean_word_length': 3.9413793103448276, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '556682', 'license': 'MIT', 'n_tokens_mistral': 478, 'n_tokens_neox': 461, 'n_words': 122, 'path': 'notebooks/plotfits.ipynb', 'repo_name': 'vilardellsalles/master-uab', 'size': '1432'}