|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "toc": true |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", |
| 10 | + "<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#Compare-weighted-and-unweighted-mean-temperature\" data-toc-modified-id=\"Compare-weighted-and-unweighted-mean-temperature-1\"><span class=\"toc-item-num\">1 </span>Compare weighted and unweighted mean temperature</a></span><ul class=\"toc-item\"><li><ul class=\"toc-item\"><li><span><a href=\"#Data\" data-toc-modified-id=\"Data-1.0.1\"><span class=\"toc-item-num\">1.0.1 </span>Data</a></span></li><li><span><a href=\"#Creating-weights\" data-toc-modified-id=\"Creating-weights-1.0.2\"><span class=\"toc-item-num\">1.0.2 </span>Creating weights</a></span></li><li><span><a href=\"#Weighted-mean\" data-toc-modified-id=\"Weighted-mean-1.0.3\"><span class=\"toc-item-num\">1.0.3 </span>Weighted mean</a></span></li><li><span><a href=\"#Plot:-comparison-with-unweighted-mean\" data-toc-modified-id=\"Plot:-comparison-with-unweighted-mean-1.0.4\"><span class=\"toc-item-num\">1.0.4 </span>Plot: comparison with unweighted mean</a></span></li></ul></li></ul></li></ul></div>" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "metadata": {}, |
| 16 | + "source": [ |
| 17 | + "# Compare weighted and unweighted mean temperature\n", |
| 18 | + "\n", |
| 19 | + "\n", |
| 20 | + "Author: [Mathias Hauser](https://github.com/mathause/)\n", |
| 21 | + "\n", |
| 22 | + "\n", |
| 23 | + "We use the `air_temperature` example dataset to calculate the area-weighted temperature over its domain. This dataset has a regular latitude/ longitude grid, thus the gridcell area decreases towards the pole. For this grid we can use the cosine of the latitude as proxy for the grid cell area.\n" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": { |
| 30 | + "ExecuteTime": { |
| 31 | + "end_time": "2020-03-17T14:43:57.222351Z", |
| 32 | + "start_time": "2020-03-17T14:43:56.147541Z" |
| 33 | + } |
| 34 | + }, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "%matplotlib inline\n", |
| 38 | + "\n", |
| 39 | + "import cartopy.crs as ccrs\n", |
| 40 | + "import matplotlib.pyplot as plt\n", |
| 41 | + "import numpy as np\n", |
| 42 | + "\n", |
| 43 | + "import xarray as xr" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "### Data\n", |
| 51 | + "\n", |
| 52 | + "Load the data, convert to celsius, and resample to daily values" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": null, |
| 58 | + "metadata": { |
| 59 | + "ExecuteTime": { |
| 60 | + "end_time": "2020-03-17T14:43:57.831734Z", |
| 61 | + "start_time": "2020-03-17T14:43:57.651845Z" |
| 62 | + } |
| 63 | + }, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "ds = xr.tutorial.load_dataset(\"air_temperature\")\n", |
| 67 | + "\n", |
| 68 | + "# to celsius\n", |
| 69 | + "air = ds.air - 273.15\n", |
| 70 | + "\n", |
| 71 | + "# resample from 6-hourly to daily values\n", |
| 72 | + "air = air.resample(time=\"D\").mean()\n", |
| 73 | + "\n", |
| 74 | + "air" |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "markdown", |
| 79 | + "metadata": {}, |
| 80 | + "source": [ |
| 81 | + "Plot the first timestep:" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": null, |
| 87 | + "metadata": { |
| 88 | + "ExecuteTime": { |
| 89 | + "end_time": "2020-03-17T14:43:59.887120Z", |
| 90 | + "start_time": "2020-03-17T14:43:59.582894Z" |
| 91 | + } |
| 92 | + }, |
| 93 | + "outputs": [], |
| 94 | + "source": [ |
| 95 | + "projection = ccrs.LambertConformal(central_longitude=-95, central_latitude=45)\n", |
| 96 | + "\n", |
| 97 | + "f, ax = plt.subplots(subplot_kw=dict(projection=projection))\n", |
| 98 | + "\n", |
| 99 | + "air.isel(time=0).plot(transform=ccrs.PlateCarree(), cbar_kwargs=dict(shrink=0.7))\n", |
| 100 | + "ax.coastlines()" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "### Creating weights\n", |
| 108 | + "\n", |
| 109 | + "For a for a rectangular grid the cosine of the latitude is proportional to the grid cell area." |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": null, |
| 115 | + "metadata": { |
| 116 | + "ExecuteTime": { |
| 117 | + "end_time": "2020-03-17T14:44:18.777092Z", |
| 118 | + "start_time": "2020-03-17T14:44:18.736587Z" |
| 119 | + } |
| 120 | + }, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "weights = np.cos(np.deg2rad(air.lat))\n", |
| 124 | + "weights.name = \"weights\"\n", |
| 125 | + "weights" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "markdown", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "### Weighted mean" |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "code", |
| 137 | + "execution_count": null, |
| 138 | + "metadata": { |
| 139 | + "ExecuteTime": { |
| 140 | + "end_time": "2020-03-17T14:44:52.607120Z", |
| 141 | + "start_time": "2020-03-17T14:44:52.564674Z" |
| 142 | + } |
| 143 | + }, |
| 144 | + "outputs": [], |
| 145 | + "source": [ |
| 146 | + "air_weighted = air.weighted(weights)\n", |
| 147 | + "air_weighted" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "metadata": { |
| 154 | + "ExecuteTime": { |
| 155 | + "end_time": "2020-03-17T14:44:54.334279Z", |
| 156 | + "start_time": "2020-03-17T14:44:54.280022Z" |
| 157 | + } |
| 158 | + }, |
| 159 | + "outputs": [], |
| 160 | + "source": [ |
| 161 | + "weighted_mean = air_weighted.mean((\"lon\", \"lat\"))\n", |
| 162 | + "weighted_mean" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "### Plot: comparison with unweighted mean\n", |
| 170 | + "\n", |
| 171 | + "Note how the weighted mean temperature is higher than the unweighted." |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": null, |
| 177 | + "metadata": { |
| 178 | + "ExecuteTime": { |
| 179 | + "end_time": "2020-03-17T14:45:08.877307Z", |
| 180 | + "start_time": "2020-03-17T14:45:08.673383Z" |
| 181 | + } |
| 182 | + }, |
| 183 | + "outputs": [], |
| 184 | + "source": [ |
| 185 | + "weighted_mean.plot(label=\"weighted\")\n", |
| 186 | + "air.mean((\"lon\", \"lat\")).plot(label=\"unweighted\")\n", |
| 187 | + "\n", |
| 188 | + "plt.legend()" |
| 189 | + ] |
| 190 | + } |
| 191 | + ], |
| 192 | + "metadata": { |
| 193 | + "kernelspec": { |
| 194 | + "display_name": "Python 3", |
| 195 | + "language": "python", |
| 196 | + "name": "python3" |
| 197 | + }, |
| 198 | + "language_info": { |
| 199 | + "codemirror_mode": { |
| 200 | + "name": "ipython", |
| 201 | + "version": 3 |
| 202 | + }, |
| 203 | + "file_extension": ".py", |
| 204 | + "mimetype": "text/x-python", |
| 205 | + "name": "python", |
| 206 | + "nbconvert_exporter": "python", |
| 207 | + "pygments_lexer": "ipython3", |
| 208 | + "version": "3.7.6" |
| 209 | + }, |
| 210 | + "toc": { |
| 211 | + "base_numbering": 1, |
| 212 | + "nav_menu": {}, |
| 213 | + "number_sections": true, |
| 214 | + "sideBar": true, |
| 215 | + "skip_h1_title": false, |
| 216 | + "title_cell": "Table of Contents", |
| 217 | + "title_sidebar": "Contents", |
| 218 | + "toc_cell": true, |
| 219 | + "toc_position": {}, |
| 220 | + "toc_section_display": true, |
| 221 | + "toc_window_display": true |
| 222 | + } |
| 223 | + }, |
| 224 | + "nbformat": 4, |
| 225 | + "nbformat_minor": 4 |
| 226 | +} |
0 commit comments