To run this notebook your python environment needs the following packages:
A suitable environment can be installed using anaconda or mamba via:
mamba create -n py_env_xarray numpy cftime=1.2.1 jupyter xarray matplotlib cartopy netcdf4
source activate py_env_xarray
# importing packages
import numpy as np
import xarray as xr
import cftime
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
# change matplotlib standard parameters
plt.rcParams['figure.figsize'] = 12,8
Loading data from disk. One PISM output file for the Antarctic and one for the Greenland ice sheet.
ds_ais = xr.open_dataset('data/ais_extra.nc')
ds_gris = xr.open_dataset('data/gris_extra.nc')
xarray automatically uses available metadata to label axes, adds a colorbar, etc.
fig, axis = plt.subplots(1,1, figsize=(8,6))
ds_ais['velsurf_mag'].plot(ax=axis)
axis.set_aspect('equal')
The cartopy package allows to plot data on maps and projections, which is useful when visualising spheric coordinates for example. For the Antarctic dataset, we choose a stereographic southpole projection and add a progressive colorbar.
map_proj = ccrs.SouthPolarStereo()
levels = [0, 1, 10, 100, 1000, 2000, 4000]
ax = ds_ais['velsurf_mag'].plot(
transform=ccrs.PlateCarree(),
x='lon', y='lat',
cmap='cividis',
levels=levels,
cbar_kwargs={'ticks': levels},
subplot_kws={'projection': map_proj})
ax.axes.gridlines(draw_labels=True)
<cartopy.mpl.gridliner.Gridliner at 0x7f968466d730>
Cartopy also comes with a predefined set of coastlines which can sometimes be useful to refer the data geographically. We plot here the ice sheet thickness of the Greenland data set.
map_proj = ccrs.Orthographic(central_longitude=-45.0, central_latitude=80.0)
levels = [0, 10, 100, 1000, 1500, 2000, 2500, 3000]
ax = ds_gris['thk'].where(ds_gris['thk']>0,np.nan).plot(
transform=ccrs.PlateCarree(),
x='lon', y='lat',
levels=levels,
cbar_kwargs={'ticks': levels},
subplot_kws={'projection': map_proj}
)
ax.axes.coastlines()
gl = ax.axes.gridlines(draw_labels=True)
gl.bottom_labels = False
gl.right_labels = False