Python assignments

From AstroEdWiki
Jump to navigation Jump to search

This page contains assignments for our Research Methods - Programming with Python short course.

Homework 1: An assignment for very simple Python

Create a program called "gamma.py" that computes and prints the Gamma function of 3/2, and computes and prints the (square root of pi) divided by 2.

Run and see that the program returns the same values for these two quantities (within the floating point precision of your computer).

Submit your homework to the class upload site.


Homework 2: An assignment for solving problems with Python

On the class server we have downloaded a copy of the SKY2000 star catalog from the Vizier service. You will find it here, along with a pdf file that describes the catalog, and a short version of the first 100 lines from it for experimenting with your code.

This catalog is similar to the NGC2000 catalog, in that it is in plain text, formatted by column, and has one star per line. Each line contains specific information about the star, including cross-referenced catalog numbers, celestial coordinates, magnitudes and spectral types. Download the data files and explanatory pdf to your computer (right click in your browser, and save).

Use the ngc_reader.example program we have discussed in class as a model, and write a program that reads and parses this file to find entries based on some aspect of the catalog. The challenge for this assignment is to write a program that (ideally on the command line, but if necessary using the input inside the program) finds any entries for the star with the HD number 128620. A complication for this catalog is that the HD entries are not the first data column, so you will have to parse them from inside each line. One way to do it is with something like this, after using readlines() to read and store the catalog line-by-line in hd_lines:

i = 0
for entries in hd_lines:
  if i == 0:
    hd_catalog = {hd_lines[i][35:43].rstrip() : hd_lines[i]}
  else:
    hd_catalog[hd_lines[i][35:43].rstrip()]] = hd_lines[i]
  i = i + 1 

However, in the columns that follow the HD number there is extra information to designate multiple stars with components that share the HD number, or uncertainties in the naming.

The program should print the right ascension, declination, magnitude, and spectral type for the star extracted from the catalog entry.

Upload your finished working program to the class homework upload site.


Homework 3: Fourier Transforms

Use the numpy_fft.example as a model to write a program that will find the Fourier transform of an oscillator with two simultaneous frequencies and damping constants. Try one at 6 Hz, damping in 2 seconds, and another at 6.6 Hz, damping in 0.5 seconds, both with the same amplitude.

Rather than the single cosine of the example, use a sum of two damped cosines as input to the FFT. The other parts of the code should be mostly unchanged. You'll see beating between the two frequencies, and the effect of the damping on the linewidth.

Upload your finished working program to the class homework upload site.


Homework 4: Image processing

Use the display_gaussian_noise.example as a model to write a program that will create a color image of the diffraction pattern of a star seen with a telescope.

Use a 1000x1000 image array with each pixel the equivalent a small angle on the sky -- an image of a star. In the absence of diffraction all the light would be in the pixel at the center of the image. With diffraction, the light is spread over the image in a diffraction pattern for a circular aperture given by

A(theta) = (2 J_1(ka sin(theta) / ka sin(theta) )^2

where J_1 is a Bessel function of order 1, k is 2 pi / lambda, a is the radius of the aperture, and theta is the angle to the optical axis. In other words, theta is equivalent the distance from the center of the image to the each pixel on a scale of radians.

In SciPy, you can compute a Bessel function J_1 with simply

import scipy.special as sp
y = sp.j1(x)

where "x" is the argument and y is the value you want. Since this is a SciPy function, x can be a NumPy array and jn will return y as an array of Bessel function values. Notice that as x goes to zero, J_1(x)/x goes to 0.5 .

For this exercise try 0.01 arcseconds on the sky for each pixel so the 1000x1000 image will cover 10 arcseconds. (One arcsecond is 1/3600 degree or 4.848x10^-6 radians.) Use a telescope diameter of 0.5 meters, or a radius of 0.25 meters. Calculate the image in each of three wavelengths to approximate the eye's response to various wavelengths lambda (red: 650 nm, green: 520 nm, and blue: 450 nm), and make a color images showing the diffraction patterns that theoretically limit the image quality of a 0.5 meter telescope.

Upload your finished working program and a png image file to the class homework upload site. You can upload multiple files but each one must have different name.

If you have time, try generating images of two stars slightly separated by adding the offset diffraction patterns. You can show that they appear as one star when too close, and are clearly two stars when they are farther apart. However, you'll also see that this resolution depends on the color so it should make for interesting images.