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

#

Table of Contents

# 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 grid cell 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

    ---------------------------------------------------------------------------
    ModuleNotFoundError                       Traceback (most recent call last)
    /build/python-xarray-bL1QJU/python-xarray-0.21.1/xarray/tutorial.py in open_dataset(name, cache, cache_dir, engine, **kws)
        113     try:
    --> 114         import pooch
        115     except ImportError as e:

    ModuleNotFoundError: No module named 'pooch'

    The above exception was the direct cause of the following exception:

    ImportError                               Traceback (most recent call last)
    /tmp/ipykernel_15989/4271698893.py in <module>
    ----> 1 ds = xr.tutorial.load_dataset("air_temperature")
          2
          3 # to celsius
          4 air = ds.air - 273.15
          5

    /build/python-xarray-bL1QJU/python-xarray-0.21.1/xarray/tutorial.py in load_dataset(*args, **kwargs)
        223     open_dataset
        224     """
    --> 225     with open_dataset(*args, **kwargs) as ds:
        226         return ds.load()
        227

    /build/python-xarray-bL1QJU/python-xarray-0.21.1/xarray/tutorial.py in open_dataset(name, cache, cache_dir, engine, **kws)
        114         import pooch
        115     except ImportError as e:
    --> 116         raise ImportError(
        117             "tutorial.open_dataset depends on pooch to download and manage datasets."
        118             " To proceed please install pooch."

    ImportError: tutorial.open_dataset depends on pooch to download and manage datasets. To proceed please install pooch.

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)
    /tmp/ipykernel_15989/1190473392.py 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

## Creating weights

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)
    /tmp/ipykernel_15989/3100398628.py 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)
    /tmp/ipykernel_15989/1401420034.py 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)
    /tmp/ipykernel_15989/3735643031.py 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)
    /tmp/ipykernel_15989/904801603.py 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