list-of-dict: converting to Xarray

This example demonstrates how list-of-dicts fieldlists can be converted into Xarray.

Data containing geography

[1]:
import earthkit.data as ekd

prototype = {
    "geography": {"latitudes": [10.0, 0.0, -10.0], "longitudes": [20, 40.0]},
    "values": [1, 2, 3, 4, 5, 6],
    "time": {"valid_datetime": "2018-08-01T09:00:00Z"},
}


d = [
    {"parameter": {"variable": "t"}, "vertical": {"level": 500}, **prototype},
    {"parameter": {"variable": "t"}, "vertical": {"level": 850}, **prototype},
    {"parameter": {"variable": "u"}, "vertical": {"level": 500}, **prototype},
    {"parameter": {"variable": "u"}, "vertical": {"level": 850}, **prototype},
]

ds = ekd.from_source("list-of-dicts", d)
ds.to_xarray()
[1]:
<xarray.Dataset> Size: 248B
Dimensions:    (level: 2, latitude: 3, longitude: 2)
Coordinates:
  * level      (level) int64 16B 500 850
  * latitude   (latitude) float64 24B 10.0 0.0 -10.0
  * longitude  (longitude) float64 16B 20.0 40.0
Data variables:
    t          (level, latitude, longitude) float64 96B ...
    u          (level, latitude, longitude) float64 96B ...
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF

Data without geography

[2]:
prototype = {
    "values": [1, 2, 3, 4, 5, 6],
    "time": {"valid_datetime": "2018-08-01T09:00:00Z"},
}


d = [
    {"parameter": {"variable": "t"}, "vertical": {"level": 500}, **prototype},
    {"parameter": {"variable": "t"}, "vertical": {"level": 850}, **prototype},
    {"parameter": {"variable": "u"}, "vertical": {"level": 500}, **prototype},
    {"parameter": {"variable": "u"}, "vertical": {"level": 850}, **prototype},
]

ds = ekd.from_source("list-of-dicts", d)
ds.to_xarray()
[2]:
<xarray.Dataset> Size: 208B
Dimensions:  (level: 2, values: 6)
Coordinates:
  * level    (level) int64 16B 500 850
Dimensions without coordinates: values
Data variables:
    t        (level, values) float64 96B ...
    u        (level, values) float64 96B ...
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF
[ ]: