xarray CfRadial2 backend#

In this example, we read CfRadial2 data files using the xarray cfradial2 backend.

[1]:
import wradlib as wrl
import warnings

warnings.filterwarnings("ignore")
import matplotlib.pyplot as plt
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:
    plt.ion()

Load CfRadial2 Volume Data#

[2]:
fpath = "netcdf/cfrad.20080604_002217_000_SPOL_v36_SUR_cfradial2.nc"
f = wrl.util.get_wradlib_data_file(fpath)
vol = xt.open_datatree(f)
Downloading file 'netcdf/cfrad.20080604_002217_000_SPOL_v36_SUR_cfradial2.nc' from 'https://github.com/wradlib/wradlib-data/raw/pooch/data/netcdf/cfrad.20080604_002217_000_SPOL_v36_SUR_cfradial2.nc' to '/home/runner/work/wradlib/wradlib/wradlib-data'.
[3]:
# fix: remove when available in xradar
for k in vol.groups[1:]:
    vol[k].ds = (
        vol[k]
        .ds.assign(sweep_fixed_angle=vol[k].ds.attrs["fixed_angle"])
        .swap_dims(time="azimuth")
        .sortby("azimuth")
    )

Inspect RadarVolume#

[4]:
display(vol)
<xarray.DatasetView>
Dimensions:              (sweep: 9)
Coordinates:
    sweep_mode           object ...
    longitude            float64 ...
    altitude             float64 ...
    time                 datetime64[ns] ...
    latitude             float64 ...
Dimensions without coordinates: sweep
Data variables:
    volume_number        int64 ...
    platform_type        object ...
    instrument_type      object ...
    primary_axis         object ...
    time_coverage_start  object ...
    time_coverage_end    object ...
    sweep_group_name     (sweep) object ...
    sweep_fixed_angle    (sweep) float64 ...
Attributes:
    version:          2.0
    title:            None
    institution:      None
    references:       None
    source:           None
    history:          None
    comment:          im/exported using wradlib
    instrument_name:  None
    fixed_angle:      0.5
    Conventions:      Cf/Radial

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

[5]:
vol.root
[5]:
<xarray.DatasetView>
Dimensions:              (sweep: 9)
Coordinates:
    sweep_mode           object ...
    longitude            float64 ...
    altitude             float64 ...
    time                 datetime64[ns] ...
    latitude             float64 ...
Dimensions without coordinates: sweep
Data variables:
    volume_number        int64 ...
    platform_type        object ...
    instrument_type      object ...
    primary_axis         object ...
    time_coverage_start  object ...
    time_coverage_end    object ...
    sweep_group_name     (sweep) object ...
    sweep_fixed_angle    (sweep) float64 ...
Attributes:
    version:          2.0
    title:            None
    institution:      None
    references:       None
    source:           None
    history:          None
    comment:          im/exported using wradlib
    instrument_name:  None
    fixed_angle:      0.5
    Conventions:      Cf/Radial

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

[6]:
display(vol["sweep_0"])
<xarray.DatasetView>
Dimensions:             (azimuth: 480, range: 996)
Coordinates:
    sweep_mode          object ...
    time                (azimuth) datetime64[ns] 2008-06-04T00:15:34 ... 2008...
  * range               (range) float32 150.0 300.0 ... 1.492e+05 1.494e+05
  * azimuth             (azimuth) float32 0.0 0.75 1.5 ... 357.8 358.5 359.2
    elevation           (azimuth) float32 ...
    longitude           float64 ...
    latitude            float64 ...
    altitude            float64 ...
Data variables: (12/17)
    sweep_number        int32 ...
    polarization_mode   |S32 ...
    prt_mode            |S32 ...
    follow_mode         |S32 ...
    fixed_angle         float32 ...
    target_scan_rate    float32 ...
    ...                  ...
    n_samples           (azimuth) int32 ...
    r_calib_index       (azimuth) int8 ...
    scan_rate           (azimuth) float32 ...
    DBZ                 (azimuth, range) float32 ...
    VR                  (azimuth, range) float32 ...
    sweep_fixed_angle   float64 0.5
Attributes:
    fixed_angle:  0.5

Georeferencing#

[7]:
swp = vol["sweep_0"].ds.copy()
swp = swp.assign_coords(sweep_mode=swp.sweep_mode)
swp = swp.wrl.georef.georeference()

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 Cf/Radial standard.

[8]:
display(swp.DBZ)
<xarray.DataArray 'DBZ' (azimuth: 480, range: 996)>
[478080 values with dtype=float32]
Coordinates: (12/15)
    sweep_mode  <U20 'azimuth_surveillance'
    time        (azimuth) datetime64[ns] 2008-06-04T00:15:34 ... 2008-06-04T0...
  * range       (range) float32 150.0 300.0 450.0 ... 1.492e+05 1.494e+05
  * azimuth     (azimuth) float32 0.0 0.75 1.5 2.25 ... 357.0 357.8 358.5 359.2
    elevation   (azimuth) float32 0.5164 0.5219 0.5164 ... 0.5219 0.5219 0.5219
    longitude   float64 120.4
    ...          ...
    y           (azimuth, range) float32 150.0 300.0 ... 1.492e+05 1.493e+05
    z           (azimuth, range) float32 46.0 47.0 48.0 ... 2.714e+03 2.718e+03
    gr          (azimuth, range) float32 150.0 300.0 ... 1.492e+05 1.494e+05
    rays        (azimuth, range) float32 0.0 0.0 0.0 0.0 ... 359.2 359.2 359.2
    bins        (azimuth, range) float32 150.0 300.0 ... 1.492e+05 1.494e+05
    crs_wkt     int64 0
Attributes:
    long_name:             Computed Horizontal Co-polar Reflectivit
    standard_name:         equivalent_reflectivity_factor
    units:                 dBZ
    threshold_field_name:
    threshold_value:       -9999.0
    sampling_ratio:        1.0
    grid_mapping:          grid_mapping

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.

For more details on plotting radar data see under Visualization.

[9]:
swp.DBZ.sortby("time").plot(x="range", y="time", add_labels=False)
[9]:
<matplotlib.collections.QuadMesh at 0x7fd3e817d790>
../../../_images/notebooks_fileio_backends_cfradial2_backend_17_1.png
[10]:
fig = plt.figure(figsize=(5, 5))
pm = swp.DBZ.wrl.vis.plot(crs={"latmin": 3e3}, fig=fig)
../../../_images/notebooks_fileio_backends_cfradial2_backend_18_0.png