You can run this notebook in a live session Binder or view it on Github.

Table of Contents

  • 1  Compare weighted and unweighted mean temperature

      • 1.0.1  Data

      • 1.0.2  Creating weights

      • 1.0.3  Weighted mean

      • 1.0.4  Plot: comparison with unweighted mean

Compare weighted and unweighted mean temperature

Author: Mathias Hauser

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.

[1]:
%matplotlib inline

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np

import xarray as xr

Data

Load the data, convert to celsius, and resample to daily values

[2]:
ds = xr.tutorial.load_dataset("air_temperature")

# to celsius
air = ds.air - 273.15

# resample from 6-hourly to daily values
air = air.resample(time="D").mean()

air
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-91cf2eecbfdd> in <module>
----> 1 ds = xr.tutorial.load_dataset("air_temperature")
      2
      3 # to celsius
      4 air = ds.air - 273.15
      5

/build/python-xarray-6p5Afb/python-xarray-0.16.0/xarray/tutorial.py in load_dataset(*args, **kwargs)
    111     open_dataset
    112     """
--> 113     with open_dataset(*args, **kwargs) as ds:
    114         return ds.load()
    115

/build/python-xarray-6p5Afb/python-xarray-0.16.0/xarray/tutorial.py in open_dataset(name, cache, cache_dir, github_url, branch, **kws)
     76         # May want to add an option to remove it.
     77         if not _os.path.isdir(longdir):
---> 78             _os.mkdir(longdir)
     79
     80         url = "/".join((github_url, "raw", branch, fullname))

FileNotFoundError: [Errno 2] No such file or directory: '/sbuild-nonexistent/.xarray_tutorial_data'

Plot the first timestep:

[3]:
projection = ccrs.LambertConformal(central_longitude=-95, central_latitude=45)

f, ax = plt.subplots(subplot_kw=dict(projection=projection))

air.isel(time=0).plot(transform=ccrs.PlateCarree(), cbar_kwargs=dict(shrink=0.7))
ax.coastlines()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-cfd25e3eb647> in <module>
      3 f, ax = plt.subplots(subplot_kw=dict(projection=projection))
      4
----> 5 air.isel(time=0).plot(transform=ccrs.PlateCarree(), cbar_kwargs=dict(shrink=0.7))
      6 ax.coastlines()

NameError: name 'air' is not defined
../_images/examples_area_weighted_temperature_6_1.png

Creating weights

For a for a rectangular grid the cosine of the latitude is proportional to the grid cell area.

[4]:
weights = np.cos(np.deg2rad(air.lat))
weights.name = "weights"
weights
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-033dcb3b260c> in <module>
----> 1 weights = np.cos(np.deg2rad(air.lat))
      2 weights.name = "weights"
      3 weights

NameError: name 'air' is not defined

Weighted mean

[5]:
air_weighted = air.weighted(weights)
air_weighted
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-648627c40eb5> in <module>
----> 1 air_weighted = air.weighted(weights)
      2 air_weighted

NameError: name 'air' is not defined
[6]:
weighted_mean = air_weighted.mean(("lon", "lat"))
weighted_mean
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-2fa583d2f71b> in <module>
----> 1 weighted_mean = air_weighted.mean(("lon", "lat"))
      2 weighted_mean

NameError: name 'air_weighted' is not defined

Plot: comparison with unweighted mean

Note how the weighted mean temperature is higher than the unweighted.

[7]:
weighted_mean.plot(label="weighted")
air.mean(("lon", "lat")).plot(label="unweighted")

plt.legend()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-7-452ca4959e9e> in <module>
----> 1 weighted_mean.plot(label="weighted")
      2 air.mean(("lon", "lat")).plot(label="unweighted")
      3
      4 plt.legend()

NameError: name 'weighted_mean' is not defined