-
Notifications
You must be signed in to change notification settings - Fork 0
Sam's Astrophysics Coding Tutorial
This Notebook will cover the basics of using Python to analyze astrophysics data, particularly images.
#Our bookkeeping cell
import os #This package allows you to interact with your operating system
import numpy as np #standard math library: https://numpy.org/doc/stable/
import matplotlib.pyplot as plt #standard plotting library: https://matplotlib.org/stable/index.html
from astropy.io import fits #Astropy is a multi-purpose python package made by astronomers: https://docs.astropy.org/en/stable/index.html
from astropy.stats import * #Astropy is massive and we only want specific tools from it for now, so it's best to import only what we need.
from photutils.background import Background2D, MedianBackground #Photutils is another package used to manipulate images and find sources in our images.
from photutils.detection import DAOStarFinder #Photutils documentation: https://photutils.readthedocs.io/en/stable/
from photutils.aperture import CircularAperture
from photutils.aperture import aperture_photometry
import warnings
warnings.filterwarnings('ignore') #I turn off warnings because python complains about log values, but doesn't actually break. You may want to remove this
Astrophysics coding follows the same general steps most of the time
- Import your data and extract relevant information
- Clean your data
- Analyze your data
Lets make some code to handle a basic example, starting by importing and opening our data
Astrophysics data almost always comes in the form of fits files. Fits (flexible image transport system) files come with a data extension and a header extension. The data of a fits file is the image itself, sometimes along with other things like bad pixel masks. The header is a table of information about the image that is relevant to using data from it.
Included with this tutorial was an LCO image. We're going to open it.
#First define the image in python as a string.
imagefile = "Test_Image.fits" #Here I am assuming the image is in the same location on my computer as my python notebook. Use the below code for more flexibility:
# imagefile = r"C:\Users\Sam Whitebook\Test_Image.fits" #Replacing the path with wherever your image actually is. The r is necessary to define a "string literal" so python knows the \'s are for a path.
#Now we will use astropy to open the file. Note that sometimes LCO files come with multiple extensions for data and headers. You may need to index through your data to find your image.
image = fits.open(imagefile)[0].data
header = fits.open(imagefile)[0].header
The data of our image is a matrix of pixel values representing a brightness (photon count) at a pixel. We can show it in python with matplotlib imshow.
plt.figure(figsize=(10,10))
plt.imshow(np.log(image), #We usually take the log of an image to see the variance of the pixels better.
origin='lower', #This tells matplotlib where the [0,0] index is. For our images it is in the bottom.
cmap = "gray", #Use whatever colormap you like, but I find gray the easiest to visualize.
vmin = 6, vmax = 11) #This defines the data range we want to see. There are ways to automate this but I always do it manually.
plt.axis('off') #This removes the axes, which aren't really relevant for just viewing an image.
plt.show()