Create meteorology data#

Create MetDataset and MetDataArray data structures for working with meteorology data.

[1]:
from datetime import datetime

import xarray as xr
import numpy as np

from pycontrails import MetDataset, MetDataArray, MetVariable

MetDataArray#

Thin wrapper around xr.DataArray

[2]:
rng = np.random.default_rng(seed=2020)

# construct an xarray DataArray with coordinate labels for data
da = xr.DataArray(
    name="random",
    data=rng.random((20, 15, 4, 5)),
    dims=["longitude", "latitude", "level", "time"],
    coords={
        "longitude": np.arange(-100, -80, 1.0),
        "latitude": np.arange(30, 45, 1.0),
        "level": np.arange(100, 500, 100),
        "time": [datetime(2021, 1, 1, h) for h in range(5)],
    },
)
da
[2]:
<xarray.DataArray 'random' (longitude: 20, latitude: 15, level: 4, time: 5)>
array([[[[4.68307543e-01, 5.14342231e-01, 8.63988281e-01,
          7.19386871e-01, 3.33497882e-01],
         [8.81663616e-01, 5.18664864e-01, 5.23219417e-01,
          7.22388696e-01, 4.46782561e-01],
         [8.50357081e-01, 6.83936263e-01, 6.65900069e-01,
          9.46717074e-01, 7.53638850e-01],
         [9.17964165e-02, 8.26090137e-01, 5.55186777e-01,
          2.31152608e-01, 9.82958913e-01]],

        [[5.84613086e-01, 4.26518507e-01, 8.45336094e-01,
          5.07850470e-01, 7.10216629e-01],
         [1.37352673e-01, 4.84569777e-01, 8.89744202e-01,
          9.52976849e-01, 3.76043941e-02],
         [9.54223457e-01, 8.98512171e-01, 4.14081276e-01,
          4.20221061e-01, 6.91400341e-02],
         [4.21756867e-01, 4.93848391e-01, 1.51696284e-01,
          9.41137061e-01, 1.88116189e-01]],

        [[1.90065507e-02, 9.44932088e-01, 9.93219943e-01,
          6.81811974e-01, 7.84335294e-01],
...
         [8.92843751e-01, 4.07684237e-01, 3.05168469e-01,
          1.73576037e-01, 3.34321840e-01]],

        [[7.61263799e-01, 9.91563651e-01, 9.47543921e-01,
          6.68735461e-01, 2.71486792e-01],
         [2.05356394e-01, 7.42337468e-01, 2.70578729e-01,
          7.15520327e-01, 7.30134504e-01],
         [6.68886804e-01, 5.15651993e-01, 8.40987212e-01,
          5.37939968e-01, 3.71399439e-01],
         [1.67243220e-01, 9.76645528e-01, 3.08876242e-01,
          8.19142068e-01, 9.69391866e-01]],

        [[4.44617360e-01, 5.02274334e-01, 2.32060554e-01,
          3.54429329e-01, 1.36471633e-01],
         [6.30717612e-01, 2.40955809e-01, 2.54390888e-01,
          7.20384123e-01, 3.42038201e-01],
         [8.20903975e-01, 6.73148542e-01, 8.67869039e-01,
          4.79726457e-01, 8.34115517e-01],
         [3.59797874e-01, 7.61370564e-01, 4.16116300e-01,
          7.46702020e-01, 6.39419576e-01]]]])
Coordinates:
  * longitude  (longitude) float64 -100.0 -99.0 -98.0 ... -83.0 -82.0 -81.0
  * latitude   (latitude) float64 30.0 31.0 32.0 33.0 ... 41.0 42.0 43.0 44.0
  * level      (level) int64 100 200 300 400
  * time       (time) datetime64[ns] 2021-01-01 ... 2021-01-01T04:00:00
[3]:
met = MetDataArray(da)
met
[3]:
MetDataArray with data:

<xarray.DataArray 'random' (longitude: 20, latitude: 15, level: 4, time: 5)>
array([[[[4.68307543e-01, 5.14342231e-01, 8.63988281e-01,
          7.19386871e-01, 3.33497882e-01],
         [8.81663616e-01, 5.18664864e-01, 5.23219417e-01,
          7.22388696e-01, 4.46782561e-01],
         [8.50357081e-01, 6.83936263e-01, 6.65900069e-01,
          9.46717074e-01, 7.53638850e-01],
         [9.17964165e-02, 8.26090137e-01, 5.55186777e-01,
          2.31152608e-01, 9.82958913e-01]],

        [[5.84613086e-01, 4.26518507e-01, 8.45336094e-01,
          5.07850470e-01, 7.10216629e-01],
         [1.37352673e-01, 4.84569777e-01, 8.89744202e-01,
          9.52976849e-01, 3.76043941e-02],
         [9.54223457e-01, 8.98512171e-01, 4.14081276e-01,
          4.20221061e-01, 6.91400341e-02],
         [4.21756867e-01, 4.93848391e-01, 1.51696284e-01,
          9.41137061e-01, 1.88116189e-01]],

        [[1.90065507e-02, 9.44932088e-01, 9.93219943e-01,
          6.81811974e-01, 7.84335294e-01],
...
         [8.92843751e-01, 4.07684237e-01, 3.05168469e-01,
          1.73576037e-01, 3.34321840e-01]],

        [[7.61263799e-01, 9.91563651e-01, 9.47543921e-01,
          6.68735461e-01, 2.71486792e-01],
         [2.05356394e-01, 7.42337468e-01, 2.70578729e-01,
          7.15520327e-01, 7.30134504e-01],
         [6.68886804e-01, 5.15651993e-01, 8.40987212e-01,
          5.37939968e-01, 3.71399439e-01],
         [1.67243220e-01, 9.76645528e-01, 3.08876242e-01,
          8.19142068e-01, 9.69391866e-01]],

        [[4.44617360e-01, 5.02274334e-01, 2.32060554e-01,
          3.54429329e-01, 1.36471633e-01],
         [6.30717612e-01, 2.40955809e-01, 2.54390888e-01,
          7.20384123e-01, 3.42038201e-01],
         [8.20903975e-01, 6.73148542e-01, 8.67869039e-01,
          4.79726457e-01, 8.34115517e-01],
         [3.59797874e-01, 7.61370564e-01, 4.16116300e-01,
          7.46702020e-01, 6.39419576e-01]]]])
Coordinates:
  * longitude     (longitude) float64 -100.0 -99.0 -98.0 ... -83.0 -82.0 -81.0
  * latitude      (latitude) float64 30.0 31.0 32.0 33.0 ... 41.0 42.0 43.0 44.0
  * level         (level) float64 100.0 200.0 300.0 400.0
  * time          (time) datetime64[ns] 2021-01-01 ... 2021-01-01T04:00:00
    air_pressure  (level) float64 1e+04 2e+04 3e+04 4e+04
    altitude      (level) float64 1.618e+04 1.178e+04 9.164e+03 7.185e+03
[4]:
# data attribute contains copy of the original xr.DataArray
isinstance(met.data, xr.DataArray)
[4]:
True
[5]:
# broadcast coords to the DataArray
met.broadcast_coords("air_pressure")
[5]:
<xarray.DataArray 'air_pressure' (longitude: 20, latitude: 15, level: 4, time: 5)>
array([[[[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        ...,

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
...
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        ...,

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]]]])
Coordinates:
  * longitude     (longitude) float64 -100.0 -99.0 -98.0 ... -83.0 -82.0 -81.0
  * latitude      (latitude) float64 30.0 31.0 32.0 33.0 ... 41.0 42.0 43.0 44.0
  * level         (level) float64 100.0 200.0 300.0 400.0
  * time          (time) datetime64[ns] 2021-01-01 ... 2021-01-01T04:00:00
    air_pressure  (level) float64 1e+04 2e+04 3e+04 4e+04
    altitude      (level) float64 1.618e+04 1.178e+04 9.164e+03 7.185e+03

MetDataset#

Thin wrapper around xr.Dataset

[6]:
ds = xr.Dataset(
    {
        "random": (["longitude", "latitude", "level", "time"], rng.random((20, 15, 4, 5))),
        "ones": (["longitude", "latitude", "level", "time"], np.ones((20, 15, 4, 5))),
    },
    coords={
        "longitude": np.arange(-100, -80, 1.0),
        "latitude": np.arange(30, 45, 1.0),
        "level": np.arange(100, 500, 100),
        "time": [datetime(2021, 1, 1, h) for h in range(5)],
    },
)
[7]:
ds
[7]:
<xarray.Dataset>
Dimensions:    (longitude: 20, latitude: 15, level: 4, time: 5)
Coordinates:
  * longitude  (longitude) float64 -100.0 -99.0 -98.0 ... -83.0 -82.0 -81.0
  * latitude   (latitude) float64 30.0 31.0 32.0 33.0 ... 41.0 42.0 43.0 44.0
  * level      (level) int64 100 200 300 400
  * time       (time) datetime64[ns] 2021-01-01 ... 2021-01-01T04:00:00
Data variables:
    random     (longitude, latitude, level, time) float64 0.08219 ... 0.3966
    ones       (longitude, latitude, level, time) float64 1.0 1.0 ... 1.0 1.0
[8]:
met = MetDataset(ds)
met
[8]:
MetDataset with data:

<xarray.Dataset>
Dimensions:       (longitude: 20, latitude: 15, level: 4, time: 5)
Coordinates:
  * longitude     (longitude) float64 -100.0 -99.0 -98.0 ... -83.0 -82.0 -81.0
  * latitude      (latitude) float64 30.0 31.0 32.0 33.0 ... 41.0 42.0 43.0 44.0
  * level         (level) float64 100.0 200.0 300.0 400.0
  * time          (time) datetime64[ns] 2021-01-01 ... 2021-01-01T04:00:00
    air_pressure  (level) float64 1e+04 2e+04 3e+04 4e+04
    altitude      (level) float64 1.618e+04 1.178e+04 9.164e+03 7.185e+03
Data variables:
    random        (longitude, latitude, level, time) float64 0.08219 ... 0.3966
    ones          (longitude, latitude, level, time) float64 1.0 1.0 ... 1.0 1.0
[9]:
# broadcast coords to the DataArray
met.broadcast_coords("air_pressure")
[9]:
<xarray.DataArray 'air_pressure' (longitude: 20, latitude: 15, level: 4, time: 5)>
array([[[[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        ...,

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
...
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        ...,

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]],

        [[10000., 10000., 10000., 10000., 10000.],
         [20000., 20000., 20000., 20000., 20000.],
         [30000., 30000., 30000., 30000., 30000.],
         [40000., 40000., 40000., 40000., 40000.]]]])
Coordinates:
  * longitude     (longitude) float64 -100.0 -99.0 -98.0 ... -83.0 -82.0 -81.0
  * latitude      (latitude) float64 30.0 31.0 32.0 33.0 ... 41.0 42.0 43.0 44.0
  * level         (level) float64 100.0 200.0 300.0 400.0
  * time          (time) datetime64[ns] 2021-01-01 ... 2021-01-01T04:00:00
    air_pressure  (level) float64 1e+04 2e+04 3e+04 4e+04
    altitude      (level) float64 1.618e+04 1.178e+04 9.164e+03 7.185e+03

MetVariable#

Dataclass to contain metadata for meteorology variables.

[10]:
mv = MetVariable(short_name="mv", standard_name="met_variable", long_name="Custom Met Variable")
mv
[10]:
MetVariable(short_name='mv', standard_name='met_variable', long_name='Custom Met Variable', level_type=None, ecmwf_id=None, grib1_id=None, grib2_id=None, units=None, amip=None, description=None)
[11]:
# attrs shorthand
mv.attrs
[11]:
{'short_name': 'mv',
 'standard_name': 'met_variable',
 'long_name': 'Custom Met Variable'}

Built-in Variables#

pycontrails contains many standard met variables in the pycontrails.core.met_var module.

The standard_name attribute is used by default as string references through the repository.

[12]:
from pycontrails.core.met_var import (
    AirTemperature,
    SpecificHumidity,
)
[13]:
AirTemperature.attrs
[13]:
{'short_name': 't',
 'standard_name': 'air_temperature',
 'long_name': 'Air Temperature',
 'units': 'K'}
[14]:
SpecificHumidity.attrs
[14]:
{'short_name': 'q',
 'standard_name': 'specific_humidity',
 'long_name': 'Specific Humidity',
 'units': 'kg kg**-1'}