Nikolay Koldunov
koldunovn@gmail.com
This is part of Python for Geosciences notes.
=============
Some of the things will not work on ZMAW computers (Iris, Cartopy).
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
Cartopy is a library providing cartographic tools for python.
Some of the key features of cartopy are:
Simple plot:
fig, ax = make_cartopy(projection=ccrs.Robinson(), resolution='110m')
_ = ax.plot(lon, lat, transform=ccrs.Geodetic(), **kw)
import cartopy.crs as ccrs
import cartopy.feature as cfeature
plt.figure(figsize=(8,7))
lat = 53.565278
lon = 10.001389
ax = plt.axes(projection=ccrs.Mercator())
ax.set_extent([-20.,40.,32.,64.])
ax.coastlines(resolution='50m')
ax.scatter(lon,lat,300,transform=ccrs.Geodetic())
<matplotlib.collections.PathCollection at 0x119982b00>
plt.figure(figsize=(8,7))
ax = plt.axes(projection=ccrs.Mercator())
ax.set_extent([-20.,40.,32.,64.])
ax.coastlines(resolution='50m',zorder=9)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.LAND)
#ax.add_feature(cfeature.COASTLINE)
ax.add_feature(cfeature.BORDERS, linestyle=':')
ax.scatter(lon,lat,300,transform=ccrs.Geodetic(),zorder=10)
<matplotlib.collections.PathCollection at 0x119c6aa90>
from cartopy.io.img_tiles import GoogleTiles, OSM,QuadtreeTiles,MapQuestOpenAerial,MapQuestOSM
plt.figure(figsize=(8,7))
ax = plt.axes(projection=ccrs.Mercator())
ax.set_extent([-20.,40.,32.,64.])
ax.coastlines(resolution='50m',zorder=9)
ax.scatter(lon,lat,30,transform=ccrs.PlateCarree(),zorder=10)
gg_tiles = GoogleTiles()
ax.add_image(gg_tiles, 3)
plt.figure(figsize=(8,7))
ax = plt.axes(projection=ccrs.Mercator())
ax.set_extent([9.95,10.05,53.55,53.6])
ax.scatter(lon,lat,500,transform=ccrs.Geodetic(),zorder=10)
gg_tiles = OSM()
ax.add_image(gg_tiles, 12)
from netCDF4 import Dataset
fl = Dataset('./air.2m.gauss.2016.nc')
air = fl.variables['air'][0,:,:]
lat = fl.variables['lat'][:]
lon = fl.variables['lon'][:]
from cartopy.util import add_cyclic_point
air_cyc, lon_cyc = add_cyclic_point(air, coord=lon)
plt.figure(figsize=(10,5))
ax = plt.axes(projection=ccrs.Robinson(central_longitude=120))
ax.coastlines()
gl = ax.gridlines(draw_labels=False) # Only PlateCarree and Mercator plots are currently supported.
ax.contourf(lon_cyc,lat,air_cyc, transform=ccrs.PlateCarree())
<matplotlib.contour.QuadContourSet at 0x11b95ab70>
Pydap is a pure Python library implementing the Data Access Protocol, also known as DODS or OPeNDAP. You can use Pydap as a client to access hundreds of scientific datasets in a transparent and efficient way through the internet; or as a server to easily distribute your data from a variety of formats.
from pydap.client import open_url
We are going to access sea ice data from CliSAP-Integrated Climate Data Center (ICDC)
dataset = open_url(u"http://icdc.cen.uni-hamburg.de/thredds/dodsC/ssmi_asi_icecon_arc2011")
print(dataset)
<DatasetType with children 'latitude', 'longitude', 'time', 'sea_ice_area_fraction', 'land'>
ice = dataset['sea_ice_area_fraction']
ice.shape
(365, 896, 608)
ice.attributes
{'_FillValue': 103, 'add_offset': 0.0, 'coordinates': 'latitude longitude time', 'long_name': 'sea-ice concentration', 'missing_value': 101, 'scale_factor': 1.0, 'standard_name': 'sea_ice_area_fraction', 'units': 'percent'}
plt.imshow(np.squeeze(ice[0,:,:]))
plt.colorbar()
<matplotlib.colorbar.Colorbar at 0x11b12bef0>
The F2PY project is created to unify the efforts of supporting easy connection between Fortran and Python languages. Example below is from Using Python and FORTRAN with F2py.
Create FORTRAN file (use %%file instead of %%writefile if you on IPython < 1.0):
%%writefile FIB1.F
C FILE: FIB1.F
SUBROUTINE FIB(A,N)
C
C CALCULATE FIRST N FIBONACCI NUMBERS
C
INTEGER N
REAL*8 A(N)
DO I=1,N
IF (I.EQ.1) THEN
A(I) = 0.0D0
ELSEIF (I.EQ.2) THEN
A(I) = 1.0D0
ELSE
A(I) = A(I-1) + A(I-2)
ENDIF
ENDDO
END
C END FILE FIB1.F
Writing FIB1.F
Compile it with f2py:
!f2py -c -m --fcompiler=gnu95 fib1 FIB1.F
running build running config_cc unifing config_cc, config, build_clib, build_ext, build commands --compiler options running config_fc unifing config_fc, config, build_clib, build_ext, build commands --fcompiler options running build_src build_src building extension "fib1" sources f2py options: [] f2py:> /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fib1module.c creating /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6 Reading fortran codes... Reading file 'FIB1.F' (format:fix,strict) Post-processing... Block: fib1 Block: fib Post-processing (stage 2)... Building modules... Building module "fib1"... Constructing wrapper function "fib"... fib(a,[n]) Wrote C/API module "fib1" to file "/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fib1module.c" adding '/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fortranobject.c' to sources. adding '/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6' to include_dirs. copying /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/f2py/src/fortranobject.c -> /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6 copying /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/f2py/src/fortranobject.h -> /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6 build_src: building npy-pkg config files running build_ext customize UnixCCompiler customize UnixCCompiler using build_ext customize Gnu95FCompiler Found executable /usr/local/bin/gfortran customize Gnu95FCompiler using build_ext building 'fib1' extension compiling C sources C compiler: gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/koldunovn/miniconda3/include -arch x86_64 -I/Users/koldunovn/miniconda3/include -arch x86_64 creating /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var creating /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var/folders creating /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var/folders/yq creating /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn creating /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T creating /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk creating /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6 compile options: '-I/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6 -I/Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include -I/Users/koldunovn/miniconda3/include/python3.6m -c' gcc: /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fib1module.c In file included from /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fib1module.c:19: In file included from /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fortranobject.h:13: In file included from /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include/numpy/arrayobject.h:4: In file included from /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include/numpy/ndarrayobject.h:18: In file included from /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788: /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings] #warning "Using deprecated NumPy API, disable it by " \ ^ /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fib1module.c:112:12: warning: unused function 'f2py_size' [-Wunused-function] static int f2py_size(PyArrayObject* var, ...) ^ 2 warnings generated. gcc: /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fortranobject.c In file included from /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fortranobject.c:2: In file included from /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fortranobject.h:13: In file included from /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include/numpy/arrayobject.h:4: In file included from /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include/numpy/ndarrayobject.h:18: In file included from /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include/numpy/ndarraytypes.h:1788: /Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:15:2: warning: "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-W#warnings] #warning "Using deprecated NumPy API, disable it by " \ ^ /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fortranobject.c:139:18: warning: comparison of integers of different signs: 'Py_ssize_t' (aka 'long') and 'unsigned long' [-Wsign-compare] if (size < sizeof(notalloc)) { ~~~~ ^ ~~~~~~~~~~~~~~~~ 2 warnings generated. compiling Fortran sources Fortran f77 compiler: /usr/local/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -arch x86_64 -fPIC -O3 -funroll-loops Fortran f90 compiler: /usr/local/bin/gfortran -Wall -g -fno-second-underscore -arch x86_64 -fPIC -O3 -funroll-loops Fortran fix compiler: /usr/local/bin/gfortran -Wall -g -ffixed-form -fno-second-underscore -Wall -g -fno-second-underscore -arch x86_64 -fPIC -O3 -funroll-loops compile options: '-I/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6 -I/Users/koldunovn/miniconda3/lib/python3.6/site-packages/numpy/core/include -I/Users/koldunovn/miniconda3/include/python3.6m -c' gfortran:f77: FIB1.F /usr/local/bin/gfortran -Wall -g -arch x86_64 -Wall -g -undefined dynamic_lookup -bundle /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fib1module.o /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/src.macosx-10.7-x86_64-3.6/fortranobject.o /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk/FIB1.o -L/usr/local/Cellar/gcc/7.1.0/lib/gcc/7/gcc/x86_64-apple-darwin16.5.0/7.1.0 -L/Users/koldunovn/miniconda3/lib -lgfortran -o ./fib1.cpython-36m-darwin.so Removing build directory /var/folders/yq/03v059jn4sg2xhnhpnrqkgcc0000gn/T/tmprf2kbptk
Import resulting fib1.so as python library:
import fib1
Read some auto generated documentation:
print(fib1.__doc__)
print(fib1.fib.__doc__)
This module 'fib1' is auto-generated with f2py (version:2). Functions: fib(a,n=len(a)) . fib(a,[n]) Wrapper for ``fib``. Parameters ---------- a : input rank-1 array('d') with bounds (n) Other Parameters ---------------- n : input int, optional Default: len(a)
Use fib function:
import numpy as np
a=np.zeros(15,'d')
fib1.fib(a)
a
array([ 0., 1., 1., 2., 3., 5., 8., 13., 21., 34., 55., 89., 144., 233., 377.])