Added Lab 1 Files

This commit is contained in:
Maxwell Millar-Blanchaer 2024-04-07 21:50:22 -07:00
parent 67419ca6b5
commit 11aae2c5aa
3 changed files with 3410 additions and 0 deletions

615
Lab_1/Lab_1.ipynb Normal file
View file

@ -0,0 +1,615 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# <p style=\"text-align: center;\">PHYS 134L Spring 2024 Lab 1</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<div class=\"alert alert-block alert-danger\"><b>Due date:</b> Sunday, April 14th, 2024 by 11:59pm, submitted through Canvas.</div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Names: "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Enter your name and your partner's name here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This lab will introduce you to **ds9**, a venerable (if not fantastic) image viewer that is a common way astronomers look at images; and you will continue to use **jupyter**. While **ds9** will have limited usefulness beyond this class (unless you continue in astronomical research!), **python** and **jupyter** are very widely used across fields. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## <p style=\"text-align: center;\">ds9 Fundamentals</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The program ds9 allows you to display astronomical images, measure many of their properties, and much more. Unfortunately, the\n",
"manual for ds9 is particularly terse and unhelpful. The only way to understand the programs capabilities is to try things, or to consult a\n",
"guru (perhaps your own lab partner). Do the following few first steps."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Download _object.fits_ and _object.cat_ from the Canvas Week 2 Module. You may right-click _object.fits_ (if you associated fits files with ds9) and open with ds9, or you my load ds9 first and then using the open function (you can do this with both the GUI button and from the \"File\" menu) to find the file _object.fits_.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Image Scaling\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you have opened the image, About how many stars do you see?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Left click on “scale”, on “more. . . ” if necessary to make “zscale” visible, and on “zscale”.\n",
"Changing the scale parameters adjusts how the 2D image array in ds9 is display to the user,\n",
"but doenst actually change the data itself. About how many stars now?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Right click inside the image window, and drag the cursor around. Describe the effect of moving it left-right? Describe the effect of moving the (right-clicked) cursor up-down?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Experiment with different scale options (there are many!) to get a feel for how this functionality works. Try different colormaps (in the ”color” menu). Describe the settings that\n",
"you find best display the image and why."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Header Data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Astronomical images ordinarily come with “metadata” = (data about the data). In images in FITS format, the metadata appear in a ”header” that can be displayed separately from the data. In the ds9 window, click on “File/Display Fits Header. . . ” Answer the following questions, based on the header."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What are the x- and y- dimensions of the image (in pixels)?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What was the image exposure time (in s)?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What filter was used? (We will go into what this means later)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What was the name of the object being observed?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What was the right ascension object being observed?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Google the object name you found above, and find an image of it on the web. Match this image to your ds9 display of object.fits. Adjust the scaling so you get a recognizable picture of the object (which is a supernova) and its host galaxy. (Try clicking “scale”, then “min/max” and then various scalings. Then play with right-click-and-drag-the-cursor to get a good-looking image. This is as much art as science!)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What are the x,y coordinates of the supernova?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the signal in the central pixel of the supernova? (units of counts)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"What is the signal in the central pixel of the host galaxy?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Type Your Answer Here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## <p style=\"text-align: center;\">Fits Files and Python</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"While ds9 can be powerful, sometimes you'll want to have a more flexible and customizable way to interact with your data. To do this, we'll read the data into python. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First import the necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os #This package allows you to interact with your operating system\n",
"import numpy as np #This is a standard \n",
"from astropy.io import fits #Astropy is a multi-purpose python package made by astronomers\n",
"import matplotlib.pyplot as plt #You've already used this in your bootcamp!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When we type `import numpy as np`, this means that we are importing a package called `numpy`, and that in\n",
"the future we will refer to it as `np`. We can call functions, classes, and objects defined within\n",
"numpy using syntax like `np.sin(x)`. The first thing we will do here is to read in the same image file as earlier\n",
"and try to make a pretty plot in python. You will use lines similar to the following, first defining a string\n",
"containing the path to your image file, and then opening the image."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"imagefile = \"object.fits\" #The file has automatically been downloaded to the appropriate location in your JupyterHub drive\n",
"image = fits.open(imagefile)[0].data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first line is to tell the program where to find the image file. Dont worry at the moment why we have to use `[0].data` when we open\n",
"the file: this is due to how fits files are structured. Feel free to read up on the astropy documentation to learn a bit more. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we will plot the image. Use the following at first:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.imshow(image, origin='lower')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This creates and displays a plot of the same image as before. It is inverted vertically relative to what you\n",
"saw in ds9 unless we force origin to be lower. Try it leaving this argument out and see for yourself!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now it is up to you to make the figure look better (leave the output of these commands as\n",
"they are; create a new cell to make your new figure). Try changing the size of the figure (e.g. with\n",
"`plt.figure(figsize=(10, 10)`) before `plt.imshow`), the color map, the minimum and maximum values\n",
"to show, and the scaling (you can plot any function of the values in image). **Read the documentation\n",
"pages.** Try square root scales, log scales, linear scales, other scales, and see what looks good! To plot with\n",
"some other scale, you may plot, e.g., `np.log(image)` or `np.exp(image)` instead of just `image`, or you could try using different [image normalizations](https://matplotlib.org/stable/tutorials/colors/colormapnorms.html). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also, its a good idea to read [this](https://matplotlib.org/users/colormaps.html) page on color maps."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, you may\n",
"turn off the axes labeling pixel values with another line\n",
"```\n",
"plt.axis(off)\n",
"```\n",
"before\n",
"```\n",
"plt.show()\n",
"```\n",
"You may also save your figure to a png file using `plt.savefig` so that you can print it out and tape it to your door or wall. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Put the code for your nicer looking figure here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## <p style=\"text-align: center;\">Stellar Photometry</p>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this final section of Lab 1 we'll move on to a catalog of stellar photometry (i.e. how bright the stars are) based on your ``object.fits`` files that was extracted using a program called Source Extractor (sometimes known as SExtractor). Yes, the name is crass, and no, you should not choose a similar name when you write your own useful pieces of software. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The catalog of extracted stars is in a file called `object.cat`. We will visualize this catalog a few ways,\n",
"getting a little practice with python arrays along the way. The first step is to load in the catalog. You will\n",
"define the catalog filename in exactly the same way you did before. You will then read it into python with the syntax\n",
"```\n",
"catalog = np.loadtxt(catalogfile)\n",
"```\n",
"where `catalogfile` is a string giving the filename of the catalog. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Open the catalog in the text editor of your choice (notepad, wordpad, word, emacs, etc.) and\n",
"take a look: the first few lines show what each column represents. For now, focus on three columns:\n",
"x-position, y-position, and flags. Note that you can download files off the JupyterHub onto any computer you're using. \n",
"\n",
"\n",
"Set a new variable x equal to the array of x-positions from the\n",
"catalog, a new variable y equal to the array of y-positions, and flags equal to the flag entry.\n",
"When you do this, remember that the first array element is indexed with a zero. Play around to see what to do, use a search\n",
"engine, and dont immediately ask for the answer. For example, what would the following lines give?\n",
"```\n",
"x = catalog[0, 0]\n",
"x = catalog[:, 0]\n",
"x = catalog[0]\n",
"x = catalog[0, :]\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Your code to read in the file goes here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you have never programmed before, this sort of indexing varies between programming languages but the\n",
"basic idea is very standard. Now plot the extracted stars atop your pretty image of `object.fits` using plt.plot(x,\n",
"y) (use a new cell). Use dots/markers rather than lines, and consult the [manual](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.html) for guidance if you need it. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Your code goes here. Overplot the catalog positions on top of the image of object.fits"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next, change the plot a bit: plot those stars that have flag == 0 in one color/marker style, and stars that have flag != 0 in a different color/marker style. Hint: the easiest way to do this will involve either `numpy.where` or boolean indexing. You might want to go back to the indexing tutorial.\n",
"Also, there is a very important difference between = and ==. The statement\n",
"```\n",
"x = 5\n",
"```\n",
"sets the variable x equal to the value 5. The statement\n",
"```\n",
"x == 5\n",
"```\n",
"will not change the value of x. It will evaluate to True if, indeed, x is the integer 5, and False otherwise. If\n",
"x is an array, this statement will return an array of True and False values (a boolean array). The statement\n",
"will return an error if x is not defined."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once you have extracted arrays like `x_flagged` and `y_flagged` and have plotted them atop\n",
"your image, comment on any differences that you notice between the flagged (flag != 0) and\n",
"unflagged (flag == 0) stars. As usual, this comment should appear in a markdown cell below\n",
"the figure. If you need insight or are curious, go search for what the flags represent in the\n",
"SExtractor manual and use this to help inform your comments."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"##Your code goes Here##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Your comments go here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The exact factor relating the peak intensity and total flux will vary from star to star. Part of the reason why is that the flux in any one pixel is subject to photon noise (also called shot noise or Poisson noise). If we have a lot of photons, the scatter in the count rate scales roughly as the square root of the number of counts (refereed to below as the \"square root rule\"). Remind yourself of why this is the case for Poisson statistics!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Choose the eight stars with fluxes between 1990 and 2010 as reported by SExtractor. If you havent already played with\n",
"boolean indexing and/or `np.where`, now is the time. You probably dont want to do this by hand. Hint: You can combine logical expressions with the python and (&) and or operators."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using your newfound ability to select elements from arrays, find the x and y coordinates of\n",
"these eight stars. Then compute the standard deviation of the eight peak pixel values. You\n",
"may compute the peak pixel values either by hand in ds9 or directly in python. Either way, be explicit\n",
"about what you are doing in your jupyter notebook. When you compute the standard deviation, you\n",
"may also do that as you like, but again be clear. What would you expect for the standard deviation\n",
"according to the square root rule? Is this what you find? If your standard deviation was larger\n",
"or smaller than this expectation, try to come up with possible explanations, and write these\n",
"in a markdown cell in your jupyter notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Your reponse here*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have one, last, easy task for this lab. You will make histograms of the fluxes returned by\n",
"SExtractor. Use the function `plt.hist()` for this purpose. Make two separate histograms: one\n",
"of the fluxes, and one of the log of the fluxes (either base ten or natural log, your choice).\n",
"Comment on which of these looks more informative and why. As before, use your judgment and the\n",
"manual pages to make a figure with labeled axes that looks nice. We will work in logarithmic units of flux\n",
"called magnitudes for the rest of the course, and indeed, this is another column of the catalog file returned\n",
"by SExtractor."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Your code here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Your reponse here*"
]
}
],
"metadata": {
"interpreter": {
"hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90"
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

2792
Lab_1/object.cat Normal file

File diff suppressed because it is too large Load diff

BIN
Lab_1/object.fits (Stored with Git LFS) Normal file

Binary file not shown.