Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Functions (and HII regions in M33)

In this week’s lab, we’ll see how to construct and use Python functions in practice, using a narrowband image of the galaxy M33 as our dataset.

Our ultimate goal will be to nicely plot and crop our image to make scientific measurements of the physical size of certain features in the galaxy.

We’ll also see at the end why writing functions was a useful way to organize and carry out this analysis. We’ll start focusing just on the image. In Part II, there is a scientific overview regarding HII regions and why we are interested in them.

# Some libraries we'll need throughout
from astropy.io import fits 
from astropy.wcs import WCS 
import numpy as np 
import matplotlib.pyplot as plt

Part 0: Warm up

Before we dive into new things, let’s re-familiarize ourselves with how to write some simple functions.

Part 1: Loading and viewing astronomical images

Astronomical images (those taken at a telescope like Keck Observatory, or in space, like HST or JWST) are, at their core, a 2D array of values. In general, we take images in one band (color) at a time, and if we want color images like those you can take with a camera, we have to later combine those images.

The vast majority of astronomical images that have been, and continue to be, taken are stored in a special file format called FITS. Fits stands for Flexible Image Transport System. It’s a binary format which requires specialized programs to open. One is SAOImage DS9, a GUI based tool I recommend having if you will be working with astronomical images.

In Python, we can use the astropy.io.fits module to load the header (a dictionary of information about the file) and data (images) from a FITS file.

Within a FITS file, information is stored in what are called extensions. These are like separate buckets/folders, and thus allow one to store multiple images, or an image + table, or multiple tables, within 1 file. But usually, we have a simple file with 1 image in the 0th extension, and its header.

When we load a FITS file, we usually also want to know something about the coordinates (on the sky, so RA and DEC) of the things in our image. To do this, we need to know the WCS (world coordinate system) coordinates of the image. The information needed to make one of these is in the image header, and there is an astropy function to convert a header to a wcs object.

The code below will load the M33 image we’ve provided into Python, setting the 2D array im as the image, and the wcs object via the astropy routine.

from astropy.io import fits 
from astropy.wcs import WCS 

with fits.open('./M33_halpha.fits') as hdu:
    im = hdu[0].data 
    wcs = WCS(hdu[0].header)

We can, if we wish, encapsulate this operation into a function. It’s short enough that it’s not overly onerous to write out each time. But we can make these three lines into one, which might make our code easier to read when loading many different fits files throughout a script.

There are now 13 lines of logic in our function, and hopefully it is becoming clear why making such a function is so useful --- we can now very quickly and easily load and plot any fits file, or any array we have on hand, with wcs on or off, with limits setting, all in one line.

Part II: The Size of HII Regions

# Here is an an example of ~what you should be seeing
(<Figure size 700x700 with 1 Axes>, <Axes: >)
<Figure size 700x700 with 1 Axes>