other:python:jyp_steps
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
other:python:jyp_steps [2016/01/20 11:58] – jypeter | other:python:jyp_steps [2016/01/22 18:02] – More changes jypeter | ||
---|---|---|---|
Line 4: | Line 4: | ||
This page tries to list some //python for the scientist// related resources, in a suggested reading order. **Do not print anything** (or at least not everything), | This page tries to list some //python for the scientist// related resources, in a suggested reading order. **Do not print anything** (or at least not everything), | ||
+ | |||
+ | ===== JYP's introduction to python ===== | ||
+ | |||
+ | ==== Part 1 ==== | ||
+ | |||
+ | You can start using python by reading the {{: | ||
+ | * this tutorial is in French (my apologies for the lack of translation, | ||
+ | * it's an introduction to python (and programming) for the climate scientist: after reading this tutorial, you should be able to do most of the things you usually do in a shell script | ||
+ | * python types, tests, loops, reading a text file | ||
+ | * the tutorial is very detailed about string handling, because strings offer an easy way to practice working with indices (indexing and slicing), before indexing numpy arrays. And our usual pre/ | ||
+ | * after reading this tutorial, you should practice with the following: | ||
+ | * [[https:// | ||
+ | * {{: | ||
+ | * {{: | ||
+ | |||
+ | ==== Part 2 ==== | ||
+ | |||
+ | Once you have done your first steps, you should read [[http:// | ||
+ | * this tutorial is in French (sorry again) | ||
+ | * after reading this tutorial, you will be able to do more than you can do in a shell script, in an easier way | ||
+ | * advanced string formatting | ||
+ | * creating functions and using modules | ||
+ | * working with file paths and handling files without calling external Linux programs\\ (e.g. using '' | ||
+ | * using command-line options for scripts, or using configuration files | ||
+ | * calling external programs | ||
===== The official python documentation ===== | ===== The official python documentation ===== | ||
- | You do not need to read all the python documentation at this step, but it is really well made and you should at least have a look at it. The **Tutorial** is really well made, and you should have a look at the table of content of the **Python Standard Library**. There is a lot in the default library that can make your life easier | + | You do not need to read all the python documentation at this step, but it is really well made and you should at least have a look at it. The **Tutorial** is very good, and you should have a look at the table of content of the **Python Standard Library**. There is a lot in the default library that can make your life easier |
==== Python 2.7 ==== | ==== Python 2.7 ==== | ||
- | [[https:// | + | [[https:// |
==== Python 3 ==== | ==== Python 3 ==== | ||
- | [[https:// | + | [[https:// |
+ | |||
+ | |||
+ | ===== Numpy and Scipy ===== | ||
+ | |||
+ | Summary: Python provides //ordered// objects (e.g. lists, strings, basic arrays, ...) and some math operators, but you can't do real heavy computation with these. **Numpy** makes it possible to work with multi-dimensional data arrays, and using array syntax and masks (instead of explicit nested loops and tests) and the apropriate numpy functions will allow you to get performance similar to what you would get with a compiled program! **Scipy** adds more scientific functions | ||
+ | |||
+ | Where: [[http:// | ||
+ | |||
+ | How to get started? | ||
+ | - always remember that indices start at '' | ||
+ | - read the [[https:// | ||
+ | - have a quick look at the full documentation to know where things are | ||
+ | - Numpy User Guide | ||
+ | - Numpy Reference Guide | ||
+ | - Scipy Reference Guide | ||
+ | |||
+ | ===== cdms2 and netCDF4 ===== | ||
+ | |||
+ | There is a good chance that your input array data will come from a file in the [[http:// | ||
+ | |||
+ | Note: the NetCDF file format is self-documented, | ||
+ | |||
+ | ==== cdms2 ==== | ||
+ | |||
+ | Summary: cdms2 can read/write netCDF files (and read //grads// dat+ctl files) and provides a higher level interface than netCDF4. Unfortunately, | ||
+ | |||
+ | How to get started: | ||
+ | - read [[http:// | ||
+ | - the tutorial is in French (soooorry!) | ||
+ | - you have to replace //cdms// with **cdms2**, and //MV// with **MV2** (sooorry about that, the tutorial was written when CDAT was based on //Numeric// instead of //numpy// to handle array data) | ||
+ | - read the [[http:// | ||
+ | - ask questions and get answers on the [[http:// | ||
+ | |||
+ | |||
+ | ==== netCDF4 ==== | ||
+ | |||
+ | Summary: netCDF4 can read/write netCDF files and is available in most python distributions | ||
+ | |||
+ | Where: [[http:// | ||
+ | |||
+ | |||
+ | ===== Matplotlib ===== | ||
+ | |||
+ | Summary: there are lots of python libraries that you can use for plotting, but Matplotlib has become a //de facto// standard | ||
+ | |||
+ | Where: [[http:// | ||
+ | |||
+ | The documentation is good, but not always easy to use. A good way to start with matplotlib is to: | ||
+ | - Look at the [[http:// | ||
+ | - Use the free hints provided by JY! | ||
+ | - a Matplotlib //Figure// is a graphical window in which you make your plots... | ||
+ | - a Matplotlib //Axis// is a plot inside a Figure... [[http:// | ||
+ | - some examples are more // | ||
+ | - sometimes the results of the python/ | ||
+ | - the documentation may mention [[http:// | ||
+ | - Read the [[http:// | ||
+ | - Download the [[http:// | ||
+ | |||
+ | ===== Basemap ===== | ||
+ | |||
+ | Summary: Basemap is an extension of Matplotlib that you can use for plotting maps, using different projections | ||
+ | |||
+ | Where: [[http:// | ||
+ | |||
+ | How to use basemap? | ||
+ | - look at the [[http:// | ||
+ | - check the [[http:// | ||
+ | - look at the [[http:// | ||
===== Scipy Lecture Notes ===== | ===== Scipy Lecture Notes ===== | ||
Line 23: | Line 117: | ||
Where: [[http:// | Where: [[http:// | ||
- | This is a really nice document that is regularly updated and used for the [[https:// | + | This is a really nice document that is regularly updated and used for the [[https:// |
===== Quick Reference ===== | ===== Quick Reference ===== | ||
- | * The nice Python 2.7 Quick Reference: [[http:// | + | * The nice and convenient |
===== Some good coding tips ===== | ===== Some good coding tips ===== | ||
Line 41: | Line 135: | ||
* **make sure that your script is not using too much memory** (the amount depends on the computer you are using)! Your script should be scalable (e.g. keeps on working even when your data gets bigger), so it's a good idea to load only the data you need in memory (e.g. not all the time steps), and learn how to load chunks of data | * **make sure that your script is not using too much memory** (the amount depends on the computer you are using)! Your script should be scalable (e.g. keeps on working even when your data gets bigger), so it's a good idea to load only the data you need in memory (e.g. not all the time steps), and learn how to load chunks of data | ||
- | * **make sure that you are using array/ | + | * **make sure that you are using array/ |
If your script is still not fast enough, there is a lot you can do to improve it, without resorting to parallelization (that may introduce extra bugs rather that extra performance). See the sections below | If your script is still not fast enough, there is a lot you can do to improve it, without resorting to parallelization (that may introduce extra bugs rather that extra performance). See the sections below |
other/python/jyp_steps.txt · Last modified: 2025/02/26 11:40 by jypeter