{ "cells": [ { "cell_type": "markdown", "id": "db9799be-8baa-4035-b0ef-e861c99f4c70", "metadata": {}, "source": [ "## Computing climate indicators with xclim\n", "\n", "The Climate Impact Lab Downscaled Projections for Climate Impacts Research (CIL-GDPCR) collections contain bias corrected and downscaled 1/4° CMIP6 projections for temperature and precipitation.\n", "\n", "See the project homepage for more information: [github.com/ClimateImpactLab/downscaleCMIP6](https://github.com/ClimateImpactLab/downscaleCMIP6).\n", "\n", "This tutorial covers constructing a time series across the CMIP:historical and ScenarioMIP:ssp126 experiments, and computing transformations using the [xclim](https://xclim.readthedocs.io/) package. Additional tutorials are available at [github.com/microsoft/PlanetaryComputerExamples](https://github.com/microsoft/PlanetaryComputerExamples/blob/main/datasets/cil-gdpcir)." ] }, { "cell_type": "code", "execution_count": 1, "id": "f6c291ba-5b61-41d9-bde9-3e49afddebaf", "metadata": {}, "outputs": [], "source": [ "# required to locate and authenticate with the stac collection\n", "import planetary_computer\n", "import pystac_client\n", "\n", "# required to load a zarr array using xarray\n", "import xarray as xr\n", "\n", "# climate indicators with xclim\n", "import xclim.indicators\n", "\n", "# optional imports used in this notebook\n", "from dask.diagnostics import ProgressBar" ] }, { "cell_type": "markdown", "id": "77d64979-7b72-4433-af1a-3edfe801dc77", "metadata": {}, "source": [ "### Building a joint historical and projection time series\n", "\n", "Let's work with the FGOALS-g3 historical and ssp1-2.6 simulations. We'll use the Planetary Computer's STAC API to search for the items we want, which contain all the information necessary to load the data with xarray.\n", "\n", "The FGOALS-g3 data are available under the `cil-gdpcir-cc0` collection (which you can check in the `cmip6:institution_id` summary of the collection)." ] }, { "cell_type": "code", "execution_count": 6, "id": "998e6893-4ddd-4be1-bf60-fd1139d53c95", "metadata": {}, "outputs": [], "source": [ "catalog = pystac_client.Client.open(\n", " \"https://planetarycomputer.microsoft.com/api/stac/v1\",\n", " modifier=planetary_computer.sign_inplace,\n", ")\n", "collection_cc0 = catalog.get_collection(\"cil-gdpcir-cc0\")\n", "items = catalog.search(\n", " collections=[\"cil-gdpcir-cc0\"],\n", " query={\n", " \"cmip6:source_id\": {\"eq\": \"FGOALS-g3\"},\n", " \"cmip6:experiment_id\": {\"in\": [\"historical\", \"ssp126\"]},\n", " },\n", ").get_all_items()" ] }, { "cell_type": "code", "execution_count": 3, "id": "771b9b39-be59-4e74-84ba-f09b21760043", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['cil-gdpcir-CAS-FGOALS-g3-ssp126-r1i1p1f1-day',\n", " 'cil-gdpcir-CAS-FGOALS-g3-historical-r1i1p1f1-day']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[item.id for item in items]" ] }, { "cell_type": "markdown", "id": "79c51100-9d7a-4cda-8157-fe1a65b00ff1", "metadata": {}, "source": [ "Retrieve object URLs by authenticating with Planetary Computer" ] }, { "cell_type": "code", "execution_count": 4, "id": "f3c8aba0-4365-4fb6-97fa-e3d1deb03011", "metadata": {}, "outputs": [], "source": [ "# use the planetary computer API to sign the asset\n", "signed_items = planetary_computer.sign(items)\n", "\n", "# select this variable ID for all models in the collection\n", "variable_id = \"tasmin\"\n", "\n", "# get the API key and other important keyword arguments\n", "open_kwargs = signed_items[0].assets[variable_id].extra_fields[\"xarray:open_kwargs\"]" ] }, { "cell_type": "markdown", "id": "0b9600a8-0270-41fe-b689-caf17a47254f", "metadata": { "tags": [] }, "source": [ "### Reading a single variable" ] }, { "cell_type": "code", "execution_count": 5, "id": "b4168174-629a-41a7-954d-f14471a7d571", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n", "Dimensions: (lat: 720, lon: 1440, time: 55115)\n", "Coordinates:\n", " * lat (lat) float64 -89.88 -89.62 -89.38 -89.12 ... 89.38 89.62 89.88\n", " * lon (lon) float64 -179.9 -179.6 -179.4 -179.1 ... 179.4 179.6 179.9\n", " * time (time) object 1950-01-01 12:00:00 ... 2100-12-31 12:00:00\n", "Data variables:\n", " tasmin (time, lat, lon) float32 dask.array<chunksize=(365, 360, 360), meta=np.ndarray>\n", "Attributes: (12/40)\n", " Conventions: CF-1.7 CMIP-6.2\n", " contact: climatesci@rhg.com\n", " data_specs_version: 01.00.31\n", " dc6_bias_correction_method: Quantile Delta Method (QDM)\n", " dc6_citation: Please refer to https://github.com/ClimateI...\n", " dc6_creation_date: 2022-01-25\n", " ... ...\n", " source_type: AOGCM\n", " sub_experiment: none\n", " sub_experiment_id: none\n", " table_id: day\n", " variable_id: tasmin\n", " variant_label: r1i1p1f1
<xarray.DataArray 'tasmin' (time: 55115, lat: 720, lon: 1440)>\n", "dask.array<concatenate, shape=(55115, 720, 1440), dtype=float32, chunksize=(365, 360, 360), chunktype=numpy.ndarray>\n", "Coordinates:\n", " * lat (lat) float64 -89.88 -89.62 -89.38 -89.12 ... 89.38 89.62 89.88\n", " * lon (lon) float64 -179.9 -179.6 -179.4 -179.1 ... 179.4 179.6 179.9\n", " * time (time) object 1950-01-01 12:00:00 ... 2100-12-31 12:00:00\n", "Attributes:\n", " cell_measures: area: areacella\n", " cell_methods: area: mean time: minimum (interval: 10 minutes)\n", " comment: minimum near-surface (usually, 2 meter) air temperature (...\n", " coordinates: height\n", " long_name: Daily Minimum Near-Surface Air Temperature\n", " standard_name: air_temperature\n", " units: K
<xarray.DataArray 'frost_days' (time: 151, lat: 720, lon: 1440)>\n", "dask.array<where, shape=(151, 720, 1440), dtype=float64, chunksize=(1, 360, 360), chunktype=numpy.ndarray>\n", "Coordinates:\n", " * time (time) object 1950-01-01 00:00:00 ... 2100-01-01 00:00:00\n", " * lat (lat) float64 -89.88 -89.62 -89.38 -89.12 ... 89.38 89.62 89.88\n", " * lon (lon) float64 -179.9 -179.6 -179.4 -179.1 ... 179.4 179.6 179.9\n", "Attributes:\n", " units: days\n", " cell_methods: area: mean time: minimum (interval: 10 minutes) time: sum...\n", " history: [2022-04-27 01:25:42] frost_days: FROST_DAYS(tasmin=tasmi...\n", " standard_name: days_with_air_temperature_below_threshold\n", " long_name: Number of frost days (tmin < 0 degc)\n", " description: Annual number of days with minimum daily temperature belo...
<xarray.DataArray 'frost_days' (time: 16)>\n", "array([131., 118., 149., 139., 165., 118., 132., 128., 118., 120., 121.,\n", " 127., 100., 122., 118., 106.])\n", "Coordinates:\n", " * time (time) object 1950-01-01 00:00:00 ... 2100-01-01 00:00:00\n", " lat float64 59.88\n", " lon float64 10.88\n", "Attributes:\n", " units: days\n", " cell_methods: area: mean time: minimum (interval: 10 minutes) time: sum...\n", " history: [2022-04-27 01:25:42] frost_days: FROST_DAYS(tasmin=tasmi...\n", " standard_name: days_with_air_temperature_below_threshold\n", " long_name: Number of frost days (tmin < 0 degc)\n", " description: Annual number of days with minimum daily temperature belo...