def plot(xx,
         yy,
         target,
         label,
         figfiles,
         figfile,
         lon=None,
         lat=None,
         show=False):
    xs, ys, mask = coord2slice(target, lon=lon, lat=lat)
    P.figure(figsize=(6, 3.5))
    P.title('Target=%(label)s / select: lon=%(lon)s, lat=%(lat)s' % locals())
    add_grid((xx, yy))
    xx = xx.asma()
    yy = yy.asma()
    if isinstance(lon, tuple):
        P.axvline(lon[0], color='m', ls='--', lw=2)
        P.axvline(lon[1], color='m', ls='--', lw=2)
    elif isinstance(lon, slice):
        i, j, k = lon.indices(xx.shape[1])
        P.plot(xx[:, i], yy[:, i], 'c--', lw=2)
        P.plot(xx[:, j - 1], yy[:, j - 1], 'c--', lw=2)
    if isinstance(lat, tuple):
        P.axhline(lat[0], color='m', ls='--', lw=2)
        P.axhline(lat[1], color='m', ls='--', lw=2)
    elif isinstance(lat, slice):
        i, j, k = lat.indices(yy.shape[0])
        P.plot(xx[i], yy[i], 'c--', lw=2)
        P.plot(xx[j - 1], yy[j - 1], 'c--', lw=2)
    P.xticks(N.arange(xx.min() - 1, xx.max() + 1))
    P.yticks(N.arange(yy.min() - 1, yy.max() + 1))
    xxi, yyi = xx, yy
    xx = xx[ys, xs]
    yy = yy[ys, xs]
    #    mask = mask[ys, xs]
    xxb, yyb = meshbounds(xx, yy)
    P.pcolormesh(xxb, yyb, mask, shading='faceted')
    P.scatter(xx.ravel(), yy.ravel(), c=(0, 1, 0))
    P.grid(True)
    P.axis('image')
    P.tight_layout()
    i = len(figfiles)
    savefig = figfile % i
    if os.path.exists(savefig): os.remove(savefig)
    P.savefig(savefig)
    figfiles.append(savefig)
    if show: P.show()
    else: P.close()
def plot(xx, yy, target, label, figfiles, figfile, lon=None, lat=None, show=False):
    xs, ys, mask = coord2slice(target, lon=lon, lat=lat)
    P.figure(figsize=(6, 3.5))
    P.title('Target=%(label)s / select: lon=%(lon)s, lat=%(lat)s'%locals())
    add_grid((xx, yy))
    xx = xx.asma()
    yy = yy.asma()
    if isinstance(lon, tuple): 
        P.axvline(lon[0], color='m', ls='--', lw=2)
        P.axvline(lon[1], color='m', ls='--', lw=2)
    elif isinstance(lon, slice):
        i, j, k = lon.indices(xx.shape[1])
        P.plot(xx[:, i], yy[:, i], 'c--', lw=2)
        P.plot(xx[:, j-1], yy[:, j-1], 'c--', lw=2)
    if isinstance(lat, tuple): 
        P.axhline(lat[0], color='m', ls='--', lw=2)
        P.axhline(lat[1], color='m', ls='--', lw=2)
    elif isinstance(lat, slice):
        i, j, k = lat.indices(yy.shape[0])
        P.plot(xx[i], yy[i], 'c--', lw=2)
        P.plot(xx[j-1], yy[j-1], 'c--', lw=2)
    P.xticks(N.arange(xx.min()-1, xx.max()+1))
    P.yticks(N.arange(yy.min()-1, yy.max()+1))
    xxi, yyi = xx, yy
    xx = xx[ys, xs]
    yy = yy[ys, xs]
#    mask = mask[ys, xs]
    xxb, yyb = meshbounds(xx, yy)
    P.pcolor(xxb, yyb, mask, shading='faceted')
    P.scatter(xx.ravel(), yy.ravel(), c=(0, 1, 0))
    P.grid('on')
    P.axis('image')
    P.tight_layout()
    i = len(figfiles)
    savefig = figfile%i
    if os.path.exists(savefig): os.remove(savefig)
    P.savefig(savefig)
    figfiles.append(savefig)
    if show: P.show()
    else: P.close()
           'or-',
           markersize=5,
           lw=.8,
           label='Interpolated' if label else None)
    P.legend(loc='best', framealpha=0.5)
P.grid()
P.title('Section')
P.tight_layout()
savefigs(code_file_name(ext='_0.png'), verbose=False, pdf=True)

# Maps
P.figure(figsize=(8, 4))
levels = auto_scale(vmin=zzi.min(), vmax=zzi.max(), nmax=30)
P.subplot(121)
P.contourf(xxi, yyi, zzi, levels=levels)
P.contour(xxi, yyi, zzi, linewidths=0.1, levels=levels, colors='k')
add_grid((xxi, yyi), alpha=.3, centers=True)
for iyi in iyis:
    P.axhline(yi[iyi], linestyle='--', color='k')
P.title('Original')
P.subplot(122)
P.contourf(xxo, yyo, zzo, levels=levels)
P.contour(xxo, yyo, zzo, linewidths=0.1, levels=levels, colors='k')
add_grid((xxo, yyo), alpha=.2, centers=True)
for iyi in iyis:
    P.axhline(yi[iyi], linestyle='--', color='k')
P.title('Interpolated')
P.tight_layout()
savefigs(code_file_name(ext='_1.png'), verbose=False, pdf=True)
P.close()
taxo = create_time(lindates(ct0, ct1, 1, 'hour'), taxi.units)

# Lag error
# - estimation
els = []
lags = N.arange(1, 6)
for lag in lags:
    els.append(N.sqrt(((sp[lag:] - sp[:-lag])**2).mean()))
els = N.array(els)
a, b, _, _, _ = linregress(lags, els)
# - plot
P.figure(figsize=(6, 6))
P.subplot(211)
P.plot(lags, els, 'o')
P.plot([0, lags[-1]], [b, a * lags[-1] + b], 'g')
P.axhline(b, color='0.8', ls='--')
P.ylim(ymin=0)
P.xlabel('Lag [hour]')
P.ylabel('Error [m s-1]')
add_key(1)
P.title('Linear lag error model')

# Interpolation
sph, speh = regrid1d(sp,
                     taxo,
                     method='cellerr',
                     erri=spe,
                     errl=-a,
                     geterr=True)

# Time zoom for plot clarity
taxo = create_time(lindates(ct0, ct1, 1, "hour"), taxi.units)

# Lag error
# - estimation
els = []
lags = N.arange(1, 6)
for lag in lags:
    els.append(N.sqrt(((sp[lag:] - sp[:-lag]) ** 2).mean()))
els = N.array(els)
a, b, _, _, _ = linregress(lags, els)
# - plot
P.figure(figsize=(6, 6))
P.subplot(211)
P.plot(lags, els, "o")
P.plot([0, lags[-1]], [b, a * lags[-1] + b], "g")
P.axhline(b, color="0.8", ls="--")
P.ylim(ymin=0)
P.xlabel("Lag [hour]")
P.ylabel("Error [m s-1]")
add_key(1)
P.title("Linear lag error model")

# Interpolation
sph, speh = regrid1d(sp, taxo, method="cellerr", erri=spe, errl=-a, geterr=True)

# Time zoom for plot clarity
tzoom = (ct1.sub(7, cdtime.Hour), ctimesi[-1])
sp = sp(tzoom)
spe = spe(tzoom)
sph = sph(tzoom)
speh = speh(tzoom)
for iyi in iyis:
    label = iyi==iyis[0]
    P.plot(xi, zzi[iyi], 'ob-', markersize=8, label='Original' if label else None)
    P.plot(xo, zzo[iyi*r], 'or-', markersize=5, lw=.8, label='Interpolated' if label else None)
    P.legend(loc='best', framealpha=0.5)
P.grid()
P.title('Section')
P.tight_layout()
savefigs(code_file_name(ext='_0.png'), verbose=False, pdf=True)

# Maps
P.figure(figsize=(8, 4))
levels = auto_scale(vmin=zzi.min(), vmax=zzi.max(), nmax=30)
P.subplot(121)
P.contourf(xxi, yyi, zzi, levels=levels)
P.contour(xxi, yyi, zzi, linewidths=0.1, levels=levels, colors='k')
add_grid((xxi, yyi), alpha=.3, centers=True)
for iyi in iyis:
    P.axhline(yi[iyi], linestyle='--', color='k')
P.title('Original')
P.subplot(122)
P.contourf(xxo, yyo, zzo, levels=levels)
P.contour(xxo, yyo, zzo, linewidths=0.1, levels=levels, colors='k')
add_grid((xxo, yyo), alpha=.2, centers=True)
for iyi in iyis:
    P.axhline(yi[iyi], linestyle='--', color='k')
P.title('Interpolated')
P.tight_layout()
savefigs(code_file_name(ext='_1.png'), verbose=False, pdf=True)
P.close()