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()
"""Test :func:`~vacumm.misc.plot.add_logo`""" # Imports from vcmq import N, P, add_logo, os, code_file_name, data_sample import matplotlib.image as mpimg # Inits logofile = data_sample('logo_ifremer.png') P.plot([2, 6]) # Default add_logo(logofile, scale=1) # Upper right / no rescale add_logo(logofile, loc='upper right') # Rescale add_logo(logofile, loc='upper left', scale=2) # Alpha add_logo(logofile, loc='lower right', alpha=0.2) # Save figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile)
xo = N.linspace(xi[0], xi[-1], (nxi - 1) * r + 1) yo = N.linspace(yi[0], yi[-1], (nyi - 1) * r + 1) xxo, yyo = N.meshgrid(xo, yo) # Interpolate zzo = krig(xxi.ravel(), yyi.ravel(), zzi.ravel(), xxo.ravel(), yyo.ravel()) zzo.shape = xxo.shape # Section P.figure(figsize=(8, 4)) iyis = [3, 4] 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
ct0 = round_date(ctimesi[0], 'hour') ct1 = round_date(ctimesi[-1], 'hour') 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)
nx = ny = 50 np = 500 mtype = 'gauss' dmax = 2.5 from vcmq import N, P, code_file_name, savefigs from vacumm.misc.grid.kriging import gridded_gauss3, random_gauss3, variogram, variogram_fit # Generate random field xxg, yyg, zzg = gridded_gauss3(nx=nx, ny=ny) x, y, z = random_gauss3(np=np) # Variogram from data d, v = variogram(x, y, z, dmax=dmax) # Variogram fit with null nugget vm = variogram_fit(x, y, z, mtype, n=0, dmax=dmax) D = N.linspace(0, d.max()) V = vm(D) # Compare P.figure(figsize=(6, 4)) P.title('Variogram') P.plot(d, v, 'ob', label='From data') P.plot(D, V, '-r', label='Fitted model (%s)'%mtype) P.legend(loc='best') P.ylim(ymin=0) savefigs(code_file_name(ext=False), pdf=True, verbose=False) P.close()
ct0 = round_date(ctimesi[0], "hour") ct1 = round_date(ctimesi[-1], "hour") 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)
nx = ny = 50 np = 500 mtype = 'gauss' distmax = 2.5 from vcmq import N, P, code_file_name, savefigs from vacumm.misc.grid.kriging import gridded_gauss3, random_gauss3, variogram, variogram_fit # Generate random field xxg, yyg, zzg = gridded_gauss3(nx=nx, ny=ny) x, y, z = random_gauss3(np=np) # Variogram from data d, v = variogram(x, y, z, distmax=distmax) # Variogram fit with null nugget vm = variogram_fit(x, y, z, mtype, n=0, distmax=distmax) D = N.linspace(0, d.max()) V = vm(D) # Compare P.figure(figsize=(6, 4)) P.title('Variogram') P.plot(d, v, 'ob', label='From data') P.plot(D, V, '-r', label='Fitted model (%s)' % mtype) P.legend(loc='best') P.ylim(ymin=0) savefigs(code_file_name(ext=False), pdf=True, verbose=False) P.close()
# Refined grid xo = N.linspace(xi[0], xi[-1], (nxi-1)*r+1) yo = N.linspace(yi[0], yi[-1], (nyi-1)*r+1) xxo, yyo = N.meshgrid(xo, yo) # Interpolate zzo = krig(xxi.ravel(), yyi.ravel(), zzi.ravel(), xxo.ravel(), yyo.ravel()) zzo.shape = xxo.shape # Section P.figure(figsize=(8, 4)) iyis = [3, 4] 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:
rr = P.linspace(0, P.sqrt(xg.ptp()**2+yg.ptp()**2), 100) vv = ock.variogram_func(rr) # Plot P.figure(figsize=(8, 8)) axis = [xg.min(), xg.max(), yg.min(), yg.max()] kwmm = dict(vmin=zi.min(), vmax=zi.max()) kwsc = dict(lw=0.2, **kwmm) ax = P.subplot(221, aspect=1) P.imshow(zzg, extent=axis, interpolation='bilinear', origin='lower', alpha=.2, **kwmm) P.scatter(xi, yi, c=zi, s=20, lw=0.2, **kwmm) P.axis(axis) P.title('Input points') P.subplot(222, aspect=1) P.scatter(xo, yo, c=zo, s=40, lw=0.2, **kwmm) P.axis(axis) P.title('Interpolated points') P.subplot(223, aspect=1) P.scatter(xo, yo, c=zoe, s=40, lw=0.2, vmin=0, vmax=100) P.axis(axis) P.colorbar(shrink=.8) P.title('Relative interpolation error') P.subplot(224) P.plot(rr, vv) P.xlabel('Distance') P.ylabel('Error') P.title('Fitted variogram') P.tight_layout() savefigs(code_file_name(), verbose=False) P.close()
# Format and convert xx, yy = N.meshgrid(N.arange(-2, 4, 0.25), N.arange(-1, 5, 0.25)) nxy = xx.shape xx.shape = -1 yy.shape = -1 pp, qq = [], [] for x, y in zip(xx, yy): p, q = curv2rect(x1,x2,x3,x4,y1,y2,y3,y4,x,y) pp.append(p) qq.append(q) pp = N.array(pp) qq = N.array(qq) # Plot xp = [x1, x2, x3, x4, x1] yp = [y1, y2, y3, y4, y1] P.subplot(211) levels = N.array([-10, 0, 1, 10.]) o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), pp.reshape(nxy), levels=levels) P.colorbar(o) P.plot(xp, yp, 'k') P.subplot(212) o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), qq.reshape(nxy), levels=levels) P.colorbar(o) P.plot(xp, yp, 'k') figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile) P.close()
pp, qq = [], [] for x, y in zip(xx, yy): p, q = curv2rect(x1, x2, x3, x4, y1, y2, y3, y4, x, y) pp.append(p) qq.append(q) pp = N.array(pp) qq = N.array(qq) # Plot xp = [x1, x2, x3, x4, x1] yp = [y1, y2, y3, y4, y1] P.subplot(211) levels = N.array([-10, 0, 1, 10.]) o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), pp.reshape(nxy), levels=levels) P.colorbar(o) P.plot(xp, yp, 'k') P.subplot(212) o = P.contourf(xx.reshape(nxy), yy.reshape(nxy), qq.reshape(nxy), levels=levels) P.colorbar(o) P.plot(xp, yp, 'k') figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile) P.close()
"""Test the fortran function :f:func:`closest2d`""" from vcmq import N, P, code_file_name, P, os, rotate_grid, add_grid from vacumm.misc.grid._interp_ import closest2d # Input grid grid = rotate_grid((N.arange(5), N.arange(4)), 30) xxi = grid.getLongitude()[:].filled() yyi = grid.getLatitude()[:].filled() # Input random points N.random.seed(0) np = 100 xxo = N.random.random(np)*(xxi.max()-xxi.min()) + xxi.min() yyo = N.random.random(np)*(yyi.max()-yyi.min()) + yyi.min() # Closest for xo, yo in zip(xxo, yyo): i,j = closest2d(xxi, yyi, xo, yo, nogeo=0) P.plot([xo, xxi[j-1, i-1]], [yo, yyi[j-1, i-1]], 'k') add_grid(grid, color='k', markersize=10, centers=True, marker='o', alpha=1) P.scatter(xxo, yyo, c='r', s=40, zorder=11) P.title('closest2d') P.axis('image') figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile) P.close()
"""Test the fortran function :f:func:`closest2d`""" from vcmq import N, P, code_file_name, P, os, rotate_grid, add_grid from vacumm.misc.grid._interp_ import closest2d # Input grid grid = rotate_grid((N.arange(5), N.arange(4)), 30) xxi = grid.getLongitude()[:].filled() yyi = grid.getLatitude()[:].filled() # Input random points N.random.seed(0) np = 100 xxo = N.random.random(np) * (xxi.max() - xxi.min()) + xxi.min() yyo = N.random.random(np) * (yyi.max() - yyi.min()) + yyi.min() # Closest for xo, yo in zip(xxo, yyo): i, j = closest2d(xxi, yyi, xo, yo, nogeo=0) P.plot([xo, xxi[j - 1, i - 1]], [yo, yyi[j - 1, i - 1]], 'k') add_grid(grid, color='k', markersize=10, centers=True, marker='o', alpha=1) P.scatter(xxo, yyo, c='r', s=40, zorder=11) P.title('closest2d') P.axis('image') figfile = code_file_name(ext='png') if os.path.exists(figfile): os.remove(figfile) P.savefig(figfile) P.close()