Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James...

26
Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building [email protected] http://www.ast.uct.ac.za/~ims/

Transcript of Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James...

Page 1: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Lecture 1

NASSP Masters 5003S - Computational Astronomy - 2009

Me:

Ian StewartRoom 521, James building

[email protected]://www.ast.uct.ac.za/~ims/

Page 2: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003S - Computational Astronomy - 2009

Lecture 1

• Aim:– Become familiar with astronomy data– Learn a bit of programming

• No set text –– Web resources– Library– My own books (maybe!)

• Programming environment:– Unix/Linux– Python– Laptops not essential

Page 3: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003S - Computational Astronomy - 2009

Lecture 1

• 24 Lectures• 6 tuts• Probable assessment breakup:

– 50% tuts– 20% ‘normal’ exam– 30% ‘take-home’ exam

Page 4: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003F - Computational Astronomy - 2009

Course outline1. Unix & python2. FITS files3. Data presentation & errors4. Finding minima5. Random quantities, histograms, Monte Carlos6. Model fitting: chi squared, likelihood7. Signal detection, variability8. Some Bayesian methods9. Catalogs of sources10. Fourier matters11. Polarized signals12. Radio astronomy13. Interferometry14. Satellite observatories15. Event-counting and HE astronomy

Page 5: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003S - Computational Astronomy - 2009

Possible computing grumbles:

• Why Unix/Linux?– default platform for ‘serious’ computing.

• Mac: has a unix emulator.• Windows: closed-source, dumbed-down philosophy makes it

difficult to keep control of what you’re doing.

• Why python?– Good web doco (eg www.python.org/doc/)– Easy to learn, easy to read– Lots of libraries and APIs.– But, you will learn other languages…

Page 6: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

• We’ll use the ‘command line’ – windows are for wimps.

• The unix ‘shell’:– This is what lets you send commands to the

system.

UNIX

UNIX* intro

* Unix and linux look very similar to the user so I will use the terms interchangeably.

The unix shell

HDD

Memory

Other bits& pieces

Page 7: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

UNIX

• Flavours of shell:– Generally bash or csh.– Do ‘printenv SHELL’ to find out yours.– Some differences between shell flavours at

the < 10% level.– bash is ‘more professional’.

Page 8: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

The UNIX file structure

• The ‘prompt’– Eg: ims@server2:~$ - this is what UNIX shows you

when it is ready to receive a new command.

• Directory, file and path:– Directory names often have ‘/’s. Eg ‘/home/ims’– File names don’t. Eg ‘clever_code.py’

• File names may have a dot then a 2- to 4-letter suffix which gives you a hint about the file type.

• It is not a good idea to include spaces in Unix file or directory names. Use an underscore instead.

– The full name for a file includes the directory where it is located, and is called a path name. Eg

• /home/ims/clever_code.py

Page 9: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

UNIX shell commands

• pwd– present working directory - tells you where

you are in the file system.

• cd <name>– change pwd to directory ‘name’.

• mkdir <name>– makes a new directory.

• rmdir <name>– removes (deletes) the named directory.

Page 10: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

UNIX shell commands

• ls– list the files in the pwd.

• cp <name> <destination>– copies the named file.

• mv <name> <destination>– equals cp followed by rm. But mv can also

move directories.

• rm <name>– removes (deletes) the named file.

Note that each of the last three can erase files – so Back up your work!

Page 11: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

UNIX shell commands

• ssh <user name>@<computer name>– How to log in (securely!) to another computer.

• exit, logout– How to log out (pretty obvious).

• scp, rsync– ways to transfer files from one computer to

another.

• man <name of command>– gives you a ‘manual’, ie a lot of

documentation (sometimes more than you really wanted!) for the named command.

Page 12: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Other interesting UNIX topics:

• Environment variables (eg SHELL, PYTHONPATH)

• The file named .bashrc (note the dot).– Try ‘more .bashrc’

• Wild cards * and ?.– Try:

• touch fred• touch bert• touch mary• ls *e*

– Should list all files which have names matching the pattern ‘anything, then an e, then anything.’ This will get fred and bert but not mary.

Page 13: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003F - Computational Astronomy - 2009

More Unix tips and tricks: STDOUT, redirection.

How to redirect your output:– If I do

– I get output to STDOUT (the screen):

– etc. But if I do

– I apparently don’t get anything. But! Look inside file99:

– etc.

$ ls > file99

benne.html Desktop Examples pic26867.jpg

$ more file99benne.htmlDesktop

$ ls

Page 14: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003F - Computational Astronomy - 2009

More Unix tips and tricks: redirection cont.

• The > character redirects the output of ls (or any other command) from the screen to the named file.

• Careful! It will overwrite any pre-existing file of that name.

• If you just want to add stuff to the end of an existing file, use >> instead of >.

• To redirect errors, you have to be more elaborate. But we won’t worry about that.

• If you want to send the output to another command, use | (called a ‘pipe’). Eg

a2ps | lp -dljuct

Page 15: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003F - Computational Astronomy - 2009

Dots in Unix.

A dot . in Unix has several different functions, depending on context.

1. Here it is just part of a filename:

2. Here is the first character in the name of a ‘hidden’ file (ls won’t list these – but ls –a will):

3. Here it means run the script called fred:

4. Here it means the present working directory:

bignob.py

.bashrc

$ . fred

$ export PATH=/usr/bin:.$ ls ./*

Note the space!

Page 16: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003F - Computational Astronomy - 2009

Double dots in Unix.

Two dots in a row means the directory 1 higher up the tree from here. It can save you some typing. Eg the following:

– If I want to cd back to comp_astron, there’s two ways to do it. The long way:

– Or the short way:

$ pwd/home/ims/doc/teaching/comp_astron$ lscode latex figures$ cd code$ pwd/home/ims/doc/teaching/comp_astron/code

$ cd /home/ims/doc/teaching/comp_astron

$ cd ../

Page 17: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

NASSP Masters 5003F - Computational Astronomy - 2009

The tilde ~

• This is another symbol which has a meaning which depends on context.

1. At the end of a file name it means the file is a backup. The shell makes these under certain conditions. They record the file before your last change. Obviously this can be useful!

2. Here it is shorthand for your home directory:

• (As an aside, cd with no arguments always takes you direct to home.)

$ cd ~/doc/teaching

some_file_name~

Page 18: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

File types

Type Suffix

Text Various

Code (specialized text files)

.f, .f90, .c, .cpp, .py, etc

Flexible Image Transport System (FITS)

Often .fit, but can vary

Acroread output .pdf

Postscript .ps

Images Eg .gif, .jpg, .png

Compressed by gzip .gz

AS

CII

Bin

ary

Page 19: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Text editors

• vi– non-windows, but abstruse commands.

• pico– non-windows, fairly user-friendly.

• emacs, xemacs– the editor of choice for many, but I don’t like it.

• gedit– my favourite.

• IDE for python coding… maybe ok…

Whichever you choose – ***** Back up your work!! *****

Page 20: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Python

Page 21: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Python

• is a ‘scripting language’, which roughly translated means:– python code doesn’t have to be ‘compiled’;– it’s pretty slow.

• is an ‘object-oriented’ (OO) language. Some OO code can look pretty wacky. But relax! you won’t have to write any*.

• #!/usr/bin/env python – huh..?

*probably not, anyway.

Page 22: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Python• Python insists on ‘block indenting’. This

means if you start any block, like an if statement, or a for loop, you have to indent within the block. The relaxation of this indenting is the way python recognizes the end of the block.

• You don’t have to run python as a script, you can also use it interactively. Type python at your normal unix prompt and you’ll get a new ‘sergeant’ prompt >>>. You can enter your python statements then line by line. Type ‘control-d’ to get out when you’re finished.

Page 23: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Large chunks of python

• The script you run on the command line, which consists of a single ascii file, is the ‘main program’. Obviously you need a ‘main’ to get anything done. But if your program is long, you may want to disperse some your code into subsidiary files, which are called from the main program. These secondary files are known as modules. They are called via the import statement.

Page 24: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

How python finds your modules• Suppose you have some neat code in a

module named huge_brane.py which you keep in a directory named /home/me/src. You want a separate program slogger.py to be able to make use of this module. At the command line, do:

• Within your code slogger.py include the line

• Note that you leave off the .py bit.

export PYTHONPATH=/home/me/src

from huge_brane import *

Page 25: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Smaller chunks of python

• Within a module the largest chunks are functions. The syntax of a typical function is

• Note the ‘:’ and the indenting – typical for any block in python. Relax the indenting for the 1st statement after the end of the function.

• You call the function from the main code like:

def myfunction(foo, bar, bert): # some stuff here return ‘some value’ # optional

newvar = myfunction(foo, bar, bert)

Page 26: Lecture 1 NASSP Masters 5003S - Computational Astronomy - 2009 Me: Ian Stewart Room 521, James building ims@ast.uct.ac.za ims

Python variables

• Python ‘scalar’ data types:– real– integer– string– boolean (‘True’ or ‘False’)– object (ie something with internal structure)

• BUT the python philosophy is to ignore this distinction as far as possible. Variables are not ‘declared’ as with other languages. Python sets (or resets, if necessary) the type of a variable to fit whatever data you try to load into it.