xarray Rainbow5 backend

In this example, we read Rainbow5 data files using the wradlib rainbow xarray backend.

[ ]:
import glob
import gzip
import io
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()

Load Rainbow5 Volume Data

[ ]:
fpath = 'rainbow/2013051000000600dBZ.vol'
f = wrl.util.get_wradlib_data_file(fpath)
vol = wrl.io.open_rainbow_dataset(f, reindex_angle=False)

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.

[ ]:
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': 3e3}, fig=fig)

Mask some values

[ ]:
dbzh = swp['DBZH'].where(swp['DBZH'] >= 0)
dbzh.plot(x="x", y="y")
[ ]:
vol[0]

Export to ODIM and CfRadial2

[ ]:
vol[0].DBZH.sortby("rtime").plot(y="rtime")
[ ]:
vol.to_odim('rainbow_as_odim.h5')
vol.to_cfradial2('rainbow_as_cfradial2.nc')

Import again

[ ]:
vola = wrl.io.open_odim_dataset('rainbow_as_odim.h5', reindex_angle=False, keep_elevation=True)
display(vola.root)
display(vola[0])
vola[0].DBZH.sortby("rtime").plot(y="rtime")
[ ]:
volb = wrl.io.open_cfradial2_dataset('rainbow_as_cfradial2.nc')
display(volb.root)
display(volb[0])
volb[0].DBZH.sortby("rtime").plot(y="rtime")

Check equality

We have to sort accordingly and drop the time variables when checking equality.

[ ]:
xr.testing.assert_allclose(vol.root, vola.root)
xr.testing.assert_allclose(vol[0].drop(["rtime", "time"]), vola[0].sortby("rtime").drop(["rtime", "time"]))
xr.testing.assert_allclose(vol.root.drop("time"), volb.root.drop("time"))
xr.testing.assert_allclose(vol[0].drop(["rtime", "time"]), volb[0].sortby("rtime").drop(["rtime", "time"]))
xr.testing.assert_allclose(vola.root, volb.root)
xr.testing.assert_allclose(vola[0].drop("rtime"), volb[0].drop(["rtime"]))

More Rainbow5 loading mechanisms

Use xr.open_dataset to retrieve explicit group

[ ]:
swp = xr.open_dataset(f, engine="rainbow", group=5, backend_kwargs=dict(reindex_angle=False))
display(swp)