nassp masters 5003s - computational astronomy - 2009 lecture 2 more python. –good books:...

27
NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 • More python. – Good books: • “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); • “Python in a Nutshell”, Martelli, O’Reilly (the ‘snake book’). • FITS files.

Upload: allison-day

Post on 04-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003S - Computational Astronomy - 2009

Lecture 2

• More python.– Good books:

• “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’);

• “Python in a Nutshell”, Martelli, O’Reilly (the ‘snake book’).

• FITS files.

Page 2: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

OO

• OO programming is the business of constructing objects. This is done via the class statement. You probably won’t need to use this. However, objects themselves are inescapable in python – if you don’t make them, someone else will.

• In fact, almost everything in python is an object.

Page 3: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

OO• Objects have two extra features:

1. Attributes, which have names separated from the object name by a dot, eg fred.shape is an attribute named shape which belongs to object type of which fred is an example (eg a numpy array object).

2. Methods: these are attributes which are functions. Like any function they can accept arguments, and must still be written with empty brackets even if there are no arguments. Eg the append() method of lists and the copy() method of dictionaries.

Page 4: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Python variables

• Ways to arrange several objects:– A single one is called a scalar.– A list: elements numbered*, data types can be

different.– A tuple: similar to a list but written with round

brackets rather than square.– A dictionary: elements accessed by a key,

data types can be different; curly brackets.

• These groupings are themselves objects.

*Python numbering starts from zero.

( , 3.14, ‘fred’,…)

{‘foo’: , ‘pi’:3.14, ‘him’:’fred’…}

[ , 3.14, ‘fred’,…]

Page 5: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Python scalars

• Numbers– Integers– Floating-point– Complex

• Logical

• Strings

• Special

• Objects (includes everything)

-43, 0, 1, 219

-2.9, 4.76e6

0.2 + 5.3j

True, False

‘Fred’, ‘impossible’, ‘23’, ‘True’

None

Page 6: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Python operations

• They are mostly pretty standard.– Arithmetic binary operators:

• Note that they can have different effect on strings.• Note that

is short for

– Logical operators:

– Comparison operators:

+, -, *, /

a += 1 # etc

a = a + 1 # etc

and, or, not

>, >=, <, <=, ==

Page 7: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Python traps• The most common is really a unix trap.

Suppose you write your nice script dothings.py. You’ve been careful with your grammar, and have as your first line

• You try to run dothings.py on the command line, but get:

• To fix this you have to do

• This makes dothings.py ‘executable’.NASSP Masters 5003S - Computational Astronomy - 2010

#!/usr/bin/python

bash: ./dothings.py: Permission denied

chmod +x dothings.py

Page 8: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Python traps

• For some types of python object, several names can point to a single data area. Changing one of these then changes all the others. This is usually not what you want to happen.

• Since it is a common thing to want to create a new variable name with the value of an old one, we have to be careful of this behaviour in python, and find work-arounds for variable types where it is a problem.

Page 9: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Python traps

• Compare the behavior of scalars with lists. We can spawn a new variable name from a scalar, which then has its own memory area:

• This should print ‘4’. But now try:

• You’ll get ‘[999,2,3]’. Cf mutability…

>>>a=4>>>b=a>>>a=3>>>print b

>>>aa=[1,2,3]>>>bb=aa>>>aa[0]=999>>>print bb

Page 10: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Trouble with ‘change in place’

Type: Mutable? Avoid the problem by:

Simple scalar No

Scalar object It depends..

Use copy module

List Yes bb=aa[:]

Tuple No

Dictionary Yes bb=aa.copy()

Page 11: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

More python traps:

• The range statement:

– Note last element is not 4, but 3.

• List slices:

– Last element is not 2, but 1.

• CHECK THESE THINGS before you accept the results of your code!

>>>mylist = range(4)>>>print mylist[0, 1, 2, 3]

>>>print mylist[:2][0, 1]

Page 12: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

More python traps:

• Truncation during division. Try:

– This returns, not 1.333, but 1. This is usually not what you want.

• Better to get into the habit of ALWAYS dividing by a floating-point. Eg:

– yields 1.3333. If you WANT the truncation, use // not /.

>>>a=4>>>b=3>>>print a/b

>>>a=4>>>b=3>>>print a/float(b)

Page 13: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Python control structures

if <test1>: # do some stuffelif <test2>: # you can have 0 or more of these # do some other stuffelse: # you can have 0 or 1 of these # third lot of stuff

while <test>: # do loop stuff break # optional – dumps out and avoids the ‘else’. continue # like ‘goto while’.else: # optional - processing after normal loop exit. # stuff to do after normal loop exit

for <item> in <list>: # etc

•There is NO ‘goto’ statement in python. This is a feature.•Anything after a # is a comment.

Page 14: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Some neato modules• math

– Gives you access to constants like pi, + math operations such as sqrt(), cos(), atan2() etc.

• os - particularly os.path– Lets you check if files exist, lets you delete

files etc

NASSP Masters 5003S - Computational Astronomy - 2009

import osif os.path.exists(‘myfile’): # do something. Maybe do os.remove(‘myfile’)

Page 15: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

One more neato module• sys

– sys.argv is a list of command-line arguments.• Beware though, they are all passed as strings.

– Suppose you put the following statements in your python script bigdogs.py:

– If you then run it on the command line, with an additional argument 0:

– Because bb is a string ‘0’ and cc is an integer 0.NASSP Masters 5003S - Computational Astronomy - 2009

#!/usr/bin/pythonimport sysbb = sys.argv[1]cc = int(bb)print bb>0, cc>0

$ bigdogs.py 0True False

Page 16: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

Python ins and outs

• We’re going to mostly read our data from FITS files, using a module called pyfits.– http://www.stsci.edu/resources/

software_hardware/pyfits/

• We’ll crunch up the data using modules called numpy and scipy.– http://numpy.scipy.org/– http://www.scipy.org/

• For graphical output we’ll use module pylab.– http://matplotlib.sourceforge.net/index.html

Page 17: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

Manuals and example code

• You won’t need to download manuals for pyfits, numpy, scipy or pylab. You can get them from my home page:– http://www.ast.uct.ac.za/~ims/teaching/teaching.html

• Some of these manuals are huge – I recommend you neither print or read them in entirety, but rather:– Read the bits you need using acroread.– Look at the example code I’ll provide you from

time to time. There are already a few available from my page.

Page 18: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

Another few cm of the python.

• Function returns. Note:

• This is a tuple. It is better to be explicit...

>>> def some_func(arg1,arg2):... a = arg1 + arg2... b = arg1 – arg2... return a, b...>>> c = some_func(42, 2)>>> print c(44, 40)

>>> def some_func(arg1,arg2):... a = arg1 + arg2... b = arg1 – arg2... return (a, b)

Page 19: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

Another few cm of the python.• Also in the function call:

– is better than

• You can also force it to return a list:

>>> (summ, diff) = some_func(42, 2)

>>> c = some_func(42, 2)

>>> def anuther_func(arg1,arg2):... a = arg1 + arg2... b = arg1 – arg2... return [a, b]...>>> c = anuther_func(20, 19)>>> print c[39, 1]

Page 20: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

Another few cm of the python.• The for statement - most of you know this

way:

• But you can also do this:

• Saves a bit of typing.

>>> mylist = [‘peas’,’beans’,’dead dog’]>>> for i in range(len(mylist)):... item = mylist[i]... print item...peasbeansdead dog

>>> mylist = [‘peas’,’beans’,’dead dog’]>>> for item in mylist:... print item...peasbeansdead dog

Page 21: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

FITS = Flexible Image Transport SystemThis will be the default data format for this course. See:

http://fits.gsfc.nasa.gov/fits_standard.html

PrimaryHDU

Extension 3

Extension 2

Extension 1

etc…

Header-Data Units

Data

Data

Data

Header

Header

Data

2880-byte blocks

etc…

etc…

“Cards”1 80

1

2

3

4

36

Unused cards of last header block filled with spaces (ASCII 32)

or

Binary table:

‘Image’

Unused bytes of last data block filled with spaces (ASCII 32)

KEYWORD = VALUE / [UNIT] COMMENT

Page 22: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

WCS keywords of FITS files

• WCS stands for World Coordinate System.

• http://fits.gsfc.nasa.gov/fits_wcs.html

• What they’re for: pixellated data – ie samples of some quantity on a regular grid.

• WCS keywords define the mapping between the pixel index and a world coordinate system.

• Eg: a 2d image of the sky. We want to know which sky direction the (j,k)th pixel corresponds to.

Page 23: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

Eg, projection onto a tangent plane.

θ

Pixel grid on tangent plane

WCS must encode therelation between θ and thepixel number.

Page 24: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

WCS example continuedThe general formula in this case is

(p - pref) * scale = tan(w – wref).

p is the pixel coordinate and w theworld coordinate. w might eg beright ascension or declination.

p- pref

w- wref

Note:(1) pref can be real-valued;(2) By convention, p at the centre of the 1st pixel = 1.0.

WCS must describe 4 things:1. pref

2. wref

3. scale4. the nature of the functional relation.Perhaps also world units.

Page 25: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

WCS keywords for array extensions• In what follows, n is an integer, corresponding to

one of the dimensions of the array.– CRVALn – wref.– CRPIXn – pref.– CDELTn – scale.– CTYPEn – an 8-character string encoding the function

type (eg ‘TAN---RA’). There is an agreed list of these.– CUNITn – string encoding the unit of w (eg ‘deg’). Also an

agreed list.

• In addition, rotated coordinate systems can be defined via either adding PCi_j keywords to the above scheme, or replacing CDELTn by CDi_j keywords. But I don’t want to get too deeply into this.

• Analogous (starting with T) WCS keywords are also defined for table columns.

Page 26: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

NASSP Masters 5003F - Computational Astronomy - 2009

FITS interfaces and software:

• CFITSIO– Aging, user-unfriendly interface to FITS. Most

more modern interfaces are built on top of it though.

• FTOOLS (from HEASARC web site)– Very useful set of tools for doing everything

you can think of to or with FITS-format data.– But (Feb 2010: were) too many tasks bundled

into the one large, not-very-portable package.

• DS9– A very useful, compact, portable FITS image

viewer.

Page 27: NASSP Masters 5003S - Computational Astronomy - 2009 Lecture 2 More python. –Good books: “Learning Python”, Lutz & Ascher, O’Reilly (the ‘rat book’); “Python

A pyfits trap:• On the nassp computers, pyfits does not

return numpy arrays, it returns numarray arrays. So if you do eg

• you will get an error. A workaround is:

• And similar for reading files – convert them using numpy.array().

NASSP Masters 5003S - Computational Astronomy - 2010

>>>import numpy as nu>>>import pyfits as pf>>>fred = nu.zeros([3,3],nu.float)>>>hdu = pf.PrimaryHDU(fred)

>>>import numarray as old_nu>>> hdu = pf.PrimaryHDU(old_nu.array(fred))