Nikolay Koldunov
koldunovn@gmail.com
This is part of Python for Geosciences notes.
%matplotlib inline
import matplotlib.pylab as plt
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
Installation instructions can be found in the README.md file of this repository. Better to use rendered version from GitHub.
In order to be productive you need comfortable environment, and this is what IPython provides. It was started as enhanced python interactive shell, but with time become architecture for interactive computing.
Since the 0.12 release, IPython provides a new rich text web interface - IPython notebook. Here you can combine:
print('I love Python')
I love Python
x = [1,2,3,4,5]
plt.plot(x);
from IPython.display import YouTubeVideo
YouTubeVideo('F4rFuIb1Ie4')
In order to start Jupyter notebook you have to type:
jupyter notebook
Web version can be accesed from the github repository.
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.
plt.plot??
Press SHIFT+TAB after opening bracket in order to get help for the function (list of arguments, doc string).
sum()
You can access system functions by typing exclamation mark.
!pwd
/Users/koldunovn/COURSE/python_for_geosciences
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
netcdf test_netcdf { dimensions: TIME = 366 ; LATITUDE = 94 ; LONGITUDE = 192 ; variables: float TIME(TIME) ; TIME:units = "hours since 1-1-1 00:00:0.0" ; float LATITUDE(LATITUDE) ; float LONGITUDE(LONGITUDE) ; float New_air(TIME, LATITUDE, LONGITUDE) ; New_air:missing_value = -9999.f ; }
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:
list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
And find out how long does it take to run it with %timeit magic function:
%timeit list(range(10))
396 ns ± 13 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Print all interactive variables (similar to Matlab function):
%whos
Variable Type Data/Info ---------------------------------- YouTubeVideo type <class 'IPython.lib.display.YouTubeVideo'> plt module <module 'matplotlib.pylab<...>ges/matplotlib/pylab.py'> x list n=5
Receive as argument both the current line where they are declared and the whole body of the cell.
%%timeit
range(10)
range(100)
348 ns ± 5.51 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Thre are several cell-oriented magic functions that allow you to run code in other languages:
%%bash
echo "My shell is:" $SHELL
My shell is: /bin/bash
%%perl
$variable = 1;
print "The variable has the value of $variable\n";
The variable has the value of 1
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)
Overwriting hello.py
And then run it:
%run hello.py
hello world!
<Figure size 432x288 with 0 Axes>
The %run magic will run your python script and load all variables into your interactive namespace for further use.
%whos
Variable Type Data/Info ---------------------------------- YouTubeVideo type <class 'IPython.lib.display.YouTubeVideo'> a str hello world! plt module <module 'matplotlib.pylab<...>ges/matplotlib/pylab.py'> x list n=5
In order to get information about all magic functions type:
%magic