{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Subset CMIP5 Datasets with xarray\n", "\n", "xarray: http://xarray.pydata.org/en/stable/index.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Search CMIP5 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='CMIP5', \n", " experiment='rcp45',\n", " model='HadCM3',\n", " ensemble='r1i1p1',\n", " time_frequency='mon',\n", " realm='atmos',\n", " data_node='esgf-data1.ceda.ac.uk',\n", " )\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", " if 'tasmax' in file.opendap_url:\n", " tasmax_url = file.opendap_url\n", " print(tasmax_url)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ESGF Logon" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pyesgf.logon import LogonManager\n", "lm = LogonManager()\n", "lm.logoff()\n", "lm.is_logged_on()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lm.logon(hostname='esgf-data.dkrz.de', interactive=True, bootstrap=True)\n", "lm.is_logged_on()" ] }, { "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(tasmax_url, chunks={'time': 120})\n", "print(ds)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da = ds['tasmax']\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": [ "## Download to NetCDF" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "da.to_netcdf('tasmax.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 }