xarray ODIM backend#

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

[1]:
import glob
import os
import wradlib as wrl
import warnings

warnings.filterwarnings("ignore")
import matplotlib.pyplot as pl
import numpy as np
import xradar as xd
import datatree as xt
import xarray as xr

try:
    get_ipython().run_line_magic("matplotlib inline")
except:
    pl.ion()

Load ODIM_H5 Volume Data#

[2]:
fpath = "hdf5/knmi_polar_volume.h5"
f = wrl.util.get_wradlib_data_file(fpath)
vol = xd.io.open_odim_datatree(f)
Downloading file 'hdf5/knmi_polar_volume.h5' from 'https://github.com/wradlib/wradlib-data/raw/pooch/data/hdf5/knmi_polar_volume.h5' to '/home/runner/work/wradlib/wradlib/wradlib-data'.

Inspect RadarVolume#

[3]:
display(vol)
<xarray.DatasetView>
Dimensions:              ()
Data variables:
    volume_number        int64 0
    platform_type        <U5 'fixed'
    instrument_type      <U5 'radar'
    time_coverage_start  <U20 '2011-06-10T11:40:02Z'
    time_coverage_end    <U20 '2011-06-10T11:43:54Z'
    longitude            float32 4.79
    altitude             float32 50.0
    latitude             float32 52.95
Attributes:
    Conventions:      ODIM_H5/V2_0
    version:          None
    title:            None
    institution:      None
    references:       None
    source:           None
    history:          None
    comment:          im/exported using xradar
    instrument_name:  None

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).

[4]:
vol.root
[4]:
<xarray.DatasetView>
Dimensions:              ()
Data variables:
    volume_number        int64 0
    platform_type        <U5 'fixed'
    instrument_type      <U5 'radar'
    time_coverage_start  <U20 '2011-06-10T11:40:02Z'
    time_coverage_end    <U20 '2011-06-10T11:43:54Z'
    longitude            float32 4.79
    altitude             float32 50.0
    latitude             float32 52.95
Attributes:
    Conventions:      ODIM_H5/V2_0
    version:          None
    title:            None
    institution:      None
    references:       None
    source:           None
    history:          None
    comment:          im/exported using xradar
    instrument_name:  None

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.).

[5]:
display(vol["sweep_0"])
<xarray.DatasetView>
Dimensions:            (azimuth: 360, range: 320)
Coordinates:
  * azimuth            (azimuth) float32 0.5 1.5 2.5 3.5 ... 357.5 358.5 359.5
    elevation          (azimuth) float32 ...
    time               (azimuth) datetime64[ns] 2011-06-10T11:40:17.361118208...
  * range              (range) float32 500.0 1.5e+03 ... 3.185e+05 3.195e+05
    longitude          float32 ...
    latitude           float32 ...
    altitude           float32 ...
Data variables:
    DBZH               (azimuth, range) float32 ...
    sweep_mode         <U20 ...
    sweep_number       int64 ...
    prt_mode           <U7 ...
    follow_mode        <U7 ...
    sweep_fixed_angle  float32 ...

Goereferencing#

[6]:
swp = vol["sweep_0"].ds
swp = swp.assign_coords(sweep_mode=swp.sweep_mode)
swp = swp.pipe(wrl.georef.georeference_dataset)

Plotting#

[7]:
swp.DBZH.plot.pcolormesh(x="x", y="y")
pl.gca().set_aspect("equal")
../../_images/notebooks_fileio_wradlib_odim_backend_14_0.png
[8]:
fig = pl.figure(figsize=(10, 10))
swp.DBZH.wrl.vis.plot(proj="cg", fig=fig)
[8]:
<matplotlib.collections.QuadMesh at 0x7f0b7008c8d0>
../../_images/notebooks_fileio_wradlib_odim_backend_15_1.png
[9]:
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
)
[10]:
map_proj = ccrs.AzimuthalEquidistant(
    central_latitude=swp.latitude.values, central_longitude=swp.longitude.values
)
pm = swp.DBZH.wrl.vis.plot(proj=map_proj)
ax = pl.gca()
ax.gridlines(crs=map_proj)
print(ax)
< GeoAxes: +proj=aeqd +ellps=WGS84 +lon_0=4.7899699211120605 +lat_0=52.953338623046875 +x_0=0.0 +y_0=0.0 +no_defs +type=crs >
../../_images/notebooks_fileio_wradlib_odim_backend_17_1.png
[11]:
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.wrl.vis.plot(ax=ax)
ax.gridlines(draw_labels=True)
[11]:
<cartopy.mpl.gridliner.Gridliner at 0x7f0b726b79d0>
../../_images/notebooks_fileio_wradlib_odim_backend_18_1.png
[12]:
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).wrl.vis.plot(ax=ax)
plot_borders(ax)
ax.gridlines(draw_labels=True)
[12]:
<cartopy.mpl.gridliner.Gridliner at 0x7f0b62e27cd0>
../../_images/notebooks_fileio_wradlib_odim_backend_19_1.png
[13]:
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.wrl.vis.plot(proj=map_proj, ax=ax)
ax = pl.gca()
ax.gridlines(crs=map_proj)
[13]:
<cartopy.mpl.gridliner.Gridliner at 0x7f0b727934d0>
../../_images/notebooks_fileio_wradlib_odim_backend_20_1.png
[14]:
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.wrl.vis.plot(ax=ax)
ax.gridlines()
[14]:
<cartopy.mpl.gridliner.Gridliner at 0x7f0b72df5510>
../../_images/notebooks_fileio_wradlib_odim_backend_21_1.png
[15]:
swp.DBZH.wrl.vis.plot()
[15]:
<matplotlib.collections.QuadMesh at 0x7f0b702e9210>
../../_images/notebooks_fileio_wradlib_odim_backend_22_1.png

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.

[16]:
display(swp.DBZH)
<xarray.DataArray 'DBZH' (azimuth: 360, range: 320)>
array([[ 22. ,  17. ,  -8. , ..., -31.5, -31.5, -31.5],
       [ 24. ,  24.5,  -9. , ..., -31.5, -31.5, -31.5],
       [ 35.5,  42. ,  12. , ..., -31.5, -31.5, -31.5],
       ...,
       [ 23. ,  14. , -13. , ..., -31.5, -31.5, -31.5],
       [ 23. ,  14. ,  -9. , ..., -31.5, -31.5, -31.5],
       [ 22. ,  18.5, -11.5, ..., -31.5, -31.5, -31.5]], dtype=float32)
Coordinates: (12/14)
  * azimuth     (azimuth) float32 0.5 1.5 2.5 3.5 ... 356.5 357.5 358.5 359.5
    elevation   (azimuth) float32 0.3 0.3 0.3 0.3 0.3 ... 0.3 0.3 0.3 0.3 0.3
    time        (azimuth) datetime64[ns] 2011-06-10T11:40:17.361118208 ... 20...
  * range       (range) float32 500.0 1.5e+03 2.5e+03 ... 3.185e+05 3.195e+05
    sweep_mode  <U20 'azimuth_surveillance'
    longitude   float32 4.79
    ...          ...
    x           (azimuth, range) float32 4.363 13.09 ... -2.777e+03 -2.786e+03
    y           (azimuth, range) float32 500.0 1.5e+03 ... 3.183e+05 3.193e+05
    z           (azimuth, range) float32 53.0 58.0 64.0 ... 7.691e+03 7.734e+03
    gr          (azimuth, range) float32 500.0 1.5e+03 ... 3.183e+05 3.193e+05
    rays        (azimuth, range) float32 0.5 0.5 0.5 0.5 ... 359.5 359.5 359.5
    bins        (azimuth, range) float32 500.0 1.5e+03 ... 3.185e+05 3.195e+05
Attributes:
    _Undetect:      0.0
    units:          dBZ
    long_name:      Equivalent reflectivity factor H
    standard_name:  radar_equivalent_reflectivity_factor_h

Create simple plot#

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

[17]:
swp.DBZH.sortby("time").plot(x="range", y="time", add_labels=False)
[17]:
<matplotlib.collections.QuadMesh at 0x7f0b702cbe50>
../../_images/notebooks_fileio_wradlib_odim_backend_26_1.png
[18]:
fig = pl.figure(figsize=(5, 5))
pm = swp.DBZH.wrl.vis.plot(proj={"latmin": 33e3}, fig=fig)
../../_images/notebooks_fileio_wradlib_odim_backend_27_0.png

Mask some values#

[19]:
swp["DBZH"] = swp["DBZH"].where(swp["DBZH"] >= 0)
swp["DBZH"].plot()
[19]:
<matplotlib.collections.QuadMesh at 0x7f0b62d27f10>
../../_images/notebooks_fileio_wradlib_odim_backend_29_1.png

Export to ODIM and CfRadial2#

[20]:
xd.io.to_odim(vol.copy(), "knmi_odim.h5")
[21]:
vol["sweep_0"].to_dataset().swap_dims(azimuth="time")
[21]:
<xarray.Dataset>
Dimensions:            (time: 360, range: 320)
Coordinates:
    azimuth            (time) float32 0.5 1.5 2.5 3.5 ... 357.5 358.5 359.5
    elevation          (time) float32 ...
  * time               (time) datetime64[ns] 2011-06-10T11:40:17.361118208 .....
  * range              (range) float32 500.0 1.5e+03 ... 3.185e+05 3.195e+05
    longitude          float32 ...
    latitude           float32 ...
    altitude           float32 ...
Data variables:
    DBZH               (time, range) float32 ...
    sweep_mode         <U20 ...
    sweep_number       int64 ...
    prt_mode           <U7 ...
    follow_mode        <U7 ...
    sweep_fixed_angle  float32 ...
[22]:
xd.io.to_cfradial2(vol.copy(), "knmi_odim_as_cfradial2.nc")

Import again#

[23]:
vola = xd.io.open_odim_datatree("knmi_odim.h5")
display(vola)
<xarray.DatasetView>
Dimensions:              ()
Data variables:
    volume_number        int64 0
    platform_type        <U5 'fixed'
    instrument_type      <U5 'radar'
    time_coverage_start  <U20 '2011-06-10T11:40:02Z'
    time_coverage_end    <U20 '2011-06-10T11:43:54Z'
    longitude            float32 4.79
    altitude             float32 50.0
    latitude             float32 52.95
Attributes:
    Conventions:      ODIM_H5/V2_2
    version:          None
    title:            None
    institution:      None
    references:       None
    source:           None
    history:          None
    comment:          im/exported using xradar
    instrument_name:  None