0% found this document useful (0 votes)
43 views124 pages

Troupin2016 PythonOceanography

The document introduces Python for scientific applications in oceanography, providing an overview of Python versus MATLAB, definitions of common terms, and resources for learning Python. It discusses installing Python via download or package manager and installing additional modules like SciPy using pip or by building from source code. The course objectives are to use Python for oceanography problems, read/write files, make figures, and find online resources.

Uploaded by

Marta Gomes
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)
43 views124 pages

Troupin2016 PythonOceanography

The document introduces Python for scientific applications in oceanography, providing an overview of Python versus MATLAB, definitions of common terms, and resources for learning Python. It discusses installing Python via download or package manager and installing additional modules like SciPy using pip or by building from source code. The course objectives are to use Python for oceanography problems, read/write files, make figures, and find online resources.

Uploaded by

Marta Gomes
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/ 124

Introduction to Scientific Python

Application to Oceanography

C. Troupin, E. Mason

SOCIB, IMEDEA

Cádiz, 27-29 January 2016


Table of contents

1. Introduction

2. Installation, update, use

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

2. dynamically typed type checking at run-time

3. object-oriented classes, objects, methods, . . .

4. high-level strong abstraction

https://www.python.org
Why Python?

1. Simple, easy to learn syntax


2. Open
3. Large user community doc, support, packages
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)

Numpy for Matlab users:


https://docs.scipy.org/doc/numpy-dev/user/numpy-for-matlab-users.html
Quick example: hello.py


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 −∗−

if you’re using non-ascii characters


Quick example: hello.py


1. Try to document you code
2. Use
# −∗− c o d i n g : u t f −8 −∗−

if you’re using non-ascii characters


3. In Python 3:
print ( ” Hello world ! ” )
Quick example: hello.py


1. Try to document you code
2. Use
# −∗− c o d i n g : u t f −8 −∗−

if you’re using non-ascii characters


3. In Python 3:
print ( ” Hello world ! ” )

4. Indentation matters!
A few definitions

Object: Python’s abstraction for data


https:
//docs.python.org/2/reference/datamodel.html#index-0
Function: series of statements which returns some value to a caller
https://docs.python.org/2/glossary.html#term-function
Module: file containing Python definitions and statements
https://docs.python.org/2/tutorial/modules.html
Class: logical grouping of data and methods (functions)
https://docs.python.org/2/tutorial/classes.html
Data types

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 ) )

Tuples are immutable (fixed value)


Data types

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

Complete list: https://wiki.python.org/moin/PythonBooks


Python 2 vs Python 3

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

1. Use Python to solve oceanography-related problems


Objectives of the course

1. Use Python to solve oceanography-related problems


2. Read/write various types of files
Objectives of the course

1. Use Python to solve oceanography-related problems


2. Read/write various types of files
3. Generate high-quality figures
Objectives of the course

1. Use Python to solve oceanography-related problems


2. Read/write various types of files
3. Generate high-quality figures
4. Find resources on the internet for specific problems
Objectives of the course

1. Use Python to solve oceanography-related problems


2. Read/write various types of files
3. Generate high-quality figures
4. Find resources on the internet for specific problems

What we won’t (can’t) do:


teach you how to be a good programmer
About the trainers

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

Hard way: download source and compile:


https://www.python.org/downloads/
Installing Python

Hard way: download source and compile:


https://www.python.org/downloads/
Normal way: use installer or package:
I Windows:
https://www.python.org/downloads/windows/
I Mac OS:
https://www.python.org/downloads/mac-osx/
I Linux: package manager:
python2.x and python2.x-dev packages
Installing Python

Hard way: download source and compile:


https://www.python.org/downloads/
Normal way: use installer or package:
I Windows:
https://www.python.org/downloads/windows/
I Mac OS:
https://www.python.org/downloads/mac-osx/
I Linux: package manager:
python2.x and python2.x-dev packages
Easy way: Python distributions such as:
Anaconda free
Enthought Canopy free and commercial
Python(x,y) free, Windows only
+ others
Installing modules

Example: SciPy (http://www.scipy.org/):


mathematics, science, and engineering
Installing modules

Example: SciPy (http://www.scipy.org/):


mathematics, science, and engineering
Easy way: Windows, Linux, Mac:
use Scientific Python distribution
Installing modules

Example: SciPy (http://www.scipy.org/):


mathematics, science, and engineering
Easy way: Windows, Linux, Mac:
use Scientific Python distribution
Intermediate: Linux, Mac: install package
I Linux: package manager
I Mac: MacPorts, Homebrew
Installing modules

Example: SciPy (http://www.scipy.org/):


mathematics, science, and engineering
Easy way: Windows, Linux, Mac:
use Scientific Python distribution
Intermediate: Linux, Mac: install package
I Linux: package manager
I Mac: MacPorts, Homebrew
Harder: build from source
$ p y t h o n s e t u p . py i n s t a l l
Installing modules

Example: SciPy (http://www.scipy.org/):


mathematics, science, and engineering
Easy way: Windows, Linux, Mac:
use Scientific Python distribution
Intermediate: Linux, Mac: install package
I Linux: package manager
I Mac: MacPorts, Homebrew
Harder: build from source
$ p y t h o n s e t u p . py i n s t a l l

Better: pip (https://pypi.python.org/pypi/pip)


Installing modules

Example: SciPy (http://www.scipy.org/):


mathematics, science, and engineering
Easy way: Windows, Linux, Mac:
use Scientific Python distribution
Intermediate: Linux, Mac: install package
I Linux: package manager
I Mac: MacPorts, Homebrew
Harder: build from source
$ p y t h o n s e t u p . py i n s t a l l

Better: pip (https://pypi.python.org/pypi/pip)


Avoid: mixing installation methods
Using pip to manage modules

pip = recommended tool for installing Python packages


Using pip to manage modules

pip = recommended tool for installing Python packages


Installation: I Included in recent Python version
I Otherwise: download and run get-pip.py
https://pip.pypa.io/en/stable/installing/#install-pip
$ p y t h o n g e t −p i p . py
Using pip to manage modules

pip = recommended tool for installing Python packages


Installation: I Included in recent Python version
I Otherwise: download and run get-pip.py
https://pip.pypa.io/en/stable/installing/#install-pip
$ p y t h o n g e t −p i p . py

Usage: I Install latest version + dependencies:


$ pip i n s t a l l Package

I Specify exact version:


$ p i p i n s t a l l P a c k a g e ==x . y . z

I Specify minimum version:


$ p i p i n s t a l l ’ Package >=x . y . z ’
More about pip

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

I List installed packages:


$ pip l i s t

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

I List installed packages:


$ pip l i s t

I Output installed packages in requirements format:


$ pip freeze

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

I List installed packages:


$ pip l i s t

I Output installed packages in requirements format:


$ pip freeze

I Show information about installed packages:


$ p i p show P a c k a g e

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

Edit, then run in a shell:


$ p y t h o n mycode . py

or
$ mycode . py

if shebang
# ! / usr / bin / python

is present at the 1st line


Running your code: several solutions

Interactive python (ipython)


Auto-completion, exploring objects, . . .
In [ 2 ] : s t r i n g = ’ Hello a l l ’

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

More: Built-in magic commands


Running your code: several solutions

Integrated Development Environment (IDE)


(editor + build automation tools + debugger)

Examples: Atom, Eclipse, PyCharm, Idle, . . .


Complete list: https://wiki.python.org/moin/PythonEditors
Running your code: several solutions

ipython notebook
(interactive computational environment)

Rich text + command executions + figures + . . .


”Data story telling” (see Programming in Python 2)
What do we work with?


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

Tips: use the function sys.argv to be able to run the code as


$ c h a n g e c a s e name s u r n a m e
Programming in
Python #1
importing/exporting data
El més important, que quedi clar el
zen de python
Objective

Be able to read/write data in various formats used in oceanography


Objective

Be able to read/write data in various formats used in oceanography

Text files (ascii)


CSV
NetCDF
HDF
Images (geotiff)
matlab files
...
Objective

Be able to read/write data in various formats used in oceanography

Text files (ascii)


CSV
NetCDF
HDF
Images (geotiff)
matlab files
...

Be able to find the resources to read another format


What is an ipython notebook?
What is an ipython notebook?

Python: high-level programming language


https://www.python.org/
What is an ipython notebook?

Python: high-level programming language


https://www.python.org/
IPython: command shell for interactive computing
http://ipython.org/
What is an ipython notebook?

Python: high-level programming language


https://www.python.org/
IPython: command shell for interactive computing
http://ipython.org/
IPython notebook: web-based interactive computational environment
combining code, text, figures, . . .
http://ipython.org/notebook.html
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
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

Run current cell


Structure of a notebook

Run current cell


Add a new cell
Structure of a notebook

Run current cell


Add a new cell
Select type of cell
Structure of a notebook

Run current cell


Add a new cell
Select type of cell
Code cell
Structure of a notebook

Run current cell


Add a new cell
Select type of cell
Code cell
Text cell
Comma-Separated Values file
Example: file buoy-canaldeibiza SALT SBE37.csv

Example of CSV file


Platform , Instrument , Paramenter , U n i t s , Date , Value , QC value
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 12:00:00 , 36.916 , 1.0
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 01:00:00 , 36.936 , 1.0
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 02:00:00 , 36.929 , 1.0
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 03:00:00 , 36.927 , 1.0
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 04:00:00 , 36.925 , 1.0
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 05:00:00 , 36.948 , 1.0
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 06:00:00 , 36.95 , 1.0
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 07:00:00 , 36.954 , 1.0
B u o y C a n a l D e I b i z a , SCB−SBE37006 , sea water salinity , psu , 2015−12−01 08:00:00 , 36.933 , 1.0
Comma-Separated Values file

I Type ”python read csv file” in search engine


Comma-Separated Values file

I Type ”python read csv file” in search engine


I Result: https://docs.python.org/2/library/csv.html
Comma-Separated Values file

I Type ”python read csv file” in search engine


I Result: https://docs.python.org/2/library/csv.html
I From the doc:
import c s v
c s v f i l e = open ( ’ buoy−c a n a l d e i b i z a \ SALT SBE37 . c s v ’ , ’ r b ’ )
r e a d e r = csv . r e a d e r ( c s v f i l e )
f o r row i n r e a d e r :
p r i n t row
csvfile . close ()
Comma-Separated Values file

I Type ”python read csv file” in search engine


I Result: https://docs.python.org/2/library/csv.html
I From the doc:
import c s v
c s v f i l e = open ( ’ buoy−c a n a l d e i b i z a \ SALT SBE37 . c s v ’ , ’ r b ’ )
r e a d e r = csv . r e a d e r ( c s v f i l e )
f o r row i n r e a d e r :
p r i n t row
csvfile . close ()

î: 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 Type ”python read cnv file” in search engine


Seabird CTD file

I Type ”python read cnv file” in search engine


I Result: https://pypi.python.org/pypi/cnv
”Sorry for the inconvenience, but I moved all the functionalities into the package seabird”
Seabird CTD file

I Type ”python read cnv file” in search engine


I Result: https://pypi.python.org/pypi/cnv
”Sorry for the inconvenience, but I moved all the functionalities into the package seabird”

I Use pip to search for the package


pip search seabird
seabird − Non o f f i c i a l p a r s e r f o r Sea−B i r d ’ s s e n s o r s .
Seabird CTD file

I Type ”python read cnv file” in search engine


I Result: https://pypi.python.org/pypi/cnv
”Sorry for the inconvenience, but I moved all the functionalities into the package seabird”

I Use pip to search for the package


pip search seabird
seabird − Non o f f i c i a l p a r s e r f o r Sea−B i r d ’ s s e n s o r s .

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 Type ”python read cnv file” in search engine


I Result: https://pypi.python.org/pypi/cnv
”Sorry for the inconvenience, but I moved all the functionalities into the package seabird”

I Use pip to search for the package


pip search seabird
seabird − Non o f f i c i a l p a r s e r f o r Sea−B i r d ’ s s e n s o r s .

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 From the doc:


from s e a b i r d . cnv import fCNV
p r o f i l e = fCNV ( ’ ’ )
profile . attributes # I t w i l l r e t u r n the header , as a d i c t i o n a r y .
p r o f i l e . keys ( ) # I t will l i s t the available variables .
p r o f i l e [ ’TEMP ’ ] # Get t h e d a t a
Seabird CTD file

I Type ”python read cnv file” in search engine


I Result: https://pypi.python.org/pypi/cnv
”Sorry for the inconvenience, but I moved all the functionalities into the package seabird”

I Use pip to search for the package


pip search seabird
seabird − Non o f f i c i a l p a r s e r f o r Sea−B i r d ’ s s e n s o r s .

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 From the doc:


from s e a b i r d . cnv import fCNV
p r o f i l e = fCNV ( ’ ’ )
profile . attributes # I t w i l l r e t u r n the header , as a d i c t i o n a r y .
p r o f i l e . keys ( ) # I t will l i s t the available variables .
p r o f i l e [ ’TEMP ’ ] # Get t h e d a t a

î: 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

Click and download the .zip file or . . .


How to get code from github
https://github.com/ctroupin/PythonCourseCadiz2016

Click and download the .zip file or . . .


Copy the URL and type git clone url in a terminal
Update the code

1. Clone the repository


g i t c l o n e h t t p s : / / g i t h u b . com / . . . . git
Update the code

1. Clone the repository


g i t c l o n e h t t p s : / / g i t h u b . com / . . . . git

2. ¨ (if there were modifications in the repository)


Update the code

1. Clone the repository


g i t c l o n e h t t p s : / / g i t h u b . com / . . . . git

2. ¨ (if there were modifications in the repository)

3. Update your version


git pull
Update the code

1. Clone the repository


g i t c l o n e h t t p s : / / g i t h u b . com / . . . . git

2. ¨ (if there were modifications in the repository)

3. Update your version


git pull

More about github:


I https://github.com/
I http://rogerdudler.github.io/git-guide/
NetCDF

Definition: software libraries and self-describing,


machine-independent data formats (source:
http://www.unidata.ucar.edu/software/netcdf/).
NetCDF

Definition: software libraries and self-describing,


machine-independent data formats (source:
http://www.unidata.ucar.edu/software/netcdf/).
Users: atmospheric/ocean observing/modelling communities:
NOAA, EUMETSAT, NASA/JPL, USGS, Copernicus
Marine Service, . . .
NetCDF

Definition: software libraries and self-describing,


machine-independent data formats (source:
http://www.unidata.ucar.edu/software/netcdf/).
Users: atmospheric/ocean observing/modelling communities:
NOAA, EUMETSAT, NASA/JPL, USGS, Copernicus
Marine Service, . . .
Tools: many tools and libraries to inspect, visualise and
explore data sets.
NetCDF

Definition: software libraries and self-describing,


machine-independent data formats (source:
http://www.unidata.ucar.edu/software/netcdf/).
Users: atmospheric/ocean observing/modelling communities:
NOAA, EUMETSAT, NASA/JPL, USGS, Copernicus
Marine Service, . . .
Tools: many tools and libraries to inspect, visualise and
explore data sets.
Structure: I header: dimensions, attributes, variables
I data: arrays
Quick inspection: ncdump

 https://www.unidata.ucar.edu/software/netcdf/docs/netcdf/ncdump.html

A text representation of a netCDF dataset (header


information, variables, . . . )

ncdump applied on a file


ncdump −h 20140628 d−OC CNR−L3−CHL−MedOC3 A 1KM−MED−DT−v02 . nc

n e t c d f \20140628 d−OC CNR−L3−CHL−MedOC3 A 1KM−MED−DT−v02 {


dimensions :
time = 1 ;
l a t = 1580 ;
l o n = 3308 ;
variables :
i n t time ( time ) ;
time : long name = ” r e f e r e n c e time ” ;
time : standard name = ” time ” ;
t i m e : a x i s = ”T” ;
time : calendar = ” Gregorian ” ;
t i m e : u n i t s = ” s e c o n d s s i n c e 1981−01−01 0 0 : 0 0 : 0 0 ” ;
...
”SUBSAMP=1\n ” ,
”OUTMODE=0\n ” ,
”” ;
}
Ferret

 http://www.ferret.noaa.gov/Ferret/

A visualization and analysis environment

Ferret to get basic info on file


ctroupin@SCBD046 ˜ / D e s k t o p $ f e r r e t c
NOAA/ PMEL TMAP
FERRET v6 . 6 2
L i n u x ( g f o r t r a n ) 2 . 6 . 9 − 8 9 . 0 . 2 0 . ELsmp − 0 7 / 0 6 / 1 3
25−Nov−15 1 2 : 2 3

y e s ? SET DATA 20140628 d−OC CNR−L3−CHL−MedOC3 A 1KM−MED−DT−v02 . nc


y e s ? SHOW DATA
c u r r e n t l y SET d a t a s e t s :
1> 20140628 d−OC CNR−L3−CHL−MedOC3 A 1KM−MED−DT−v02 . nc ( d e f a u l t )
name title I J K L
CHL M e d i t e r r a n e a n Sea D a i l y C h l o r o p 1 : 3 3 0 8 1:1580 ... 1:1
QI Quality Index of Mediterranean 1:3308 1:1580 ... 1:1

yes ?
ncview

 http://meteora.ucsd.edu/∼pierce/ncview home page.html

A quick visualisation of 3-4D fields


ncbrowse

 http://www.epic.noaa.gov/java/ncBrowse/

A interactive graphical display


Panoply

 http://www.giss.nasa.gov/tools/panoply/

A plot, slice, combine, overlay, . . .


cdo – Climate Data Operators

 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

I Compute standard deviation:


cdo f l d s t d i n p u t . nc o u t p u t . nc
NCO – netCDF Operators

 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

High-level functions to read/write data from/to a netCDF file:


I http://octave.sourceforge.net/netcdf/overview.html
I http://es.mathworks.com/help/matlab/network-common-data-form.html

Example with Octave


nc = n e t c d f ( ’ i n p u t . nc ’ , ’ r ’ ) ; % open n e t c d f f i l e i n r e a d−o n l y
CHL = nc{ ’CHL ’ } ( : ) ; % retrieve variable
C H L u n i t s = nc{ ’CHL ’ } . u n i t s ; % r e t r i e v e the a t t r i b u t e units
C H L v a l i d r a n g e = nc{ ’CHL ’ } . v a l i d r a n g e ; % r e t r i e v e the a t t r i b u t e valid range
g l o b a l h i s t o r y = nc . h i s t o r y ; % r e t r i e v e the global a t t r i b u t e history
Python

Python interface to the netCDF C library:


http://unidata.github.io/netcdf4-python/
Example: file
dep0001 station-santantoni scb-wlog001 L1 2016-01.nc

Example with ipython


import netCDF4
nc = netCDF4 . D a t a s e t ( ’ d e p 0 0 0 1 s t a t i o n −s a n t a n t o n i s c b −wlog001 L1 2016 −01. nc ’ )
p r i n t nc
<t y p e ’ netCDF4 . netCDF4 . D a t a s e t ’>
r o o t g r o u p ( NETCDF3 CLASSIC d a t a model , f i l e format UNDEFINED ) :
t i t l e : D a t a from i n s t r u m e n t SCB−WLOG001 on p l a t f o r m S t a t i o n S a n t A n t o n i
i n s t i t u t i o n : SOCIB ( S i s t e m a de O b s e r v a c i o n y p r e d i c c i o n C o s t e r o de l a s I s l a s B a l e a r e s )
netcdf version : 3.0
C o n v e n t i o n s : CF−1.6
a b s t r a c t : Deployment o f i n s t r u m e n t SCB−WLOG001 a t S a n t A n t o n i s t a t i o n i n e n d u r a n c e l i n e

...
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

read netcdf local.ipynb: local netCDF file


read netcdf opendap.ipynb: netCDF on thredds data server
read netcdf cf.ipynb: netCDF using CF conventions
read shapefile.ipynb: geospatial vector data

read geotiff.ipynb: geotiff image


read image.ipynb: jpg image

read HDF file.ipynb: Hierarchical Data Format


read mat file.ipynb: .mat file
Exercise 1: data reading

Objective: read unknown file format

: 1-read_CMEMS_indexfile.ipynb
Programming in
Python #2
time series, scatter plots, . . .
Useful packages

NumPy: package for scientific computing


http://www.numpy.org/
SciPy: open-source software for mathematics, science, and
engineering http://www.scipy.org/
Useful packages

NumPy: package for scientific computing


http://www.numpy.org/
SciPy: open-source software for mathematics, science, and
engineering http://www.scipy.org/
matplotlib: 2D plotting library which produces publication quality
figures http://matplotlib.org/
List of notebooks

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

oceanography.mplstyle: file to define figure style


Exercise 2: mooring time series

Objective: plot time series of temperature and salinity

: 2-plot_ibiza_temperature_salinity.ipynb
Programming in
Python #3
2D plots and plots on map
Useful packages

NumPy: package for scientific computing


http://www.numpy.org/
SciPy: open-source software for mathematics, science, and
engineering http://www.scipy.org/
Useful packages

NumPy: package for scientific computing


http://www.numpy.org/
SciPy: open-source software for mathematics, science, and
engineering http://www.scipy.org/
matplotlib: 2D plotting library which produces publication quality
figures http://matplotlib.org/
Useful packages

NumPy: package for scientific computing


http://www.numpy.org/
SciPy: open-source software for mathematics, science, and
engineering http://www.scipy.org/
matplotlib: 2D plotting library which produces publication quality
figures http://matplotlib.org/
Basemap toolkit : library for plotting 2D data on maps in Python
http://matplotlib.org/basemap/
Useful packages

NumPy: package for scientific computing


http://www.numpy.org/
SciPy: open-source software for mathematics, science, and
engineering http://www.scipy.org/
matplotlib: 2D plotting library which produces publication quality
figures http://matplotlib.org/
Basemap toolkit : library for plotting 2D data on maps in Python
http://matplotlib.org/basemap/

Bokeh: interactive visualization library


(http://bokeh.pydata.org/en/latest/)
List of notebooks

Located in Plotting
plot2D contours pcolor.ipynb: plot 2D fields with contour or pseudo-color techniques

plot2D map field.ipynb: plot 2D field on a map


plot2D map vector.ipynb: plot vector field on a map
plot2D map scatter.ipynb: scatter plot on a map
Exercise 3: 2D plot

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

You might also like