-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Feature/weighted #2922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feature/weighted #2922
Changes from 55 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
0f2da8e
weighted for DataArray
mathause 5f64492
remove some commented code
mathause 685e5c4
pep8 and faulty import tests
mathause c9d612d
add weighted sum, replace 0s in sum_of_wgt
mathause a20a4cf
weighted: overhaul tests
mathause 26c24b6
weighted: pep8
mathause f3c6758
weighted: pep8 lines
mathause 25c3c29
weighted update docs
mathause 5d37d11
weighted: fix typo
mathause b1c572b
weighted: pep8
mathause d1d1f2c
undo changes to avoid merge conflict
mathause 6be1414
Merge branch 'master' into feature/weighted
mathause 059263c
add weighted to dataarray again
mathause 8b1904b
remove super
mathause 8cad145
overhaul core/weighted.py
mathause 49d4e43
add DatasetWeighted class
mathause 527256e
_maybe_get_all_dims return sorted tuple
mathause 739568f
work on: test_weighted
mathause f01305d
black and flake8
mathause 2e3880d
Apply suggestions from code review (docs)
mathause ae8d048
restructure interim
mathause dc7f605
restructure classes
mathause c646568
Merge branch 'master' into feature/weighted
mathause e2ad69e
update weighted.py
mathause bd4f048
black
mathause 3c7695a
use map; add keep_attrs
mathause ef07edd
implement expected_weighted; update tests
mathause 064b5a9
add whats new
mathause fec1a35
Merge branch 'master' into feature/weighted
mathause 72c7942
undo changes to whats-new
mathause 0e91411
F811: noqa where?
mathause 1eb2913
api.rst
mathause 118dfed
add to computation
mathause e08c921
small updates
mathause 0fafe0b
add example to gallery
mathause a8d330d
typo
mathause ae0012f
another typo
mathause 111259b
correct docstring in core/common.py
mathause 5afc6f3
Merge branch 'master' into feature/weighted
mathause 668b54b
typos
mathause d877022
adjust review
mathause ead681e
clean tests
mathause c4598ba
add test nonequal coords
mathause 866fba5
comment on use of dot
mathause 3cc00c1
fix erroneous merge
mathause 8f34167
Merge branch 'master' into feature/weighted
mathause 9f0a8cd
update tests
mathause 98929f1
Merge branch 'master' into feature/weighted
mathause 62c43e6
move example to notebook
mathause 2e8aba2
move whats-new entry to 15.1
mathause d14f668
some doc updates
mathause 7fa78ae
dot to own function
mathause 3ebb9d4
simplify some tests
mathause f01d47a
Doc updates
dcherian 4b184f6
very minor changes.
dcherian 1e06adc
fix & add references
dcherian 706579a
doc: return 0/NaN on 0 weights
mathause b2718db
Merge branch 'feature/weighted' of https://github.com/mathause/xarray…
mathause 4c17108
Merge branch 'master' into feature/weighted
mathause 8acc78e
Update xarray/core/common.py
dcherian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": { | ||
| "toc": true | ||
| }, | ||
| "source": [ | ||
| "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", | ||
| "<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>" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "# Compare weighted and unweighted mean temperature\n", | ||
| "\n", | ||
| "\n", | ||
| "Author: [Mathias Hauser](https://github.com/mathause/)\n", | ||
| "\n", | ||
| "\n", | ||
| "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" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": { | ||
| "ExecuteTime": { | ||
| "end_time": "2020-03-17T14:43:57.222351Z", | ||
| "start_time": "2020-03-17T14:43:56.147541Z" | ||
| } | ||
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "%matplotlib inline\n", | ||
| "\n", | ||
| "import cartopy.crs as ccrs\n", | ||
| "import matplotlib.pyplot as plt\n", | ||
| "import numpy as np\n", | ||
| "\n", | ||
| "import xarray as xr" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "### Data\n", | ||
| "\n", | ||
| "Load the data, convert to celsius, and resample to daily values" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": { | ||
| "ExecuteTime": { | ||
| "end_time": "2020-03-17T14:43:57.831734Z", | ||
| "start_time": "2020-03-17T14:43:57.651845Z" | ||
| } | ||
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "ds = xr.tutorial.load_dataset(\"air_temperature\")\n", | ||
| "\n", | ||
| "# to celsius\n", | ||
| "air = ds.air - 273.15\n", | ||
| "\n", | ||
| "# resample from 6-hourly to daily values\n", | ||
| "air = air.resample(time=\"D\").mean()\n", | ||
| "\n", | ||
| "air" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "Plot the first timestep:" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": { | ||
| "ExecuteTime": { | ||
| "end_time": "2020-03-17T14:43:59.887120Z", | ||
| "start_time": "2020-03-17T14:43:59.582894Z" | ||
| } | ||
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "projection = ccrs.LambertConformal(central_longitude=-95, central_latitude=45)\n", | ||
| "\n", | ||
| "f, ax = plt.subplots(subplot_kw=dict(projection=projection))\n", | ||
| "\n", | ||
| "air.isel(time=0).plot(transform=ccrs.PlateCarree(), cbar_kwargs=dict(shrink=0.7))\n", | ||
| "ax.coastlines()" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "### Creating weights\n", | ||
| "\n", | ||
| "For a for a rectangular grid the cosine of the latitude is proportional to the grid cell area." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": { | ||
| "ExecuteTime": { | ||
| "end_time": "2020-03-17T14:44:18.777092Z", | ||
| "start_time": "2020-03-17T14:44:18.736587Z" | ||
| } | ||
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "weights = np.cos(np.deg2rad(air.lat))\n", | ||
| "weights.name = \"weights\"\n", | ||
| "weights" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "### Weighted mean" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": { | ||
| "ExecuteTime": { | ||
| "end_time": "2020-03-17T14:44:52.607120Z", | ||
| "start_time": "2020-03-17T14:44:52.564674Z" | ||
| } | ||
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "air_weighted = air.weighted(weights)\n", | ||
| "air_weighted" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": { | ||
| "ExecuteTime": { | ||
| "end_time": "2020-03-17T14:44:54.334279Z", | ||
| "start_time": "2020-03-17T14:44:54.280022Z" | ||
| } | ||
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "weighted_mean = air_weighted.mean((\"lon\", \"lat\"))\n", | ||
| "weighted_mean" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "markdown", | ||
| "metadata": {}, | ||
| "source": [ | ||
| "### Plot: comparison with unweighted mean\n", | ||
| "\n", | ||
| "Note how the weighted mean temperature is higher than the unweighted." | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": null, | ||
| "metadata": { | ||
| "ExecuteTime": { | ||
| "end_time": "2020-03-17T14:45:08.877307Z", | ||
| "start_time": "2020-03-17T14:45:08.673383Z" | ||
| } | ||
| }, | ||
| "outputs": [], | ||
| "source": [ | ||
| "weighted_mean.plot(label=\"weighted\")\n", | ||
| "air.mean((\"lon\", \"lat\")).plot(label=\"unweighted\")\n", | ||
| "\n", | ||
| "plt.legend()" | ||
| ] | ||
| } | ||
| ], | ||
| "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.6" | ||
| }, | ||
| "toc": { | ||
| "base_numbering": 1, | ||
| "nav_menu": {}, | ||
| "number_sections": true, | ||
| "sideBar": true, | ||
| "skip_h1_title": false, | ||
| "title_cell": "Table of Contents", | ||
| "title_sidebar": "Contents", | ||
| "toc_cell": true, | ||
| "toc_position": {}, | ||
| "toc_section_display": true, | ||
| "toc_window_display": true | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 4 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.