This is part of Python for Geosciences notes.
================
Core scientific packages¶
When people say that they do their scientific computations in Python it's only half true. Python is a construction set, similar to MITgcm or other models. Without packages it's only a core, that although very powerful, does not seems to be able to do much by itself.
There is a set of packages, that almost every scientist would need:
We are going to talk about all exept Sympy
IPython and pylab¶
In order to be productive you need comfortable environment, and this is what IPython provide. It was started as enhanced python interactive shell, but with time become architecture for interactive computing.
Starting IPython with --pylab option loads some necessary modules (NumPy for data array support and Matplotlib for plotting support), that make it similar to Matlab console.
In order to use latest version of IPython on zmaw computers one has to load appropriate module:
module load python/2.7-ve2
and then
ipython --pylab
On Ubuntu or other Debian-based distributions something like this will set you up for the rest of the course:
sudo apt-get install ipython-notebook python-matplotlib python-scipy python-pandas python-sympy python-mpltoolkits.basemap netcdf-bin cdo
IPython notebook¶
Since the 0.12 release, IPython provides a new rich text web interface - IPython notebook. Here you can combine:
Code execution¶
print('I love Python')
Text (Markdown)¶
$\LaTeX$ equations¶
$$\int_0^\infty e^{-x^2} dx=\frac{\sqrt{\pi}}{2}$$ $$ F(x,y)=0 ~~\mbox{and}~~ \left| \begin{array}{ccc} F''_{xx} & F''_{xy} & F'_x \\ F''_{yx} & F''_{yy} & F'_y \\ F'_x & F'_y & 0 \end{array}\right| = 0 $$
Plots¶
x = [1,2,3,4,5]
plot(x)
Rich media¶
from IPython.display import YouTubeVideo
YouTubeVideo('F4rFuIb1Ie4')
Run notebook¶
In order to start IPython notebook you have to type:
ipython notebook
but in order to work in the notebook efficiently one has to start it with pylab and inline mode, that allow inline graphics
ipython notebook --pylab inline
on ZMAW computers, where default browser is Konqueror, you might need to use:
ipython notebook --pylab inline --browser="firefox"
You can download and run this lectures:¶
Web version can be accesed from the github repository.
You can download them as .zip file:
wget https://github.com/koldunovn/python_for_geosciences/archive/master.zip
Unzip:
unzip master.zip
And run:
cd python_for_geosciences-master/
ipython notebook --pylab inline
Main IPyhton features¶
Getting help¶
You can use question mark in order to get help. To execute cell you have to press Shift+Enter
?
Question mark after a function will open pager with documentation. Double question mark will show you source code of the function.
plot??
Press TAB after opening bracket in order to get help for the function (list of arguments, doc string).
sum(
Accessing the underlying operating system¶
You can access system functions by typing exclamation mark.
!pwd
If you already have some netCDF file in the directory and ncdump is installed, you can for example look at its header.
!ncdump -h test_netcdf.nc
Example of cdo use:
!cdo nyear test_netcdf.nc
Get information from OS output to the python variable
nmon = !cdo nmon test_netcdf.nc
nmon
Return information from Pyhton variable to the SHELL
!echo {nmon[1]}
Magic functions¶
The magic function system provides a series of functions which allow you to control the behavior of IPython itself, plus a lot of system-type features.
Let's create some set of numbers using range command:
range(10)
And find out how long does it take to run it with %timeit magic function:
%timeit range(10)
Print all interactive variables (similar to Matlab function):
%whos
Cell-oriented magic¶
Receive as argument both the current line where they are declared and the whole body of the cell.
%%timeit
range(10)
range(100)
Thre are several cell-oriented magic functions that allow you to run code in other languages:
%%bash
echo "My shell is:" $SHELL
echo "My memory status is:"
free
%%perl
$variable = 1;
print "The variable has the value of $variable\n";
You can write content of the cell to a file with %%writefile (or %%file for ipython < 1.0):
%%writefile hello.py
#if you use ipython < 1.0, use %%file comand
#%%file
a = 'hello world!'
print(a)
And then run it:
%run hello.py
The %run magic will run your python script and load all variables into your interactive namespace for further use.
%whos
In order to get information about all magic functions type:
%magic
Comments !