{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Calculate Geospatial Weighted Averages from Monthly Time Series\n", "===============================================================\n", "\n", "Authors:\n", "\n", "* [Tom Vo](https://github.com/tomvothecoder/)\n", "* [Stephen Po-Chedley](https://github.com/pochedls/)\n", "\n", "Date: 05/27/22\n", "\n", "Related APIs:\n", "\n", "* [xarray.Dataset.spatial.average()](../generated/xarray.Dataset.spatial.average.rst)\n", "\n", "The data used in this example can be found through the [Earth System Grid Federation (ESGF) search portal](https://aims2.llnl.gov/metagrid/search)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Overview\n", "\n", "A common data reduction in geophysical sciences is to produce spatial averages. Spatial averaging functionality in xcdat allows users to quickly produce area-weighted spatial averages for selected regions (or full dataset domains).\n", "\n", "In the example below, we demonstrate the opening of a (remote) dataset and spatial averaging over the global, tropical, and Niño 3.4 domains." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2018-11-28T20:51:35.958210Z", "start_time": "2018-11-28T20:51:35.936966Z" } }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import matplotlib.pyplot as plt\n", "import xcdat\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Open the `Dataset`\n", "\n", "We are using xarray's OPeNDAP support to read a netCDF4 dataset file directly from its source. The data is not loaded over the network until we perform operations on it (e.g., temperature unit adjustment).\n", "\n", "More information on the xarray's OPeNDAP support can be found [here](https://docs.xarray.dev/en/stable/user-guide/io.html#opendap)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2018-11-28T20:51:36.072316Z", "start_time": "2018-11-28T20:51:36.016594Z" } }, "outputs": [ { "data": { "text/html": [ "
<xarray.Dataset>\n",
"Dimensions: (time: 1980, bnds: 2, lat: 145, lon: 192)\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 1850-01-16T12:00:00 ... 2014-12-16T12:00:00\n",
" * lat (lat) float64 -90.0 -88.75 -87.5 -86.25 ... 86.25 87.5 88.75 90.0\n",
" * lon (lon) float64 0.0 1.875 3.75 5.625 ... 352.5 354.4 356.2 358.1\n",
" height float64 2.0\n",
"Dimensions without coordinates: bnds\n",
"Data variables:\n",
" time_bnds (time, bnds) datetime64[ns] 1850-01-01 1850-02-01 ... 2015-01-01\n",
" lat_bnds (lat, bnds) float64 -90.0 -89.38 -89.38 ... 89.38 89.38 90.0\n",
" lon_bnds (lon, bnds) float64 -0.9375 0.9375 0.9375 ... 357.2 357.2 359.1\n",
" tas (time, lat, lon) float32 -27.19 -27.19 -27.19 ... -25.29 -25.29\n",
"Attributes: (12/49)\n",
" Conventions: CF-1.7 CMIP-6.2\n",
" activity_id: CMIP\n",
" branch_method: standard\n",
" branch_time_in_child: 0.0\n",
" branch_time_in_parent: 87658.0\n",
" creation_date: 2020-06-05T04:06:11Z\n",
" ... ...\n",
" version: v20200605\n",
" license: CMIP6 model data produced by CSIRO is li...\n",
" cmor_version: 3.4.0\n",
" _NCProperties: version=2,netcdf=4.6.2,hdf5=1.10.5\n",
" tracking_id: hdl:21.14100/af78ae5e-f3a6-4e99-8cfe-5f2...\n",
" DODS_EXTRA.Unlimited_Dimension: time<xarray.DataArray 'tas' (time: 1980)>\n",
"array([12.52127071, 13.09115223, 13.60703132, ..., 15.5767848 ,\n",
" 14.65664621, 13.84951678])\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 1850-01-16T12:00:00 ... 2014-12-16T12:00:00\n",
" height float64 2.0<xarray.DataArray 'tas' (time: 1980)>\n",
"array([25.24722608, 25.61795924, 25.96516235, ..., 26.79536823,\n",
" 26.67771602, 26.27182383])\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 1850-01-16T12:00:00 ... 2014-12-16T12:00:00\n",
" height float64 2.0<xarray.DataArray 'tas' (time: 1980)>\n",
"array([27.00284678, 27.06796429, 26.18095324, ..., 27.17515272,\n",
" 27.30917002, 27.38399379])\n",
"Coordinates:\n",
" * time (time) datetime64[ns] 1850-01-16T12:00:00 ... 2014-12-16T12:00:00\n",
" height float64 2.0