This is a url from Kerfooot's TDS server, using the multidimensional NetCDF datasets created by a private ERDDAP instance. These multidimensonal datasets are also available from ERDDAP, along with a flattened NetCDF representation and a ragged NetCDF representation.
The glider ERDDAP is here: http://erddap.marine.rutgers.edu/erddap
The glider TDS is here: http://tds.marine.rutgers.edu:8080/thredds/catalog/cool/glider/all/catalog.html
In [1]:
import iris
url = 'http://tds.marine.rutgers.edu:8080/thredds/dodsC/cool/glider/all/ru22-20130924T2010.ncCFMA.nc3.nc'
cubes = iris.load_raw(url)
print(cubes)
In [2]:
cube = cubes.extract('sea_water_temperature')[0] #<- it always returns a list!
print(cube)
In [3]:
import numpy as np
import numpy.ma as ma
import seawater as sw
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
%matplotlib inline
def plot_glider(cube, mask_topo=False, track_inset=False, **kw):
"""Plot glider cube."""
cmap = kw.pop('cmap', plt.cm.rainbow)
lon = cube.coord(axis='X').points.squeeze()
lat = cube.coord(axis='Y').points.squeeze()
z = cube.coord(axis='Z').points.squeeze()
data = ma.masked_invalid(cube.data.squeeze())
t = cube.coord(axis='T')
t = t.units.num2date(t.points.squeeze())
dist, pha = sw.dist(lat, lon, units='km')
dist = np.r_[0, np.cumsum(dist)]
dist, z = np.broadcast_arrays(dist[..., None], z)
try:
z_range = cube.coord(axis='Z').attributes['actual_range']
except KeyError:
z_range = z.min(), z.max()
try:
data_range = cube.attributes['actual_range']
except KeyError:
data_range = data.min(), data.max()
condition = np.logical_and(data >= data_range[0], data <= data_range[1])
data = ma.masked_where(~condition, data)
condition = np.logical_and(z >= z_range[0], z <= z_range[1])
z = ma.masked_where(~condition, z)
fig, ax = plt.subplots(figsize=(9, 3.75))
cs = ax.pcolor(dist, z, data, cmap=cmap, snap=True, **kw)
if mask_topo:
h = z.max(axis=1)
x = dist[:, 0]
ax.plot(x, h, color='black', linewidth='0.5', zorder=3)
ax.fill_between(x, h, y2=h.max(), color='0.9', zorder=3)
ax.invert_yaxis()
ax.set_title('Glider track from {} to {}'.format(t[0], t[-1]))
fig.tight_layout()
if track_inset:
axin = inset_axes(ax, width="25%", height="30%", loc=4)
axin.plot(lon, lat, 'k.')
start, end = (lon[0], lat[0]), (lon[-1], lat[-1])
kw = dict(marker='o', linestyle='none')
axin.plot(*start, color='g', **kw)
axin.plot(*end, color='r', **kw)
axin.axis('off')
return fig, ax, cs
In [4]:
fig, ax, cs = plot_glider(cube, mask_topo=False, track_inset=True)
fig, ax, cs = plot_glider(cube, mask_topo=True)