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 python or python3 into 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. Note that on the Computing Lab machines, python invokes python2.7, so you need to call python3 to get python3.

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 fi lename 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

Older tutorials in python 2.7: Python primer, notebook

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

By default, python on uhura starts python2.7. To get the latest python3, along with the latest versions of astropy, numpy, scipy and matplotlib, you should install in your own copy of python3 through miniconda as follows (video version of these instructions):

  1. Download Miniconda Linux 64 bit installer for linux from https://docs.conda.io/en/latest/miniconda.html#linux-installers.
  2. Copy the Miniconda installer to your astrolab directory, e.g. /astrolab/Fall_21/aeinstein (replace Fall_21/aeinstein with the appropriate path to your data directory).
  3. Run the Miniconda installer with bash, e.g.
    /usr/bin/bash Miniconda3-py39_4.10.3-Linux-x86_64.sh
    When prompted for the install directory YOU MUST SUPPLY YOUR astrolab directory, e.g. /astrolab/Fall_21/aeinstein/conda . Say “yes” to the prompt about initializing Miniconda3. The Miniconda installer will modify your .bashrc file to point to a new binary directory which will include all packages installed by Miniconda.
  4. Logout of uhura and log back in. When you execute the command which python3 you should see python3 under your astrolab conda directory, e.g. /astrolab/Fall_21/aeinstein/conda/bin/conda/bin/python3
  5. Execute the command conda install astropy. This will install astropy and the requisite packages such as numpy, scipy, matplotlib, etc. This should take a few moments to complete.
  6. Test the astropy installation by starting Python3 using the command python3 to enable the Python 3 interpreter. 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 error you have completed the installation. These six steps will have successfully set up astropy and other required packages for you on uhura. If you wish to access these packages from other machines then copy your .bashrc file from uhura to the other Astronomy Computing Facility machines.

Note that it running jupyter remotely 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 (e.g. uhura); see here.

Other python resources

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