<jupyter_start><jupyter_text>Use Astropy to analyze FITS images<jupyter_code>%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits<jupyter_output><empty_output><jupyter_text>Open the FITS file<jupyter_code>fname = "HorseHead.fits"
hdu_list = fits.open(fname)
hdu_list.info()
# image data is in the primary block

image_data = hdu_list[0].data
print(type(image_data))
print(image_data.shape)<jupyter_output><class 'numpy.ndarray'>
(893, 891)<jupyter_text>close FITS file<jupyter_code>hdu_list.close()
# can use shortcut to read in image data and close the file: 
# image_data = fits.getdata(fname)<jupyter_output><empty_output><jupyter_text>Let's show the data<jupyter_code>plt.imshow(image_data, cmap='gray')
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Get some basic statistics about our image<jupyter_code>print('Min:', np.min(image_data))
print('Max:', np.max(image_data))
print('Mean:', np.mean(image_data))
print('Stdev:', np.std(image_data))<jupyter_output>Min: 3759
Max: 22918
Mean: 9831.481676287574
Stdev: 3032.3927542049046<jupyter_text>Plot a histogram  we'll need to cast the data from 2-D array to 1-D, let's use ndarray.flatten()<jupyter_code>histogram = plt.hist(image_data.flatten(),bins='auto')<jupyter_output><empty_output><jupyter_text>Displaying the image with logarithmic color scale<jupyter_code>from matplotlib.colors import LogNorm
plt.imshow(image_data, cmap='gray', norm=LogNorm())

# Choose the tick marks based on the histogram above
cbar = plt.colorbar(ticks=[5.e3,1.e4,2.e4])
cbar.ax.set_yticklabels(['5,000','10,000','20,000'])<jupyter_output><empty_output><jupyter_text>Stacking images to improve the signal to noise ratio  images of M13 galaxy<jupyter_code># make a list of filenames
image_list = ['M13_blue_0001.fits','M13_blue_0002.fits','M13_blue_0003.fits','M13_blue_0004.fits','M13_blue_0005.fits']
# make an array of images from the list of images
image_concat = [fits.getdata(image) for image in image_list]

# sum the images together
final_image = np.sum(image_concat, axis=0)

# plot a histogram of the image pixel values
image_hist = plt.hist(final_image.flatten(), bins='auto')
# here we use the keywords vmin and vmax to set limits on the color scaling for imshow

plt.imshow(final_image, cmap='gray', vmin=2E3, vmax=3E3)
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Writing a new FITS file<jupyter_code># we write the file using the writeto() method
# you'll receive an error if the file you are trying to write already exist
#   so we use clobber=True

outfile = 'stacked_M13_blue.fits'
hdu = fits.PrimaryHDU(final_image)
hdu.writeto(outfile, overwrite=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11529933481152993, 'fraction_numerical': 0.04065040650406504, 'mean_word_length': 7.704180064308682, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '87489', 'license': 'MIT', 'n_tokens_mistral': 1085, 'n_tokens_neox': 993, 'n_words': 233, 'path': 'astropy_tutorial.ipynb', 'repo_name': 'natewachter/astr-119-session-18', 'size': '2706'}