{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Subset CMIP6 Datasets with xarray\n", "\n", "xarray: http://xarray.pydata.org/en/stable/index.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Search CMIP6 Dataset with ESGF pyclient\n", "\n", "using: https://esgf-pyclient.readthedocs.io/en/latest/index.html" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pyesgf.search import SearchConnection\n", "conn = SearchConnection('https://esgf-data.dkrz.de/esg-search', distrib=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ctx = conn.new_context(\n", " project='CMIP6', \n", " source_id='UKESM1-0-LL', \n", " experiment_id='historical', \n", " variable='tas', \n", " frequency='mon', \n", " variant_label='r1i1p1f2',\n", " data_node='esgf-data3.ceda.ac.uk')\n", "ctx.hit_count" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = ctx.search()[0]\n", "result.dataset_id" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "files = result.file_context().search()\n", "for file in files:\n", " print(file.opendap_url)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Subset single dataset with xarray\n", "\n", "Using OpenDAP: http://xarray.pydata.org/en/stable/io.html?highlight=opendap#opendap" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "ds = xr.open_dataset(files[0].opendap_url, chunks={'time': 120})\n", "print(ds)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da = ds['tas']\n", "da = da.isel(time=slice(0, 1))\n", "da = da.sel(lat=slice(-50, 50), lon=slice(0, 50))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "da.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Subset over multiple datasets\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds_agg = xr.open_mfdataset([files[0].opendap_url, files[1].opendap_url], chunks={'time': 120}, combine='nested', concat_dim='time')\n", "print(ds_agg)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da = ds_agg['tas']\n", "da = da.isel(time=slice(1200, 1201))\n", "da = da.sel(lat=slice(-50, 50), lon=slice(0, 50))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da.to_netcdf('tas_africa_19500116.nc')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.3" } }, "nbformat": 4, "nbformat_minor": 4 }