Load ODIM_H5 Volume data from German Weather Service

In this example, we obtain and read the latest 2 hours of available volumetric radar data from German Weather Service available at opendata.dwd.de. Finally we do some plotting.

This retrieves 24 timesteps of the 10 sweeps (moments DBZH and VRADH) of the DWD volume scan of a distinct radar. This amounts to 240 data files which are combined into one volumetric Cf/Radial2 like xarray powered structure.

Exports to single file Odim_H5 and Cf/Radial2 format are shown at the end of this tutorial.

Note

The used open_odim implementation is based on netcdf4, h5py, h5netcdf and Xarray. It claims multiple data files and presents them in a simple structure. See also the notebook wradlib_odim_multi_file_dataset for further details.

[1]:
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.xarray import open_odim, CfRadial
[2]:
import urllib3
import os
import io
import glob
import shutil
import datetime
from itertools import chain

Download radar volumes of latest 2 hours from server using wetterdienst

wetterdienst is a neat package for easy retrieval of data primarily from DWD. For further information have a look at their documentation.

[3]:
from wetterdienst.dwd.radar import DWDRadarData, DWDRadarParameter, DWDRadarDate, DWDRadarDataFormat, DWDRadarDataSubset
from wetterdienst.dwd.radar.sites import DWDRadarSite
[4]:
start_date = datetime.datetime.utcnow()

# Horizontal Doppler Velocity
request_velocity = DWDRadarData(
    parameter=DWDRadarParameter.SWEEP_VOL_VELOCITY_H,
    start_date=start_date - datetime.timedelta(hours=2),
    end_date=start_date,
    site=DWDRadarSite.ESS,
    fmt=DWDRadarDataFormat.HDF5,
    subset=DWDRadarDataSubset.POLARIMETRIC,
)

# Horizontal Reflectivity
request_reflectivity = DWDRadarData(
    parameter=DWDRadarParameter.SWEEP_VOL_REFLECTIVITY_H,
    start_date=start_date - datetime.timedelta(hours=2),
    end_date=start_date,
    site=DWDRadarSite.ESS,fmt=DWDRadarDataFormat.HDF5,
    subset=DWDRadarDataSubset.POLARIMETRIC,
)

# Submit requests.
results = chain(request_velocity.collect_data(), request_reflectivity.collect_data())

Acquire data as memory buffer

[5]:
# Collect in-memory data
files = []
for item in results:
    files.append(item.data)

Read the data into xarray powered structure

[6]:
# Decode data using wradlib.
vol = wrl.io.open_odim(files)
Open: 100%|██████████| 485/485 [00:00<00:00, 520.55 Files/s]
Collecting: 100%|██████████| 10/10 [00:00<00:00, 133.23 Angles/s]

Inspect structure

Root Group

[7]:
vol.root
[7]:
<xarray.Dataset>
Dimensions:              (sweep: 10)
Dimensions without coordinates: sweep
Data variables:
    volume_number        int64 0
    platform_type        <U5 'fixed'
    instrument_type      <U5 'radar'
    primary_axis         <U6 'axis_z'
    time_coverage_start  <U20 '2020-11-25T11:15:35Z'
    time_coverage_end    <U20 '2020-11-25T13:16:44Z'
    latitude             float64 51.41
    longitude            float64 6.967
    altitude             float64 185.1
    sweep_group_name     (sweep) <U7 'sweep_0' 'sweep_1' ... 'sweep_8' 'sweep_9'
    sweep_fixed_angle    (sweep) float64 5.5 4.5 3.5 2.5 ... 8.0 12.0 17.0 25.0
Attributes:
    version:          H5rad 2.2
    title:            None
    institution:      None
    references:       None
    source:           None
    history:          None
    comment:          im/exported using wradlib
    instrument_name:  None
    Conventions:      ODIM_H5/V2_2
[8]:
vol.root.sweep_fixed_angle
[8]:
<xarray.DataArray 'sweep_fixed_angle' (sweep: 10)>
array([ 5.5,  4.5,  3.5,  2.5,  1.5,  0.5,  8. , 12. , 17. , 25. ])
Dimensions without coordinates: sweep

Sweep Groups

[9]:
list(vol)
[9]:
[<wradlib.XRadTimeSeries>
 Dimension(s): (time: 25, azimuth: 360, range: 180)
 Elevation(s): (5.5),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 25, azimuth: 360, range: 180)
 Elevation(s): (4.5),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 25, azimuth: 360, range: 180)
 Elevation(s): (3.5),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 24, azimuth: 360, range: 180)
 Elevation(s): (2.5),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 24, azimuth: 360, range: 180)
 Elevation(s): (1.5),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 24, azimuth: 360, range: 180)
 Elevation(s): (0.5),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 24, azimuth: 360, range: 124)
 Elevation(s): (8.0),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 24, azimuth: 360, range: 60)
 Elevation(s): (12.0),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 24, azimuth: 360, range: 60)
 Elevation(s): (17.0),
 <wradlib.XRadTimeSeries>
 Dimension(s): (time: 24, azimuth: 360, range: 60)
 Elevation(s): (25.0)]
[10]:
vol[0]
[10]:
<wradlib.XRadTimeSeries>
Dimension(s): (time: 25, azimuth: 360, range: 180)
Elevation(s): (5.5)

plot sweeps

DBZH

[11]:
fig = pl.figure(figsize=(20, 30))
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(4, 3, wspace=0.4, hspace=0.4)
for i, ts in enumerate(vol):
    swp = ts[0]
    swp.data.DBZH.pipe(wrl.georef.georeference_dataset).wradlib.plot(ax=gs[i], fig=fig)
    ax = pl.gca()
    ax.set_title(vol.root.sweep_fixed_angle[i].values)
../../_images/notebooks_fileio_wradlib_load_DWD_opendata_volumes_18_0.png

VRADH

[12]:
fig = pl.figure(figsize=(20, 30))
import matplotlib.gridspec as gridspec
gs = gridspec.GridSpec(4, 3, wspace=0.4, hspace=0.4)
for i, ts in enumerate(vol):
    swp = ts[0]
    swp.data.VRADH.pipe(wrl.georef.georeference_dataset).wradlib.plot(ax=gs[i], fig=fig)
    ax = pl.gca()
    ax.set_title(vol.root.sweep_fixed_angle[i].values)
../../_images/notebooks_fileio_wradlib_load_DWD_opendata_volumes_20_0.png

Plot single sweep using cartopy

[13]:
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cfeature

map_trans = ccrs.AzimuthalEquidistant(central_latitude=vol.root.latitude.values,
                                      central_longitude=vol.root.longitude.values)
[14]:
vol[-1].data
Collecting: 100%|██████████| 24/24 [00:00<00:00, 32.58 Timesteps/s]
[14]:
<xarray.Dataset>
Dimensions:     (azimuth: 360, range: 60, time: 24)
Coordinates:
  * azimuth     (azimuth) float64 0.5 1.5 2.5 3.5 ... 356.5 357.5 358.5 359.5
  * time        (time) datetime64[ns] 2020-11-25T11:18:50 ... 2020-11-25T13:1...
    elevation   (azimuth) float64 25.03 25.03 25.03 25.03 ... 25.03 25.03 25.03
    rtime       (time, azimuth) datetime64[ns] 2020-11-25T11:18:54.716000 ......
  * range       (range) float32 500.0 1500.0 2500.0 ... 57500.0 58500.0 59500.0
    sweep_mode  <U20 'azimuth_surveillance'
    latitude    float64 51.41
    longitude   float64 6.967
    altitude    float64 185.1
Data variables:
    DBZH        (time, azimuth, range) float32 -64.00293 -64.00293 ... -64.00293
    VRADH       (time, azimuth, range) float32 -32.269188 ... -32.269188
[15]:
map_proj = ccrs.AzimuthalEquidistant(central_latitude=vol.root.latitude.values,
                                      central_longitude=vol.root.longitude.values)
pm = vol[-1][0].data.pipe(wrl.georef.georeference_dataset).DBZH.wradlib.plot_ppi(proj=map_proj)
ax = pl.gca()
ax.gridlines(crs=map_proj)
print(ax)
< GeoAxes: <cartopy.crs.AzimuthalEquidistant object at 0x7fd77d50e400> >
../../_images/notebooks_fileio_wradlib_load_DWD_opendata_volumes_24_1.png
[16]:
map_proj = ccrs.Mercator(central_longitude=vol.root.longitude.values)
fig = pl.figure(figsize=(10,8))
ax = fig.add_subplot(111, projection=map_proj)
pm = vol[-1][0].data.pipe(wrl.georef.georeference_dataset).DBZH.wradlib.plot_ppi(ax=ax)
ax.gridlines(draw_labels=True)
[16]:
<cartopy.mpl.gridliner.Gridliner at 0x7fd77d347220>
../../_images/notebooks_fileio_wradlib_load_DWD_opendata_volumes_25_1.png
[17]:
fig = pl.figure(figsize=(10, 8))
proj=ccrs.AzimuthalEquidistant(central_latitude=vol.root.latitude.values,
                               central_longitude=vol.root.longitude.values)
ax = fig.add_subplot(111, projection=proj)
pm = vol[-1][0].data.DBZH.wradlib.plot_ppi(ax=ax)
ax.gridlines()
[17]:
<cartopy.mpl.gridliner.Gridliner at 0x7fd77d22a9d0>
../../_images/notebooks_fileio_wradlib_load_DWD_opendata_volumes_26_1.png

Inspect radar moments

The dataarrays can be accessed by key or by attribute. Each dataarray has the datasets dimensions and coordinates of it’s parent dataset. There are attributes connected which are defined by Cf/Radial and/or ODIM_H5 standard.

[18]:
vol[-1][0].data.DBZH
[18]:
<xarray.DataArray 'DBZH' (azimuth: 360, range: 60)>
array([[-64.00293, -64.00293, -64.00293, ..., -64.00293, -64.00293, -64.00293],
       [-64.00293, -64.00293, -64.00293, ..., -64.00293, -64.00293, -64.00293],
       [-64.00293, -64.00293, -64.00293, ..., -64.00293, -64.00293, -64.00293],
       ...,
       [-64.00293, -64.00293, -64.00293, ..., -64.00293, -64.00293, -64.00293],
       [-64.00293, -64.00293, -64.00293, ..., -64.00293, -64.00293, -64.00293],
       [-64.00293, -64.00293, -64.00293, ..., -64.00293, -64.00293, -64.00293]],
      dtype=float32)
Coordinates:
  * azimuth     (azimuth) float64 0.5 1.5 2.5 3.5 ... 356.5 357.5 358.5 359.5
    time        datetime64[ns] ...
    elevation   (azimuth) float64 ...
    rtime       (azimuth) datetime64[ns] ...
  * range       (range) float32 500.0 1500.0 2500.0 ... 57500.0 58500.0 59500.0
    sweep_mode  <U20 ...
    latitude    float64 ...
    longitude   float64 ...
    altitude    float64 ...
    x           (azimuth, range) float64 3.953 11.86 19.77 ... -461.2 -469.1
    y           (azimuth, range) float64 453.0 1.359e+03 ... 5.285e+04 5.375e+04
    z           (azimuth, range) float64 396.5 819.5 ... 2.51e+04 2.553e+04
    gr          (azimuth, range) float64 453.0 1.359e+03 ... 5.285e+04 5.375e+04
    rays        (azimuth, range) float64 0.5 0.5 0.5 0.5 ... 359.5 359.5 359.5
    bins        (azimuth, range) float32 500.0 1500.0 2500.0 ... 58500.0 59500.0
Attributes:
    _Undetect:      0.0
    units:          dBZ
    long_name:      Equivalent reflectivity factor H
    standard_name:  radar_equivalent_reflectivity_factor_h
[19]:
vol[-1][0].data.sweep_mode
[19]:
<xarray.DataArray 'sweep_mode' ()>
array('azimuth_surveillance', dtype='<U20')
Coordinates:
    time        datetime64[ns] ...
    sweep_mode  <U20 ...
    latitude    float64 ...
    longitude   float64 ...
    altitude    float64 ...
[20]:
vol.root
[20]:
<xarray.Dataset>
Dimensions:              (sweep: 10)
Dimensions without coordinates: sweep
Data variables:
    volume_number        int64 0
    platform_type        <U5 'fixed'
    instrument_type      <U5 'radar'
    primary_axis         <U6 'axis_z'
    time_coverage_start  <U20 '2020-11-25T11:15:35Z'
    time_coverage_end    <U20 '2020-11-25T13:16:44Z'
    latitude             float64 51.41
    longitude            float64 6.967
    altitude             float64 185.1
    sweep_group_name     (sweep) <U7 'sweep_0' 'sweep_1' ... 'sweep_8' 'sweep_9'
    sweep_fixed_angle    (sweep) float64 5.5 4.5 3.5 2.5 ... 8.0 12.0 17.0 25.0
Attributes:
    version:          H5rad 2.2
    title:            None
    institution:      None
    references:       None
    source:           None
    history:          None
    comment:          im/exported using wradlib
    instrument_name:  None
    Conventions:      ODIM_H5/V2_2

Plot Quasi Vertical Profile

[21]:
vol
[21]:
<wradlib.XRadVolume>
Dimension(s): (sweep: 10)
Elevation(s): (5.5, 4.5, 3.5, 2.5, 1.5, 0.5, 8.0, 12.0, 17.0, 25.0)
[22]:
ts = vol[-1]
ts
[22]:
<wradlib.XRadTimeSeries>
Dimension(s): (time: 24, azimuth: 360, range: 60)
Elevation(s): (25.0)
[23]:
fig = pl.figure(figsize=(10, 4))
ax = fig.add_subplot(111)
ts.data.DBZH.median('azimuth').plot(x='time', vmin=-10, vmax=30, ax=ax)
ax.set_title(f"{np.datetime_as_string(ts.data.time[0].values, unit='D')}")
ax.set_ylim(0, 20000)
[23]:
(0.0, 20000.0)
../../_images/notebooks_fileio_wradlib_load_DWD_opendata_volumes_34_1.png

Export to OdimH5

This exports the radar volume at given timestep including all moments into one ODIM_H5 compliant data file.

[24]:
vol.to_odim('dwd_odim.h5', timestep=0)

Export to Cf/Radial2

This exports the radar volume at given timestep including all moments into one Cf/Radial2 compliant data file.

[25]:
vol.to_cfradial2('dwd_cfradial2.nc', timestep=0)

Import again and check equality

Small time differences are possible so drop times before comparison

[26]:
try:
    vol1 = open_odim('dwd_odim.h5')
    vol2 = CfRadial('dwd_cfradial2.nc', dim0='azimuth')
    xr.testing.assert_equal(vol1.root, vol2.root)
    xr.testing.assert_equal(vol1[1][0].data.drop('time').rename({'rtime': 'time'}), vol2['sweep_1'])
    print("Equality Check successful")
except:
    print("Equality Check not successful")
Open: 100%|██████████| 1/1 [00:00<00:00, 51.36 Files/s]
Collecting: 100%|██████████| 10/10 [00:00<00:00, 2144.33 Angles/s]
Equality Check not successful