In this example, we'll focus on interacting with the Radar Query Service to retrieve radar data.
But first! Bookmark these resources for when you want to use Siphon later!
First, we point at the top level of the Radar Query Service (the "Radar Server") to see what radar collections are available:
In [1]:
from siphon.catalog import TDSCatalog
cat = TDSCatalog('http://thredds.ucar.edu/thredds/radarServer/catalog.xml')
list(cat.catalog_refs)
Out[1]:
Next we create an instance of the RadarServer
object to point at one of these collections. This downloads some top level metadata and sets things up so we can easily query the server.
In [2]:
from siphon.radarserver import RadarServer
rs = RadarServer(cat.catalog_refs['NEXRAD Level III Radar from IDD'].href)
We can use rs.variables to see a list of radar products available to view from this access URL.
In [3]:
print(sorted(rs.variables))
If you're not a NEXRAD radar expert, there is more information available within the metadata downloaded from the server. (NOTE: Only the codes above are valid for queries.)
In [4]:
sorted(rs.metadata['variables'])
Out[4]:
We can also see a list of the stations. Each station has associated location information.
In [5]:
print(sorted(rs.stations))
In [6]:
rs.stations['TLX']
Out[6]:
Next, we'll create a new query object to help request the data. Using the chaining methods, let's ask for reflectivity data at the lowest tilt (NOQ) from radar TLX (Oklahoma City) for the current time. We see that when the query is represented as a string, it shows the encoded URL.
In [7]:
from datetime import datetime
query = rs.query()
query.stations('TLX').time(datetime.utcnow()).variables('N0Q')
Out[7]:
The query also supports time range queries, queries for closest to a lon/lat point, or getting all radars within a lon/lat box.
We can use the RadarServer instance to check our query, to make sure we have required parameters and that we have chosen valid station(s) and variable(s).
In [8]:
rs.validate_query(query)
Out[8]:
Make the request, which returns an instance of TDSCatalog. This handles parsing the catalog
In [9]:
catalog = rs.get_catalog(query)
We can look at the datasets on the catalog to see what data we found by the query. We find one NIDS file in the return
In [10]:
catalog.datasets
Out[10]:
We'll work through doing some more queries on the radar server. Some useful links:
See if you can write Python code for the following queries:
Get ZDR (differential reflectivity) for 3 days ago from the radar nearest to Hays, KS (lon -99.324403, lat 38.874929). No map necessary!
In [ ]:
Get base reflectivity for the last two hours from all of the radars in Wyoming (call it the bounding box with lower left corner 41.008717, -111.056360 and upper right corner 44.981008, -104.042719)
In [ ]:
We can pull that dataset out of the dictionary and look at the available access URLs. We see URLs for OPeNDAP, CDMRemote, and HTTPServer (direct download).
In [11]:
ds = list(catalog.datasets.values())[0]
ds.access_urls
Out[11]:
We'll use the CDMRemote reader in Siphon and pass it the appropriate access URL. (This will all behave identically to using the 'OPENDAP' access, if we replace the Dataset
from Siphon with that from netCDF4
).
In [12]:
from siphon.cdmr import Dataset
data = Dataset(ds.access_urls['CdmRemote'])
The CDMRemote reader provides an interface that is almost identical to the usual python NetCDF interface.
In [13]:
list(data.variables)
Out[13]:
We pull out the variables we need for azimuth and range, as well as the data itself.
In [14]:
rng = data.variables['gate'][:]
az = data.variables['azimuth'][:]
ref = data.variables['BaseReflectivityDR'][:]
Then convert the polar coordinates to Cartesian using numpy
In [15]:
import numpy as np
x = rng * np.sin(np.deg2rad(az))[:, None]
y = rng * np.cos(np.deg2rad(az))[:, None]
ref = np.ma.array(ref, mask=np.isnan(ref))
Finally, we plot them up using matplotlib and cartopy.
In [16]:
%matplotlib inline
import matplotlib.pyplot as plt
import cartopy
import cartopy.feature as cfeature
from metpy.plots import ctables # For NWS colortable
# Create projection centered on the radar. This allows us to use x
# and y relative to the radar.
proj = cartopy.crs.LambertConformal(central_longitude=data.RadarLongitude,
central_latitude=data.RadarLatitude)
# New figure with specified projection
fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(1, 1, 1, projection=proj)
ax.add_feature(cfeature.STATES.with_scale('50m'), linewidth=2)
# Set limits in lat/lon space
ax.set_extent([data.RadarLongitude - 2.5, data.RadarLongitude + 2.5,
data.RadarLatitude - 2.5, data.RadarLatitude + 2.5])
# Get the NWS typical reflectivity color table, along with an appropriate norm that
# starts at 5 dBz and has steps in 5 dBz increments
norm, cmap = ctables.registry.get_with_steps('NWSReflectivity', 5, 5)
mesh = ax.pcolormesh(x, y, ref, cmap=cmap, norm=norm, zorder=0)
Try making your own plot of radar data. Various options here, but this is pretty open-ended. Some options to inspire you: