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 = {
        "latitudes": [10.0, 0.0, -10.0],
        "longitudes": [20, 40.0],
        "values": [1, 2, 3, 4, 5, 6],
        "valid_datetime": "2018-08-01T09:00:00Z",
    }

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

ds = ekd.from_source("list-of-dicts", d)
ds.to_xarray()
[1]:
<xarray.Dataset> Size: 248B
Dimensions:    (levelist: 2, latitude: 3, longitude: 2)
Coordinates:
  * levelist   (levelist) 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          (levelist, latitude, longitude) float64 96B ...
    u          (levelist, latitude, longitude) float64 96B ...
Attributes:
    Conventions:  CF-1.8
    institution:  ECMWF

Data without geography

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

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

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