Title: UNIX intro
1UNIX intro
- Well 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 lt 10 level.
- bash is more professional.
Unix and linux look very similar to the user so
I will use the terms interchangeably.
2The UNIX file structure
- The prompt
- ims_at_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 dont. 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
3UNIX shell commands
- pwd
- present working directory - tells you where you
are in the file system. - cd ltnamegt
- change pwd to directory name.
- mkdir ltnamegt
- makes a new directory.
- rmdir ltnamegt
- removes (deletes) the named directory.
4UNIX shell commands
- ls
- list the files in the pwd.
- cp ltnamegt ltdestinationgt
- copies the named file.
- mv ltnamegt ltdestinationgt
- equals cp followed by rm. But mv can also move
directories. - rm ltnamegt
- removes (deletes) the named file.
Note that each of the last three can erase files
so Back up your work!
5UNIX shell commands
- ssh ltuser namegt_at_ltcomputer namegt
- 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 ltname of commandgt
- gives you a manual, ie a lot of documentation
(sometimes more than you really wanted!) for the
named command.
6Other 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.
7File types
ASCII
Binary
8FITS Flexible Image Transport System
This will be the default data format for this
course. See http//fits.gsfc.nasa.gov/fits_standa
rd.html
Cards
2880-byte blocks
1
80
1
Header
2
3
4
Header
36
KEYWORD VALUE / UNIT COMMENT
etc
Unused cards of last header block filled with
spaces (ASCII 32)
Binary table
Data
Data
or
Image
Data
Data
etc
Unused bytes of last data block filled with
spaces (ASCII 32)
9Text editors
- vi
- non-windows, but abstruse commands.
- pico
- non-windows, fairly user-friendly.
- emacs, xemacs
- the editor of choice for many, but I dont like
it. - gedit
- my favourite.
- IDE for python coding maybe ok
Whichever you choose Back up your work!!
10Python
11Python
- is a scripting language, which roughly
translated means - python code doesnt have to be compiled
- its pretty slow.
- is an object-oriented (OO) language. Some OO
code can look pretty wacky. But relax! you wont
have to write any. - !/usr/bin/env python huh..?
probably not, anyway.
12Python
- 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 dont have to run python as a script, you can
also use it interactively. Type python at your
normal unix prompt and youll get a new
sergeant prompt gtgtgt. You can enter your python
statements then line by line. Type control-d to
get out when youre finished.
13Large 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.
14How 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
15Smaller 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)
16Python 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.
17Python 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.
18Python 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
- Youll get 999,2,3. Cf mutability
gtgtgta4 gtgtgtba gtgtgta3 gtgtgtprint b
gtgtgtaa1,2,3 gtgtgtbbaa gtgtgtaa0999 gtgtgtprint bb
19Trouble with change in place
20OO
- OO programming is the business of constructing
objects. This is done via the class statement.
You probably wont need to use this. However,
objects themselves are inescapable in python if
you dont make them, someone else will. - In fact, almost everything in python is an object.
21OO
- Objects have two extra features
- 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. - 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.
22Python control structures
if lttest1gt do some stuff elif lttest2gt you
can have 0 or more of these do some other
stuff else you can have 0 or 1 of these
third lot of stuff
- There is NO goto statement in python. This is a
feature. - Anything after a is a comment.
while lttestgt 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 ltitemgt in ltlistgt
etc
23Python ins and outs
- Were going to mostly read our data from FITS
files, using a module called pyfits. - http//www.stsci.edu/resources/software_hardware/p
yfits - Well crunch up the data using a module called
numpy. - http//numpy.scipy.org/
- For graphical output well 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/