In [18]:
%pylab inline
rcParams['figure.figsize'] = (10,6)
from scipy import interpolate
from lagranto import Tra
from mpl_toolkits.axes_grid1 import AxesGrid
import matplotlib.gridspec as gridspec
from mpl_toolkits.basemap import Basemap
from matplotlib.collections import LineCollection


Populating the interactive namespace from numpy and matplotlib
WARNING: pylab import has clobbered these variables: ['mean', 'std']
`%matplotlib` prevents importing * from pylab and numpy

Loading trajectories using Tra


In [2]:
filename = 'data/lsl_20070119_12_ana_48'
trajs = Tra(filename)
print trajs
print trajs.shape


         303 trajectories with 9 time steps. 
         Available fields: time/lon/lat/p/Q/RH/TH/PV/P/LABEL
         total duration: 2880.0 minutes
(303, 9)

Interpolate PV to evenly spaced pressure levels


In [3]:
ilevels = np.arange(200,1000,50)
nlev = len(ilevels)

In [4]:
def interp(pvalue, intvalue):
    interpfield = interpolate.interp1d(pvalue[::-1], intvalue[::-1], bounds_error=False)
    return interpfield(ilevels)

In [5]:
ntrajs = {}
variables = ['Q', 'RH', 'TH', 'PV']
for var in variables:
    ntrajs[var] = np.zeros((trajs.ntra, nlev))
for i, traj in enumerate(trajs):
    for var in variables:
        ntrajs[var][i, :] = interp(traj['P'], traj[var])

Plot the mean, mean + std, mean - std of PV interpolated on pressure levels


In [19]:
ax = subplot(111)
mean = np.nanmean(ntrajs['PV'], axis=0)
std = np.nanstd(ntrajs['PV'], axis=0)
pstd = mean + std
mstd = mean - std
_ = fill_between(ilevels, pstd, mstd, alpha=0.5, label='+/- standar deviation')
_ = plot(ilevels, np.nanmean(ntrajs['PV'], axis=0), 'r', label='mean')
_ = xticks(ilevels)
ax.invert_xaxis()
legend()


Out[19]:
<matplotlib.legend.Legend at 0x7949f90>

Plot the same values for TH, RH, PV, Q in a 4x4 plot using AxesGrid


In [7]:
def plot_mean_std(pos, var):
    ax = subplot(pos)
    mean = np.nanmean(ntrajs[var], axis=0)
    std = np.nanstd(ntrajs[var], axis=0)
    pstd = mean + std
    mstd = mean - std
    mins = np.nanmin(mstd)
    maxs = np.nanmax(pstd)
    ax.set_title(var)
    _ = ax.fill_between(ilevels, pstd, mstd, alpha=0.5, label='+/- standar deviation')
    _ = ax.plot(ilevels, np.nanmean(ntrajs[var], axis=0), 'r', label='mean')
    _ = ax.set_xticks(ilevels)
    ax.invert_xaxis()
    legend()

In [8]:
fig = figure(figsize=(20,20))
grid = gridspec.GridSpec(2,2, wspace=0.1, hspace=0.1)
for box, var in zip(grid, variables):
    plot_mean_std(box, var)


Do a scatter plot of Q versus PV, with points colored by pressure


In [20]:
scatter(trajs['Q'], trajs['PV'], c=trajs['P'])
colorbar()


Out[20]:
<matplotlib.colorbar.Colorbar instance at 0x81b6710>

Plot TH at each P level in the form of a boxplot


In [21]:
ax = subplot(111)
bplot = boxplot(ntrajs['TH'], sym='')
for line in bplot['medians']:
    line.set_color('k')
ax.set_xticklabels([str(level) for level in ilevels])
ax.invert_xaxis()


Plot the trajectories on a map


In [11]:
lllon,urlon = trajs['lon'].min(), trajs['lon'].max()
lllat,urlat = trajs['lat'].min(), trajs['lat'].max()
mapf = Basemap(llcrnrlon=lllon,llcrnrlat=lllat, urcrnrlon=urlon, urcrnrlat=urlat, resolution='i')

In [22]:
mapf.drawcoastlines()
mapf.plot(trajs['lon'].T, trajs['lat'].T, latlon=True, color='k')
title('Trajectories')


Out[22]:
<matplotlib.text.Text at 0x584b210>

Plot the trajectories colored by pressure on a map


In [23]:
mapf.drawcoastlines()
for traj in trajs:
    points = np.array([traj['lon'], traj['lat']]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    lc = LineCollection(segments, cmap=plt.get_cmap('Spectral'))
    lc.set_array(traj['P'])
    plt.gca().add_collection(lc)
colorbar(lc)


Out[23]:
<matplotlib.colorbar.Colorbar instance at 0x9c47b90>

In [ ]: