import
mechanism a = [1, 2, 3]
a + a
[1, 2, 3, 1, 2, 3]
a * 4
[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
import numpy as np # central tool for vectorized operations
a = np.array([1, 2, 3])
a
array([1, 2, 3])
a * 4
array([ 4, 8, 12])
from scipy.interpolate import interp1d # scientific "toolbox"
import matplotlib.pyplot as plt # plotting like in matlab
x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(-x**2/9.0)
f1 = interp1d(x, y)
f2 = interp1d(x, y, kind='cubic')
xnew = np.linspace(0, 10, 101, endpoint=True)
plt.plot(x, y, 'o', xnew, f1(xnew), '-', xnew, f2(xnew), '--')
plt.legend(['data', 'linear', 'cubic'], loc='best');
Short answer: use Python 3
Long answer: there is absolutely no reason for a beginner to use Python 2. The Py2/Py3 problem is unfortunate, but Python 3 is better in all aspects. The only reason why people still use Python 2 is when they have a heavy bagage of old (untested) code.
*If you need to use a software which is py2 only, try to contact the author of the package (or use another package).*
On linux/mac, python is installed per default
But ... how to install the packages?
apt-get install numpy
will work, but it will be outdated and the installation is frozenUse the conda package manager, installed via miniconda
Download: https://conda.io/miniconda.html
Installation: https://conda.io/docs/install/quick.html
Tutorial: https://conda.io/docs/test-drive.html
First thing to do after install:
conda config --add channels conda-forge
conda forge is the best source for scientific python packages.
import xarray
xarray.__file__ # tells me where the file is located
'/home/mowglie/.bin/conda/envs/python_demo/lib/python3.5/site-packages/xarray/__init__.py'
pip is the standard (traditional) way to install python packages. It works well for pure python packages, but is limited when it comes to complex packages with non-python dependencies (e.g. NetCDF, GDAL...).
Conda will install binaries, meaning that it will ship with all packages dependencies in the same bundle.
The good news is that pip install my-special-package
will work in a conda environment! This is not true the other way around (pip doesn't know about conda), and in general you shouldn't use pip
yourselves (unless the package you want to install is not on conda).
pandas : working with tabular data
import pandas as pd
ts = pd.read_csv('GLB.Ts+dSST.csv', index_col=0, header=1)['J-D']
ts.plot(label='Annual T')
ts.rolling(window=31, center=True, min_periods=1).mean().plot(label='30-yr avg')
plt.legend(loc='upper left');
xarray: pandas, but for N-Dimensions and netcdf files
import xarray as xr
import cartopy.crs as ccrs
air = xr.tutorial.load_dataset('air_temperature').air
ax = plt.axes(projection=ccrs.Orthographic(-80, 35))
air.isel(time=0).plot.contourf(ax=ax, transform=ccrs.PlateCarree());
ax.set_global(); ax.coastlines();
⇨ Workshop next week!
scipy: all the scientific stuff
matplotlib: plotting à la Matlab
cartopy: plotting on maps
salem: projection transformations and WRF files
statsmodels and scikit-learn: statistical models and machine learning
...
Demo: notebook, first module
"Advanced" IDE: pycharm (free version)
(my personnal choice over spyder)
Questions?
Discussion:
Install python on your laptop: https://github.com/fmaussion/teaching/blob/master/install_python.rst
Install python on the computer room (linux terminal):
$ gedit ~/.bashrc
At the end of this file, add the following two lines:
# added for Fabien's course:
export PATH="/scratch/c707/c7071047/miniconda3/bin:$PATH"
Download the "getting started" notebook:
https://raw.githubusercontent.com/fmaussion/teaching/master/python_intro_acinn/Getting_Started.ipynb (use "save link as")
Start the notebook interface:
$ jupyter-notebook
</small>