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
[2]:
Show/Hide data repr Show/Hide attributes
xarray.DataArray
'air'
  • time: 730
  • lat: 25
  • lon: 53
  • -31.2775 -30.849998 -30.475002 ... 23.065002 22.715004 22.390007
    array([[[-31.2775   , -30.849998 , -30.475002 , ..., -39.7775   ,
             -37.975    , -35.475002 ],
            [-28.575005 , -28.5775   , -28.874996 , ..., -41.9025   ,
             -40.324997 , -36.85     ],
            [-19.149998 , -19.927498 , -21.3275   , ..., -41.675    ,
             -39.454998 , -34.524998 ],
            ...,
            [ 23.15001  ,  22.824997 ,  22.849998 , ...,  22.747505 ,
              22.170013 ,  21.795006 ],
            [ 23.174995 ,  23.574997 ,  23.592514 , ...,  23.022507 ,
              22.850006 ,  22.397507 ],
            [ 23.470009 ,  23.845001 ,  23.950005 , ...,  23.872505 ,
              23.897507 ,  23.82251  ]],
    
           [[-29.550003 , -29.650005 , -29.849998 , ..., -34.177498 ,
             -32.3525   , -30.0775   ],
            [-25.3275   , -25.95     , -26.927498 , ..., -37.225    ,
             -36.552498 , -34.550003 ],
            [-19.627502 , -21.0775   , -22.852497 , ..., -35.452496 ,
             -34.277496 , -31.25     ],
            ...,
            [ 22.972504 ,  22.24501  ,  22.049995 , ...,  22.99501  ,
              22.720001 ,  22.0225   ],
            [ 23.972504 ,  23.647507 ,  22.9225   , ...,  23.497498 ,
              23.622505 ,  23.070007 ],
            [ 24.545006 ,  24.372513 ,  23.875    , ...,  23.975006 ,
              23.997505 ,  24.100006 ]],
    
           [[-29.2775   , -29.1525   , -29.177502 , ..., -33.6525   ,
             -31.599998 , -29.25     ],
            [-27.552498 , -26.900002 , -26.729996 , ..., -36.727497 ,
             -35.5275   , -33.125    ],
            [-20.975002 , -20.474998 , -20.649998 , ..., -32.127495 ,
             -30.350002 , -27.454998 ],
            ...,
            [ 23.395004 ,  22.57251  ,  22.425003 , ...,  22.625    ,
              21.850006 ,  21.150002 ],
            [ 24.222504 ,  23.674995 ,  23.07251  , ...,  23.092514 ,
              22.850006 ,  22.345009 ],
            [ 24.625    ,  24.345001 ,  24.095001 , ...,  24.174995 ,
              24.1725   ,  24.32251  ]],
    
           ...,
    
           [[-13.055    , -13.154995 , -13.682495 , ..., -23.23     ,
             -24.5075   , -25.780003 ],
            [ -6.6524963,  -6.207489 ,  -6.404991 , ..., -20.155003 ,
             -20.005001 , -19.477505 ],
            [ -6.757492 ,  -6.8074875,  -6.8799896, ..., -16.435001 ,
             -14.129997 , -11.229996 ],
            ...,
            [ 21.519997 ,  20.870003 ,  20.9225   , ...,  24.322502 ,
              23.895004 ,  22.920006 ],
            [ 22.792511 ,  22.897507 ,  22.7725   , ...,  24.6175   ,
              24.097504 ,  22.967506 ],
            [ 24.297493 ,  24.620003 ,  24.2975   , ...,  24.947502 ,
              24.542496 ,  23.970001 ]],
    
           [[-25.130001 , -24.405003 , -23.755005 , ..., -24.1325   ,
             -24.807495 , -25.685001 ],
            [-21.93     , -20.805    , -19.805004 , ..., -29.032505 ,
             -28.0075   , -26.382504 ],
            [-14.830006 , -14.380005 , -13.959999 , ..., -28.579998 ,
             -25.085    , -20.382504 ],
            ...,
            [ 23.215004 ,  22.265    ,  22.015007 , ...,  23.740005 ,
              23.195007 ,  22.195    ],
            [ 24.3675   ,  24.514992 ,  23.895012 , ...,  23.415    ,
              22.995003 ,  22.269997 ],
            [ 25.417496 ,  25.592499 ,  25.192497 , ...,  23.642502 ,
              23.190002 ,  22.720001 ]],
    
           [[-28.935001 , -29.535    , -30.385002 , ..., -29.410004 ,
             -28.960003 , -28.46     ],
            [-23.834995 , -24.060001 , -24.559998 , ..., -32.585    ,
             -31.635002 , -30.035004 ],
            [-10.209999 , -10.784988 , -11.434998 , ..., -33.684998 ,
             -31.035    , -27.135002 ],
            ...,
            [ 21.69001  ,  21.990005 ,  23.489998 , ...,  22.265007 ,
              22.015    ,  21.415009 ],
            [ 23.390007 ,  24.439995 ,  24.94001  , ...,  22.415009 ,
              22.315002 ,  21.640007 ],
            [ 24.840012 ,  25.590004 ,  25.54     , ...,  23.065002 ,
              22.715004 ,  22.390007 ]]], dtype=float32)
    • time
      (time)
      datetime64[ns]
      2013-01-01 ... 2014-12-31
      array(['2013-01-01T00:00:00.000000000', '2013-01-02T00:00:00.000000000',
             '2013-01-03T00:00:00.000000000', ..., '2014-12-29T00:00:00.000000000',
             '2014-12-30T00:00:00.000000000', '2014-12-31T00:00:00.000000000'],
            dtype='datetime64[ns]')
    • lat
      (lat)
      float32
      75.0 72.5 70.0 ... 20.0 17.5 15.0
      standard_name :
      latitude
      long_name :
      Latitude
      units :
      degrees_north
      axis :
      Y
      array([75. , 72.5, 70. , 67.5, 65. , 62.5, 60. , 57.5, 55. , 52.5, 50. , 47.5,
             45. , 42.5, 40. , 37.5, 35. , 32.5, 30. , 27.5, 25. , 22.5, 20. , 17.5,
             15. ], dtype=float32)
    • lon
      (lon)
      float32
      200.0 202.5 205.0 ... 327.5 330.0
      standard_name :
      longitude
      long_name :
      Longitude
      units :
      degrees_east
      axis :
      X
      array([200. , 202.5, 205. , 207.5, 210. , 212.5, 215. , 217.5, 220. , 222.5,
             225. , 227.5, 230. , 232.5, 235. , 237.5, 240. , 242.5, 245. , 247.5,
             250. , 252.5, 255. , 257.5, 260. , 262.5, 265. , 267.5, 270. , 272.5,
             275. , 277.5, 280. , 282.5, 285. , 287.5, 290. , 292.5, 295. , 297.5,
             300. , 302.5, 305. , 307.5, 310. , 312.5, 315. , 317.5, 320. , 322.5,
             325. , 327.5, 330. ], dtype=float32)

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()
[3]:
<cartopy.mpl.feature_artist.FeatureArtist at 0x7f3e04414e50>
/home/docs/checkouts/readthedocs.org/user_builds/xray/conda/v0.15.1/lib/python3.8/site-packages/cartopy/io/__init__.py:260: DownloadWarning: Downloading: http://naciscdn.org/naturalearth/110m/physical/ne_110m_coastline.zip
  warnings.warn('Downloading: {}'.format(url), DownloadWarning)
../_images/examples_area_weighted_temperature_6_2.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
[4]:
Show/Hide data repr Show/Hide attributes
xarray.DataArray
'weights'
  • lat: 25
  • 0.25881907 0.30070582 0.34202015 ... 0.9396926 0.95371693 0.9659258
    array([0.25881907, 0.30070582, 0.34202015, 0.38268346, 0.42261827,
           0.4617486 , 0.49999997, 0.5372996 , 0.57357645, 0.6087614 ,
           0.6427876 , 0.67559016, 0.70710677, 0.7372773 , 0.76604444,
           0.7933533 , 0.81915206, 0.8433914 , 0.8660254 , 0.8870108 ,
           0.90630776, 0.9238795 , 0.9396926 , 0.95371693, 0.9659258 ],
          dtype=float32)
    • lat
      (lat)
      float32
      75.0 72.5 70.0 ... 20.0 17.5 15.0
      standard_name :
      latitude
      long_name :
      Latitude
      units :
      degrees_north
      axis :
      Y
      array([75. , 72.5, 70. , 67.5, 65. , 62.5, 60. , 57.5, 55. , 52.5, 50. , 47.5,
             45. , 42.5, 40. , 37.5, 35. , 32.5, 30. , 27.5, 25. , 22.5, 20. , 17.5,
             15. ], dtype=float32)

Weighted mean

[5]:
air_weighted = air.weighted(weights)
air_weighted
[5]:
DataArrayWeighted with weights along dimensions: lat
[6]:
weighted_mean = air_weighted.mean(("lon", "lat"))
weighted_mean
[6]:
Show/Hide data repr Show/Hide attributes
xarray.DataArray
  • time: 730
  • 6.092401 5.5279922 5.6512914 5.786239 ... 6.4502645 5.985239 5.5805774
    array([ 6.092401 ,  5.5279922,  5.6512914,  5.786239 ,  5.9117665,
            5.6834373,  5.9767127,  6.456724 ,  6.5710645,  6.504647 ,
            6.134899 ,  5.926867 ,  5.8268237,  5.7228684,  5.5780067,
            5.4655232,  5.09124  ,  4.9860163,  5.22863  ,  5.2516627,
            5.4277263,  5.3877945,  5.4338994,  5.364401 ,  5.4685388,
            5.2290297,  5.350285 ,  5.3418307,  5.372671 ,  5.3595133,
            5.1403375,  5.0555673,  5.0724645,  5.23522  ,  5.3184857,
            5.4991755,  5.720886 ,  5.7286143,  5.7608094,  5.825561 ,
            6.268504 ,  6.436903 ,  6.510233 ,  6.564767 ,  6.6087847,
            6.4212685,  5.9147425,  5.5546775,  5.329217 ,  5.3359075,
            5.0705895,  5.283736 ,  5.5952206,  6.05466  ,  6.530731 ,
            6.507418 ,  6.3917418,  6.3951263,  6.3980885,  6.5293736,
            6.4771113,  6.535765 ,  6.692519 ,  6.6773686,  6.511633 ,
            6.4470334,  6.860378 ,  7.437535 ,  7.6981063,  7.484263 ,
            7.2581897,  7.135959 ,  7.093408 ,  7.267086 ,  7.348537 ,
            7.3217864,  7.221145 ,  7.212927 ,  7.2840424,  7.54338  ,
            7.854373 ,  8.11584  ,  8.261896 ,  8.111622 ,  8.219126 ,
            8.358713 ,  8.716147 ,  9.151885 ,  9.370043 ,  9.415865 ,
            9.073439 ,  8.820655 ,  8.804644 ,  8.856381 ,  9.0674515,
            9.40715  ,  9.696928 ,  9.742079 ,  9.659618 ,  9.695613 ,
            9.561744 ,  9.4079895,  9.617972 ,  9.76423  ,  9.68687  ,
            9.507037 ,  9.583357 ,  9.536606 ,  9.149774 ,  9.206877 ,
            9.712005 ,  9.949959 , 10.212819 , 10.352596 , 10.463948 ,
           10.969345 , 11.17319  , 11.37765  , 11.666062 , 11.5937605,
           11.5184355, 11.372785 , 11.2966   , 11.389726 , 11.630326 ,
           12.04973  , 12.28303  , 12.51103  , 12.665153 , 12.658167 ,
           12.697796 , 12.935123 , 13.231032 , 13.601825 , 13.677147 ,
           13.944387 , 14.055336 , 14.044068 , 14.039132 , 14.049725 ,
           14.107756 , 14.125751 , 14.220755 , 14.389268 , 14.666479 ,
           14.894946 , 15.093599 , 15.187727 , 15.229364 , 15.385192 ,
           15.517196 , 15.576119 , 15.46246  , 15.336645 , 15.5871935,
           15.935525 , 16.160788 , 16.362488 , 16.402073 , 16.613111 ,
           16.690079 , 16.710402 , 16.957804 , 17.036436 , 17.024513 ,
           17.113266 , 17.403803 , 17.471989 , 17.354223 , 17.286469 ,
           17.469456 , 17.691341 , 17.808374 , 18.005512 , 18.180136 ,
           18.14735  , 18.087774 , 18.541489 , 18.812765 , 18.772768 ,
           18.774654 , 18.75971  , 18.61955  , 18.493517 , 18.666697 ,
           18.623306 , 18.700373 , 18.745745 , 18.938585 , 19.04691  ,
           19.034967 , 18.728828 , 18.678013 , 18.704285 , 18.970568 ,
           19.138163 , 19.204895 , 19.254156 , 19.216059 , 19.307415 ,
           19.161972 , 19.060398 , 19.24664  , 19.396854 , 19.503927 ,
           19.524248 , 19.532833 , 19.501417 , 19.38236  , 19.44305  ,
           19.590784 , 19.67537  , 19.759476 , 19.736116 , 19.748497 ,
           19.778793 , 19.929216 , 19.915003 , 19.93247  , 19.816433 ,
           19.897675 , 19.921324 , 20.089659 , 20.174969 , 20.020954 ,
           19.916275 , 19.674044 , 19.609095 , 19.70313  , 19.742498 ,
           19.547344 , 19.40055  , 19.364971 , 19.30133  , 19.32931  ,
           19.252117 , 19.19223  , 19.12292  , 19.05988  , 18.91547  ,
           18.972017 , 19.109762 , 19.17509  , 18.943657 , 18.778591 ,
           18.799852 , 18.990286 , 19.173693 , 19.13864  , 19.046425 ,
           18.653067 , 18.528996 , 18.39046  , 18.382334 , 18.224634 ,
           18.074722 , 17.809874 , 17.728031 , 17.559685 , 17.185799 ,
           17.007568 , 16.844471 , 16.628885 , 16.632135 , 16.684452 ,
           16.7827   , 16.902582 , 16.982395 , 16.94174  , 16.731483 ,
           16.569954 , 16.642183 , 16.44107  , 16.32606  , 16.18429  ,
           15.9199915, 15.794434 , 15.333749 , 15.139457 , 15.239242 ,
           15.183755 , 15.217643 , 15.256958 , 15.194689 , 15.338222 ,
           15.195775 , 14.989729 , 15.019909 , 14.836974 , 14.780034 ,
           14.746626 , 14.643647 , 14.434376 , 14.174599 , 13.690615 ,
           13.393573 , 13.324159 , 13.234417 , 13.105837 , 12.825273 ,
           12.590388 , 12.360748 , 12.4433975, 12.511931 , 12.623505 ,
           12.464619 , 11.873396 , 11.3994465, 11.027147 , 11.073167 ,
           10.917732 , 10.710046 , 10.599323 , 10.606334 , 10.488928 ,
           10.463197 , 10.3303175, 10.669272 , 10.642239 , 10.355392 ,
           10.218304 , 10.060601 ,  9.39898  ,  8.9061985,  8.508362 ,
            8.3262005,  8.183812 ,  8.244926 ,  8.287311 ,  8.149289 ,
            7.8001986,  7.683447 ,  7.886427 ,  7.908317 ,  8.032764 ,
            8.3206215,  8.253178 ,  7.9912343,  7.34998  ,  6.836603 ,
            6.3923655,  6.120939 ,  5.973691 ,  5.8665175,  5.5887747,
            5.2916183,  5.564002 ,  5.691005 ,  5.80986  ,  6.1114473,
            6.0680313,  6.117207 ,  6.0920706,  5.9908032,  5.9610896,
            5.9642596,  5.702404 ,  5.974395 ,  5.7453547,  5.720085 ,
            5.7497687,  5.9001875,  5.3603168,  4.975006 ,  5.034215 ,
            4.940766 ,  4.8569155,  5.053116 ,  5.1932135,  5.154923 ,
            4.8237185,  4.9460754,  5.5160084,  6.096235 ,  6.306667 ,
            6.5905867,  6.879803 ,  7.4664016,  7.676546 ,  7.454749 ,
            7.2756977,  6.8854356,  6.8190045,  7.000944 ,  6.9153275,
            6.518248 ,  6.376813 ,  6.1025376,  5.836243 ,  6.2818537,
            6.7731247,  6.2265654,  5.8736043,  5.888508 ,  6.1134453,
            6.2495136,  6.5044417,  6.24279  ,  5.889445 ,  5.349453 ,
            5.1401687,  4.6669383,  4.414816 ,  4.721158 ,  4.9752355,
            4.608664 ,  4.661077 ,  4.349577 ,  4.6422057,  5.1544943,
            5.759283 ,  5.732609 ,  6.1441774,  6.4816713,  6.8110523,
            6.8179736,  6.5025144,  6.6047516,  6.3821716,  5.914061 ,
            5.7290044,  5.559222 ,  5.483134 ,  5.661207 ,  5.625481 ,
            5.448103 ,  5.1356015,  4.9774427,  5.2434754,  5.8586645,
            6.168095 ,  6.305263 ,  6.4765425,  6.870062 ,  7.175213 ,
            7.3970156,  7.301994 ,  7.3982806,  7.2345557,  7.2241607,
            7.2945085,  7.289131 ,  7.447318 ,  7.640786 ,  7.600175 ,
            7.36517  ,  7.075017 ,  6.9393187,  6.971508 ,  6.862118 ,
            7.409107 ,  7.8365316,  8.264829 ,  8.424241 ,  8.335242 ,
            8.2654295,  8.436648 ,  8.637371 ,  8.597472 ,  8.744505 ,
            9.08334  ,  9.472183 ,  9.498424 ,  9.722453 ,  9.790919 ,
            9.929076 ,  9.949384 ,  9.750111 ,  9.586125 ,  9.281569 ,
            9.379645 ,  9.647652 , 10.149494 , 10.374341 , 10.813217 ,
           11.019511 , 11.4107485, 11.348444 , 11.275326 , 11.391884 ,
           11.480863 , 11.496771 , 11.4580965, 11.741908 , 12.039109 ,
           12.119159 , 12.294326 , 12.496779 , 12.63022  , 12.644609 ,
           12.698251 , 12.771256 , 12.8391485, 12.969559 , 13.328925 ,
           13.524718 , 13.442509 , 13.43661  , 13.444263 , 13.500961 ,
           13.596045 , 13.940781 , 14.161496 , 14.333419 , 14.536955 ,
           14.600366 , 14.704756 , 15.068306 , 15.144813 , 15.133619 ,
           15.378984 , 15.531531 , 15.775833 , 15.857242 , 15.643098 ,
           15.790469 , 15.875157 , 15.876257 , 15.883789 , 16.18826  ,
           16.136084 , 16.061764 , 16.140556 , 16.192955 , 16.183643 ,
           16.596642 , 16.691175 , 16.750244 , 16.765684 , 16.822803 ,
           16.830599 , 17.189337 , 17.297546 , 17.34677  , 17.249882 ,
           17.421677 , 17.662115 , 17.73621  , 17.911348 , 18.158567 ,
           18.063082 , 18.149403 , 18.307577 , 18.539606 , 18.647154 ,
           18.710331 , 18.558868 , 18.850899 , 18.846672 , 18.771374 ,
           18.881458 , 18.969193 , 19.0445   , 19.062105 , 18.9497   ,
           18.962055 , 19.090527 , 19.243608 , 19.44696  , 19.593018 ,
           19.392294 , 19.387154 , 19.369015 , 19.36154  , 19.38268  ,
           19.553045 , 19.74886  , 20.010616 , 20.067825 , 19.960098 ,
           19.71552  , 19.759594 , 19.94238  , 19.978153 , 19.960987 ,
           20.020414 , 20.074722 , 20.038465 , 19.872437 , 20.054897 ,
           20.122032 , 20.110695 , 20.11393  , 19.893097 , 19.756624 ,
           19.817728 , 19.870953 , 20.03211  , 19.948633 , 19.85636  ,
           19.89647  , 19.88095  , 20.027094 , 20.135996 , 20.0121   ,
           19.980116 , 19.959826 , 20.002214 , 19.86149  , 19.853733 ,
           19.663826 , 19.39195  , 19.252623 , 19.327692 , 19.449005 ,
           19.417816 , 19.292124 , 19.140076 , 19.20655  , 19.26588  ,
           19.160797 , 19.017757 , 18.7078   , 18.320269 , 18.16304  ,
           17.959549 , 17.953415 , 17.640057 , 17.511862 , 17.214102 ,
           17.160482 , 17.318962 , 17.481031 , 17.470463 , 17.67231  ,
           17.652508 , 17.61472  , 17.517643 , 17.6717   , 17.708685 ,
           17.484276 , 17.331753 , 17.202616 , 17.068213 , 16.910051 ,
           16.536924 , 16.133308 , 16.05551  , 16.100082 , 15.909406 ,
           15.764091 , 15.631487 , 15.827745 , 16.026222 , 16.319872 ,
           16.156448 , 15.898445 , 15.830862 , 15.810078 , 15.589792 ,
           15.309618 , 15.105176 , 14.96468  , 14.966973 , 14.904603 ,
           14.61066  , 14.330112 , 14.255611 , 14.31403  , 13.940103 ,
           13.758863 , 13.820865 , 14.021832 , 13.888187 , 13.724709 ,
           13.190875 , 12.995149 , 12.669842 , 12.585032 , 12.377669 ,
           12.178653 , 12.082313 , 11.874204 , 11.660166 , 11.601137 ,
           11.558611 , 11.183846 , 11.237345 , 11.091917 , 10.472193 ,
            9.898911 ,  9.431238 ,  9.491593 ,  9.688618 ,  9.998573 ,
            9.793551 ,  9.315285 ,  9.259933 ,  9.3849945,  9.343004 ,
            9.202585 ,  9.472328 ,  9.424212 ,  9.050674 ,  8.568184 ,
            7.7191467,  7.3312225,  7.4512987,  7.4235888,  7.518795 ,
            7.4950347,  7.623865 ,  8.083245 ,  8.049131 ,  8.027269 ,
            8.069612 ,  7.912531 ,  8.042945 ,  8.34481  ,  8.507071 ,
            8.708198 ,  8.604949 ,  8.312463 ,  8.257239 ,  7.9841394,
            7.693307 ,  7.421974 ,  7.4352374,  7.4829583,  7.642844 ,
            7.908468 ,  8.036132 ,  7.625418 ,  7.7533164,  7.850424 ,
            7.6213007,  6.8473396,  6.4502645,  5.985239 ,  5.5805774],
          dtype=float32)
    • time
      (time)
      datetime64[ns]
      2013-01-01 ... 2014-12-31
      array(['2013-01-01T00:00:00.000000000', '2013-01-02T00:00:00.000000000',
             '2013-01-03T00:00:00.000000000', ..., '2014-12-29T00:00:00.000000000',
             '2014-12-30T00:00:00.000000000', '2014-12-31T00:00:00.000000000'],
            dtype='datetime64[ns]')

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()
[7]:
<matplotlib.legend.Legend at 0x7f3e04040a90>
../_images/examples_area_weighted_temperature_13_1.png