{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Using masks and computing weighted average" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This example is based from xarray example http://xarray.pydata.org/en/stable/examples/area_weighted_temperature.html" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import python packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: nc-time-axis in /opt/conda/lib/python3.8/site-packages (1.4.1)\n", "Requirement already satisfied: matplotlib in /opt/conda/lib/python3.8/site-packages (from nc-time-axis) (3.5.1)\n", "Requirement already satisfied: cftime>=1.5 in /opt/conda/lib/python3.8/site-packages (from nc-time-axis) (1.6.0)\n", "Requirement already satisfied: numpy in /opt/conda/lib/python3.8/site-packages (from nc-time-axis) (1.19.5)\n", "Requirement already satisfied: cycler>=0.10 in /opt/conda/lib/python3.8/site-packages (from matplotlib->nc-time-axis) (0.10.0)\n", "Requirement already satisfied: pyparsing>=2.2.1 in /opt/conda/lib/python3.8/site-packages (from matplotlib->nc-time-axis) (2.4.7)\n", "Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/lib/python3.8/site-packages (from matplotlib->nc-time-axis) (2.8.1)\n", "Requirement already satisfied: fonttools>=4.22.0 in /opt/conda/lib/python3.8/site-packages (from matplotlib->nc-time-axis) (4.30.0)\n", "Requirement already satisfied: pillow>=6.2.0 in /opt/conda/lib/python3.8/site-packages (from matplotlib->nc-time-axis) (8.1.2)\n", "Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.8/site-packages (from matplotlib->nc-time-axis) (20.9)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /opt/conda/lib/python3.8/site-packages (from matplotlib->nc-time-axis) (1.3.1)\n", "Requirement already satisfied: six in /opt/conda/lib/python3.8/site-packages (from cycler>=0.10->matplotlib->nc-time-axis) (1.15.0)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "pip install nc-time-axis" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import xarray as xr\n", "xr.set_options(display_style='html')\n", "import intake\n", "import cftime\n", "import matplotlib.pyplot as plt\n", "import cartopy.crs as ccrs\n", "import numpy as np\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
pangeo-cmip6 catalog with 7686 dataset(s) from 514961 asset(s):
| \n", " | unique | \n", "
|---|---|
| activity_id | \n", "18 | \n", "
| institution_id | \n", "36 | \n", "
| source_id | \n", "88 | \n", "
| experiment_id | \n", "170 | \n", "
| member_id | \n", "657 | \n", "
| table_id | \n", "37 | \n", "
| variable_id | \n", "700 | \n", "
| grid_label | \n", "10 | \n", "
| zstore | \n", "514961 | \n", "
| dcpp_init_year | \n", "60 | \n", "
| version | \n", "737 | \n", "
| \n", " | activity_id | \n", "institution_id | \n", "source_id | \n", "experiment_id | \n", "member_id | \n", "table_id | \n", "variable_id | \n", "grid_label | \n", "zstore | \n", "dcpp_init_year | \n", "version | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "CMIP | \n", "NCC | \n", "NorESM2-LM | \n", "historical | \n", "r1i1p1f1 | \n", "Amon | \n", "tas | \n", "gn | \n", "gs://cmip6/CMIP6/CMIP/NCC/NorESM2-LM/historica... | \n", "NaN | \n", "20190815 | \n", "
<xarray.Dataset>\n",
"Dimensions: (lat: 96, bnds: 2, lon: 144, member_id: 1, time: 1980)\n",
"Coordinates:\n",
" height float64 ...\n",
" * lat (lat) float64 -90.0 -88.11 -86.21 -84.32 ... 86.21 88.11 90.0\n",
" lat_bnds (lat, bnds) float64 dask.array<chunksize=(96, 2), meta=np.ndarray>\n",
" * lon (lon) float64 0.0 2.5 5.0 7.5 10.0 ... 350.0 352.5 355.0 357.5\n",
" lon_bnds (lon, bnds) float64 dask.array<chunksize=(144, 2), meta=np.ndarray>\n",
" * time (time) object 1850-01-16 12:00:00 ... 2014-12-16 12:00:00\n",
" time_bnds (time, bnds) object dask.array<chunksize=(1980, 2), meta=np.ndarray>\n",
" * member_id (member_id) <U8 'r1i1p1f1'\n",
"Dimensions without coordinates: bnds\n",
"Data variables:\n",
" tas (member_id, time, lat, lon) float32 dask.array<chunksize=(1, 990, 96, 144), meta=np.ndarray>\n",
"Attributes: (12/54)\n",
" Conventions: CF-1.7 CMIP-6.2\n",
" activity_id: CMIP\n",
" branch_method: Hybrid-restart from year 1600-01-01 of piControl\n",
" branch_time: 0.0\n",
" branch_time_in_child: 0.0\n",
" branch_time_in_parent: 430335.0\n",
" ... ...\n",
" variable_id: tas\n",
" variant_label: r1i1p1f1\n",
" netcdf_tracking_ids: hdl:21.14100/2486cf87-033c-4848-ab3e-e828c3b7c...\n",
" version_id: v20190815\n",
" intake_esm_varname: ['tas']\n",
" intake_esm_dataset_key: CMIP.NCC.NorESM2-LM.historical.Amon.gn