0% found this document useful (0 votes)
7 views

20160324_python

Uploaded by

mamalee393
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

20160324_python

Uploaded by

mamalee393
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

IPSL BootCamp: python∗

Institute Pierre Simone Laplace, IPSL BootCamp


The content of the BootCamp can be found in:
https://forge.ipsl.jussieu.fr/igcmg_doc/wiki/Train

March 25, 2016

Contents
1 Introduction 2
1.1 Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

2 basics 3
2.1 before starting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 print . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.3 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.4 loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.5 list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.6 dictionary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.7 tuple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.8 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.9 libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

3 NO such basics 6
3.1 functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3.2 classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4 netCDF & python 8

5 drawing: matplotlib 9

6 Useful links 10

∗ Main author of this chapter: L. Fita, Laboratoire de Météorologie Dynamique (LMD). This work is licensed under Creative Commons

Attribution-ShareAlike 4.0 International License

1
IPSL BootCamp: 2016/03/24. python 2

1 Introduction
Python was born on 1989 by ’Guido van Rossum’ and takes its name from the ’Monthy Python Flying Circus’1
It is a high level interpreted language, designed to be easy to be read and it is object-oriented.
There is a ‘new’ version of python (python 3.x) which is not compatible with the python.2.x. This course uses python
2.x. In a mid term is recommendable to migrate to python 3.x, nowadays (2016) might not be fully recommendable.

• High level language: Programmer does not need to worry to much about to deal with the CPU and memory.
It is done by the program which it has a high level of abstraction. That facilitates the coding and reading the
source, but penalize efficiency and speed
• Interpreted language: Code is not compiled. When on executes the source, it is read and executed
• Objects: Encapsulated data and methods in a single piece of code. Very versatile and flexible
• easy to be read: It is mandatory to indent the code

Python is of free access and it is managed by the Python Software Foundation.

1.1 Object
In the old class programming languages like: C, Fortran, bash, ... variables might be algebraic data-sets (scalar,
vector, matrices) of 4 basic types: integer, float, boolean and character.
An object could be understood (in a very simple way) as a variable which has a more complex structure. It can
be a combination of variables, functions and data-sets. Thus it is not only a group of values, either a memory address
with a ‘dynamic’ behavior.

1 https://en.wikipedia.org/wiki/Monty_Python
IPSL BootCamp: 2016/03/24. python 3

2 basics
2.1 before starting
Some very basic zero stuff:

• #: comment character
• >>>: Terminal string in an ‘interactive’ python session (after $ python)
• Mandatory to indent on if, loop (recommended 4 white spaces)
• \: Character of continuation of line

• Case Sensitive program


• Language based on the uploading of different packages via import
• Counts as C-like (starting at 0!)

• Extension of the scripts .py


• [var][i]: from variable [var], access position i
• [obj].[mth]: from object [obj], use the method [mth]

2.2 print
Some initial prints:

>>> print 'Hola' prints string between ’ ’

Hola

Range print:

>>> print range(4) Range of 4 values from 0 to 3

[0, 1, 2, 3]

2.3 if
’conditionals’, a ’nice’ if

i = 3
if i == 2: ’:’ to mark end of ‘if’
print 'two' ’indent’ mandatory !!
elif i == 3: no-indent as it is part of the initial ‘if’
print 'three'
else:
print 'whatever' No end if instruction, just do not indent

three
IPSL BootCamp: 2016/03/24. python 4

2.4 loops
Again, indented ‘nice’ loops

for i in range(4): ’:’ as end of loop instruction


print i indent to mark the loop

0
1
2
3

for i in range(1,10,2): from ’1’ to ’10’ every ’2’


print i

1
3
5
7
9

2.5 list
‘List’ is a data structure of values sorted as they fill it. It can grow, and it can contain different types of data

>>> listvalues = [ ] ‘[ ]’ as list creation


>>> listvalues.append('1') giving the first value as string
>>> listvalues.append(2.) second value as float
>>> listvalues.append(3) third value as integer
>>> listvalues.append(2)
>>> print listvalues print values

['1', 2.0, 3, 2]

>>> print listvalues[2]/listvalues[1], Tacking different values.


listvalues[2]/listvalues[3]
1.5 1 Result depends on type of variable
>>> print type(listvalues) ‘type’ to know the type of the list
<type 'list'>
>>> print type(listvalues[0]), ‘type’ to know the type of the values
type(listvalues[1]) within the list
<type 'str'> <type 'float'>

Some methods of a list


>>> print listvalues.index(3)
2 print a single value of the list
>>> list2 = listvalues.pop(2)
>>> print list2 removing the third value of the list
3 provides the removed value
>>> print listvalues final list without the third value
['1', 2.0, 2]
IPSL BootCamp: 2016/03/24. python 5

2.6 dictionary
Data-structure composed with pairs of values like in a dictionary with its ‘word’ and its ‘meaning’. Values are not
alphabetically sorted. It can contain different kind of data
>>> lmdcouloir = { }
’{ }’ definition of a variable as a dictionary
>>> lmdcouloir['218'] = ['Shan', 'Lluis']
providing values for key ’218’ as a list
>>> lmdcouloir['313'] = 'seminar room'
providing values for key ’313’ as a string
>>> print lmdcouloir
printing content of the dictionary
{'313': 'seminar room', '218': ['Shan',
'Lluis']}
print content for the ’313’ value
>>> print lmdcouloir['313']
seminar room
Some methods of a dictionary:
>>> lmdcouloir.keys() listing all keys of the dictionary
['313', '218']
>>> lmdcouloir.has_key('315') search for the existence of a ‘key’
False

2.7 tuple
Data-structure with values. It can not grow. Data access is more faster. It can mix data types
>>> tuplevalues = () ‘( )’ definition of variable as tuple
>>> tuplevalues = (1, 2, 3, 89) assigning values to tuple
>>> print tuplevalues printing values
(1, 2, 3, 89)

2.8 Miscellaneous
Some general hints
• >>> print dir([obj]): list of the methods of [obj]
• >>> print [obj].__doc__: provides documentation of object [obj]
• >>> quit(): exiting python
• """: for documentation in a class/function
• ’Header’ beginning of scripts, where one can import a huge number of different libraries/modules/packages

import [library] Import all the content of the library


import [library] as [abbreviation] library’s functions called as [abbreviation].[function]
from [library] import [section] just using a section of the library

2.9 libraries
python can be run with complementary powerful libraries. They cover a wide range of functinoalities and capabilities.
A very short and the most valuable ones is here given:
• numpy, scipy: the scientific numerical libraries
• os, subProcess: for system calls
• matplotlib: for plotting (similar to ’matplot’)
• datetime: for date/time variables
• optparse: parsing for arguments options when calling script
IPSL BootCamp: 2016/03/24. python 6

3 NO such basics
There are two main ways to generate self content actions: functions, classes

3.1 functions
The general structure of a function is this:

def FunctionName([Arg1], [Arg2], ...): ‘def’ marks definition of function ‘:’ end of definition
""" Description of the function ””” to start documentation of the function
""" ””” to end documentation of the function

(...) whatever is required

return [result of the function] values to provide as result of the function

A given example for the Fibonacci’s series https://en.wikipedia.org/wiki/Fibonacci_number

def fibonacci(Nfibo): Function definition


""" Function to provide the Nfibo Retrieval of ‘Nbio’ numbers of Fibonacci
numbers of the Fibonacci numbers
Nfibo= Quantitty of numbers
more info:
http://en.wikipedia.org/wiki/Fibonacci_number
N_i = N_i-2 + N_i-1
"""
import numpy as np Getting module ‘numpy’ as np
errmsg= 'ERROR  error  ERROR  error'

fname = 'fibonacci'

if Nfibo == 'h': Message to appear in case of help


print fname + '_______' is required when Nfbio=’h’
print fibonacci.__doc__ here will be printed the header content within ”””
quit()

if Nfibo < 2: Error message if ‘Nfbio’ is too small


print errmsg
print ' ' +fname+ ': Increase' + \
Nfbio!'
print ' only', Nfibo, 'given !!'
quit(-1) Error message system output ‘-1’

numbers = np.zeros((Nfibo), dtype=int) numpy array of ‘Nfbio’ zeros

numbers[0] = 0 First value ‘0’


numbers[1] = 1 Second value ‘1’

for i in range(Nfibo-2): Loop over the ‘Nfbio-2’ numbers


numbers[i+2]=numbers[i]+numbers[i+1]

return numbers Returning the numpy array

When we call:
IPSL BootCamp: 2016/03/24. python 7

>>> fibonacci('h') Regarding function documentation


fibonacci_______
Function to provide the Nfibo
numbers of the Fibonacci numbers
Nfibo= Quantitty of numbers
more info: h
ttp://en.wikipedia.org/wiki/Fibonacci_number
N_i = N_i-2 + N_i-1

>>> fibonacciSeries = fibonacci(7) Function brings back a numpy array


>>> print fibonacciSeries it has to be printed to see its values!
[0 1 1 2 3 5 8]

3.2 classes
’classes’, are similar to functions, but much more complex and flexible (too much for an introductory course!)
IPSL BootCamp: 2016/03/24. python 8

4 netCDF & python


There are different libraries to access netCFD. In this case we will use http://netcdf4-python.googlecode.com/
svn/trunk/docs/netCDF4-module.html. Very flexible, robust and easy to use
• Some basic functions

– [ncobj] = NetCDFFile([filename], ’[access]’): open a file [filename] as object [ncobj] with [access]=’w’,
write; ’a’, append, ’r’, read
– [ncobj].variables: dictionary will all the variables of the file
– [ncobj].dimensions: dictionary will all the dimensions of the file
– [ncvarobj] = [ncobj].variables[’varnaname’]: object [ncvarobj] with variable ’varname’
– [ncvarobj].dimensions: dictionary will all the dimensions of the variable ’varname’
– [ncvarobj][:]: all the values of variable ’varname’ (shape automatically given)
– [ncvarobj].shape: shape of the variable ’varname’
– createVariable([varname], [kind], [dimensions], fill_value=[missValue]): creation of a vari-
able with a missing value of [missValue]
• Example with ‘reading_nc.py’

import numpy as np
from netCDF4 import Dataset as NetCDFFile Importing ‘netCDF4’ module to manage netCDF

filename = 'Relief.nc' name of the file


toponame = 'RELIEF' name of the variable with topography

ncobj = NetCDFFile(filename, 'r') open netCDF in read mode

objtopo = ncobj.variables[toponame] getting topography variable object

print 'variables:',ncobj.variables.keys() printing all variables within the file


print 'dimension of the topography:', \
objtopo.shape
topovals = objtopo[:] getting topography values
utopo = objtopo.getncattr('units') getting the ‘units’ as an attribute
print 'max. height:', np.max(topovals), \ computing the maximum height
utopo

ncobj.close()

When we execute

$ python reading_nc.py
variables: [u'longitude', u'latitude', u'RELIEF']
dimension of the topography: (1080, 2160)
max. height: 7833.0 m
IPSL BootCamp: 2016/03/24. python 9

5 drawing: matplotlib
For plotting there is a matplot like environment http://matplotlib.org/. Very flexible, robust and powerful.
Example with ’plotting_topo.py’

import numpy as np
from netCDF4 import Dataset as NetCDFFile
import matplotlib as mpl importing ‘matplotlib’
mpl.use('Agg')
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap importing coastal line for mapping

filename = 'Relief.nc'
toponame = 'RELIEF'
ncobj = NetCDFFile(filename, 'r') opening for reading netCDF

objtopo = ncobj.variables['toponame'] getting topography variable as an object


olon = ncobj.variables['longitude'] getting longitudes variable as an object
olat = ncobj.variables['latitude'] getting latitude variable as an object
topovals = objtopo[:] getting topography values
utopo = objtopo.getncattr('units')

plt.rc('text', usetex=True) using LATEX like format for strings in the plot

dx = olon.shape[0] x-range of the values


dy = olat.shape[0] y-range of the values
nlon = np.min(olon) minimum of the longitudes
xlon = np.max(olon) maximum of the longitudes
nlat = np.min(olat) minimum of the latitudes
xlat = np.max(olat) maximum of the latitudes
plt.xlim(nlon, xlon) x-limits of the plot
plt.ylim(nlat, xlat) y-limits of the plot
m = Basemap(projection='cyl', \ using ‘Basemap’ to geo-localize the values
llcrnrlon=nlon, llcrnrlat=nlat, \ in a cylindric projection
urcrnrlon=xlon, urcrnrlat= xlat, \
resolution='l') with low (‘l’) resolution information

lons = olon[:] getting longitudes (in file is 1D)


lats = olat[:] getting latitudes (in file is 1D)
x,y = m(lons,lats) lon, lat 2D matrix plotting creation

plt.pcolormesh(x,y,topovals, \ plotting using shading of the topography


cmap=plt.get_cmap('terrain'), \ ‘x’, ‘y’, coordinates, ‘topovals’ values
vmin=0., vmax=7000.) ‘terrain’ color bar within the range [0, 7000.]
cbar = plt.colorbar() drawing color bar
cbar.set_label(utopo) giving units to the color bar
m.drawcoastlines() drawing coastal-line

plt.xlabel('W-E') x-axis label


plt.ylabel('S-N') y-axis label
plt.title('Topography from a ' + \ title of the graph
'LMDZ domain at 1/' + \
str(olon.shape[0]/360) + ' deg')
plt.savefig("topo.png") output as Portable Network Graphics (png)
plt.close() finishing graphic
IPSL BootCamp: 2016/03/24. python 10

Which generates:

6 Useful links
• Python Source: https://www.python.org/
• Shan’s python course: http://www.xn--llusfb-5va.cat/python/ShanCourse/PythonCourse.html (almost
the same content as here)
• List of some useful modules: https://wiki.python.org/moin/UsefulModules
• Aymeric Spiga’s PLANETOPLOT plotting toolkit from LMD useful to plot http://www.lmd.jussieu.fr/
~aslmd/planetoplot
• L. Fita’s netCDF/plottiong toolkit from LMD for netCDF file managements is already available from LMDZ_WRF
subversion trunk repository:

$ svn co http://svn.lmd.jussieu.fr/LMDZ_WRF/trunk/tools LFita_pyTools

– nc_var.py: wrapper to use all functions:


$ python nc_var.py -o [operation] -f [file] -S [values]
– nc_var_tools.py, all functions and classes which are used throughout nc_var.py
– drwaing.py: wrapper to use all functions:
$ python drawing.py -o [graphic] -f [file] -S [values]
– drawing_tools.py, all functions and classes which are used throughout drawing.py
– variables_values.dat, necessary external ASCII file with CD-definition of variables

You might also like