Python - anjavdl/PHY517_AST443 GitHub Wiki

Introduction

Python is an interpreted, object oriented, high-level programming language. An interpreted programming language is di fferent from a compiled language in a number of ways, but in practice it means that you need to de fine all of the classes and functions at the top of the code, before you use them. In compiled languages some things can appear out of order, because the compiler reads through the whole source code before making the compiled program. Interpreted languages are read line by line by the interpreter, so things cannot be out of order in the code.

Calling python

There are a number of ways of working with python, in particular:

  1. interactively on the command line (shell mode)
  2. with python programs / scripts
  3. through jupyter notebooks
  4. through Google Colab notebooks

Shell mode

In shell mode, you simply type the command pythoninto a terminal, which will print some details about the version of python you have installed, the date, and the commands for how to get help, credits, and licensing information. The terminal will show >>>, which means you can now type in python commands and the interpreter will execute those commands.

Python programs / script

The second way to use python is in script mode. You write a text file containing all of the python commands you want python to execute, in the order that you want them done. After you save this file, which is called a script, you type python into a terminal with your script filename as the first argument, and the python interpreter reads the script and executes it line by line. An example is the rdj2aau.py script that you can use to convert a list of RA, Dec, JD to azimuth, altitude, and UT date. In this case it takes two additional filenames for the input and output files:

python rdj2aau.py in.txt out.txt

Jupyter notebooks

Jupyter notebooks are a great way to keep code organized and documented. Jupyter can be used with several kernels, including python. Please see here for further instructions specific to this class.

Google Colab

Google Colab lets you run python code (in notebook format) on Google's computers. This means that there is no need for you to install python on your own computer. It also makes it easy to share your notebook with others through Google Drive and/or github, and thus to work collaboratively on code. Our experience shows that if you need a particular python package for your project, it is much easier to ``install'' it on Google Colab than your own computer or the Computing Lab.

Python tutorials for this class

General python tutorial
Data file for python tutorial: test_data.txt

Python notebook on reading FITS files, numpy, plotting histograms.
Fits files: 00000025.BIAS.FIT , 00000026.BIAS.FIT

Installing python on your laptop

Python is available for all common operating systems. The easiest way to install it is through anaconda. Anaconda includes all the packages needed for this lab, in particular numpy and astropy. Anaconda does take up significant disk space, so instead you could install miniconda along with the packages you need.

Python on the Astro Computing Cluster

Python runs on the machines in the cluster, but you need to add a few packages. You can do this with pip install:

pip install matplotlib
pip install scipy
pip install astropy

Alternatively, you can install your own python version via miniconda (or anaconda), see e.g. these instructions).

To test your installation start python with the command python on a bash shell. Once in the python interpreter execute the following commands to test the installation:

import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
import astropy as ap

If these modules load without (major) errors you have completed the installation.

Note that running jupyter remotely on the lab computers with window-formatting is prohibitively slow. However, we have a work-around to use your laptop's browser to work with a jupyter notebook running on a lab machine; see here.

Other python resources

Excellent tutorials and resources are available on the webpage for Prof. Zingale's python class.