# Using Python to Access NCEI Archived NEXRAD Level 2 Data¶

This notebook shows how to access the THREDDS Data Server (TDS) instance that is serving up archived NEXRAD Level 2 data hosted on Amazon S3. The TDS provides a mechanism to query for available data files, as well as provides access to the data as native volume files, through OPeNDAP, and using its own CDMRemote protocol. Since we're using Python, we can take advantage of Unidata's Siphon package, which provides an easy API for talking to THREDDS servers.

But first! Bookmark these resources for when you want to use Siphon later!

First we'll create an instance of RadarServer to point to the appropriate radar server access URL.

In [1]:
from siphon.radarserver import RadarServer


Next, we'll create a new query object to help request the data. Using the chaining methods, let's ask for the latest data at the radar KLVX (Louisville, KY). We see that when the query is represented as a string, it shows the encoded URL.

In [2]:
from datetime import datetime, timedelta
query = rs.query()
query.stations('KLVX').time(datetime.utcnow())

Out[2]:
time=2020-04-01T20%3A36%3A42.722194&stn=KLVX

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 [3]:
rs.validate_query(query)

Out[3]:
True

Make the request, which returns an instance of TDSCatalog; this handles parsing the returned XML information.

In [4]:
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 volume in the return, since we asked for the volume nearest to a single time.

In [5]:
catalog.datasets

Out[5]:
['KLVX20200401_202635_V06']

We can grab that datasetthat dataset and call remote_access(), which sets us up to access the data remotely, without downloading the entire file.

In [6]:
data = catalog.datasets[0].remote_access()


We define some helper functions to make working with the data easier. One takes the raw data and converts it to floating point values with the missing data points appropriately marked. The other helps with converting the polar coordinates (azimuth and range) to Cartesian (x and y).

In [7]:
import numpy as np
# Values come back signed. If the _Unsigned attribute is set, we need to convert
# from the range [-127, 128] to [0, 255].
if var._Unsigned:
data = data & 255

# Convert to float using the scale and offset
return data * var.scale_factor + var.add_offset

def polar_to_cartesian(az, rng):
return x, y


The CDMRemote reader provides an interface that is almost identical to the usual python NetCDF interface. We pull out the variables we need for azimuth and range, as well as the data itself.

In [8]:
sweep = 0
ref_var = data.variables['Reflectivity_HI']
ref_data = ref_var[sweep]
rng = data.variables['distanceR_HI'][:]
az = data.variables['azimuthR_HI'][sweep]


Then convert the raw data to floating point values and the polar coordinates to Cartesian.

In [9]:
ref = raw_to_masked_float(ref_var, ref_data)
x, y = polar_to_cartesian(az, rng)


MetPy is a Python package for meteorology (Documentation: http://metpy.readthedocs.org and GitHub: http://github.com/MetPy/MetPy). We import MetPy and use it to get the colortable and value mapping information for the NWS Reflectivity data.

In [10]:
import metpy.plots as mpplots
ref_norm, ref_cmap = mpplots.ctables.registry.get_with_steps('NWSReflectivity', 5, 5)


Finally, we plot them up using matplotlib and cartopy. We create a helper function for making a map to keep things simpler later.

In [11]:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature

def new_map(fig, lon, lat):
# Create projection centered on the radar. This allows us to use x
# and y relative to the radar.
proj = ccrs.LambertConformal(central_longitude=lon, central_latitude=lat)

# New axes with the specified projection
ax = fig.add_axes([0.02, 0.02, 0.96, 0.96], projection=proj)

return ax


Use the function to make a new map and plot a colormapped view of the data

In [12]:
fig = plt.figure(figsize=(10, 10))
ax = new_map(fig, data.StationLongitude, data.StationLatitude)
ax.pcolormesh(x, y, ref, cmap=ref_cmap, norm=ref_norm, zorder=0)

Out[12]:
<matplotlib.collections.QuadMesh at 0x119540190>

This time we'll make a query based on a longitude, latitude point and using a time range.

In [13]:
query = rs.query()
dt = datetime(2012, 10, 29, 15) # Our specified time
query.lonlat_point(-73.687, 41.175).time_range(dt, dt + timedelta(hours=1))

Out[13]:
time_start=2012-10-29T15%3A00%3A00&time_end=2012-10-29T16%3A00%3A00&longitude=-73.687&latitude=41.175

The specified longitude, latitude are in NY and the TDS helpfully finds the closest station to that point. The time range we request is an hour of data form 29 October 2012; we're looking for data from Hurricane Sandy. We can see that this time we obtained multiple datasets.

In [14]:
cat = rs.get_catalog(query)
cat.datasets

Out[14]:
['KOKX20121029_150259_V06.gz', 'KOKX20121029_150854_V06.gz', 'KOKX20121029_151451_V06.gz', 'KOKX20121029_152046_V06.gz', 'KOKX20121029_152639_V06.gz', 'KOKX20121029_153234_V06.gz', 'KOKX20121029_153829_V06.gz', 'KOKX20121029_154422_V06.gz', 'KOKX20121029_155017_V06.gz', 'KOKX20121029_155612_V06.gz']

Grab the first dataset so that we can get the longitude and latitude of the station and make a map for plotting. We'll go ahead and specify some longitude and latitude bounds for the map.

In [15]:
ds = cat.datasets[0]
data = ds.remote_access()
fig = plt.figure(figsize=(10, 7.5))
ax = new_map(fig, data.StationLongitude, data.StationLatitude)

# Set limits in lat/lon space
ax.set_extent([-77, -70, 37, 41])


Out[15]:
<cartopy.mpl.feature_artist.FeatureArtist at 0x120bd3310>

Now we can loop over the collection of returned datasets and plot them. As we plot, we collect the returned plot objects so that we can use them to make an animated plot. We also add a timestamp for each plot.

In [16]:
meshes = []
for ds_name in cat.datasets:
# After looping over the list of sorted datasets, pull the actual Dataset object out
# of our list of items and access over CDMRemote
data = cat.datasets[ds_name].remote_access()

# Pull out the data of interest
sweep = 0
rng = data.variables['distanceR_HI'][:]
az = data.variables['azimuthR_HI'][sweep]
ref_var = data.variables['Reflectivity_HI']

# Convert data to float and coordinates to Cartesian
x, y = polar_to_cartesian(az, rng)

# Plot the data and the timestamp
mesh = ax.pcolormesh(x, y, ref, cmap=ref_cmap, norm=ref_norm, zorder=0)
text = ax.text(0.7, 0.02, data.time_coverage_start, transform=ax.transAxes,
fontdict={'size':16})

# Collect the things we've plotted so we can animate
meshes.append((mesh, text))


Using matplotlib, we can take a collection of Artists that have been plotted and turn them into an animation. Using the FFMpeg utility, this animation can be converted to HTML5 video viewable in the notebook.

In [17]:
# Set up matplotlib to do the conversion to HTML5 video
import matplotlib
matplotlib.rcParams['animation.html'] = 'html5'

# Create an animation
from matplotlib.animation import ArtistAnimation
ArtistAnimation(fig, meshes)

Out[17]: