Troupin2016 PythonOceanography
Troupin2016 PythonOceanography
Application to Oceanography
C. Troupin, E. Mason
SOCIB, IMEDEA
1. Introduction
3. Importing/exporting data)
4. Time series
5. 2-D plots
Table of contents
1. Introduction
1.1– Python vs. Matlab
1.2– Definitions and data types
1.3– Resources, tutorials, books
2. Installation, update, use
2.1– Installing
2.2– Running your code
3. Importing/exporting data)
3.1– ipython notebook
3.2– Comma-Separated Values file
3.3– CNV file
3.4– A few words about github
3.5– NetCDF file
4. Time series
5. 2-D plots
Introduction:
What? Why? How?
What is Python?
Programming language:
1. interpreted instructions executed directly
https://www.python.org
Why Python?
Source: http://langpop.corger.nl/
Python vs. Matlab
Python Matlab
General
programming language programming language + numerical
computing environment
open proprietary algorithms
general purpose linear algebra
Indexing
a[0] a(1)
a[-1] a(end)
a[::2] a(1:2:end)
Functions
a.max() max(a)
a.shape() size(a)
Quick example: hello.py
# ! / usr / bin / python
# −∗− c o d i n g : u t f −8 −∗−
’’’
This f u n c t i o n p r i n t s ” Hello world ”
’’’
def h e l l o ( ) :
print ” Hello world ”
return
d e f main ( ) :
hello ()
if name == ’ main ’:
main ( )
Quick example: hello.py
1. Try to document you code
Quick example: hello.py
1. Try to document you code
2. Use
# −∗− c o d i n g : u t f −8 −∗−
1. Try to document you code
2. Use
# −∗− c o d i n g : u t f −8 −∗−
1. Try to document you code
2. Use
# −∗− c o d i n g : u t f −8 −∗−
4. Indentation matters!
A few definitions
1. Numbers
Example:
g = 9.81
h = 4 . 1 3 5 6 6 7 6 6 2 e−15
Data types
1. Numbers
2. String
Example:
name = ” Rickman ”
s = ” this is a string ”
Data types
1. Numbers
2. String
3. List
Example:
l i s t = [ ’ one ’ , 2 , ’ t h r e e ’ , ’ f o u r ’ , 5 ]
Data types
1. Numbers
2. String
3. List
4. Tuple
Example:
t u p l e 1 = ( ’ one ’ , 2 , ’ t h r e e ’ , ’ f o u r ’ , 5 )
t u p l e 2 = ( 1 , ’ 1 ’ , ’ one ’ , [ 1 , 2 ] , ( 1 , 2 , 3 ) )
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
Example:
e m a i l = { ’ Evan ’ : ’ emason@imedea . u i b−c s i c . e s ’ , \
’ I r e n e ’ : ’ i r e n e . laiz@uca . es ’ ,\
’ Charles ’ : ’ ctroupin@socib . es ’}
email [ ’ Irene ’ ]
’ i r e n e . laiz@uca . es ’
Data types
1. Numbers
2. String
3. List
4. Tuple
5. Dictionary
More details:
https://docs.python.org/2/tutorial/datastructures.html#
Resources
Web:
I https://docs.python.org/2.7/tutorial/index.html
I https://developers.google.com/edu/python/introduction?hl=en
tutorial + exercises
I http://www.python-course.eu
I http://www.learnpython.org online code execution
https://pythonprogramming.net
I https://www.gitbook.com/book/djangogirls/djangogirls-tutorial
Resources
Learning platforms:
I Programming Foundations with Python Learn Object-Oriented
Programming (7 weeks)
I Code Academy (13 hours)
Resources
Youtube:
I Python Beginner Tutorial (For Absolute Beginners) (4 parts)
I Google Python Class (7 × 30 minutes)
I Zero to Hero with Python (11 hours)
Resources
Books:
I Learn Python the hard way, Z.A. Shaw, 2013
http://learnpythonthehardway.org/book/
I Learning Python, 5th Edition, M. Lutz, 2013
I Python Programming: An Introduction to Computer Science,
J.M. Zelle, 2002
Some differences:
I Print function
I Integer division
I Unicode
I ...
Python 3.x = present and future of the language
More details:
Python 2 or Python 3
Will Scientists Ever Move to Python 3?
Objectives of the course
Evan Mason
Oceanographer
Post-doctoral researcher at IMEDEA
10-year experience with Python
About the trainers
Evan Mason
Oceanographer
Post-doctoral researcher at IMEDEA
10-year experience with Python
Charles Troupin
Engineer, oceanographer
Head of SOCIB Data Center
2.5-year experience with Python
Structure of the course
1. Reading/writing
Structure of the course
1. Reading/writing
2. Time series
Structure of the course
1. Reading/writing
2. Time series
3. 2-D fields
Structure of the course
1. Reading/writing
2. Time series
3. 2-D fields
4. Functions, classes, modules
Installation
& use
Installing Python
I Uninstall packages:
$ pip u n i n s t a l l
More about pip
I Uninstall packages:
$ pip u n i n s t a l l
a p t o n c d ( 0 . 1 . 9 8 − b z r 1 1 7 −1.2)
b a c k p o r t s . s s l −match−h o s t n a m e ( 3 . 4 . 0 . 2 )
basemap ( 1 . 0 . 7 )
...
xhtml2pdf ( 0 . 0 . 6 )
zope . i n t e r f a c e ( 3 . 6 . 1 )
More about pip
I Uninstall packages:
$ pip u n i n s t a l l
a p t o n c d ===0.1.98 − b z r 1 1 7 −1.2
b a c k p o r t s . s s l −match−h o s t n a m e = = 3 . 4 . 0 . 2
basemap = = 1 . 0 . 7
...
xhtml2pdf ==0.0.6
zope . i n t e r f a c e = = 3 . 6 . 1
More about pip
I Uninstall packages:
$ pip u n i n s t a l l
M e t a d a t a −V e r s i o n : 1 . 1
Name : numpy
Version : 1.9.2
Summary : NumPy : a r r a y p r o c e s s i n g f o r numbers , s t r i n g s , ...
...
Requires :
Running your code: several solutions
or
$ mycode . py
if shebang
# ! / usr / bin / python
In [ 3 ] : string .
string . capitalize s t r i n g . encode s t r i n g . format ...
string . rstrip string . strip s t r i n g . upper
...
string . startswith string . translate
+ magic functions:
%run: Run the named file inside IPython as a program
%timeit: Time execution of a Python statement or expression
%who: Print all interactive variables, with some minimal formatting
ipython notebook
(interactive computational environment)
Exercise 1: changecase.py
:
Write a program that takes 2 arguments: the name and the surname,
both written with a mix of upper and lowercase, and return the name
with the first letter in uppercase and the surname with all the letters in
uppercase.
Examples:
changecase allan rickman returns Allan RICKMAN
changecase aLlAn ricKmaN returns Allan RICKMAN
I User-friendly
I Free, easy to write, easy to read
I Code and results visible online via http://nbviewer.ipython.org
Why ipython notebooks?
I User-friendly
I Free, easy to write, easy to read
I Code and results visible online via http://nbviewer.ipython.org
I Data story-telling
Structure of a notebook
Structure of a notebook
î: read_csv_file.ipynb
Seabird CTD file
Example: file dsb01.cnv
Example of CNV file
∗ Sea−B i r d SBE 9 D a t a F i l e :
∗ FileName = C:\OCEANO\CTD\DATOS\IMEDEA−SHEBEX\SB01 . hex
∗ Software Version Seasave V 7.22
∗ T e m p e r a t u r e SN = 5427
∗ C o n d u c t i v i t y SN = 3872
...
38.86539 2.78989 . . . 3 . 4 5 3 5 e−02 7.459
38.86542 2.78986 . . . 223 0 . 0 0 0 0 e +00
Seabird CTD file
I Install package:
p i p i n s t a l l −−u s e r s e a b i r d = = 0 . 6 . 3
Seabird CTD file
I Install package:
p i p i n s t a l l −−u s e r s e a b i r d = = 0 . 6 . 3
I Install package:
p i p i n s t a l l −−u s e r s e a b i r d = = 0 . 6 . 3
î: read_cnv_file.ipynb
How to get code from github
https://github.com/ctroupin/PythonCourseCadiz2016
How to get code from github
https://github.com/ctroupin/PythonCourseCadiz2016
https://www.unidata.ucar.edu/software/netcdf/docs/netcdf/ncdump.html
http://www.ferret.noaa.gov/Ferret/
yes ?
ncview
http://www.epic.noaa.gov/java/ncBrowse/
http://www.giss.nasa.gov/tools/panoply/
https://code.zmaw.de/projects/cdo
A manipulate (merging, averaging) netCDF files (+other
formats)
Examples: I Basic info (min, max, avg, size, . . . ):
cdo i n f o i n p u t . nc
http://nco.sourceforge.net/
A command line operations on netCDF files
Examples: I Average variable over domain:
ncwa −O −a l o n , l a t i n p u t . nc o u t p u t . nc
I Extract subregion:
n c k s −d l o n , 1 3 . , 1 8 . 0 −d l a t , 3 3 . 0 , 3 6 . 0
i n p u t . nc o u t p u t . nc
ODV – Ocean Data View
http://odv.awi.de/en/home/
A interactive exploration, analysis and visualization of
oceanographic data
Octave / Matlab
...
nc . c l o s e ( )
List of notebooks
Located in Data_ReadWrite
read csv file.ipynb: Comma separated values
read cnv file.ipynb: SeaBird CTD file
: 1-read_CMEMS_indexfile.ipynb
Programming in
Python #2
time series, scatter plots, . . .
Useful packages
Located in Plotting
plot line.ipynb: simple line plots and configuration
plot subfigure.ipynb: organizing plots in sub-figures
plot TS diagram.ipynb: scatter plot using temperature and salinity from CTD
: 2-plot_ibiza_temperature_salinity.ipynb
Programming in
Python #3
2D plots and plots on map
Useful packages
Located in Plotting
plot2D contours pcolor.ipynb: plot 2D fields with contour or pseudo-color techniques
Objective: create a plot with the salinity from a numerical model and
the velocities taken from a HF radar system.
: 3-plot_radar_salinity.ipynb