xarray ODIM backend

In this example, we read ODIM_H5 (HDF5) data files using the xarray odim backend.

[ ]:
import glob
import os
import wradlib as wrl
import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as pl
import numpy as np
import xarray as xr
try:
    get_ipython().magic("matplotlib inline")
except:
    pl.ion()
from wradlib.io import open_odim_dataset

Load ODIM_H5 Volume Data

[ ]:
fpath = 'hdf5/knmi_polar_volume.h5'
f = wrl.util.get_wradlib_data_file(fpath)
vol = wrl.io.open_odim_dataset(f)

Inspect RadarVolume

[ ]:
display(vol)

Inspect root group

The sweep dimension contains the number of scans in this radar volume. Further the dataset consists of variables (location coordinates, time_coverage) and attributes (Conventions, metadata).

[ ]:
vol.root

Inspect sweep group(s)

The sweep-groups can be accessed via their respective keys. The dimensions consist of range and time with added coordinates azimuth, elevation, range and time. There will be variables like radar moments (DBZH etc.) and sweep-dependend metadata (like fixed_angle, sweep_mode etc.).

[ ]:
display(vol[0])

Goereferencing

[ ]:
swp = vol[0].copy().pipe(wrl.georef.georeference_dataset)

Plotting

[ ]:
swp.DBZH.plot.pcolormesh(x='x', y='y')
pl.gca().set_aspect('equal')
[ ]:
fig = pl.figure(figsize=(10,10))
swp.DBZH.wradlib.plot_ppi(proj='cg', fig=fig)
[ ]:
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cfeature

map_trans = ccrs.AzimuthalEquidistant(central_latitude=swp.latitude.values,
                                      central_longitude=swp.longitude.values)
[ ]:
map_proj = ccrs.AzimuthalEquidistant(central_latitude=swp.latitude.values,
                                      central_longitude=swp.longitude.values)
pm = swp.DBZH.wradlib.plot_ppi(proj=map_proj)
ax = pl.gca()
ax.gridlines(crs=map_proj)
print(ax)
[ ]:
map_proj = ccrs.Mercator(central_longitude=swp.longitude.values)
fig = pl.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection=map_proj)
pm = swp.DBZH.wradlib.plot_ppi(ax=ax)
ax.gridlines(draw_labels=True)
[ ]:
import cartopy.feature as cfeature
def plot_borders(ax):
    borders = cfeature.NaturalEarthFeature(category='physical',
                                           name='coastline',
                                           scale='10m',
                                           facecolor='none')
    ax.add_feature(borders, edgecolor='black', lw=2, zorder=4)

map_proj = ccrs.Mercator(central_longitude=swp.longitude.values)
fig = pl.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection=map_proj)

DBZH = swp.DBZH
pm = DBZH.where(DBZH > 0).wradlib.plot_ppi(ax=ax)
plot_borders(ax)
ax.gridlines(draw_labels=True)
[ ]:
import matplotlib.path as mpath
theta = np.linspace(0, 2*np.pi, 100)
center, radius = [0.5, 0.5], 0.5
verts = np.vstack([np.sin(theta), np.cos(theta)]).T
circle = mpath.Path(verts * radius + center)

map_proj = ccrs.AzimuthalEquidistant(central_latitude=swp.latitude.values,
                                     central_longitude=swp.longitude.values,
                                    )
fig = pl.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection=map_proj)
ax.set_boundary(circle, transform=ax.transAxes)

pm = swp.DBZH.wradlib.plot_ppi(proj=map_proj, ax=ax)
ax = pl.gca()
ax.gridlines(crs=map_proj)
[ ]:
fig = pl.figure(figsize=(10, 8))
proj=ccrs.AzimuthalEquidistant(central_latitude=swp.latitude.values,
                               central_longitude=swp.longitude.values)
ax = fig.add_subplot(111, projection=proj)
pm = swp.DBZH.wradlib.plot_ppi(ax=ax)
ax.gridlines()
[ ]:
swp.DBZH.wradlib.plot_ppi()

Inspect radar moments

The DataArrays can be accessed by key or by attribute. Each DataArray has dimensions and coordinates of it’s parent dataset. There are attributes connected which are defined by ODIM_H5 standard.

[ ]:
display(swp.DBZH)

Create simple plot

Using xarray features a simple plot can be created like this. Note the sortby('rtime') method, which sorts the radials by time.

[ ]:
swp.DBZH.sortby('rtime').plot(x="range", y="rtime", add_labels=False)
[ ]:
fig = pl.figure(figsize=(5,5))
pm = swp.DBZH.wradlib.plot_ppi(proj={'latmin': 33e3}, fig=fig)

Mask some values

[ ]:
swp['DBZH'] = swp['DBZH'].where(swp['DBZH'] >= 0)
swp['DBZH'].plot()

Export to ODIM and CfRadial2

[ ]:
vol.to_odim('knmi_odim.h5')
vol.to_cfradial2('knmi_odim_as_cfradial.nc')

Import again

[ ]:
vola = wrl.io.open_odim_dataset('knmi_odim.h5')
[ ]:
volb = wrl.io.open_cfradial2_dataset('knmi_odim_as_cfradial.nc')

Check equality

[ ]:
xr.testing.assert_allclose(vol.root, vola.root)
xr.testing.assert_equal(vol[0], vola[0])
xr.testing.assert_allclose(vol.root, volb.root)
xr.testing.assert_equal(vol[0], volb[0])
xr.testing.assert_allclose(vola.root, volb.root)
xr.testing.assert_equal(vola[0], volb[0])

More ODIM loading mechanisms

Use xr.open_dataset to retrieve explicit group

[ ]:
swp = xr.open_dataset(f, engine="odim", group="dataset14")
display(swp)

Use xr.open_mfdataset to retrieve timeseries of explicit group

[ ]:
fpath = os.path.join(wrl.util.get_wradlib_data_path(), "hdf5/71*.h5")
f = glob.glob(fpath)
ts = xr.open_mfdataset(f, engine="odim", concat_dim="time", combine="nested", group="dataset1")
display(ts)

Use wrl.io.open_odim_mfdataset to retrieve volume timeseries

[ ]:
fpath = os.path.join(wrl.util.get_wradlib_data_path(), "hdf5/71*.h5")
f = glob.glob(fpath)
ts = wrl.io.open_odim_mfdataset(f)
display(ts)
[ ]:
display(ts[0])