Transcript Lecture 1

Lecture 1
Me:
Ian Stewart
Room 521, James building
[email protected]
http://www.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
NASSP Masters 5003S - Computational Astronomy - 2009
Lecture 1
• 24 Lectures
• 6 tuts
• Probable assessment breakup:
– 50% tuts
– 20% ‘normal’ exam
– 30% ‘take-home’ exam
NASSP Masters 5003S - Computational Astronomy - 2009
Course outline
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
Unix & python
FITS files
Data presentation & errors
Finding minima
Random quantities, histograms, Monte Carlos
Model fitting: chi squared, likelihood
Signal detection, variability
Some Bayesian methods
Catalogs of sources
Fourier matters
Polarized signals
Radio astronomy
Interferometry
Satellite observatories
Event-counting and HE astronomy
NASSP Masters 5003F - 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…
NASSP Masters 5003S - Computational Astronomy - 2009
UNIX* intro
• We’ll use the ‘command line’ – windows
are for wimps.
• The unix ‘shell’:
– This is what lets you send commands to the
system.
HDD
The unix shell
UNIX
Memory
Other bits
& pieces
* Unix and linux look very similar to the user so I will use the terms interchangeably.
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’.
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
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.
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!
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.
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.
More Unix tips and tricks: STDOUT, redirection.
How to redirect your output:
– If I do
$ ls
– I get output to STDOUT (the screen):
benne.html
Desktop
Examples
pic26867.jpg
– etc. But if I do
$ ls > file99
– I apparently don’t get anything. But! Look
inside file99:
$ more file99
benne.html
Desktop
– etc.
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
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:
bignob.py
2. Here is the first character in the name of a
‘hidden’ file (ls won’t list these – but ls –a will):
.bashrc
3. Here it means run the script called fred:
Note the space!
$ . fred
4. Here it means the present working directory:
$ export PATH=/usr/bin:.
$ ls ./*
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:
$ pwd
/home/ims/doc/teaching/comp_astron
$ ls
code latex figures
$ cd code
$ pwd
/home/ims/doc/teaching/comp_astron/code
– If I want to cd back to comp_astron, there’s
two ways to do it. The long way:
$ cd /home/ims/doc/teaching/comp_astron
– Or the short way:
$ cd ../
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!
some_file_name~
2. Here it is shorthand for your home directory:
$ cd ~/doc/teaching
•
(As an aside, cd with no arguments
always takes you direct to home.)
NASSP Masters 5003F - Computational Astronomy - 2009
ASCII
File types
Type
Suffix
Text
Various
Code (specialized text
files)
.f, .f90, .c, .cpp, .py,
etc
Binary
Flexible Image Transport Often .fit, but can vary
System (FITS)
Acroread output
.pdf
Postscript
.ps
Images
Eg .gif, .jpg, .png
Compressed by gzip
.gz
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!! *****
Python
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.
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.
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.
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:
export PYTHONPATH=/home/me/src
• Within your code slogger.py include the
line
from huge_brane import *
• Note that you leave off the .py bit.
Smaller chunks of python
• Within a module the largest chunks are
functions. The syntax of a typical function
is
def myfunction(foo, bar, bert):
# some stuff here
return ‘some value’ # optional
• 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:
newvar = myfunction(foo, bar, bert)
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.