guy griffiths. general purpose interpreted programming language widely used by scientists and...
TRANSCRIPT
![Page 1: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/1.jpg)
Guy Griffiths
![Page 2: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/2.jpg)
General purpose interpreted programming language
Widely used by scientists and programmers of all stripes
Supported by many 3rd-party libraries (currently 21,054 on the main python package website)
Free!
![Page 3: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/3.jpg)
Standardisation of programming language to teach to students
The Met Office is moving towards Python
Big user communityPublication-quality plots
![Page 4: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/4.jpg)
An integrated graphical environment like Matlab (although there are tools which put it in one – e.g. Spyder)
Specifically designed for scientists/mathematicians (but the 3rd-party libraries for plotting/numerical work are some of the best around)
High performance (but it is very easy to wrap C/Fortran libraries in Python code)
![Page 5: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/5.jpg)
Met OfficeYahoo Maps/GroupsGoogleNASAESRIYouTubeLinux distros reddit
![Page 6: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/6.jpg)
The best way to understand syntax is to look at some examples
![Page 7: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/7.jpg)
Matlab Python
Indexing starts at 1 Indexing starts at 0
Spaces aren’t very important Spaces indicate loops and blocks
1 externally-visible function per file
Functions can be defined anywhere
Result of each line output by default, suppressed by ;
No output unless specifically asked for
Comes with an IDE (but can be run without one)
Doesn’t come with an IDE (but several are available)
Functions are globally present if they’re in the path
Most functions must be imported before being used
Namespaces are awkward and rarely used
Namespaces are inherent
![Page 8: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/8.jpg)
Numpy Numerical library for python Written in C, wrapped by python Fast
Scipy Built on top of numpy and BLAS/LAPACK (i.e.
fast) Common maths, science, engineering routines
Matplotlib Hugely flexible plotting library Similar syntax to Matlab Produces publication-quality output
![Page 9: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/9.jpg)
Numpy arrays behave slightly differently to Python lists They cannot hold mixed data types But they’re a lot faster than lists For numerical work, always use Numpy
arrays Convert a list to an array with np.array(list)
Numpy functions all return arrays, so often nothing specific needs doing
![Page 10: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/10.jpg)
Matplotlib has very similar syntax to Matlab
Lots of examples: http://matplotlib.org/gallery.html http://matplotlib.org/basemap/users/
examples.htmlUsing documentation and examples
makes it easy to do almost any plot you could want
![Page 11: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/11.jpg)
NetCDF Use python-netcdf
CSV np.recfromcsv()
GRIB Use python-grib, python-grib2, or cf-python
PP cf-python
Matlab .mat scipy.io.loadmat(‘filename.mat’)
Others If it’s a common format, someone will probably
have written an adapter If it’s text based, use np.genfromtxt()
![Page 12: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/12.jpg)
Spyder is most Matlab-like Contains inline help, variable inspector,
interactive console & editor IPython is powerful console-based
interpreter Not an IDE, but highly recommended for
experimenting with prior to actual scripting Eclipse + Pydev make a very powerful
Python IDE Quite heavyweight Good for very large projects, probably overkill
otherwise
![Page 13: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/13.jpg)
Online HTML documentation is generated from
code comments In console:
help(np.array) In IPython console:
np.array? np.array()?
In Spyder: Start typing, and function help appears in
the help window
![Page 14: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/14.jpg)
Let’s put all that into action with an example: Reading from a NetCDF file and creating
a plot of mean and standard deviation
![Page 15: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/15.jpg)
Firstly, get version 2.7.x. Python 3 will work but numerical libraries are less widely supported.
Windows – Python(x,y) [www.pythonxy.com]This is a scientific/engineering oriented distribution of python. It includes everything you need to get started
Linux – it’s already there! Unless you’re running a very unusual distro (in which case you probably already know what you’re doing).
Mac – it’s already there on OS X, but it’s old. Get a more up-to-date one [www.python.org]
![Page 16: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/16.jpg)
The official python tutorial:http://docs.python.org/tutorial/
Software Carpentry:http://software-carpentry.org/
Dive into Python:http://www.diveintopython.net/
Learn Python the Hard Way:http://learnpythonthehardway.org/
A Byte of Python:http://www.ibiblio.org/g2swap/byteofpython/read/
![Page 17: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/17.jpg)
http://www.scipy.org/NumPy_for_Matlab_Users This is the most useful Matlab -> Python
I’ve come across. Contains key differences, things to note,
and a big list of examples in both Matlab and Python
![Page 18: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/18.jpg)
Python Essential Reference David M. Beazley (Addison Wesley)
Programming in Python 3: A Complete Introduction to the Python LanguageMark Summerfield (Addison Wesley)
Learning PythonMark Lutz (O’Reilly Media)
![Page 19: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/19.jpg)
Go away and try it! Convert existing Matlab code (easy) Convert existing Fortran code (harder) Experiment with something new
Then come back in 3 weeks’ time for a workshop, bringing any questions/problems No planned lecture Will go through common problems people
have Join the met-python mailing list
![Page 20: Guy Griffiths. General purpose interpreted programming language Widely used by scientists and programmers of all stripes Supported by many 3 rd -party](https://reader035.vdocuments.mx/reader035/viewer/2022062417/5515f8d9550346a2308b4852/html5/thumbnails/20.jpg)
Thanks for listening