<jupyter_start><jupyter_text>This notebook is part of the $\omega radlib$ documentation: https://docs.wradlib.org.Copyright (c) $\omega radlib$ developers.Distributed under the MIT License. See LICENSE.txt for more info.  Plot data via xarray<jupyter_code>import numpy as np
import matplotlib.pyplot as pl
import wradlib
import warnings
warnings.filterwarnings('ignore')
try:
    get_ipython().magic("matplotlib inline")
except:
    pl.ion()<jupyter_output><empty_output><jupyter_text>Read a polar data set from the German Weather Service<jupyter_code>filename = wradlib.util.get_wradlib_data_file('dx/raa00-dx_10908-0806021735-fbg---bin.gz')
print(filename)
img, meta = wradlib.io.read_dx(filename)<jupyter_output><empty_output><jupyter_text>Inspect the data set a little<jupyter_code>print("Shape of polar array: %r\n" % (img.shape,))
print("Some meta data of the DX file:")
print("\tdatetime: %r" % (meta["datetime"],))
print("\tRadar ID: %s" % (meta["radarid"],))<jupyter_output><empty_output><jupyter_text>transform to xarray DataArray<jupyter_code>r = np.arange(img.shape[1], dtype=np.float)
r += (r[1] - r[0]) / 2.
az = meta['azim']
az += (az[1] - az[0]) / 2.
da = wradlib.georef.create_xarray_dataarray(img, r=r, phi=az, theta=meta['elev'])
da = wradlib.georef.georeference_dataset(da)
da<jupyter_output><empty_output><jupyter_text>The simplest way to plot this datasetUse the `wradlib` xarray DataArray Accessor<jupyter_code>pm = da.wradlib.plot_ppi()
txt = pl.title('Simple PPI')
pm = da.wradlib.contourf()
txt = pl.title('Simple Filled Contour PPI')
pm = da.wradlib.contour()
txt = pl.title('Simple Contour PPI')
da = wradlib.georef.create_xarray_dataarray(img, r=r, phi=az, theta=meta['elev'])
da = wradlib.georef.georeference_dataset(da)
pm = da.wradlib.contour(proj='cg')
txt = pl.title('Simple CG Contour PPI', y=1.05)<jupyter_output><empty_output><jupyter_text>create DataArray with proper azimuth/range dimensionsUsing ranges in meters and correct site-location in (lon, lat, alt)<jupyter_code>r = np.arange(img.shape[1], dtype=np.float) * 1000.
r += (r[1] - r[0]) / 2.
az = meta['azim']
az += (az[1] - az[0]) / 2.

da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'], 
                                            site=(10, 45, 0))
da = wradlib.georef.georeference_dataset(da)

pm = da.wradlib.plot_ppi()
txt = pl.title('Simple PPI')
da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'], 
                                            site=(10, 45, 0), rf=1e3)
da = wradlib.georef.georeference_dataset(da)

pm = da.wradlib.plot_ppi()
txt = pl.title('Simple PPI with adjusted range axis')
da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'], 
                                            proj=wradlib.georef.get_default_projection(),
                                            site=(10, 45, 0))
da = wradlib.georef.georeference_dataset(da)
pm = da.wradlib.plot_ppi()
txt = pl.title('Simple projected PPI (WGS84)')<jupyter_output><empty_output><jupyter_text>Plotting just one sector For this purpose, we slice azimuth/range...<jupyter_code>da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'], rf=1e3)
da = wradlib.georef.georeference_dataset(da)
da_sel = da.sel(azimuth=slice(200,250), 
                range=slice(40, 80))
pm = da_sel.wradlib.plot_ppi()
txt = pl.title('Sector PPI')<jupyter_output><empty_output><jupyter_text>Adding a crosshair to the PPI<jupyter_code># We introduce a site offset...
site = (10., 45., 0)
r = np.arange(img.shape[1], dtype=np.float)
r += (r[1] - r[0]) / 2.
r *= 1000
az = np.arange(img.shape[0], dtype=np.float)
az += (az[1] - az[0]) / 2.
da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'],
                                            site=(10, 45, 0))
da = wradlib.georef.georeference_dataset(da)
da.wradlib.plot_ppi()
# ... plot a crosshair over our data...
wradlib.vis.plot_ppi_crosshair(site=site, ranges=[50e3, 100e3, 128e3], 
                               angles=[0, 90, 180, 270], 
                               line=dict(color='white'), 
                               circle={'edgecolor': 'white'})
pl.title('Offset and Custom Crosshair')
pl.axis("tight")
pl.axes().set_aspect('equal')<jupyter_output><empty_output><jupyter_text>Placing the polar data in a projected Cartesian reference system Using the `proj` keyword we tell the function to:- interpret the site coordinates as longitude/latitude- reproject the coordinates to the given projection (here: dwd-radolan composite coordinate system)<jupyter_code>site=(10., 45., 0)
r = np.arange(img.shape[1], dtype=np.float)
r += (r[1] - r[0]) / 2.
r *= 1000
az = np.arange(img.shape[0], dtype=np.float)
az += (az[1] - az[0]) / 2.
proj_rad = wradlib.georef.create_osr("dwd-radolan")
da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'], 
                                            site=site)
da = wradlib.georef.georeference_dataset(da, proj=proj_rad)
pm = da.wradlib.plot_ppi()
ax = pl.gca()
# Now the crosshair ranges must be given in meters
wradlib.vis.plot_ppi_crosshair(site=site,
                               ax=ax,
                               ranges=[40e3, 80e3, 128e3],
                               line=dict(color='white'),
                               circle={'edgecolor':'white'},
                               proj=proj_rad
                               )
pl.title('Georeferenced/Projected PPI')
pl.axis("tight")
pl.axes().set_aspect('equal')<jupyter_output><empty_output><jupyter_text>Some side effects of georeferencing Transplanting the radar virtually moves it away from the central meridian of the projection (which is 10 degrees east). Due north now does not point straight upwards on the map.The crosshair shows this: for the case that the lines should actually become curved, they are implemented as a piecewise linear curve with 10 vertices. The same is true for the range circles, but with more vertices, of course.<jupyter_code>site=(45., 7., 0.)
r = np.arange(img.shape[1], dtype=np.float) * 1000.
r += (r[1] - r[0]) / 2.
az = np.arange(img.shape[0], dtype=np.float)
az += (az[1] - az[0]) / 2.
da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'], site=site)
da = wradlib.georef.georeference_dataset(da, proj=proj_rad)
pm = da.wradlib.plot_ppi()

ax = wradlib.vis.plot_ppi_crosshair(site=site,
                                    ranges=[64e3, 128e3],
                                    line=dict(color='red'),
                                    circle={'edgecolor': 'red'},
                                    proj=proj_rad
                                   )
txt = pl.title('Projection Side Effects')<jupyter_output><empty_output><jupyter_text>Simple Plot on Mercator-Map using cartopy<jupyter_code>import cartopy.crs as ccrs
ccrs
site=(7, 45, 0.)
map_proj = ccrs.Mercator(central_longitude=site[1])
r = np.arange(img.shape[1], dtype=np.float) * 1000.
r += (r[1] - r[0]) / 2.
az = np.arange(img.shape[0], dtype=np.float)
az += (az[1] - az[0]) / 2.
da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'], 
                                            site=site)
da = wradlib.georef.georeference_dataset(da)
fig = pl.figure(figsize=(10, 10))
pm = da.wradlib.plot_ppi(proj=map_proj, fig=fig)
ax = pl.gca()
ax.gridlines(draw_labels=True)<jupyter_output><empty_output><jupyter_text>More decorations and annotations You can annotate these plots by using standard matplotlib methods.<jupyter_code>r = np.arange(img.shape[1], dtype=np.float)
r += (r[1] - r[0]) / 2.
az = np.arange(img.shape[0], dtype=np.float)
az += (az[1] - az[0]) / 2.

da = wradlib.georef.create_xarray_dataarray(img, phi=az, r=r, theta=meta['elev'])
da = wradlib.georef.georeference_dataset(da)
pm = da.wradlib.plot_ppi()
ax = pl.gca()
ylabel = ax.set_xlabel('easting [km]')
ylabel = ax.set_ylabel('northing [km]')
title = ax.set_title('PPI manipulations/colorbar')
# you can now also zoom - either programmatically or interactively
xlim = ax.set_xlim(-80, -20)
ylim = ax.set_ylim(-80, 0)
# as the function returns the axes- and 'mappable'-objects colorbar needs, adding a colorbar is easy
cb = pl.colorbar(pm, ax=ax)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1449957744778462, 'fraction_numerical': 0.02426657008330315, 'mean_word_length': 3.482683982683983, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '236787', 'license': 'MIT', 'n_tokens_mistral': 3188, 'n_tokens_neox': 2967, 'n_words': 645, 'path': 'notebooks/visualisation/wradlib_plot_ppi_xarray.ipynb', 'repo_name': 'wradlib/wradlib-notebooks', 'size': '8283'}