diff --git a/notebooks/use-case_plot-unstructured_psyplot_cmip6.ipynb b/notebooks/use-case_plot-unstructured_psyplot_cmip6.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..3904b570cf8b2bed96f0e1994834a83bc462bd2c --- /dev/null +++ b/notebooks/use-case_plot-unstructured_psyplot_cmip6.ipynb @@ -0,0 +1,229 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Plot Earth System Model data on unstructured grids with psyplot\n", + "\n", + "This notebook introduces you to the `mapplot` function of the package `psyplot`.\n", + "It is suitable to plot maps from data on unstructured grids like the ones from ICON and FESOM.\n", + "\n", + "We therefore search for the corresponding data in the CMIP6 data pool with intake-esm.\n", + "Afterwards, we open a file with `xarray` and configure the opened xarray dataset as well as psyplot for a map plot.\n", + "\n", + "In the end, we discuss the functions of `psyplot.project.plot.mapplot`." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import psyplot.project as psy\n", + "import matplotlib as mpl\n", + "import xarray as xr\n", + "import intake" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We open a swift catalog from dkrz cloud which is accessible without authentication." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "col_url = \"https://swift.dkrz.de/v1/dkrz_a44962e3ba914c309a7421573a6949a6/intake-esm/mistral-cmip6.json\"\n", + "col = intake.open_esm_datastore(col_url)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this example, we aim at plotting the Sea Surface Temperature of the upper boundary of the liquid ocean, including temperatures below sea-ice and floating ice shelves from AWI.\n", + "We therefore search for `tos` in the catalog for monthly frequency. We use 1 realization of 1 experiment only." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "tos=col.search(source_id=\"AWI-CM-1-1-MR\",\n", + " experiment_id=\"ssp370\",\n", + " variable_id=\"tos\",\n", + " table_id=\"Omon\",\n", + " member_id=\"r1i1p1f1\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We now open the file on the mistral file system." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dset = xr.open_dataset(tos.df[\"path\"].to_list()[0])\n", + "#dset = xr.open_mfdataset(tos.df[\"path\"].to_list())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In order to make `tos` plottable, we set the following configuration.\n", + "- The `CDI_grid_type` is a keyword for `psyplot`.\n", + "- Coordinates are not fully recognized by `xarray` so that we have to add some manually (version from Dec 2020)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dset[\"tos\"][\"CDI_grid_type\"]=\"unstructured\"\n", + "coordlist=[\"vertices_latitude\", \"vertices_longitude\", \"lat_bnds\", \"lon_bnds\"]\n", + "dset=dset.set_coords([coord for coord in dset.data_vars if coord in coordlist])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is based on the example from:\n", + "https://psyplot.readthedocs.io/projects/psy-maps/en/latest/examples/example_ugrid.html#gallery-examples-example-ugrid-ipynb" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "psy.rcParams['plotter.maps.xgrid'] = False\n", + "psy.rcParams['plotter.maps.ygrid'] = False\n", + "mpl.rcParams['figure.figsize'] = [10, 8.]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "iconplot11=psy.plot.mapplot(\n", + " dset, name=\"tos\", cmap='rainbow',\n", + " clabel=dset[\"tos\"].description,\n", + " stock_img=True, lsm='50m')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We now do the same with a smaller subset to see the fine resolution of the AWI ocean model FESOM.\n", + "The subsetting is required because the plotting takes too long otherwise." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dset2 = dset.isel(time=slice(1,3)).where( (dset.lon > -10. ) &\n", + " (dset.lon < 50. ) &\n", + " (dset.lat > 40. ) &\n", + " (dset.lat < 70. ), drop=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dset2.to_netcdf(\"/home/dkrz/k204210/test.nc\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dset2=xr.open_dataset(\"/home/dkrz/k204210/test.nc\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dset2[\"tos\"][\"CDI_grid_type\"]=\"unstructured\"\n", + "coordlist=[\"vertices_latitude\", \"vertices_longitude\", \"lat_bnds\", \"lon_bnds\"]\n", + "dset2=dset2.set_coords([coord for coord in dset2.data_vars if coord in coordlist])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "iconplot12=psy.plot.mapplot(\n", + " dset2, name=\"tos\", cmap='rainbow',\n", + " lonlatbox='Ireland',\n", + " clabel=dset[\"tos\"].description,\n", + " stock_img=True,\n", + " lsm='50m',\n", + " datagrid=dict(c='b', lw=0.2))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 unstable (using the module python3/unstable)", + "language": "python", + "name": "python3_unstable" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.8" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}