NASSP Masters 5003F - Computational Astronomy - 2009 UNIX* intro We’ll use the ‘command line’...
-
Upload
emily-mcbride -
Category
Documents
-
view
214 -
download
0
Transcript of NASSP Masters 5003F - Computational Astronomy - 2009 UNIX* intro We’ll use the ‘command line’...
NASSP Masters 5003F - 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.– Generally bash or csh.– Do ‘printenv SHELL’ to find out yours.– Some differences at the < 10% level.– bash is ‘more professional’.
* Unix and linux look very similar to the user so I will use the terms interchangeably.
NASSP Masters 5003F - Computational Astronomy - 2009
The UNIX file structure• The ‘prompt’
– 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
NASSP Masters 5003F - Computational Astronomy - 2009
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.
NASSP Masters 5003F - Computational Astronomy - 2009
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!
NASSP Masters 5003F - Computational Astronomy - 2009
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.
NASSP Masters 5003F - Computational Astronomy - 2009
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.
NASSP Masters 5003F - Computational Astronomy - 2009
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
NASSP Masters 5003F - Computational Astronomy - 2009
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
NASSP Masters 5003F - Computational Astronomy - 2009
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!! *****
NASSP Masters 5003F - Computational Astronomy - 2009
Python
NASSP Masters 5003F - Computational Astronomy - 2009
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.
NASSP Masters 5003F - Computational Astronomy - 2009
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.
NASSP Masters 5003F - Computational Astronomy - 2009
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.
NASSP Masters 5003F - Computational Astronomy - 2009
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 *
NASSP Masters 5003F - Computational Astronomy - 2009
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)
NASSP Masters 5003F - Computational Astronomy - 2009
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.
NASSP Masters 5003F - Computational Astronomy - 2009
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’,…]
NASSP Masters 5003F - Computational Astronomy - 2009
Python operations
• They are mostly pretty standard.
• ‘Change in place’ can be a trap. Eg, type ‘python’ at the prompt, then try:
• This should print ‘4’. 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
NASSP Masters 5003F - Computational Astronomy - 2009
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()
NASSP Masters 5003F - Computational Astronomy - 2009
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.
NASSP Masters 5003F - Computational Astronomy - 2009
OO• Objects have two extra features:
1. Attributes, which have names separated from the object name by a dot, eg fred.height is an attribute named height which belongs to object type of which fred is an example.
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.
NASSP Masters 5003F - Computational Astronomy - 2009
Python control structuresif <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.
NASSP Masters 5003F - Computational Astronomy - 2009
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 a module called numpy.– http://numpy.scipy.org/
• For graphical output we’ll use module ppgplot, (2 ps) which is a (fairly crude) wrapper to a package called PGPLOT.– http://efault.net/npat/hacks/ppgplot/– http://www.astro.caltech.edu/~tjp/pgplot/